0.更新软件源
$ sudo apt update
1.安装VNC
$ sudo apt install tightvncserver
2.安装GNOME
$ sudo apt install -y gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal ubuntu-desktop
3. 安装支持VNC与Windows之间复制粘贴
$ sudo apt install xclip autocutsel
4.VNC配置
4.1 首次运行设置密码
$ vncserver
4.2 修改配置文件
$ vi ~/.vnc/xstartup
直接复制如下内容,覆盖原文件内容
$ cat ~/.vnc/xstartup
#!/bin/sh
xsetroot -solid grey
autocutsel -fork
XAUTHORITY=$HOME/.Xauthority
export XAUTHORITY
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
export XKL_XMODMAP_DISABLE=1
export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME"
export XDG_MENU_PREFIX="gnome-flashback-"
export QT_STYLE_OVERRIDE=""
export FONTCONFIG_PATH=/etc/fonts
export FONTCONFIG_FILE=/etc/fonts/fonts.conf
gnome-session --session=gnome-flashback-metacity --disable-acceleration-check
4.3 添加启动关闭脚本
或者直接直接运行启动关闭命令
$ cat ~/vncserver_start.sh
vncserver -geometry 1920x1080 :1
$ cat ~/vncserver_stop.sh
vncserver -kill :1
5.VNC Viewer访问
下载并安装VNCViewer:
Download VNC Viewer - free - latest version
VNC Viewer访问IP:5901
6.常见问题解决
6.1 VNC 与 Windows不能自由复制粘贴
解决办法:安装上述 3 的软件包,并增加配置autocutsel -fork到xstartup
6.2 VNC中不能打开firefox,chrome等图形软件
命令打开firefox报错
$ firefox&
client is not authorized to connect server. Error cannot open display: :1
图形打开chrome也报错:
$ tail -f ~/.vnc/test:1.log
18/02/24 02:59:49 rfbProcessClientNormalMessage: ignoring unknown encoding 24
18/02/24 02:59:49 rfbProcessClientNormalMessage: ignoring unknown encoding 15
18/02/24 02:59:49 Using hextile encoding for client IP
18/02/24 02:59:49 rfbProcessClientNormalMessage: ignoring unknown encoding 16
18/02/24 02:59:49 rfbProcessClientNormalMessage: ignoring unknown encoding 22
18/02/24 02:59:49 rfbProcessClientNormalMessage: ignoring unknown encoding 21
18/02/24 02:59:49 rfbProcessClientNormalMessage: ignoring unknown encoding -314
18/02/24 02:59:49 Enabling full-color cursor updates for client IP
18/02/24 02:59:49 rfbProcessClientNormalMessage: ignoring unknown encoding -223
AUDIT: Sun Feb 18 03:00:05 2024: 9115 Xtightvnc: client 19 rejected from local host
AUDIT: Sun Feb 18 03:01:21 2024: 9115 Xtightvnc: client 19 rejected from local host
AUDIT: Sun Feb 18 03:01:39 2024: 9115 Xtightvnc: client 19 rejected from local host
报错:
# tail -f /var/log/syslog
Feb 18 05:14:12 test metacity.desktop[16840]: Xlib: extension "X-Resource" missing on display ":1".
Feb 18 05:14:13 test metacity.desktop[16840]: Xlib: extension "X-Resource" missing on display ":1".
Feb 18 05:14:14 test update-notifier.desktop[18750]: Error executing command as another user: Not authorized
Feb 18 05:14:14 test update-notifier.desktop[18750]: This incident has been reported.
Feb 18 05:14:17 test metacity.desktop[16840]: Xlib: extension "X-Resource" missing on display ":1".
Feb 18 05:14:18 test systemd[1731]: Started snap.chromium.chromium-78740d23-4cba-42a3-abaa-23bd8d66c8ed.scope.
Feb 18 05:14:19 test kernel: [11200.212683] kauditd_printk_skb: 27 callbacks suppressed
Feb 18 05:14:19 test kernel: [11200.212690] audit: type=1326 audit(1708233259.395:210): auid=1000 uid=1000 gid=1000 ses=2 subj=snap.chromium.chromium pid=18923 comm="chrome" exe="/snap/chromium/2757/usr/lib/chromium-browser/chrome" sig=0 arch=c000003e syscall=330 compat=0 ip=0x7f27c51d3f6b code=0x50000
Feb 18 05:14:19 test kernel: [11200.212703] audit: type=1326 audit(1708233259.395:211): auid=1000 uid=1000 gid=1000 ses=2 subj=snap.chromium.chromium pid=18922 comm="chrome" exe="/snap/chromium/2757/usr/lib/chromium-browser/chrome" sig=0 arch=c000003e syscall=330 compat=0 ip=0x7f7ebf741f6b code=0x50000
Feb 18 05:14:19 test chromium_chromium.desktop[18753]: Client is not authorized to connect to Server[18753:18753:0218/051419.723825:ERROR:ozone_platform_x11.cc(239)] Missing X server or $DISPLAY
Feb 18 05:14:19 test chromium_chromium.desktop[18753]: [18753:18753:0218/051419.723881:ERROR:env.cc(257)] The platform failed to initialize. Exiting.
解决办法:在启动vncserver之前,加上环境变量
$ XAUTHORITY=$HOME/.Xauthority
$ export XAUTHORITY
$ env | grep XAUTHORITY
XAUTHORITY=/home/xxx/.Xauthority
也可以直接命令行执行,或者加到~/.bashrc中