Saturday, January 1, 2011

VNC Remote Desktop on RedHat / CentOS

VNC is the best thing since sliced bread. If you don't believe me, give it a try.

There are two ways to use VNC. The first way is to provide remote access to your normal desktop, the desktop you get when you sit down in-front of your computer and login. The second way, which we will be exploring, is to create a desktop that is separate from the physical display and only lives in memory.

We will use VNC much like one would use a shell account with screen. We will set up an instance of an interactive desktop that is not attached to a physical keyboard, monitor or mouse. This kind of configuration is perfect for headless servers, or for running GUI applications on a server that you never actually touch. This desktop will appear in a window on your client machine. When you disconnect from this desktop, the programs will remain running/persistent, and still be there the next time you reconnect.

On our remote server, let's install the VNC packages:
server# yum -y install vnc vnc-server

and start a vnc server as a normal user account on the server:
server$ vncserver :8 -geometry 1024x768 -depth 16

Now, we could connect directly to the vnc server by supplying the hostname and port to our viewer, but chances are the connection is traversing a public network. So let's set up an ssh encrypted tunnel first. On your client machine run this ssh command:
client$ ssh -L 5908:localhost:5908 myserver.net \
"echo connected to localhost:5908 ; sleep 30"

This ssh command will securely forward your local port 5908 to the remote port 5908, where vncserver is listening. With the encrypted tunnel set up, run the vnc viewer on your client in another terminal:
client$ vncviewer localhost:8

So now you should be looking at your remote desktop in a window, but it isn't pretty because it is running the bare-minimum system default for your window manager. Let's make it look like a real Gnome desktop by un-commenting a few lines in the ~/.vnc/xstartup file on the server:
server$ vim ~/.vnc/xstartup

# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc


Now stop and re-start the vnc server with the new session manager:
server$ vncserver -kill :8
server$ vncserver :8 -geometry 1024x768 -depth 16

When you re-connect with ssh & vncviewer, you will have the normal Gnome desktop. This desktop will persist until you "-kill" it. This means you can open and close your viewer as much as you like, from where ever you like, to peak in on your GUI applications. Happy VNC-ing!



No comments:

Post a Comment