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.
- 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]
- Run command sudo apt-get update in the terminal to update the ubuntu package.
ubuntu@ip-179-99-189-89:~$ sudo apt-get update
- 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
- 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
- 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.
- 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
- Run the below command to stop the VNC server.
vncserver -kill :1
- 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
- Run vim .vnc/xstartup command to edit the VNC server startup file.
ubuntu@ip-......:~$ vim .vnc/xstartup
- 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 &
- 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.
- 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.
- 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.
- Download the tight VNC client java version. Because the java version is cross-platform, so I use it.
- 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
- 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.
- Click the Connect button, it will connect to the VNC server, and popup the VNC Authentication dialog to let you input the password.
- 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.
- Click the Finder icon at the macOS bottom dock bar.
- Click Go —> Connect to Server… menu item at macOS top menu bar.
- 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.
- 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.
- 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.