Building a ManagementPi for the Home Lab

Posted by MarWinsWorld on Sunday, March 28, 2021

Contents

Owning a home lab for testing things like infrastructure as code (IaC), containers, and Kubernetes and always wanted to orchestrate the environment with exactly these technologies? Here’s an easy way on how to build an always-on management server, based on a spare Raspberry Pi.

I always like to try out new technologies, especially in the areas of automation and containers. From my personal experience, the best way to improve knowledge about new things is to play with and study them in a safe environment where you can break and fix things easily. So, why not creating something which can manage that kind of environment using the same technologies?

This is what this short series is about, building a small management server with the most essential tools to orchestrate the lab, deploy containers and manage my containerized home automation. The management server is built on a spare Raspberry Pi 3+ using open source software only.

Now, let’s start with an overview of this short series of blog posts on how to create a ManagementPi for your home lab.

Any feedback is welcome :-)

Install Ubuntu 20.04 on a microSD card

The easiest way to install Ubuntu 20.04 on a microSD card is the Raspberry Pi Imager. It is available for Ubuntu, Windows, and macOS.

Important: Installing Ubuntu 20.04 on the microSD card will erase all data on the flash drive. So make sure that there is no important data on it.

After downloading the Raspberry Pi Imager, insert your microSD card into your computer and start the tool. Now choose which version of Ubuntu 20.04 you want to install. For our ManagementPi, I’d recommend the following:

Other general purpose OS --> Ubuntu --> Ubuntu Server 20.04 LTS (RPi 3/4/400)

Furthermore, you choose the SD card — if there is only one plugged into the computer, the choice should not be difficult. Then click on Write and the installation process begins. Depending on your internet speed, the download will take a few minutes.

Before the first start, you can already prepare the Raspberry Pi with Ubuntu 20.04 This is recommended, especially if you want to connect the device to the network via Wi-Fi.

Wi-Fi or Ethernet cable

The first question will be whether you want to connect the Raspberry Pi to the network via Wi-Fi or Ethernet cable. With a cable, this is easy because you don’t have to do anything else.

If you want to configure the Raspberry Pi with Ubuntu 20.04 for wireless networking before the first boot, a small configuration step is necessary.

Let’s assume the microSD card is still in your computer, look for the partition system-boot. If necessary, you have to eject the disk and plug it in again. The partition contains configuration files that are loaded during the first boot. This is similar to configuring Raspbian for headless use.

On the partition, you will find a file network-config. Open it with a text editor and adapt the wifis-section to your needs.

For example:

wifis:
  wlan0:
    dhcp4: true
    optional: true
    access-points:
      HomeNetwork:
        password: "topsecret"

Important: If your Wi-Fi network name contains a space, you must enclose the name in quotation marks: “Home Network”.

You can also change the network settings later by editing the file /etc/netplan/50-cloud-init.yaml.

Wi-Fi should work now with Ubuntu 20.04 LTS on the Raspberry Pi

Booting Raspberry Pi with Ubuntu 20.04 LTS

Now insert the microSD card into your Raspberry Pi and start the system.

Access Ubuntu 20.04 for Raspberry Pi via SSH

I prefer to run my Pis headless without a keyboard or monitor attached. So I recommend checking your router or DHCP server for the assigned IP address(es) of the Pi and directly connect via SSH.

You may want to give a static IP address to a server anyway. I always do this via DHCP server because the IP address is then bound to the MAC address. Even when I install a new operating system, I know what IP address the device has. Just as a thought.

Important: Username and password are ubuntu by default (both the same).

When you log in for the first time, the system will immediately ask you to change your password. This is useful because the SSH server is activated by default. If you don’t change the password, everyone on the same network can access your Raspberry Pi.

ssh ubuntu@<IP address of the Raspberry Pi>

Change keyboard settings on Ubuntu 20.04 Server

If you ever connect your Pi with a keyboard and monitor you should potentially change the keyboard layout is the next step. Use the command on the console:

L='de' && sudo sed -i 's/XKBLAYOUT=\"\w*"/XKBLAYOUT=\"'$L'\"/g' /etc/default/keyboard

Specify L with whatever layout you require.

Changing the time zone

You should also be aware that the timezone is set to UTC. If you want to change that as well use the following command:

sudo timedatectl set-timezone Europe/Berlin

Now you can check with date whether this has worked or not.

Set Hostname

To change the hostname of your Pi, set it within the file /etc/hostname.

sudo vi /etc/hostname

Update your system to the latest version

As a last step for now, go for a system update.

sudo apt update
sudo apt upgrade -y

After you are done reboot the system.

Optional Tweaks

Typically I make some minor tweaks on Linux servers, which are all optional of course. Here’s what I do:

SSH Keys

To simplify ssh authentication I add my public key stored on my workstation to the Linux server. I do this with:

ssh-copy-ip ubuntu@<IP address of the Raspberry Pi>

Doing so will enable SSH key authentication and I’m not required to type in the password anymore.

End password prompts for Sudo

Next, I’d like to end sudo password prompts. If you like, you can disable them for the ubuntu-user with

sudo visudo -f /etc/sudoers.d/custom-users

and insert the following line

`ubuntu ALL=(ALL) NOPASSWD:ALL`

Creating a nice ASCII-Art-Banner

So that you always know that you are working on your management server, you can create a SSH login banner with figlet.

sudo apt install -y figlet
figlet "ManagementPi" > /tmp/motd && sudo mv /tmp/motd /etc/motd
 __  __                                                   _   ____  _
|  \/  | __ _ _ __   __ _  __ _  ___ _ __ ___   ___ _ __ | |_|  _ \(_)
| |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '_ ` _ \ / _ \ '_ \| __| |_) | |
| |  | | (_| | | | | (_| | (_| |  __/ | | | | |  __/ | | | |_|  __/| |
|_|  |_|\__,_|_| |_|\__,_|\__, |\___|_| |_| |_|\___|_| |_|\__|_|   |_|
                          |___/

Bash Tweaks

Ubuntu by default uses bash as the shell. Here are some small tweaks I typically add to my .bashrc.

  • scr will reattach to a possibly already running screen session. If there is none, it will recreate one.
  • .. eases jumping multiple levels down from the current directory. A .. 3 would be equivalent to cd ../../../
  • tc lists all your custom functions available in your current shell
cp ~/.bashrc ~/.bashrc.orig
cat <<EOF >> ~/.bashrc
function scr {
    if screen -ls | grep -q Main; then
         # reattach to Main:
         screen -xr Main
    else
         # name session "Main":
         screen -S Main
    fi
   }

function .. (){
    local arg=\${1:-1};
    local dir=""
    while [ \$arg -gt 0 ]; do
        dir="../\$dir"
        arg=\$((\$arg - 1));
    done
    cd \$dir #>&/dev/null
}

tc() { set \${*,,} ; echo \${*^} ; }
EOF

source ~/.bashrc