远程桌面在 Linux
中一般使用 VNC
,下面我们总结一下 ubuntu 16.04/18.04
下的配置总结:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$ sudo apt-get update # ubuntu 18.04/20.04 默认使用gdm3,导致VNC工作异常,需要切换到 lightdm # ubuntu 16.04 默认使用 lightdm 因此一般不需要调整 $ sudo apt install lightdm # 配置切换到 lightdm $ sudo dpkg-reconfigure gdm3 $ sudo apt install xserver-xorg-video-dummy # 如果从 gdm3 切换到 lightdm 需要重启系统,否则不能正常工作 $ sudo reboot $ sudo apt-get install x11vnc # ubuntu 20.04 # sudo apt-get install net-tools # 设置登录密码,如果不设置密码,会导致任意人都可以登录 $ sudo x11vnc -storepasswd /etc/x11vnc.pass # 需要手工设置一下权限,默认设置的权限可能会导致其他用户无法正常读取 $ sudo chmod 755 /etc/x11vnc.pass # '-rfbport' 参数指定监听端口,'-forever' 参数指定客户端断开后不要停止服务而是继续等待下一次的连接请求 '-rfbauth' 参数指定验证密码的存储文件 $ sudo x11vnc -auth guess -rfbauth /etc/x11vnc.pass -rfbport 5900 -forever -display :0 |
设置开机启动
1 2 3 |
$ sudo apt-get install vim $ sudo vim /etc/systemd/system/x11vnc.service |
里面内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description=x11vnc (Remote access) After=network-online.target [Service] Type=simple ExecStart=/usr/bin/x11vnc -auth guess -rfbauth /etc/x11vnc.pass -rfbport 5900 -forever -display :0 ExecStop=/bin/kill -TERM $MAINPID ExecReload=/bin/kill -HUP $MAINPID KillMode=control-group Restart=on-failure [Install] WantedBy=graphical.target |
配置 systemd
启用服务
1 2 3 4 5 |
$ sudo systemctl daemon-reload $ sudo systemctl enable x11vnc $ sudo systemctl start x11vnc |
如果登录之后,只出现一个桌面背景,没有任何菜单,如下:
参考 Intel NUC(NUC6i3SYH)在不接显示器的情况下VNC不显示桌面(Ubuntu 18.04) 解决,网上搜索相关问题的时候可以使用关键词 HEADLESS X11 查找解决方案,其实就是不插入显示器的情况下,如何强制显卡渲染。
如果系统是ubuntu 21.10版本,则需要编辑
1 2 3 |
# ubuntu 21.10 版本配置文件 $ sudo vim /etc/X11/xorg.conf |
然后在文件尾部,增加如下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Section "Device" Identifier "Configured Video Device" Driver "dummy" EndSection Section "Monitor" Identifier "Configured Monitor" HorizSync 31.5-48.5 VertRefresh 50-70 EndSection Section "Screen" Identifier "Default Screen" Monitor "Configured Monitor" Device "Configured Video Device" DefaultDepth 24 SubSection "Display" Depth 24 Modes "1920x1080" EndSubSection EndSection |
完成后,重启系统。
注意:如果系统上使用nvidia显卡,需要首先通过nvidia-xconfig生成默认配置,如果没有默认配置,会导致 nvidia-smi找不到显卡,一些使用显卡的计算任务或者机器学习框架会出现问题。
参考命令如下:
1 |
$ sudo nvidia-xconfig -a --virtual=1920x1200 |
可能会生成类似如下配置内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# nvidia-xconfig: X configuration file generated by nvidia-xconfig # nvidia-xconfig: version 495.44 Section "ServerLayout" Identifier "Default Layout" Screen 0 "Screen0" InputDevice "Keyboard0" "CoreKeyboard" InputDevice "Mouse0" "CorePointer" EndSection Section "InputDevice" # generated from default Identifier "Keyboard0" Driver "kbd" EndSection Section "InputDevice" # generated from default Identifier "Mouse0" Driver "mouse" Option "Protocol" "auto" Option "Device" "/dev/psaux" Option "Emulate3Buttons" "no" Option "ZAxisMapping" "4 5" EndSection Section "Monitor" Identifier "Monitor0" VendorName "Unknown" ModelName "Unknown" Option "DPMS" EndSection Section "Device" Identifier "Device0" Driver "nvidia" VendorName "NVIDIA Corporation" BoardName "NVIDIA GeForce RTX 3060" EndSection Section "Screen" Identifier "Screen0" Device "Device0" Monitor "Monitor0" DefaultDepth 24 SubSection "Display" Virtual 1920 1200 Depth 24 EndSubSection EndSection |
然后在配置文件的尾部增加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Section "Device" Identifier "Configured Video Device" Driver "dummy" EndSection Section "Monitor" Identifier "Configured Monitor" HorizSync 31.5-48.5 VertRefresh 50-70 EndSection Section "Screen" Identifier "Default Screen" Monitor "Configured Monitor" Device "Configured Video Device" DefaultDepth 24 SubSection "Display" Depth 24 Virtual 1920 1200 Modes "1920x1200" EndSubSection EndSection |
最后修改Section "ServerLayout"字段里的Screen 0为新增的屏幕(Section "Screen" 字段中的 Identifier定义的名字)。
修改参考如下:
1 2 3 4 5 6 |
Section "ServerLayout" Identifier "Default Layout" Screen 0 "Default Screen" InputDevice "Keyboard0" "CoreKeyboard" InputDevice "Mouse0" "CorePointer" EndSection |
修改后的完整内如如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# nvidia-xconfig: X configuration file generated by nvidia-xconfig # nvidia-xconfig: version 495.44 Section "ServerLayout" Identifier "Default Layout" Screen 0 "Default Screen" InputDevice "Keyboard0" "CoreKeyboard" InputDevice "Mouse0" "CorePointer" EndSection Section "InputDevice" # generated from default Identifier "Keyboard0" Driver "kbd" EndSection Section "InputDevice" # generated from default Identifier "Mouse0" Driver "mouse" Option "Protocol" "auto" Option "Device" "/dev/psaux" Option "Emulate3Buttons" "no" Option "ZAxisMapping" "4 5" EndSection Section "Monitor" Identifier "Monitor0" VendorName "Unknown" ModelName "Unknown" Option "DPMS" EndSection Section "Device" Identifier "Device0" Driver "nvidia" VendorName "NVIDIA Corporation" BoardName "NVIDIA GeForce RTX 3060" EndSection Section "Screen" Identifier "Screen0" Device "Device0" Monitor "Monitor0" DefaultDepth 24 SubSection "Display" Virtual 1920 1200 Depth 24 EndSubSection EndSection Section "Device" Identifier "Configured Video Device" Driver "dummy" EndSection Section "Monitor" Identifier "Configured Monitor" HorizSync 31.5-48.5 VertRefresh 50-70 EndSection Section "Screen" Identifier "Default Screen" Monitor "Configured Monitor" Device "Configured Video Device" DefaultDepth 24 SubSection "Display" Depth 24 Virtual 1920 1200 Modes "1920x1200" EndSubSection EndSection |
尽管经过上面的设置,可以正确使用VNC,但是更推荐使用RDP协议,功能更丰富,性能更高,安全性更好。参考 VNC 还是 RDP? 云上的远程桌面究竟该如何选。
这里是不是应该
sudo dpkg-reconfigure lightdm
?
如果系统当前默认的是 lightdm 则执行 sudo dpkg-reconfigure lightdm
但是从目前测试来看,不管执行哪个,都会弹出一个选择界面,让我们在两者中选择一个