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.
- For this tutorial, you’ll need to have an Alibaba Cloud Elastic Compute Service (ECS) instance. If you are new to ECS, you can get Free Credits in your Alibaba Cloud account. If you don’t know about how to setup your ECS instance, you can refer to this tutorial or check quick-start guide.
- You have to setup your server’s hostname.
- You have access to the VNC console using Alibaba Cloud account, or the SSH client installed in your PC.
Follow the steps outlined in the following sections to set up VNC on an Alibaba Cloud ECS instance.
Logging to Your Console
Log on as root user to Alibaba Cloud with your root username and password and with the SSH client (which you did through Putty or the VNC console available in your Alibaba Cloud account dashboard.
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
Before we download and install VNC, we need to make sure our ECS instance has all the dependencies installed.
First, update the package manager cache by running the following commands:
# sudo apt-get update# sudo apt-get upgrade
Creating a New User
VNC Desktop will be available for a non-root user. So, we need to create this 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
By default, an Ubuntu 16.04 Installation on an ECS instance does not come with a graphical desktop environment or a VNC server installed, so you’ll need to begin by installing one of those. Specifically, you will install packages for the latest Xfce desktop environment and the TightVNC package available in the official Ubuntu repository.
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
Now we will install VNC Server using the
sudo apt-get install vnc4server command. To complete the VNC server's initial configuration after installation, also use the
vncserver command to set up a secure password.
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.
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
We need to configure VNC to launch the full Unity desktop when it starts. To do it, first kill the VNC session by running the
vncserver –kill :1 command. The output should look like the following (with a different PID though).
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 your_server_name.com:1Starting applications specified in /home/gqadir/.vnc/xstartupLog file is /home/gqadir/.vnc/liniverse.com:1.log
Starting the VNC Server on Boot
These following steps are optional. You can complete these steps to configure the VNC server to start automatically after reboot.
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 :
@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
It is necessary that you create a new service file if you want to use the the VNC Server as a service.
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 startupAfter=syslog.target network.target[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]WantedBy=multi-user.target
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 email@example.com
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:
firstname.lastname@example.org - 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: Starting TightVNC server on Ubuntu 16.04...systemd: pam_unix(login:session): session opened for user finid by (uid=0)systemd: pam_unix(login:session): session opened for user finid by (uid=0)systemd: Started TightVNC server on Ubuntu 16.04.
Connecting to the VNC Server through SSH Tunnel
In part of the tutorial, we will connect to our VNC server through an ssh tunnel. To do this, first, open your terminal and type the ssh command as below.
ssh -L 5901:127.0.0.1:5901 -N -f -l gqadir 192.168.32.50
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
192.168.35.50 on port 5901.
Now open your
vnc viewer application, create a new connection, and type the VNC server address with the 127.0.0.1 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.
This tutorial covered the essentials for getting started with VNC running on an Alibaba Cloud ECS instance installed with Ubuntu 16.04. You should now have a secured VNC server up and running on your ECS instance. Now you’ll be able to manage your files, software, and settings with an easy-to-use and familiar graphical interface in an easy and intuitive way.