實現Ubuntu VNC自動化操作可通過以下步驟完成:
以TightVNC為例,執行命令:
sudo apt update && sudo apt install tightvncserver
首次運行時會提示設置VNC連接密碼。
創建系統服務文件
編輯 /etc/systemd/system/vncserver@.service
,內容如下(替換username
為實際用戶名):
[Unit]
Description=VNC Server
After=syslog.target network.target
[Service]
Type=forking
User=username
WorkingDirectory=/home/username
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target
保存后執行:
sudo chmod 644 /etc/systemd/system/vncserver@.service
sudo systemctl daemon-reload
sudo systemctl enable vncserver@1.service # 啟用1號虛擬桌面
sudo systemctl start vncserver@1.service # 立即啟動
驗證服務狀態
sudo systemctl status vncserver@1.service
確保服務處于active (running)
狀態。
sudo apt install novnc
/etc/systemd/system/novnc.service
,內容如下:[Unit]
Description=noVNC Service
After=vncserver@1.service
[Service]
ExecStart=/usr/bin/novnc_proxy --vnc localhost:5901 --listen 6080
Restart=always
[Install]
WantedBy=multi-user.target
啟用并啟動:sudo systemctl enable novnc.service
sudo systemctl start novnc.service
通過瀏覽器訪問http://服務器IP:6080
即可無需插件連接。若啟用防火墻(如UFW),需開放VNC端口(默認5901):
sudo ufw allow 5901/tcp
可編寫腳本實現批量啟?;蚣傻奖O控系統,例如:
#!/bin/bash
# 批量啟動所有用戶的VNC服務
for user in $(ls /home); do
sudo -u $user /usr/bin/vncserver :1 &
done
賦予執行權限后即可運行。
通過以上步驟,可實現Ubuntu VNC的自動化安裝、啟動及遠程訪問,滿足運維或遠程管理需求。