1. 使用SSH隧道加密传输
通过SSH隧道将VNC流量封装在加密的SSH连接中,防止中间人攻击。操作步骤:在本地终端执行ssh -L 5901:localhost:5901 username@your_server_ip -N(将本地5901端口映射到远程服务器的5901端口),然后通过VNC客户端连接localhost:5901。此方法无需暴露VNC端口到公网,是基础且有效的安全措施。
2. 配置防火墙限制访问
使用ufw(Uncomplicated Firewall)仅允许特定IP地址或网络访问VNC端口(默认5901,对应显示编号1)。例如,允许本地回环和特定IP的命令:sudo ufw allow from 127.0.0.1 to any port 5901 proto tcp(本地访问)、sudo ufw allow from 192.168.1.0/24 to any port 5901 proto tcp(局域网访问),最后执行sudo ufw reload使规则生效。
3. 设置强密码认证
使用vncpasswd命令为TigerVNC创建强密码(至少8位,包含大小写字母、数字和特殊字符),密码文件默认存储在~/.vnc/passwd(权限需设为600,防止未授权读取)。定期更换密码(如每3个月),避免密码泄露。
4. 禁用root用户运行VNC
创建普通系统用户(如vncuser),并将其加入sudo组(sudo useradd -m -s /bin/bash vncuser、sudo usermod -aG sudo vncuser),然后以该用户身份启动VNC服务器(vncserver :1)。禁止直接使用root用户运行VNC,降低权限滥用风险。
5. 启用TigerVNC加密(TLS/SSL)
通过SSL/TLS加密VNC连接,提升数据传输安全性。步骤:① 生成自签名证书(sudo mkdir /etc/vnc/ssl、sudo openssl req -new -x509 -days 365 -nodes -out /etc/vnc/ssl/vnc.crt -keyout /etc/vnc/ssl/vnc.key);② 修改TigerVNC服务文件(/etc/systemd/system/vncserver@.service),在ExecStart中添加-SecurityTypes TLSVnc -X509Cert /etc/vnc/ssl/vnc.crt -X509Key /etc/vnc/ssl/vnc.key;③ 重载服务(sudo systemctl daemon-reload)并重启VNC。
6. 限制并发与访问范围
① 限制单个用户的VNC连接数(如最多2个),可通过iptables规则实现:sudo iptables -A INPUT -p tcp --dport 5901 -m connlimit --connlimit-above 2 -j DROP;② 配置访问控制列表(ACL),在~/.vnc/xstartup.d/access-control中添加allow=192.168.1.100,192.168.1.101(允许特定IP)、deny=*(拒绝其他IP),或通过~/.vnc/config设置localhost = 127.0.0.1(仅本地访问)。
7. 定期更新系统与软件
保持Debian系统和TigerVNC软件包为最新版本,及时修复已知安全漏洞。操作命令:sudo apt update && sudo apt upgrade,建议开启自动更新(sudo apt install unattended-upgrades)。
8. 配置VNC服务开机自启与权限
通过systemd创建TigerVNC服务文件(/etc/systemd/system/vncserver@.service),设置User为普通用户、WorkingDirectory为用户主目录,确保服务以最小权限运行。示例内容:
[Unit]
Description=Start TigerVNC server at startup
After=syslog.target network.target
[Service]
Type=forking
User=vncuser
Group=sudo
WorkingDirectory=/home/vncuser
PIDFile=/home/vncuser/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver :%i -depth 24 -geometry 1280x800
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target
然后执行sudo systemctl daemon-reload、sudo systemctl enable vncserver@1.service、sudo systemctl start vncserver@1.service启用并启动服务。