ubuntu

Ubuntu使用TigerVNC连接远程桌面

小樊
45
2025-09-23 15:12:12
栏目: 智能运维

Installing TigerVNC Server on Ubuntu
To begin, update your system’s package list and install the TigerVNC standalone server and common components. Open a terminal and run:

sudo apt update
sudo apt install tigervnc-standalone-server tigervnc-common -y

This installs the necessary packages for TigerVNC to function as a remote desktop server.

Setting a VNC Password
A VNC password is required to authenticate remote connections. Run the following command and follow the prompts to set a password (the password is encrypted and stored in ~/.vnc/passwd):

vncpasswd

The password must be at least 6 characters long—this is the password you’ll enter when connecting via a VNC client.

Configuring the Desktop Environment
TigerVNC requires a startup script (~/.vnc/xstartup) to launch the desktop environment. If the file doesn’t exist, create it:

nano ~/.vnc/xstartup

Add the following configuration tailored to your desktop environment (replace with your preferred environment if needed):

Save the file and exit the editor. Then, make the script executable:

chmod +x ~/.vnc/xstartup

This ensures the correct desktop environment launches when you connect.

Starting the VNC Server
Initiate the VNC server with a specific display number (e.g., :1), which corresponds to port 5901 (the formula is 5900 + display number). For example:

vncserver :1 -geometry 1920x1080 -depth 24
vncserver -kill :1

.

Configuring the Systemd Service for Auto-Start
To ensure TigerVNC starts automatically after a reboot, create a systemd service file:

sudo nano /etc/systemd/system/vncserver@.service

Paste the following content, replacing <USER> with your Ubuntu username:

[Unit]
Description=Start TigerVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=<USER>
Group=<USER>
WorkingDirectory=/home/<USER>
ExecStartPre=/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver :%i -depth 24 -geometry 1920x1080
ExecStop=/usr/bin/vncserver -kill :%i
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save the file, then reload systemd to apply changes:

sudo systemctl daemon-reload

Enable the service to start on boot and start it immediately:

sudo systemctl enable vncserver@1.service
sudo systemctl start vncserver@1.service

.

Adjusting Firewall Rules
If Ubuntu’s firewall (UFW) is enabled, allow traffic on the VNC port (default: 5901 for display :1). Run:

sudo ufw allow 5901/tcp
sudo ufw reload

This permits remote connections to the VNC server.

Connecting to the Remote Desktop
On a remote computer (e.g., a Windows machine), install a VNC client like TigerVNC Viewer or RealVNC. Open the client and enter the Ubuntu server’s IP address followed by the display number (e.g., 192.168.1.100:1). Click “Connect” and enter the VNC password you set earlier. You should now see the Ubuntu desktop remotely.

Optional: Enhancing Security with SSH Tunneling
For added security, use an SSH tunnel to encrypt the VNC connection. On your local machine, run:

ssh -L 5901:localhost:5901 <USER>@<SERVER_IP>

Replace <USER> with your Ubuntu username and <SERVER_IP> with the server’s IP. Then, connect the VNC client to localhost:5901 instead of the server’s direct IP. This routes traffic through SSH, protecting it from interception.

0
看了该问题的人还看了