18May/130

Remote VNC Access

VNC Access to Raspberry Pi

 

There's nothing quite like being 500 miles from home and having the ability to control your home computer with your cell phone. In the past, I have used PocketCloud in conjunction with the built in RDP server to access my Windows 7 computer.  It was pretty handy for managing my media library from work or my laptop.

I ran into a problem, though, once I completely moved my home computers to Linux: the best RDP server solution for Linux (xRDP) just didn't cut it. It was nowhere near as seamless as the built in utilities for Windows, and I don't like fiddling past initial setup.

I decided my Raspberry Pi would make a great remote access point. No sensitive information, very low power draw for 24/7 uptime, and I can tuck it in next to my router so I never have to see it.

Join me after the break for a quick and easy tutorial for enabling remote access to your own Linux machine! We'll be using TightVNC Server for Linux, a Raspberry Pi running Raspbian, and your choice of a VNC client.

Setting up VNC access is pretty simple:

  • Install VNC server
  • Forward ports
  • ???
  • Profit!

For this tutorial, I'm going to assume you have a computer running a Debian Linux variant and a static IP setup. It's also helpful to have a DNS hostname pointed at your home network IP address. I recommend DynDNS.org for their free and easy to setup basic DNS services.

The installation is easy; I'd recommend either having SSH setup for headless devices, or using a display if you've got one handy. Log in to the computer you plan on using as your VNC server. Open a terminal, and enter this command:

sudo apt-get install tightvncserver

Once TightVNC finishes installing, go ahead and start up the daemon:

tightvncserver

It'll ask you to setup a password, go ahead and enter one twice. You'll be presented with an option to set a read only password. I'd ignore that, as read only access is fairly useless for our purposes. See how easy that was? Now you can access your computer via VNC! Go ahead - fire up a VNC client and connect to 192.168.1.5:1 (substitute your VNC server's IP address)

xtightvncviewer 192.168.1.5:1

xtightvncviewer 192.168.1.5:1

To install TightVNC Viewer on Linux, just open a terminal and type:

sudo apt-get install xtightvncviewer

Once it installs, you can test your VNC connection:

xtightvncviewer 192.168.1.5:1

Substitute your VNC server's IP address, and you should be prompted for a password! The :1 indicates you are connecting to virtual desktop "1".

Great, so you've got VNC access from another computer, but that doesn't help you get in from a computer outside your home network. You'll need to forward the network ports from your router to the computer.

I'll be using a Linksys router with DD-WRT installed, but you can look up instructions specific to your router. The important thing is to forward the correct port to the correct IP address.

For DD-WRT routers, login to 192.168.1.1, or whatever your router's IP address is. Navigate to "NAT/QoS" and choose the "Port Forwarding" tab. Add an entry as follows:

Screenshot from 2013-05-18 15:18:44

Let's break it down. The first field is a name for the connection to easily distinguish it. The second is the remote port. Choose "both" for TCP/UDP and substitute your VNC server's local IP address in the fourth field. Enter 5901 for the local port, and finally, you'll enable the Port Forwarding entry.

Forwarding port 5901 to your VNC server allow it to respond to any outside connections on port 5901, effectively giving you remote access.

Edit: FuelCell250 mentioned that it's possible to have bots try the standard port and attempt to brute force the password. If this concerns you, try setting the external port to another number. Make sure you're not using a reserved or other common port number, then adjust the settings in your remote client as required.

We have one more item to handle: Automatically starting VNCServer at boot.

From your terminal, enter this command to create the "init" file:

Screenshot from 2013-05-18 15:20:24

sudo nano /etc/init.d/tightvnc

Now, copy and paste the following into your new file:

#!/bin/sh
### BEGIN INIT INFO
# Provides: tightvncserver
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start vnc server
# Description:
### END INIT INFO

case “$1″ in
start)
su pi -c ‘vncserver :1 -geometry 1600×900 -depth 16 -pixelformat rgb565:’echo “VNC Started”
;;
stop)
pkill Xtightvnc
echo “VNC Terminated”
;;
*)
echo “Usage: /etc/init.d/tightvnc {start|stop}”
exit 1
;;
esac

Replace "pi" with the username for the account you wish to access remotely, and adjust the resolution to your preferred size. Now hit "CTRL+W" to write the file, then "CTRL+X" to exit nano.

Adjust permissions on the file and make it start automatically:

sudo chmod 755 /etc/init.d/tightvnc

sudo update-rc.d tightvnc defaults

Reboot your VNC server, and try connecting! You should now be able to automatically connect to your computer from any VNC client, whether on your home network or 500 miles away on a 3G connection. As I mentioned before, make sure to setup a hostname to point to your network; this will make it much easier to remotely login to your computer. When outside your home network, you'll need to enter your hostname or external IP address, connecting on port 5901.

Great thanks to the HowToGeek for the init script!

 

--dreadpirate15

Screenshot from 2013-05-18 15:06:53

Switch to our mobile site