How to Install VNC on Ubuntu 18.04 (Windows, Linux and Mac)

How to Install VNC on Ubuntu 18.04 & 16.04 (Windows, Linux and Mac)

Last updated on

In this guide we will install the xfce4 desktop environment and VNC server to allow us to control Ubuntu 18.04 remotely from Linux, Mac or Windows. This guide has also been tested on Ubuntu 16.04 to Ubuntu 17.10.

Prerequisites

You should use a non-root user account with sudo privileges. Please see the Initial Server Setup for Ubuntu 18.04 / 18.10 or Initial Server Setup for Ubuntu 16.04 / 17.10 for more details.

You will also need to download and install a VNC client for your OS. We recommend the following:

Windows VNC Clients

MacOS VNC Clients

Linux VNC Clients

1. Install Xfce and VNC

Begin by updating the package lists.

sudo apt update

Now install Xfce4 and VNC server. Press y and ENTER if prompted to install.

sudo apt install xfce4 xfce4-goodies tightvncserver

2. Configure VNC Server

Run vncserver.

vncserver

You will be asked to enter a password. Generate a password 8 chars long, any longer and it will be truncated.

You will also be asked if you want a view-only password where the user can only view and not control the remote server. You can skip this if you don’t require it.

VNC Server should now be running.

xauth: file /home/john/.Xauthority does not exist

New 'X' desktop is ubuntu_server:1

Creating default startup script /home/john/.vnc/xstartup
Starting applications specified in /home/john/.vnc/xstartup
Log file is /home/john/.vnc/ubuntu_server:1.log

We now need to edit the xstartup file. Before doing so, kill VNC service.

vncserver -kill :1

Back up xstartup file.

cp ~/.vnc/xstartup ~/.vnc/xstartup.bak

Edit xstartup file.

nano ~/.vnc/xstartup

Add the following to the bottom of the file.

~/.vnc/xstartup
startxfce4 &

This will tell VNC to start the Xfce desktop environment when VNC starts.

Save and close. (press CTRL + X, press Y and then press ENTER)

Make the xstartup file executable.

sudo chmod +x ~/.vnc/xstartup

Now start vncserver again.

vncserver
New 'X' desktop is ubuntu_server:1

Starting applications specified in /home/john/.vnc/xstartup
Log file is /home/john/.vnc/ubuntu_server:1.log

3. Create SSH Tunnel

VNC server is now running, but in order to encrypt communications between the VNC client and server, we must create an SSH tunnel and connect through it. We can do this by binding port 5901 of the remote connection to port 5901 on your local machine.

Linux and Mac

You can create an SSH tunnel easily using terminal. Replace john with your non-root username and your_server_ip with your server IP.

ssh -L 5901:127.0.0.1:5901 -C -N -l john your_server_ip

Enter your Linux password (not your VNC password) and skip to Step 4 below.

Windows

You will need the PuTTY client to create an SSH Tunnel.

In PuTTY, select Connection > SSH > Tunnels.

In Source Port, enter 5901

In Destination, add your_ip:5901

Click Add.

In the left-hand category menu, go to Session.

In Host Name, enter your non-root Linux username and IP (e.g, [email protected]).

In Saved Sessions, enter “SSH Tunnel” (you can call this whatever you like).

Click Save and then click Open to start the connection.

PuTTY SSH tunnel and 5901 port forward

When prompted, enter your Linux password to log in (not your VNC password).

Once logged in, to check if the SSH tunnel is up, right-click the PuTTY title bar to invoke the context menu, then click Event Log.

If the tunnel is up, at the bottom of the log, you should see Local port 5901 forwarding to ip_address:5901

PuTTY SSH tunnel and 5901 port forward

You can now continue to the next step to connect to the VNC server using your VNC client.

4. Connect to VNC

Port 5901 of the remote connection should now be bound to port 5901 on your local machine, creating a secure SSH tunnel. You should now be able to connect to the VNC server using locahost:5509 with your VNC client of choice.

VNC Viewer on Windows and MacOS

There are many different VNC viewers for Windows and MacOS, but in this guide we are using VNC Viewer (RealVNC).

Run VNC Viewer, click File and click New Connection.

In the VNC Server field, enter localhost:5901 and click OK.

RealVNC connection window

Double-click localhost:5901.

VNC Viewer Connections screen

When prompted for a password, enter the VNC password you created earlier, check Remember Password, and click OK.

If you see a warning about Unencrypted connection, ignore it and click Continue. (VNC Viewer is not aware that we are using an encrypted SSH tunnel)

If VNC connects successfully, you should see the default Xfce desktop.

Click Use default config if prompted.

Xfce default Desktop via VNC

Linux

If you are using Ubuntu Desktop, it comes preloaded with a VNC-capable client called Remmina. Just type “VNC” or “remmina” in the dash to start it.

In the Name field, Enter a profile name of your choice.

In the Server field, enter localhost::5901.

In the Password, field, enter your VNC password your created earlier.

Click Connect.

Remmina Ubuntu VNC connection window

If VNC connects successfully, you should see the default Xfce desktop.

Click Use default config if prompted.

Xfce default Desktop via VNC

5. Run VNC as a Service

You should now make VNC a service so that it starts automatically should you ever reboot your server.

Create a new unit file for VNC. A unit file encodes information about a service

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

Paste in the following, replacing john with your own user name. You can also change 1280x800 to your desired resolution.

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

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

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

[Install]
WantedBy=multi-user.target

Save and close. (press CTRL + X, press Y and then press ENTER)

Reload systemd manager configuration.

sudo systemctl daemon-reload

Enable the unit file you just created.

sudo systemctl enable [email protected]

Kill vncserver.

vncserver -kill :1

Start vncserver again.

sudo systemctl start [email protected]

Ensure service is running.

sudo systemctl status [email protected]

If the vncserver is running, you should see:

 [email protected] - Start TightVNC server at startup
Loaded: loaded (/etc/systemd/system/[email protected]; indirect; vendor preset: enabled)
Active: active (running) since Sat 2019-02-16 18:14:22 CET; 6s ago
Process: 31449 ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :1 (code=exited, status=0/SUCCESS)
Process: 31436 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=2)
Main PID: 31461 (Xtightvnc)
Tasks: 57 (limit: 1152)
CGroup: /system.slice/system-vncserver.slice/[email protected]

Now if you ever reboot your server, the VNC Server service should start automatically.

To connect to VNC again, don’t forget to start your SSH tunnel in Step 3.

Let me know in the comments if this helped. Follow me on Twitter, Facebook and YouTube.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

1 Star2 Stars3 Stars4 Stars5 Stars 5.00 (3 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *

We use Markdown to style comments, like on Github and Reddit.
To do a line break, type two spaces after the sentence.
You can add inline code by wrapping it in backticks: `code here`

    To do an entire block of code  
    type four spaces before the line
    and it will appear in a block like this.
    <-- four empty spaces

2 replies

really works on 18.04, with some small error, but it starts-up, the error could be because of other tutorials that didnt work…
thank you guys!!!

how about samba tutoria to work with graphical gui on lubuntu?
regards from PowerPC G5 running lubuntu with 16 gigs of ram and 18 users for server ;)))