ubuntu

如何在Ubuntu上共享TigerVNC桌面

小樊
52
2025-10-04 09:22:43
栏目: 智能运维

Prerequisites
Ensure your Ubuntu system has a graphical desktop environment (e.g., GNOME, Xfce) installed. For server editions, install one first:

sudo apt update && sudo apt install ubuntu-desktop  # For GNOME
# OR
sudo apt install xfce4  # For Xfce (lighterweight)

Update system packages and install essential tools:

sudo apt update && sudo apt upgrade -y
sudo apt install build-essential curl wget git -y

1. Install TigerVNC Server

Install the TigerVNC standalone server and common components:

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

2. Configure VNC Password

Set a secure VNC password (different from your system login password) for authentication:

vncpasswd

3. Configure Desktop Environment for VNC

TigerVNC requires a startup script (~/.vnc/xstartup) to launch the desktop environment.

3.1 Generate xstartup File (if missing)

Run vncserver once to create the .vnc directory and default files:

vncserver

Press Ctrl+C to stop the server immediately after creation.

3.2 Edit xstartup for Your Desktop Environment

Open the xstartup file in a text editor (e.g., nano):

nano ~/.vnc/xstartup

Replace the content with the following (adjust for your desktop environment):

Save the file (Ctrl+O, Enter, Ctrl+X) and make it executable:

chmod +x ~/.vnc/xstartup

4. Start the TigerVNC Server

Launch the VNC server with a specified display number (e.g., :1 for port 5901):

vncserver :1

5. Configure Firewall (Allow VNC Traffic)

If UFW (Uncomplicated Firewall) is enabled, allow the VNC port (default: 5901 for :1):

sudo ufw allow 5901/tcp
sudo ufw enable  # Enable firewall if not already active

6. (Optional) Set Up Systemd for Auto-Start

To ensure the VNC server starts at boot, create a systemd service file:

6.1 Create the Service File

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

6.2 Add Configuration

Replace <USER> with your Ubuntu username and adjust the geometry/display settings as needed:

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

[Service]
Type=forking
User=<USER>
Group=<USER>
WorkingDirectory=/home/<USER>
PIDFile=/home/<USER>/.vnc/%H:%i.pid
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

6.3 Enable and Start the Service

Reload systemd, enable the service, and start it:

sudo systemctl daemon-reload
sudo systemctl enable vncserver@1.service  # Replace "1" with your display number
sudo systemctl start vncserver@1.service

7. Connect to the Ubuntu Desktop Remotely

On a remote computer (Windows/macOS/Linux), use a VNC client (e.g., RealVNC Viewer, TigerVNC Viewer) to connect:

  1. Open the client and enter the Ubuntu server’s IP address followed by the display number (e.g., 192.168.1.100:1).
  2. Enter the VNC password you set in Step 2.
  3. The remote desktop will load with your configured environment.

Troubleshooting Tips

By following these steps, you can securely share your Ubuntu desktop using TigerVNC and access it from any remote device.

0
看了该问题的人还看了