Setup Xfce desktop on remote ubuntu 16.10 server and access it via VNC

By | March 6, 2017

Running desktop on remote server

Usually you would use the ssh terminal when working on a remote linux server. But there can be times when you need to run a gui app on the server and keep it running for quite some time.

Recently I had to do something similar and therefore I setup an Ubuntu server with desktop and accessed it via VNC.

The idea is simple. Install any desktop environment of your choice on the server. In this tutorial we shall be using Xfce, as it has a light footprint compared to the larger ones like Gnome and KDE.

Then use vnc server to launch the desktop environment and create an X display session that we shall access from our local desktop machine through a vnc client.

Install Desktop environment and VNC server

Xfce is a lightweight desktop and ideal for usage on a remote server. First install the xfce packages and tightvnc server. It would be a good idea to update the package cache before doing the actual install.

sudo apt-get update
sudo apt-get install xfce4 xfce4-goodies tightvncserver

Note that this will only install the packages and not start anything. We shall be starting the vncserver with specific settings on our own, later in this guide.

If the dpkg process exits unexpectedly, you may have to run the following command -

# sudo dpkg --configure -a

Create a new user for vnc

The next thing to do is create a unix user that will be used during the vnc sessions. The username can be anything. Use the adduser command.

# adduser mike
Adding user `mike' ...
Adding new group `mike' (1001) ...
Adding new user `mike' (1001) with group `mike' ...
Creating home directory `/home/mike' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for mike
Enter the new value, or press ENTER for the default
        Full Name []: 
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
Is the information correct? [Y/n] 

The vncserver shall launch the desktop environment with this unix user. Which means, that you shall be this user when working on the remote desktop

Setup the "vnc password" for the user

The vnc server maintains a separate password that is used to login to the vnc server via vnc clients. This password is different from the unix user password. It is configured using the vncpasswd command.

First switch to user "mike" that was created in the previous step and setup the vnc server password.

# su - mike

Next use the vncpasswd command

$ vncpasswd
Using password file /home/mike/.vnc/passwd
VNC directory /home/mike/.vnc does not exist, creating.
Would you like to enter a view-only password (y/n)? 

Note that the passwd file did not exist and was created for the first time in this step.

If you have already run the vncserver command previously, then it would have created the files. When you run vncserver for the first time, it creates a default startup script

$ vncserver 

You will require a password to access your desktops.

Password too short
enlightened@desktop:~$ vncserver 

You will require a password to access your desktops.

Would you like to enter a view-only password (y/n)? n

New 'X' desktop is desktop:1

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

However, we dont need to run the vncserver command. It shall be launched automatically using startup scripts.

Create the xstartup script

The next important file is the xstartup script. It contains instructions on which X apps to start. The desktop environment is the X application that we have to start.

First take a backup of the file if it already exists

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

Now edit it using nano

vnc@server:~$ nano .vnc/xstartup

Note - This is inside the home directory of the user vnc, that is /home/mike/.vnc/xstartup

Enter the following lines in the xstartup script

xrdb $HOME/.Xresources
startxfce4 &

The startxfce4 command will launch the xfce desktop. Save the file and close it.

Make the xstartup file executable. This is necessary so that vncserver can execute this file.

$ chmod +x ~/.vnc/xstartup

Create vnc service file

The next step is to create a vnc service file so that we can start vnc server using the service command and don't have to run the vncserver command every time.

Make sure to enter the correct username in the USER variable. This is the user that vnc server is going to use to start the desktop session.

root@bintu:~# sudo nano /etc/init.d/vncserver

Paste the following script

export USER="mike"
OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
. /lib/lsb/init-functions

case "$1" in
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"

log_action_begin_msg "Stoping vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"

$0 stop
$0 start
exit 0

Save the file and close it. Make it executable

# chmod +x /etc/init.d/vncserver

Start the service

Now everything is read. Its time to run the vncserver and test our step.

First reload systemctl so that it can use the vncserver startup script.

systemctl daemon-reload

Now start vncserver. It starts a server on port 5901

# service vncserver start

Check that its running

root@bintu:~# service vncserver status
‚óŹ vncserver.service
   Loaded: loaded (/etc/init.d/vncserver; bad; vendor preset: enabled)
   Active: active (exited) since Thu 2017-03-02 05:36:42 UTC; 6s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 24877 ExecStart=/etc/init.d/vncserver start (code=exited, status=0/SUCCESS)

Mar 02 05:36:40 bintu systemd[1]: Starting vncserver.service...
Mar 02 05:36:40 bintu vncserver[24877]:  * Starting vncserver for user 'vnc' on localhost:1...
Mar 02 05:36:40 bintu su[24885]: Successful su for vnc by root
Mar 02 05:36:40 bintu su[24885]: + ??? root:vnc
Mar 02 05:36:40 bintu su[24885]: pam_unix(su:session): session opened for user vnc by (uid=0)
Mar 02 05:36:42 bintu vncserver[24877]: New 'X' desktop is bintu:1
Mar 02 05:36:42 bintu vncserver[24877]: Starting applications specified in /home/vnc/.vnc/xstartup
Mar 02 05:36:42 bintu vncserver[24877]: Log file is /home/vnc/.vnc/bintu:1.log
Mar 02 05:36:42 bintu systemd[1]: Started vncserver.service.
$ cat ~/.vnc/*.pid
# ps -ef | grep tightvnc
vnc      24574     1  0 05:32 ?        00:00:00 Xtightvnc :1 -desktop X -auth /home/vnc/.Xauthority -geometry 1024x768 -depth 16 -rfbwait 120000 -rfbauth /home/vnc/.vnc/passwd -rfbport 5901 -fp /usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb
root     24744 10412  0 05:33 pts/0    00:00:00 grep --color=auto tightvnc

Check the open ports of the vnc server. You need the correct port number when connecting from the vnc client

# netstat -nlp | grep vnc
tcp        0      0  *               LISTEN      24574/Xtightvnc 
tcp        0      0  *               LISTEN      24574/Xtightvnc 
unix  2      [ ACC ]     STREAM     LISTENING     5225386  24574/Xtightvnc     /tmp/.X11-unix/X1

Vnc server can also be started by calling the script directly.

# /etc/init.d/vncserver start
[ ok ] Starting vncserver (via systemctl): vncserver.service.

To stop vncserver

# service vncserver stop

Install vncviewer client on Desktop

Now that we have the vnc server up and running with the gui desktop environment, its time to install a vnc client and access the remote desktop.

Install xtightvncviewer on Ubuntu.

$ sudo apt-get install xtightvncviewer

Now connect to the remote vnc server using the vncviewer command.

$ vncviewer -quality 5 -encodings "copyrect tight hextile zlib corre rre raw" -compresslevel 5 IPADDR:5901

We use lower quality and compression encodings to compress the image data being transferred and make it faster.

Using other vnc viewers like KRDC might be slower.


For further reading, check out these useful links -

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

One Comment

Setup Xfce desktop on remote ubuntu 16.10 server and access it via VNC

Leave a Reply

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