Computer >> Hướng Dẫn Máy Tính >  >> Phần Mềm >> Máy Ảo

Đăng nhập Amazon Linux 2 VM: Hướng dẫn Truy cập An toàn & Thiết lập Mật khẩu

Bạn có thể đã nghe về Amazon Linux 2. Đây là OS của AWS, được Amazon phát triển. Nó được cung cấp miễn phí, và bạn thậm chí có virtual machine appliance để test. Đó là những gì tôi làm, như tôi chỉ trong bài viết về chủ đề này.

Một rắc rối tôi gặp trong test là login. Thông thường, bạn dùng SSH để login vào EC2 instance. Nhưng bạn dùng gì cho một VM essentially offline? Tôi không tìm thấy bất kỳ combo root/ec2-user nào online, và phương pháp thường thay đổi password trong single mode không chạy. Do đó hướng dẫn này. Nó sẽ chỉ cho bạn cách manually change login credentials cho Amazon Linux 2 VM của bạn, để bạn có thể bắt đầu testing. Theo chân mình nhé.

Login Prompt

Vâng, download image. Boot. Login. What now?

Single Mode

Long-time Linux user sẽ nhớ trick. Force system vào single mode, remount root as writable, change password, reboot. Chúng ta đều làm này nhiều lần. Thương thay, không còn possible.

Tôi thử append init=/bin/bash vào boot options trong boot menu, và system kernel panic. Tôi không chắc nếu đây là vấn đề kernel, Tatsache Amazon Linux dùng glorious systemd, hoặc something else, nhưng old and proven method không help.

Đọc online, bên cạnh million bin-bash entry không yield anything new, tôi tìm thấy detailed article về cách change password cho RHEL version 7. Tutorial cũng mention philosophical concept hơn về change transitioning từ RHEL 6 sang RHEL 7, và fact này là part of RHCSA exam. I mean what? Where has simplicity gone?

Anyway, tôi promise you a rant, but như bài viết states, thêm rd.break vào kernel line might help, và vì Amazon Linux 2 based on RHEL, tôi nghĩ let's do it. Only it did nothing. Amazon Linux blithely boot normally. This did not yield desired results, either.

Amazon có một đoạn rất ngắn và vague giải thích cách làm. Họ nói bạn nên tạo cloud-init configuration ISO, và sau đó refer bạn đến virtualization platform documentation. Not very helpful, I'm afraid.

Mount VDI Thủ công

Chúng ta đã nói về này trong quá khứ. Tôi chỉ cho bạn cách mount virtual machine disk và partition as loopback device, để bạn có thể edit filesystem contents. Chúng ta dùng partx để clone KVM machine. Chúng ta sẽ làm same thing here, và tôi sẽ chỉ cho bạn yet another way to mount và present virtual machine image filesystems.

Chúng ta cần tool qemu-nbd, làm similar trick. Utility này available as part of qemu-kvm package trong most distro, nên bạn cài nó. Sau đó, dùng tool. Nếu execution fails, bạn có thể cần manually load nbd (network block device) module vào memory. Vì vậy, sequence of commands (as sudo or root):

<package manager> install qemu-kvm
modprobe nbd
qemu-nbd -c /dev/nbd0 <name of virtual machine vdi hard disk>

Locate download Amazon Linux 2 vdi image và map nó vào command trên. Bạn không specifically cần dùng /dev/nbd0, bạn có thể dùng any number, nhưng easier và cleaner để làm sequentially.

Khi block device đã mapped, bạn sẽ tìm thấy partitions của nó dưới /dev/nbd0p*. Số sẽ match actual hard disk partition layout, nói p1, p2, etc. Lại, này almost identical to what we did with partx. Bước tiếp theo là mount desired partition somewhere:

mount /dev/nbd0pX /mountpoint

Khi bạn đã làm command này, bạn giờ có contents của Amazon Linux 2 vdi filesystem (partition) exposed dưới desired mountpoint (ví dụ /mnt), và bây giờ chúng ta có thể manipulate nó.

Edit File /etc/shadow

Bây giờ chúng ta muốn change password. Cách chúng ta làm là sau. Trên native Linux, cái actually running VM, tạo new user. Bạn có thể gọi nó whatever you want. Giả sử amazon. Tạo password. Mở file /etc/shadow. Bạn sẽ notice password được encrypted và presented as a long hash, ở giữa hai colon delimiter. Great. Đây là những gì chúng ta cần. Giống như:

amazon:$123456$dedoimedo:11111:0:99999:6:::

Bây giờ navigate đến Amazon Linux 2 filesystem mountpoint từ trước, nói /mnt và open shadow file của nó trong text editor:

<text editor> /mnt/etc/shadow

Bạn sẽ notice default user thực sự gọi là ec2-user. Dù bạn không biết, bây giờ bạn biết. Nó cũng có some hash đó cho password. Tôi không biết nó là gì, và không matter. Replace hash với hash từ local amazon user. Save file, unmount vdi.

Reboot, Login, Work, and Enjoy

Start VM lại. Khi bạn đến login prompt, identify yourself correctly với right user name và password mà chúng ta vừa plant đó. Bạn sẽ đã logged in, và bạn có thể bắt đầu làm việc với VM appliance của bạn. Bây giờ, chúng ta làm với Amazon Linux, nhưng trick này applies to ALL Linux distro và filesystems out there. Nếu bạn không thể run system maintenance normally, bạn có thể dùng trick này để get around limitation và make necessary changes đến filesystem. This is quite useful cho virtual machines. Chúng ta làm này với KVM và bây giờ với VirtualBox, cũng.

Kết luận

Nếu bạn keen on testing Amazon Linux 2 nhưng bạn hitting login credentials snag, và bạn không feel like tạo own ISO images, hoặc trying to figure out full, proper way to use EC2 stuff, bạn có thể dùng partx hoặc qemu-nbd để mount virtual hard disk, và manually edit shadow file. This is a guaranteed workaround, in case booting into single mode or rescue mode hoặc whatever does not cut it cho bạn.

Amazon Linux 2 trông như một OS vui với several rather interesting features, và nó sẽ là a shame to miss them because of a technical/bureaucratic bungle like the initial login/password. I mean there's no reason why there ought not to be a default set, like any which distro. Plus the generic changes in the Linux infrastructure, vis-a-vis init=/bin/bash sure do not help. Hy vọng, bài viết này cung cấp cho bạn necessary technical know how và adventurous spur để start your journey. Best of all, it's distro-agnostic. Any which virtual machine appliance, any which filesystem, you'll be able to make the necessary changes for your testing. Have fun exploring.

Chúc vui.