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 a VNC server on amazon ec2 ubuntu Linux. It also tells 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 hostname is ec2-18-123-28-21.ap-northeast-1.compute.amazonaws.com.
    ssh -i "/Users/..../key.pem" [email protected]
  2. Run command sudo apt-get update in the terminal to update the ubuntu package.
    ubuntu@ip-179-99-189-89:~$ 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 installation.
    ubuntu@ip-179-99-189-89:~$ 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.
    ubuntu@ip-179-99-189-89:~$ sudo apt-get install vnc4server
  5. If you meet an error during the above installation process, you can read the article How To Fix E: Package ‘vnc4server’ Has No Installation Candidate.

2. Configure VNC Server.

  1. Run command vncserver in the amazon ec2 ubuntu Linux ssh window, it will prompt you to enter the 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.
    ubuntu@ip-179-99-189-89:~$ vncserver
    
    You will require a password to access your desktops.
    
    Password:
    Verify:
    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 the below command to stop the VNC server.
    vncserver -kill :1
  3. Now you can run command pwd to get the current directory path, and ls -al to list all the files ( include hidden folders and files ) in the current directory. You can see that there is a folder named .vnc.
    ubuntu@ip-......:~$ pwd
    /home/ubuntu
    
    ubuntu@ip-......:~$ 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.
    ubuntu@ip-......:~$ 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.
    #!/bin/sh
      
    # Uncomment the following two lines for normal desktop:
     unset SESSION_MANAGER
     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. Press the ESC key, and then press the key :wq! in sequence 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.
    ubuntu@ip-......:~$ 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 connecting the VNC client to the VNC server.

  1. Open a terminal and run the below SSH command to connect to the VNC server first. Please note the -L 5901:127.0.0.1:5901 argument, the port number must be 5901. The -i “/Users/……/key.pem” argument should be provided also, otherwise, it will throw the error Permission denied (publickey).
    ssh -i "/Users/....../key.pem" -L 5901:127.0.0.1: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.
    /Users/songzhao/Downloads/tvnjviewer-2.8.3-bin-gnugpl
    (base) songs-MacBook-Pro:tvnjviewer-2.8.3-bin-gnugpl songzhao$ tree ./
    ./
    ├── LICENSE.txt
    ├── LICENSE_trilead-ssh2.txt
    ├── README.txt
    ├── nossh
    │   └── tightvnc-jviewer.jar
    ├── tightvnc-jviewer.jar
    └── viewer-applet-example.html
  3. Open a terminal and go to the above folder then run the command java -jar tightvnc-jviewer.jar in the terminal. This will pop up the New TightVNC Connection dialog window. Input 127.0.0.1 in Remote Host, 5901 in Port number.
  4. Click the Connect button, it will connect to the VNC server, and popup the VNC Authentication dialog to let you input the password.
  5. Click the OK button, it will connect with the VNC server.

5. Mac Built-in VNC Client.

The macOS contains a built-in VNC client, now I will tell you how to use it.

  1. Click the Finder icon at the macOS bottom dock bar.
  2. Click Go —> Connect to Server… menu item at macOS top menu bar.
  3. Input vnc://127.0.0.1:5901 in the popup Connect to Server dialog, click Connect button, it will let you input the VNC server password, then you can use the remote desktop.

6. Bad Local Forwarding Specification Error.

  1. When you use the command ssh -i “/Users/……/key.pem” -L 5901:127.0.0.1:5901 [email protected] to create the security channel between VNC client and server, you may encounter error Bad Local Forwarding Specification.
  2. To fix this error, you just need to start a new terminal and run the ssh -i “/Users/……/key.pem” -L 5901:127.0.0.1:5901 [email protected] command again in the new terminal.

Leave a Comment

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

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