安装VNC服务器
以TightVNC为例,执行命令:
sudo apt update
sudo apt install tightvncserver
或安装TigerVNC:
sudo apt install tigervnc-standalone-server
设置VNC密码
运行命令:
vncpasswd
按提示输入并确认密码(用于客户端连接)。
启动VNC服务器
指定显示号(如:1对应端口5901):
vncserver :1
可选:通过-geometry参数设置分辨率(如1920x1080)。
配置开机自启动
创建Systemd服务文件:
sudo nano /etc/systemd/system/vncserver@:1.service
粘贴内容(替换your_username):
[Unit]
Description=VNC Server
After=syslog.target network.target
[Service]
Type=forking
User=your_username
WorkingDirectory=/home/your_username
ExecStart=/usr/bin/vncserver :1 -depth 24 -geometry 1280x800
ExecStop=/usr/bin/vncserver -kill :1
[Install]
WantedBy=multi-user.target
保存后执行:
sudo systemctl daemon-reload
sudo systemctl enable vncserver@:1.service
sudo systemctl start vncserver@:1.service
开放防火墙端口
若使用UFW防火墙,运行:
sudo ufw allow 5901/tcp
(5901为VNC默认端口,对应显示号:1)。
客户端连接
在远程设备安装VNC客户端(如TightVNC Viewer),输入:
服务器IP:1 # 端口号为5901+显示号
输入之前设置的VNC密码即可连接。
安全建议:
ssh -L 5901:localhost:5901 user@server_ip -N -f
然后通过localhost:1连接。