1/ Connecting to the Raspberry Pi via SSH
There are two ways to have access to our Raspberry Pi
- Connect it directly to a display with an HDMI cable, and use a keyboard and potentially a mouse for input
- Access it remotely from another computer, over a wired or wireless connection. This is especially useful when we just need a terminal screen, for instance to run Python scripts
Before establishing a connection, we need to perform a few tasks that require a keyboard and a screen. First of all, we need to enable the SSH server on the Raspberry Pi.
Enabling the SSH server
On the Raspberry Pi’s terminal, run the following command:
This takes us to the following screen:
Let’s go to Advanced Options and select SSH:
We can the select <Yes> when prompted to enable the server:
A notification then confirms that the SSH server was enabled. Choose <Finish> to exit this configuration tool. Now we need to know the IP address of the Raspberry Pi to allow a SSH connection.
Retrieving the Raspberry Pi IP address
Make sure the Raspberry Pi is connected to internet using an ethernet cable and enter the following command :
Save the IP address written after inet addr: it should be something like: 192.168.X.X.
We can now unplug the screen and keyboard from the Raspberry Pi, and connect from the other computer with Putty.
Connecting with Putty
One of the most popular software used for SSH connections is called Putty. This is a free program that you can download on the official page. Install it on your computer and then open it. You should get a screen like this :
On the Host Name field, enter the IP address you saved earlier and click on the Open button. You should see a terminal window asking you for the login and the password. After entering these informations, you are now connected to your Raspberry Pi ! Before clicking Open, you can fill in the field below Saved sessions and click Save. This way, you will not need to enter the IP address the next time you want to connect. You could just double click on the previously saved session.
Note : There is a common error which happens when you reinstall Raspbian and tries to connect via SSH, which says :
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
If you get this message, run :
ssh-keygen -R <IP_ADDRESS> (where <IP_ADDRESS> must be replaced by yours)
Otherwise, you don’t have to do anything.
2/ Programming with Python
One of the common way to program on the Raspberry Pi is to use Python scripts. Normally, Python is installed when you install Raspbian but you will need to install some common packaging tools for more advance development. To do that, run:
sudo apt-get install python-dev
To check which version of Python is installed, just run:
If you have a GUI Raspbian, you can directly code within the Raspberry Pi using an IDE. If you only have access to the terminal, you can use the default editors (nano for instance) but, this is not recommended since you would lack proper IDE functionalities. The other way would be to code on your laptop/computer and then transfer the file into the Raspberry Pi. The convenient way to do that is to use a program, such as the free WinSCP.
3/ Useful Linux commands
There are some useful commands you might want to know while using a Raspberry Pi, here is a non-exhaustive list of them:
sudo nano <PATH_OF_FILE>
If the file on the specified path exists, this command opens it on the nano editor. If it doesn’t, it will be created, and you can start writing into it.
Note: While inside the editor, you can see the actions you can perform (on the bottom of the screen). For instance, to exit this mode, you must press Ctrl + X (the “^” character stands for “Ctrl”). If you modified the file, the editor will ask you if you want to save the changes.
Note 2: <PATH_OF_FILE> must be replaced by either the full file’s path or just the file’s name. The former creates the file on the specified path, the latter creates it on the current directory.
Ex : sudo nano test/blink.py opens/creates the file blink.py located in the test folder
Move to the specified directory.
Note: If you don’t specify a PATH, you will be moved to home directory.
cd .. moves you to the parent directory.
Create a folder on the specified path.
Remove the specified file. Add the -d flag to remove an empty directory. Add the -rf flag to remove a directory and all the files inside it. If you get a permission denied error while trying to delete a file or folder, try to put sudo before the command.
Note : Flags must be placed between rm and <FILE>.
Display a list of folders and files inside the current directory
Run the specified python script.
sudo reboot -h now
Reboot the Raspberry Pi immediately. It is useful to apply changes to the Raspberry Pi
sudo shutdown -h now
Software shutdown of the Raspberry Pi. It is recommended to run this command before stopping power supply.