This page looks best with JavaScript enabled

Basic Iptables

 ·  🎃 kr0m

Iptables is the network traffic system offered by Linux, it is a complex system but here we will indicate the basic configuration steps for a workstation, no advanced operation will be performed, only incoming and outgoing traffic will be filtered on the local computer.

This is a basic introduction to iptables from the point of view of a server/workstation.

I delete all entries:

iptables -F

By default, I don’t accept anything:

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

I accept all established and related connections so that services like ftp work correctly:

iptables -A INPUT -m state –state ESTABLISHED -j ACCEPT
iptables -A INPUT -m state –state RELATED -j ACCEPT
iptables -A OUTPUT -m state –state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state –state RELATED -j ACCEPT
iptables -A FORWARD -m state –state ESTABLISHED -j ACCEPT
iptables -A FORWARD -m state –state RELATED -j ACCEPT

I accept anything from loopback:

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A FORWARD -o lo -j ACCEPT

I define the rules:

iptables -A OUTPUT -p udp –dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 443 -j ACCEPT
iptables -A OUTPUT -p icmp -j ACCEPT
iptables -A OUTPUT -p tcp –dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 5222 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 1863 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 6667 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 20 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 21 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 3389 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 139 -j ACCEPT
iptables -A OUTPUT -p tcp –dport 445 -j ACCEPT

The allowed services are:

  • DNS
  • HTTP
  • HTTPS
  • ICMP
  • SSH
  • GTalk
  • AMSN
  • IRC
  • FTP-DATA
  • FTP
  • Terminal Server (Windows Remote Desktop)
  • Samba

To save the rules, execute the following command:

iptables-save > PATH

To restore them:

iptables-restore PATH

To delete a rule, use the following command:

iptables -D OUTPUT/INPUT "rule number"

To insert rules at a specific position, use the following command:

iptables -I OUTPUT/INPUT "rule number"

You can view the entries with the following command:

iptables -L -n -v –line-number

Save the rules:

/etc/init.d/iptables save

Add iptables to the startup:

rc-update add default iptables

Remember to load the necessary modules unless everything has been compiled into the kernel itself.

If you liked the article, you can treat me to a RedBull here