ubuntu配置vnc服务

切换到root

安装服务

1
apt install -y tigervnc-scraping-server

配置vnc密码

1
vncpasswd
1
2
3
Password:
Verify:
Would you like to enter a view-only password (y/n)? n

启动服务

直接启动

1
x0vncserver -rfbauth "/root/.vnc/passwd"

注册成服务

下面的代码保存成vnc.sh并执行.(给所有用户增加vnc服务)

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
vnc_users=`ls /home`
vnc_users="${vnc_users}"

for uv in ${vnc_users}
do
if [ ! -d "/home/${uv}/.vnc" ]; then
mkdir -p /home/${uv}/.vnc
chmod 700 /home/${uv}/.vnc
chown ${uv}:${uv} /home/${uv}/.vnc
fi
cp ${VNC_DIR}/passwd /home/${uv}/.vnc/
chmod 600 /home/${uv}/.vnc/passwd
chown ${uv}:${uv} /home/${uv}/.vnc/passwd
if [ ! -d "/home/${uv}/.config/systemd/user" ]; then
mkdir -p /home/${uv}/.config/systemd/user
chmod 700 /home/${uv}/.config
chown ${uv}:${uv} /home/${uv}/.config
chmod 700 /home/${uv}/.config/systemd
chown ${uv}:${uv} /home/${uv}/.config/systemd
chmod 700 /home/${uv}/.config/systemd/user
chown ${uv}:${uv} /home/${uv}/.config/systemd/user
fi
if [ ! -d "/home/${uv}/.config/systemd/user/default.target.wants" ]; then
mkdir -p /home/${uv}/.config/systemd/user/default.target.wants
chmod 700 /home/${uv}/.config/systemd/user/default.target.wants
chown ${uv}:${uv} /home/${uv}/.config/systemd/user/default.target.wants
fi
# 注册服务
cat>/home/${uv}/.config/systemd/user/x0vncserver.service<<EOF
[Unit]
Description=Remote desktop service (VNC)
After=network.target
[Service]
Type=simple
ExecStartPre=/bin/sh -c 'while ! pgrep -U "${uv}" Xorg; do sleep 1; done'
ExecStart=/usr/bin/x0vncserver -rfbauth %h/.vnc/passwd
[Install]
WantedBy=default.target
EOF
# 权限
chmod 700 /home/${uv}/.config/systemd/user/x0vncserver.service
chown ${uv}:${uv} /home/${uv}/.config/systemd/user/x0vncserver.service
rm -rf /home/${uv}/.config/systemd/user/default.target.wants/x0vncserver.service
# 开机启动
ln -s /home/${uv}/.config/systemd/user/x0vncserver.service /home/${uv}/.config/systemd/user/default.target.wants/x0vncserver.service
chmod 777 /home/${uv}/.config/systemd/user/default.target.wants/x0vncserver.service
chown ${uv}:${uv} /home/${uv}/.config/systemd/user/default.target.wants/x0vncserver.service
done

测试

使用vncviewer测试

vncviewer1

vncviewer2