如何在Ubuntu 20.04上安装和配置VNC [快速入门]

介绍 (Introduction)

Virtual Network Computing, or VNC, is a connection system that allows you to use your keyboard and mouse to interact with a graphical desktop environment on a remote server. It makes managing files, software, and settings on a remote server easier for users who are not yet comfortable with the command line.

虚拟网络计算 (VNC)是一种连接系统,使您可以使用键盘和鼠标与远程服务器上的图形桌面环境进行交互。 对于尚不熟悉命令行的用户,它使在远程服务器上管理文件,软件和设置更加容易。

In this quickstart guide, you’ll set up a VNC server with TightVNC on an Ubuntu 20.04 server and connect to it securely through an SSH tunnel. Then, you’ll use a VNC client program on your local machine to interact with your server through a graphical desktop environment.

在本快速入门指南中,您将在Ubuntu 20.04服务器上使用TightVNC设置VNC服务器,并通过SSH隧道安全地连接到该服务器。 然后,您将使用本地计算机上的VNC客户端程序通过图形桌面环境与服务器进行交互。

先决条件 (Prerequisites)

To complete this tutorial, you’ll need:

要完成本教程,您需要:

步骤1 —安装桌面环境和VNC服务器 (Step 1 — Installing the Desktop Environment and VNC Server)

After connecting to your server with SSH, update your list of packages:

使用SSH连接到服务器后,更新软件包列表:

  • sudo apt update

    sudo apt更新

Then install Xfce along with the xfce4-goodies package, which contains a few enhancements for the desktop environment:

然后将Xfce与xfce4-goodies软件包一起安装,该软件包包含针对桌面环境的一些增强功能:

  • sudo apt install xfce4 xfce4-goodies

    sudo apt安装xfce4 xfce4-goodies

Once that installation completes, install the TightVNC server:

安装完成后,安装TightVNC服务器:

  • sudo apt install tightvncserver

    sudo apt安装tightvncserver

Next, run the vncpasswd command to set a VNC access password and create the initial configuration files:

接下来,运行vncpasswd命令来设置VNC访问密码并创建初始配置文件:

  • vncpasswd

    vncpasswd

You’ll be prompted to enter and verify a password to access your machine remotely:

系统将提示您输入并验证密码以远程访问您的计算机:


   
   
Output
You will require a password to access your desktops. Password: Verify:

The password must be between six and eight characters long; passwords more than 8 characters will be truncated automatically. Once you verify the password you’ll have the option to create a view-only password, but this isn’t required.

密码长度必须介于6到8个字符之间; 超过8个字符的密码将被自动截断。 验证密码后,您可以选择创建仅查看密码,但这不是必需的。

If you ever want to change your password or add a view-only password, re-run the vncpasswd command.

如果要更改密码或添加仅查看密码,请重新运行vncpasswd命令。

步骤2 —配置VNC服务器 (Step 2 — Configuring the VNC Server)

The commands that the VNC server runs at startup are located in a configuration file called xstartup in the .vnc folder under your home directory. In this step, we’ll create a custom xstartup script which will tell the VNC server to connect to the Xfce desktop.

VNC服务器在启动时运行的命令位于主目录下.vnc文件夹中名为xstartup的配置文件中。 在此步骤中,我们将创建一个自定义xstartup脚本,该脚本将告诉VNC服务器连接到Xfce桌面。

Create a new xstartup file and open it in a text editor, such as nano:

创建一个新的xstartup文件,并在文本编辑器(例如nano中将其打开:

  • nano ~/.vnc/xstartup

    纳米〜/ .vnc / xstartup

Add the following lines to the new file:

将以下行添加到新文件:

~/.vnc/xstartup
〜/ .vnc / xstartup
#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &

Following the shebang, the first command in the file, xrdb $HOME/.Xresources, tells VNC’s GUI framework to read the server user’s .Xresources file. The second command tells the server to launch Xfce.

在执行shebang之后 ,文件xrdb $HOME/.Xresources的第一个命令告诉VNC的GUI框架读取服务器用户的.Xresources文件。 第二个命令告诉服务器启动Xfce。

Save and close the file after adding these lines. If you used nano, do so by pressing CTRL + X, Y, then ENTER.

添加这些行后,保存并关闭文件。 如果您使用过nano ,请按CTRL + XY ,然后按ENTER

Then make the file executable:

然后使文件可执行:

  • chmod +x ~/.vnc/xstartup

    chmod + x〜/ .vnc / xstartup

And start the VNC server with the vncserver command:

并使用vncserver命令启动VNC服务器:

  • vncserver -localhost

    vncserver-本地主机

This command includes the -localhost option, which binds the VNC server to your server’s loopback interface. This will cause VNC to only allow connections that originate from the server on which it’s installed.

此命令包括-localhost选项,该选项将VNC服务器绑定到服务器的环回接口。 这将导致VNC仅允许来自其安装服务器的连接。

You’ll see output similar to this:

您将看到类似于以下的输出:


   
   
Output
New 'X' desktop is your_hostname:1 Starting applications specified in /home/sammy/.vnc/xstartup Log file is /home/sammy/.vnc/your_hostname:1.log

Here, you can see that the command launches a default server instance on port 5901. This port is called a display port, and is referred to by VNC as :1:

在这里,您可以看到该命令在端口5901上启动默认服务器实例。 此端口称为显示端口 ,VNC将该端口称为:1

步骤3 —安全地连接到VNC桌面 (Step 3 — Connecting to the VNC Desktop Securely)

To securely connect to your server, you’ll establish an SSH tunnel and then tell your VNC client to connect using that tunnel rather than making a direct connection.

为了安全地连接到服务器,您将建立SSH隧道,然后告诉您的VNC客户端使用该隧道进行连接,而不是直接建立连接。

Create an SSH connection on your local computer that securely forwards to the localhost connection for VNC. You can do this via the terminal on Linux or macOS with the following ssh command:

在本地计算机上创建一个SSH连接,该连接安全地转发到VNC的localhost连接。 您可以使用以下ssh命令通过Linux或macOS上的终端执行此操作:

  • ssh -L 59000:localhost:5901 -C -N -l sammy your_server_ip

    ssh -L 59000 :localhost: 5901 -C -N -l sammy your_server_ip

The local port can be any port that isn’t already blocked by another program or process, though we use 59000 in this example. Also, make sure to change sammy to your Ubuntu user’s username and your_server_ip to reflect your server’s IP address.

本地端口可以是尚未被另一个程序或进程阻止的任何端口,尽管在此示例中我们使用59000 。 另外,确保将sammy更改为Ubuntu用户的用户名和your_server_ip以反映服务器的IP地址。

If you are using PuTTY to connect to your server, you can create an SSH tunnel by right-clicking on the top bar of the terminal window, and then clicking the Change Settings… option:

如果您使用PuTTY连接到服务器,则可以通过在终端窗口的顶部栏上单击鼠标右键,然后单击“ 更改设置...”选项来创建SSH隧道:

Find the Connection branch in the tree menu on the left-hand side of the PuTTY Reconfiguration window. Expand the SSH branch and click on Tunnels. On the Options controlling SSH port forwarding screen, enter 59000 as the Source Port and localhost:5901 as the Destination, like this:

在“ PuTTY重新配置”窗口左侧的树菜单中找到“ 连接”分支。 展开SSH分支,然后单击“ 隧道” 。 在“ 控制SSH端口转发选项”屏幕上,输入59000作为Source Port ,输入localhost:5901作为Destination ,如下所示:

Then click the Add button, and then the Apply button to implement the tunnel.

然后单击添加按钮,然后单击应用按钮以实现隧道。

Once the tunnel is running, use a VNC client to connect to localhost:59000. You’ll be prompted to authenticate using the password you set in Step 1.

隧道运行后,使用VNC客户端连接到localhost:59000 。 系统将提示您使用在步骤1中设置的密码进行身份验证。

Once you are connected, you’ll see the default Xfce desktop. It should look something like this:

建立连接后,您将看到默认的Xfce桌面。 它看起来应该像这样:

You can access files in your home directory with the file manager or from the command line, as seen here:

您可以使用文件管理器或从命令行访问主目录中的文件,如下所示:

Press CTRL+C in your local terminal to stop the SSH tunnel and return to your prompt. This will disconnect your VNC session as well.

在本地终端中按CTRL+C停止SSH隧道并返回到提示符。 这也将断开您的VNC会话。

步骤4 —将VNC作为系统服务运行 (Step 4 — Running VNC as a System Service)

By setting up the VNC server to run as a systemd service you can use systemd’s management commands start, stop, and restart the server, as well as enable it to start running whenever the server boots up.

通过将VNC服务器设置为作为systemd服务运行,您可以使用systemd的管理命令来启动,停止和重新启动服务器,并使它能够在服务器启动时开始运行。

First, create a new systemd unit file called /etc/systemd/system/vncserver@.service:

首先,创建一个名为/etc/systemd/system/vncserver@.service的新systemd单元文件:

  • sudo nano /etc/systemd/system/vncserver@.service

    须藤纳米/etc/systemd/system/vncserver@.service

The @ symbol at the end of the name will let us pass in an argument you can use in the service configuration. You’ll use this to specify the VNC display port you want to use when you manage the service.

名称末尾的@符号将使我们传递您可以在服务配置中使用的参数。 您将使用它来指定管理服务时要使用的VNC显示端口。

Add the following lines to the file, making sure to change the value of User, Group, WorkingDirectory, and the username in the value of PIDFILE to match your username:

将以下行添加到文件,确保更改PIDFILE值中的UserGroupWorkingDirectory和用户名的值以匹配您的用户名:

/etc/systemd/system/vncserver@.service
/etc/systemd/system/vncserver@.service
[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=sammy
Group=sammy
WorkingDirectory=/home/sammy

PIDFile=/home/sammy/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Save and close the file.

保存并关闭文件。

Next, make the system aware of the new unit file:

接下来,使系统知道新的单位文件:

  • sudo systemctl daemon-reload

    sudo systemctl守护进程重新加载

Enable the unit file:

启用单位文件:

  • sudo systemctl enable vncserver@1.service

    sudo systemctl启用vncserver@1.service

The 1 following the @ sign signifies which display number the service should appear over, in this case the default :1 as was discussed in Step 2.

@符号后的1表示服务应显示在哪个显示号上,在这种情况下,默认值:1如步骤2所述。

Stop the current instance of the VNC server if it’s still running:

如果VNC服务器的当前实例仍在运行,请停止它:

  • vncserver -kill :1

    vncserver -kill:1

Then start it as you would start any other systemd service:

然后像启动其他任何systemd服务一样启动它:

  • sudo systemctl start vncserver@1

    sudo systemctl启动vncserver @ 1

You can verify that it started with this command:

您可以验证它是否使用以下命令启动:

  • sudo systemctl status vncserver@1

    sudo systemctl状态vncserver @ 1

See our tutorial on How To Use Systemctl to Manage Systemd Services and Units for more information on systemctl.

有关systemctl更多信息,请参见有关如何使用Systemctl来管理系统服务和单元的教程。

To reconnect, start your SSH tunnel again:

要重新连接,请再次启动SSH隧道:

  • ssh -L 59000:127.0.0.1:5901 -C -N -l sammy your_server_ip

    ssh -L 59000 :127.0.0.1: 5901 -C -N -l sammy your_server_ip

Then make a new connection using your VNC client software to localhost:59000 to connect to your server.

然后使用您的VNC客户端软件与localhost: 59000建立新连接localhost: 59000以连接到服务器。

结论 (Conclusion)

You now have a secured VNC server up and running on your Ubuntu 20.04 server. Now you’ll be able to manage your files, software, and settings with a user-friendly graphical interface, and you’ll be able to run graphical software like web browsers remotely.

您现在已经在Ubuntu 20.04服务器上启动并运行了安全的VNC服务器。 现在,您将可以使用用户友好的图形界面来管理文件,软件和设置,并且可以远程运行Web浏览器之类的图形软件。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-20-04-quickstart

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值