Lab 6 of 8 · Networking, SSH and firewalls
Secure SSH and put a firewall in front of your server
Inspect the network, sign in with your own SSH key, turn off password sign-in, and allow only SSH and your status page through a firewall.
- Time
- 60 minutes
- Cost
- Free.
What you learn
Servers are reached over the network, administered over SSH, and protected by a firewall that allows only what is needed.
- Addresses, routes, listening ports and DNS from the server side
- Signing in with your own SSH key, and turning off password sign-in
- A default-deny firewall with ufw
- Testing access from another machine
In the AI era: Firewall and SSH changes are where AI-suggested commands lock people out. Always allow SSH before turning a firewall on, check config before reloading, and keep a second session open.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
Before you start
Finish labs 1 and 4 (you need the status page on port 8080).
Start your server and open your repository: run
multipass start sysadmin-labandmultipass shell sysadmin-labon your computer, thencd linux-admin-labon the server.
Step by step
1 See the server from the inside
ipshows addresses and routes,ssshows which programs listen on which ports, andresolvectlshows which DNS server is used.ip -brief address ip route sudo ss -tlnp resolvectl status | head -n 12You should see:Your server's address, its default route,
sshdon port 22,python3on port 8080, and a DNS server.2 Create an SSH key on your computer
Open a second terminal on your own computer for this step, not on the server. Press Enter to accept the defaults, then copy the line that starts with
ssh-ed25519.ssh-keygen -t ed25519 -f ~/.ssh/sysadmin-lab -C "sysadmin-lab" cat ~/.ssh/sysadmin-lab.pubYou should see:One line starting with
ssh-ed25519and ending withsysadmin-lab.3 Authorise the key on the server
Replace the placeholder with the line you copied. Only someone holding the matching private key can now sign in as
ubuntu.echo "PASTE-YOUR-PUBLIC-KEY-LINE-HERE" >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys4 Sign in with your key
Replace SERVER-IP with the address from the first line. Leave this session open: it is your safety line while you change SSH and the firewall.
multipass info sysadmin-lab | grep IPv4 ssh -i ~/.ssh/sysadmin-lab ubuntu@SERVER-IPYou should see:A shell on
sysadmin-labwithout any password prompt.5 Turn off password sign-in
Put your settings in a drop-in file, test the whole configuration with
sshd -t, and reload only if the test passes.printf 'PasswordAuthentication no\nPermitRootLogin no\n' | sudo tee /etc/ssh/sshd_config.d/10-hardening.conf sudo sshd -t && sudo systemctl reload ssh sudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin) 'You should see:passwordauthentication noandpermitrootlogin no, and your key session still works.6 Ask AI for firewall rules, then dry-run them
Ask your AI assistant: "Write ufw commands for a server that must allow SSH and TCP port 8080 and block all other incoming traffic." Before running anything, check that its answer allows SSH: turning on a firewall without it locks you out.
--dry-runshows what ufw would change without changing it.sudo ufw --dry-run allow OpenSSH sudo ufw --dry-run allow 8080/tcpYou should see:The rules ufw would add, and nothing changes yet.
7 Turn the firewall on
Default deny for incoming traffic, then allow exactly two things.
sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw allow OpenSSH sudo ufw allow 8080/tcp sudo ufw --force enable sudo ufw status verboseYou should see:Status: active,Default: deny (incoming), andALLOWrules forOpenSSHand8080/tcp.8 Test from your computer
The first command should reach the status page through the firewall. The second tries a password sign-in, which must now be refused.
curl http://SERVER-IP:8080 ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password ubuntu@SERVER-IPYou should see:Your
<h1>line, thenPermission denied (publickey).9 Write your notes
Create the file with
nano notes/06-network.md.# Networking, SSH and firewall ## What my server listens on | Port | Program | Needed? | |------|---------|---------| | 22 | sshd | yes, for administration | | 8080 | python3 | ... | ## SSH - How I sign in now: ... - Why password sign-in is off: ... ## Firewall - Default policy: ... - What the AI suggested, and whether it would have locked me out: ...10 Commit and push
Keep the SSH settings with your notes.
mkdir -p config sudo cp /etc/ssh/sshd_config.d/10-hardening.conf config/ sudo chown ubuntu: config/10-hardening.conf git add config notes git commit -m "Harden SSH and add a firewall" git push
You are done when
You signed in with your own SSH key, and password sign-in is refused.
ufw is active and denies incoming traffic except SSH and port 8080.
Your SSH settings and notes are on GitHub.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.