Although in most cases logging in to an Ubuntu server via SSH is enough, some programs occasionally need to run in a graphical interface. In that case, you can use VNC Server. The following records a basic method for installing and starting VNC Server on Ubuntu.
Step 1: Install VNC Server
First update the package sources, then install VNC Server. On older Ubuntu versions, you can use vnc4server:
sudo apt-get update
sudo apt-get install vnc4server
If vnc4server is no longer available in your current system's package sources, you can use a common alternative package, such as TigerVNC:
sudo apt-get install tigervnc-standalone-server tigervnc-common
The exact package names may vary depending on the Ubuntu version. Before installing, you can confirm them with the following command:
apt-cache search vnc server
Step 2: Set the VNC Password
The VNC password cannot be too short. Run:
vncpasswd
Enter and confirm the password as prompted:
Password: ******
Verify: ******
Step 3: Check the Firewall
VNC's default display number :1 usually corresponds to TCP port 5901, :2 corresponds to 5902, and so on. Before connecting, confirm that the server firewall, security group, or routing rules allow access to the corresponding port.
If you use ufw, you can check the current status:
sudo ufw status
To open the port corresponding to :1:
sudo ufw allow 5901/tcp
For production servers, it is not recommended to expose the VNC port directly to the public internet; a safer approach is to connect through SSH tunnel forwarding.
Step 4: Start VNC Server
Start a VNC session:
vncserver
If successful, you will usually get a display number similar to hostname:1. You can also explicitly specify the display number:
vncserver :1
View the running VNC sessions:
vncserver -list
Stop a session, for example :1:
vncserver -kill :1
Step 5: Connect Through a Client
In the VNC client, enter the server address and append the display number, for example:
server-ip:1
Or use the port directly:
server-ip:5901
Enter the VNC password you set earlier to connect.
Original reference: Linux Commune website (www.linuxidc.com), original link: http://www.linuxidc.com/Linux/2007-11/9252.htm
