溫馨提示×

Ubuntu VNC如何安裝和配置

小樊
42
2025-09-25 02:54:08
欄目: 智能運維

Installing and Configuring VNC on Ubuntu

VNC (Virtual Network Computing) enables remote access to an Ubuntu system’s graphical desktop. Below is a structured guide to installing, configuring, and securing a VNC server on Ubuntu.

1. Update System Packages

Before installing VNC software, update your system’s package list to ensure compatibility with the latest versions:

sudo apt update

2. Install VNC Server and Desktop Environment

For a lightweight and efficient setup, use TigerVNC (recommended) with the XFCE desktop environment (less resource-intensive than GNOME):

sudo apt install tigervnc-standalone-server tigervnc-common xfce4 xfce4-goodies -y
  • TigerVNC: A high-performance VNC server with encryption support.
  • XFCE: A minimal desktop environment ideal for remote access (avoids lag on slower networks).

3. Set a VNC Password

Configure a password for VNC authentication (required for all connections). The password must be between 6–8 characters long:

vncpasswd
  • Enter and confirm your password. The encrypted password will be saved to ~/.vnc/passwd.

4. Configure the VNC Startup File

Customize the ~/.vnc/xstartup file to launch the XFCE desktop when the VNC server starts. This file defines the session environment:

nano ~/.vnc/xstartup

Replace the existing content with the following (for XFCE):

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4
  • Key Lines:
    • unset SESSION_MANAGER: Prevents session management conflicts.
    • exec startxfce4: Launches the XFCE desktop.
  • Save changes (Ctrl+O, Enter) and exit (Ctrl+X).
  • Make the file executable:
    chmod +x ~/.vnc/xstartup
    

5. Start the VNC Server

Launch the VNC server with a specified display number (e.g., :1). The display number corresponds to a TCP port (5900 + display number; :1 = port 5901):

vncserver :1 -geometry 1920x1080 -depth 24
  • Parameters:
    • :1: Display number (adjust as needed, e.g., :2 for port 5902).
    • -geometry 1920x1080: Sets the remote desktop resolution.
    • -depth 24: Configures color depth (24-bit for full color).
  • The server will output connection details (e.g., IP address and port) to the terminal.

6. Configure the Firewall

If your Ubuntu system uses UFW (Uncomplicated Firewall), allow traffic to the VNC port (default: 5901 for :1):

sudo ufw allow 5901/tcp
sudo ufw reload
  • Verify the rule was added:
    sudo ufw status
    

7. (Optional) Enable Auto-Start at Boot

To ensure the VNC server starts automatically after a system reboot, create a Systemd service file:

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

Add the following content (replace your_username with your actual username and :1 with your display number):

[Unit]
Description=VNC Server for user %u at display %i
After=network.target

[Service]
Type=simple
User=your_username
WorkingDirectory=/home/your_username
ExecStartPre=/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1920x1080 :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Save and exit. Then, enable and start the service:

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

8. Connect to the VNC Server

On a remote computer, use a VNC client (e.g., RealVNC, TightVNC, or TigerVNC Viewer) to connect to your Ubuntu system:

  • Enter the server’s IP address and port (e.g., 192.168.1.100:5901 for :1).
  • When prompted, enter the VNC password you set in Step 3.
  • The remote desktop (XFCE) will load, allowing full control of the Ubuntu system.

Troubleshooting Common Issues

  • Black Screen: Ensure ~/.vnc/xstartup has executable permissions (chmod +x ~/.vnc/xstartup) and uses the correct desktop environment (e.g., startxfce4 for XFCE).
  • Port Conflicts: Verify no other service is using the VNC port (5901 by default) using sudo netstat -tulnp | grep 5901.
  • Firewall Blocking: Double-check UFW rules (sudo ufw status) to confirm port 5901 is allowed.
  • Resolution Issues: Adjust the -geometry parameter in the vncserver command (e.g., 1280x720 for smaller screens).

This guide provides a secure and functional VNC setup for Ubuntu, enabling remote desktop access with minimal overhead. For advanced security (e.g., SSH tunneling), consider encrypting VNC traffic using SSH to prevent unauthorized access.

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女