iptables
This cheatsheet provides a collection of iptables.
iptables
This document provides a comprehensive list of iptables commands, crucial for network administrators and security professionals. iptables is the default firewall tool on Linux systems, used for network packet filtering and manipulation.
The cheat sheet covers basic to advanced iptables functionalities, including setting up firewalls, NAT, port forwarding, and managing traffic rules. It serves as a handy guide for securing Linux systems and managing network traffic flows. Given the powerful nature of iptables, it is advised to use these commands with caution, as incorrect usage can lead to network outages or security vulnerabilities.
- List All Rules
sudo iptables -L
- Lists all active rules.
- List Rules with Numbers
sudo iptables -L --line-numbers
- Lists rules with line numbers for easier management.
- Delete Rule by Number
sudo iptables -D INPUT [LINE_NUMBER]
- Deletes a specific rule from the INPUT chain.
- Set Default Policy
sudo iptables -P [CHAIN] [POLICY]
- Sets the default policy (e.g., ACCEPT, DROP) for a chain (e.g., INPUT, OUTPUT).
- Allow Specific Port (TCP)
sudo iptables -A INPUT -p tcp --dport [PORT] -j ACCEPT
- Allows incoming traffic on a specific TCP port.
- Allow Specific Port (UDP)
sudo iptables -A INPUT -p udp --dport [PORT] -j ACCEPT
- Allows incoming traffic on a specific UDP port.
- Drop Traffic from an IP Address
sudo iptables -A INPUT -s [IP_ADDRESS] -j DROP
- Blocks all incoming traffic from a specific IP address.
- Allow Traffic from an IP Address
sudo iptables -A INPUT -s [IP_ADDRESS] -j ACCEPT
- Allows all incoming traffic from a specific IP address.
- Reject Traffic on a Port
sudo iptables -A INPUT -p tcp --dport [PORT] -j REJECT
- Rejects traffic on a specific port.
- Save iptables Rules
sudo iptables-save > /etc/iptables/rules.v4
- Saves the current rules to a file (Debian-based systems).
- Flush All Rules
sudo iptables -F
- Removes all rules.
- Log Dropped Packets
sudo iptables -A INPUT -j LOG --log-prefix "IPTables-Dropped: " --log-level 4
- Logs dropped packets for debugging.
- Limit Connections per Second
sudo iptables -A INPUT -p tcp --dport [PORT] -m limit --limit [RATE] -j ACCEPT
- Limits the number of connections per second to a port.
- Port Forwarding
sudo iptables -t nat -A PREROUTING -p tcp --dport [PORT] -j DNAT --to-destination [DEST_IP]:[DEST_PORT]
- Forwards traffic from one port to another IP and port.
- Masquerade (NAT)
sudo iptables -t nat -A POSTROUTING -o [OUT_INTERFACE] -j MASQUERADE
- Enables NAT for outgoing traffic on an interface.
- Drop Invalid Packets
sudo iptables -A INPUT -m state --state INVALID -j DROP
- Drops packets that are invalid.
- Allow Established and Related Connections
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- Allows incoming traffic related to established connections.
- Block Outgoing Traffic to a Domain
sudo iptables -A OUTPUT -p tcp -d [DOMAIN] --dport 80 -j DROP
- Blocks outgoing HTTP traffic to a specific domain.
- Redirect Traffic to Another Port
sudo iptables -t nat -A PREROUTING -p tcp --dport [PORT] -j REDIRECT --to-port [NEW_PORT]
- Redirects traffic from one port to another port on the same machine.
- Block Ping Requests
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
- Blocks ICMP echo requests (ping).
- Allow Traffic on Multiple Ports
sudo iptables -A INPUT -p tcp -m multiport --dports [PORT1],[PORT2],[PORT3] -j ACCEPT
- Allows traffic on multiple specified ports.
- Rate Limiting Incoming Connections
sudo iptables -A INPUT -p tcp --dport [PORT] -m state --state NEW -m recent --set
sudo iptables -A INPUT -p tcp --dport [PORT] -m state --state NEW -m recent --update --seconds [SECONDS] --hitcount [HITCOUNT] -j DROP
- Limits new connections to a port within a given timeframe.
- Block Traffic from a Specific Network
sudo iptables -A INPUT -s [NETWORK/MASK] -j DROP
- Blocks all incoming traffic from a specific network.
- Allow Traffic Only from a Specific Network
sudo iptables -A INPUT -s [NETWORK/MASK] -j ACCEPT
- Allows traffic only from a specific network.
- Log New Connections
sudo iptables -A INPUT -m state --state NEW -j LOG --log-prefix "New Connection: "
- Logs new incoming connections.
- Drop Outgoing Traffic to a Specific Port
sudo iptables -A OUTPUT -p tcp --dport [PORT] -j DROP
- Blocks outgoing traffic to a specific port.
- Redirect All HTTP Traffic to HTTPS
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 443
- Redirects all HTTP traffic to HTTPS.
- Block Outgoing SMTP Mail
sudo iptables -A OUTPUT -p tcp --dport 25 -j REJECT
- Blocks outgoing SMTP mail.
- Allow SSH Access from a Specific Network
sudo iptables -A INPUT -p tcp --dport 22 -s [NETWORK/MASK] -j ACCEPT
- Allows SSH access only from a specific network.
- Block Outgoing Telnet
sudo iptables -A OUTPUT -p tcp --dport 23 -j DROP
- Blocks outgoing Telnet connections.
Follow me on : Medium Linkedin Researchgate