Networking & Security
Ports, Firewalls
& Linux Security
How hackers see your server. What ports are. How firewalls work. How to harden SSH. And how DNS translates google.com into an IP address in 20 milliseconds.
Sections
6
Read time
25 min
Level
Beginner
Hacker scanning your server:
What is a Port?
Your server has one IP address. But many different services run on it simultaneously — SSH, a web server, a database. How does the server know which incoming connection goes to which service? Ports.
Port = the apartment number
192.168.64.3:22 → apartment 22 → SSH lives here
192.168.64.3:80 → apartment 80 → web server lives here
192.168.64.3:5432 → apartment 5432 → PostgreSQL database lives here
When you SSH in: your Mac says "I want to connect to 192.168.64.3, apartment 22"
Did You Know?
Key Takeaway
A port is just a number. It tells the OS "this incoming connection is for THAT service." Without ports, a server could only run one service at a time.
Well-Known Port Numbers
These are standardized — every Linux server uses the same ones. Memorize the common ones:
Warning
Firewalls & ufw
By default, your server accepts connections on ANY port that has a listening service. A firewall sits in front of all those ports and enforces rules — only letting in what you explicitly allow.
❌ WITHOUT firewall
Anyone → port 22 → SSH ✓ connects
Anyone → port 80 → web ✓ connects
Anyone → port 5432 → DB ✓ connects 😱
✅ WITH ufw
Anyone → port 22 → ALLOW ✓
Anyone → port 80 → ALLOW ✓
Anyone → port 5432 → DENY ✗ blocked
ufw commands
Click the Play button above to start!
Watch the commands execute step by step
Key Takeaway
Default ufw policy when enabled: DENY all incoming, ALLOW all outgoing. You explicitly allow what you need. Everything else is blocked.
SSH Hardening
SSH on default settings works but isn't maximally secure. Hardening means changing /etc/ssh/sshd_config to make it harder to attack.
PermitRootLogin noAttacker must first compromise a normal user, then escalate — two steps instead of one.
PasswordAuthentication noOnly SSH keys allowed. Keys are mathematically impossible to brute-force. Passwords can be guessed.
MaxAuthTries 3Disconnect after 3 wrong attempts. Limits how fast an attacker can try passwords.
LoginGraceTime 20Disconnect if not logged in within 20 seconds. Frees connections from automated scanners.
AllowUsers charithOnly this specific user can SSH in. Even if an attacker creates another account, they can't SSH.
Click the Play button above to start!
Watch the commands execute step by step
Fail2ban
Even with hardening, attackers will still try. Fail2ban watches your logs and automatically bans IPs that fail too many times.
How Fail2ban works:
Attacker tries password 1 → fail → logged in /var/log/auth.log
Attacker tries password 2 → fail → logged
Attacker tries password 3 → fail → logged
Fail2ban reads log → "this IP failed 3 times"
Fail2ban adds rule → IP 1.2.3.4 BANNED for 1 hour
Attacker tries password 4 → connection REFUSED ✓
Click the Play button above to start!
Watch the commands execute step by step
Did You Know?
DNS — The Internet's Phonebook
Every website has an IP address. Google is at 142.250.80.46. But nobody remembers that. DNS (Domain Name System) translates domain names into IP addresses automatically.
You → phonebook → "what's John's number?"
Phonebook → "John's number is 555-1234"
You → call 555-1234
DNS is the phonebook. Domain names are names. IP addresses are phone numbers.
How DNS resolution works step by step
You type google.com in your browser
Browser asks the OS: what's google.com's IP?
OS asks the local DNS resolver
On Ubuntu servers, that's systemd-resolved at 127.0.0.53:53
Local resolver checks its cache
Recently looked up? Return cached answer immediately. Otherwise...
Asks a Root DNS server
"Who handles .com domains?"
Root says 'ask Verisign'
Verisign handles all .com registrations
Verisign says 'ask Google's nameserver'
google.com's own authoritative nameserver
Google says '142.250.80.46'
The actual IP address
Your browser connects to 142.250.80.46
All of this takes ~20–50ms. You never see it.
Click the Play button above to start!
Watch the commands execute step by step
Key Takeaway
Port 53 is the DNS port. Your Ubuntu server runs a local DNS resolver (systemd-resolved) at 127.0.0.53:53 — so when apt needs to find archive.ubuntu.com, it uses its own local DNS first.
Key Concepts Flashcards
Click any card to flip and reveal the definition.