使用執行命令在 Linux VM 中執行殼層指令碼Run shell scripts in your Linux VM with Run Command

執行命令會使用 VM 代理程式在 Azure Linux VM 中執行殼層指令碼。Run Command uses the VM agent to run shell scripts within an Azure Linux VM. 這些指令碼可用於一般機器或應用程式管理,且可用來快速診斷和修復虛擬機器存取和網路問題,並使虛擬機器恢復正常狀態。These scripts can be used for general machine or application management, and can be used to quickly diagnose and remediate VM access and network issues and get the VM back to a good state.

優點Benefits

有多個選項可以用來存取您的虛擬機器。There are multiple options that can be used to access your virtual machines. 執行命令可以使用虛擬機器代理程式,從遠端在虛擬機器上執行指令碼。Run Command can run scripts on your virtual machines remotely using the VM agent. 可以透過 Azure 入口網站、REST API 或適用於 Linux VM 的 Azure CLI 來執行命令。Run Command can be used through the Azure portal, REST API, or Azure CLI for Linux VMs.

您可以要在虛擬機器中執行指令碼的所有情況下使用此功能;且針對因網路或系統管理使用者設定不適當而未開啟 RDP 或 SSH 連接埠的虛擬機器,只有使用此功能才能進行疑難排解和修復。This capability is useful in all scenarios where you want to run a script within a virtual machines, and is one of the only ways to troubleshoot and remediate a virtual machine that doesn't have the RDP or SSH port open due to improper network or administrative user configuration.

限制Restrictions

以下列出在使用執行命令時所受到的限制。The following are a list of restrictions that are present when using Run Command.

  • 輸出僅限於最後 4096 個位元組Output is limited to the last 4096 bytes
  • 執行指令碼的最短時間大約 20 秒The minimum time to run a script about 20 seconds
  • 指令碼依預設會以提高權限的使用者身分在 Linux 上執行Scripts run by default as elevated user on Linux
  • 一次可執行一個指令碼One script at a time may run
  • 不支援提示資訊 (互動模式) 的指令碼。Scripts that prompt for information (interactive mode) are not supported.
  • 您無法取消執行中的指令碼You cannot cancel a running script
  • 指令碼可以執行的最長時間是 90 分鐘,經過這段時間後會逾時The maximum time a script can run is 90 minutes, after which it will time out
  • 需要有虛擬機器的輸出連線,才能傳回指令碼結果。Outbound connectivity from the VM is required to return the results of the script.

注意

Run 命令需要連線 (連接埠 443) 到 Azure 公用 IP 位址,才能正確運作。To function correctly, the Run Command requires connectivity (port 443) to Azure public IP addresses. 如果擴充功能無法存取這些端點,指令碼可能會執行成功,但不會傳回結果。If the extension doesn't have access to these endpoints, the scripts may run successfully but not return the results. 如果您要封鎖虛擬機器上的流量,可以使用服務標籤,以便利用 AzureCloud 標籤來允許送至 Azure 公用 IP 位址的流量。If you are blocking traffic on the virtual machine, you can use service tags to allow traffic to Azure public IP addresses by using the AzureCloud tag.

Azure CLIAzure CLI

以下是使用 az vm run-command 命令在 Azure Linux VM 上執行殼層指令碼的範例。The following is an example using the az vm run-command command to run a shell script on an Azure Linux VM.

az vm run-command invoke -g myResourceGroup -n myVm --command-id RunShellScript --scripts "sudo apt-get update && sudo apt-get install -y nginx"

注意

若要以不同的使用者身分執行命令,您可以使用 sudo -u 指定要使用的使用者帳戶。To run commands as a different user, you can use sudo -u to specify a user account to use.

Azure 入口網站Azure portal

瀏覽至 Azure 中的 VM,並選取 [作業] 下的 [執行命令]。Navigate to a VM in Azure and select Run command under OPERATIONS. 您會看到將可在虛擬機器上執行之命令的清單。You are presented with a list of the available commands to run on the VM.

執行命令清單

選擇要執行的命令。Choose a command to run. 有些命令可能會有選擇性或必要的輸入參數。Some of the commands may have optional or required input parameters. 對於這些命令,參數會顯示為可讓您提供輸入值的文字欄位。For those commands the parameters are presented as text fields for you to provide the input values. 對於每個命令,您可以展開 [檢視指令碼] 檢視執行中的指令碼。For each command you can view the script that is being run by expanding View script. RunShellScript 不同於其他命令,因為它可讓您提供您自己的自訂指令碼。RunShellScript is different from the other commands as it allows you to provide your own custom script.

注意

內建命令是無法編輯的。The built-in commands are not editable.

選擇命令之後,按一下 [執行] 執行指令碼。Once the command is chosen, click Run to run the script. 指令碼隨即執行,完成時將在輸出視窗中傳回任何錯誤。The script runs and when complete, returns the output and any errors in the output window. 下列螢幕擷取畫面顯示執行 ifconfig 命令的範例輸出。The following screenshot shows an example output from running the ifconfig command.

執行命令指令碼輸出

可用的命令Available Commands

下表顯示 Linux VM 的可用命令清單。This table shows the list of commands available for Linux VMs. RunShellScript 命令可用來執行您想要的任何自訂指令碼。The RunShellScript command can be used to run any custom script you want.

名稱Name 說明Description
RunShellScriptRunShellScript 執行 Linux 殼層指令碼。Executes a Linux shell script.
ifconfigifconfig 取得所有網路介面的組態。Get the configuration of all network interfaces.

限制於執行命令的存取Limiting access to Run Command

列出執行命令,或顯示命令的詳細資料,都需要 Microsoft.Compute/locations/runCommands/read 權限,這是內建讀者角色或更高角色擁有的權限。Listing the run commands or showing the details of a command require the Microsoft.Compute/locations/runCommands/read permission, which the built-in Reader role and higher have.

執行命令需要 Microsoft.Compute/virtualMachines/runCommand/action 權限,這是參與者角色或更高角色擁有的權限。Running a command requires the Microsoft.Compute/virtualMachines/runCommand/action permission, which the Contributor role and higher have.

您可以使用其中一個內建角色或建立自訂角色使用執行命令。You can use one of the built-in roles or create a custom role to use Run Command.

後續步驟Next steps

請參閱在 Linux VM 中執行指令碼,了解如何以其他方式從遠端在您的 VM 中執行指令碼和命令。See, Run scripts in your Linux VM to learn about other ways to run scripts and commands remotely in your VM.