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
- Key Notes:
- XFCE is lightweight and ideal for servers; replace with
ubuntu-desktopif you prefer the default GNOME environment. tigervnc-commonprovides essential utilities for VNC configuration.
- XFCE is lightweight and ideal for servers; replace with
3. Set a VNC Password
Configure a password for VNC access (required for all connections). The password must be 6–8 characters long:
vncpasswd
- The password is encrypted and stored in
~/.vnc/passwd. - Important: This is not your system user password—keep it secure.
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
- Why?: Ensures the correct desktop environment starts. Without this, you may see a blank screen.
- Save changes (
Ctrl+O,Enter,Ctrl+X) and make the file executable:chmod +x ~/.vnc/xstartup
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
- Key Parameters:
-depth 24: Sets color depth (24-bit for best quality).-geometry 1920x1080: Defines screen resolution. Adjust to your preference.
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
- To check the server status:
sudo systemctl status 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
- Note: If you change the display number (e.g.,
:2), adjust the port to5902(5900 + display number).
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
- Black Screen: Ensure
~/.vnc/xstartupis correctly configured and executable. - Connection Refused: Verify the VNC server is running (
sudo systemctl status vncserver@:1.service) and the firewall allows the port. - Resolution Issues: Modify the
-geometryparameter in the systemd service orvncservercommand.
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.
以上就是关于“Ubuntu VNC如何安装VNC服务器”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm