2015-10-28

VNC over SSH to Linux

DevOps is one of my titles. I control a lot of computers from my iMac. The computers are often local VMs, Hyper-V,  Azure, or Google Cloud VMs. The normal way to access Windows computers is via a graphical user interface by using the Microsoft Remote Desktop app. The normal way to access Linux computers is a text-based user interface via SSH in the Terminal app. Doing the opposite can be a bit challenging. I like that Windows is building in support for OpenSSH, so using a text-based UI to access Windows computers will be easier. Remotely accessing graphical UIs of Linux computers is possible by using VNC. Screen Sharing in a VNC client that comes preinstalled on Mac OS X and that is what I use as my VNC client.


The way I connect to the remote machine is by securely tunneling the port for the VNC session through my SSH connection. The default port that the client tries to connect to is TCP 5900. The VNC server may host many displays. Display :0 is generally for a physical display and made available on port 5900. The virtual displays :1, :2, :3, etc are on ports 5901, 5902, 5903, etc. Documented here, we will only have one virtual display made available on port 5901 on the server. We tunnel it through SSH to local port 5900 like so:

ssh alinuxvm -L 5900:localhost:5901

where `alinuxvm` is the IP address or host name for the server. I can then connect to the computer using the Screen Sharing app:


SSH gets you access to the computer. The VNC session on the computer is protected by a password that has a max of 8 characters. :/ The nice thing about the Screen Sharing app is that it remembers the password in my keychain.



When I connect, I'm able to access my 1900x1200 display of the Xfce Desktop Environment. I'll explain how to adjust the resolution and set this up below.


I've installed VNC server on both Ubuntu 14.04 desktop and server. There are two primary tutorials I used:

  1. How to Install and Configure VNC on Ubuntu 14.04
    https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-14-04
  2. How to install VNC Server on Ubuntu 14.04
    https://www.namhuy.net/3106/install-vnc-server-ubuntu-14-04.html
I tried both TightVNC and vnc4server and I had better luck with TightVNC. I recommend it. Follow the instructions listed in #1. Step three has you create a /etc/init.d/vncserver file. The only thing I did was modify the geometry and user as documented.



After adjusting the geometry, just restart the vncserver with:

sudo service vncserver restart

You don't need to close the Screen Sharing app before doing so. The only other step I stumbled on was not being able to see what I typed in the Xfce Terminal app. I resolved this by deselecting Use colors from system theme:


Enjoy remote accessing the Linux graphical user interfaces!