Monday, 9 September 2013

Raspberry Pi - remote connection from Windows. A simple(ish) guide

Remote desktop connection

From a Windows PC to a Raspberry Pi

The simple way

An introduction.
Hopefully, I have written this for an average Pi, or Linux, tinkerer who is comfortable with command line stuff, and networking. It's been the hardest project that I've had for a long time. Setting up a web server has been easy, setting up security - easy. Setting up remote webcam viewing? Easy. Setting up Minecraft? Easy. In this blog I'll be writing documents on all of those. In my sleep, compared to this one.

Remote desktop set-up? Rocket science. But, wait, here is a guide on how to do it. I've pulled together all the information I've found THAT WORKS and dismissed several pages of stuff that is either too hard to follow, or just doesn't work without knowledge of other things.

If you want the answer on how to do it in one sentence: Install xdm with xaccess permissions on the pi and then run an x-server on the pc. Easy, no.

Why is it so hard?
What the hell is X-11, and X-11 forwarding? And what is xdm, or tunneling, or ssh, or PuTTY?? Why isn't the Pi the server? Why doesn't it just work? Why do you run the commands on the server to get the client to work, and why is the Pi the client but you don't type the commands on it? And why doesn't installing an x-server just work?

Simple answer? Don't ask. Read this, and you will get it working. If I can do it, anyone who understands a command line can. And that's with my 15 years experience doing everything else with Linux, Unix, Solaris, and many other quite hard to do things.

Unless you have a very patient and friendly expert friend in sysadmin, and, you have a lot of hair to pull out, a lot of time to read website after website after website to try and figure it out for your own setup. And, then, you have enough time to try all the different instruction pages there are - all of which have vital bits missing - like HOW TO MAKE IT WORK!!!! Lots of tekky folk will try and make it sound simple, and show you on THEIR machines how simple it is.

Lastly, virtually every X server out there seems to require a detailed understanding of how X-11 works. Almost all the documentation is written in double Dutch to even the most technical user, and even for me, with too many years experience than I care to say, could not fathom it out.

Launching the Space Shuttle is ALSO simple. When you know how.

I know I'm writing these instruction sheets pretty much backwards, but there's a simple reason. So far, I've been able to set up everything I've needed on my Pi without much extra help, so I just went ahead and did it. But this, remote desktop connections, has been a REAL PAIN for me to work out. And I class myself pretty technical, so I thought I'd write this in case it helps someone else.

Ok, cut the crap, show me how to do it.

You'll need:
  1. A Raspberry Pi installed with Raspian Wheezy (Debian)
  2. SSH server running on the Pi
  3. PuTTY installed on your Windows PC
  4. A network connection from your PC to your Pi. Wifi or cable, doesn't matter so long as it works
Steps involved.
  1. Configure PuTTY on your PC for X-11 forwarding
  2. Configure your Pi for remote desktop access (xdm)
  3. Choose and set up an X-11 server on your PC
  4. Configure your X-11 server to connect to your Pi. THIS IS THE HARDEST PART TO FIGURE OUT
  5. View your Pi desktop on your PC

Step 1. Configure Putty for X-11 forwarding

This is necessary for the two computers to be able to connect to each other over an SSH connection and display remote desktops using X-11 and xdm. You what? Don't worry, just follow this. You need it. SSH standards for Secure Shell. It means your access is encrypted. Nobody but the NSA or GCHQ can read what's being done.

In your PuTTY settings on your PC, make sure that under Connection, SSH, X11, that X11 forwarding is ticked. Save the settings for your Pi connection. I'll write a separate page for this soon.

Edit: On testing, it doesn't seem like SSH is needed, or even enabled. So things aren't encrypted  I'll have to work on this one and try and figure it out for you.

Step 2. Configure the Pi for remote desktop access (X11/xdm)

Use PuTTY to open an SSH connection from your Windows PC to your Pi. 

Become SuperUser (root): 
$ su

Now we'll install the xdm package(s) which we need for X-11 and xdm remote desktop connections.
# apt-get install xdm
When the box comes up asking you if you want lightdm or xdm, choose xdm.

Go to the /etc/X11/xdm directory to edit 2 config files
# cd /etc/X11/xdm

Using vi, or your favourite text editor, edit the file xdm-config
# vi xdm-config

Comment out the line DisplayManager.requestPort:     0
!DisplayManager.requestPort:     0

Save and exit.

# vi Xaccess

Uncomment the line: #*                                      #any host can get a login window
*                                       #any host can get a login window

Save and exit

NB: network security isn't covered here, for now we're just allowing anyone to access the desktop, provided they have the login and password details. Your router should be stopping anyone from the internet accessing it, and your Windows firewall will probably block the first attempt from your PC anyway, you'll need to allow it when the time comes. 

Start the xdm service and install some extra apps.
# service xdm start
# apt-get install x11-apps

Exit su (logout from root) and add a file in your home directory to enable colour desktops.
# <ctrl-d>
$ cd
$ vi .Xdefaults (note the dot before the filename, this is important)

Save and exit vi.

Repeat this for the root login by editing /.Xdefaults and adding the same line. You'll need su again to do this. Exit su once you've done that.

Let's check if XDMCP is listening - that the Pi is ready to accept your connection.

$ sudo netstat -ulnp | grep 177

You should see:
udp        0      0   *                          3772/xdm

The above bits in bold and underline are the important ones. The other information will vary on your setup. So long as you see udp, 177, and xdm, you're good.

Step 3 Choose and install an X-11 server on your PC

After MANY MANY failed attempts, I finally got something working and (sort of) easy to configure,

I chose the Xming X-11 server in the end. Download it and install it on your PC. DO NOT expect it to work straight away, there's a few things we still need to do. Here comes the hard part - at least it was for me, anyway. Ignore web pages that just suggest you run Xlaunch, or something else simple. They don't work.

Step 4. Configure your X-11 server on your PC

This is the hardest bit to understand and to get working. Why is it SO damn hard? First, the "server" is your PC, not your Pi. Pardon? Yes, the Pi is the CLIENT. Why? Don't ask me! Also, the X-11 needs a "tunnel" to connect, this is where we use PuTTY. Again, don't ask me! Just follow the steps and it should work. It's fiendishly complicated, and has taken me a LOT of hair pulling, which is why I've written this guide.

You installed Ximg, which is your X-11 server. Now let's get it working.

When you installed Ximg, it probably didn't update the system $PATH settinngs to enable to to ACTUALLY run the damn thing from the command line, which you NEED. On your PC do Start and "My Computer", right-click Properties, click "Advanced system settings", click "Environment variables", find PATH, and edit. Add where Ximg is installed. On my computer the PATH should have c:\Program Files <x86>\Xming. There should be a ";" before it after the other PATHs. E.g.

C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;c:\Android\platform-tools;c:\Program Files <x86>\Xming

We're nearly there! Ignore any documentation on "just" run Xming, Xming launch, or anything like that. It doesn't work.

You need to know the ip address of your Pi. If you forgot, ssh to it, login, and run:
$ ifconfig -a

And look for: inet addr: for the interface you're using - probably eth0. In my case, the IP address of the Pi is On yours it will be different, but it will probably be 192.168.something.something.

Ready? Let's go.

On your PC, press Start and type "cmd" without the quotes. You'll get a DOS command line something along the lines of "C:\".

Enter this command (with the IP address of your Pi)
Xming :1 -query (that being the address of your Pi, as above that's the IP address of my Pi, yours probably isn't

If it worked, within a few seconds (give it time) you'll get a login prompt for your Pi. The password won't show what you're typing, but type it anyway. Give it another few seconds and you'll have

A remote desktop on your Pi from your Windows PC

If windows says the command Xming isn't recognised, then you need to research your PATH settings to make sure they include the directory where Xming is installed. If that doesn't work, it's a Windows problem and I can't help you there.

Windows command error?
Click: Start
In "Search programs and files": Enter cmd and press Enter. DOS command line comes up,
C:\(whatever)>cd  "c:\Program Files <x86>\Xming"
Then c:\Program Files <x86>Xming>Xming :1 -query

(with 192,168,1,74 being the IP address of my Pi, not yours)