Set-up · 20 minutes
Set up your networking and hardening lab
- Get the practice system of the course ready, with two terminals
- Make the lab folder and take a snapshot or a backup before the network lessons
- Prove with a check script that the lab is ready
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
What you need
This course changes addresses, firewall rules, SSH, a web server and a database, and it ends with a server that you must take over. A mistake can cut you off from a machine. So every task runs on a practice system that you can break and restore. Use the one from Part 2 (administration): a Linux virtual machine, a Linux container with its own init system, WSL with systemd turned on, or a spare computer. Do not use the main system of a work laptop, and never touch a system that is not yours. Do not use a server that is reachable from the internet: lesson 4 starts an SSH server with lab passwords, and lesson 8 a web server. Work at the console of the practice system (its own window or terminal), not in an SSH session: lesson 4 closes the SSH server to the network, so a new SSH login from another machine fails from then on.
| Need | Why |
|---|---|
A Linux with systemd, bash and apt (the outputs come from Ubuntu 24.04) | The lessons use systemctl, apt, netplan and Ubuntu paths. Other families are named in a table where they differ. |
A user with sudo rights | Most network and security changes need administrator rights. |
| About 3 GB of free disk space, or 25 GB to build the kernel in lesson 9 | The web stack, and the kernel source in lesson 9 (about 1.5 GB unpacked). A full kernel build needs the larger size. |
A network connection for apt | The lessons install packages. All tests stay on your own machine. |
| Release | On a newer release (Ubuntu 26.04 and later) the commands and exit codes are the same. The wording of messages, version numbers and some file lists differ a little. Answer every check from a number, a name or an exit code, never from a message. |
| Two terminal windows | See step 2. |
Set it up
1. Open a terminal and make the lab folder
mkdir -p makes a folder and the folders above it. cd moves you into it.
mkdir -p ~/krlab/linux-advanced
cd ~/krlab/linux-advanced
pwd/home/you/krlab/linux-advancedEvery lesson starts from this folder. Files that a lesson tells you to save go here.
2. Open a second terminal
Open a second terminal window (or a second tab) and keep it open for the whole course. Use it as your way back. If a change to SSH, the firewall or an address cuts the first window off, the second one, which stays logged in, can undo the change. Lesson 4 tells you when to use it. Run sudo -v in each window now. It asks for your password once, and sudo then runs without a prompt for a few minutes.
- You → Snapshot: take it before the lesson
- Terminal 2 → Terminal 1: undo a mistake from here
3. Take a snapshot or a backup
A snapshot is a saved state of the whole machine, to which you can return. Take one now, and name it before-network.
| Practice system | How |
|---|---|
| Virtual machine | The snapshot function of the program (VirtualBox, VMware, virt-manager). |
| WSL | In PowerShell: wsl --export Ubuntu ubuntu-before-network.tar |
| Linux container | The snapshot command of the container tool that you use. |
| Spare computer | The backup of step 4 below, and a way to reinstall. |
The commands of the tools in this table were not run on the build machine. Check the manual of your tool.
4. Back up the settings
A snapshot may not be possible. A copy of /etc, where most settings live, is a small second way back. tar -czf makes a compressed archive, and -t lists one (lesson 9 of Part 1).
sudo tar -czf ~/krlab/linux-advanced/etc-backup.tar.gz /etc
sudo chmod 600 ~/krlab/linux-advanced/etc-backup.tar.gz
sudo tar -tzf ~/krlab/linux-advanced/etc-backup.tar.gz | head -3tar: Removing leading `/' from member names
etc/
etc/.resolv.conf.systemd-resolved.bak
etc/newt/The archive holds secret files such as the password hashes and the SSH host keys. chmod 600 lets only its owner, root, read it, so the listing needs sudo too. tar removes the leading /, so the archive holds etc/... and can be unpacked anywhere. The first lines of your list differ. To restore one file, extract only that file with sudo tar -xzf FILE -C / etc/hosts.
5. Install the tools
The lessons use these tools: dig (package dnsutils), rsync, nft (package nftables), gpg (package gnupg) and the SSH server. -y answers yes to the question of apt.
sudo apt update
sudo apt install -y dnsutils rsync nftables gnupg openssh-server6. Write the check script
Save this script as labcheck.sh in the lab folder. The function check runs a command and prints ok or MISSING. It counts the checks that passed, prints the last line, and exits with a failure code if one check failed. Two words of the shell are new. local keeps a variable inside the function. shift drops the first argument, so that "$@" (all the arguments) holds the command that check must run.
labcheck.sh
#!/usr/bin/env bash
# labcheck.sh - checks that this machine can run Part 3 of the Linux course (networking and hardening).
ok=0
total=0
check() {
local name=$1
shift
total=$((total + 1))
if "$@" > /dev/null 2>&1; then
ok=$((ok + 1))
echo "ok $name"
else
echo "MISSING $name"
fi
}
check "ip" command -v ip
check "ss" command -v ss
check "curl" command -v curl
check "python3" command -v python3
check "dig" command -v dig
check "firewall tool nft" command -v nft
check "gpg" command -v gpg
check "rsync" command -v rsync
check "ssh" command -v ssh
check "ping" command -v ping
check "perl" command -v perl
check "unshare" command -v unshare
check "systemd" test -d /run/systemd/system
check "sudo without a prompt" sudo -n true
check "lab folder" test -w "$HOME/krlab/linux-advanced"
echo "lab ready: $ok of $total"
(( ok == total ))Check your lab
Run the script with bash.
bash labcheck.shok ip
ok ss
ok curl
ok python3
ok dig
ok firewall tool nft
ok gpg
ok rsync
ok ssh
ok ping
ok perl
ok unshare
ok systemd
ok sudo without a prompt
ok lab folder
lab ready: 15 of 15Each line is one requirement, and the last line is the result. Type the last line of your own run.
If it fails
A line that starts with MISSING names what is absent. Fix it, and run the script again.
| Line | What to do |
|---|---|
ip, ss | sudo apt install iproute2. On Fedora: sudo dnf install iproute. |
curl | sudo apt install curl, or sudo dnf install curl. |
dig, firewall tool nft, gpg, rsync | Step 5. On Fedora: bind-utils, nftables, gnupg2, rsync. |
ssh, ping, perl, unshare | sudo apt install openssh-client iputils-ping perl util-linux. |
python3 | sudo apt install python3, or sudo dnf install python3. |
systemd | The machine does not use systemd. On WSL, add [boot] and systemd=true to /etc/wsl.conf, and restart WSL. In a container, use an image that runs systemd. |
sudo without a prompt | Run sudo -v in the same window, and enter your password. If you are not allowed to use sudo, ask the owner of the machine, or use your own virtual machine. |
lab folder | Go back to step 1. Check that the folder is yours with ls -ld ~/krlab/linux-advanced. |
If the last line says lab ready: 14 of 15, fix the missing part before lesson 2. You can still do lesson 1 without sudo.