How to Install & Configure VNC Server on Ubuntu 22.04

How to Install & Configure VNC Server on Ubuntu 20.04

[powerkit_collapsible title=”Not using Ubuntu? Choose a different version or distro.”]
Debian 10/11
CentOS/RockyLinux/AlmaLinux
[/powerkit_collapsible]

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.

VNC remains popular and is still used by many today, especially those who prefer an open-source remote desktop option or need to connect to Linux machines. It provides a graphical interface familiar to many.

In this tutorial, we will go step-by-step through installing and configuring the VNC Server on an Ubuntu 22.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.

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]For a guide on using Remote Desktop Protocol (RDP) with Ubuntu 22.04, you can also check our guide on using xRDP for Ubuntu 22.04, for X2Go (which uses a modified NX 3 protocol) you can check our guide on using X2Go for Ubuntu 22.04, or for Chrome Remote Desktop you can check our guide on installing Chrome Remote Desktop on an Ubuntu 22.04. For a somewhat different take on remote desktops, you can also check our tutorial on Xpra.[/powerkit_alert]

Prerequisites

  • A machine running Ubuntu 22.04 to which we’ll connect. (For my server, I’m using a cheap VPS with high RAM from Contabo. I’ve written a review of Contabo’s VPS if you’re interested – or use a raspberry pi).
  • 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) 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 server, then you’ll notice that servers don’t usually come with a preinstalled desktop environment. On booting up, you will get a command-line interface to execute your commands.

If our Ubuntu remote machine doesn’t have a desktop environment installed, we’ll install one ourselves.

As of writing this post, there are many Linux desktop environments available for you to install. This guide includes installing GNOME, KDE Plasma Desktop, MATE, Budgie, Xfce Desktop, Cinnamon Desktop, and more.

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

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]

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.[/powerkit_alert]

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 the XFCE Desktop Environment, which brings a lot more advanced enhancement.

You may 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 most desktop environments.

  • Initially, I tried TightVNC and got it to work with XFCE, LXQt, LXDE, KDE Plasma, and Gnome Flashback, but I 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:

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]Note: The password must be between 6 – 8 characters long. The system will automatically truncate any password longer than eight characters. For example, if you enter a password like startup2233, it will be truncated to startup2.[/powerkit_alert]

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.

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]In my case, TigerVNC does not create the ~/.vnc/xstartup file by itself. If in your case it does, then you can just back up the existing one by running mv ~/.vnc/xstartup ~/.vnc/xstartup.bak, and create a new file.[/powerkit_alert]

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

# 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
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.

Allow Connections to VNC Port in Firewall (UFW)

IMPORTANT: It’s possible that you have UFW (Uncomplicated Firewall) set up and active on your server. And by default, UFW doesn’t allow connections through port 5901, which is the port we need for the default VNC connection. If you’re trying to connect via VNC and the connection just hangs there, this might be the issue.

To allow connections through 5901, run:

sudo ufw allow 5901
sudo ufw reload

That’s it. Now your firewall should allow VNC connections.

To allow multiple connections from multiple users, VNC will to connect to ports 5902, 5903, etc. You should allow those as well if you know you’ll have multiple connections.

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, when you don’t run -localhost no, TigerVNC binds the server to your Ubuntu 22.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

Let’s 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 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.

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Important: Make sure you set up the VNC service for the appropriate user. In the first example, I’m setting it up for my non-root sudo user (YOUR_USERNAME), and in the second example, I’m setting it up for my root user.
[/powerkit_alert]

Setting up VNC as a service for a user

/etc/systemd/system/[email protected] on Ubuntu 22.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: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 for the root user

/etc/systemd/system/[email protected] on Ubuntu 22.04
[Unit]
Description=Start TigerVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=root
Group=root
WorkingDirectory=/root

PIDFile=/root/.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

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 vncserver@1

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

sudo systemctl status vncserver@1
[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]
Output
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 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 22.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 vncserver@1

    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 22.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 22.04, I recommend TigerVNC instead of TightVNC.

To install GNOME on Ubuntu 22.04, we’ll run the following:

sudo apt install gnome-session gnome-terminal

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

#!/bin/sh
/usr/bin/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 what the full GNOME Desktop installs looks like:

VNC with GNOME on Ubuntu 22.04

 

Configure VNC to Use XFCE

We covered this initially in the tutorial, but we 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 the 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

# 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 the 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

# 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 the 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

# 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:

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

# Start up the standard system desktop
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS

/usr/bin/budgie-desktop

[ -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 the 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

# 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:

ubuntu_cinnamon_tigervnc

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 XMODIFIERS=@im=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

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Thanks to this gist for the xstartup contents.
[/powerkit_alert]

This is what Deepin looks like.

Deepin Ubuntu 22.04

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
Error Output
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:

Output
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.

Frequently Asked Questions

Does Ubuntu offer VNC remote desktop access by default?

Yes, Ubuntu has support for VNC. In Ubuntu 20.04, VNC is included by default as a remote desktop solution. However, in Ubuntu 22.04, both RDP and VNC are supported, with RDP being the more modern solution and VNC considered the legacy remote desktop technology. Users can still choose to use VNC if preferred.

Is VNC better than RDP?

Whether VNC is better than RDP depends on the use case. RDP generally offers better performance and is more integrated with Windows environments. VNC, on the other hand, is more cross-platform compatible and can be used in a variety of environments, including Linux.

RDP is also considered to be more secure than VNC and uses encryption by default. VNC does not encrypt data by default; however, you can increase the security of VNC by tunneling it through SSH, as we’re doing in our tutorial.

Why should you not use VNC?

Reasons not to use VNC might include performance issues, as it can be slower compared to other solutions like RDP or X2Go.

VNC can be laggy compared to RDP because it simply sends pixel data rather than rendering graphics on the client side as RDP does. Tunneling VNC through SSH can also contribute to latency. Lowering color depth and resolution can help improve VNC performance.

Security can also be a concern if not properly configured (such as with SSH tunneling), as VNC, by default, does not encrypt data by default.

Does VNC work without a monitor?

Yes, VNC can work without a monitor attached to the remote machine. It can create a virtual desktop session even if there is no physical monitor connected, and you’ll just use the remote machine using the command line. However, the host computer still needs to be powered on, and the VNC server should be running.

How to restart the VNC server?

Restarting the VNC server involves first stopping the current VNC sessions and then starting a new session. Here’s how to do it:

1. Killing a VNC session

To kill a VNC session, use the following command:

vncserver -kill :1

Here, :1 represents the session number. If you have multiple sessions running, they will be numbered sequentially (e.g., :1, :2, :3, etc.). To kill any other session, replace :1 with the corresponding session number.

3. Listing all active VNC sessions

To view a list of all currently active VNC sessions, use:

vncserver -list

This command will display a list of all running VNC sessions along with their session numbers. You can then use these session numbers to kill specific sessions as needed.

After killing the sessions, you can start a new VNC server session by simply typing vncserver in the terminal.

Restarting the VNC server if it’s set up as a system service

If your VNC server is set up as a service, as we mentioned above in the tutorial, then you can restart it with the command:

systemctl restart vncserver@1

Here, the @1 represents the session number. If you have multiple sessions running, they’ll be numbered sequentially (e.g., @1, @2, @3, etc.).

Output
TigerVNC server sessions:

X DISPLAY #     RFB PORT #      RFB UNIX PATH   PROCESS ID #    SERVER
1               5901                            1714            Xtigervnc

Conclusion

I believe this guide has given you a step-by-step guide on installing and configuring a VNC server on Ubuntu 22.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.

Article Revisions & Updates

[Sep 20 2023]: Updated GNOME .xstartup file. Updated Budgie .xstartup file. Updated Deepin for Ubuntu 22.04. Updated systemd unit file (thanks to Steffen L’s comment). Added how to restart the server in FAQ.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

69 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Noby
Noby
2 years 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
2 years ago

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

Thatoo
Thatoo
2 years ago

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.

Thatoo
Thatoo
2 years ago
Reply to  EdXD

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.

Eric
Eric
2 years ago

Great post. Thanks.

Best
Best
2 years ago

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

Omxpro
1 year ago

Hey can you add a tutorial for Deepin OS’s Deppin Desktop environment? It’s a good looking environment

Mike
1 year ago

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!

Pavel
Pavel
1 year ago
Reply to  EdXD

Hello! I couldn’t figure out what to write in PIDFile

Bard
Bard
1 year ago

The xstartup for KDE is wrong :\

Brendan Moran
Brendan Moran
1 year ago

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 vncserver@1

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

Brendan Moran
Brendan Moran
1 year ago

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.

Phil
Phil
1 year ago
Reply to  Brendan Moran

Change two things in the file

Type=simple

and

ExecStart=/usr/bin/vncserver -fg [...]

where […] is whatever is followed in the original command. Important is the “-fg” part.

Brendan Moran
Brendan Moran
1 year ago

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?

Agent31
Agent31
1 year ago

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?

Agent31
Agent31
1 year ago
Reply to  EdXD

I’ve created the xstartup as described for gnome as well as chmod +x it. when running vncserver:2 I get the attacked error.

vnc1.jpg
Kaziath
Kaziath
9 months ago
Reply to  EdXD

I get the exact same error. I have a fresh install of 22.04 Desktop. I don’t have any x-window-manager. Typing “x-window-manager”, ubuntu helpfully suggests many packages that provide it, none of them gnome. On your system, where is x-window-manager? Is it a softlink to something?

Agent31
Agent31
1 year ago
Reply to  EdXD

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.

Astro-Otter
1 year ago

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

Astro-Otter
1 year ago
Reply to  EdXD

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

Astro-Otter
1 year ago
Reply to  EdXD

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 🙂

Astro-Otter
1 year ago
Reply to  Astro-Otter

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

Astro-Otter
1 year ago
Reply to  EdXD

After remove ~/.vnc/ directory and disable service, i relaunch vncserver like a first time and /etc/vnc/xstartup is not created.
If i’m creating my own with :

  #!/bin/sh
  
  unset SESSION_MANAGER
  exec /etc/X11/xinit/xinitrc

,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

Joshi
Joshi
1 year ago
Reply to  Astro-Otter

Im also using this thread for vnc help connecting to my rpi 4, with a 16 channel servo hat mod, for fixed wing flight controls over 4g cellular networks, for really really long range/ ok maybe a global expanse of flying micro-servers. I find the live video feed lag/delay over these networks with vnc is, adjustably reasonable and quite adequete for even more high speed model drone flight controls, if you are willing to dial down the high color bandwidth of your live connection, and use a realtime video feed resolution of say (320×240,) its suffeciently responsive to fly with FPV, for obstacle avoidance and for landmark recognition. Having a remote desktop login for each flying device will be really nice too, with password security and the built-in “view-only” feature. Customized and laid out flight controls/HUD menu made simple with ubuntu mate. Just of recent, I finished coding (with python) the onboard control flaps/surfaces to respond to keyboard input, thru these vnc connections again, for planned manual flight cotrol and autopiloting overides, which I’m also developing with a digital mpu for autopilot/flight stabilazation, waypoint functionality, loiter, track and follow commands, and the ‘return to base’ auto-flight feature. thank you so much! im doing this mainly for personal gratification, for the love of flight, and for the open source community, which may find use inevitably with the potential for commercial applications, like wilderness search and rescue for example, using the inexpensive python infrared cameras now available, or for emergency preparedness. it’s abilty to securely drop and deploy encrypted wifi access points to remote regions could be useful, or maybe to look for survivors after an earthquake or tsunami, but of course the signal will drop out to sea without cell towers, unless someone sets up a network of autonomous cell/radio relay boyous. anyway, thanks!!! each success along the way feels nice.

Untitled2.jpg
Squeaky
Squeaky
1 year ago

FYI – Do NOT use any special charactors when creating the password or this process WILL NOT WORK.

I guess we’re back to 1980 and we can only use simple characters for passwords.

gilbert
gilbert
1 year ago

Anybody got problems with Snap and installing additional applications to XFCE?
I installed chromium but it´s impossible to start it:

$ 2022/10/05 08:55:02.865260 cmd_run.go:1055: WARNING: cannot start document portal: Expected portal at “/run/user/1000/doc”, got “/home/gilbert/.cache/doc”
/system.slice/system-vncserver.slice/[email protected] is not a snap cgroup

To me this sounds a bit as if the vncserver should have been installed via snap too…

gilbert
gilbert
1 year ago
Reply to  gilbert

played a bit around – chromium seems to work if installed from an alternative source via apt (snap disabled).
It´s the first time setting up a Ubuntu Server with SNAP and i already hate it…

Alex B.
Alex B.
1 year ago

Hi there! Great manual, almost all is work! )
Only one don’t work: vnc as service:
[email protected]: Can’t open PID file /home/ubuntu/.vnc/vps:5901.pid (yet?) after start: Operation not permited”
And directory /home/ubuntu/.vnc/ does not contain any pid file.
What is error may be?

Alex B.
Alex B.
1 year ago

update: https://bytexd.com/how-to-install-configure-vnc-server-on-ubuntu/#comment-2762

It is need to see simultaneously what file it creates in folder /home/user/.vnc/ vnc-server when make start and make string PIDFile=/home/user/.vnc/%H:590%i.pid like name this file. In my case – PIDFile=/home/ubuntu/.vnc/%H.vps.ovh.net:590%i.pid

All is work! Thank for manual! 😉

Ali`
Ali`
1 year ago

Hi i configured the vncserver using tiger vnc and it’s working okay on ubunto 22.04 but i have a problem that i cannot open internet browsers on my xfce4 desktop on the server.
Error:
“Failed to Execute Default Web Browser. Input/Output Error.”

Ali
Ali
1 year ago
Reply to  EdXD

Hi, thank you for your reply. I tried the default and also firefox they both gave me the same issue

naduxi
naduxi
1 year ago
Reply to  Ali

Uninstall the firefox snap, and try with the ppa. Full recipe here. It worked for me.

Steve S
Steve S
1 year ago

Hi,

Thanks for the very detailed and wonderful tutorial. Sadly, I have been unable to get this working on my fresh install of 22.04. I continually get the following:

Mon Feb 13 18:07:08 2023
 vncext:   VNC extension running!
 vncext:   Listening for VNC connections on all interface(s), port 5902
 vncext:   created VNC server for screen 0
[mi] mieq: warning: overriding existing handler (nil) with 0x55d56525c400 for event 2
[mi] mieq: warning: overriding existing handler (nil) with 0x55d56525c400 for event 3
3NI3X0 New Xtigervnc server ‘TheOpera2:2 (ssilva)’ on port 5902 for display :2.
3NI3X0 Use xtigervncviewer -SecurityTypes VncAuth,TLSVnc -passwd /home/ssilva/.vnc/passwd TheOpera2:2 to connect to the VNC server.
Terminated
 ComparingUpdateTracker: 0 pixels in / 0 pixels out
 ComparingUpdateTracker: (1:-nan ratio)

(x-window-manager:7118): Gtk-WARNING **: 18:07:08.386: cannot open display: TheOpera2:2

This is with my xstartup:

#!/bin/sh

# Start up the standard system desktop
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
export XAUTHORITY=$HOME/.Xauthority

#/usr/bin/startxfce4 &
/usr/bin/gnome-session

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources

export DISPLAY=TheOpera2:2

x-window-manager &

Any hints?

Steve

Steffen L
Steffen L
1 year ago
Reply to  Steve S

Hi Steve,
I set up all on Ubuntu 22.04 LTS just today.

My xstartup:
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4

an then run: chmod +x ~/.vnc/xstartup       

It works!

Rgds
S

Steve S
Steve S
1 year ago
Reply to  Steffen L

This did not work for me, but I discovered the problem! You MUST turn off wayland as the default windowing system. Once I did that, it worked!

https://askubuntu.com/questions/1410256/how-do-i-use-x-instead-of-wayland-on-22-04

Steve

Steffen L
Steffen L
1 year ago

-start TigerVNC-Server automatically during boot via systemd service

Ubuntu 22.04 LTS: Create service file [email protected]

I had to adapt the commands for 22.04. There are unkown user-names written… You forgot to change the code to “YOUR_USERNAME” everywhere the username is mentioned etc…

User=edxd
Group=edxd
WorkingDirectory=/home/edxd
PIDFile=/home/edxd/.vnc/%H:590%i.pid%H:590%i.pid

I thought this “edxd” is something new around ubuntu 22.04, but it is just the old username… and you have to change it to make the file working…

Furthermore the ExecStartPre answers with error all time. No matter what I try. But doing it out, is no problem. All works fine.

My code is:

[Unit]
Description=Start TigerVNC server at startup
After=syslog.target network.target
 
[Service]
Type=forking
User=sl
Group=sl
WorkingDirectory=/home/sl
 
PIDFile=/home/sl/.vnc/%H:590%i.pid
#ExecStartPre=-bash -c “/usr/bin/vncserver -kill :%i > /dev/null 2>&1”         
#ExecStartPre=-/bin/sh -c “/usr/bin/vncserver -kill :%i > /dev/null 2>&1”
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280×800 -localhost :%i
ExecStop=/usr/bin/vncserver -kill :%i
 
[Install]
WantedBy=multi-user.target

Hope that helps!
-Steffen

doc
doc
1 year ago

Hi;
Just install new ubuntu server 22.04 folled the xcf4 instructions and keep getting
unset: SESSION_MANAGER: bad variable name ?
thanks

Brian Oblivion
Brian Oblivion
11 months ago

I’m trying to get TigerVNC working on a fresh ubuntu 22 server installation. I followed the steps here and I’ve definitely gotten farther than from any other guide (THANK YOU!) but I’m still having problems.

I’m using the gdm3 with the ubuntu-desktop.

Basically, I’m trying to connect to my server which doesn’t have a keyboard,mouse or monitor plugged into it. So this 100% remote VNC.

If I ssh in and startup “vncserver -localhost no” at the command line, it startups fine and I can remote VNC into this display/port just fine. Everything works.

But the systemd service as defined/tweaked here, starts up on boot no problem. When I VNC connect to that display/port it “creates”, it connects but I get the error below.

I can’t find logs anywhere that suggests why it blew up.

Here’s my systemd config I’ve tweaked it a few different was with no change ( and for this post, substituted my real user name out).

BTW I did add the restart so it would restart after a disconnect.

[Unit]
Description=Start TigerVNC server at startup
After=syslog.target network.target

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

PIDFile=/home/MYUSER/.vnc/%H:590%i.pid
ExecStartPre=-bash -c “/usr/bin/vncserver -kill :%i > /dev/null 2>&1”
#ExecStartPre=-/usr/bin/vncserver -kill :%i
#ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280×800 -localhost no :%i
ExecStop=/usr/bin/vncserver -kill :%i
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

VNCXError.png
patienttruth
patienttruth
10 months ago
Reply to  Brian Oblivion

I’m having the same issue. However, I was able to successfully make the connection via the service once or twice, and now I’m getting the same error page immediately.

I’ve switched back to the non-service approach “vncserver”, and it starts and connects normally.

Carlo
Carlo
10 months ago

Hi everyone!
thank you very much for this wonderful guide and for the discussion.
I am running ubuntu 23.04. I want to add to the previous comments that I had to write the Exec statements as follows or it wouldn’t start:

ExecStartPre=-bash -c “/usr/bin/vncserver -kill :%i > /dev/null 2>&1”
ExecStart=-bash -c “/usr/bin/vncserver -depth 32 -geometry 1280×800 -localhost no :%i > /dev/null 2>&1”
ExecStop=-bash -c “/usr/bin/vncserver -kill :%i > /dev/null 2>&1”

My problem now is for audio. I have no audio when i connect to the server. Is there a way to add it? I am using tigerVNC and Realvnc as client.

I see in your output when you run sudo systemctl status vncserver@1 that the libpulseaudio is loaded while in mine is not

Jon
Jon
9 months ago

Are you sure this the systemd service works with GNOME?

Trying for Ubuntu20, I have tried just about every permutation imaginable, and I can only get the GNOME desktop to successfully load if it’s called interactively from SSH session, or via an ssh -f command to localhost itself for the ExecStart systemd commands.

Other variants, such as “su -l user …” “/bin/bash -i -….” etc simply will not work. In all cases, gnome complains of “unsupported session type” (apparently due to lack of real interactive terminal), and I haven’t been able to figure out a way to fake this short of using an “ssh -f” command, which is far from ideal because trying to automate a passwordless SSH trust from a generic starting point is a real pain, not to mention the fact that it seems unnecessary to have to SSH to itself to make this work.

I’ve also tried things such as cron on @reboot, and the results are the same — gnome simply isn’t going to work when not launched from interactive shell with appropriate user variables.

aussielunix
aussielunix
9 months ago

Great page but I needed some changes for Ubuntu 22.04 to work.

Do not create ~/.vnc/xstartup – the default at /etc/X11/Xtigervnc-session works fine.

Create an ~/.xsession and put a single entry in to select which DE to start.

# startxfce4
gnome-session

Atylarap
Atylarap
8 months ago

This useless.
It doesn’t work.

JailSeed
JailSeed
8 months ago

Thanks! Works like a charm for 22.04

Will S
Will S
8 months ago

For Budgie 22.04, the config needs to be budgie-desktop instead of budgie-session to work.

DD Dol
DD Dol
6 months ago
Reply to  Will S

Thank you for this, it’s quite extensive !
I’m trying to set it up as a service, with gnome, on ubuntu 22.
Starting with command line it works like a charm (/usr/bin/vncserver -depth 24 -geometry 1280×800 -localhost no :1 ): the server is laucned and I can access it with my client.

But if I start the service, with my username setup everywhere, the service starts but the client crashs on first connection, with a stupid message like “something get wrong”. In the file ~/.vnc/MY_MACHINE:5901.log I see something like that:

Connections: accepted: 192.168.200.113::64462
SConnection: Client needs protocol version 3.3
SConnection: AuthFailureException: Authentication failure
VNCSConnST: closing 192.168.200.113::64462: Authentication failure

Any idea ?

Alex
Alex
1 month ago

This is a fantastic guide. Including the ‘xstartup’ files for each DE is extremely helpful.

adi
adi
10 days ago

GNOME Issue: installing the setup with gnome as decribed above worked well, exept of the facts that the gnome dock does not show up (only the “activity” panel), neither are folder icons presented at the desktop. When the screen logs out after 5min I can see the prompt to type in the PW, however typing the pw into the prompt doesn’t work. Did anyone encounter the same problems?

Last edited 10 days ago by adi
You May Also Like