Posted by tony on January 8, 2018
How to Install and Configure VNC Server on Ubuntu 16.04
While the domination of Linux on the server scene is undisputed, managing servers isn't for everyone. One big reason is that people just don't know their way around the command line even if they are knowledgeable about what goes where. This prevents even highly experienced power users from being able to configure the server to their liking.
With VNC, thankfully, there is a way out. Short for Virtual Network Computing, VNC allows a user to work on a server using their keyboard and mouse, exactly as if they were managing their own computer. But the astute reader would have noticed: how can you use a mouse on a server? You can't, and that's why VNC requires the installation of a graphical environment as well. Let's see how this is done.
Install VNC Server and a Desktop Environment
The first step is to install the VNC server and a desktop environment that VNC can expose over the network. This is really like the chicken-and-egg problem: you must log in through the command line first to be able to install a graphical environment!
Anyway, assuming you are logged in to your server, install the XFCE graphical environment and TightVNC, a popular VNC server:
# apt install xfce xfce-goodies
# apt install tightvncserver
The xfce-goodies package contains visual tweaks and some other enhancements that help you get more out of your XFCE environment. Why XFCE and not something else? Well, XFCE is one of the oldest, most popular, and most mature graphical environments available.
In fact, if you've ever done any disk rescue or repair using some bootable tools, you have come across XFCE before. Sure, it doesn't look even half as good as the GNOME or KDE environments, but it's blazingly fast even on crappy hardware. This makes XFCE ideal for network environments, where bandwidth is at a premium.
With this, the VNC server is installed.
Set Up Passwords for NVC Server
The next step is to create passwords for your VNC access account. Generally, passwords aren't mentioned in a tutorial on server management, but there is something special in the case of a VNC server. VNC allows you to create two types of passwords: regular passwords and view-only passwords.
If you connect to your VNC system using a regular password, you will be able to control it as you wish; however, with a view-only password, you can only see what's going on – kind of like watching a presentation. The view-only passwords can be useful for your use-case, so you might want to set one up.
We'll come to that after we've set up a regular user.
Create a user with sudo privileges and switch accounts
At this point, you should create a regular user on your Linux machine with sudo privileges. That's because using the system directly as a root user can pose security risks, and can be just plain destructive if you're not careful.
Assuming that the user is going to be called phil, do the following:
# adduser phil
After this, you'll be asked to set a password and provide some optional details. Once you're done, it's time to add this user to the 'sudo' group:
# usermod -aG sudo phil
Now switch accounts and log in as this user:
# su - phil
Enter the password you set up earlier, and we're good to go. For good measure, also make sure this user actually was given sudo privileges:
$ sudo apt update
Enter the password, and if the package manager succeeds in updating, your job is done.
And finally, it's time to set up the passwords.
To do that, simply issue the following command and follow the on-screen instructions:
What now remains is the configuration part.
Configuring VNC Server on Ubuntu 16.04
The next step is to configure the VNC server to stream everything through XFCE. Currently, while we do have XFCE installed, the VNC server is unaware of its existence.
First, we need to stop the VNC server:
$ vncserver -kill :1
What's the odd-looking `:1` part at the end? This is how VNC server marks the port number it's using. Take 5900 and add to it the number after the colon – in this case, '1' – and you get 5901, the default port for VNC. Similarly, :4 will refer to port number 5900 + 4 = 5904.
The VNC server creates a configuration file in your home directory, which is what we need to edit. But first, it's a good idea to make a backup of that file (just in case we end up screwing everything and want to go back to square one).
$ mv ~/.vnc/xstartup ~/.vnc/xstartup.old
Then create a new file using your favorite command-line text editor:
$ vi ~/.vnc/xstartup
Inside this file, we need three simple lines:
Finally, just make sure the VNC server can read and execute this file:
$ chmod +x !/.vnc/xstartup
And then, start the vnc server:
If all went well, you shouldn't see any error, and you now have a working VNC setup. Now use a VNC client of your liking and enjoy!