VNC is a system that allows you to remotely control another computer. It allows you to relay your mouse and keyboard inputs as if you were physically sitting in front of the system, when in fact you could be on the other side of the world.
In this guide, we will go over the steps to setup a VNC server on your Ubuntu 20.04 system. When we’re done, you’ll be able to access your system remotely from anywhere, provided that your client system and the VNC server have an internet connection.
In this tutorial you will learn:
- How to install and configure TightVNC Server
- How to install and configure XFCE desktop manager
- How to connect to our VNC server with TightVNC Viewer
- How to tunnel VNC connections through SSH
- How to manage multiple VNC desktops with a Systemd script
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Installed or upgraded Ubuntu 20.04 Focal Fossa |
Software | TightVNC Server, XFCE desktop manager, TightVNC Viewer |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Install VNC server
We’ll need to install a few packages first. There are multiple options for VNC server software, but we’ll be going with TightVNC Server for this tutorial. Along with VNC, we also need a graphical desktop. XFCE is a good choice, so that’s what we’ll be covering here.
- Start by opening a terminal and entering the following command to install TightVNC server and the XFCE desktop manager core files:
$ sudo apt install tightvncserver XFCE4 XFCE4-goodies
- After the packages are done installing, we need to configure a username and password that will be used to connect to the VNC server by a client. Create a password with this command:
$ vncpasswd
- Next, let’s configure VNC to start the XFCE desktop environment when a client connects. Use nano or the text editor of your preference (ex. Atom, Sublime) to create the following file:
$ nano ~/.vnc/xstartup
- Insert the following few lines and then save your changes and exit the file:
#!/bin/sh unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS startxfce4 &
- With those changes made, edit the permissions of the file to make it executable:
$ chmod +x ~/.vnc/xstartup
Start VNC server
VNC runs as a service on your system. In order for clients to connect, the VNC server must obviously be running and listening for incoming connection attempts. With our configuration complete, we are ready to start VNC server with the following command:
$ vncserver
If your terminal returns some output like in the screenshot below, your VNC server is running correctly.
VNC will use a new port for every remote desktop that is created. At this point, your system should be listening on port 5901
for incoming VNC connections. You can see this for yourself with the ss -ltn
command:
$ ss -ltn
If you have the UFW firewall enabled, you’ll need to open port 5901
so it doesn’t block incoming VNC connections:
$ sudo ufw allow from any to any port 5901 proto tcp Rule added Rule added (v6)
Connect to VNC server
There are a lot of choices in the way of VNC clients and any of them should be capable of connecting to your newly launched VNC server. If you don’t already have a preferred client to use, follow along with us as we cover the instructions for connecting to the VNC server with the TightVNC Viewer.
- Start by installing the
xtightvncviewer
package on your Ubuntu client system:$ sudo apt install xtightvncviewer
- Once the VNC client is installed, you can use the
vncviewer
command, followed by either the hostname or IP address of the VNC server, in order to connect to it.$ vncviewer linuxconfig.org:1
Enter your password that we created previously when setting up VNC Server. If all went well, you will be presented with a XFCE desktop session running on the remote VNC server Ubuntu system:
Tunnel VNC through SSH
For extra security, you can tunnel the VNC connection through SSH on your VNC server. Of course, this is assuming that you have SSH access on the VNC server. If you’d like the added security, follow along with these steps:
- If you don’t already have SSH installed, that’s a pretty obvious prerequisite for this to work:
$ sudo apt install ssh
- Next, create an SSH tunnel on a local port 5901 leading to a remote port 5901 on your VNC server. In the following command, make sure you replace
user1
andlinuxconfig
with the username and hostname of your VNC server:$ ssh -L 5901:127.0.0.1:5901 -N -f -l user1 linuxconfig
The above command will open a local port
5901
on a localhost loopback network interface127.0.0.1
:$ ss -ltn State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 5 127.0.0.1:631 0.0.0.0:* LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* LISTEN 0 128 127.0.0.1:6011 0.0.0.0:* LISTEN 0 128 0.0.0.0:38299 0.0.0.0:* LISTEN 0 128 127.0.0.1:5901 0.0.0.0:*
- Next, use the local port
5901
to connect to a remote VNC server via the SSH tunnel:$ vncviewer localhost:1
VNC server system startup script
While this configuration works, you may have a scenario where you need to manage multiple VNC desktop sessions. In that case, creating a systemd startup script can facilitate that.
Use nano or another text editor to create the following file:
$ sudo nano /etc/systemd/system/vncserver@.service
Once you have the file opened, insert the following lines while replacing the user1
username with the username of your VNC user on Line 7 and Line 10. Optionally, change the screen resolution settings and apply other vncserver
options or arguments:
[Unit]
Description=Systemd VNC server startup script for Ubuntu 20.04
After=syslog.target network.target
[Service]
Type=forking
User=user1
ExecStartPre=-/usr/bin/vncserver -kill :%i &> /dev/null
ExecStart=/usr/bin/vncserver -depth 24 -geometry 800x600 :%i
PIDFile=/home/user1/.vnc/%H:%i.pid
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target
Next, reload Systemd for the changes to take effect:
$ sudo systemctl daemon-reload
To start VNC desktop 1
, enter:
$ sudo service vncserver@1 start
To stop VNC desktop 1
, execute:
$ sudo service vncserver@1 stop
The following linux command will enable the VNC desktop 1
to start after reboot:
$ sudo systemctl enable vncserver@1
To start VNC desktop 2
, enter:
$ sudo service vncserver@2 start
And so on…
Conclusion
In this guide, we saw how to install and configure TightVNC Server on Ubuntu 20.04 Focal Fossa. We installed XFCE desktop manager for our VNC clients to use when they connect.
We also learned how to use TightVNC Viewer to connect remotely to our VNC server. On top of all this, we also covered how to tunnel VNC connections through SSH for extra security, and use a custom Systemd startup script to efficiently manage multipe VNC desktop connections.