標籤:windows azure下的linux vm密碼重設
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工具登陸
650) this.width=650;" title="clip_image002" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image002" src="http://s3.51cto.com/wyfs02/M00/53/F9/wKiom1R1OP_g4mmBAADmu8Qj0C4373.jpg" height="303" />
建立linux 電腦
650) this.width=650;" title="clip_image004" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image004" src="http://s3.51cto.com/wyfs02/M01/53/F9/wKiom1R1OP-Tbb5NAADlLGUccV8439.jpg" height="308" />
預設開放的連接埠
650) this.width=650;" title="clip_image006" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image006" src="http://s3.51cto.com/wyfs02/M02/53/F9/wKiom1R1OP_xe5uPAADlX-3YWEo150.jpg" height="321" />
預設也是安裝代理的
650) this.width=650;" title="clip_image008" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image008" src="http://s3.51cto.com/wyfs02/M00/53/F9/wKiom1R1OP-wcVm4AADRKrVfGsI833.jpg" height="334" />
正在建立vm
650) this.width=650;" title="clip_image010" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image010" src="http://s3.51cto.com/wyfs02/M01/53/F9/wKiom1R1OP_itJKQAADP3AOmpkI011.jpg" height="282" />
我們通過xshell來登陸
650) this.width=650;" title="clip_image012" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image012" src="http://s3.51cto.com/wyfs02/M02/53/F9/wKiom1R1OP_CjHMFAAId6hk93hU795.jpg" height="496" />
我們知道ubuntu預設root是不啟用的,所以我們需要給root設定一個密碼,然後切換到root
Sudo passwd root
650) this.width=650;" title="clip_image013" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image013" src="http://s3.51cto.com/wyfs02/M00/53/F7/wKioL1R1OYGjcTs9AADnjyXbDnw390.jpg" height="109" />
然後通過
su root
切換到root下
650) this.width=650;" title="clip_image014" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image014" src="http://s3.51cto.com/wyfs02/M01/53/F7/wKioL1R1OYGgYbKrAADE6JF-By4454.jpg" height="101" />
查看有那些使用者可以登陸該系統
650) this.width=650;" title="clip_image015" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image015" src="http://s3.51cto.com/wyfs02/M02/53/F7/wKioL1R1OYHzp4MnAAEk4pkJF5Q446.jpg" height="151" />
我系統下有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
650) this.width=650;" title="clip_image017" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image017" src="http://s3.51cto.com/wyfs02/M00/53/F7/wKioL1R1OYHDZzFsAAEXDjlx8vQ645.jpg" height="340" />
運行windows azure powershell
650) this.width=650;" title="clip_image019" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image019" src="http://s3.51cto.com/wyfs02/M01/53/F7/wKioL1R1OYGxXYR1AAD5Kwz_KWg628.jpg" height="356" />
匯入訂閱檔案
650) this.width=650;" title="clip_image021" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image021" src="http://s3.51cto.com/wyfs02/M01/53/F9/wKiom1R1OQDi3xn8AAD8KZ4egr0632.jpg" height="353" />
訂閱檔案匯入成功。
650) this.width=650;" title="clip_image023" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image023" src="http://s3.51cto.com/wyfs02/M02/53/F7/wKioL1R1OYGiMDGiAAFpsXPdtBM478.jpg" height="349" />
因為我環境下有多個訂閱,所以我們需要設定預設的訂閱
650) this.width=650;" title="clip_image025" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image025" src="http://s3.51cto.com/wyfs02/M00/53/F7/wKioL1R1OYKgv5oQAAGJvBZ-DrA067.jpg" height="357" />
檢查Azure PowerShell中預設的Azure賬戶是不是自己想要操作的賬戶:
輸入命令:
Get-AzureSubscription –Default
,檢查出來的結果中會出現Subscription ID,查看一下這個Sub ID是否是自己Azure管理平台上的訂閱ID.
如果Sub ID不同,那麼需要重新設定預設賬戶(如果您是首次操作,那麼應該會是相同的)。更改命令:
Select- Select-AzureSubscription -SubscriptionName "添加賬戶名" -Default
650) this.width=650;" title="clip_image027" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image027" src="http://s3.51cto.com/wyfs02/M01/53/F7/wKioL1R1OYKA8NyVAACErBLGXV8607.jpg" height="116" />
註:如果一個訂閱檔案下有多個訂閱id的話,我們也可以選擇修改指定的訂閱id為預設
650) this.width=650;" title="clip_image029" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image029" src="http://s3.51cto.com/wyfs02/M02/53/F7/wKioL1R1OYLieKjyAACaNibWljo872.jpg" height="118" />
修改後,我們輸入命令
Get-AzureVM
(查到自己的虛擬機器名稱及雲端服務名稱)
650) this.width=650;" title="clip_image031" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image031" src="http://s3.51cto.com/wyfs02/M00/53/F7/wKioL1R1OYKCqlQCAAB0x14a9Vg036.jpg" height="159" />
輸入命令
$VM = Get-AzureVM -ServiceName "xxxx" -Name "mxxx"
定義一個變數vm,然後查看當前服務的資訊
#指定虛擬機器
$vm = Get-AzureVM -ServiceName ‘MyServiceName’ -Name ‘MyVMName’
650) this.width=650;" title="clip_image033" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image033" src="http://s3.51cto.com/wyfs02/M01/53/F7/wKioL1R1OYLxzXZsAABS9ZS41Tk687.jpg" height="54" />
(查看輸出結果是否是‘True’) 如果安裝代理,返回的結果為true
$vm.GetInstance().ProvisionGuestAgent = $true
650) this.width=650;" title="clip_image035" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image035" src="http://s3.51cto.com/wyfs02/M02/53/F7/wKioL1R1OYLy6O2YAACUPoiWhoU885.jpg" height="221" />
#輸入您當前的使用者名稱和新密碼
$UserName = "CurrentName"$Password = "NewPassword"$PrivateConfig = ‘{"username":"‘+ $UserName + ‘", "password":"‘ + $Password + ‘""‘
650) this.width=650;" title="clip_image037" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image037" src="http://s3.51cto.com/wyfs02/M02/53/F9/wKiom1R1OQHSc6aEAAClM7Wysmc899.jpg" height="168" />
#開始執行
$ExtensionName = ‘VMAccessForLinux‘$Publisher = ‘Microsoft.OSTCExtensions‘$Version = ‘1.0‘Set-AzureVMExtension -ExtensionName $ExtensionName -VM $vm -Publisher $Publisher -Version $Version -PrivateConfiguration $PrivateConfig | Update-AzureVM
650) this.width=650;" title="clip_image039" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image039" src="http://s3.51cto.com/wyfs02/M00/53/F7/wKioL1R1OYKSxAU4AAEObR8dja4629.jpg" height="182" />
通過新密碼登陸。
650) this.width=650;" title="clip_image041" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image041" src="http://s3.51cto.com/wyfs02/M00/53/F9/wKiom1R1OQGDwxZfAAD9cflcd1k275.jpg" height="285" />
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
650) this.width=650;" title="clip_image043" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clip_image043" src="http://s3.51cto.com/wyfs02/M01/53/F9/wKiom1R1OQGCt1u8AACRC6JrjPE513.jpg" height="232" />
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
本文出自 “高文龍” 部落格,請務必保留此出處http://gaowenlong.blog.51cto.com/451336/1582670
Windows azure下的Linux vm密碼重設