How to Install and Configure VNC Server on Ubuntu

Virtual Network Computing (VNC) is a widely used graphical desktop-sharing system that allows user accounts to remotely connect and control the desktop interface of one computer from another computer or mobile device.

In this article, we will explain how to install and configure a VNC Server on a Ubuntu 18.04 Desktop edition via tigervnc-server program.

Testing Environment

VNC Server: 192.168.56.108
VNC Client: 192.168.56.2

Install a Desktop Environment in Ubuntu

As I said, VNC is a desktop-sharing system, so you need to have a desktop environment installed on your Ubuntu server. You can install the DE of your choice by running the appropriate commands below. For the purpose of this article, we will install Ubuntu Gnome (Official flavor).

$ sudo apt-get install ubuntu-desktop		#Default Ubuntu desktop
$ sudo apt install ubuntu-gnome-desktop	        #Ubuntu Gnome (Official flavor)
$ sudo apt-get install xfce4			#LXDE
$ sudo apt-get install lxde			#LXDE
$ sudo apt-get install kubuntu-desktop		#KDE

Install and Configure a VNC in Ubuntu

Tigervnc-server is a high-speed, multi-platform VNC program which runs an Xvnc server and starts parallel sessions of Gnome or other Desktop Environment on the VNC desktop.

To install TigerVNC server and other associated packages in Ubuntu, run the following command.

$ sudo apt install tigervnc-standalone-server tigervnc-common tigervnc-xorg-extension tigervnc-viewer

Now start the VNC server by running the vncserver command as a normal user. This action will create the initial configuration stored in the $HOME/.vnc directory and it will also prompt you to set up a login password.

Enter a password (which must be at least six characters length) and confirm/verify it. Then set a view-only password if you wish, as follows.

$ vncserver
$ ls -l ~/.vnc 
Start VNC Server
Start VNC Server

Next, we need to configure the DE to work with the VNC server. So, stop the VNC server using the following command, in order to perform some configurations.

$ vncserver -kill :1
Stop VNC Server
Stop VNC Server

To configure GNOME or whatever desktop you have installed, create a file called xstartup under the configurations directory using your favorite text editor.

$ vi ~/.vnc/xstartup

Add the following lines in the file. These commands will be automatically executed whenever you start or restart the TigerVNC server. Note that the commands may vary depending on the DE you installed.

#!/bin/sh
exec /etc/vnc/xstartup
xrdb $HOME/.Xresources
vncconfig -iconic &
dbus-launch --exit-with-session gnome-session &

Save the file and set the appropriate permission on the file so it can be executed.

$ chmod 700 ~/.vnc/xstartup

Next, start the VNC server by running the following command as a normal user. Set your own values for the display geometry. In addition, use the -localhost flag to allow connections from the localhost only and by analogy, only from users authenticated on the server.

In addition, VNC by default uses TCP port 5900+N, where N is the display number. In this case, the :1 means that the VNC server will run on display port number 5901.

$ vncserver :1 -localhost -geometry 1024x768 -depth 32
Start VNC Server to Connect
Start VNC Server to Connect

To list VNC server sessions on your system, run the following command.

$ vncserver -list
List VNC Sessions
List VNC Sessions

Once the VNC server has started, check the port it is running on with the netstat command.

$ netstat -tlnp
Verify VNC Running Ports
Verify VNC Running Ports

Connecting to VNC Server via VNC Client

In this section, we will show how to connect to the VNC server, but before we go into that, you need to know that by default VNC is not secure by default (it is not an encrypted protocol and can be subject to packet sniffing). This problem can be fixed by creating a tunnel from the client to server connection through SSH.

Using SSH tunneling, you can securely forward traffic from your local machine on port 5901 to the VNC server on the same port.

On Linux client machine, open a new terminal window and run the following command to create an SSH tunnel to VNC server.

$ ssh -i ~/.ssh/ubuntu18.04 -L 5901:127.0.0.1:5901 -N -f -l tecmint 192.168.56.108

Next install vncviewer client such as TigerVNC Viewer as follow s(you can install any other client of your choice).

$ sudo apt install tigervnc-viewer		#Ubuntu/Debian
$ sudo yum install tigervnc-viewer		#CnetOS/RHEL
$ sudo yum install tigervnc-viewer		#Fedora 22+
$ sudo zypper install tigervnc-viewer	        #OpenSUSE
$ sudo pacman -S tigervnc			#Arch Linux

Once the installation is complete, run your VNC client, specify the address localhost:5901 to connect to display 1 as follows.

$ vncviewer localhost:5901

Alternatively, open it from the system menu, enter the address above and then click Connect.

Open VNC Client to Connect
Open VNC Client to Connect

You will be prompted to enter the VNC login password created earlier on, enter it and click OK to proceed.

Enter VNC Login Password
Enter VNC Login Password

If the password is correct, you will land in the login interface of your desktop. Enter your password to access the desktop.

Access Desktop Login Interface
Access Desktop Login Interface
Ubuntu Desktop Access via VNC
Ubuntu Desktop Access via VNC

Attention: If you are security conscious, you may have noticed that the VNC viewer is showing “connection not encrypted” even though we have enabled SSH tunneling.

This is because it is designed to use specific security schemes other than SSH tunneling when attempting to authenticate with the server. However, the connection is secure once you have enabled SSH tunneling.

Creating a Systemd Unit File for TigerVNC Server

In order to manage the VNC server under systemd i.e start, stop, and restart the VNC service as needed, we need to create a unit file for it under the /etc/systemd/system/ directory, with root privileges.

$ sudo vim /etc/systemd/system/[email protected]

Then add the following lines in the file:

[Unit] 
Description=Remote desktop service (VNC) 
After=syslog.target network.target 

[Service] 
Type=simple 
User=tecmint 
PAMName=login 
PIDFile=/home/%u/.vnc/%H%i.pid 
ExecStartPre=/usr/bin/vncserver -kill :%i > /dev/null 2>&1 || :
ExecStart=/usr/bin/vncserver :%i -localhost no -geometry 1024x768 
ExecStop=/usr/bin/vncserver -kill :%i 

[Install] 
WantedBy=multi-user.target

Save the file and close it.

Next, reload the systemd manager configuration to read the newly created a unit file, as follows.

$ sudo systemctl daemon-reload

Then start the VNC service, enable it to auto-start at system boot and check its status as shown.

$ sudo systemctl start vncserver@1
$ sudo systemctl enable vncserver@1
$ sudo systemctl status vncserver@1
Start and Verify VNC Status
Start and Verify VNC Status

That’s all! In this article, we’ve explained how to install and configure VNC server on Ubuntu Linux distribution. Share your queries or thoughts with us via the feedback form below.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

26 thoughts on “How to Install and Configure VNC Server on Ubuntu”

  1. In your:

    ~/.vnc/xstartup
    
    #!/bin/sh
    exec /etc/vnc/xstartup
    xrdb $HOME/.Xresources
    vncconfig -iconic &
    dbus-launch --exit-with-session gnome-session &
    

    “exec /etc/vnc/xstartup” – does not exist

    Reply
  2. This approach doesn’t work with the latest ubuntu (20.10).

    Mär 29 11:19:26 hostname systemd[2597]: pam_unix(login:session): session closed for user rst
    Mär 29 11:19:26 hostname dbus-daemon[1487]: [system] Rejected send message, 2 matched rules; type=”method_call”, sender=”:1.129″ (uid=1000 pid=2597 comm=”(sd-pam) ” label=”unconfined”) interface=”org.freedesktop.login1.Manager” member=”Rele>
    Mär 29 11:19:26 hostname systemd[2597]: pam_systemd(login:session): Failed to release session: Access denied

    Reply
  3. Hi, thanks for the procedure, I managed to get it working on ubuntu 18.04 LTS with cinnamon by starting manually the server using:

    $ vncserver -localhost no -geometry 800x600 -depth 24,
    

    It works fine in local LAN with RealVNC on an Ipad, however for some reason, the server startup via systemd does not work, the service status log shows an inactive status (dead), what would be the problem? I’ve tried doing the systemd procedure locally on a server Terminal, and remotely via SSH, also checked the proper username in the script and rebooted with no success and ran out of ideas, your kind help is required, the status log is down below.

    Please let me know your ideas or suggestion to remedy this issue.

    Thanks and regards
    Abraham

    PS: during the interim, I also experienced the problem that when a desktop session was already started, locally or remotely an attempt to start a second session remotely or locally would fail with the error “could not acquire name on session bus“,

    This problem was fixed, as per unix.stackexchange.com by adding the following line to ~/.vnc/xstartup:

    unset DBUS_SESSION_BUS_ADDRESS
    

    After applying this patch, I can now have concurrent desktop sessions, locally and remotely, I can also have a Mate desktop running locally on the server and a cinnamon desktop remotely on my iPad via RealVNC.

    Vnc server startup status log:

    abesr@abesr-UbuntuMate:~$ sudo systemctl status vncserver@1                     
    [sudo] password for abesr:                                                      
     [email protected] - Remote desktop service (VNC)                             
       Loaded: loaded (/etc/systemd/system/[email protected]; indirect; vendor pres
       Active: inactive (dead) since Tue 2020-09-08 23:57:04 CDT; 11h ago           
      Process: 1155 ExecStop=/usr/bin/vncserver -kill :1 (code=exited, status=0/SUCC
      Process: 1137 ExecStart=/usr/bin/vncserver :1 -localhost no -geometry 800x600 
      Process: 852 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 || : (c
     Main PID: 1137 (code=exited, status=0/SUCCESS)                                 
                                                                                    
    sep 08 23:56:59 abesr-UbuntuMate systemd[1]: Starting Remote desktop service (VN
    sep 08 23:56:59 abesr-UbuntuMate systemd[852]: pam_unix(login:session): session 
    sep 08 23:57:03 abesr-UbuntuMate systemd[1]: Started Remote desktop service (VNC
    sep 08 23:57:03 abesr-UbuntuMate systemd[1137]: pam_unix(login:session): session
    sep 08 23:57:04 abesr-UbuntuMate systemd[1155]: pam_unix(login:session): session
    
    Reply
  4. Thanks for the tutorial, I only merely can start the service using vncserver:1 -localhost -geometry 1024x768 -depth 32, but not using the systemd which most of us want to achieve.

    The error is below.

    Xvnc TigerVNC 1.10.0 - built Apr  9 2020 06:49:31
    Copyright (C) 1999-2019 TigerVNC Team and many others (see README.rst)
    See https://www.tigervnc.org for information on TigerVNC.
    Underlying X server release 12008000, The X.Org Foundation
    
    
    Sun Jun 28 15:21:59 2020
     vncext:      VNC extension running!
     vncext:      Listening for VNC connections on all interface(s), port 5901
     vncext:      created VNC server for screen 0
     ComparingUpdateTracker: 0 pixels in / 0 pixels out
     ComparingUpdateTracker: (1:-nan ratio)
    X connection to :1 broken (explicit kill or server shutdown).
    

    Any idea? Have been reading a lot of tutorial with no success.

    Reply
  5. I always got black screens. Finally, it worked with x0vncserver:

    $ sudo apt install tigervnc-scraping-server
    ..
    x0vncserver -passwordfile ~/.vnc/passwd -display :1
    

    -> gnome desktop takes :1 on my system and with x0vncserver I can remotely access that display.

    Reply
  6. Ubuntu 18.04 on IBM cloud. I found some instructions for running the GUI on the Google cloud which worked with Google cloud but I think some at least can be useful to try with IBM cloud as well, as IBM does not provide support for running a GUI on Ubuntu in their virtual servers.

    It looks like one of the problems is that Ubuntu cannot find a screen and then it doesn’t show an image. https://cloud.google.com/solutions/chrome-desktop-remote-on-compute-engine.

    Reply
    • @Marcelo

      I guess the problem is the lack of a desktop environment installed on the remote system in the IBM cloud. Try to install a desktop environment as described in the guide.

      Reply
  7. Hi, I get a grey screen and nothing else when I connect using VNCviewer. I am using the IBM cloud. Please help. Thanks

    Reply
  8. My opinion is that VNC server support in Ubuntu 18.04 and up is completely broken at least for GNOME. It is possible to get a partially working setup by running vncserver from ssh session or via cron job, but if you try to run for example the gnome-control-center application in your VNC session, the session crashes.

    Any attempt to start the VNC server via systemd service fails in the GNOME environment initialization stage. It is funny to see so many tutorials about VNC server installation in Ubuntu, every one of that looking similar, and nothing works.

    Some useful links:
    https://askubuntu.com/questions/1031147/running-vnc-server-on-ubuntu-desktop-18-04-by-creating-new-sessions
    https://ubuntuforums.org/showthread.php?t=2427087

    Reply
    • @AI

      It’s true, Gnome session seems to have issues on VNC X servers. We will look at the shared links/solutions and update the article where necessary. Many thanks for the feedback and sharing.

      Reply
  9. I have followed probably 20 different “how to install VNC on ubuntu 18.04” guides and had zero success.

    I ran yours and got to the line:

    # chmod 700 ~/.vnc/xstartup
    

    this came up with an error “changing permission of ‘/home/simon/.vnc/xstartup’ : operation not permitted”.

    So I used Sudo instead. This still failed, the 700 permission was rejected when I tried to launch vncserver and got:
    "vncserver: Can't fixup permissions of start script '/home/simon/.vnc/xstartup' : Operation not permitted"

    So i ran sudo chmod 777 ~/.vnc/xstartup

    Now vncserver will start but if I run vncviewer connecting to localhost is correctly asks me to log in but shows just a black screen with a grey dialogue box with “accept clipboard” and three other items all ticked

    What am I missing, please?

    Reply
    • @Simon,

      Please check the configuration of ~/.vnc/xstartup file and make sure to add your Desktop Environment, that you installed.

      Reply
  10. Starting vncserver works but I can’t get service to work.

    Ubuntu 18.04.3 Server, I edited User=tecmint to User=densm and –geometry 1024×768 to -geometry 1600×900 in /etc/systemd/system/[email protected].
    server:1.log:
    Sat Sep 14 22:24:16 2019
    vncext: VNC extension running!
    vncext: Listening for VNC connections on all interface(s), port 5901
    vncext: created VNC server for screen 0
    XIO: fatal IO error 11 (Resource temporarily unavailable) on X server “:1”
    after 175 requests (175 known processed) with 1 events remaining.

    Reply
  11. vncserver :1 -localhost -geometry 1024×768 -depth 32– This did not let me connect to server.

    This did
    vncserver :1 -localhost no -geometry 1024×768 -depth 32
    You added “no” in [email protected] so it’s probably just a typo

    Reply
  12. Nice attempt.

    Unfortunately the xstartup bash cracks when it can NOT find “/etc/vnc/xstartup” to execute.

    There is a “/etc/tigervnc/” directory but it doesn’t contain an “xstartup” file either.

    So where is this “xstartup” executable ?

    Reply
    • @Arend

      The most important xstartup file is the one located in ~/.vnc/xstartup. You can remove the line “exec /etc/vnc/xstartup” if the file doesn’t exit.

      Reply

Leave a Reply to Marcelo Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.