Think like an attacker, act like a defender
Security people have a habit: before they defend a server, they look at it the way an attacker would. What can someone reach from outside? What's running that nobody remembers? Who can log in? You can't protect what you don't know is there, so this lesson is about seeing your own server clearly, and then making it smaller and safer.
You will learn
- The rule that comes before everything else: permission
- Threat modelling in four questions
- Your server's attack surface, and the commands that map it on both families
- The defender's habits: less is safer, least privilege, layers
Permission comes first
Everything in this path happens on machines you own or are responsible for: your server, your practice lab, a VM on your own computer. Looking for weaknesses in someone else's computer without written permission is illegal in most countries, even if you "only looked" and changed nothing. Laws like the USA's Computer Fraud and Abuse Act and the UK's Computer Misuse Act are written exactly for that.
Professionals who test other people's systems (penetration testers) always have a signed agreement first that says exactly what they may test, when, and how. No agreement, no testing. That's the whole difference between a security job and a crime.
Only test what you own or have written permission to test. At school or work, the network belongs to someone else: ask first, in writing.
Threat modelling in four questions
You can't protect everything from everyone, so professionals start with four questions (from Adam Shostack's threat-modelling framework):
- What are we working on? A small web server for a club, with a members list.
- What can go wrong? A guessed password, a forgotten service leaking the members list, an old account nobody watches.
- What are we going to do about it? Keys only for SSH, turn off what isn't needed, remove old accounts, keep it patched.
- Did we do a good job? Check again, from the outside, the way an attacker would.
Notice question 2 is about likely problems, not movie hackers. Most real break-ins use something boring: a reused password, an old account, a test server someone forgot to switch off.
Your attack surface
Your attack surface is everything on the server that someone could reach or abuse. The smaller it is, the less can go wrong. Here's how to map it:
| Question | Rocky / RHEL | Ubuntu / Debian |
|---|---|---|
| What's listening for connections? | sudo ss -tlnp same on both | |
| Which services are running? | systemctl list-units --type=service --state=running same on both | |
| Which accounts can log in? | getent passwd | awk -F: '$7 !~ /(nologin|false)$/ {print $1}' same on both | |
| Who is an admin (can use sudo)? | getent group wheel | getent group sudo |
| Anything out of date? | dnf updateinfo list --security | apt list --upgradable |
In ss -tlnp, look at the Local Address column. 127.0.0.1:… or 127.0.0.53%lo:… means only the server itself can connect. 0.0.0.0:… or [::]:… means every network the server is on can reach it. The -p part names the program, which is why it needs sudo.
The defender's habits
- Less is safer. Every service you don't need is a door you don't have to guard. Stop it and disable it:
sudo systemctl disable --now NAME. - Least privilege. Each account gets only what it needs. Old accounts get locked or removed, and admin rights are rare.
- Layers (defence in depth). Assume one protection will fail someday. A strong SSH setup and a firewall and updates and logs mean one mistake isn't the end.
- Patch (Linux Basics 2, lesson 6), watch (Linux Sysadmin, lesson 4) and back up (Linux Sysadmin, lesson 8). Boring, and the best protection there is.
For an account you're not sure about, lock it instead of deleting it: sudo usermod -L NAME (no password logins), sudo usermod -s /sbin/nologin NAME (no shell), and take away admin rights. If someone complains, you can give it back. Once you're sure, sudo userdel -r NAME removes it for good.
Try it: the previous admin's leftovers 🔎
You've just taken over this server. The previous admin said "it's all fine". Map the attack surface, find what doesn't belong, and shrink it.
Quick check
1. Your school's network has a server you think is badly configured. What should you do?
✓ Without permission, even "just looking" can be illegal. Report it; don't test it.
2. ss -tlnp shows 0.0.0.0:3306. What does that tell you?
✓ 0.0.0.0 means "all addresses". A database usually only needs 127.0.0.1.
3. You find a service nobody needs. What fully removes it from the attack surface?
✓ Stop alone is undone by the next reboot. And relying only on the firewall is one layer, not two.