If you noticed my posts in this blog, I love to play with the Raspberry Pi machines. They are good, small, (will be back to) cheap, and very versatile. They allow me to build services for my home uses and fulfil my nerdy server side appetite.

As I am always starting from the same point, I decided to just transcribe the next Raspberry Pi spawn I do at home, as a reference in future posts.

Overview

This article intends to go through all steps from the delivered plain Raspberry Pi until a working-and-ready-for-playing host connected to the network. I divided the article in the following sections:

  1. Before we start
  2. Raspberry Pi Imager
  3. First Raspberry start
  4. Setting up the network configuration
  5. Activate the basics in the .bashrc file
  6. Setting up the Key pair
  7. Wrapping up

0. Before we start

0.1. What do I need?

SanDisk ExtremeHere are my 2 levels of basics. For the first level it's pretty clear:

  • A Raspberry Pi, a 4th edition in this case. The power cable and the network patch cable are obvious, right?

  • A micro SD card. I've tried several ones and SanDisk ones always gave me a good performance. Lately I read this article and I bought a pair of SanDisk Extreme A1 32GB, that even they're not the PRO, the specs were the same and the price was reasonable, and 32GB should be more than enough.

  • A secondary computer to burn the image on the Micro SD card. I use my everyday laptop for it.

For the second level I add:

  • A USB keyboard

  • A screen with a mini-HDMI connector.

Why? I like to do the first start physically, to see the boot sequence, to set up the network statically, and then all the rest I can do remote. Allows me to be versatile if anything pops up. I always have a spare USB Keyboard at home and any of my monitors have a spare connection I can use.

Anyways, it is not really needed. As long as the SSH server can be set up when we install the system into the micro-SD card, we can just check the router to recognise the new IP in the network and then SSH into the host and do everything in this article remotely. Your choice 😊

0.2. What is the goal for this host?

This seems obvious but makes sense: the goal of the host shapes the installation that we do.

For instance, in my case I usually set my Raspberries in a cluster setup I printed myself, as they provide services and are not really accessed directly by humans. This means mainly:

  • No wireless, they are all wired to a switch.

  • Headless, they will be accessed via SSH only.

This tell us that we don't need a graphic desktop when choosing which OS to install, and that we won't set up the wifi.

Your needs can be different, just have a thought about it beforehand.

0.3. Assumtions

  • The hostname will be jakku

  • The username will be xavi

  • The goal is providing a service, so headless and no wireless.

  • There is a local DNS server called dagobah in 192.168.1.231 that we'll make use of, as I set up in the past article Quick DNS server on a Raspberry Pi.

  • My secondary computer is a Mac laptop. Steps may be somewhat different to the ones you may need to do.

1. Raspberry Pi Imager

Nowadays the ecosystem is really easy to set the Raspberries up. We have a new official Raspberry Pi OS in place of the old Raspbian, and the guys behind the project brought the Raspberry Pi Imager, a SD card burner that does almost everything for you. We'll use all of if. Raspberry Pi Imager

1.1. Download and install Raspberry Pi Imager in your computer

This is the easiest step. Just visit the official Raspberry Pi Software section and download the Raspberry Pi Imager that fits with your laptop's OS. Then proceed to install it the way your OS likes to have things done.

It is really simple, but if you still need help, visit the official documentation.

1.2. Install the Raspberry Pi OS into the Micro SD card

Now we insert the micro-SD card into the laptop and start the Raspberry Pi Imager. A screen will appear with 2 white buttons and a 3rd one greyed.

1.2.1. Operating system

Pressing the Choose OS button allows us to select which operating system do we want to install into the micro-SD card. We have here a Raspberry Pi 4, so it is a 64 bits one. Also we know we want it headless and that we want the official Raspberry Pi OS, so our selection should be Raspberry Pi OS Lite (64-bit) under Raspberry Pi OS (other)

1.2.2. Storage

Pressing the Choose Storage button allows to select the micro-SD card where we'll install the Raspberry Pi OS into.

1.2.3. Advanced options

Once we have chosen the Operating system, a button with a gear appears in the lower right corner. This is the advanced options that we'll make use of, as it allows us to pre-configure some parameters that helps us in the first start of the host.

➡️ Set hostname

Mark it and introduce here the name of the host that we want to asign to this machine. I introduced jakku

➡️ Enable SSH

Yes, mark it, and mark also Use password authentication.

➡️ Set username and password

Yes, mark it, and set the username and password that you wish. xavi as username in my case.

➡️ Configure wireless LAN

Not in my case, as explained above. This would be the place to set up your wireless configuration if you want to have it wireless.

➡️ Set locale settings

Yes, mark it and set the Time zone and Keyboad layout that fits best to you. Europe/Berlin and us for me.

➡️ Persistent settings

Here I recommend to mark at least the Eject media when finished, so this step is automatically done and make the process easier for the user.

... and press Save when you're done.

💡Making use of these Advanced options is the best tool to achieve a completely headless installation, as you can set up the Wifi, username, hostname and SSH server beforehand and once the Raspberry Pi does the first start you'll be ready to SSH into it and continue the set up.

1.2.4. Write

Pressing the Write button will start the process of installing the Raspberry Pi Imager into the micro-SD card and will notice us when it's done, auto-ejecting it for our convenience.

2. First Raspberry Pi start

Insert the Micro SD card into the Raspberry Pi, make sure the network patch cable, HDMI and the USB keyboard are connected, and connect the power cable, which will automatically power the machine.

It will initiate and do a first quick step and proceeds to reboot. You'll see in the screen the list of actions and appearing and scrolling, until leaving you prompting for the login username and password under the hostname, which is already the ones we set up in the Raspberry Pi Imager. Proceed with the login.

2.1. Initial setup with raspi-config

Once we're logged, type:

sudo raspi-config

... and a text-based graphical tool will appear, that will help us to do the final touches to our Raspberry Pi

➡️ Enable SSH

It should be already enabled by the setup in the Raspberry Pi Imager, but just in case:

  1. Select 3 Interface Options and then I2 SSH.

  2. It asks Would you like the SSH server to be enabled? And we select Yes. A confirmation message will appear and with pressing <Ok> we'll be directed to the main raspi-config screen.

➡️ Set up the Localisation

In my case I have my Raspberries in English, and I connect through SSH with a keyboard in English too, so I want to make sure that it is the language and layot set up, and that we won't receive this localization not set up error.

  1. Select 5 Localization Options and then L1 Locale.

  2. A screen with a list of locales to be generated that we need to select. It's a multiselect list, and I had pre-selected en_GB.UTF-8 UTF-8. Select the ones that fit to you. In my case, I prefer the US version for compatibility matters, so I unselect en_GB.UTF-8 UTF-8 and select en_US.UTF-8 UTF-8. Tab to select <Ok> and Press Enter.

  3. A screen will appear asking for the default locale for the system environment. As I mentioned, I want also to get rid of the error message that the locales are not set by default, so here I select the one I wanted to have generated en_US.UTF-8. Tab to select <Ok> and Press Enter. It will proceed to generate the locales and bring us back to the main raspi-config screen.

Now we ensure that we are in the right timezone

  1. Select 5 Localization Options and then L2 Timezone.

  2. A screen to select the geographic area will appear.. In my case I select Europe, Tab to select <Ok> and Press Enter.

  3. A screen to select the Time zone will appear. In my case I select Berlin as I am living in Germany, Tab to select <Ok> and Press Enter. A quick process is done and we're redirected to the main raspi-config screen.

Now we setup the keyboard layout

  1. Select 5 Localization Options and then L3 Keyboard.

  2. A screen to select the keyboard model will appear. In my case I only use the USB keyboard once (actually now) and in case of any issue that requires physical hands on. For the rest I connect via SSH, so any generic that fits in our layout should work. I select Generic 105-key PC (intl.). Just for a note, there is a Macbook/Macbook Pro option, which could fit from what I connect from the 90% of the times, but the Generic one always worked for me whereve I connect from. Tab to select <Ok> and Press Enter.

  3. A screen asking for the Keyboard layout will appear. I mentioned I want English, and I also want to be able to work properly whenever I connect from bia SSH, so I won't select anything too special. I select the basic first one English (US). Tab to select <Ok> and Press Enter.

  4. A screen asking for the behaviour of the AltGr key will appear. One more time,I will connect via SSH so I don't want to settle with one too specific layout. Maybe for you it is interesting to set it up as Right Alt (AltGr) or as the Left Logo key if your keyboard is old and does not have a logo key. I will use the first The default for the keyboard layout. Tab to select <Ok> and Press Enter.

  5. A screen asking for the behaviour of the Compose key will appear. As we connect via SSH, there is No compose key, so we select this option, Tab to select <Ok> and Press Enter. A quick process is done and we're redirected to the main raspi-config screen.

 ➡️ Expand Filesystem

Here we make sure that the filesystem is using the 100% of the Micro SD card we have installed. In all my set ups that step was unnecessary, but still it's good to ensure it.

  1. Select 6 Advanced Options and then A1 Expand Filesystem. A quick process will happen and a new screen will appear saying that it's done and will be applied in the next reboot. Pressing Enter will driver us to the main raspi-config screen.

Here I usually take a break and do the reboot. I like to have one set up at a time to ensure that if anything fails I am able to point out what was it, so from the main raspi-config screen we Tab to <Finish> and press Enter. It will ask us to reboot now and we select <Yes> and press Enter. The system will automatically shutdown and start again, and leave us into the login screen, where we introduce the username and password and we'll be in again. Then with sudo raspi-config again we'll be back to the main raspi-config screen.

➡️ Make dhcpcd as our network configuration

There are several ways to setup the network configuration, and I like dhcpcd as it is clear, fast and easy. You can also use NetworkManager. In this article I will set the network via dhcpcd, so:

  1. Select 6 Advanced Options and then AA Network Config.

  2. A screen to select the network configuration to use will appear. Select dhcpcd, Tab to select <Ok> and Press Enter.

  3. A screen saying that dhcpcd is active will appear. Tab to select <Ok> and Press Enter. We'll be back to the main raspi-config screen.

As we're done configuring, we Tab to <Finish> and press Enter. It will ask us to reboot now and we select <Yes> and press Enter. The system will automatically shutdown and start again, and leave us into the login screen, where we introduce the username and password and we'll be in again.

3. Setting up the network configuration

Here we'll set up a static IP in this machine using the dhcpcd network configuration, and will tell the DNS local server that we have a new machine called jakku with this IP.

3.1 Set up a static IP with dhcpcd

Assuming that you're already logged in,

  1. Edit the network configuration file

    sudo nano /etc/dhcpcd.conf
  2. In the line hostname, we add jakku next to it. It will tell to the DHCP servers the hostname of this machine. The line should end up like:

    hostname jakku
  3. Even we have some configuration commented out that we could use, I like to keep them commented as a matter of "explanation". So anyways, at the end of the file, we add the following block:

    static ip_address=192.168.1.237/24
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.231
    • static ip_address defines the static IP of this machine.

    • static routers defines the IP of the router on our network.

    • static domain_name_servers defines the IP of the DNS that we'll use.

    This is my local configuration, and you can adjust it yo yours. One example is that anything that it is not set up here will be taken from the possible DHCP server that you may have in your network, so maybe you don't want to have the static domain_name_servers (and to be honest, I also have that the DHCP delivers the IP of the local DNS, so it is unneccessary).

  4. We save (ctrl + o) and exit (ctrl + x)

  5. And now we reboot the host

    reboot

ℹ️ Note: Check for the full parameters set and their explanations of the dhcpcd config file

Once we're back and logged in, we can check that we have the static IP correctly set up:

ifconfig

We should see a pair of network interfaces, eth0 (the interface of the host) and lo (the loopback one). The IP and the mask in eth0 should be what we set up above.

Now that we're here, we can ping the router:

ping 192.168.1.1

... and the router should answer.

And also we can ping the DNS server:

ping 192.168.1.231

... and the local DNS server should answer too.

3.2. Add the host to the local DNS server

This is surely optional for you. I have a local DNS server and I want to use hostnames and not IPs while working around.

So, from any computer of my local network,

  1. SSH into the DNS server:

    ssh xavi@dagobah
  2. Edit the hosts configuration file:

    sudo nano /etc/hosts
  3. Add a new entry for the new host:

    192.168.0.237 jakku
  4. Save (ctrl + o) and exit (ctrl + x)

  5. Restart the DNS server:

    sudo /etc/init.d/dnsmasq restart

Now, coming back to the terminal of any computer of the network, we can test that the DNS entry is working properly by pinging to jakku:

ping jakku

... and jakku should answer.

ℹ️ Note: From here on, we should be able to do everything else remotely via SSH even using the local DNS server. It's up to you if you continue the configuration physically as we have the keyboard and the screen connected, or if you prefer to do everything else remotely via SSH.

4. Activate the basics in the .bashrc file

So here we just enable some of the things that for me are basic when behaving with the Raspberry Pi:

  1. SSH into jakku:

    ssh xavi@jakku
  2. Edit the .bashrc file:

    nano ~/.bashrc
  3. Uncoment the alias lines, so they appear like:

    alias ll='ls -l'
    alias la='ls -A'
    alias l='ls -CF'
  4. Save (ctrl + o) and exit (ctrl + x)

  5. Reload the .bashrc file:

    source ~/.bashrc

5. Setting up the Keys pair

I do connect often via SSH from my laptop to my Raspberries, so a very convenient thing for me is to set up the SSH key authentication between my laptop and this new Raspberry Pi, so that the Raspberry accepts the key from the laptop and does not asks the password every time.

I won't extend too much here, as I have already a nice article explaining exactly this topic:

📄 Set up the SSH Key authentication between hosts

5.1. Get the laptop's public key

By the article above, I assume that we have already generated the key pair in our laptop. So the only thing we need to do here is to copy its public key:

  1. Show the key. In our case is a ED25519:

    cat ~/.ssh/id_ed25519.pub
  2. It will show a text string. Copy all of it.

5.2. Add the laptop's public key into the Raspberry Pi

At this point, even we activated the SSH server in our Raspberry Pi, it lacks all the host / user configuration like keys and so on. We could skip this process and just add the file that is read when a host wants to connect to it, but I know in the future I would like to have the keys generated, so there we go.

  1. SSH into jakku from the laptop:

    ssh xavi@jakku
  2. Generate the new pair of ED25519 keys for this host (as explained in the linked article above):

    ssh-keygen -t ed25519

This also creates the directory where all the host / user configuration is placed, so at this point we should be able to add the laptop's public key in the required file.

  1. Create and edit a file for the incoming authorised connection keys:

    sudo nano ~/.ssh/authorized_keys
  2. Paste here the public key line we copied from the laptop.

  3. Save (ctrl + o) and exit (ctrl + x).

  4. Disconnect from this SSH connection (ctrl + d)

  5. SSH into jakku again to test that we don't need the password.

  6. Should work!

6. Wrapping up

Now we have a host in our network ready to receive any project. Just install Docker and try any fediverse servers like Akkoma to host your own instance, or play with Nextcloud to host your own cloud server, or why don't you set up a local DNS server for your network? There are plenty of possibilities!

I am also planning to follow up in this article with some optimizations to apply, some tips and tricks that I am trying first and hopefully I can reveal them working. Stay tuned!

Previous Post Next Post