温馨提示×

如何在Ubuntu上共享TigerVNC桌面

小樊
54
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
  • Enter and confirm your password (max 8 characters).
  • Skip the “view-only password” prompt unless you need read-only access.

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):

  • For GNOME (default on Ubuntu Desktop):

    #!/bin/sh
    unset SESSION_MANAGER
    unset DBUS_SESSION_BUS_ADDRESS
    xrdb $HOME/.Xresources
    vncconfig -iconic &
    gnome-session &
    
  • For Xfce (lightweight alternative):

    #!/bin/sh
    unset SESSION_MANAGER
    unset DBUS_SESSION_BUS_ADDRESS
    xrdb $HOME/.Xresources
    startxfce4 &
    

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
  • The first instance uses port 5901 (:1 = 5900 + 1).
  • Check active sessions with:
    vncserver -list
    

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

  • Port Conflicts: Ensure no other service uses the VNC port (default: 5901 for :1). Check with netstat -tulnp | grep 5901.
  • Black Screen: Verify the xstartup file has execute permissions (chmod +x ~/.vnc/xstartup) and matches your desktop environment.
  • Firewall Issues: Confirm UFW allows the VNC port (sudo ufw status).
  • SSH Tunneling (Secure Connection): For added security, use SSH to forward the VNC port locally:
    ssh -L 5901:localhost:5901 <USER>@<SERVER_IP>
    
    Then connect the VNC client to localhost:1.

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

0