Chapter 5 / Series
Chapter 5 № 05 · 2026

Chapter 5 — The Basics of Defense

Shrink the attack surface, automate updates

Why the Defense Chapter Comes Before the Run-a-Service Chapter

In a typical primer, this is about where a "let's run something" chapter shows up. Stand up a web server, start sharing files, publish your own app — that is where the fun begins.

This book puts the defense chapter first. The reason is simple: the moment you expose a server to the world, bots from all over the world start knocking on the door in unison. That is not a scare tactic. While you have not exposed anything yet, take a peek at the SSH login failures.

sudo journalctl -u ssh --since "1 hour ago" | grep -i failed

If your server lives only inside a home LAN, this should be nearly silent. The failure log will be, at most, the few lines where you mistyped your own password. There is value in seeing this "quiet" with your own eyes once. Run the same command after you go public, and failure logs from unknown IPs pile up by the minute. That contrast is what makes you understand, in your body, what this chapter is defending.

# A minimal Debian 13 install has no /var/log/auth.log. Read the record with journalctl

How heavy your defense needs to be depends on where your server sits. Sorted out, there are two levels.

If you are LAN-only now, learn your defenses while it is still quiet. Scrambling to learn after you go public is like searching for how to fit a lock while the door stands wide open.

Section 1 — The Idea of an Attack Surface

Security talk is full of jargon and makes people brace themselves, but the idea underneath is startlingly simple.

Software you never installed cannot be attacked. A service that is not running is not a hole. This is called "shrinking the attack surface." The first step of defense is not adding clever settings; it is subtracting what you do not need. The stance repeated in "Design by Subtraction" works here too.

First, look at every "ear" your server has open to the outside.

# Ports and processes currently listening (LISTEN)
ss -tlnp

The output looks like this.

State   Recv-Q  Send-Q  Local Address:Port   Peer Address:Port  Process
LISTEN  0       128     0.0.0.0:22           0.0.0.0:*          users:(("sshd",pid=701,fd=3))
LISTEN  0       4096    127.0.0.1:631        0.0.0.0:*          users:(("cupsd",pid=812,fd=7))

What matters here is to read every single line and what it means. 0.0.0.0:22 means "listening on port 22 on every network interface" — it can be reachable from outside. But listening and being reachable are different things. Whether it is reachable depends on the firewall, so try it from another machine. 127.0.0.1:631 means "listening on localhost only" — not reachable from outside. Once you see this difference, you can see which services are truly exposed.

Ask Claude ①: Decipher the ss -tlnp Output

I ran ss -tlnp on my Debian 13 server and got this output:

[paste the ss -tlnp output as-is]

For each line, explain what service it is and whether it is reachable from outside (the internet) or local only. Then, given my server's purpose [e.g., LAN-only file sharing], classify which ones are "safe to close" and which "must not be closed," with reasons.

Having Claude walk through the ss output line by line is the single most valuable use in this chapter. What looks to your eyes like a string of symbols turns, once handed to Claude, into a map of "this ear is needed, this ear can be sealed."

Services that turn out to be unnecessary: stop them, and disable auto-start too.

# Example: the print server cups was unneeded on a server box
sudo systemctl disable --now cups
# Confirm it is no longer listening
ss -tlnp

disable --now does both "stop it now" and "do not start it from now on" at once. One less item on the attack surface.

Section 2 — Firewall: Deny by Default, In and Out

Once you have subtracted the attack surface, two things remain to decide: "of the ears that remain, who do we answer?" and "where may this machine speak to, outside?" That is the firewall's job. On Debian, ufw (Uncomplicated Firewall) is easy to handle.

The idea is one thing. Deny everything by default, incoming and outgoing. Allow only what is needed, by name. Most introductions stop at "deny in, allow out." This series closes outgoing too. The reason lies in the intrusions of 2025–2026. What an attacker does after getting in is send stolen data out, and talk to a command server outside over HTTPS. With outgoing open, however hard you make the way in, once they are in everything after that passes freely. With outgoing closed, even an intruder has no road out. The US NIST firewall guideline (SP 800-41 Rev.1, 2009) likewise asks that the default policy deny both inbound and outbound.

Decide first. The parties this server needs to speak to are normally just five.

Whenever a need to reach anything else arises, write down one line — "what, to where, why" — each time you add it.

sudo apt install ufw

Never get the order wrong here. If you enable "deny everything" before allowing SSH, you lock out the very SSH session you are connected with. Always write allow ssh and the outgoing allows first, then enable.

# 1. Policy first: deny by default, incoming and outgoing
sudo ufw default deny incoming
sudo ufw default deny outgoing

# 2. Allow SSH first so you do not lock yourself out (most important)
sudo ufw allow ssh

# 3. Allow the outgoing destinations by name (DNS, NTP, apt and the web, DHCP)
sudo ufw allow out 53
sudo ufw allow out 123/udp
sudo ufw allow out 80,443/tcp
sudo ufw allow out 67/udp

# 4. Only now, enable it
sudo ufw enable

# 5. Check the current rules
sudo ufw status verbose

If the Default: line of ufw status verbose reads deny (incoming), deny (outgoing), both directions are denied by default.

The DHCP line is required if you chose a DHCP reservation on the router in Chapter 3. With a reservation, the server still gets its address over DHCP. Reading ufw's default rules (/usr/share/ufw/iptables/before.rules), there is a line that accepts the DHCP reply, but none that lets the request go out. With outgoing closed, the request to renew the address when its lease runs out is blocked. Whether the address then actually lapses and the machine drops off the network depends on how the DHCP client is built, so put this line in. If you fixed the address on the server itself, you do not need this line.

Make it a habit to pause right before enable and confirm that allow ssh and allow out 53 are in place. The replies of the SSH session you are on pass automatically as an "established connection," so closing outgoing does not cut it. That, however, is about SSH coming in from outside. SSH going out from this machine (a git push to GitHub on port 22, say) does stop. And forget DNS, and the next apt stops, unable to resolve a name. That is the first tangible sign that you have closed outgoing. Forget allow ssh on a remote server, and you cannot get back in short of a physical console or recovery means.

Know in advance what stops when outgoing is closed. apt, git clone, and certificate issuance are covered by ports 80 and 443 above. When a service added in a later chapter needs another port outward (say, 587 for sending mail), add one line in that chapter. If something stops silently, suspect the firewall first and read the kernel log.

# Who tried to go where, and was stopped
sudo journalctl -k --grep UFW

On a Machine Already in Use, Close Outgoing in Two Steps

On a freshly installed machine you can type the steps above in one go. On a machine where things already run, split it in two. The first step is incoming only (default deny incoming, allow ssh, enable). Outgoing is closed in a second step, on another day.

Before the second step, count where this machine is talking to right now.

# Established connections and the processes holding them
sudo ss -tnp state established

Incoming ssh shows up here too. Lines whose local port (after the : in the left-hand address) is high — 32768 or more — are connections going out from this machine. If anything other than the five above shows up, find out what it is, and add allow out first for what is needed. Measured on the writer's desk PC, it was talking out to port 22 (git push) and two other ports. Closing with only the three lines for DNS, NTP, and 80/443 stopped the push and outgoing ssh.

When you close it, set up the way back first. It is the same idea as Chapter 4's "Leave Yourself One Way Back."

# In 10 minutes, set outgoing back to "allow" automatically
sudo systemd-run --unit=ufw-undo --on-active=10min /usr/sbin/ufw default allow outgoing

# Close outgoing
sudo ufw default deny outgoing

# Check: do updates get through, and what was blocked?
sudo apt update
sudo journalctl -k --since -10min --grep 'UFW BLOCK'

# If all is well, cancel the way back
sudo systemctl stop ufw-undo.timer

If you lose a connection after closing, outgoing opens again within 10 minutes. Add the missing allow, then try again.

For a service used only inside your LAN, you can write "allow only from a specific network." This narrows the attack surface further.

# Example: allow web access only from the home LAN (192.168.1.0/24)
sudo ufw allow from 192.168.1.0/24 to any port 80
# To allow SSH only from the home LAN (for a non-public server)
sudo ufw allow from 192.168.1.0/24 to any port 22
# If the server must speak to LAN devices (a NAS or a printer), name the outgoing side too
sudo ufw allow out to 192.168.1.0/24

from 192.168.1.0/24 means "only from devices within this subnet." It becomes a setting that the outside cannot reach in the first place.

By now the roads out are narrowed to five port numbers. Port 443, however, is open to every process and every destination. What the intrusions of 2025–2026 used was exactly this "443 anyone can pass through." The next step narrows this hole per service. When you run an app as a systemd unit in Chapter 8, add these lines in the same [Service] place as the sandbox settings.

# This service does not speak to the outside (write only the needed destinations in IPAddressAllow)
IPAddressDeny=any
IPAddressAllow=localhost

Only apt and Caddy truly need port 443. Give every other service IPAddressDeny=any, and it holds no right to go outside. The firewall guards the machine's doors; the unit guards each service's doors. Narrow twice.

Ask Claude ②: Design a Full ufw Ruleset from Your Use Case

I am setting up ufw on my Debian 13 server. My usage is as follows:

  • Location: [e.g., inside my home LAN / on a VPS exposed to the internet]
  • Services I plan to run: [e.g., SSH, file sharing (Samba), a web server later]
  • IP range of my management devices: [e.g., home LAN is 192.168.1.0/24]
  • What this server needs to reach outside: [e.g., apt updates, Let's Encrypt certificates, sending mail]

With deny-by-default for both incoming and outgoing, and keeping the order that avoids a lockout, give me the ufw commands to run from top to bottom. Add a one-line comment on what each command allows or denies. For each outgoing allow, say in the comment what it lets reach where.

Convey your use case accurately, and the full ruleset drops straight onto your desk. Then you vet it line by line and run it. With a firewall, a one-character mistake leads directly to a lockout, so you need the stance of reviewing it rather than swallowing it whole. For each outgoing line in particular, ask yourself whether it truly needs to reach there.

Section 3 — Automating Updates

The defense that works the most yet is forgotten the most is keeping updates applied. Most attacks aim at known holes for which a patch has long since shipped. The hole is closed, but it stays open because you did not update — this is the most frequent accident.

A server is often not sitting in front of a screen. So instead of relying on manual updates, make it so that security updates at least get applied automatically. Debian has unattended-upgrades.

sudo apt install unattended-upgrades

It is configured by default to auto-apply security updates, but confirm and enable it interactively.

sudo dpkg-reconfigure -plow unattended-upgrades

Answer Yes to "Enable automatic updates?" and from then on security updates apply quietly. You can read what got applied later with cat /var/log/unattended-upgrades/unattended-upgrades.log.

That said, some updates "do not take effect until you reboot" — kernel and glibc updates among them. Whether a reboot is needed shows up as the presence of a file.

# If this file exists, a reboot is pending
ls -l /var/run/reboot-required 2>/dev/null && cat /var/run/reboot-required

# Tells you which services are still holding old libraries
sudo apt install needrestart
sudo needrestart

needrestart handles what can be done without a reboot by just restarting the service, and tells you only about what truly needs a reboot. When you would rather not reboot a server needlessly, this distinction pays off.

Ask Claude ③: Decipher Suspicious Lines in the ssh Log

On my Debian 13 server, the output of sudo journalctl -u ssh --since "today" had lines like these:

[paste the failure logs / unfamiliar lines as-is]

Is this ordinary brute-forcing by bots, or a sign of something targeted? Line by line, explain what each means (source, attempted username, reason for failure). If there is action I should take now, tell me in order of severity.

The "hand the log to Claude as-is" craft you picked up in the main series' Chapter 8 troubleshooting moves here into a security context. Do not fear lines you cannot read — paste them. Claude sorts them into "just bot noise" versus "a sign worth caring about."

Section 4 — fail2ban, the Watchdog

Finally, post a watchdog. fail2ban is a tool that watches logs and, when login failures from the same IP keep coming over a short span, blocks that IP for a set time. It physically slows brute-force attacks.

Let me make the priority clear here. fail2ban is a supplement, not the lead. The heart of SSH defense is what you did in Chapter 4: key authentication plus disabling password authentication. Once passwords are disabled, no amount of brute-forcing gets through. On top of that, fail2ban is posted to chase off bots that keep polluting the logs and to keep those logs readable. The order is "key auth first, fail2ban after."

sudo apt install fail2ban

The Debian way is not to edit jail.conf directly but to write only your overrides into jail.local. The minimal config for SSH is just this.

# Create /etc/fail2ban/jail.local (contents below)
sudo tee /etc/fail2ban/jail.local > /dev/null <<'EOF'
[DEFAULT]
# How long to ban (10 minutes)
bantime = 10m
# Within this window
findtime = 10m
# After this many failures
maxretry = 5

[sshd]
enabled = true
EOF

sudo systemctl restart fail2ban

Whether it is running, and whether it has banned anyone, you check with the dedicated client.

sudo fail2ban-client status sshd

The IPs listed under Banned IP list: are who is currently shut out. On a LAN-only server this will stay empty for days. On a public server, within a few hours a handful start lining up. Proof that the watchdog is barking properly.

Ask Claude ④: Inspect Whether Your Server's Defenses Are Complete

Here is the current state of my Debian 13 server's defenses:

$ ss -tlnp
[output]
$ sudo ufw status verbose
[output]
$ sudo fail2ban-client status sshd
[output]

SSH key auth is [enabled / not yet], password auth is [disabled / not yet]. Placement is [LAN-only / planning to go public]. In this state, point out what is missing from my defenses, what is out of order, and what is excessive. Inspect whether there is a fatal hole if I go public right now.

Hand over several command outputs together, and Claude can inspect "attack surface, firewall, watchdog, and SSH itself" as a single picture. Ask Claude for a final check before you press the publish button. It catches the "ordering holes" you would miss on your own.

Summary

What you did in this chapter:

  1. Peeked at pre-publication SSH failure logs and recorded the "quiet" as a baseline.
  2. Read every open ear with ss -tlnp and disable --now'd the services you did not need.
  3. Started ufw from "deny by default, in and out," allowed the four outgoing destinations (DNS, NTP, apt, web) by name, and learned LAN-restricted allows too.
  4. Automated security updates with unattended-upgrades and learned to tell when a reboot is needed.
  5. Posted fail2ban as a watchdog and sorted out its priority relative to key authentication.

What you hold now:

The foundation of defense is in place. In Chapter 6, "The Service as a Unit," you finally run something. The insides of a server are, in the end, "a collection of processes that systemd looks after." You will learn to handle starting, stopping, auto-start, logs, and failure response all through the same shape — the unit called a "service." The very verb you typed with systemctl disable --now in this chapter is the doorway into the next.


The server sub-series can be navigated from Learning Debian with Claude — Server Edition. The main desktop series is at all chapters. Comments and discussion go to the Facebook group: AISeed — Biodiversity, Food, AI and Life.