第1步:安装桌面环境

默认情况下,Ubuntu服务器不带桌面环境,VNC需要桌面环境运行。

安装XFCE的轻量级环境

sudo apt update
sudo apt install xfce4 xfce4-goodies xorg dbus-x11 x11-xserver-utils
在Ubuntu 16.04/18.04上安装和配置VNC服务器

第2步:安装VNC服务器

有很多VNC服务器,这里我们使用Tigervnc。

要安装tigervnc,请运行以下命令:

sudo apt install tigervnc-standalone-server tigervnc-common

安装Tigervnc后,请运行以下命令以设置初始设置,包括创建密码以访问桌面。

vncserver

输出示例:

You will require a password to access your desktops.
Password:
Verify:
Would you like to enter a view-only password (y/n)? n
New 'ubuntu1804:1 (richrd)' desktop at :1 on machine ubuntu1804
Starting applications specified in /etc/X11/Xvnc-session
Log file is /home/richrd/.vnc/ubuntu1804:1.log
Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/richrd/.vnc/passwd :1 to connect to the VNC server.
vncserver: Failed command '/etc/X11/Xvnc-session': 256!
=================== tail -15 /home/richrd/.vnc/ubuntu1804:1.log ===================
Starting applications specified in /etc/X11/Xvnc-session has failed.
Maybe try something simple first, e.g.,
    tigervncserver -xstartup /usr/bin/xterm

VNCServer命令将创建密码文件并将其存储在~/.vnc文件夹中。

步骤3:配置VNC服务器

运行以下命令进行配置。

vncserver -kill :1

然后创建一个启动配置文件:

nano ~/.vnc/xstartup

添加下面的内容到文件并保存。

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4

授予可执行权限:

chmod u+x ~/.vnc/xstartup

创建系统服务文件

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

添加内容如下,并保存。

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=simple
User=jack
PAMName=login
PIDFile=/home/%u/.vnc/%H%i.pid
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill :%i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver :%i -geometry 1440x900 -alwaysshared -fg
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target

重新加载SystemD配置。

sudo systemctl daemon-reload

启动并启用VNC服务器

sudo systemctl start vncserver@1.service
sudo systemctl enable vncserver@1.service

检查后查看VNC是否已启动并运行。

sudo systemctl status vncserver@1.service

输出示例:

● vncserver@1.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/vncserver@.service; indirect; vendor preset: enabled)
   Active: active (running) since Mon 2019-02-04 15:22:07 CST; 17s ago
 Main PID: 14025 (vncserver)
    Tasks: 0 (limit: 4663)
   CGroup: /system.slice/system-vncserver.slice/vncserver@1.service
           ‣ 14025 /usr/bin/perl /usr/bin/vncserver :1 -geometry 1440x900 -alwaysshared -fg
Nov 04 15:22:06 ubuntu1804 systemd[1]: Starting Remote desktop service (VNC). 
Nov 04 15:22:07 ubuntu1804 systemd[14011]: pam_unix(login:session): session opened for user richrd by (uid=0)
Nov 04 15:22:07 ubuntu1804 systemd[1]: Started Remote desktop service (VNC).
Nov 04 15:22:07 ubuntu1804 systemd[14025]: pam_unix(login:session): session opened for user richrd by (uid=0)

现在VNC服务器已经准备好了,

使用VNC客户端(比如:RealVNC)并尝试到服务器(server_name:5901)。

日期:2020-07-07 20:55:43 来源:oir作者:oir