How To Install VNC Server On Amazon EC2 Ubuntu Linux Server And Connect It With VNC Client

This article will tell you how to install vnc server on amazon ec2 ubuntu linux. It also tell you how to connect to the vnc server from your local machine use tightvnc client.

1. Install VNC Server On Amazon EC2 Ubuntu Linux Steps.

  1. Open a terminal and connect to amazon ec2 ubuntu linux server use ssh command like below. Please note the user name is ubuntu, the host name is
    ssh -i "/Users/..../key.pem" [email protected]
  2. Run command sudo apt-get update in the terminal to update the ubuntu package.
    [email protected]:~$ sudo apt-get update
  3. Run command sudo apt-get install lxde -y in terminal to install a ubuntu desktop software lxde. This process may take some time because of package download and install.
    [email protected]:~$ sudo apt-get install lxde -y
  4. Run command sudo apt-get install vnc4server to install a vnc server on the ubuntu server, we choose vnc4server.
    [email protected]:~$ sudo apt-get install vnc4server

2. Configure VNC Server.

  1. Run command vncserver in the amazon ec2 ubuntu linux ssh window, it will prompt you to enter password for the vncserver, please remember this password, you will use it to connect to this vnc server from vnc client later. The vncserver user name is ubuntu.
    [email protected]:~$ vncserver
    You will require a password to access your desktops.
    xauth:  file /home/ubuntu/.Xauthority does not exist
    New 'ip-172-31-27-1:1 (ubuntu)' desktop is ip-172-31-27-1:1
    Creating default startup script /home/ubuntu/.vnc/xstartup
    Starting applications specified in /home/ubuntu/.vnc/xstartup
    Log file is /home/ubuntu/.vnc/ip-172-31-27-1:1.log
  2. Run below command to stop the vnc server.
    vncserver -kill :1
  3. Now you can run command pwd to get current directory path, and ls -al to list all the files ( include hidden folders and files ) in current directory. You can see that there is a folder named .vnc.
    [email protected]:~$ pwd
    [email protected]:~$ ls -al
    drwxr-xr-x 2 ubuntu ubuntu 4096 Feb 14 04:21 .vnc
  4. Run vim .vnc/xstartup command to edit the vnc server startup file.
    [email protected]:~$ vim .vnc/xstartup
  5. Uncomment below line ( line 4 and line 5 ) in .vnc/xstartup file. And change line 5 to exec sh /etc/X11/xinit/xinitrc.
    # Uncomment the following two lines for normal desktop:
     exec sh /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 &
  6. Presee ESC key, and press :wq! to save above file changes and exit vim. Run command cat .vnc/xstartup to verify the changes.
  7. Run command vncserver in terminal to start it again. But each time you reboot the linux server, you should start the vnc server manually.
    [email protected]:~$ vncserver
    New 'ip-172-31-27-1:1 (ubuntu)' desktop is ip-172-31-27-1:1
    Starting applications specified in /home/ubuntu/.vnc/xstartup
    Log file is /home/ubuntu/.vnc/ip-172-31-27-1:1.log

3. Run Below SSH Command To Redirect Local Port Request To VNC Server.

This step should be processed first before connect vnc client to vnc server.

  1. Open terminal and run bellow ssh command to connect to the vnc server first. Please note the -L 5901: argument, the port number must be 5901. The -i “/Users/……/key.pem” argument should be provided also, otherwise it will throw error Permission denied (publickey).
    ssh -i "/Users/....../key.pem" -L 5901: [email protected]

4. Install VNC Client And Connect To Amazon EC2 VNC Server.

  1. Download the tight vnc client java version. Because the java version is cross platform, so i use it.
  2. Unzip the download zip file to a local folder. Below is the folder content.
    tightvnc viewer folder content
  3. Open a terminal and goto above folder then run command java -jar tightvnc-jviewer.jar in the terminal. This will popup a dialog widow like below. Input in Remote Host, 5901 in Port number.
    tightvnc client connect window
  4. Click Connect button, it will connect to the vnc server, and popup a dialog to let you input password.
    input vnc server auth password
  5. Click OK button, it will connect with the vnc server.

5. Mac Built-in VNC Client.

Mac os contains a built-in VNC client, now i will tell you how to use it.

  1. Click Finder icon at bottom dock bar.
    mac finder command icon new
  2. Click Go —> Connect to Server menu item at top menu bar.
    mac os finder - go - connect to server menu item
  3. Input vnc:// in the popup dialog, click Connect button, it will let you input the vnc server password, then you can use the remote desktop.
    mac os finder connect to server vnc address input text box

6. Bad Local Forwarding Specification Error.

When you use command ssh -i “/Users/……/key.pem” -L 5901: [email protected] create the security channel between vnc client and server, you may encounter error Bad Local Forwarding Specification.

To fix this error, you just need to start a new terminal and run the ssh -i “/Users/……/key.pem” -L 5901: [email protected] command again in the new terminal.

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x