How to Install & Configure VNC Server on Ubuntu 20.04

How to Install & Configure VNC Server on Ubuntu 20.04

VNC (Virtual Network Computing) is a visual connection system that enables you to interact with the graphical desktop environment of a remote PC using a mouse and a keyboard.

If you have worked with Microsoft Remote Desktop Protocol (RDP) before, think of VNC as an open-source alternative.

VNC is quite a lifesaver for many who are not comfortable working from the command-line and need to manage files, install software and configure settings on a remote server.

In this tutorial we will go step-by-step through installing and configuring the VNC Server on an Ubuntu 20.04 machine, and we’ll look at how we can connect to it via VNC desktop client on our other PC using a secure SSH tunnel.

We will also install some of the most popular desktop environments and configure the VNC server to use them.

Let’s dive in and get started.

Prerequisites

  • A machine running Ubuntu 20.04 to which we’ll connect.
  • Acting as a non-root sudo user for security reasons. You can see our tutorial on creating a sudo user in Ubuntu 20.04 if you need to.
  • A VNC Client (also called VNC Viewer) which is the software that you’ll run on your local machine and will enable you to control your VNC server remotely. There are some popular VNC viewers that you can choose from. The one I’m using in this tutorial is Real VNC Viewer.
    It’s available for Windows, macOS, Linux, Android, and others. You can choose your OS and download Real VNC Viewer here. If you’re on Linux then I prefer Remmina.

Step 1: Install a Desktop Environment

If you intend to connect via VNC to an Ubuntu 20.04 server, then you’ll notice that the server doesn’t come with a preinstalled Desktop Environment. On booting up, you will get a command-line interface to execute your commands.

However, that doesn’t mean you can install one.

As of writing this post, there are several Linux desktop environments available for you to install. They include GNOME desktop, KDE Plasma Desktop, Mate Desktop, Budgie Desktop, Xfce/Xubuntu Desktop, Cinnamon Desktop, and many more.

For this particular post, we will install the XFCE desktop environment. It is fast, stable, and easy to use.

After establishing an SSH connection to your server, execute the following command to update your package index:

sudo apt update

Next we’ll run the following command to install xfce4 and xfce4-goodies:

sudo apt install xfce4 xfce4-goodies

xfce4-goodies is an additional package for XFCE Desktop Environment, which brings a lot more advanced enhancement.

You will see a prompt to select a display manager for your newly installed XFCE Desktop Environment during the installation.

select_display_manager

A Display Manager is a program that enables your Linux distribution to possess graphical login capabilities. It is also responsible for starting the Display Server and the Desktop Environment once you log in with your username and password.

Select any display manager and press Enter.

Step 2: Install VNC server

After successfully installing the XFCE desktop environment, we can proceed to install the VNC server.

There are several VNC servers available for Linux today. They include TightVNC, x11VNC, and TigerVNC.

For this particular tutorial, we will install the TigerVNC server.

The reason for choosing TigerVNC is because I got it to work with the most desktop environments.

  • Initially I tried TightVNC and got it to work with XFCE, LXQt, LXDE, KDE Plasma, and Gnome Flashback, but struggled with GNOME 3 and Budgie. I didn’t try MATE or Cinnamon, however.

Install TigerVNC on Ubuntu

To install TigerVNC execute the command below:

sudo apt install tigervnc-standalone-server

After a successful installation, we need to perform the initial VNC configuration, set up a VNC access password and initialize the VNC server.

Execute the command below to initialize the VNC server instance and set up a password. Assuming you are acting as a non-root sudo user, do NOT execute this command with sudo:

vncserver
Set VNC Password
You will require a password to access your desktops.

Password:
Verify:

After setting up the password, you will get a prompt to set a View-Only password. That means anybody who accesses the VNC server with a view-only password will not be able to VNC desktop with either Mouse or Keyboard.

For this particular post, we won’t set up a view-only password. I will just type N and hit Enter.

Finish Setting VNC Password
Would you like to enter a view-only password (y/n)? N
/usr/bin/xauth:  file /home/edxd/.Xauthority does not exist

New 'bytexd:1 (edxd)' desktop at :1 on machine bytexd

Starting applications specified in /etc/X11/Xvnc-session
Log file is /home/edxd/.vnc/bytexd:1.log

Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/edxd/.vnc/passwd :1 to connect to the VNC server.

We can see this process lists the connection information for our VNC server from the message above. Additionally, it started the VNC server at port 5901, which is the display port. This port (5901) is referred to as :1.

If you create additional VNC server instances, they will be presented as port 5902 as :2, port 5903 as :3, and so on.

Up to this point, our VNC server is up and running. However, it cannot give us access to a graphical interface since it’s not configured to launch our XFCE desktop environment;

Tip: If you ever want to change the VNC password or the view-only password, execute the command below:

vncpasswd

Step 3: Configure the VNC server

Up to this point, we have successfully installed the VNC server and even set up a password.

Now we need to configure the commands that will be executed by the server every time we start a VNCV instance.

One main goal is telling VNC which Desktop Environment to connect and use – in this case, XFCE.

To get started, let’s kill the running VNC instance that we launched in Step 2 above running on port 5901. Execute the command below:

vncserver -kill :1
Output
Killing Xtigervnc process ID 28634... success!

If another instance were running on another port, say 5902 or 5903, we would execute the commands vcnserver -kill :2 and vncserver -kill :3, respectively.

To configure VNC, we will need to create a file called xstartup file in the .vnc folder under the home directory (~/.vnc/xstartup). This is where we’ll configure what desktop environment we want the VNC server to use.

Now, let’s create and open a new xstartup file with the nano editor. Execute the commands below:

nano ~/.vnc/xstartup

Add the lines below on the editor:

~/.vnc/xstartup
#!/bin/sh
xrdb $HOME/.Xresources
startxfce4 &

Save the file (Ctrl + O, then Enter) and Exit (Ctrl + X). Now let’s go line by line and explain the code above:

  • #!/bin/sh: This line, commonly referred to as shebang, tells the system which interpreter we will be using. In this case, it’s the bash interpreter. Other Linux shells include Almquist shell, KornShell, etc.
  • xrdb $HOME/.Xresources: This line tells the VNC server to read the .Xresources file where users can make changes on the graphical desktop general settings like fonts, color, etc.
  • startxfce4 &: This line tells VNC which Desktop Environment to launch. startxfce4 is a script responsible for starting an Xfce session.

Lastly, we need to make this file executable. Run the command below:

chmod +x ~/.vnc/xstartup

Once done, proceed to restart the VNC server with the command below.

vncserver -localhost no :1
Restart VNC Output
New 'bytexd:1 (edxd)' desktop at :1 on machine bytexd

Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/edxd/.vnc/passwd :1 to connect to the VNC server.

The above command launches a VNC server instance on port 5901.

Why we used -localhost no

By default, TigerVNC accepts connection only from 127.0.0.1 or localhost, for security reasons. Since we’re just testing now, we added -localhost no to be able to access the server via VNC from the outside.

We’ll cover this in a little bit more detail further in this article.

As such, before proceeding and looking at how to create a secure connection, let’s test the VNC server.

Connecting to your VNC Remote Desktop

Launch your VNC Client (or VNC Viewer) application on your local machine and connect to the VNC server using server_ip_address:1 or server_ip_address:5901.

In my case, I will use 149.28.227.198:1 (I’m using a Vultr server), and my VNC Client is Real VNC Viewer, which supports many operating systems including Windows, macOS and Linux.

VNC with XFCE Desktop Environment

From the image above, you can now see our VNC server is well configured, and we can access it from our local machine.

Close the VNC client desktop session and kill the VNC instance on the server using the command below:

vncserver -kill :1

Step 4: Establish a secure connection to the VNC Desktop

To establish a secure connection, restart your VNC server by simply running vncserver without the -localhost no option as shown below.

vncserver
Output
New 'bytexd:1 (edxd)' desktop at :1 on machine bytexd

Starting applications specified in /home/edxd/.vnc/xstartup
Log file is /home/edxd/.vnc/bytexd:1.log

Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/edxd/.vnc/passwd :1 to connect to the VNC server.

By default, TigerVNC by default, when you don’t run -localhost no, binds the server to your Ubuntu 20.04 server loopback interface. That ensures that the VNC server only accepts connections incoming from the server where it is installed.

To get around this and connect to the VNC server from our local machine, we will establish an SSH tunnel from our local machine to the server. That is also an additional layer of security as only users with SSH access to the server can connect to the VNC server.

We’ll set up our SSH tunnel in one of two ways, depending on your preference – by running a command in your terminal or by configuring it in Putty. You can choose whichever one you prefer.

Set up an SSH Tunnel with Your Terminal

You’re probably familiar with your terminal if you are on Linux or macOS. On Windows you can use PowerShell or a terminal emulator such as Cmder.

Execute the command below on your local machine terminal emulator.

ssh -L 59000:localhost:5901 -C -N -l server_user_name server_ip_address

Remember to replace the server_user_name and server_ip_address accordingly. In my case, I will execute the command below:

ssh -L 59000:localhost:5901 -C -N -l edxd 149.28.227.198
Note: After executing this command, the SSH connection will hang after you enter the password. Don’t worry that is perfectly fine.

When you are done with using the VNC Desktop session, you can kill it using Ctrl + C. Alternatively; you can add a -f argument which runs SSH tunneling in the background.

Session Hangs

Lets, dissect the SSH tunneling command above:

  • -L 59000:localhost:5901: The -L argument specifies the post on your local computer (ours is 59000) to the given host (localhost) and port (5901) on the destination server (server_ip_address).
    Note: The local port, 59000, can be any number but not more than 65535 and not running another service.
  • -C: This argument speeds up processes and minimizes resource usage.
  • -N: This argument prevents execution of remote commands and tells SSH to only forward ports.
  • -l server_user_name server_ip_address: This line specifies the user you will use to login to the server. Make sure the user is non-root and doesn’t have root privileges.

Now to connect securely to your VNC server, launch the VNC client application and connect with the address localhost:59000:

VNC Ubuntu XFCE

Set up an SSH Tunnel with Putty

If you are connecting to your server using Putty, create an SSH tunnel using the procedure below. Launch Putty and enter your server’s address in the session > hostname.

Putty Tunnel VNC 1

On the left side of the Putty window, scroll down and get to the SSH option. Extend it and select the Tunnels option. Enter the details as shown in the image below—Port 59000 as the source port and localhost:5901 as the destination address.

Putty Tunnel VNC 2

When done, click Add, then Open/Apply to create the SSH tunnel. You can now connect to the VNC server using the VNC client address: localhost:59000.

Step 5: Setting up the VNC as a system service

The advantage of setting up VNC as a service is that we can start, stop, restart or even view its status (whether it’s running or not) just like we would for other system services. We will also use systemd to launch our VNC server on boot.

To get started, let’s create a unit file called [email protected] using the command below:

sudo nano /etc/systemd/system/[email protected]

Paste the lines below and remember to replace the YOUR_USERNAME text with your username, and -depth 24 -geometry 1280x800 with the depth and geometry you prefer.

  • -depth: represents the color depth, in bits per pixel. The value can be between 8 and 32.
  • -geometry: the width and height of the desktop.
[Unit]
Description=Start TigerVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=YOUR_USERNAME
Group=YOUR_USERNAME
WorkingDirectory=/home/YOUR_USERNAME

PIDFile=/home/YOUR_USERNAME/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Save the file (Ctrl + O, then Enter) and Exit (Ctrl + X).

Now we need to make the system aware of our new unit file. Execute the commands below:

sudo systemctl daemon-reload
sudo systemctl enable [email protected]
Output
Created symlink /etc/systemd/system/multi-user.target.wants/[email protected] → /etc/systemd/system/[email protected]

With that done, we can now start, stop and restart our VNC server as a system service. To test this, let’s first stop the instance that we had launched previously with the command below:

vncserver -kill :1

Once done, let’s now start the VNC server as a service. Execute the command below:

sudo systemctl start [email protected]

To check its status and confirm whether its running or not, use the command below:

sudo systemctl status [email protected]
[email protected] - Start TigerVNC server at startup
     Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-07-23 11:37:59 UTC; 5s ago
    Process: 83157 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=0/SUCCESS)
    Process: 83162 ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :1 (code=exited, status=0/>   Main PID: 83170 (Xtigervnc)
      Tasks: 166 (limit: 2270)
     Memory: 236.5M
     CGroup: /system.slice/system-vncserver.slice/[email protected]
             ├─83170 /usr/bin/Xtigervnc :1 -desktop bytexd:1 (edxd) -auth /home/edxd/.Xauthority -geometry 1280x80>             ├─83180 xfce4-session
             ├─83191 /usr/bin/dbus-launch – sh-syntax – exit-with-session xfce4-session
             ├─83192 /usr/bin/dbus-daemon – syslog – fork – print-pid 5 – print-address 7 – session
             ├─83198 /usr/libexec/at-spi-bus-launcher
             ├─83203 /usr/bin/dbus-daemon – config-file=/usr/share/defaults/at-spi2/accessibility.conf – nofork – >             ├─83207 /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd
             ├─83213 /usr/libexec/at-spi2-registryd – use-gnome-session
             ├─83219 /usr/bin/ssh-agent -s
             ├─83223 xfwm4
             ├─83227 /usr/libexec/gvfsd
             ├─83238 xfsettingsd
             ├─83239 xfce4-panel
             ├─83245 Thunar – daemon
             ├─83250 xfdesktop
             ├─83251 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugi>             ├─83252 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugi>
     ├─83253 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugi>             ├─83256 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugi>             ├─83257 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugi>             ├─83270 /usr/lib/x86_64-linux-gnu/xfce4/notifyd/xfce4-notifyd
             ├─83273 /usr/libexec/geoclue-2.0/demos/agent
             ├─83281 xiccd
             ├─83282 /usr/lib/x86_64-linux-gnu/tumbler-1/tumblerd
             ├─83283 /usr/bin/python3 /usr/share/system-config-printer/applet.py
             ├─83287 nm-applet
             ├─83289 /usr/libexec/evolution-data-server/evolution-alarm-notify
             ├─83302 /usr/libexec/dconf-service
             ├─83308 xfce4-power-manager
             ├─83339 /usr/libexec/evolution-source-registry
             ├─83348 /usr/libexec/goa-daemon
             ├─83356 /usr/libexec/goa-identity-service
             ├─83363 /usr/libexec/evolution-calendar-factory
             ├─83376 /usr/libexec/evolution-addressbook-factory
             ├─83394 /usr/libexec/gvfs-udisks2-volume-monitor
             ├─83400 /usr/libexec/gvfs-goa-volume-monitor
             ├─83405 /usr/libexec/gvfs-gphoto2-volume-monitor
...

From the image above, we can see that we have successfully started VNC as a service.

That’s it! You have successfully installed and configured the VNC server on Ubuntu 20.04 LTS, and you can now manage it just like any system service.

Configuring VNC to Use Other Desktop Environments (And Switching Between Them)

To configure VNC to use a desktop environment with TigerVNC we have to:

  1. Install the desktop environment on our Ubuntu 20.04 machine
  2. Appropriately edit the ~/.vnc/xstartup file where we tell VNC what applications to start on our behalf when it creates a new desktop. In this section, where we mention what contents to add to the ~/.vnc/xstartup file, we’ll assume the file is empty.
  3. Restart the VNC server after you’ve edited the ~/.vnc/xstartup file.
    If you set up the VNC service as a system service you can restart it by running:

    sudo systemctl start [email protected]

    If you don’t have it set up as a system service then you can just kill the VNC instance and start it again:

    vncserver -kill :1
    vncserver
For more details on how to install other desktop environments, please check our related tutorial on how to install a desktop environment (GUI) on an Ubuntu 20.04 server, where you can also find some additional details on each desktop environment. You can also install just minimal versions of desktop environments, with minimal necessary software packages, or full versions with all the bells and whistles. In this tutorial we’ll stick to minimal installs for demonstration purposes.

Please keep in mind that if you install multiple desktop environments on the same machine, then you may encounter bugs or conflicts, depending on which you have installed.

Configure VNC to Use GNOME

As mentioned before, and in case someone stumbles upon this article looking specifically for how to use GNOME with VNC on Ubuntu 20.04, I recommend TigerVNC instead of TightVNC.

To install GNOME on Ubuntu 20.04 we’ll run:

sudo apt install gnome-session gnome-terminal

And to configure VNC to use GNOME create a new ~/.vnc/xstartup file or edit the existing one, and paste in the following:

#!/bin/sh
# Start Gnome 3 Desktop
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
vncconfig -iconic &
dbus-launch --exit-with-session gnome-session &

Make the file executable in case it isn’t already:

chmod +x ~/.vnc/xstartup

Now restart the VNC server, and when you connect to it you should be using GNOME.

Here’s how the GNOME minimal installation looks like in my case:

ubuntu_gnome_3_minimal_tigervnc

And this is how the full GNOME desktop looks like, if you install it with the following command:

sudo apt install ubuntu-desktop
Note: I know I said that we’d install only minimal versions, but I wanted to give you an idea of the differences.

This is how the full GNOME Desktop install it looks like:

ubuntu_gnome_3_tigervnc

Configure VNC to Use XFCE

We covered this initially in the tutorial, but will add it here too, so the article is more easily skimmable.

To install XFCE run:

sudo apt install xfce4 xfce4-goodies

To configure VNC to use XFCE create a new ~/.vnc/xstartup file or edit the existing one, and paste in the following:

#!/bin/sh
xrdb $HOME/.Xresources
startxfce4 &

Make the file executable in case it isn’t already:

chmod +x ~/.vnc/xstartup

Now restart the VNC server and next time you connect you should be using XFCE.

This is what XFCE looks like:

VNC with XFCE Desktop Environment

Configure VNC to Use LXQt

To install LXQt run:

sudo apt install lxqt

To configure VNC to use LXQt create a new ~/.vnc/xstartup file, or edit the existing one, and paste in the following:

#!/bin/sh
xrdb $HOME/.Xresources
startlxqt &

Make the file executable in case it isn’t already:

chmod +x ~/.vnc/xstartup

Now restart the VNC server and next time you connect you should be using LXQt.

This is what LXQt looks like in my case:

ubuntu_vnc_lxqt

Configure VNC to Use LXDE

To install LXDE run:

sudo apt install lxde

To configure VNC to use LXQt create a new ~/.vnc/xstartup file, or edit the existing one, and paste in the following:

#!/bin/sh
xrdb $HOME/.Xresources
startlxde &

Make the file executable in case it isn’t already:

chmod +x ~/.vnc/xstartup

Now restart the VNC server and next time you connect you should be using LXDE.

This is what LXDE looks like in my case:

ubuntu_lxde_tigervnc

Configure VNC to Use MATE Desktop

To install MATE Desktop run:

sudo apt install ubuntu-mate-desktop

To configure VNC to use MATE Desktop create a new ~/.vnc/xstartup file, or edit the existing one, and paste in the following:

#!/bin/sh
xrdb $HOME/.Xresources
exec /usr/bin/mate-session &

Make the file executable in case it isn’t already:

chmod +x ~/.vnc/xstartup

Now restart the VNC server and next time you connect you should be using MATE Desktop.

This is what MATE Desktop looks like in my case:

ubuntu_mate_desktop_tigervnc

Configure VNC to Use Budgie

To install Budgie run:

sudo apt install ubuntu-budgie-desktop

To configure VNC to use Budgie create a new ~/.vnc/xstartup file, or edit the existing one, and paste in the following:

#!/bin/sh
xrdb $HOME/.Xresources
exec budgie-desktop &

Make the file executable in case it isn’t already:

chmod +x ~/.vnc/xstartup

Now restart the VNC server and next time you connect you should be using Budgie.

This is what Budgie looks like in my case:

ubuntu_budgie_tigervnc

Configure VNC to Use KDE Plasma Desktop

We’ll install, KDE Plasma Desktop, which is the minimal installation of Kubuntu. You can see more details on how to also install KDE Standard or KDE Full here.

To install KDE Plasma Desktop run:

sudo apt install kde-plasma-desktop

To configure VNC to use KDE Plasma Desktop create a new ~/.vnc/xstartup file, or edit the existing one, and paste in the following:

#!/bin/sh
xrdb $HOME/.Xresources
startplasma-x11 &

Make the file executable in case it isn’t already:

chmod +x ~/.vnc/xstartup

Now restart the VNC server and next time you connect you should be using KDE Plasma Desktop.

This is what KDE Plasma Desktop looks like in my case:

ubuntu_kde_plasma_tigervnc

Configure VNC to Use Cinnamon

To install Cinnamon run:

sudo apt install cinnamon-desktop-environment

To configure VNC to use Cinnamon create a new ~/.vnc/xstartup file, or edit the existing one, and paste in the following:

#!/bin/sh
xrdb $HOME/.Xresources
cinnamon-session &

Make the file executable in case it isn’t already:

chmod +x ~/.vnc/xstartup

Now restart the VNC server  and next time you connect you should be using Cinnamon.

This is what Cinnamon looks like in my case:

ubuntu_cinnamon_tigervnc

Conclusion

I believe this guide has given you a step-by-step guide on installing and configuring a VNC server on Ubuntu 20.04. Feel free to share any additional VNC configuration tips with our readers or ask any questions in the comments below or by contacting us.

22 Shares:
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Noby
Noby
1 month ago

I followed the steps exactly,
It shows only a black screen hen I access from localhost.

But i works in localhost when I run vncserver as sudo

Nick Floersch
Nick Floersch
1 month ago

The article says to start the server for testing using “vncserver localhost -no :1” … however that should be “vncserver -localhost no :1”

You May Also Like