Windows azure下的Linux vm密码重置

简介:

上一篇我们介绍了windows azure下的windows vm下的密码忘记重置,今天介绍一下,在windows azure下的linux vm的密码重置。

密码忘记有两种方式,一种是知道需要重置的账户密码,一一种是不知道需要重置的密码。

比如上届管理员创建完后工作没交接就走了,本届管理员不知道密码,不知道重置那个密码,所以windows azure就用覆盖的方法来完成从重置密码操作。

先决条件

微软 Azure Linux 代理 2.0.5 或更高版本。请注意,大多数 Azure 虚拟机 Linux 镜像库都包含 2.0.5 版本。您可以通过运行 waagent -version 来确认此版本已安装在虚拟机中。为保证扩展程序最佳用户体验,建议按照本文结尾“其他注意事项”中的步骤更新到最新版本。

Azure PowerShell。请注意,针对扩展程序的跨平台 CLI 支持有望在未来几周内推出。

您想要对您的 VM 重置的新密码或 SSH 密钥。

使用 VMAccess 扩展程序

根据您想要为 VM 重置的内容,VMAccess 的使用有 5 种场景。以下将介绍这些场景和对应的 PowerShell 示例脚本。请注意,您只需为每种场景指定不同的参数,“开始执行” 这行注释之后的第二部分在不同场景中均相同。脚本非常简单。

(如果你使用的是Ubuntu,需要将上述命令中的“waagent” 替换为“walinuxagent”)

注意:1. 如需要更新到其他版本,请参看GitHub说明。2. 运行以上命令需要Root权限。

接下来我们演示一下,我们知道linux一般都通过ssl工具登陆

clip_image002

创建linux 计算机

clip_image004

默认开放的端口

clip_image006

默认也是安装代理的

clip_image008

正在创建vm

clip_image010

我们通过xshell来登陆

clip_image012

我们知道ubuntu默认root是不启用的,所以我们需要给root设置一个密码,然后切换到root

1
Sudo passwd root

clip_image013

然后通过

1
su root

切换到root下

clip_image014

查看有那些用户可以登陆该系统

clip_image015

我系统下有azureuser、root两个用户,我如果root密码忘记了或者azureuser密码忘记了我应该怎么做呢,需要通过powershell azure进行重置。

我们下载了windows azure powershel,那windows azure poershell跟系统自带的windows powershell有什么区别呢,最大的区别就是有没有azure相关的服务模块,再次也不多介绍了,有了powershell 后,我们需要windows azure的订阅文件,我们需要通过访问windows azure的portal界面进行下载或者通过powershell下载。

https://manage.windowsazure.cn/publishsettings/index?client=powershell

clip_image017

运行windows azure powershell

clip_image019

导入订阅文件

clip_image021

订阅文件导入成功。

clip_image023

因为我环境下有多个订阅,所以我们需要设置默认的订阅

clip_image025

检查Azure PowerShell中默认的Azure账户是不是自己想要操作的账户:

输入命令:

1
Get-AzureSubscription  Default

,检查出来的结果中会出现Subscription ID,查看一下这个Sub ID是否是自己Azure管理平台上的订阅ID.

如果Sub ID不同,那么需要重新设置默认账户(如果您是首次操作,那么应该会是相同的)。更改命令:

1
Select-  Select-AzureSubscription  -SubscriptionName  "添加账户名"  - Default

clip_image027

注:如果一个订阅文件下有多个订阅id的话,我们也可以选择修改指定的订阅id为默认

clip_image029

修改后,我们输入命令

1
  Get-AzureVM

(查到自己的虚拟机名称及云服务名称)

clip_image031

输入命令

1
$VM  Get-AzureVM  -ServiceName  "xxxx"  -Name  "mxxx"

定义一个变量vm,然后查看当前服务的信息

#指定虚拟机

1
$vm  Get-AzureVM  -ServiceName ‘MyServiceName’ -Name ‘MyVMName’

clip_image033

(查看输出结果是否是‘True’) 如果安装代理,返回的结果为true

1
$vm .GetInstance().ProvisionGuestAgent =  $true

clip_image035

#输入您当前的用户名和新密码

1
2
3
$UserName  "CurrentName"
$Password  "NewPassword"
$PrivateConfig  '{"username":"' $UserName  '", "password":"'  +   $Password  '""'

clip_image037

#开始执行

1
2
3
4
$ExtensionName  'VMAccessForLinux'
$Publisher  'Microsoft.OSTCExtensions'
$Version  '1.0'
Set-AzureVMExtension  -ExtensionName  $ExtensionName  -VM  $vm  -Publisher  $Publisher  -Version  $Version  -PrivateConfiguration  $PrivateConfig  Update-AzureVM

clip_image039

通过新密码登陆。

clip_image041

windows azure命令结合:

http://msdn.microsoft.com/en-us/library/dn495240.aspx

注意:官网上的代码标点符号有问题,所以执行起来会有问题:

我们可以将以上代码写为一个.ps文件去执行即可。

1
2
3
4
5
6
7
8
9
$vm  Get-AzureVM  -ServiceName ‘?ZZtestlinux’?¥ -Name ‘?ZZtestlinux’?¥
$vm .GetInstance().ProvisionGuestAgent =  $true
$UserName  "azureuser"
$Password  "Password2014"
$PrivateConfig  '{"username":"' $UserName  '", "password":"'  $Password  '"}'
$ExtensionName  'VMAccessForLinux'
$Publisher  'Microsoft.OSTCExtensions'
$Version  '1.0'
Set-AzureVMExtension  -ExtensionName  $ExtensionName  -VM  $vm  -Publisher  $Publisher  -Version  $Version  -PrivateConfiguration  $PrivateConfig  Update-AzureVM

clip_image043

2. Reset the SSH key only

1
2
3
4
5
6
7
8
9
10
11
12
#Sample script to reset your SSH keys
#Identify the VM
$vm  Get-AzureVM  -ServiceName ‘MyServiceName’ -Name ‘MyVMName’
#Enter the current user name and the path of your new public SSH key
$UserName  "CurrentName"
$cert  Get-Content  "CertPath"
$PrivateConfig  '{"username":"'  $UserName  '", "ssh_key":"'  $cert  '"}'
# Begin execution
$ExtensionName  'VMAccessForLinux'
$Publisher  'Microsoft.OSTCExtensions'
$Version  '1.0'
Set-AzureVMExtension  -ExtensionName  $ExtensionName  -VM  $vm  -Publisher  $Publisher  -Version  $Version  -PrivateConfiguration  $PrivateConfig  Update-AzureVM

3. Reset the password and the SSH key

1
2
3
4
5
6
7
8
9
10
11
12
13
#Sample script to reset your password and SSH key
#Identify the VM
$vm  Get-AzureVM  -ServiceName ‘MyServiceName’ -Name ‘MyVMName’
#Enter the new password, and cert path of the new SSH public key, with the current user name
$UserName  "CurrentName"
$Password  "NewPassword"
$cert  Get-Content  "CertPath"
$PrivateConfig  '{"username":"'  $UserName  '", "password": "'  $Password  '", "ssh_key":"'  $cert  '"}'
# Begin execution
$ExtensionName  'VMAccessForLinux'
$Publisher  'Microsoft.OSTCExtensions'
$Version  '1.0'
Set-AzureVMExtension  -ExtensionName  $ExtensionName  -VM  $vm  -Publisher  $Publisher  -Version  $Version  -PrivateConfiguration  $PrivateConfig  Update-AzureVM

4. Create a new sudo user account

If you forget your user name, you can use VMAccess to create a new one with the sudo authority. Note in this case, your original user name and login keys will not be modified, it should still work.

To create a new sudo user with password access, use the script in scenario 1; for creating a new sudo user with SSH key access, use the script in scenario 2; you can also use scenario 3 to create a new user with both access;  remember you need to change the “UserName” to a new user name.

5. Reset the SSH configuration

If the SSH configuration is messed up, you might also lose the access to the VM. You can use VMAccess extension to reset the configuration to default. To do so, you just need to remove all the new access parameters in the configuration (user name, password, or SSH key). The extension will restart the SSH server, open the SSH port on your VM, and reset the SSH configuration to default. The user account (password or SSH keys) of your VM remains unchanged.

Note, The SSH configuration file that get reset is located at /etc/ssh/sshd_config.

1
2
3
4
5
6
7
8
9
#Sample script to reset the SSH configuration on your VM
#Identify the VM
$vm  Get-AzureVM  -ServiceName ‘MyServiceName’ -Name ‘MyVMName’
$PrivateConfig  '{"reset_ssh": "True"}'
# Begin execution
$ExtensionName  'VMAccessForLinux'
$Publisher  'Microsoft.OSTCExtensions'
$Version  '1.0'
Set-AzureVMExtension  -ExtensionName  $ExtensionName  -VM  $vm  -Publisher  $Publisher  -Version  $Version  -PrivateConfiguration  $PrivateConfig  Update-AzureVM

Query the results

The status of the VMAccess extension could be retrieved using Azure PowerShell Cmdlet Get-AzureVM or Get-Deployment.

Access the VM after resetting

After the VMAccess Extension completes resetting the credentials and configurations, you can log on to the instance using the new account name, password or SSH key.

Additional Notes

Note if you only want to reset the password or SSH key for the existing user account, you need to make sure the user name you entered matches the original user name. If you enter a name that is different from your original name, the VMAccess extension will consider this as scenario 4 listed above, and create a new user account.

Known issue

When you run the PowerShell command “Set-AzureVMExtension” on Linux VM, you may hit following error: “Provision Guest Agent must be enabled on the VM object before setting IaaS VM Access Extension”.

Root Cause: when you create the image via portal, the value of guest agent on the VM is not always  set to “True”. If your VM is created using PowerShell, you will not see this issue.

Resolution: Add the following PowerShell command to set the ProvisionGuestAgent to “True”;

1
2
$vm  Get-AzureVM  -ServiceName ‘MyServiceName’ -Name ‘MyVMName’
$vm .GetInstance().ProvisionGuestAgent =  $true



本文转自 高文龙 51CTO博客,原文链接:http://blog.51cto.com/gaowenlong/1582670,如需转载请自行联系原作者
相关文章
|
2月前
|
监控 安全 Linux
在Linux中设定账户密码的安全性策略
这些操作应该由有经验的系统管理员进行,因为不当的配置可能导致无法预期的安全问题或者系统访问问题。此外,提升安全性的同时,也需要考虑到用户的便利性,避免设置过于严苛的政策导致用户体验不佳。通常,强密码策略配合两因素认证(2FA)将大大加强账户的安全性。
164 13
|
2月前
|
Linux 虚拟化 iOS开发
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux
497 0
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux
|
3月前
|
运维 Ubuntu Linux
Linux重置root用户密码
本文详细介绍了Linux系统中root密码重置的核心技能,涵盖主流发行版如RHEL、CentOS、Debian、Ubuntu、Arch、openSUSE等的实操方法。内容包括通过GRUB引导编辑、单用户模式和Live CD救援三种方式重置密码的具体步骤,适配物理机、虚拟机及云服务器环境。文章分步解析了启动拦截、权限获取和密码重置三大阶段,并提供各发行版的实际操作代码示例,帮助管理员快速解决忘记root密码的问题。
|
6月前
|
Unix Linux 编译器
windows下和linux下cmake的规则有区别吗
通过合理使用CMake的条件逻辑和平台特定的配置选项,开发者可以编写更加灵活和健壮的CMake脚本,确保项目在Windows和Linux上的一致性和可移植性。
320 76
|
4月前
|
安全 Ubuntu Linux
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
143 0
Nipper 3.8.0 for Windows & Linux - 网络设备漏洞评估
|
7月前
|
安全 Linux 数据安全/隐私保护
linux root登陆,密码正确但,错误提示su: Authentication failure
通过系统化的排查和解决方案,可以有效应对 `su: Authentication failure` 问题,确保系统安全和用户权限的正确管理。
3168 36
|
7月前
|
安全 网络安全 数据安全/隐私保护
Windows Server 2025 Active Directory 重置用户密码
密码重置是管理员日常任务之一,用户因忘记或多次输错密码导致账户锁定时需进行重置。本文介绍在Active Directory服务器上重置密码的三种方法。
463 3
|
9月前
|
安全 网络协议 Linux
telnet在windows和linux上的使用方法
Telnet是一个简单且强大的网络工具,广泛用于远程管理和网络诊断。尽管存在安全风险,但在受控环境中,Telnet仍然是一个非常有用的工具。通过本文的介绍,您应该能够在Windows和Linux系统上安装并使用Telnet进行各种网络操作。
1374 18
|
9月前
|
Ubuntu 网络协议 Linux
快速部署WSL(Windows Subsystem for Linux)
WSL提供了一种轻量级的方法,使开发者能够在Windows上无缝运行Linux环境。通过本文介绍的步骤,可以快速安装、配置和使用WSL,以满足开发和测试的需求。
1746 8
|
9月前
|
关系型数据库 MySQL Linux
MySQL数据库下载安装教程(Windows&Linux)
本文档详细介绍了MySQL的安装步骤,包括安装前的准备工作、下载安装包、Windows和Linux系统下的具体安装流程,以及如何配置MySQL服务、设置环境变量、启动服务和连接数据库等关键操作。