How to Install and Configure VNC on an Alibaba Cloud ECS Instance

By Ghulam Qadir, Alibaba Cloud Community Blog author.

Virtual Network Computing (VNC) is a connection system that allows you to intuitively use your keyboard and mouse to interact with a graphical desktop environment on a remote server — an advanced version of Remote Desktop. This in turn can make managing files, software, and settings on a remote server significantly easier and more intuitive for both you and other users who are not yet comfortable with using a command line interface (CLI).

In this tutorial, we will be setting up VNC on an Alibaba Cloud ECS instance that is installed with Ubuntu version 16.04 and will be connecting to it securely through SSH tunneling. For this instance, or VNC server, we will be using is TightVNC, which is a fast and lightweight remote control package. Chosing this service ensures that our VNC connection will be smooth and stable, even on slower internet connections.

VNC is compatible and works well with Alibaba Cloud Elastic Compute Service (ECS) instance servers, and so this installation should be a very smooth process. As a developer myself, I prefer to use the services of Alibaba Cloud to Install Composer on Ubuntu version 16.04. Next, Alibaba Cloud Elastic Compute (ECS) is highly flexible and you can upgrade the hardware resources anytime when you get more traffic.


  1. You have to setup your server’s hostname.
  2. You have access to the VNC console using Alibaba Cloud account, or the SSH client installed in your PC.


Logging to Your Console

Just as a friendly reminder, again, before you begin this tutorial, you’ll need:

  • An Alibaba Cloud ECS instance installed with Ubuntu version 16.04 with at least 2 GB of RAM, private networking enabled, and one non-root user.
  • A local computer with a VNC client installed that supports VNC connections over SSH tunnels. If you are using Windows, you could use TightVNC, RealVNC, or UltraVNC. And for Mac OS X users, you can use the built-in Screen Sharing program, or can use a cross-platform app like RealVNC. Linux users can choose from many options, such as Vinagre and KRDC in addition to RealVNC and TightVNC.

Installing the Dependencies

First, update the package manager cache by running the following commands:

# sudo apt-get update# sudo apt-get upgrade

Creating a New User

# useradd –m –s /bin/bash gqadir# passwd gqadir

Now add the newly created user gqadir to the sudo group for root privileges access by running the command: # usermod –a –G sudo gqadir.

Installing a Desktop Environment and VNC Server

sudo apt-get install ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal

Now install XFCE Desktop and TightVNC packages by running the following commands:

sudo apt-get updatesudo apt install xfce4 sfce4-goodies tightvncserver

Installing the VNC Server

After you use this command, you’ll be prompted 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.

Running vncserver for the first time will automatically create new configuration directory .vnc and run the first VNC session.

Use the following commands to check the VNC session.

ls –lah ~/.vnc/ps –ef | grep Xtightvnc

With these packages installed, you are now ready to configure your VNC server.

Configuring the VNC Server

OutputKilling Xtightvnc process ID 19443

It better to keep the backup of existing xstartup file. You can run this command (mv ~/.vnc.xtartup ~/.vnc/xstartup.bak) to backup. Now, edit the file ~/.vnc/xstartup to match the configuration given below by running this command: nano ~/.vnc/xstartup. Finally, paste these following commands into the file so that they are performed automatically whenever you start or restart the VNC server:

~/.vnc/xstartup#!/bin/sh# Uncomment the following two lines for normal desktop:# unset SESSION_MANAGER# exec /etc/X11/xinit/xinitrc[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresourcesxsetroot -solid greyvncconfig -iconic &x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &x-window-manager &gnome-panel &gnome-settings-daemon &metacity &nautilus &

Save the file and exit from the text editor.

Next, 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. You can do so by running this command: sudo chmod +x ~/.vnc/xstartup. Next, restart the VNC server with the vncserver command. The server should be started with an output like this:

OutputNew 'X' desktop is applications specified in /home/gqadir/.vnc/xstartupLog file is /home/gqadir/.vnc/

Starting the VNC Server on Boot

First what you want to do is open your crontab. Note that if you've never edited it before, you may be prompted to choose a text editor:

crontab -eno crontab for user - using an empty oneSelect an editor.  To change later, run 'select-editor'.1. /bin/ed2. /bin/nano  <---- easiest3. /usr/bin/vim.basic4. /usr/bin/vim.tinyChoose 1-4 [2]:

Add @reboot /usr/bin/vncserver :1 to the bottom of the file. Your crontab should look like this:

# Edit this file to introduce tasks to be run by cron.## Each task to run has to be defined through a single line# indicating with different fields when the task will be run# and what command to run for the task## To define the time you can provide concrete values for# minute (m), hour (h), day of month (dom), month (mon),# and day of week (dow) or use '*' in these fields (for 'any').## Notice that tasks will be started based on the cron's system# daemon's notion of time and timezones.## Output of the crontab jobs (including errors) is sent through# email to the user the crontab file belongs to (unless redirected).## For example, you can run a backup of all your user accounts# at 5 a.m every week with:# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/## For more information see the manual pages of crontab(5) and cron(8)## m h dom mon dow command@reboot /usr/bin/vncserver :1

Creating a VNC Service File

For this, to start out, first you need to create a new unit file called /etc/systemd/system/vncserver@.serviceusing your preferred text editor, and enter the following command:

sudo nano /etc/systemd/system/vncserver@.service

Next, copy and paste the following content into it. Be sure to change the value of user and the username in the value of PIDFile match your username.

/etc/systemd/system/vncserver@.service[Unit]Description=Start TightVNC server at[Service]Type=forkingUser=gqadirPAMName=loginPIDFile=/home/gqadir/.vnc/%H:%i.pidExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%iExecStop=/usr/bin/vncserver -kill :%i[Install]

Save the file and exit the text editor. Now reload the systemd services and start the VNC Server service with the sudo systemctl daemon-reloadcommand. And then, enable the unit file by running the following command:

sudo systemctl enable vncserver@1.service

Stop the current instance of the VNC server if it’s still running by using the vncserver –kill :1 command. Then, start it as you would start any other systemd service.

sudo systemctl start vncserver@1

You can verify that it started by running this command: sudo systemctl status vncserver@1. If it started correctly, the output should look like this:

vncserver@1.service - TightVNC server on Ubuntu 16.04Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)Active: active (running) since Mon 2016-04-25 03:21:34 EDT; 6s agoProcess: 2924 ExecStop=/usr/bin/vncserver -kill :%i (code=exited, status=0/SUCCESS)...systemd[1]: Starting TightVNC server on Ubuntu 16.04...systemd[2938]: pam_unix(login:session): session opened for user finid by (uid=0)systemd[2949]: pam_unix(login:session): session opened for user finid by (uid=0)systemd[1]: Started TightVNC server on Ubuntu 16.04.

Connecting to the VNC Server through SSH Tunnel

ssh -L 5901: -N -f -l gqadir

Enter your SSH login password. Afterwards, the command will create the tunnel between your localhost and the VNC server. The port 5901 on localhost will be forwarded to the VNC server on port 5901.

Now open your vnc viewer application, create a new connection, and type the VNC server address with the localhost IP and port 5901. Then, connect to the VNC server and you will be asked for the VNC server password. Type your password and click OK. Now you will get the XFCE desktop from your server.

Last, click the use default config button for the first time, and you will get the XFCE desktop with default configuration. Installation of the VNC server using TightVNC on Ubuntu 16.04 has been completed successfully.


Original Source

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.