How to install a VNC server on Ubuntu 22.04
Installing a VNC server on Ubuntu 22.04 gives you the opportunity to access your Linux desktop from anywhere. Whether you’re working from home, on a business trip or just want to work from another room — with a VNC server, you’ll always have access to your work environment.
What are the advantages of a VNC server on Ubuntu 22.04?
Using a VNC server on Ubuntu 22.04 offers many opportunities to make everyday work a lot easier. With a VNC server, you can access your Ubuntu desktop from anywhere in the world. This means you don’t have to be at your desk to manage your applications and data. This flexibility is extremely convenient for remote workers, business travellers or people who want to work from different locations. What’s more, setting up a VNC server is relatively easy, so even less tech-savvy users can quickly establish a functioning remote connection.
A VNC server also promotes collaboration within teams. It allows multiple members simultaneous access to the same desktop. This allows teams to work together effectively regardless of their location, to work on projects together and to solve problems in real time. IT administrators and support can also resolve errors remotely without having to be on site. This saves time and costs for travel and additional appointments. In terms of security, many VNC servers on Ubuntu 22.04 have robust options. Encrypted connections and various authentication methods protect your data and ensure that only authorised people have access to your system.
These are the requirements for a VNC server on Ubuntu 22.04
First, you need a working installation of Ubuntu 22.04. Many VNC servers are fully compatible with this operating system and all required packages and dependencies are provided in the official Ubuntu repositories. Make sure your system is up to date to avoid possible complications during installation. In general, you should fulfill the following requirements:
- Ubuntu 22.04: A working and up-to-date installation of Ubuntu 22.04.
- Network connection: A stable Ethernet or WLAN connection for remote access.
- Root authorisations: The VNC server user requires root rights for installation and configuration.
- Software package: Installation of VNC server software, such as TigerVNC, RealVNC or x11vnc from the Ubuntu repositories.
- Desktop environment: The required desktop environment (e.g., GNOME, XFCE) should be installed.
- Firewall configuration: Open port 5900 or adjust the firewall rules to allow VNC traffic.
Try out your VPS for 30 days. If you're not satisfied, we'll fully reimburse you.
How to install a VNC Server on Ubuntu 22.04
There are many options when it comes to choosing a VNC server and desktop environment. In this tutorial, we’ll install the latest packages for the XFCE desktop environment and the TigerVNC package available in the official Ubuntu repository. XFCE and TigerVNC both require few resources and have fast performance. As a result, the VNC connection remains smooth and stable even with slower internet connections.
Step 1: Update packages
Before you start the installation, make sure that your system is up to date. Execute the following commands to install all available updates:
sudo apt update
sudo apt upgrade
bashStep 2: Install the desktop environment
The VNC server requires a desktop environment as it provides the desktop over the network. Ubuntu 22.04 comes with GNOME by default, but you can also install other desktop environments such as XFCE, which is particularly lightweight.
sudo apt install xfce4 xfce4-goodies
bashThe xfce4-goodies
package contains additional useful tools and plugins for XFCE.
Step 3: Create a new user
After you have installed the desktop environment, you must create a new user to operate the VNC server. It’s advisable not to do this as a root user to ensure security. In this case, we’ll create a new user with the name ‘john’ and give him the necessary permissions to run the VNC server under this user account.
First, you must create the user ‘john’ and assign him a password. You can do this with the following commands:
sudo useradd -m -s /bin/bash john
sudo passwd john
bashThe command sudo useradd -m -s /bin/bash john
creates a new user ‘john’ and sets up a home directory, setting the default shell to /bin/bash
. Then use sudo passwd john
to set a password for this user. It’s essential to choose a secure password.
After the user has been created, you must add it to the sudo group so that it can execute administrative commands. You do this with the following command:
sudo usermod -aG sudo john
bashThis command enables ‘john’ to execute commands with extended rights that are provided with sudo.
To check whether ‘john’ has the correct authorisations, log in as ‘john’ and test the root rights. To do this, execute these commands:
su - john
sudo su
bashWith su - john
you switch to the user ‘john’ and with sudo su
you gain root rights, provided the password was entered correctly. If the authentication is successful, you’ll see a prompt such as root@hostname
, which confirms that you have root rights.
Step 4: Install the VNC server software
For this tutorial, we’ll use TigerVNC as it’s a widely used and well-supported VNC server option. Install TigerVNC with this command:
sudo apt install tigervnc-standalone-server tigervnc-common tigervnc-tools
bashThis entry installs the VNC server software as well as some necessary common packages that are required for operation.
Step 5: Create the VNC server instance
Before you can start the VNC server, you must set a password that’s required to access the VNC server. Enter the following command in the terminal:
vncserver
bashYou’ll be asked to enter and confirm a password. This password protects access to your VNC server. You can also select an option to set a view-only password if you want other users to only see the screen but not have control.
When the server is started for the first time, a new display is created, typically :1
. The output shows the exact number of the display that you can use.
Here’s an example:
New 'X' desktop is your-machine-name:1
bashMake a note of the display number (in this case :1
), as you’ll need this to connect to the VNC client.
Step 6: Configure the VNC server instance
Create a configuration file for the VNC server to ensure that XFCE is loaded when the VNC server is started. The file ~/.vnc/xstartup
can have the following content, for example:
#!/bin/sh
# Start up the standard system desktop
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/usr/bin/startxfce4
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
x-window-manager &
bashThe file becomes executable when you use this command:
chmod +x ~/.vnc/xstartup
bashStep 7: Run VNC server as a system service
By setting up the VNC server as a Systemd service, you can start, stop and restart it like any other service. You can also use Systemd commands to start the VNC server automatically when the system boots up.
First, create a new file with a text editor of your choice:
nano /etc/systemd/system/vncserver@.service
bashAdd the following content to the file:
[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:%i.pid
ExecStart=/usr/bin/vncserver %i
ExecStop=/usr/bin/vncserver -kill %i
[Install]
WantedBy=multi-user.target
bashReplace <your-username>
with your actual username. Load the new service file and activate the service:
sudo systemctl daemon-reload
sudo systemctl enable vncserver@1.service
sudo systemctl start vncserver@:1
bashThe command sudo systemctl enable vncserver@1.service
activates the VNC server service for display :1
so that it starts automatically the next time the system is started. The systemd service file vncserver@.service
is used for this, in which %i
is replaced by 1
. This makes it possible to configure the VNC server for the display :1
and to ensure that it starts every time the system boots. The command creates the necessary shortcuts to include the service in the correct startup process.
Step 8: Configure the firewall
If you have activated a firewall on your Ubuntu server, you must release the VNC port to allow remote connections. By default, the VNC server uses port 5900, followed by the display number. So, for display :1
the port is 5901.
Open the port using ufw
:
sudo ufw allow 5901/tcp
bashCheck the firewall rules to make sure that the port is open:
sudo ufw status
bashHere’s an example of a possible output:
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
5901/tcp ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
5901/tcp (v6) ALLOW Anywhere (v6)
bashStep 9: Connect to the VNC server
To establish a connection to your VNC server, you need a VNC client on your local computer. Popular VNC clients are RealVNC, TigerVNC Viewer and TightVNC Viewer. To improve the security of your VNC connection, you should consider using an encrypted connection via SSH. This protects the communication between your VNC client and the server.
Establish a connection to your server via SSH and forward the VNC port:
ssh -L 59000:localhost:5901 -C -N -l <your-username>@<server-ip>
bashssh
: The command for the secure connection to a remote server via the SSH protocol, which transmits data in encrypted form.-L 59000:localhost:5901
: Enables local port forwarding. Traffic from port 59000 on your local computer is forwarded through the SSH tunnel to port 5901 on the remote server.localhost
refers to the remote server on which the VNC server is running.C
: Activates the compression of data traffic. This can improve the transmission speed and reduce bandwidth utilisation, especially with slow connections.N
: Prevents commands from being executed on the remote server. This mode is useful if you only want to set up the tunnel without starting a shell on the remote server.-l <your-username>
: Specifies the username with which you log in to the remote server.<server-ip>
: The IP address or host name of the remote server to which you want to connect via SSH.
Replace <your-username>
with your username and <server-ip>
with the IP address of your server.
Connect to the local port 5901 that you forwarded via the SSH tunnel by entering localhost:5901
in your VNC viewer.
This method guarantees that your VNC connection is made via an encrypted SSH connection and is therefore more secure.
Step 10: Manage the VNC server
To stop or restart the VNC server, use the following commands:
Stop VNC server:
vncserver -kill :1
bashRestart VNC server:
vncserver :1
bashExit VNC server (with systemd):
sudo systemctl stop vncserver@:1
bashCheck status:
sudo systemctl status vncserver@1
bashOne possible output could be:
vncserver@1.service - Start TigerVNC server at startup
Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2024-07-29 16:57:26 UTC; 20s ago
Process: 97088 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=2)
Process: 97092 ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :1 (code=exited, status=0/SUCCESS)
Main PID: 97103 (Xtigervnc)
Tasks: 77 (limit: 4665)
Memory: 146.7M
CPU: 4.459s
CGroup: /system.slice/system-vncserver.slice/vncserver@1.service
. . .
bashStep 11: Troubleshooting
If you encounter problems, check the following:
- Log files: The log files for the VNC server are located in
~/.vnc
. Check these files for error messages that may indicate possible problems. - Network connection: Make sure that no network restrictions or firewalls are blocking access to the VNC port.
- Configuration files: Make sure that there are no errors in the configuration of the desktop environment in the file
~/.vnc/xstartup
.
You now have the option of conveniently managing your files and programs as well as your system settings via a VNC server. If you prefer to use Windows, we recommend our tutorial ‘Install VNC server on Windows’.
- Enterprise hardware
- Power and flexibility
- Latest security technology