Raspberry Pi – Basic Setup on Raspbian (Raspberry Pi OS)

I love playing around with a Raspberry Pi*. Every time I decide to try a new project, I have to redo the basic setup to create a working system (install Rasbian and updates, setup language / keyboard stuff, expand disk space, import SSH keys, setup internet connection, ...). I know that I could do it one time, store a backup from the (micro)SD-card in a single .iso file and restore it every time I launch a new project. However, this also leads to missed updates, changes in versions etc. Therefore, I prefer starting from scratch every time.

I know, there are tons of tutorials and guides out there for basic Raspberry Pi setup, so I will not reinvent the world. This article can be seen as a quick go-through guide to remember the important steps. So if it is your first time setting up a Raspberry Pi, you better look for a beginner tutorial.

Steps

  1. Download and install Raspbian on a (micro)SD-card
  2. Enable SSH directly
  3. Apply basic configs (locate, expand disk, network)
  4. Update your system
  5. Add and enable a static IP address
  6. Add SSH keys

Guide

Preparation:

First, download Raspbian and put it on your (micro)SD card.

  1. Download latest Raspbian on https://www.raspberrypi.org/downloads/raspbian/
    • If you decide to use the Imager Tool, install it, put your (micro)SD-card in and write the system on the card
    • If you prefer the manual way, go on.
  2. Extract .zip file
  3. Write the .img file on the (micro)SD-card (not the file directly but with an image writer from your system)

If you don't want to connect a monitor, enable SSH directly:

  1. Add empty ssh file into the boot partition

  2. Add (micro)SD-card into your Raspberry Pi, but a LAN cable in and boot the system by using the power supply

Main config

After your Pi is up, use a monitor and keyboard if you want to work directly on a system or connect to it from another PC via SSH with ssh pi@192.168.X.X on Linux or Mac. Windows users can use Putty.

To find out the IP of your Pi, just look into your router web interface or try guessing it 😁

The default root username is pi with password raspberry.

Let's start with the main config:

  1. Run sudo raspi-config (Navigation Hint: Select with space, navigate with arrows, enter-key for confirmation, tab-key for "jumping")

    1. Select the first entry to change the password of the pi user

    2. Select 2 Network Options

      • Select N1 Hostname and change the name to something you prefer
    3. Select 4 Localisation Options

      • Select I1 Change Locale and choose your location. I prefer to use the UTF-8 ones.

      • Select I2 Change Timezone and select your time zone

      • Select I3 Change Keyboard Layout (automatic process)

    4. Perhaps select and change I4 Change Wi-fi Country

    5. Select 7 Advanced Options

      • Select A1 Expand Filesystem to be sure that all of the SD card storage is available for the OS
    6. In the end, select 8 Update to update the tool (just to be sure)

  2. For security and stability reasons it is important to keep your system up-to-date! So let's start by typing sudo apt-get update and sudo apt-get upgrade afterward to update and upgrade all your packages. Perhaps you need to accept the installation process by typing Y + Enter.

Static IP Address

I prefer giving my small microcontroller a static IP address and there are several ways to do it. The old, traditional way is to change the /etc/network/interfaces file. But Raspbian Jessi comes with a DHCP client daemon called DHCPCD. The suggested way is to change the config file of this daemon:

  1. Make sure that the dhcpcd client is running. Check it with sudo service dhcpcd status. If it is not running, use sudo service dhcpcd start and sudo systemctl enable dhcpcd to start and enable it.

  2. Now edit the config of the daemon with sudo nano /etc/dhcpcd.conf (or use another editor like vi or vim)

  3. Use your arrow keys to get to the end of the file. There you can find an example static IP configuration (commented out). Add the following lines at the end of the file. Remember to change it to your local network configuration:

    interface eth0
    static ip_address=192.168.1.2/24
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1
    

    Hint: The /24 stands for the subnetmask 255.255.255.0

  4. Save the file with Cmd + O and hitting enter and close it with Cmd + X (when using nano)

  5. It should work after restarting your system with sudo reboot

Import SSH keys

If you want to disable username and password login and use public / private key authentication instead, go on by

  1. Copying your public SSH key from your client to your Raspberry Pi with ssh-copy-id -i ~/.ssh/id_rsa.pub pi@192.168.X.X

    • Remember to change ~/.ssh/id_rsa.pub to your public SSH key location and 192.168.X.X with your Raspberry Pi IP address
    • If you don't have SSH keys on your client, generate some with ssh-keygen
  2. If you want to disable username and password login:

    1. Edit the config file with sudo nano /etc/ssh/sshd_config (or use another editor like vi or vim)

    2. Search for the following keywords and set it to yes / no:

      #[...]
      PasswordAuthentication no
      #[...]
      ChallengeResponseAuthentication no
      #[...]
      PubkeyAuthentication yes
      #[...]
      RSAAuthentication yes
      
    3. Save and close the file with Cmd + O, Enter and Cmd + X (when using nano)

    4. Restart your SSHD service with sudo systemctl restart sshd

That's it! We are done!

Footnote

  • Raspbian changes its name to Raspberry Pi OS
  • Raspberry Pi is a trademark of the Raspberry Pi Foundation. Logo by raspberrypi.com.