ubuntu

Ubuntu VNC如何安装VNC服务器

小樊
44
2025-09-29 08:41:12
栏目: 云计算

Installing a VNC Server on Ubuntu: A Step-by-Step Guide
VNC (Virtual Network Computing) enables remote access to your Ubuntu system’s graphical desktop. Below is a structured guide to installing and configuring a VNC server on Ubuntu, optimized for reliability and security.

1. Update System Packages

Before installing software, refresh your system’s package repository to ensure you get the latest versions:

sudo apt update && sudo apt upgrade -y

2. Install VNC Server and Supporting Software

For Ubuntu, the recommended VNC server is TigerVNC (or tightvncserver for lightweight needs). Install it along with a desktop environment (e.g., XFCE for better performance on servers):

# Install TigerVNC server and XFCE desktop (replace with your preferred desktop)
sudo apt install tigervnc-standalone-server tigervnc-common xfce4 xfce4-goodies -y

3. Set a VNC Password

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

vncpasswd

4. Configure the VNC Server

a. Customize Desktop Startup

Edit the ~/.vnc/xstartup file to define the desktop environment launched when connecting via VNC. For XFCE:

nano ~/.vnc/xstartup

Replace the file’s content with:

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4

b. Create a Systemd Service (Optional but Recommended)

For automatic startup at boot and easier management, create a systemd service:

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

Add this configuration (replace your_username with your actual username):

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

[Service]
Type=forking
User=your_username
PAMName=login
PIDFile=/home/your_username/.vnc/%H:1.pid
ExecStartPre=-/usr/bin/vncserver -kill :1 > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1920x1080 :1
ExecStop=/usr/bin/vncserver -kill :1

[Install]
WantedBy=multi-user.target

5. Start and Enable the VNC Server

Reload systemd to apply the new service, then start and enable the VNC server:

# Reload systemd to recognize the new service
sudo systemctl daemon-reload

# Start the VNC server (display :1 = port 5901)
sudo systemctl start vncserver@:1.service

# Enable auto-start at boot
sudo systemctl enable vncserver@:1.service

6. Configure the Firewall

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

sudo ufw allow 5901/tcp
sudo ufw reload

7. Connect to the VNC Server

Use a VNC client (e.g., TigerVNC Viewer, RealVNC, or Remmina) on your local machine. Enter the server’s IP address and port (e.g., 192.168.1.100:5901) and authenticate with the password set in Step 3.

Troubleshooting Tips

By following these steps, you’ll have a secure and functional VNC server on Ubuntu, enabling remote access to your desktop environment from any compatible client.

0
看了该问题的人还看了