How to install and configure VNC on Ubuntu 18.04

Introduction

In this guide, you will set up a VNC server on an Ubuntu 18.04 server and connect to that server securely through an SSH tunnel. You will use TightVNC, a fast and lightweight remote control software package. This choice will ensure that our VNC connection is smooth and stable even on slower Internet connections.

Preparation

To complete this tutorial, you need:

Step 1-Install Desktop Environment and VNC Server

By default, Ubuntu 18.04 server does not have a graphical desktop environment or VNC server installed, so we install them first. Specifically, we will install the software package for the latest Xfce desktop environment and the TightVNC package provided in the official Ubuntu repository.

On your server, update your package list:

sudo apt update

Now install the Xfce desktop environment on your server:

sudo apt install xfce4 xfce4-goodies

After the installation is complete, install the TightVNC server:

sudo apt install tightvncserver

To complete the initial configuration of the VNC server after installation, use the vncserver command to set a secure password and create an initial configuration file:

vncserver

You will be prompted to enter and verify a password to access your computer remotely:

You will require a password to access your desktops.
​
Password:
Verify:

The password length must be between six and eight characters. Passwords longer than 8 characters will be automatically truncated.

After verifying the password, you can choose to create a view-only password. Users who log in with the view-only password will not be able to use the mouse or keyboard to control the VNC instance. This is a useful option if you want to use a VNC server to present content to others, but it is not required.

Then, the process creates the necessary default configuration files and connection information for the server:

Would you like to enter a view-only password(y/n)? n
xauth:  file /home/sammy/.Xauthority does not exist
​
New 'X' desktop is your_hostname:1
​
Creating default startup script /home/sammy/.vnc/xstartup
Starting applications specified in/home/sammy/.vnc/xstartup
Log file is /home/sammy/.vnc/your_hostname:1.log

Now let's configure the VNC server.

Step 2-Configure VNC server

The VNC server needs to know the commands to be executed at startup. Specifically, VNC needs to know which graphical desktop it should connect to.

These commands are located in the configuration file called in the .vnc folder of xstartup in the home directory. The startup script was created when vncserver was run in the previous step, but we will create our own script to launch the Xfce desktop.

When VNC is set up for the first time, it will start a default server instance on port 5901. This port is called display port and is called :1 by VNC. VNC can launch multiple instances on other display ports, such as :2, :3, etc.

Because we are going to change the way the VNC server is configured, first 5901 uses the following command to stop the VNC server instance running on the port:

vncserver -kill :1

The output should look like this, although you will see a different PID:

Killing Xtightvnc process ID 17648

Before modifying the xstartup file, please back up the original file:

mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

Now create a new xstartup file and open it in a text editor:

nano ~/.vnc/xstartup

Whenever you start or restart the VNC server, the commands in this file will be executed automatically. If it is not already started, we need VNC to start our desktop environment. Add these commands to the file:

#! /bin/bash
xrdb $HOME/.Xresources
startxfce4 &

The first command xrdb $HOME/.Xresources in the file tells the VNC GUI framework to read the .Xresources file of the server user. Users can change some settings of the graphical desktop in .Xresources, such as terminal colors, cursor themes and font rendering. The second command tells the server to start Xfce, where you can find all the graphics software you need to manage the server comfortably.

To ensure that the VNC server can use this new startup file correctly, we need to make it executable.

sudo chmod +x ~/.vnc/xstartup

Now, restart the VNC server.

vncserver

You will see output similar to this:

New 'X' desktop is your_hostname:1
​
Starting applications specified in/home/sammy/.vnc/xstartup
Log file is /home/sammy/.vnc/your_hostname:1.log

Once the configuration is in place, let's connect to the server from the local computer.

Step 3-Securely connect to the VNC desktop

VNC itself does not use security protocols when connecting. We will use an SSH tunnel to connect to our server securely, and then tell our VNC client to use that tunnel instead of connecting directly.

Create an SSH connection on the local computer to safely forward to the localhost VNC connection. You can do this through the terminal on Linux or macOS using the following command:

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

The port binding specified by the -L switch. In this case, we bind the port 5901 for remote connection of 5901 to the port on the local computer. The -C switch enables compression, and the -N switch tells ssh that we do not want to execute remote commands. The -l switch specifies the remote login name.

Remember to replace sammy and your_server_ip with the sudo non-root username and IP address of your server.

If you are using a graphical SSH client (such as PuTTY), please use your_server_ip as the connection IP, and set localhost:5901 as the new forwarding port in the SSH tunnel settings of the program.

After the tunnel is running, use the VNC client to connect to localhost:5901. You will be prompted to authenticate with the password set in step 1.

After connecting, you will see the default Xfce desktop. It should look like this:

You can use the file manager or the command line to access the files in the home directory as follows:

Press CTRL+C terminal to stop the SSH tunnel and return to your prompt. This will also disconnect your VNC session.

Next let's set up the VNC server as a service.

Step 4-Run VNC as a system service

Next, we set the VNC server as a systemd service so that we can start, stop, and restart it as needed, just like any other service. This will also ensure that VNC starts when the server restarts.

First, create a new /etc/systemd/system/[email protected] unit file using your favorite text editor:

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

@ The symbol at the end of the name will let us pass in a parameter that we can use in the service configuration. We will use it to specify the VNC display port we want to use when managing the service.

Add the following lines to the file. Be sure to change the values of User, Group, WorkingDirectory, and the username in the PIDFILE value to match your username:

[ Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target
​
[ Service]
Type=forking
User=sammy
Group=sammy
WorkingDirectory=/home/sammy
​
PIDFile=/home/sammy/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i >/dev/null2>&1
ExecStart=/usr/bin/vncserver -depth 24-geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i
​
[ Install]
WantedBy=multi-user.target

If VNC is already running, the ExecStartPre command will stop. The ExecStart command starts VNC and sets the color depth to 24-bit color with a resolution of 1280x800. You can also modify these startup options to meet your needs.

Save and close the file.

Next, let the system know about the new unit file.

sudo systemctl daemon-reload

Enable unit files.

sudo systemctl enable [email protected]

The @ symbol below 1 indicates that the service with its display number should have appeared. In this case, the default :1 is discussed in step 2..

If the VNC server is still running, stop its current instance.

vncserver -kill :1

Then start it, just like any other systemd service.

sudo systemctl start vncserver@1

You can verify whether it has started with this command:

sudo systemctl status vncserver@1

If it starts correctly, the output should look like this:

[email protected] - Start TightVNC server at startup
 Loaded:loaded(/etc/systemd/system/[email protected]; indirect; vendor preset: enabled)
 Active:active(running) since Mon 2018-07-0918:13:53 UTC; 2min 14s ago
 Process:22322 ExecStart=/usr/bin/vncserver -depth 24-geometry 1280x800 :1(code=exited, status=0/SUCCESS)
 Process:22316 ExecStartPre=/usr/bin/vncserver -kill :1>/dev/null2>&1(code=exited, status=0/SUCCESS)
 Main PID:22330(Xtightvnc)
​
...

After restarting your computer, your VNC server is now available.

Start the SSH tunnel again:

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

Then use the VNC client software to establish a new connection localhost:5901 to connect to your computer.

in conclusion

You have now up and running a secure VNC server on the Ubuntu 18.04 server. Now you will be able to manage files, software and settings using an easy-to-use and familiar graphical interface, and you will be able to remotely run graphical software such as a web browser.

For more Ubuntu tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.

Reference: "How to Install and Configure VNC on Ubuntu 18.04"

Recommended Posts

How to install and configure VNC on Ubuntu 18.04
How to install and configure NATS on Ubuntu 16.04
How to install and configure Gogs on Ubuntu 18.04
How to install and configure Cyberpanel on Ubuntu 18.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure ownCloud on Ubuntu 16.04
How to install and configure VNC on CentOS 8
How to install and configure Ansible on Ubuntu 18.04
How to install and configure Elasticsearch on Ubuntu 16.04
How to install and configure PostGIS on Ubuntu 14.04
How to install and configure Sphinx on Ubuntu 16.04
How to install and configure OrientDB on Ubuntu 14.04
How to install and configure AppScale on Ubuntu 12.04
How to install and configure PostGIS on Ubuntu 14.04
How to install Pycharm and Ipython on Ubuntu 16.04/18.04
How to install and configure Elasticsearch on CentOS 7
How to install and use Docker on Ubuntu 20.04
How to install and use Composer on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to install and secure phpMyAdmin on Ubuntu 16.04
How to install and configure Redis on CentOS 8
How to install and use Composer on Ubuntu 20.04
How to install and use BaasBox on Ubuntu 14.04
How to install and use PostgreSQL on Ubuntu 16.04
How to install and configure phpMyAdmin on CentOS 6
How to install and configure Owncloud on CentOS 8
How to install and use Docker on Ubuntu 16.04
How to install and configure Redmine on CentOS 8
How to install Ruby on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
Install and configure MySQL on Ubuntu
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04