Guide to…Remote Connections

This post is a general overview of the many options which I’ve come across and tried, which ones you choose and find useful will depend greatly on your needs and how much setting up you are comfortable doing.

In most cases, I will link to other guides and tutorials as these things have been covered in many places (so there is little point re-writing things for the sake of it).  Often it is just knowing what solutions exist and probably more importantly getting past the vast range of odd and strange terms which are commonly used by Linux gurus, to find some useful gems.

The intention is to add to and improve the information in this guide, so feel free to contact me and provide feedback and suggestions for improvements/additions.

Remote Command Line

Often I need to use my Raspberry Pi without connecting it to a separate screen and keyboard, perhaps to run a script or try out a little python program.  This is quite easy using “Putty” in windows, which uses SSH to provide access to the Raspberry Pi’s terminal.  I’ve even connected and used my phone for some things, by using ConnectBot for Android.

SSH (Secure SHell) is simply the name of the network protocol used to transfer this data back and forth, it can work through your home network, or even using a “Serial” link.

Regardless of which connection you use (just select the correct IP or COM port), Putty or a similar program can be used to connect to the Raspberry Pi and run most programs which usually run within the terminal (such as nano, or bash scripts or python etc).  It can be particularly useful if you create yourself lots of scripts to do common tasks, as you can use the command line to quickly connect, kick them off and check back on later to see their progress.

Using SSH-Terminal to connect and run commands remotely.

Using SSH-Terminal to connect and run commands remotely.

To switch on (or off) SSH you can access raspi config (just type “sudo raspi-config” from the terminal) and select SSH in the menu (it seems like SSH is already enabled by default for some distros).

Enable/Disable SSH via Raspi-config

Enable/Disable SSH via Raspi-config

You then enter your Raspberry Pi’s IP address (if connecting via the network), which you can find out by typing “hostname -I” (although depending on your network setup this may change each time you boot up – unless you set it to a fixed address (see section on IP addresses at the bottom)).

Putty Configuration for IP 192.168.1.69

Putty Configuration for given IP

To use SSH through a serial (RS232) link, there is an excellent USB lead which is available (see links below) which allows you to connect to the GPIO header pins directly and use the Raspberry Pi’s built in serial pins.  The “console” lead is similar to typical USB-to-serial cables, except that the data voltages are at safe level for connecting directly to the Raspberry Pi (otherwise, additional circuits would be required to “shift” between voltages).

Tip: Don’t forget, after connecting via Putty, you have to press “enter” in the window before it starts the communication (this has caught me out and made me think something wasn’t working).

Setting up and using SSH on a network

Adafruit’s Raspberry Pi Lesson 6. Using SSH

http://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh

Two guides on using a TTL Serial-USB cable

Adafruit’s Raspberry Pi Lesson 5: Using a Console Cable

http://learn.adafruit.com/adafruits-raspberry-pi-lesson-5-using-a-console-cable

Raspi-TV: How to Run Raspberry Pi with No Monitor or Network

http://raspi.tv/2012/how-to-run-raspberry-pi-with-no-monitor-or-network

Buy Raspberry Pi Console TTL Serial-USB cable

Adafruit – https://www.adafruit.com/products/954

ModMyPi - https://www.modmypi.com/USB-to-TTL-Serial-Cable-Debug-Console-Cable-for-Raspberry-Pi?

Remote Desktop

However, I often needed to use programs like Scribus (for working on The MagPi articles), or Python programs which are graphical and require the desktop to run.  To do this, I’ve used VNC (a remote access program which allows you to control a desktop session from a window on your machine).  Again for mobile access I use an Android program Free bVNC.

Note, this is a little bit different to how VNC shares your desktops when using windows machines, where the current session is shared and mirrored on your local screen.  The VNC session here is separate to the one which may be active on the Raspberry Pi’s display itself.

Raspberry Pi Desktop session running using VNC

Raspberry Pi Desktop session running using VNC

Three guides on setting up VNC for remote desktop:

Colin Deady’s (fellow MagPi) guide on VNC

http://www.rasptut.co.uk/files/raspberry-pi-vnc.php

Adafruit’s Raspberry Pi Lesson 7: Remote Control with VNC

http://learn.adafruit.com/adafruit-raspberry-pi-lesson-7-remote-control-with-vnc

Raspi-TV: Install and use TightVNC remote desktop on raspberry pi – through windows, Android or IOS

http://raspi.tv/2012/install-and-use-tightvnc-remote-desktop-on-raspberry-pi-through-windows-android-or-ios

Dual Control

Alternatively, if I do have screen available (perhaps a second screen for instance next to the PC), you can use a program called synergy.  Synergy runs a service on the Raspberry Pi and the controlling computer which allows you to transfer your mouse and keyboard commands seamlessly between networked computers just by moving the mouse over to the screen.  It is something I use all the time when using a 2nd computer, and saves having multiple keyboards and mice and remembering which is which.

I’ve had mixed results with using this on the Raspberry Pi, such as trying to get the service to start automatically for desktop sessions and a few issues with character mapping (making writing code particularly difficult as some characters where hard to produce).  It may simply take a little more tinkering to get these issues sorted out.

Server set-up of Synergy running on RPi

Server set-up of Synergy running on RPi

Synergy

Setting up Synergy on the Raspberry Pi by Rootusers.com (medium difficulty):

http://www.rootusers.com/compiling-synergy-from-source-on-the-raspberry-pi/

Accessing Files Remotely

Probably one of the most useful things you can do (at least I find it invaluable) is to share your Raspberry Pi’s home directory as a shared network folder.  In usual cryptic Linux, this is using something called Samba (see Wikipedia for it’s history), which allows folders to be shared on the network to Windows machines.  You can then map the folder to a drive on your computer and access it whenever the Raspberry Pi is switched on and connected to the network.

This is particularly useful for backing up your files and programs you are working on (I even use a program called AllwaySync which ensures any files I change are synced with my backup copy).  However, since you can access all your files, you can also use your computer (and favourite editors) to edit files such as Python scripts directly (making it easier to browse and cut and paste at speed).

Guide to setting up shared folders:

Setting up Samba on the Raspberry Pi from the RPi Wiki Pages (user Bredman):

http://elinux.org/R-Pi_NAS#Configure_a_Samba_server_on_Debian_RPi

The best is last – X-11 Forwarding

It is something which one of the fellow MagPi’s Colin Deady briefly showed me, which for some reason isn’t mentioned very often.  It is the ability of SSH to transfer X-11 data to an X-Windows server running on your machine…

Now to most people who are new to Linux (including me), it all sounded rather meaningless without a little more investigation…so what is X-Windows and X-11, and why is this a good thing?

Basically, X-Windows and X-11 (11 being the version), is what provides the method by which the Raspberry Pi (and many other Linux based computers) can display and control graphical windows as part of a desktop.  More info on Wikipedia.

What this means to us, is that the X-11 data can be sent to a server program running on another computer, or laptop and return any control commands back to the Raspberry Pi.  Not only does this allow you to host a desktop session but you can also open individual X-windows programs within your normal desktop environment, as if it was running directly.

For instance, when you type “leafpad” in the remote SSH terminal window, you will probably get an error “leafpad: Cannot open display”.

This is because Leafpad is a graphical program (it runs in a window on the Raspberry Pi desktop) it can’t run in the terminal session which is purely text based.  However, with a X-Windows server running, and X-11 protocol configured, it will launch a window directly on the connected computer and act almost as if it was running natively!

Leafpad through X11-Forwarding

Leafpad through X11-Forwarding

Or perhaps, slightly more visual, programs like scratch!

Scratch running nicely inside a window!

Scratch running nicely inside a window!

To get X-11 working, there is only three things you need to do extra over normal SSH access:

1. Ensure X-11 forwarding is enabled on the Raspberry Pi (again, a lot of distros now have this enabled by default).

Use nano with the following command:

sudo nano /etc/ssh/sshd_config

Look for a line in the /etc/ssh/sshd_config file which controls X11Forwarding and ensure it says:

X11Forwarding yes

Save if required (ctrl+x,Y, enter), and reboot:

sudo reboot

2. Install and run a X-Windows server on your computer

Just download and run Xming.

More detail can be found in this simple guide on installing and using Xming by Arsc:

http://people.arsc.edu/~murakami/xming/

3. Ensure your SSH program (such as Putty) has X-11 enabled.

In the PuTTY configuration, find “Connection”, “SSH”, “X11″ and tick the check-box for “X11 forwarding”.  If you leave the X display location blank, it will assume the default “Server 0:0″ (you can confirm the server number by hovering your mouse over the Xming icon in the system tray).  You can save your set-up within Putty so you will won’t have to do this each time.

Putty Configuration for X11 Forwarding

Putty Configuration for X11 Forwarding

Additional X-11 Tips:

1. If you want to run an X program, but still be able to use the same terminal console for other stuff, you can run the command in the background with &:

i.e. leafpad &

Just remember that the more programs you run, the slower everything will get.  You can switch to the background program by typing “fg”, check for background jobs with “bg”.

2. You can even run a full desktop session through X-11, although it isn’t particularly user-friendly and VNC will produce better results.  To do this you have to use “lxsession” instead of “startx”.

3. If you get the following error (or similar) when running PyGame or Tkinter scripts:

_tkinter.TclError: couldn't connect to display "localhost:10.0"

Use the fix below:

sudo cp ~/.Xauthority ~root/

4. Scratch through X11 Forwarding
This can throw up errors when run using Linux or Mac OSX to view the X11 Windows (see comments on the post).

You’ll see errors including:

Executing: /usr/lib/squeak/4.4.7-2357/squeakvm -encoding UTF-8 -vm-display-x11 -xshm -plugins /usr/lib/scratch/plugins/:/usr/lib/squeak/4.4.7-2357/ -vm-sound-alsa /usr/share/scratch/Scratch.image
 X Error: BadShmSeg (invalid shared segment parameter)

The solution is to remove -xshm and then execute the command directly (if you use this a lot then you can put it into a scratchx11.sh file to save time):

/usr/lib/squeak/4.4.7-2357/squeakvm -encoding UTF-8 -vm-display-x11 -plugins /usr/lib/scratch/plugins/:/usr/lib/squeak/4.4.7-2357/ -vm-sound-alsa /usr/share/scratch/Scratch.image

Alternatively, running lxsession or lxpanel provide the option to start the program through the desktop start menu, and this doesn’t have this problem.

A fix for scratch I believe is already in place, so should make it into the Raspberry Pi files at some stage.

SSH X-Forwarding On Windows by Jeremy-Nicola.info

http://jeremy-nicola.info/portfolio-item/ssh-x-forwarding-on-windows/

A small note on IP Addresses

Many of the above solutions rely you knowing the Raspberry Pi’s IP address.  Depending on your situation, you can fix the IP address to be the same every time you start up by configuring a fixed IP address directly on the Raspberry Pi.

However, this can cause problems if you switch to other networks (as a different address may be required), or if you switch distros/cards, so I prefer to get my router to provide the same IP address (this will be configured using your router settings, which will be different for every model and make, look for DHCP settings).

Comments
  1. mike632t says:

    Since I first got my Pi I found having to use a seperate keyboard and mouse a bit of a pain so now my Pi is more or less permanantly attached to my laptop via a USB serial cable (for the console) and network cable so I can conenct remotly to the desktop. (see http://mike632t.wordpress.com/2013/02/10/configure-serial-console-pi/ and http://mike632t.wordpress.com/2013/04/18/remote-desktop-vnc/ )

  2. [...] discussed previously (in the Guide to…Remote Connections), there is the option of using a (console) TTL-serial cable, however this only provides rather slow [...]

  3. Sebastien says:

    Hello Meltwater.

    Thank for your mine of info. I have just found this article through the Raspberry website. It took me several hours to find all this by myself. I have put my experience on my blog http://t.co/AV4t5fOw3N where I talk about SSH, and RDC. Especially you left out that once you have SSH, you can easily browse and modify files on your Raspberry Pi very easily using WinSCP for example.

    As for the IP address problem, I think a good way would be to have a small screen or just some LEDs to give you visual feedback. A shutdown button would be usefull as well…

    • Yep, most of the info was out there, but as you say it can take a while to hunt it all down and follow all the dead-ends.

      The network solution is great since if you set-up other things like shared folders (SMB) you can drag and drop files, and backup everything.

      The IP address problem can still be a pain, but being able to set it to a fixed IP via the SDCard (when you need to) helps a lot. As mentioned before, the PI “should” be able to automatically set an IP for direct connections too, and if the “hostname” lookup worked every-time, the IP address set becomes less of a problem.

      We are getting closer to a perfect, easy solution, but we are not there yet (hopefully a few tweaks to the distro and we will be).

      Yes, a safe-shutdown on the RPi, would also help a lot for running headless.

  4. Andy says:

    Interesting to see how you guys have gone about things … and the X11 forwarding looks intriguing … will try it out later.
    However, I notice that you talk about VNC and have a separate X session. That is fine, but I found that annoying.
    I had set Rhythymbox to start automatically to act as my music server. Using tightVNC meant that I would not ordinarily
    be able to control Rhythymbox via tightVNC.
    A bit of searching around and I found x11vnc which was very easy to set up and connects to an existing X session.
    If you would like some notes, let me know.

    • Yes that would be very useful.
      I wasn’t keen on VNC starting each time (if it was needed or not), so I added that to a start up menu, so I could enable it via SSH if required.

      Would be interested to hear more about the x11vnc solution, could be helpful. So far, x11 works great (particularly now I got it working with Tkinter etc) when coupled with shared folders, means you can code directly on PC.

      • Andy says:

        sudo apt-get install x11vnc
        x11vnc -setpasswd
        x11vnc -forever -usepw -display :0

        whcih seems pretty easy.

        I have set x11vnc to start auto-matically on start up.
        Let me know what you reckon.

  5. briti says:

    amazing thank you took a bit of time but was worth it :D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s