Setting up the Raspberry Pi

  • Setting up SSH

    For many projects and experiments, a keyboard, mouse, and monitor connected to Raspberry Pi are more of a hindrance than a help. The SSH protocol provides network access to Raspberry Pi via a command line from a computer on the same network.

    This means that once you've finished setting up Raspberry Pi, you should only have the power cord connected to it, along with either an Ethernet cable or a USB Wi-Fi adapter.

    Click on the terminal icon at the top of the Raspberry Pi desktop (Figure 1) and enter the following command in the LXTerminal window (omit the "$" symbol; it's just a command line prompt):


    sudo raspi-config

    Starting LXTerminal
    image.png (40.62 KiB)
    Starting LXTerminal Viewed 2534 times
    Activating SSH with rasoi-config
    image.png (95.12 KiB)
    Activating SSH with rasoi-config Viewed 2534 times
    This will open the raspi-config tool for configuring Raspberry Pi. Using the arrow keys, navigate to the Advanced menu and press <Enter>. Then, use the arrow keys again to select SSH (Figure 2).

    Next, choose Enable and then execute the Finish command from the menu. SSH protocol for remote access is now enabled on your Raspberry Pi.

    It's time to connect from your computer.

    If you're using a Linux or macOS operating system on your computer, it has its terminal program capable of connecting to Raspberry Pi.

    Finding the IP Address of Raspberry Pi

    Before you can connect to Raspberry Pi via SSH from another computer on your network, you need to determine the IP address of Raspberry Pi. To do this, run the following command in the LXTerminal window on Raspberry Pi:


    hostname -I


    This will return a 4-group number constructed as follows: 192.168.1.23. This is the IP address of your Raspberry Pi.

    SSH on a Windows Computer

    If your computer runs the Microsoft Windows operating system, you'll need the PuTTY program to work with Raspberry Pi. You don't need to install the program; you can download the PuTTY executable file from http://www.putty.org/. Simply save it somewhere (e.g., your desktop) and double-click to launch it. The PuTTY configuration window will open (Figure 3).
    PuTTY configuration window
    image.png (63.23 KiB)
    PuTTY configuration window Viewed 2534 times
    In the Host Name (or IP address) field, enter the IP address of your Raspberry Pi (refer to the earlier section "Finding the IP Address of Raspberry Pi") and click the Open button. The system will prompt you to log in to pi (Figure 3.7). Enter the username: pi and the password: raspberry - that's it, you're in. Now you can enter commands in PuTTY on your main computer, and they will be executed on Raspberry Pi.

    SSH on macOS or Linux

    If you're using a computer running macOS or Linux, the programs required for connecting to Raspberry Pi are already pre-installed on your computer. Open a terminal session and enter the following command, replacing the Raspberry Pi board's IP address (192.168.1.23) with the address of your Raspberry Pi board:


    ssh 192.168.1.23 -l pi


    remote control of Raspberry Pi via SSH protocol
    image.png (63.68 KiB)
    remote control of Raspberry Pi via SSH protocol Viewed 2534 times
    The first time you do this, you'll see the following message:

    Code: Select all

    The authenticity of host 1192.168.1.23 (192.168.1.23)' can't be established.
    RSA key fingerprint is 48:8f:c3:07:c2:04:9e:8b:59:ed:53:2b:0b:d0:aa:e5.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added *192.168.1.23' (RSA) to the list of known hosts.
    p10192.168.1.23's password:

    To proceed, confirm the authenticity of the computer you're connecting to by entering "yes." From this point on, all the commands you enter will be executed on Raspberry Pi, not on your computer.

    Linux Command Line

    If you're used to working with the Windows or macOS operating systems, you might not have had to interact with a computer through the command line. However, the Linux operating system used by Raspberry Pi requires you to enter commands in the command line for tasks such as installing software, copying and renaming files, running programs, or editing files.

    We already used the Raspberry Pi command line when setting up the SSH connection on Raspberry Pi, and now you can execute Raspberry Pi commands via SSH or directly through the LXTerminal.

    You might have noticed that once LXTerminal or the SSH session is ready to accept a command, a "$" symbol appears at the end of the line. This is called the command prompt, and it's how Linux indicates that it's ready to receive the next command.

    Working with the Linux command line is based on the concept of the current directory. This is the directory you are currently working in. This means that if you want to run a Python program located in a specific directory, you typically need to navigate to that directory before running the program. The command used for such navigation is called cd (short for "change directory").

    Right after starting an LXTerminal session, you're in the current directory, which is typically [/home/pi]. This is called the home directory. If there is a folder named "make_action" in the home directory, you can navigate to it by entering the following command (changing the directory relative to the current one):


    cd make_action


    Alternatively, you can enter the entire path to the directory:


    cd /home/pi/make_action


    All code for Raspberry Pi is written in the Python programming language. To run a Python program named "test.py," you would use the following command:

    python test.py



    Another command you'll often encounter is sudo. It's used to run the command that follows it in superuser mode. Linux tries to protect itself from accidental deletion of important system files and the execution of other critical actions by not granting such rights to regular users. The idea is that a user acting as a superuser knows what they are doing. Nevertheless, working in superuser mode is necessary to access the GPIO pins, which you'll need to do quite frequently in this book.

    For example, if the "test.py" program uses GPIO pins, you would need to run it with the following command:


    sudo python test.py


    The screenshot shows the nano text editor in action.
    Editor nano
    image.png (111.48 KiB)
    Editor nano Viewed 2534 times
    Since nano is designed to work in a command-line environment (such as LXTerminal or via SSH), you'll need to navigate through the file using arrow keys instead of a mouse. When you're ready to save the file, press <Ctrl>+<X>, then press "Y," and finally, press "Enter" to confirm the save.