How to SSH tunnel a VNC connection and launch a Gnome desktop

For remote graphical access to a linux server I generally prefer to use NX aka nomachine, however I’ve been doing admin work on some shared servers that I don’t want to install NX on for a few different reasons.  All of the other admins here use VNC with SSH tunneling to access these boxen so I figured I would toe the party line and use VNC as well.  Here’s a quick guide as to what I did in order to get VNC with SSH tunneling working, complete with accessing a gnome desktop.

I am using Putty for SSH on Windows, and a TightVNC client to access a CentOS 6.x box that is running a Gnome desktop.

Putty Setup

In the putty session for the host you are connecting to, go to SSH –> Tunnels and set the port that you are going to forward.  VNC ports start @ 5900 by default and vary depending on the session number you choose.  In my example we’re going to use session number 66, so we’re going to forward port 5966 to localhost:5966 and click add, then save the configuration.

vnc5966

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

VNC Server Configuration

In this section we’re going to configure the VNC server, define ~/.vnc/xstartup to launch Gnome when your VNC session is started and actually launch a VNC session to connect to.

SSH to the vncserver using the session with the tunnel defined from the last step.  Once you’re there create ~/.vnc/xstartup if it is not already defined and replace the contents with the following:

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
/usr/bin/gnome-session & gnome-terminal &

Mainly what we’re looking for here is the last line, which tells X to launch a gnome session when a new VNC session is created.  In this example I also have it launching gnome-terminal, so you can see how you could launch multiple apps every time a session is initiated.  You could add whatever apps you want to this, such as firefox or eclipse.  When you’re done editing the file, make sure it is executable by doing a chmod +x.

Finally, we’re going to launch a vnc session  and then connect to it with tightvnc.  If this is your first time launching a VNC session then it will ask you to define a password to secure your current and future sessions.  Here, :66 defines the session number.

$vncserver :66

It is important that your session number match the port that is forwarded… vncserver :10 would launch a session on port 5910, vncserver :22 would launch a session on port 5922.  Whatever session number/corresponding port is launched has to be defined as a tunnel in putty.

Now that the session is launched, we just open TightVNC(or your favorite VNC client) and connect it to localhost:66 (session number), enter the VNC password that you defined and voila!

tvnc

 

 

 

 

That’s it!  we now have our connection to our Linux server with Gnome launched!

vncdone

 

 

 

 

 

 

 

 

-bb