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.
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).
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)).
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
Two guides on using a TTL Serial-USB cable
Adafruit’s Raspberry Pi Lesson 5: Using a Console Cable
Raspi-TV: 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
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.
Three guides on setting up VNC for remote desktop:
Colin Deady’s (fellow MagPi) guide on VNC
Adafruit’s Raspberry Pi Lesson 7: Remote Control with VNC
Raspi-TV: Install and use TightVNC remote desktop on raspberry pi – through windows, Android or IOS
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.
Setting up Synergy on the Raspberry Pi by Rootusers.com (medium difficulty):
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):
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!
Or perhaps, slightly more visual, programs like scratch!
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:
Save if required (ctrl+x,Y, enter), and 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:
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.
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
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).