安装和配置远程桌面以连接到 Azure 中的 Linux VMInstall and configure Remote Desktop to connect to a Linux VM in Azure

通常使用安全外壳 (SSH) 连接从命令行管理 Azure 中的 Linux 虚拟机 (VM)。Linux virtual machines (VMs) in Azure are usually managed from the command line using a secure shell (SSH) connection. 如果不熟悉 Linux,或者要快速进行故障排除,使用远程桌面可能会更方便。When new to Linux, or for quick troubleshooting scenarios, the use of remote desktop may be easier. 本文详细介绍如何使用 Resource Manager 部署模型为 Linux VM 安装和配置桌面环境 (xfce) 和远程桌面 (xrdp)。This article details how to install and configure a desktop environment (xfce) and remote desktop (xrdp) for your Linux VM using the Resource Manager deployment model.

先决条件Prerequisites

本文需要 Azure 中的现有 Ubuntu 16.04 LTS VM。This article requires an existing Ubuntu 16.04 LTS VM in Azure. 如果需要创建 VM,请使用以下方法之一:If you need to create a VM, use one of the following methods:

Note

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

在 Linux VM 上安装桌面环境Install a desktop environment on your Linux VM

Azure 中的大多数 Linux VM 默认情况下未安装桌面环境。Most Linux VMs in Azure do not have a desktop environment installed by default. 通常使用 SSH 连接(而不是桌面环境)来管理 Linux VM。Linux VMs are commonly managed using SSH connections rather than a desktop environment. Linux 中有各种可以选择的桌面环境。There are various desktop environments in Linux that you can choose. 根据所选的桌面环境,可能会占用 1 到 2 GB 的磁盘空间,并需要 5 到 10 分钟来安装和配置所有所需的包。Depending on your choice of desktop environment, it may consume one to 2 GB of disk space, and take 5 to 10 minutes to install and configure all the required packages.

以下示例在 Ubuntu 16.04 LTS VM 上安装轻型 xfce4 桌面环境。The following example installs the lightweight xfce4 desktop environment on an Ubuntu 16.04 LTS VM. 其他发行版的命令略有不同(例如,使用 yum 在 CentOS 上安装并配置适当的 selinux 规则,或者使用 zypper 在 SUSE 上安装)。Commands for other distributions vary slightly (use yum to install on CentOS and configure appropriate selinux rules, or use zypper to install on SUSE, for example).

首先,通过 SSH 连接到 VM。First, SSH to your VM. 以下示例使用用户名 azureuser 连接到名为 myvm.chinanorth.cloudapp.chinacloudapi.cn 的 VM。The following example connects to the VM named myvm.chinanorth.cloudapp.chinacloudapi.cn with the username of azureuser. 使用自己的值:Use your own values:

ssh azureuser@myvm.chinanorth.cloudapp.chinacloudapi.cn

如果要使用 Windows 并且需要有关使用 SSH 的详细信息,请参阅如何将 SSH 密钥用于 WindowsIf you are using Windows and need more information on using SSH, see How to use SSH keys with Windows.

接下来,使用 apt 安装 xfce,如下所示:Next, install xfce using apt as follows:

sudo apt-get update
sudo apt-get install xfce4

安装和配置远程桌面服务器Install and configure a remote desktop server

安装桌面环境后,请配置远程桌面服务来侦听传入连接。Now that you have a desktop environment installed, configure a remote desktop service to listen for incoming connections. xrdp 是大多数 Linux 分发版中提供的开源远程桌面协议 (RDP) 服务器,可与 xfce 完美配合。xrdp is an open source Remote Desktop Protocol (RDP) server that is available on most Linux distributions, and works well with xfce. 在 Ubuntu VM 上安装 xrdp,如下所示:Install xrdp on your Ubuntu VM as follows:

sudo apt-get install xrdp
sudo systemctl enable xrdp

告诉 xrdp 在启动会话时要使用的桌面环境。Tell xrdp what desktop environment to use when you start your session. 配置 xrdp 以使用 xfce 作为桌面环境,如下所示:Configure xrdp to use xfce as your desktop environment as follows:

echo xfce4-session >~/.xsession

重新启动 xrdp 服务使更改生效,如下所示:Restart the xrdp service for the changes to take effect as follows:

sudo service xrdp restart

设置本地用户帐户密码Set a local user account password

如果在创建 VM 时已为用户帐户创建密码,请跳过此步骤。If you created a password for your user account when you created your VM, skip this step. 如果仅使用 SSH 密钥身份验证,并且未设置本地帐户密码,请在使用 xrdp 之前指定密码以登录到 VM。If you only use SSH key authentication and do not have a local account password set, specify a password before you use xrdp to log in to your VM. xrdp 无法接受使用 SSH 密钥进行身份验证。xrdp cannot accept SSH keys for authentication. 以下示例为用户帐户 azureuser 指定密码:The following example specifies a password for the user account azureuser:

sudo passwd azureuser

Note

指定密码不会将 SSHD 配置更新为允许密码登录(如果当前不允许)。Specifying a password does not update your SSHD configuration to permit password logins if it currently does not. 从安全角度看,用户可能想要使用基于密钥的身份验证通过 SSH 隧道连接到 VM,并连接到 xrdp。From a security perspective, you may wish to connect to your VM with an SSH tunnel using key-based authentication and then connect to xrdp. 如果是这样,请跳过以下创建网络安全组规则的步骤,以允许远程桌面流量。If so, skip the following step on creating a network security group rule to allow remote desktop traffic.

为远程桌面流量创建网络安全组规则Create a Network Security Group rule for Remote Desktop traffic

若要允许远程桌面流量到达 Linux VM,需要创建网络安全组规则以允许端口 3389 上的 TCP 访问 VM。To allow Remote Desktop traffic to reach your Linux VM, a network security group rule needs to be created that allows TCP on port 3389 to reach your VM. 有关网络安全组规则的详细信息,请参阅什么是网络安全组?For more information about network security group rules, see What is a network security group? 还可以使用 Azure 门户创建网络安全组规则You can also use the Azure portal to create a network security group rule.

以下示例使用 az vm open-port 在端口 3389 上创建一个网络安全组规则。The following example creates a network security group rule with az vm open-port on port 3389. 使用 Azure CLI(而不是与 VM 的 SSH 会话),打开以下网络安全组规则:From the Azure CLI, not the SSH session to your VM, open the following network security group rule:

az vm open-port --resource-group myResourceGroup --name myVM --port 3389

使用远程桌面客户端连接 Linux VMConnect your Linux VM with a Remote Desktop client

打开本地的远程桌面客户端,并连接到 Linux VM 的 IP 地址或 DNS 名称。Open your local remote desktop client and connect to the IP address or DNS name of your Linux VM. 输入 VM 上的用户帐户的用户名和密码,如下所示:Enter the username and password for the user account on your VM as follows:

使用远程桌面客户端连接到 xrdp

进行身份验证后,会加载 xfce 桌面环境,其外观类似于以下示例:After authenticating, the xfce desktop environment will load and look similar to the following example:

通过 xrdp 连接 xfce 桌面环境

如果本地 RDP 客户端使用网络级别身份验证 (NLA),则可能需要禁用该连接设置。If your local RDP client uses network level authentication (NLA), you may need to disable that connection setting. XRDP 目前不支持 NLA。XRDP does not currently support NLA. 也可以查看其他支持 NLA 的 RDP 解决方案,例如 FreeRDPYou can also look at alternative RDP solutions that do support NLA, such as FreeRDP.

故障排除Troubleshoot

如果无法使用远程桌面客户端连接到 Linux VM,请在 Linux VM上使用 netstat 验证 VM 是否正在侦听 RDP 连接,如下所示:If you cannot connect to your Linux VM using a Remote Desktop client, use netstat on your Linux VM to verify that your VM is listening for RDP connections as follows:

sudo netstat -plnt | grep rdp

以下示例显示正在按预期方式侦听 TCP 端口 3389 的 VM:The following example shows the VM listening on TCP port 3389 as expected:

tcp     0     0      127.0.0.1:3350     0.0.0.0:*     LISTEN     53192/xrdp-sesman
tcp     0     0      0.0.0.0:3389       0.0.0.0:*     LISTEN     53188/xrdp

如果xrdp-sesman 服务未在侦听,请在 Ubuntu VM 上重新启动该服务,如下所示:If the xrdp-sesman service is not listening, on an Ubuntu VM restart the service as follows:

sudo service xrdp restart

请查看 Ubuntu VM 上的 /var/log 中的日志,以获得该服务可能未响应的原因的指示。Review logs in /var/log on your Ubuntu VM for indications as to why the service may not be responding. 也可以在远程桌面连接尝试期间监视 syslog 以查看任何错误:You can also monitor the syslog during a remote desktop connection attempt to view any errors:

tail -f /var/log/syslog

其他 Linux 发行版(例如,CentOS 和 SUSE)可能采用其他方式来重启服务并更换要查看的日志文件位置。Other Linux distributions such as CentOS and SUSE may have different ways to restart services and alternate log file locations to review.

如果用户在远程桌面客户端中未收到任何响应,并且在系统日志中看不到任何事件,则此行为指示远程桌面流量无法到达 VM。If you do not receive any response in your remote desktop client and do not see any events in the system log, this behavior indicates that remote desktop traffic cannot reach the VM. 查看网络安全组规则,以确保有规则允许端口 3389 上的 TCP。Review your network security group rules to ensure that you have a rule to permit TCP on port 3389. 有关详细信息,请参阅排查应用程序连接问题For more information, see Troubleshoot application connectivity issues.

后续步骤Next steps

有关为 Linux VM 创建 SSH 密钥和在 Linux VM 上使用 SSH 密钥的详细信息,请参阅在 Azure 中为 Linux VM 创建 SSH 密钥For more information about creating and using SSH keys with Linux VMs, see Create SSH keys for Linux VMs in Azure.

有关从 Windows 使用 SSH 的信息,请参阅如何在 Windows 中使用 SSH 密钥For information on using SSH from Windows, see How to use SSH keys with Windows.