树莓派运行图形界面非常普遍,树莓派3运行官方raspbian或者ubuntu+xfce等都完全可以,树莓派4甚至运行ubuntu desktop也勉强可以。

但一般树莓派又不会接显示器,键盘鼠标;一般接在路由器上。而使用x11 forward(比如用xmanager,xming)通常会非常卡,几乎无法使用;这时vnc就是一个比较好的选择了。


apt-get install vnc4server
# 如果装的是没图形界面的系统,会列出很多需要的包
# 如果是ubuntu 20.04,或者其他没有vnc4server的系统
apt-get install tigervnc-standalone-server tigervnc-common

安装后,第一次运行需要设置密码,然后会vnc启动:1

ubuntu@ubuntu:~$ vncserver 
You will require a password to access your desktops.
Password:
Verify:
Would you like to enter a view-only password (y/n)? n

Starting applications specified in /etc/X11/Xvnc-session
Log file is /home/ubuntu/.vnc/ubuntu:1.log

Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/ubuntu/.vnc/passwd :1 to connect to the VNC server.

这时,内网电脑尝试连接会连不上,因为tigervnc默认监听本地端口

ubuntu@ubuntu:~$ ss -ltn
State   Recv-Q  Send-Q    Local Address:Port     Peer Address:Port  Process  
LISTEN  0   5      127.0.0.1:5901     0.0.0.0:*    

先杀掉vnc,重新设置监听全部地址

ubuntu@ubuntu:~/.vnc$ vncserver -kill :1
Killing Xtigervnc process ID 26487... success!
ubuntu@ubuntu:~/.vnc$ vncserver -localhost no

这时按理可以连上,提示输入密码,但仍然会报错,而且vncserver进程都会挂

查看vnc的log,在~/.vnc$ vim ubuntu\:1.log

Sat May 9 05:34:15 2020
Connections: accepted: 192.168.3.3::2303
SConnection: Client needs protocol version 3.8
SConnection: Client requests security type VncAuth(2)
terminate called after throwing an instance of 'rdr::Exception'
terminate called recursively
(EE)
(EE) Backtrace:
(EE) 0: /usr/bin/Xtigervnc (OsLookupColor+0x188) [0xaaaac24fe038]
(EE) unw_get_proc_info failed: no unwind info found [-10]
(EE)
(EE)
Fatal server error:
(EE) Caught signal 6 (Aborted). Server aborting
(EE)
X connection to :1 broken (explicit kill or server shutdown).^M
Killing Xtigervnc process ID 26960… which was already dead
Cleaning stale pidfile '/home/ubuntu/.vnc/ubuntu:1.pid'!

发现是rdr::Exception,原因大概是链接库一类的问题,启动前带上libgcc_s即可

# 64位系统
LD_PRELOAD=/lib/aarch64-linux-gnu/libgcc_s.so.1 vncserver -localhost no
# 32位系统
LD_PRELOAD=/lib/arm-linux-gnueabihf/libgcc_s.so.1 vncserver -localhost no

这样大体上就可以运行了,关于其他画质,颜色深度,分辨率,屏保之类的设置就不说了