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

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

This tutorial shows how to set up VNC on an Alibaba Cloud ECS instance installed with Ubuntu 16.04 and how to connect to it securely with SSH tunneling.

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. Choosing 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.

Prerequisites

  1. 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.
  2. You have to setup your server's hostname.
  3. You have access to the VNC console using Alibaba Cloud account, or the SSH client installed in your PC.

Procedure

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 update

sudo 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.

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

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).

Output

Killing Xtightvnc process ID 19443

It's 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/.Xresources

xsetroot -solid grey

vncconfig -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:

Output

New 'X' desktop is your_server_name.com:1

Starting applications specified in /home/gqadir/.vnc/xstartup

Log 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 -e

no crontab for user - using an empty one

Select an editor.  To change later, run 'select-editor'.

1. /bin/ed

2. /bin/nano  <---- easiest

3. /usr/bin/vim.basic

4. /usr/bin/vim.tiny

Choose 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

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@.service using 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 startup

After=syslog.target network.target

[Service]

Type=forking

User=gqadir

PAMName=login

PIDFile=/home/gqadir/.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 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.04

Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)

Active: active (running) since Mon 2016-04-25 03:21:34 EDT; 6s ago

Process: 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

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.

Summary

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.

2 0 0
Share on

Alibaba Clouder

2,599 posts | 762 followers

You may also like

Comments

5596272229645083 October 28, 2019 at 4:17 am

too many typo in this article. If you copy from other resources, please make sure you test it first

5973478487134912 January 9, 2020 at 7:55 pm

errors in package names too

5973478487134912 January 9, 2020 at 7:55 pm

sudo apt install xfce4 sfce4-goodies tightvncserverwrong package name sfce4-goodies --> xfce4-goodies