Raspberry Pi Pico Tips and Tricks

Wednesday, 7 January 2015

Setting up remote access using TightVNC on a Raspberry Pi

The following post is a section of the book 'Raspberry Pi: Measure, Record, Explore'.  The entire book can be downloaded in pdf format for free from Leanpub or you can read it online here.
Since this post is a snapshot in time. I recommend that you download a copy of the book which is updated frequently to improve and expand the content.
---------------------------------------

Raspberry Pi remote access

To allow us to work on our Raspberry Pi from our normal desktop we will give ourselves the ability to connect to the Pi from another computer. The will mean that we don’t need to have the keyboard / mouse or video connected to the Raspberry Pi and we can physically place it somewhere else and still work on it without problem. This process is called ‘remotely accessing’ our computer .
To do this we need to install an application on our windows desktop which will act as a ‘client’ in the process and software on our Raspberry Pi to act as the ‘server’. There is a couple of different ways that we can accomplish this task. One way is to give us access to the Pi GUI from a remote computer (so you can use the Raspberry Pi desktop in the same way that we did with the startx command earlier) using a program called TightVNC and the other way is to get access to the command line (where all we do is type in our commands (like when we first log into the Pi)) via what’s called SSH access.
You don’t need to install both of these methods of remote access (or either if you want to keep using the Pi from its own keyboard, mouse and screen) but using one or the other would be a neat thing to allow you to put the Raspberry Pi in a location completely separate from your immediate location.
Which you choose to use depends on how you feel about using the device. If you’re more comfortable with a GUI environment, then TightVNC will be the solution. This has the disadvantage of using more computing resources on the Raspberry Pi so if you are considering working it fairly hard, then SSH access may be a better option.
Remote access using SSH will be covered in the next post.

Remote access via TightVNC

The software we will install is called TightVNC. It is free for personal and commercial use and implements a service called Virtual Network Computing. We need to set up instances of it on the client (the Windows desktop machine) and the server (the Raspberry Pi).

The Client-Server model

The ‘client-server’ model of computing is a very common term. It refers to a distribution of tasks or workload between computers to allow an application or service to operate. In this case the provider of the service (the server) is the Raspberry Pi and the user of the service (the client) is the Windows machine.
SETTING UP THE CLIENT (WINDOWS)
To install TightVNC for windows, go to the downloads page and select the appropriate version for your operating system.
Work through the installation process answering all the questions until you get to the screen asking what set-up type to choose.
TightVNC Set-up
We only want to install the viewer for the software (since we don’t want to make our Windows desktop available to other computers on the network). So click on ‘Custom’ and then select ‘TightVNC Viewer’ then select ‘Next’.
TightVNC Viewer
The ‘Select Additional Tasks’ selections can be left at their defaults.
TightVNC Additional Tasks
Then click on ‘Install’.
You will be asked to set a password for the TightVNC service. In theory we won’t be using TightVNC as a service (just as a viewer) but for the sake of safety(?) enter (and note somewhere) a password for remote access. You will also be asked for an administrative password. This could be useful in a multi user situation, so use your discretion here if required.
TightVNC Passwords
Click on ‘OK’ and you should be finished. You can now find ‘TightVNC Viewer’ from the start menu (but don’t bother running it yet as we still need to set up the Raspberry Pi!).
SETTING UP THE SERVER (RASPBERRY PI)
We’ll approach the process of installing the TightVNC server on the Raspberry Pi in two stages. In the first stage we’ll install the software, run it and test it. In the second stage we’ll configure it so that it starts automatically when the Raspberry Pi boots up which means that we can work remotely from that point.
Be Warned. This form of connection cannot be regarded as secure enough to connect via the Internet. We’re only using it for the convenience and because we should arguably be more interested in learning about using computers on a home network than being worried about whether or not we will be ‘hacked’. Make no expectations of security for this connection or the data on the Raspberry Pi, but don’t let that stop you using it.
Installing software on the Raspberry Pi is a pretty easy task. All you need to do is from the command line, type;
sudo apt-get install tightvncserver
You will recall that the sudo portion of the command makes sure that you will have the correct permissions (in this case to run a command). The command that is run is one of the family of apt-get commands, which deal with package management. The install part of the command tells the apt-get program to not just get the software, but to install it as well. Lastly, tightvncserver is the application that will be installed. For more information on the apt-get command, see the Glossary.
The Raspberry Pi may ask you if you want to continue once it’s done some checks that it can get the software (and any other software that it might be dependant on). Just answer ‘y’ if prompted.
After a short scrolling of messages, tightvncserver should be installed!
Now we can run the program by typing in;
tightvncserver
You will be prompted to enter a password that we will use on our Windows client software to authenticate that we are the right people connecting. Feel free to enter an appropriate password (believe it or not in this crazy security conscious world, there is a maximum length of password of 8 characters).
You will be asked if you want to have a ‘view-only’ password that would allow a client to look at, but not interact with the remote desktop (in this case I wouldn’t bother).
You will be asked for this information the first time you run tightvncserver, but from then on it will simply use your initial settings (so remember the password).
The software will then assign us a desktop and print out a couple of messages. One that we will need to note will say something like;
New 'X' desktop is raspberrypi:1
The :1 informs us of the number of the desktop session that we will be looking at (since you can have more than one).
Now on the Windows desktop, start the TightVNC Viewer program. We will see a dialogue box asking which remote host we want to connect to. In this box we will put the IP address of our Raspberry Pi followed by a colon (:) and the number of the desktop that was allocated in the tightvncserver program (10.1.1.8:1).
TightVNC Start
We will be prompted for the password that we set to access the remote desktop;
TightVNC Password
In theory we will then be connected to a remote desktop view of the Raspberry Pi from our Windows desktop.
Take a moment to interact with the connection and confirm that everything is working as anticipated.
TightVNC Desktop
COPYING AND PASTING BETWEEN WINDOWS AND THE RASPBERRY PI
Remotely accessing the Raspberry Pi is a great thing to be able to to, but to make the experience even more useful we need to have the ability to copy and paste between the Windows environment and the Raspberry Pi.
We can certainly survive without this feature, but being able to carry out research on a more powerful machine and then copy-paste code from one to the other is a real advantage.
On the raspberry Pi, first we have to install ‘autocutsel’ as follows;
sudo apt-get install autocutsel
Then we need to edit the ‘xstartup’ file as follows;
nano /home/pi/.vnc/xstartup
… and add in the line autocutsel -fork to start it when the graphical display starts;
#!/bin/sh

xrdb $HOME/.Xresources
xsetroot -solid grey
autocutsel -fork
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
# Fix to make GNOME work
export XKL_XMODMAP_DISABLE=1
/etc/X11/Xsession
Make sure that you place the autocutsel -fork in the position indicated in the example above as otherwise it will not work as desired.
All that remains is to reboot the Raspberry Pi for the changes to take effect.
sudo reboot
STARTING TIGHTVNC AT BOOT ON THE PI.
Having a remote desktop is extremely useful, but if we need to run the tightvncserver program on the Raspberry Pi each time we want to use the remote desktop, it would be extremely inconvenient. What we will do instead is set up tightvncserver so that it starts automatically each time the Raspberry Pi boots up.
To do this we are going to use a little bit of Linux cleverness. We’ll explain it as we go along, but be aware, some will find the explanations a little tiresome (if you’re already familiar) but I’m sure that there will be some readers who will benefit.
Our first task will be to edit the file /etc/rc.local. This file can contain commands that get run on start-up. If we look at the file we can see that there is already few entries in there;
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

exit 0
The first set of lines with a hash mark (#) in front of them are comments. These are just there to explain what is going on to someone reading the file.
The lines of code towards the bottom clearly have something to do with the IP address of the computer. In fact they are a short script that checks to see if the Raspberry Pi has an IP address and if it does, it prints it out. If you recall when we were setting out IP address earlier in the chapter, we could see the IP address printed out on the screen when the Pi booted up like so
My IP address is 10.1.1.8

Raspbian GNU/Linux 7 raspberrypi tty1

raspberrypi login:
This piece of script in rc.local is the code responsible for printing out the IP address!
We will add the following command into rc.local;
su - pi -c '/usr/bin/tightvncserver :1'
This command switches user to be the ‘pi’ user with su - pi. The su stands for ‘switch user’ the dash (-) makes sure that the user pi’s environment (like all their settings) are used correctly and pi is the user.
The -c option declares that the next piece of the line is going to be the command that will be run and the part inside the quote marks ('/usr/bin/tightvncserver :1') is the command.
The command in this case executes the file tightvncserver which is in the /usr/bin directory and it specifies that we should start desktop session 1 (:1).
To do this we will edit the rc.local file with the following command;
sudo nano /etc/rc.local
Add in our lines so that the file looks like the following;
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# Start tightvncserver
su - pi -c '/usr/bin/tightvncserver :1`

exit 0
(We can also add our own comment into the file to let future readers know what’s going on)
That should be it. We should now be able to test that the service starts when the Pi boots by typing in;
sudo reboot
When the Raspberry has finished starting up again, we should be able to see in the list of text that shows up while the boot sequence is starting the line New 'X' desktop is raspberrypi:1.
Assuming that this is the case, we can now start the TightVNC Viewer program on the Windows desktop and we will be able to see a remote view of the Raspberry Pi’s desktop.
TightVNC Desktop
There are alternative ways to allow tightvncserver to start automatically, and I am sure that they are completely valid for different purposes. One of the most popular allows the remote desktop to start as the root user. The instructions above deliberately start the desktop as the pi user to attempt to mirror the user experience as if accessing the Raspberry Pi directly. It also preserves a degree of security in that the user can not automatically start messing up some of the more delicate functions of the computer (they just have to think a little more about it :-)).
Now for the big test.
Power off the Raspberry Pi;
sudo poweroff
Now physically turn off the power to the Pi.
Unplug the keyboard / mouse and the video from the unit so that there is only the power connector and the network cable left plugged in.
Now turn the power back on.
(We will need to wait for 30 seconds or so while it boots up)
Start the TightVNC Viewer program on the Windows desktop and we will be able to see a remote view of the Raspberry Pi’s desktop but this time the Pi doesn’t have a keyboard / mouse or video screen attached.
Raspberry Pi Liberated by TightVNC


If this is the first time that you’ve done something like this it can be a very liberating feeling. Suddenly you can see possibilities for using the Raspberry Pi that do not involve having it physically tethered to a lot of extra peripherals. And if you’re anyone like me, the next thing you do is ask yourself, “How can I get rid of that network cable?

The post above (and heaps of other stuff) is in the book 'Raspberry Pi: Measure, Record, Explore' that can be downloaded for free (or donate if you really want to :-)).

2 comments:

  1. Hey Malcolm,
    Thanks for the great raspberry pi book, exactly what I was looking for. One thing I noticed is that there is little mention of time zones. As a beginner, how MYSQL does timezones and that is shown in the D3 visualization is brain melting. In the end I just want the correct times to appear on my graph rather than UTC.

    ReplyDelete
    Replies
    1. That's a really good question. You may have an earlier copy of the book. I updated it a while ago and included a section on changing the time zones in a chapter called 'Tips and Tricks'. Just go back to Leanpub and you can automagically download the latest version. If you just want to check it out online you can read it here https://leanpub.com/RPiMRE/read#leanpub-auto-changing-the-default-local-time. and in a few days time I am going to put it up on the blog.
      Glad that you're enjoying the book :-).

      Delete