Intermediate 24 min readModule: Module 7: Linux Networking, SSH Hardening & UFW Firewalls
Linux Networking, SSH Key Hardening & UFW Firewall
Inspect network interfaces, socket statistics (ss), configure SSH public-key authentication, disable root password logins, and manage packet filtering with UFW.
What You Will Learn in This Lesson
- Inspecting network addresses and routes with ip addr and ip route
- Auditing listening network ports and TCP sockets with ss -tulpn
- Hardening OpenSSH Server configuration (/etc/ssh/sshd_config)
- Configuring stateful packet filtering rules with UFW (Uncomplicated Firewall)
Introduction & Core Concept
Linux networking is governed by the kernel's network stack and the Netfilter packet filtering subsystem. Securing an internet-facing Linux server requires restricting listening ports, enforcing cryptographic SSH key authentication, and configuring firewall rules to reject unauthorized traffic.
WHY DOES THIS MATTER IN THE REAL WORLD?
Public cloud servers are scanned thousands of times daily by automated botnets attempting SSH brute-force attacks. Hardening SSH and maintaining strict firewall rules is the first line of defense against network intrusion.
Syntax & Structure
bash
ss -tulpnufw default deny incomingufw allow 22/tcpufw enablessh-keygen -t ed25519Securing Server Ports and Hardening SSH Configuration
bashbash
12345678910111213141516171819202122232425262728293031#!/usr/bin/env bash# Server Network Inspection & UFW Firewall Hardeningecho "=== 1. Active Listening Network Sockets ==="sudo ss -tulpn | grep LISTENecho -e "=== 2. Configuring UFW Firewall Baseline ==="# Default: Deny all inbound, allow all outboundsudo ufw default deny incomingsudo ufw default allow outgoing# Allow essential web and administration portssudo ufw allow 22/tcp comment "SSH Administration"sudo ufw allow 80/tcp comment "HTTP Web"sudo ufw allow 443/tcp comment "HTTPS Secure Web"# Enable Firewallsudo ufw --force enablesudo ufw status verboseecho -e "=== 3. Recommended OpenSSH Hardening Settings (/etc/ssh/sshd_config) ==="cat << 'EOF'# Key Hardening Directives:# PasswordAuthentication no# PermitRootLogin prohibit-password# PubkeyAuthentication yes# X11Forwarding no# MaxAuthTries 3EOF
Line-by-Line Technical Breakdown
1ED25519 Keys: Modern cryptography recommends `ssh-keygen -t ed25519` over older RSA keys because ED25519 provides faster signature verification, smaller key sizes (68 characters), and stronger resistance to side-channel attacks.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[BASH]
BASH SOURCE EDITOR
Interactive Live CodeCommon Mistakes & How to Avoid Them
#1: Enabling UFW firewall before allowing port 22/SSH, locking the administrator out of the server.
Always whitelist your SSH administration port BEFORE enabling the firewall on remote cloud servers.
Incorrect / Antipattern
sudo ufw default deny incoming
sudo ufw enableCorrect / Professional Solution
sudo ufw allow 22/tcp
sudo ufw enableIndustry Best Practices & Professional Standards
- Use modern ED25519 SSH keys with a passphrase instead of traditional password logins.
- Disable PasswordAuthentication and PermitRootLogin in /etc/ssh/sshd_config.
- Regularly audit listening network ports using ss -tulpn to ensure no unauthorized processes are exposed.
Lesson Summary & Core Takeaways
- Use `ss -tulpn` to identify all network services listening on open ports.
- Enforce public-key authentication for SSH and disable root password logins.
- Configure UFW to deny incoming traffic by default and whitelist only required ports.