Although the Raspberry Pi can be used as a computer, later down the line there’s no reason that you can’t use it as an embedded platform. Plug it in and leave it running somewhere secluded and it can do whatever you want it to. However the only problem is that there may come a time when you need to alter something on it. Instead of having to move the RPi to plug in a monitor, mouse and keyboard, we’re going to look at setting up means of accessing it remotely.
SSH is a protocol for connecting to computers that is best known for its use as in Unix-based systems (like Linux). It allows the user to send encrypted data over a network to an SSH server thus allowing them remote access. Most Linux distributions have SSH installed, including Raspian on the Raspberry Pi. Once the SSH server has started on a computer, another computer can use an SSH client to connect to it.
SSH is entirely text-based and is run from the terminal: it essentially allows the user connecting to a remote machine to use that machine’s terminal as if they were sat in front of it. We will start by looking at SSH access and then look at connecting to the Raspberry Pi’s Desktop to allow us a visual display.
What You Need
In order for your computer to remotely connect to your Raspberry Pi, it needs to be on the same network. Either device can be connected wirelessly or via an Ethernet cable to the router.
Unless you know what you’re doing we recommend you don’t enable access to your Raspberry Pi from anywhere outside of your local network. It can be very easy to add a backdoor to your network if you don’t know what you’re doing.
However, that doesn’t mean your RPi cannot connect to the internet like any other computer (and all of our tutorials will assume that you have done this).
Get Your IP Address
In order to connect to our Raspberry Pi, we are going to need to know its IP Address on the Local Area Network (LAN). In the terminal, type in ifconfig and the RPi’s network information will be displayed.
The IP Address will be displayed after inet addr (as shown above). This will be the IP Address that we use to connect with using our other computer.
First we need to start by enabling SSH on the Raspberry Pi. Before we can start connecting remotely, we need to set some things up on the RPi. Start by opening up the terminal and type in sudo raspi-config
Navigate down to ssh with the arrow keys and hit the enter key. Select Enable and hit enter again. You should now receive message saying SSH has been enabled.
Create a Password
When we originally set up Raspian, we suggested that you change the default password if you plan on networking your Raspberry Pi. If you have already done this then you can skip to the next step, but if not then we strongly recommend you create a new password.
Whilst in the Raspian configuration window, select change_pass from the menu; you will then be prompted to enter a new password.
Now that our Raspberry is setup for SSH access, we need to install a client on another computer which will connect to it. There are several clients available for SSH access and you are welcome to use which ever you like. If you are using windows then we recommend you use PuTTY. If you are using Linux or Mac OS X, you can simply open up the terminal and type in ssh followed by your parameters (we’ll get to these soon).
Connecting with PuTTY
First open up PuTTY and you will be presented with an options screen:
Select SSH from the Connection type and type in your IP address in the Host Name (port 22 should automatically be selected as this is the default SSH Port), then click Open.
Enter pi as your username (unless you have different login information) and then type your password.
If you are presented with a notice about the authenticity, just type yes and the notice will disappear. We can ignore this since we are only connecting across a local network. If you wanted to set up SSH for a wide area network, you would have to add more security but we won’t be going into that.
You have successfully connected to your Raspberry Pi via SSH. You can now navigate around your RPi as if you were using its terminal.
Connecting with Mac OS X
Open up the Mac Terminal (this can be found in the Utilities folder of Applications), and type in ssh pi@ipaddress where ipaddress is the IP Address of your Raspberry Pi. If you wish to login as a different user simply replace pi with your selected username.
Enter yes if you are presented with a notice about the authenticity of the host and then enter your password. We can ignore this since we are only connecting across a local network. If you wanted to set up SSH for a wide area network, you would have to add more security but we won’t be going into that.
Once successful you can navigate around your RPi as if you were using its terminal.
In order to connect to our Raspberry Pi’s Desktop we must first install some software on the RPi and then download client software for other computers.
Installing VNC Server
First open up your Raspberry Pi and ensure it is connected to the internet and that SSH is enabled (as detailed in the steps above).
Open up the terminal and type in sudo apt-get install tightvncserver
Apt-get is a program on our Raspberry Pi that connects to an online repository of software. We want to install the software called TightVNCServer. After you hit enter, the program will find our software and ask if we want to install it; type y and hit enter until the program downloads and installs.
Once complete we can type in tightvncserver and it will run for the first time:
For added security you should enter a password here and this will be required when people try to connect. Type n when prompted for a view-only password and you server will start.
A view-only password is another password for users who just want to observe rather than control the RPi.
Starting VNC Server on Login
Now that we have our VNC Server installed we need to ensure it starts automatically when we turn the Raspberry Pi on; otherwise we’d have to start it manually which defeats the point of having it placed somewhere out of the way without a screen, mouse and keyboard.
Getting a program to start automatically when a user logs in is very simple on Raspian (especially compared to other Linux distros).
We want to place a .desktop file in the autostart folder of the user’s configuration. Open up terminal and type in mkdir /home/pi/.config/autostart
This will create a new directory in the .config folder of our user (pi). The autostart folder doesn’t exist by default but if it does you will presented with an error message saying that it does exist.
Next navigate to our folder by typing cd /home/pi/.config/autostart and then we want to create a new file called vnc.desktop. We can do this by typing nano vnc.desktop
Nano is an easy to use Linux text editor that is built into Raspian. The command nano will open the specified file and if it doesn’t exist then it will create it. In this case, the file vnc.desktop doesn’t exist but this same command later on will let you edit the file again.
In our .desktop file we want to execute the command tightvncserver :1 which will start a new vnc server on port 1
You must ensure there is a space between tightvncserver and :1 otherwise the command will return an error.
Press CTRL-X to exit nano and when prompted, type y to save the file.
We can now restart the Raspberry Pi and see if our server has started.
Once your RPi has loaded again, open up the terminal and type in tightvncserver :1 (the command we told the computer to run on start-up).
If all goes well you should be presented with an error saying the VNC server is already running. You can plug your Raspberry Pi in somewhere secluded and as long as it has a connection to your network then you will be able to remotely control it.
Now that we have a running VNC Server on our Raspberry Pi, we need software for our other computers to allow us to access the Desktop. We will be using RealVNC since it is very easy to use and can work on many platforms.
Head over to the VNC Viewer download page and select the program appropriate to your Operating System.
We will now demonstrate how to use VNC Viewer on Mac OS X however it is identical in Windows.
Once open simply type your Raspberry Pi’s operating system followed by :1 (for the server on port 1 as we specified earlier) and click Connect.
Note that you can also type the computer’s name instead of the IP Address, by default Raspian gives your Raspberry Pi the name raspberrypi so typing raspberrypi:1 would also work (unless of course you have changed it).
You will be given a notice to say that your connection won’t be encrypted but simply ignore this since we are only connecting across a local network.
Finally you will be prompted for the password that you set when you installed tightvncserver (as shown above).
Enter your password and if all goes well you should be able to connect to the Raspberry Pi’s Desktop.