使用“运行命令”在 Linux VM 中运行 shell 脚本Run shell scripts in your Linux VM by using Run Command

“运行命令”功能使用虚拟机 (VM) 代理在 Azure Linux VM 中运行 shell 脚本。The Run Command feature uses the virtual machine (VM) agent to run shell scripts within an Azure Linux VM. 可以使用这些脚本进行常规计算机或应用程序管理。You can use these scripts for general machine or application management. 它们可以帮助你快速诊断和修正 VM 访问与网络问题,使 VM 恢复正常状态。They can help you to quickly diagnose and remediate VM access and network issues and get the VM back to a good state.


可通过多种方式访问虚拟机。You can access your virtual machines in multiple ways. “运行命令”可以使用 VM 代理在虚拟机上以远程方式运行脚本。Run Command can run scripts on your virtual machines remotely by using the VM agent. 对于 Linux VM,可以通过 REST APIAzure CLI 使用“运行命令”。You use Run Command through REST API, or Azure CLI for Linux VMs.

在需要在虚拟机中运行脚本的所有方案中,此功能都很有用。This capability is useful in all scenarios where you want to run a script within a virtual machine. 它是排查和修正因网络或管理用户配置错误而未打开 RDP 或 SSH 端口的虚拟机的唯一方法。It's one of the only ways to troubleshoot and remediate a virtual machine that doesn't have the RDP or SSH port open because of improper network or administrative user configuration.


使用“运行命令”时存在以下限制:The following restrictions apply when you're using Run Command:

  • 输出限制为最后的 4,096 个字节。Output is limited to the last 4,096 bytes.
  • 运行脚本的最短时间大约为 20 秒。The minimum time to run a script is about 20 seconds.
  • 在 Linux 上,脚本默认情况下以提升用户的身份运行。Scripts run by default as an elevated user on Linux.
  • 一次只能运行一个脚本。You can run one script at a time.
  • 不支持提示输入信息(交互模式)的脚本。Scripts that prompt for information (interactive mode) are not supported.
  • 无法取消正在运行的脚本。You can't cancel a running script.
  • 脚本最多可以运行 90 分钟。The maximum time a script can run is 90 minutes. 90 分钟后脚本将会超时。After that, the script will time out.
  • 需要从 VM 建立出站连接才能返回脚本的结果。Outbound connectivity from the VM is required to return the results of the script.


若要正常工作,“运行命令”需要连接(通过端口 443)到 Azure 公共 IP 地址。To function correctly, Run Command requires connectivity (port 443) to Azure public IP addresses. 如果扩展无法访问这些终结点,则脚本可能会成功运行,但不会返回结果。If the extension doesn't have access to these endpoints, the scripts might run successfully but not return the results. 如果要阻止虚拟机上的流量,可以使用服务标记以通过 AzureChinaCloud 标记允许流量发往 Azure 公共 IP 地址。If you're blocking traffic on the virtual machine, you can use service tags to allow traffic to Azure public IP addresses by using the AzureChinaCloud tag.

可用的命令Available commands

下表显示了可用于 Linux VM 的命令的列表。This table shows the list of commands available for Linux VMs. 可以使用 RunShellScript 命令运行所需的任何自定义脚本。You can use the RunShellScript command to run any custom script that you want. 使用 Azure CLI 或 PowerShell 运行命令时,为 --command-id-CommandId 参数提供的值必须是下面列出的值之一。When you're using the Azure CLI or PowerShell to run a command, the value that you provide for the --command-id or -CommandId parameter must be one of the following listed values. 如果指定的值不是可用的命令,将会收到以下错误:When you specify a value that is not an available command, you receive this error:

The entity was not found in this Azure location
名称Name 说明Description
RunShellScriptRunShellScript 运行 Linux shell 脚本。Runs a Linux shell script.
ifconfigifconfig 获取所有网络接口的配置。Gets the configuration of all network interfaces.

Azure CLIAzure CLI

以下示例使用 az vm run-command 命令在 Azure Linux VM 上运行 shell 脚本。The following example uses 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 "apt-get update && apt-get install -y nginx"


若要以另一个用户的身份运行命令,请输入 sudo -u 以指定用户帐户。To run commands as a different user, enter sudo -u to specify a user account.

Azure 门户Azure portal

转到 Azure 门户 中的某个 VM,然后在“操作”下选择“运行命令”。 Go to a VM in the Azure portal and select Run command under OPERATIONS. 你将看到可以在 VM 上运行的可用命令的列表。You see a list of the available commands to run on the VM.


选择要运行的命令。Choose a command to run. 某些命令可能有可选或必需的输入参数。Some of the commands might 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's being run by expanding View script. RunShellScript 不同于其他命令,因为它允许你提供自己的自定义脚本。RunShellScript is different from the other commands, because it allows you to provide your own custom script.


内置命令不可编辑。The built-in commands are not editable.

选择命令之后,选择“运行”以运行脚本。After you choose the command, select Run to run the script. 脚本完成之后,它会在输出窗口中返回输出和任何错误。After the script finishes, it returns the output and any errors in the output window. 下面的屏幕截图显示了运行 ifconfig 命令时的示例输出。The following screenshot shows an example output from running the ifconfig command.



以下示例使用 Invoke-AzVMRunCommand cmdlet 在 Azure VM 上运行 PowerShell 脚本。The following example uses the Invoke-AzVMRunCommand cmdlet to run a PowerShell script on an Azure VM. 该 cmdlet 需要 -ScriptPath 参数中引用的脚本位于运行该 cmdlet 的位置本地。The cmdlet expects the script referenced in the -ScriptPath parameter to be local to where the cmdlet is being run.

Invoke-AzVMRunCommand -ResourceGroupName '<myResourceGroup>' -Name '<myVMName>' -CommandId 'RunShellScript' -ScriptPath '<pathToScript>' -Parameter @{"arg1" = "var1";"arg2" = "var2"}

限制对“运行命令”的访问Limiting access to Run Command

列出“运行命令”或显示某个命令的详细信息需要 Microsoft.Compute/locations/runCommands/read 权限。Listing the run commands or showing the details of a command requires the Microsoft.Compute/locations/runCommands/read permission. 内置读者角色和更高级别具有此权限。The built-in Reader role and higher levels have this permission.

运行命令需要 Microsoft.Compute/virtualMachines/runCommand/action 权限。Running a command requires the Microsoft.Compute/virtualMachines/runCommand/action permission. 虚拟机参与者角色和更高级别具有此权限。The Virtual Machine Contributor role and higher levels have this permission.

若要使用“运行命令”,可以使用内置角色之一,也可以创建一个自定义角色You can use one of the built-in roles or create a custom role to use Run Command.

后续步骤Next steps

若要了解以远程方式在 VM 中运行脚本和命令的其他方式,请参阅在 Linux VM 中运行脚本To learn about other ways to run scripts and commands remotely in your VM, see Run scripts in your Linux VM.