Aim :
Learn how to use iptables for configuring the IP packet filter rules on a Linux system.
iptables Commands: A Step-by-Step Guide
iptables is a powerful tool for configuring the Linux kernel firewall, providing packet filtering, network address translation (NAT), and other packet mangling. Follow these steps to use iptables for configuring firewall rules.
Guidance on How to Use:
Prerequisite:
Ensure that your Linux system has iptables installed.
iptables Commands:
- List all current rules:
sudo iptables -L
- Allow incoming traffic on a specific port:
Example:sudo iptables -A INPUT -p [protocol] --dport [port] -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
(Allow SSH) - Deny incoming traffic on a specific port:
Example:sudo iptables -A INPUT -p [protocol] --dport [port] -j DROP
sudo iptables -A INPUT -p udp --dport 53 -j DROP
(Deny DNS) - Allow outgoing traffic on a specific port:
Example:sudo iptables -A OUTPUT -p [protocol] --dport [port] -j ACCEPT
sudo iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
(Allow HTTP) - Deny outgoing traffic on a specific port:
Example:sudo iptables -A OUTPUT -p [protocol] --dport [port] -j DROP
sudo iptables -A OUTPUT -p udp --dport 123 -j DROP
(Deny NTP) - Save iptables rules:
sudo iptables-save > /etc/iptables/rules.v4
- Restore iptables rules from a file:
sudo iptables-restore < /etc/iptables/rules.v4
Verification:
Verify the applied iptables rules by listing them:
sudo iptables -L