安装SSH任务并启动
sudo apt update
sudo apt install openssh-server
sudo systemctl status ssh.service
确认是否开机自启动
sudo systemctl is-enabled ssh.service
安装vncserver
sudo apt install tigervnc-standalone-server tigervnc-xorg-extension
安装GNOME组件
sudo apt install -y gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal ubuntu-desktop
安装支持VNC与Windows之间复制粘贴
sudo apt install xclip autocutsel
以管理员编辑用户配置文件
sudo vim /etc/tigervnc/vncserver.users
将端口号与用户名对应,如用户andrew使用5902端口,用户lisa使用5903端口的配置如下:
:2=andrew
:3=lisa
给每个用户配置config文件
以为andrew配置为例,先使用用户andrew执行命令vncserver,再执行vncserver -kill :*关闭刚启动的VNC桌面,这样VNC的配置目录(~/.vnc)就建立好了。
接着新建文件/home/andrew/.vnc/config,内容如下:
session=ubuntu
geometry=1920x1080
securitytypes=vncauth,tlsvnc
上面启动的服务, 默认是只监听本地127.0.0.1, 所以从其它机器是无法连接的, 如果需要连接, 可以用这个命令启动
vncserver -localhost no
设置运行的桌面环境
sudo vim ~/.vnc/xstartup
填入以下内容:
#!/bin/sh
Start up the standard system desktop
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/usr/bin/gnome-session
-x /etc/vnc/xstartup && exec /etc/vnc/xstartup
-r $HOME/.Xresources && xrdb $HOME/.Xresources
x-window-manager &
添加文件的可执行权限chmod +x xstartup
将 tigervncservice 服务添加到启动, :2根据自己的实例修改
sudo systemctl start tigervncserver@:2.service
sudo systemctl enable tigervncserver@:2.service
查看vnc实例:vncserver -list
另一种方法:
或者在~/.bash_profile中添加:
sudo vim ~/.bash_profile
vncserver :1 -geometry 1920x1080 -depth 24 -localhost no
其中~/.vnc/xstartup中的内容如下:
#!/bin/sh
Start up the standard system desktop
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
启动 GNOME 桌面环境 (替换原有内容)
/usr/bin/gnome-session &
启动 X Window 管理器(作为备用或辅助)
-x /etc/vnc/xstartup && exec /etc/vnc/xstartup
-r $HOME/.Xresources && xrdb $HOME/.Xresources
x-window-manager &
启动复制粘贴支持工具
autocutsel -fork
添加:sudo vim /etc/rc.local
启动 ps 用户的 VNC 服务器
-l 指定用户名
sudo -u ps /usr/bin/vncserver :1 -geometry 1920x1080 -depth 24 -localhost no &
或者,调用我们之前写的包装脚本
sudo -u ps /home/ps/start-vnc.sh &
exit 0
rc.local方法:
sudo vim /etc/rc.local
#!/bin/bash
这行是必须的,告诉脚本在出错时立即退出
set -e
在这里添加你想要在开机时执行的命令
例如:启动 ps 用户的 VNC 服务器
注意:必须使用 sudo -u username 来指定运行的用户
sudo -u ps /usr/bin/vncserver :1 -geometry 1920x1080 -depth 24 -localhost no &
脚本必须最后以 exit 0 结束
exit 0
sudo chmod +x /etc/rc.local
创建
sudo vim /etc/systemd/system/rc-local-fix.service
Unit
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
Service
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
Install
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable rc-local-fix.service
sudo systemctl start rc-local-fix.service
sudo systemctl status rc-local-fix.service
可以再套一层:
sudo vim /usr/local/bin/start-ps-vnc.sh
#!/bin/bash
睡眠几秒,确保网络和系统服务在开机时已就绪 (可选,但推荐)
sleep 10
切换到 ps 用户,并启动一个 login shell (-l) 来执行后续的命令
-c 后面是要执行的命令字符串
首先 source 用户的 profile 来获取环境变量
然后执行 vncserver 命令
sudo -u ps bash -lc "source /home/ps/.profile; /usr/bin/vncserver :1 -geometry 1920x1080 -depth 24 -localhost no"
sudo chmod +x /usr/local/bin/start-ps-vnc.sh
修改/etc/rc.local文件,在文件中调用脚本:
调用包装脚本
/usr/local/bin/start-ps-vnc.sh &
一个标准的xstartup文件内容如下:
#!/bin/bash
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
设置 Ubuntu GNOME 环境变量
export XDG_CURRENT_DESKTOP=ubuntu:GNOME
export DESKTOP_SESSION=ubuntu
export GNOME_SHELL_SESSION_MODE=ubuntu
关闭错误输出(可选,避免日志冗余)
exec > ~/.vnc/startup.log 2>&1
取消 X 权限检查(部分系统需要)
xrdb $HOME/.Xresources
禁用屏幕保护和电源管理(避免黑屏)
xset s off
xset -dpms
xset s noblank
GNOME 桌面(需安装 gnome-session)
if -x /usr/bin/gnome-session ; then
exec gnome-session --session=ubuntu &
fi
autocutsel -fork