Skip to Main Content

TigerVNC

# Install TigerVNC server and password tool
sudo apt update
sudo apt install - y tigervnc-standalone-server tiervnc-tools

# Requires password setting for first time (will generate ~/. nc/passwd)
vncpasswd

# Create a 1920x1080 resolution and 24-bit depth VNC session (Port: 1)
vncserver :1 -geometry 1920x1080 - deth 24

# Launch VNC service
# with TigerVNC, explicitly specify that local access to
Xtigervnc :1 - geometry 1920x1080 - depth 24 -rfbport 5901 - localhost no -SecurityTypes VncAuth -passwordfile ~/. nc/passwd

# or use standard vncserver command (if full TigerVNC installed)
vncserver :1 - geometry 1920x1080 - depth 24 - localhost no
# 永久配置方案

sudo apt install xfce4 xfce4-goodies -y
# 执行之后会有个选项
# 建议选择:lightdm

# 1. 创建VNC配置目录
mkdir -p ~/.vnc

# 2. 创建正确的xstartup脚本(关键修复)
cat > ~/.vnc/xstartup << 'EOF'
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
export XDG_CURRENT_DESKTOP="GNOME"
exec gnome-session
EOF

# # 可选:使用XFCE桌面环境(需安装XFCE组件)
# cat > ~/.vnc/xstartup << 'EOF'
# #!/bin/sh
# unset SESSION_MANAGER
# unset DBUS_SESSION_BUS_ADDRESS
# exec startxfce4
# EOF

# # 可选:使用GNOME桌面环境(需安装GNOME组件)
# #!/bin/sh
# unset SESSION_MANAGER
# unset DBUS_SESSION_BUS_ADDRESS
# export XDG_CURRENT_DESKTOP="GNOME"
# exec gnome-session

# 3. 添加执行权限
chmod +x ~/.vnc/xstartup

# 4. 设置VNC密码(必须步骤)
vncpasswd
# 输入并确认密码(不会显示输入内容)

# 创建VNC配置文件
mkdir -p ~/.vnc
cat > ~/.vnc/config << 'EOF'
geometry=1920x1080
depth=24
localhost=no
rfbport=5901
EOF

# 设置VNC密码
vncpasswd

# 创建systemd服务实现开机自启
sudo tee /etc/systemd/system/vncserver@.service << 'EOF'
[Unit]
Description=TigerVNC Server on %i
After=syslog.target network.target
[Service]
Type=forking
User=pengdd
Group=pengdd
WorkingDirectory=/home/pengdd
PIDFile=/home/pengdd/.vnc/%H%i.pid  # 添加PID文件路径
# 修复启动命令,添加-xstartup指定会话脚本
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver %i -geometry 1920x1080 -depth 24 -localhost no -xstartup /home/pengdd/.vnc/xstartup
ExecStop=/usr/bin/vncserver -kill %i
[Install]
WantedBy=multi-user.target
EOF

# 启用并启动服务
sudo systemctl daemon-reload
sudo systemctl enable vncserver@:1
sudo systemctl start vncserver@:1
# Common administration command

# lists all VNC sessions
vncserver -list

# terminating the specified session (:1 display number)
vncserver -kill :1

# belt log startup (troubles)
vncserver :1 -geometry 1920x1080 -depth 24 -log ~/vncserver.log