如何在 Azure VM 上重置本地 Linux 密码How to reset local Linux password on Azure VMs

本文介绍了几种重置本地 Linux 虚拟机 (VM) 密码的方法。This article introduces several methods to reset local Linux Virtual Machine (VM) passwords. 如果用户帐户已过期或者只是想要创建新帐户,则可以使用以下方法来创建新的本地管理员帐户并重新获得对 VM 的访问权限。If the user account is expired or you just want to create a new account, you can use the following methods to create a new local admin account and re-gain access to the VM.

症状Symptoms

无法登录到 VM 时会收到一条消息,指示所使用的密码不正确。You can't log in to the VM, and you receive a message that indicates that the password that you used is incorrect. 此外,无法在 Azure 门户上使用 VMAgent 重置密码。Additionally, you can't use VMAgent to reset your password on the Azure portal.

手动密码重置过程Manual password reset procedure

备注

以下步骤不适用于包含非托管磁盘的 VM。The following steps does not apply to the VM with unmanaged disk.

  1. 为受影响的 VM 的 OS 磁盘拍摄快照,从快照创建磁盘,然后将该磁盘附加到故障排除 VM。Take a snapshot for the OS disk of the affected VM, create a disk from the snapshot, and then attach the disk to a troubleshoot VM. 有关详细信息,请参阅通过使用 Azure 门户将 OS 磁盘附加到恢复 VM 来对 Windows VM 进行故障排除For more information, see Troubleshoot a Windows VM by attaching the OS disk to a recovery VM using the Azure portal.

  2. 使用远程桌面连接到故障排除 VM。Connect to the troubleshooting VM using Remote Desktop.

  3. 在故障排除 VM 上运行以下 SSH 命令,成为超级用户。Run the following SSH command on the troubleshooting VM to become a super-user.

    sudo su
    
  4. 运行 fdisk -l ,或查看系统日志以查找新附加的磁盘。Run fdisk -l or look at system logs to find the newly attached disk. 找到要装载的驱动器名称。Locate the drive name to mount. 然后在临时 VM 上,查找相关的日志文件。Then on the temporal VM, look in the relevant log file.

    grep SCSI /var/log/kern.log (ubuntu)
    grep SCSI /var/log/messages (centos, suse, oracle)
    

    下面是 grep 命令的输出示例:The following is example output of the grep command:

    kernel: [ 9707.100572] sd 3:0:0:0: [sdc] Attached SCSI disk
    
  5. 创建名为 tempmount 的装入点。Create a mount point called tempmount .

    mkdir /tempmount
    
  6. 在该装入点上装载 OS 磁盘。Mount the OS disk on the mount point. 通常需要装载 sdc1 或 sdc2 。You usually need to mount sdc1 or sdc2 . 这将取决于损坏的计算机磁盘的 /etc 目录中的托管分区。This will depend on the hosting partition in /etc directory from the broken machine disk.

    mount /dev/sdc1 /tempmount
    
  7. 在进行任何更改之前,请创建核心凭据文件的副本:Create copies of the core credential files before making any changes:

    cp /etc/passwd /etc/passwd_orig    
    cp /etc/shadow /etc/shadow_orig    
    cp /tempmount/etc/passwd /etc/passwd
    cp /tempmount/etc/shadow /etc/shadow 
    cp /tempmount/etc/passwd /tempmount/etc/passwd_orig
    cp /tempmount/etc/shadow /tempmount/etc/shadow_orig
    
  8. 重置所需的用户密码:Reset the user's password that you need:

    passwd <<USER>> 
    
  9. 将已修改的文件移动到断开的计算机磁盘上的正确位置。Move the modified files to the correct location on the broken machine's disk.

    cp /etc/passwd /tempmount/etc/passwd
    cp /etc/shadow /tempmount/etc/shadow
    cp /etc/passwd_orig /etc/passwd
    cp /etc/shadow_orig /etc/shadow
    
  10. 返回到根目录并卸载磁盘。Go back to the root and unmount the disk.

    cd /
    umount /tempmount
    
  11. 在 Azure 门户中,从故障排除 VM 分离该磁盘。In Azure portal, detach the disk from the troubleshooting VM.

  12. 更改受影响 VM 的 OS 磁盘Change the OS disk for the affected VM.

后续步骤Next steps