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

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

[复制链接]

尚未签到

发表于 2018-6-22 09:48:07 | 显示全部楼层 |阅读模式
  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工具登陆
DSC0000.jpg

  创建linux 计算机
DSC0001.jpg

  默认开放的端口
DSC0002.jpg

  默认也是安装代理的
DSC0003.jpg

  正在创建vm
DSC0004.jpg

  我们通过xshell来登陆
DSC0005.jpg

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

  然后通过
su root  切换到root下
DSC0007.jpg

  查看有那些用户可以登陆该系统
DSC0008.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
DSC0009.jpg

  运行windows azure powershell
DSC00010.jpg

  导入订阅文件
DSC00011.jpg

  订阅文件导入成功。
DSC00012.jpg

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

  检查Azure PowerShell中默认的Azure账户是不是自己想要操作的账户:
  输入命令:
Get-AzureSubscription –Default
  ,检查出来的结果中会出现Subscription>
  如果Sub>Select- Select-AzureSubscription -SubscriptionName "添加账户名" -Default
DSC00014.jpg

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

  修改后,我们输入命令
Get-AzureVM  (查到自己的虚拟机名称及云服务名称)
DSC00016.jpg

  输入命令
$VM = Get-AzureVM -ServiceName "xxxx" -Name "mxxx"  定义一个变量vm,然后查看当前服务的信息
  #指定虚拟机
$vm = Get-AzureVM -ServiceName ‘MyServiceName’ -Name ‘MyVMName’
DSC00017.jpg

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

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

  #开始执行
$ExtensionName = 'VMAccessForLinux'  
$Publisher = 'Microsoft.OSTCExtensions'
  
$Version = '1.0'
  
Set-AzureVMExtension -ExtensionName $ExtensionName -VM $vm -Publisher $Publisher -Version $Version -PrivateConfiguration $PrivateConfig | Update-AzureVM
DSC00020.jpg

  通过新密码登陆。
DSC00021.jpg

  windows azure命令结合:
  http://msdn.microsoft.com/en-us/library/dn495240.aspx
  注意:官网上的代码标点符号有问题,所以执行起来会有问题:
  我们可以将以上代码写为一个.ps文件去执行即可。
$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
DSC00022.jpg

  2. Reset the SSH key only
#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
#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.
#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”;
$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-529060-1-1.html 上篇帖子: SCOM 2012 R2_自动恢复服务脚本 下篇帖子: 在Windows下写PHP的C扩展
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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