Skip to main content

Configuring rules UFW , Iptables

Prerequisites

  • Access Level: root or sudo required.
  • Install tools:
sudo apt update && sudo apt install ufw iptables -y

  • Verify installation:
ufw --version
iptables --version

  • Knowledge: Basic understanding of common ports (SSH 22, HTTP 80, HTTPS 443).

5W+1H Framework

QuestionAnswer
Whatufw (Uncomplicated Firewall) and iptables are tools that filter incoming/outgoing network traffic.
WhyProtect servers from brute-force, DDoS, and unauthorized access.
WhenSet up immediately after provisioning and update when adding services.
WhereRuns at Linux kernel networking layer, not inside WordPress.
WhoSysadmins, DevOps, and WordPress site owners.
HowDefine rules that allow or block traffic on ports or from IPs.

Syntax Breakdown

ufw

ufw [allow|deny|delete] [service|port]

iptables

iptables -A [CHAIN] -p [protocol] --dport [port] -j [ACTION]

Options / Flags

ToolFlagMeaningExample
ufwallowPermit trafficufw allow 80
ufwdenyBlock trafficufw deny 21
ufwdeleteRemove ruleufw delete allow 80
ufwdefaultSet default policyufw default deny incoming
iptables-AAppend ruleiptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables-DDelete ruleiptables -D INPUT -s IP -j DROP
iptables-LList rulesiptables -L -n -v
iptables-FFlush rulesiptables -F
iptables-PSet chain policyiptables -P INPUT DROP

25 Real Commands with Goal, Explanation & Expected Output

UFW Commands (1–16)

1. Check firewall status

sudo ufw status

  • Goal: Verify firewall status.
  • Explanation: Shows rules and whether UFW is active.
  • Output:
Status: active
22 ALLOW Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere

2. Enable UFW

sudo ufw enable

  • Goal: Turn on firewall.
  • Explanation: Activates UFW on boot.
  • Output:
Firewall is active and enabled on system startup

3. Disable UFW

sudo ufw disable

  • Goal: Stop firewall temporarily.
  • Explanation: Useful for debugging.
  • Output:
Firewall stopped and disabled on system startup

4. Allow SSH

sudo ufw allow 22

  • Goal: Keep SSH accessible.
  • Explanation: Prevents accidental lockout.
  • Output:
Rule added
Rule added (v6)

5. Allow HTTP

sudo ufw allow 80

  • Goal: Open port 80.
  • Explanation: Required for WordPress websites.
  • Output:
Rule added
Rule added (v6)

6. Allow HTTPS

sudo ufw allow 443

  • Goal: Enable SSL/TLS.
  • Explanation: Secure site traffic.
  • Output:
Rule added
Rule added (v6)

7. Deny FTP

sudo ufw deny 21

  • Goal: Block insecure FTP.
  • Explanation: Forces SFTP instead.
  • Output:
Rule added
Rule added (v6)

8. Delete a rule

sudo ufw delete allow 80

  • Goal: Remove rule.
  • Explanation: Useful if rule is no longer needed.
  • Output:
Rule deleted
Rule deleted (v6)

9. Allow by service name

sudo ufw allow ssh

  • Goal: Open SSH via service name.
  • Explanation: Equivalent to ufw allow 22.
  • Output:
Rule added
Rule added (v6)

10. Allow port range

sudo ufw allow 3000:3010/tcp

  • Goal: Open multiple ports.
  • Explanation: Useful for apps needing ranges.
  • Output:
Rule added
Rule added (v6)

11. Allow specific IP

sudo ufw allow from 203.0.113.5

  • Goal: Whitelist a trusted IP.
  • Explanation: Restricts access to one source.
  • Output:
Rule added

12. Allow IP to specific port

sudo ufw allow from 203.0.113.5 to any port 22

  • Goal: Restrict SSH to one IP.
  • Explanation: Extra security layer.
  • Output:
Rule added

13. Deny specific IP

sudo ufw deny from 203.0.113.200

  • Goal: Block attacker IP.
  • Explanation: Prevents brute force.
  • Output:
Rule added

14. Default deny incoming

sudo ufw default deny incoming

  • Goal: Block all by default.
  • Explanation: Secure baseline.
  • Output:
Default incoming policy changed to 'deny'

15. Default allow outgoing

sudo ufw default allow outgoing

  • Goal: Allow server connections.
  • Explanation: Needed for updates.
  • Output:
Default outgoing policy changed to 'allow'

16. Reset firewall

sudo ufw reset

  • Goal: Reset UFW.
  • Explanation: Wipes all rules.
  • Output:
Resetting all rules to installed defaults

iptables Commands (17–25)

17. List rules with counters

sudo iptables -L -n -v

  • Goal: Inspect firewall rules.
  • Explanation: Shows packet/byte counters.
  • Output:
Chain INPUT (policy ACCEPT)
pkts bytes target prot opt in out source destination

18. Allow HTTP

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

  • Goal: Open HTTP.
  • Explanation: Appends rule to INPUT chain.
  • Output: (no output, confirm with iptables -L)

19. Allow HTTPS

sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

  • Goal: Enable SSL/TLS.
  • Explanation: Allows encrypted traffic.
  • Output: (silent, verify with iptables -L)

20. Allow SSH from one IP

sudo iptables -A INPUT -p tcp -s 203.0.113.5 --dport 22 -j ACCEPT

  • Goal: Restrict SSH.
  • Explanation: Only one IP can connect.
  • Output: (silent)

21. Block malicious IP

sudo iptables -A INPUT -s 203.0.113.200 -j DROP

  • Goal: Drop attacker.
  • Explanation: Packets from that IP ignored.
  • Output: (silent)

22. Drop all incoming by default

sudo iptables -P INPUT DROP

  • Goal: Default deny stance.
  • Explanation: Requires explicit allows.
  • Output: (silent)

23. Save rules

sudo sh -c "iptables-save > /etc/iptables/rules.v4"

  • Goal: Persist rules.
  • Explanation: Ensures rules survive reboot.
  • Output: (file created)

24. Delete a rule

sudo iptables -D INPUT -s 203.0.113.200 -j DROP

  • Goal: Remove rule.
  • Explanation: Reverts blocking.
  • Output: (silent)

25. Flush all rules

sudo iptables -F

  • Goal: Clear all firewall rules.
  • Explanation: Resets everything — dangerous.
  • Output: (silent, verify with iptables -L → empty)

Use Case Scenarios

ScenarioCommand(s)Why Useful
Prevent SSH lockoutufw allow 22 then ufw enableKeeps access open
WordPress hardeningufw allow 80, ufw allow 443, ufw default deny incomingOnly web ports exposed
Block insecure FTPufw deny 21Prevents weak logins
Restrict SSH by IPufw allow from 203.0.113.5 to any port 22Stops brute-force
Block attackersiptables -A INPUT -s BAD_IP -j DROPMitigate abuse fast

Benefits

  • Stronger server security.
  • Flexibility: ufw for simple, iptables for advanced.
  • Granular control over traffic.
  • Logging and auditing capabilities.

Best Practices

  1. Always open SSH before enabling firewall.
  2. Restrict to essential ports only.
  3. Save iptables rules for persistence.
  4. Test rules before production.
  5. Pair with fail2ban for brute-force prevention.

Quick Lab

  1. Allow web + SSH:
sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443

  1. Block FTP:
sudo ufw deny 21

  1. Enable firewall:
sudo ufw enable

  1. Verify rules:
sudo ufw status

Cheat Sheet

TaskCommand
Allow SSHufw allow 22
Allow HTTPufw allow 80
Allow HTTPSufw allow 443
Block FTPufw deny 21
List rulesiptables -L -n -v
Block IPiptables -A INPUT -s IP -j DROP
Delete ruleiptables -D INPUT -s IP -j DROP
Flush rulesiptables -F

Mini-Quiz

  1. Which UFW command blocks all incoming by default?
  2. How do you allow SSH only from a specific IP?
  3. What’s the iptables command to delete a rule?
  4. Why allow SSH before enabling firewall?
  5. How do you persist iptables rules after reboot?

This is now fully consistent for all 25 commands with goal, explanation, and expected output.

Would you like me to also create a Troubleshooting Matrix (problem → command to check → possible fix) as the final layer of this module?