How to configure networking with iptables and ufw

· Category: Linux

Short answer

iptables is the low-level Linux firewall; ufw provides a simpler frontend for common rules.

Steps

  1. Enable ufw:
sudo ufw enable
  1. Allow SSH:
sudo ufw allow ssh
  1. Allow a port:
sudo ufw allow 8080/tcp
  1. Deny all incoming:
sudo ufw default deny incoming
  1. List rules:
sudo ufw status numbered

Tips

  • Always allow SSH before enabling the firewall to avoid lockout.
  • iptables rules are not persistent by default; use iptables-persistent or equivalent.
  • ufw is available on Debian/Ubuntu; firewalld is common on RHEL/CentOS.

Common issues

  • Rule ordering matters in iptables; first match wins.
  • Accidental lockout: use console access or scheduled disable commands to recover.