设为首页 收藏本站
查看: 2683|回复: 0

[经验分享] Windows azure下的Linux vm密码重置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-11-27 08:24:34 | 显示全部楼层 |阅读模式
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工具登陆
wKiom1R1OP_g4mmBAADmu8Qj0C4373.jpg
创建linux 计算机
wKiom1R1OP-Tbb5NAADlLGUccV8439.jpg
默认开放的端口
wKiom1R1OP_xe5uPAADlX-3YWEo150.jpg
默认也是安装代理的
wKiom1R1OP-wcVm4AADRKrVfGsI833.jpg
正在创建vm
wKiom1R1OP_itJKQAADP3AOmpkI011.jpg
我们通过xshell来登陆
wKiom1R1OP_CjHMFAAId6hk93hU795.jpg
我们知道ubuntu默认root是不启用的,所以我们需要给root设置一个密码,然后切换到root
1
Sudo passwd root



wKioL1R1OYGjcTs9AADnjyXbDnw390.jpg
然后通过
1
su root



切换到root下

wKioL1R1OYGgYbKrAADE6JF-By4454.jpg
查看有那些用户可以登陆该系统
wKioL1R1OYHzp4MnAAEk4pkJF5Q446.jpg
我系统下有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
wKioL1R1OYHDZzFsAAEXDjlx8vQ645.jpg
运行windows azure powershell
wKioL1R1OYGxXYR1AAD5Kwz_KWg628.jpg
导入订阅文件
wKiom1R1OQDi3xn8AAD8KZ4egr0632.jpg
订阅文件导入成功。
wKioL1R1OYGiMDGiAAFpsXPdtBM478.jpg
因为我环境下有多个订阅,所以我们需要设置默认的订阅
wKioL1R1OYKgv5oQAAGJvBZ-DrA067.jpg
检查Azure PowerShell中默认的Azure账户是不是自己想要操作的账户:
输入命令:
1
Get-AzureSubscription –Default



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

如果Sub ID不同,那么需要重新设置默认账户(如果您是首次操作,那么应该会是相同的)。更改命令:
1
Select- Select-AzureSubscription -SubscriptionName "添加账户名" -Default



wKioL1R1OYKA8NyVAACErBLGXV8607.jpg
注:如果一个订阅文件下有多个订阅id的话,我们也可以选择修改指定的订阅id为默认
wKioL1R1OYLieKjyAACaNibWljo872.jpg
修改后,我们输入命令
1
Get-AzureVM



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

wKioL1R1OYKCqlQCAAB0x14a9Vg036.jpg
输入命令
1
$VM = Get-AzureVM -ServiceName "xxxx" -Name "mxxx"



定义一个变量vm,然后查看当前服务的信息
#指定虚拟机
1
$vm = Get-AzureVM -ServiceName ‘MyServiceName’ -Name ‘MyVMName’



wKioL1R1OYLxzXZsAABS9ZS41Tk687.jpg
(查看输出结果是否是‘True’) 如果安装代理,返回的结果为true
1
$vm.GetInstance().ProvisionGuestAgent = $true



wKioL1R1OYLy6O2YAACUPoiWhoU885.jpg
#输入您当前的用户名和新密码
1
2
3
$UserName = "CurrentName"
$Password = "NewPassword"
$PrivateConfig = '{"username":"'+ $UserName + '", "password":"' +  $Password + '""'



wKiom1R1OQHSc6aEAAClM7Wysmc899.jpg
#开始执行
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



wKioL1R1OYKSxAU4AAEObR8dja4629.jpg
通过新密码登陆。
wKiom1R1OQGDwxZfAAD9cflcd1k275.jpg
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



wKiom1R1OQGCt1u8AACRC6JrjPE513.jpg
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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-33809-1-1.html 上篇帖子: Windows Azure体验之新建虚拟网络 下篇帖子: Windows Azure上通过挂载磁盘的方式修改Linux系统登录密码 Windows Linux 密码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表