UFW Firewall Setup on Linux — Secure Your Server in 10 Minutes
UFW (Uncomplicated Firewall) is the easiest way to lock down your Linux server. This guide covers enabling UFW, allowing only the ports you need, blocking everything else, and verifying your rules.
UFW Firewall Setup on Linux
UFW (Uncomplicated Firewall) is the default firewall tool on Ubuntu and most Debian-based distros. It wraps iptables in a much simpler interface and is the fastest way to lock down a Linux server.
Step 1 — Install UFW
UFW is pre-installed on most Ubuntu systems. Check if it's there:
sudo apt install ufw
Step 2 — Check Current Status
sudo ufw status verbose
If it says Status: inactive, UFW is installed but not yet enforcing rules.
Step 3 — Set Default Policies
Before enabling, set your default behavior:
sudo ufw default deny incoming
sudo ufw default allow outgoing
This blocks all inbound traffic by default and allows all outbound. You then open only what you need.
Step 4 — Allow SSH (Critical — Do This Before Enabling)
If you're connected via SSH, allow it before enabling UFW or you'll lock yourself out:
sudo ufw allow ssh
Or by port number:
sudo ufw allow 22/tcp
If you use a non-standard SSH port (e.g., 2222):
sudo ufw allow 2222/tcp
Step 5 — Allow Other Ports You Need
Web Server
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
Blockchain Nodes
sudo ufw allow 30303/tcp # Ethereum P2P
sudo ufw allow 16110/tcp # Kaspa RPC / P2P
sudo ufw allow 8333/tcp # Bitcoin P2P
Custom App Port
sudo ufw allow 8080/tcp
Step 6 — Enable UFW
sudo ufw enable
You'll be prompted to confirm. Type y and press Enter.
Step 7 — Verify Your Rules
sudo ufw status numbered
Example output:
Status: active
To Action From
-- ------ ----
[ 1] 22/tcp ALLOW IN Anywhere
[ 2] 80/tcp ALLOW IN Anywhere
[ 3] 443/tcp ALLOW IN Anywhere
Deleting Rules
Delete a rule by number (use status numbered to find the number):
sudo ufw delete 2
Or delete by rule spec:
sudo ufw delete allow 80/tcp
Disable UFW (If Needed)
sudo ufw disable
This stops enforcement without deleting your rules.
Rate Limiting (Brute Force Protection)
Limit connection attempts to SSH (blocks IPs that attempt 6+ connections in 30 seconds):
sudo ufw limit ssh
Quick Reference
| Goal | Command |
|---|---|
| Check status | sudo ufw status verbose |
| Default deny inbound | sudo ufw default deny incoming |
| Allow SSH | sudo ufw allow ssh |
| Allow port | sudo ufw allow 8080/tcp |
| Enable UFW | sudo ufw enable |
| Disable UFW | sudo ufw disable |
| Delete rule | sudo ufw delete [number] |
| Rate limit SSH | sudo ufw limit ssh |
UFW is fast to configure and straightforward to maintain — every Linux server exposed to the internet should have it enabled.
Derrk Samuel
Web3, crypto, and Linux PC insights — practical guides, security tips, mining setups, blockchain tools, and decentralized tech strategies for techs.
Related Articles
Discussion
Leave a comment
No comments yet. Be the first to start the discussion!

