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 22.04 or 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.
Table of Contents
- Prerequisites
- Step 1: Install a Desktop Environment
- Step 2: Install VNC server
- Step 3: Configure the VNC server
- Step 4: Establish a secure connection to the VNC Desktop
- Step 5: Setting up the VNC as a system service
- Configuring VNC to Use Other Desktop Environments (And Switching Between Them)
- Troubleshooting
- Conclusion
Prerequisites
- A machine running Ubuntu 22.04 or 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 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 22.04 or 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.
XFCE is very popular and lightweight. If you’d like to install a different desktop environment right away, you can jump to our section below, on installing and configuring other desktop environments with VNC: Configuring VNC to Use other Desktop Environments
You can also install multiple desktop environments at the same time, and just switch between them. However, you may end up with bugs or conflicts, depending on the desktop environments you have simultaneously installed.
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 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.
- 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
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.
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;
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
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.
mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
, and create a new file.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:
#!/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 &
Save the file (Ctrl + O, then Enter) and Exit (Ctrl + X).
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
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.
-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.
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
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 22.04 / 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
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.
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
:
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.
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.
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 systemd unit file called [email protected]
using the command below:
sudo nano /etc/systemd/system/[email protected]
Paste the lines below, depending on the version of Ubuntu you’re using (20.04 or 22.04) 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.
Setting up VNC as a service on Ubuntu 22.04
[Unit] Description=Start TigerVNC server at startup After=syslog.target network.target [Service] Type=forking User=edxd Group=edxd WorkingDirectory=/home/edxd PIDFile=/home/edxd/.vnc/%H:590%i.pid ExecStartPre=-/bin/sh -c "/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
Setting up VNC as a service on Ubuntu 20.04
[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]
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 Wed 2022-07-27 13:47:15 UTC; 13min ago Process: 19472 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=0/SUCCESS) Process: 19490 ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :1 (code=exited, status=0/SUCCESS) Main PID: 19500 (Xtigervnc) Tasks: 129 (limit: 2274) Memory: 237.4M CGroup: /system.slice/system-vncserver.slice/[email protected] ├─19500 /usr/bin/Xtigervnc :1 -desktop vncserverXD:1 (edxd) -auth /home/edxd/.Xauthority -geometry 1280x800 -depth 24 -rfbwait 30000 -rfbauth /home/edxd/.vnc/passwd -rfbport 5901 -pn -SecurityTypes VncAuth,TLSVnc ├─19503 /usr/bin/perl /usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :1 ├─19504 /bin/sh /home/edxd/.vnc/xstartup ├─19505 xfce4-session ├─19516 /usr/bin/dbus-launch --sh-syntax --exit-with-session xfce4-session ├─19517 /usr/bin/dbus-daemon --syslog --fork --print-pid 5 --print-address 7 – session ├─19519 /usr/libexec/at-spi-bus-launcher ├─19524 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3 ├─19528 /usr/lib/x86_64-linux-gnu/xfce4/xfconf/xfconfd ├─19534 /usr/libexec/at-spi2-registryd --use-gnome-session ├─19540 /usr/bin/ssh-agent -s ├─19544 xfwm4 ├─19548 /usr/libexec/gvfsd ├─19556 xfsettingsd ├─19557 xfce4-panel ├─19563 Thunar --daemon ├─19568 xfdesktop ├─19569 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugins/libsystray.so 6 14680072 systray Notification Area Area where notification icons appear ├─19570 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugins/libpulseaudio-plugin.so 8 14680073 pulseaudio PulseAudio Plugin Adjust the audio volume of the PulseAudio sound system ├─19571 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugins/libxfce4powermanager.so 9 14680074 power-manager-plugin Power Manager Plugin Display the battery levels of your devices and control the brightness of your display ├─19574 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugins/libnotification-plugin.so 10 14680075 notification-plugin Notification Plugin Notification plugin for the Xfce panel ├─19575 /usr/lib/x86_64-linux-gnu/xfce4/panel/wrapper-2.0 /usr/lib/x86_64-linux-gnu/xfce4/panel/plugins/libactions.so 14 14680076 actions Action Buttons Log out, lock or other system actions ├─19588 /usr/lib/x86_64-linux-gnu/xfce4/notifyd/xfce4-notifyd ├─19592 /usr/bin/python3 /usr/share/system-config-printer/applet.py ├─19593 /usr/libexec/evolution-data-server/evolution-alarm-notify ├─19595 xiccd ├─19601 nm-applet ├─19606 /usr/libexec/geoclue-2.0/demos/agent ├─19618 xfce4-power-manager ├─19627 /usr/libexec/dconf-service ├─19646 /usr/libexec/evolution-source-registry ├─19659 /usr/libexec/goa-daemon ├─19667 /usr/libexec/goa-identity-service ├─19674 /usr/libexec/evolution-calendar-factory ├─19687 /usr/libexec/evolution-addressbook-factory ├─19705 /usr/libexec/gvfs-udisks2-volume-monitor ├─19711 /usr/libexec/gvfs-goa-volume-monitor ├─19716 /usr/libexec/gvfs-mtp-volume-monitor ├─19721 /usr/libexec/gvfs-gphoto2-volume-monitor ├─19726 /usr/libexec/gvfs-afc-volume-monitor ├─19734 /usr/libexec/gvfsd-trash --spawner :1.7 /org/gtk/gvfs/exec_spaw/0 └─19741 /usr/libexec/gvfsd-metadata
From the image above, we can see that we have successfully started VNC as a service.
Start Multiple VNC Server Instances on Boot
If you’d like to have multiple VNC server instances as services on boot, then you can just change the identifier – the identifier is what comes after the @
. In our example above, the identifier is 1
. We can enable other VNC servers to start on boot replacing 1
with 2
, 3
, etc.
sudo systemctl enable [email protected]
Created symlink /etc/systemd/system/multi-user.target.wants/[email protected] → /etc/systemd/system/[email protected]
That’s it! You have successfully installed and configured the VNC server on Ubuntu 22.04 / 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:
- Install the desktop environment on our Ubuntu 22.04 / 20.04 machine
- 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.
- 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
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
To install GNOME on Ubuntu 22.04 / 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 up the standard system desktop unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS /usr/bin/gnome-session [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources x-window-manager &
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:
And this is how the full GNOME desktop looks like, if you install it with the following command:
sudo apt install ubuntu-desktop
This is how the full GNOME Desktop install it looks like:
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 # 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 &
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:
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 # Start up the standard system desktop unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS /usr/bin/startlxqt [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources x-window-manager &
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:
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 # Start up the standard system desktop unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS /usr/bin/startlxde [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources x-window-manager &
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:
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 # Start up the standard system desktop unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS /usr/bin/mate-session [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources x-window-manager &
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:
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 # Start up the standard system desktop unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS /usr/bin/budgie-session [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources x-window-manager &
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:
Configure VNC to Use KDE Plasma Desktop
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 # Start up the standard system desktop unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS /usr/bin/startplasma-x11 [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources x-window-manager &
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:
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 # Start up the standard system desktop unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS /usr/bin/cinnamon-session [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources x-window-manager &
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:
Configure VNC Deepin Desktop Environment (DDE)
To install Deepin first add the stable PPA for DDE:
sudo add-apt-repository ppa:ubuntudde-dev/stable
Update your package index:
sudo apt update
Install Deepin Desktop Environment:
sudo apt install ubuntudde-dde
To configure VNC to use Deepin create a new ~/.vnc/xstartup file, or edit the existing one, and paste in the following:
#!/bin/bash unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS export XKL_XMODMAP_DISABLE=1 export GTK_IM_MODULE=fcitx export QT_IM_MODULE=fcitx export [email protected]=fcitx #__using_dde__ session=startdde #__enable_clipboard_sync__ if [ -f /usr/bin/autocutsel ] then /usr/bin/autocutsel -fork fi #__deepin_uos_activator__ if [ -f /usr/bin/uos-activator ] then /usr/bin/uos-activator & fi #__enable_fcitx__ if [ -f /usr/bin/fcitx ] then (sleep 15 && /usr/bin/fcitx) & fi #__kill_user_apps_duplicated__ killall -9 v2ray & exec dbus-launch $session
This is what Deepin looks like in my case:
Troubleshooting
Failed to execute child process ?dbus-launch? (No such file or directory)
If you get an error such as the following when running:
vncserver
New Xtigervnc server 'vncserverXD:1 (edxd)' on port 5901 for display :1. Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/edxd/.vnc/passwd :1 to connect to the VNC server. =================== tail /home/edxd/.vnc/vncserverXD:5901.log =================== ** (process:46301): WARNING **: 09:51:30.948: Could not make bus activated clients aware of XDG_CURRENT_DESKTOP=GNOME environment variable: Failed to execute child process ?dbus-launch? (No such file or directory) Terminated ================================================================================= Session startup via '/home/edxd/.vnc/xstartup' cleanly exited too early (< 3 seconds)! Maybe try something simple first, e.g., tigervncserver -xstartup /usr/bin/xterm The X session cleanly exited! Killing Xtigervnc process ID 46297... success!
Then we can fix this by installing the dbus-x11
package:
sudo apt install dbus-x11
Now when we run:
vncserver
We should get an output similar to this:
New Xtigervnc server 'vncserverXD:1 (edxd)' on port 5901 for display :1. Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/edxd/.vnc/passwd :1 to connect to the VNC server.
Conclusion
I believe this guide has given you a step-by-step guide on installing and configuring a VNC server on Ubuntu 22.04 or 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.
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
The article says to start the server for testing using “vncserver localhost -no :1” … however that should be “vncserver -localhost no :1”
Hi. Apologies for that. I don’t know what I was thinking.
Thanks so much for pointing that out.
It’s corrected now.
Thank you for your post but it doesn’t work completely with Zorin OS 16 based on Ubuntu 20.04… or at least I can’t make it work completely.
I tried to explain my issue in here : https://gist.github.com/spinxz/1692ff042a7cfd17583b?permalink_comment_id=4097680#gistcomment-4097680
If you have any comment to help me solve this issue, I’d be thankfull.
Hi Thatoo,
I’ve also tried it with ZorinOS and the same thing happens. When running it as a service I get the same error with GNOME, otherwise it works.
I haven’t been able to find a solution. Have you had any success with this?
Hello EdXD,
No I haven’t.
However, I was wondering if there was not a conflict with the builtin VNC system of Zorin OS 16 but I don’t know where to look at and how to disable it before testing again TigerVNC.
Great post. Thanks.
Hi, Eric. Thank you for the feedback! I hope it was useful to you.
good article explain in detail and very useful
I’ve searched and tried many ways. until the end of this article
thank you very much
Thank you for the kind words! I’m glad it worked for you!
Hey can you add a tutorial for Deepin OS’s Deppin Desktop environment? It’s a good looking environment
Hello. Thanks a lot for the suggestion.
I added Deepin just now, however I couldn’t get it to work for Ubuntu 22.04, only for 20.04.
Hope that works for you.
Thanks for the guide! A couple things didn’t work for me in Ubuntu 22.04, so I though I’d share the alterations that were necessary.
First, the version of systemd which ships with Ubuntu 22.04 (specifically, that’s version 249) does not use a shell to interpret “ExecStartPre”:
> Note that shell command lines are not directly supported. If shell command
> lines are to be used, they need to be passed explicitly to a shell
> implementation of some kind.
source: https://www.freedesktop.org/software/systemd/man/systemd.service.html
That means the following line won’t work (because the tokens intended to trigger process output redirection are actually passed as arguments to vncserver):
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
That line should instead read:
ExecStartPre=-bash -c “/usr/bin/vncserver -kill :%i > /dev/null 2>&1”
Second, the version of TigerVNC which is available in the Ubuntu 22.04 “apt” repository (specifically, that’s version 1.12.0) doesn’t create a PID file at the path referenced by this tutorial. From man tigervncserver:
> ~/.vnc/:.pid
> Identifies the VNC server process ID, used by the -kill option.
…where “rfbport#” is defined as
> -rfbport rfbport#
> Specifies the TCP port on which Xtigervnc listens for connections
> from viewers (the protocol used in VNC is called RFB – “remote
> framebuffer”). The default is 5900 plus the display number display#.
That means the following line won’t work (since systemd will expand the value to a non-existent file):
PIDFile=/home/mike/.vnc/%H:%i.pid
One small but inelegant fix would be to approximate integer addition using string concatenation:
PIDFile=/home/mike/.vnc/%H:590%i.pid
This will break if using a systemd instance name greater than 9, though. A safer (but more drastic) solution would be to define the unit using the “simple” type. Crucially, this requires adding the -fg argument:
[Service]
User=YOUR_USERNAME
Group=YOUR_USERNAME
WorkingDirectory=/home/YOUR_USERNAME
ExecStart=/usr/bin/vncserver -fg -depth 24 -geometry 1280×800 -localhost :%i
Thanks again!
Hi Mike!
I apologize for the delay in responding. Thank you so much for your comment.
It helped immensely that you took the time to explain the issues on Ubuntu 22.04 with those directives and their values. I would’ve probably had to use up an entire day troubleshooting.
I can’t believe I missed that when testing out the tutorial on 22.04. I’m very sorry for the incorrect information as well.
The tutorial has been updated with some of the changes you mention. I’ll look into the PIDFile issue in the meantime.
Thanks again!
The xstartup for KDE is wrong :\
Hi Bard. Could you please elaborate? It worked 3 weeks ago.
UPDATE: Apologies. You’re right. It had cinnamon-session. I must’ve copied it by mistake. It should be fixed now.
Thanks for this! I followed the steps exactly, testing at every step, and everything worked, up until I tried to run it as a system service. Doing
sudo systemctl start [email protected]
results in the following:
Job for [email protected] failed because the control process exited with error code.
See “systemctl status [email protected]” and “journalctl -xeu [email protected]” for details.
Running the status command, here’s the juicy bit:
Jul 08 09:30:00 Linux-CC1 systemd[1]: Starting Start TigerVNC server at startup…
Jul 08 09:30:00 Linux-CC1 systemd[16189]: [email protected]: Failed to determine user credentials: No such process
Jul 08 09:30:00 Linux-CC1 systemd[16189]: [email protected]: Failed at step USER spawning /usr/bin/vncserver: No such process
Jul 08 09:30:00 Linux-CC1 systemd[16190]: [email protected]: Failed to determine user credentials: No such process
Jul 08 09:30:00 Linux-CC1 systemd[16190]: [email protected]: Failed at step USER spawning /usr/bin/vncserver: No such process
Addendum to my previous comment, which I can’t edit at the moment:
Found a typo in the service file and fixed it, but now it’s hanging.
Jul 08 09:42:47 Linux-CC1 tracker-miner-f[16939]: Owner of volume monitor org.gtk.vfs.GoaVolumeMonitor disconnected from the bus; removing drives/volumes/mounts
Jul 08 09:42:47 Linux-CC1 tracker-miner-f[16939]: Owner of volume monitor org.gtk.vfs.GPhoto2VolumeMonitor disconnected from the bus; removing drives/volumes/mounts
Jul 08 09:42:47 Linux-CC1 indicator-appli[16902]: Name Lost
Jul 08 09:42:47 Linux-CC1 gnome-session-f[17065]: Cannot open display:
Jul 08 09:42:47 Linux-CC1 dbus-daemon[17079]: [session uid=1000 pid=17077] AppArmor D-Bus mediation is enabled
Jul 08 09:42:47 Linux-CC1 tracker-miner-fs-3.desktop[16939]: OK
Jul 08 09:43:57 Linux-CC1 systemd[1]: [email protected]: start operation timed out. Terminating.
Jul 08 09:43:57 Linux-CC1 systemd[1]: [email protected]: Failed with result ‘timeout’.
Jul 08 09:43:57 Linux-CC1 systemd[1]: Failed to start Start TigerVNC server at startup.
Sorry, I’m just full of questions. Assuming I can get the service working, what if I want to launch 3 VNC servers (as services) on system boot, instead of just the one?
Hi Brendan. I apologize for being so late to comment. In case this is still useful to you, I edited the section on setting up a systemd unit file for the vncserver service.
It’s my fault for not checking thoroughly – thanks to Mike’s comment I tested it out and the service file needed some changes to work on 22.04.
And regarding setting up multiple instances of VNC servers on boot, all you have to do is change the identifier when enabling the service. The identifier is the number after @ (in our examples it’s 1, 2, 3, 4). The system will pick that up.
Hope this helps, and apologies again for the delay in responding.
I’m using Ubuntu 22.04 minimal install and followed the instructions but I’m getting an error that x-window-manager is not found. What window manager should I be using for the GNOME desktop?
Hi. Thank you for commenting! Would you mind letting me know at what point do you get x-window-manager is not found, and a screenshot of the error?
I’ve created the xstartup as described for gnome as well as chmod +x it. when running vncserver:2 I get the attacked error.
I’ve been trying to reproduce your scenario somehow, and to find solutions online, but I can’t seem to figure it out.
Is your server a fresh install or it’s a server that you’ve been working on for a while (so it has other packages installed that may cause conflicts?). I’m not sure what I’m looking for, just probing to hopefully find a solution.
Any info you think could be useful would be appreciated.
I also tried xfce and was getting weird errors so I just tried plasma and that seems to work. Not sure why gnome doesn’t work. Would be nice to keep the same desktop since I already have it on this box.
I see. Have you tried switching to GNOME again, after installing KDE Plasma? I’m thinking that maybe there’s a chance some package got installed along with KDE Plasma that may fix your original error.
Hello,
I’v followed your great tutorial for server configuration. I’d like to connect to server on my smartphone, but when i’m connecting, server is crashing.
I’ve made a topic on stackoverflow maybe you will have an answer for my problem.
Thank you
Hi. Everything looks good, except for one small detail.
The only issue is that TigerVNC expects connections only from localhost, so you have to establish an SSH tunnel between the device you connect from.
Either that, or set the vncserver to allow connections from other hosts other than localhost. To do this you just edit your
/etc/systemd/system/[email protected]
and change:to
The
no
is the difference. It should work after this.The secure option would be to establish an SSH tunnel from your phone, but I haven’t tried that yet to be able to provide you with a solution.
Please let me know how it goes.
(I have also given the same answer on your Stack Overflow thread)
Post on stackoverflow is closed because problem is not about programming…posted here instead
I’ve edited my [email protected] file with your correction.
Is the ssh tunnel mandatory ? My RPI will not be connected on internet and vnc connexion will be only between smartphone and rpi (fixed on a telescop ^^).
I’ve tested with xfce4 too (and change xstartup fille) but same problem and logs are identicals https://pastebin.com/FM7Y8DM8
(1) You edited the
[email protected]
file and ransudo systemctl daemon-reload
(I should’ve mentioned this initially) and it still didn’t work?(2) The SSH tunnel is needed only when you don’t have that line with
-localhost no
. The goal of the SSH tunnel is to basically make the Rasperry PI server think that you’re accessing its VNC server from itself. It’s a security measure, acting as an extra layer of protection. It makes it so that only someone who has both SSH access and VNC credentials can connect to the server.Also, I saw you mentioned you don’t understand the SSH tunnel thing.
The SSH tunnel situation is like the VNC server is a laboratory in your house – by default it only has a door that you can get in if you’re in the house first (localhost). To get in your house, you need the keys (SSH access) and then the key to the laboratory (VNC server credentials).
Otherwise, if you set
-localhost no
it’s like your lab has a door directly to the outside, and you don’t need to go through your house to access it.You can come in from outside, and just need the VNC server credentials.
And I think this is because VNC is very popular, people often find vulnerabilities in it, and that’s why SSH is good as an extra security measure. Hope that helps a little. (We also have an article on SSH tunnels, in case you want to clarify, but it’s not mandatory for our situation, I’d say)
Also I know you said that the error seems related to the desktop environment. I’m not sure how I can reproduce your situation in this sense. I installed MATE on an Ubuntu 20.04 machine and other than the localhost thing, everything seemed worked.
Yes ‘I’ve reload deamon, restart rpi too sometimes.
Your explaination about ssh tunnel is perfect :). I will waiting for answers in raspberrypi.stackexchange maybe somebody will have an idea. Thank you very much for your time and your answers 🙂
I’ve just seen i don’t have
/etc/vnc/xstartup
file. Is this an bug ? I’ve commented the line with this path in my~/.vnc/xstartup
It should have been created when you ran
vncserver
for the first time, when it asked you to set a password.Are you still trying to get VNC to work with GNOME and still getting the same error?
After remove
~/.vnc/
directory and disable service, i relaunchvncserver
like a first time and/etc/vnc/xstartup
is not created.If i’m creating my own with :
,reload service, create my own xstartup and….crash again when client try to connect but without X11 errors, only this in log :
Wed Aug 10 13:42:21 2022
vncext: VNC extension running!
vncext: Listening for VNC connections on all interface(s), port 5901
vncext: created VNC server for screen 0
Wed Aug 10 13:43:15 2022
Connections: accepted: 192.168.1.22::44276
SConnection: Client needs protocol version 3.8
SConnection: Client requests security type VncAuth(2)
terminate called after throwing an instance of 'rdr::Exception'
terminate called recursively
(EE)
(EE) Backtrace:
(EE)
(EE)
Fatal server error:
(EE) Caught signal 6 (Aborted). Server aborting
(EE)
I’ve trying only with mate-session and xfce4
Hi. Did you manage to get it to work? I tried contacting you via Email to perhaps communicate using a messenger app, in hopes of getting to the bottom of this faster.