Getting one of your Five-a-Day – connecting Remotely to a Raspberry Pi from Linux Mint

It’s Christmas. To mark the occasion, my son bought me a top-of-the-range computer…

pi_board

Christmas has come early ! Er, hang, on…

Yes, a Raspberry Pi 2 b-spec, complete with 900 MHz Quad-core ARM processor and 1 GB RAM.

Getting it up and running was a bit more of a challenge than I had anticipated.
The Pi uses HDMI for Video output and my ageing monitor is not equipped for HDMI…

tv

The best program on TV – NOOBS doing it’s thing.

In the end, I had to “borrow” the TV.
This arrangement was, of necessity, extremely temporary. The TV had to be back in it’s usual place ready for The Strictly-TOWIE-Dancing-Get-Me-Out-Of-Here Christmas Special, on pain of pain.
Therefore, my first Pi project was to connect to it remotely from another machine, namely, my Linux Mint Laptop.
This will enable me to run the Pi headless (i.e. without a monitor/keyboard/mouse attached to it).

I’m going to cover two different methods of connecting to the Pi.
The first is using ssh to connect to the command line.
The second is to connect remotely to the Raspbian desktop itself.

Just to avoid any confusion, I will be referring to the Raspberry Pi as “the Pi” and the machine I’m connecting from as “Mint”.

About the Environment

The Pi

The Pi I’m using for this is running the Raspbian Jessie OS.
It is set to start the desktop on boot.
The Pi is up and the desktop is running.
The Pi is connected to the router via a network cable.

The Mint Machine

The version of Mint I’m running on is 17.2.

The Network

I’m running on a standard home network with all devices connecting to a router.
The router assigns IP addresses to each connected machine dynamically via DHCP.
Apart from the router itself, no device on the network has a fixed IP address.

SSH – Setup

We need to perform these steps on the Pi.

First of all, we need to know what the machine name of the Pi is.

As the devices that connecting to the network are dynamically allocated an IP address it’s simpler to address a specific machine by name.

So, on the Pi, open a terminal and type :

uname -n

This returns the name of the computer. In my case :

raspberrypi

The next thing we need to do is to make sure that the Pi will accept connections via SSH.

On the Raspbian Desktop, click on the Menu and select Preferences/Raspberry Pi Configuration

prefs_menu

Let’s face it, this is the closest I’m going to get to fresh fruit and veg for the next couple of weeks.

Next click on the Interfaces tab and make sure that ssh is enabled.

config_window

Once the steps have been completed, we’re ready to test…

SSH – from the remote machine

We want to connect to the Pi from a remote machine as a user that exists on the Pi.
Note that this user does not need to exist on the remote machine.

When we run the ssh command, we need to specify the user we’re connecting as, and the name of the machine we’re connecting to (i.e. the Raspberry Pi itself).

I’m going to connect as the user pi. We’ve already found out that the name of the Raspberry Pi is “raspberrypi”.

So, I just need to open a terminal window on my remote machine and type :

ssh pi@raspberrypi

The first time you ssh to another computer on your network, you’ll get this warning :

The authenticity of host 'raspberrypi (192.168.1.144)' can't be established.
ECDSA key fingerprint is 03:72:d9:84:58:c8:a6:cc:37:bc:c3:47:8f:1c:90:e0.
Are you sure you want to continue connecting (yes/no)? 

Type “yes” and Hit Return…

Warning: Permanently added 'raspberrypi,192.168.1.144' (ECDSA) to the list of known hosts.
pi@raspberrypi's password:

Enter the password and…

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

To confirm that we’re now connected to the pi…

uname -a
Linux raspberrypi 4.1.13-v7+ #826 SMP PREEMPT Fri Nov 13 20:19:03 GMT 2015 armv7l GNU/Linux

Whilst ssh is handy if you just need command line access to the Pi, if you want access to the Desktop, you’ll need to try something a bit different.

Using RDP to run the Pi Desktop Remotely

Now, there is more than one way to do this. I’ve chosen to use RDP as it’s quite simple to setup.

Installing xrdp on the Pi

To start with, we need to install the xrdp package on the Pi. At this point, you can either do this on the machine itself ( by opening a Terminal window), or connect via ssh.
Either way, the command you need to enter is :

sudo apt-get install xrdp

You will be prompted for your password and should then get some output that looks like this :

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  vnc4server x11-apps x11-session-utils xbase-clients xbitmaps xfonts-base
Suggested packages:
  vnc-java mesa-utils x11-xfs-utils
The following NEW packages will be installed:
  vnc4server x11-apps x11-session-utils xbase-clients xbitmaps xfonts-base xrdp
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 8,468 kB of archives.
After this operation, 17.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main x11-apps armhf 7.7+4 [529 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main x11-session-utils armhf 7.7+1 [60.1 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main xbase-clients all 1:7.7+7 [36.7 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main vnc4server armhf 4.1.1+X4.3.0-37.6 [1,434 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main xbitmaps all 1.1.1-2 [32.1 kB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ jessie/main xfonts-base all 1:1.0.3 [6,181 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ jessie/main xrdp armhf 0.6.1-2 [195 kB]
Fetched 8,468 kB in 35s (236 kB/s)
Selecting previously unselected package x11-apps.
(Reading database ... 123536 files and directories currently installed.)
Preparing to unpack .../x11-apps_7.7+4_armhf.deb ...
Unpacking x11-apps (7.7+4) ...
Selecting previously unselected package x11-session-utils.
Preparing to unpack .../x11-session-utils_7.7+1_armhf.deb ...
Unpacking x11-session-utils (7.7+1) ...
Selecting previously unselected package xbase-clients.
Preparing to unpack .../xbase-clients_1%3a7.7+7_all.deb ...
Unpacking xbase-clients (1:7.7+7) ...
Selecting previously unselected package vnc4server.
Preparing to unpack .../vnc4server_4.1.1+X4.3.0-37.6_armhf.deb ...
Unpacking vnc4server (4.1.1+X4.3.0-37.6) ...
Selecting previously unselected package xbitmaps.
Preparing to unpack .../xbitmaps_1.1.1-2_all.deb ...
Unpacking xbitmaps (1.1.1-2) ...
Selecting previously unselected package xfonts-base.
Preparing to unpack .../xfonts-base_1%3a1.0.3_all.deb ...
Unpacking xfonts-base (1:1.0.3) ...
Selecting previously unselected package xrdp.
Preparing to unpack .../xrdp_0.6.1-2_armhf.deb ...
Unpacking xrdp (0.6.1-2) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for fontconfig (2.11.0-6.3) ...
Processing triggers for systemd (215-17+deb8u2) ...
Setting up x11-apps (7.7+4) ...
Setting up x11-session-utils (7.7+1) ...
Setting up xbase-clients (1:7.7+7) ...
Setting up vnc4server (4.1.1+X4.3.0-37.6) ...
update-alternatives: using /usr/bin/vnc4server to provide /usr/bin/vncserver (vncserver) in auto mode
update-alternatives: using /usr/bin/Xvnc4 to provide /usr/bin/Xvnc (Xvnc) in auto mode
update-alternatives: using /usr/bin/x0vnc4server to provide /usr/bin/x0vncserver (x0vncserver) in auto mode
update-alternatives: using /usr/bin/vnc4passwd to provide /usr/bin/vncpasswd (vncpasswd) in auto mode
update-alternatives: using /usr/bin/vnc4config to provide /usr/bin/vncconfig (vncconfig) in auto mode
Setting up xbitmaps (1.1.1-2) ...
Setting up xfonts-base (1:1.0.3) ...
Setting up xrdp (0.6.1-2) ...
Processing triggers for systemd (215-17+deb8u2) ...

Once that little lot has scrolled up your screen, you can exit the session ( just type “exit”).
There are some guides which suggest that you need to re-boot the Pi at this point. I found that this was not necessary. However, if things don’t quite work as described from this point on, it may be worth doing this. After all, “Have you tried turning it off and on again ?” is a cliche for a reason !

It’s probably worth mentioning that, at this point, you should be able to connect from any Windows ( Windows 7 or above) remote machine using the built-in Remote Desktop app.
That’s not we’re after though. Oh no. We want to be able to do this from Mint…

Installing rdesktop on Linux Mint

Back on Mint, open a Terminal window and…

sudo apt-get install rdesktop

Once again you should be prompted for your password ( remember this is for your user on the Mint machine, not on the Pi). You should then see something like …

[sudo] password for mike:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  pcscd
The following NEW packages will be installed
  rdesktop
0 to upgrade, 1 to newly install, 0 to remove and 83 not to upgrade.
Need to get 139 kB of archives.
After this operation, 427 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/universe rdesktop amd64 1.7.1-1ubuntu2 [139 kB]
Fetched 139 kB in 10s (12.6 kB/s)
Selecting previously unselected package rdesktop.
(Reading database ... 192214 files and directories currently installed.)
Preparing to unpack .../rdesktop_1.7.1-1ubuntu2_amd64.deb ...
Unpacking rdesktop (1.7.1-1ubuntu2) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up rdesktop (1.7.1-1ubuntu2) ...

Again, I found that no re-boot was required on the Mint machine. Once again, it might be different for you.

Either way, we should now be able to prove that the Pi will run happily without all of those wires plugged into it…

Connecting via rdesktop

…but I’d recommend keeping it all plugged in until you’ve tested first.

To connect from Mint, open a Terminal window and type :

rdesktop raspberrypi -u username_on_pi -p password -g 90%

…where username_on_pi is the name of the user on the Pi and password is the password for that user.

The -g switch tells rdesktop what size to create the window ( in our case, 90% of the screen size on our main machine)

You should now see (after a short interlude whilst the desktop is rendered)….

rdp_on_pi

Alternatively, if you want to run in a full screen, you can use the -f switch for rdesktop instead – i.e. :

rdesktop raspberrypi -u pi -p raspberry -f

Once you’re in full screen mode, you can toggle between full screen and window mode at any point by pressing CTRL+ALT+ENTER.
It’s worth noting if you do this and then minimize the rdesktop window, when you maximize again, desktop can appear to be blank and or the re-drawing might not be complete. I’m not sure why this is, or what the fix is.

One other point to note, it’s more secure to run rdesktop without specifying the password. In this way, you’ll be prompted for it when you connect.
So, if you run…

rdesktop raspberrypi -u pi -g 90%

…you will be presented with

pi_login

A Python Program to automate connection

To save us typing in the rdesktop command each time we want to connect to the Pi, we could write a simple bash script to automate our rdesktop command. However, in the circumstances, Python seems a more appropriate medium…

#!/usr/bin/env python

import sys, subprocess

def main():
    # Make sure that we've got a single argument passed in
    # NOTE - the first argument is the name of this program.
    if len(sys.argv) != 2 :
        sys.exit('Usage : %s screen_size as percentage between 25 and 100', sys.argv[0])

    if is_valid_screen_size(sys.argv[1]) :
        # Pass in the screen size to the function to build the rdesktop command
        command = build_rdp_command(sys.argv[1])
    else :
        sys.exit('Usage : %s screen_size as percentage between 25 and 100', sys.argv[0])

    try :
        # Run the command...
        status = subprocess.call(command, shell=True)
    except OSError as e :
        print >> sys.stderr, 'Error : ', e
    sys.exit( status)

def is_valid_screen_size( input_size) :
    # Validate the screen size.
    # Return True if it's a valid value

    # Make sure requested size is an integer
    try :
        int( input_size)
    except ValueError :
        return False
    # Now make sure it's not ridiculously small...or over 100
    if int( input_size) < 25 or int(input_size) > 100 :
        return False
    else :
        return True

def build_rdp_command(screen_size):
    # Return the appropriate rdesktop command

    # Initialize &quot;constants&quot; to use in the rdesktop command.
    PI_NAME = 'raspberrypi'
    PI_USER = 'pi'

    command_str = "rdesktop " + PI_NAME + " -u " + PI_USER
    if screen_size == 100 :
        # Full screen
        command_str = command_str + " -f "
    else :
        # Specify the percentage
        command_str = command_str + " -g " + screen_size +"%"
    return command_str    

if __name__ == "__main__" :
    main()

The program is saved as rdp_to_pi.py.
At the time of writing, the default Python version on Mint (17.2) is 2.7.6 ( although you can invoke a Python 3.4.3 interpreter by typing python3 at the prompt).
Therefore, this program is written for Python v2.

The first line of the program tells Linux to use the Python interpreter when this program is executed.

The program then following :

  • validates that it’s been passed a sensible argument value on the command line for the screen size percentage
  • builds the appropriate rdesktop command line using “constant” value for the machine name for the Pi and the name of the user to connect as
  • executes the command

To run the program, you first need to set the appropriate file permissions…

chmod u+x rdp_to_pi.py

…and then run it using a single “.” followed by a “/” ( not the usual “. ./” to run a bash script)…

./rdp_to_pi.py 90

Keyboard Mapping Issues

There are some other issues as well, most notably, the Pi seems to have forgotten where it is and has somehow adopted settings for a US keyboard.
If you want to test this and happen to have a UK keyboard, try opening a text editor and typing any of the following :

  • ” – will print @
  • @ – will print “
  • £ – will print #
  • | – will print ???
  • ~ – will print |

I’ve tried various things to fix this, but to no avail.
Despite the fact that both the Pi and the remote machine are configured with a UK keyboard, rdesktop seems to ignore this and insist on using US keyboard mappings.
I suspect that this is something to do with an X-Server configuration setting somewhere but I just can’t figure out where.

You may have more luck using this link as a starting point.

If anyone does have a solution to this, please let me know.

For now though, I’ve achieved my project goals :

  • get the telly back in place before there’s trouble
  • allow me to play with my Raspberry Pi whilst the festive extravaganza plays out on said TV

That should keep me quiet for a bit.

Advertisements

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