2. VNC Server

Often there is a need to operate the workstation remotely but using various graphical user interfaces (GUIs). As an alternative to XServer or commercial remote control programs (e.g. TeamViewer, AnyDesk, etc.) Virtual Network Computing (VNC) is a viable alternative that can often make life easier. This guide shows you how to install on an Ubuntu system (20.04) the server (which is the machine you want to control) and then use it with a client (RealVNC in this case) on any PC.

vnc

Fig. 2.2 A VNC server includes an X server that can communicate with local client programs.

Note

Part of this Guide is an elaboration of the tutorials:

  1. Enable multiuser logins with VNC

  2. How to Install and Configure VNC on Ubuntu 20.04

2.1. VNC Server Installation

VNC is a cross-platform tool that can provide remote access to Linux, UNIX, Mac OS X, Windows and other Operative Systems from any type of client. On Linux there are several programs that allow your computer to become a VNC server, in this guide we install one of the most used TightVNC.

  1. Update everithing

    $ sudo apt update && sudo apt upgrade
    
  2. Install the VNC server client TightVNC server

    $ sudo apt install tightvncserver tigervnc-standalone-server
    

Warning

Please make sure that you are using only vnc-server and no other VNC-server are installed as this could give errors in future mostly that clipboard sharing between the host Ubuntu Server & vnc-client machine. You can check it as follows:

$ dpkg -l | grep vnc

2.2. Install desktop environment (DE)

In addition to VNC, the desktop environment (DE) must be installed, which is necessary to be able to reproduce the entire interface and manage the GUIs of the applications. In the linux mode there are several options, so it’s up to you. Here we propose 3 possible options, from the simplest DE to the most aesthetic.

XFCE is probably the fastest and lightest, while being visually appealing and easy to use DE.

  1. Install XFCE DE

    $ sudo apt install xfce4 xfce4-goodies
    

2.3. Set up VNC session for each user

To avoid interference between different users, the server must be set up for each user. We will assume that on our machine there are 3 users test1, test2 and test3.

Warning

Please note, you are accessing the machine as several users using super user permissions. So be very careful and avoid doing any damage!

  1. Login to test1 using the root privilages.

    $ sudo su # loging as super user
    
    $ su - test1 # loging as test1
    
  2. Now initiate the vnc configuration for test1. This will create the hidden folder .vnc with all the default configuration files.

    $ vncserver
    
    You will require a password to access your desktops through VNC Clients.
    Password:******
    Verify:******
    

    You will be asked for the VNC server password – type your password. And for the “view-only” password, you can enable or disable it. The user who logins to the server using the “view-only” password will not be able to control the mouse and keyboard.

    Now we kill the server that we just start:

    $ vncserver -kill :*
    
  3. Change the configuration file.

    $ cd $HOME/.vnc
    
    $ vim xstartup
    

    Inside the file copy and paste the following lines according the choose Desktop Environment.

     1#!/bin/bash
     2unset SESSION_MANAGER
     3unset DBUS_SESSION_BUS_ADDRESS
     4export XKL_XMODMAP_DISABLE=1
     5export GTK_IM_MODULE=fcitx
     6export QT_IM_MODULE=fcitx
     7export XMODIFIERS=@im=fcitx
     8
     9#__using_dde__
    10session=startxfce4 # XFCE=startxfce4 GNOME=gnome-session DeepinDE=startdde
    11
    12#__enable_clipboard_sync__
    13if [ -f /usr/bin/autocutsel ]
    14then
    15    /usr/bin/autocutsel -fork
    16fi
    17
    18#__deepin_uos_activator__
    19if [ -f /usr/bin/uos-activator ]
    20then
    21    /usr/bin/uos-activator &
    22fi
    23
    24#__enable_fcitx__
    25if [ -f /usr/bin/fcitx ]
    26then
    27    (sleep 15 && /usr/bin/fcitx) &
    28fi
    29
    30#__kill_user_apps_duplicated__
    31killall -9 v2ray &
    32
    33exec dbus-launch $session
    

    Make the file executable

    $ chmod +x xstartup
    

Repeat all the step for all the users (eg. test2)

Note

If you what to chage the VNC server password you can use the following command:

$ vncpasswd

2.4. Running VNC as a Service at startup

With this step we will make crate a systemd service that will start the VNC server at each boot. Even this step has to be repeated for each user.

  1. Make the Unit file.

    $ cd /etc/systemd/system
    
    $ sudo vim vncserver_test1.service # This is for the ``test1`` user
    

    copy and paste the following:

     1[Unit]
     2 Description=Remote desktop service (VNC)
     3 After=syslog.target network.target
     4
     5[Service]
     6  Type=forking
     7  User=USER
     8  PIDFile=/home/USER/.vnc/%H:PORT.pid
     9  ExecStartPre=-/usr/bin/vncserver -kill :PORT > /dev/null 2>&1
    10  ExecStart=/usr/bin/vncserver -depth 24 -geometry 1980x1080 :PORT
    11  ExecStop=/usr/bin/vncserver -kill :PORT
    12
    13[Install]
    14  WantedBy=multi-user.target
    

    replace USER with the user name (eg. test1) and PORT with a sequential number. Below an example:

     1[Unit]
     2 Description=Remote desktop service (VNC)
     3 After=syslog.target network.target
     4
     5[Service]
     6  Type=forking
     7  User=test1
     8  PIDFile=/home/test1/.vnc/%H:1.pid
     9  ExecStartPre=-/usr/bin/vncserver -kill :1 > /dev/null 2>&1
    10  ExecStart=/usr/bin/vncserver -depth 24 -geometry 1980x1080 :1
    11  ExecStop=/usr/bin/vncserver -kill :1
    12
    13[Install]
    14  WantedBy=multi-user.target
    

    Warning

    The number choose for PORT will correspond to the last number of the port where the VNC server will listening. For example if PORT is 1, the VNC server will be listening on port 5901.

  2. Reload and start the service.

    sudo systemctl daemon-reload
    
    sudo systemctl start vncserver_test1.service
    

    Assuming everything goes error-free, add the service to the boot time and check the service status using systemctl.

    sudo systemctl enable vncserver_test1.service
    
    sudo systemctl status vncserver_test1.service
    

Repeat all the step for all the users (eg. test2)

Now the VNC server is set and it is listening for one port per each user, you can restart the machine and check if the service start at the startup correctly.

2.5. Connect to the VNC Server Through SSH Tunnel

  1. Open the SSH Tunnel.

    Open your terminal and type the ssh command as below

    ssh -L 5901:localhost:5901 -N -f test1@denerg33078.polito.it
    

    Note

    With this command the port 5901 on local computer is connected with the 5901 of the server. Note that we connect with the USER test1, therefore we connect the port 5901 since we set PORT as 1 in the vncserver_test1.service.

  2. Download and install the VNC client, we are using RealVNC Viewer.

  3. Set the defoult qulity to High.

    1. Open RealVNC Preferences File > Preferences...

      vnc

      Fig. 2.3 RealVNC: Preference

    #. Go to the Expert preference, scroll the list until you find the Quality entry and change the values from Auto to High (Expert > Quality > Value: High).

    vnc

    Fig. 2.4 RealVNC: Expert dialogue window

    1. Click on the button OK and close the preferences window.

  4. Connect to the VNC server.

    1. Type on the upper bar the andress and press ENTER.

      localhost:PORT
      

      Where PORT is the user VNC server port, e.g for the user test1 is 5901, (therfore it will be localhost:5901)

      vnc

      Fig. 2.5 RealVNC: Server andress

    2. Type the VNC server password, that you set during the set up of the VNC session

      vnc

      Fig. 2.6 RealVNC: Authentication pop-up

      Warning

      VNC will show a pop-up warning questioning the security of the connection. This happens because it does not recognize that we are using SSH (Secure SHell) tunneling to connect the two computers. Ignore it! The connection is secure because the SSH protocol encrypts all messages.

  5. ENJOY IT!

    vnc