VNC server is used to share graphical desktop which can be controlled from other computers.
Basically ubuntu server does not contain GUI, which needs to be installed before installing VNC server. Please note that ubuntu server edition is carefully designed to utilize less hardware resources (minimal environment), installing GUI might leads to high hardware utilization.
In this guide, we will be setting up VNC on a Ubuntu 16.04 server and connecting to it securely through an SSH tunnel. The VNC server we will be using is TightVNC, a fast and lightweight remote control package. This choice will ensure that our VNC connection will be smooth and stable even on slower internet connections.
1: Installing the Desktop Environment and VNC Server
By default, an Ubuntu Server 16.04 does not come with a graphical desktop environment or a VNC server installed, so we’ll begin by installing those. Specifically, we will install packages for the latest Xfce desktop environment and the TightVNC package available in the official Ubuntu repository.
On your server, install the Xfce and TightVNC packages.
$ sudo apt install xfce4 xfce4-goodies tightvncserver
To complete the VNC server’s initial configuration after installation, use the vncserver
command to set up a secure password.
$ vncserver
You’ll be promoted to enter and verify a password, and also a view-only password. Users who log in with the view-only password will not be able to control the VNC instance with their mouse or keyboard. This is a helpful option if you want to demonstrate something to other people using your VNC server, but isn’t necessary.
Note: if you are not promoted to enter and verify a password, you can use the following command to do so. See here (pdf) for more info about vncpasswd command.
$ vncpasswd
Running vncserver
completes the installation of VNC by creating default configuration files and connection information for our server to use. With these packages installed, you are now ready to configure your VNC server.
2: Configuring the VNC Server
First, we need to tell our VNC server what commands to perform when it starts up. These commands are located in a configuration file called xstartup
in the .vnc
folder under your home directory. The startup script was created when you ran the vncserver
in the previous step, but we need modify some of the commands for the Xfce desktop.
When VNC is first set up, it launches a default server instance on port 5901. This port is called a display port, and is referred to by VNC as :1
. VNC can launch multiple instances on other display ports, like :2
, :3
, etc. When working with VNC servers, remember that :X
is a display port that refers to 5900+X
.
Because we are going to be changing how the VNC server is configured, we’ll need to first stop the VNC server instance that is running on port 5901.
$ vncserver -kill :1
The output should look like this, with a different PID:
Killing Xtightvnc process ID 17648
Before we begin configuring the new xstartup
file, let’s back up the original.
$ mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
Now create a new xstartup
file with nano
or your favorite text editor.
$ nano ~/.vnc/xstartup
Paste these commands into the file so that they are performed automatically whenever you start or restart the VNC server, then save and close the file.
#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &
The first command in the file, xrdb $HOME/.Xresources
, tells VNC’s GUI framework to read the server user’s .Xresources
file. .Xresources
is where a user can make changes to certain settings of the graphical desktop, like terminal colors, cursor themes, and font rendering. The second command simply tells the server to launch Xfce, which is where you will find all of the graphical software that you need to comfortably manage your server.
To ensure that the VNC server will be able to use this new startup file properly, we’ll need to grant executable privileges to it.
$ sudo chmod +x ~/.vnc/xstartup
Now, restart the VNC server.
$ vncserver
The server should be started with an output similar to this:
New 'X' desktop is your_server_name.com:1
Starting applications specified in /home/liping/.vnc/xstartup
Log file is /home/liping/.vnc/liniverse.com:1.log
At this point you have a VNC server running on display :1. You will use this information later when you want to connect to it.
3: Testing the VNC Desktop
connect directly to the server using VNC Viewer.
on your client computer (not on the server), install vncviewer using the command below (assuming your client computer is running Ubuntu or linux-based OS).
$ sudo apt-get install vncviewer
then type the command below in your client computer terminal to connect to your server via the VNC Desktop. Note the space after your server host name and before “:”
$ vncviewer -via username@yourserver_hostname :1
You need to change the port number 1 to yours.
Once you are connected, you should see the default Xfce desktop. It should look something like this:
You can access files in your home directory with the file manager or from the command line, as seen here:
4: Creating a VNC Service File
Next, we’ll set up the VNC server as a systemd service. This will make it possible to start, stop, and restart it as needed, like any other systemd service.
First, create a new unit file called /etc/systemd/system/vncserver@.service
using your favorite text editor:
$ sudo nano /etc/systemd/system/vncserver@.service
Copy and paste the following into it. Be sure to change the value of User and the username in the value of PIDFILE to match your username.
[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target
[Service]
Type=forking
User=liping
PAMName=login
PIDFile=/home/liping/.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 the file.
Next, make the system aware of the new unit file.
$ sudo systemctl daemon-reload
Enable the unit file.
$ sudo systemctl enable vncserver@1.service
Stop the current instance of the VNC server if it’s still running.
$ vncserver -kill :1
Then start it as you would start any other systemd service.
$ sudo systemctl start vncserver@1
You can verify that it started with this command:
$ sudo systemctl status vncserver@1
If it started correctly, the output should look like this:
NOTE: DO NOT USE the shut down or log off menu in your VNC viewer, instead, just click the cross button at the top left corner. Otherwise, it will cause problems when you try to connect to it next time. Because this is a virtual GUI, remember not to use the shut down etc. menu on the top right corner of your GUI.
Conclusion
You should now have a secured VNC server up and running on your Ubuntu 16.04 server. Now you’ll be able to manage your files, software, and settings with an easy-to-use and familiar graphical interface.
Posts referenced:
How to Install and Configure VNC on Ubuntu 16.04 (April 26, 2016, digitalocean.com)
How to install VNC server on ubuntu 14.04
Setup VNC in Ubuntu 16.04
How To Install VNC Server On Ubuntu 14.04
How do you run Ubuntu Server with a GUI?
Administer Ubuntu Server Trusty Tahr 14.04LTS using VNC