iptables's Examples

by using this command we can list number of rules in the working environment

iptables -L

Explanation:

  • -L lists all rules.
  • -v shows detailed information.
  • -n displays numerical IP addresses (faster output).
  • --line-numbers shows rule numbers for easier deletion.

sudo iptables -L -v -n
or
sudo iptables -L --line-numbers

Explanation:
Allows SSH connections on port 22.

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

Explanation:
Blocks all traffic from 192.168.1.100.

sudo iptables -A INPUT -s 192.168.1.100 -j DROP

Explanation:
Allows web traffic on port 80 (HTTP) and port 443 (HTTPS).

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

Explanation:
Blocks incoming traffic on port 8080.

sudo iptables -A INPUT -p tcp --dport 8080 -j DROP

Explanation:
Allows unrestricted traffic from 192.168.1.50.

sudo iptables -A INPUT -s 192.168.1.50 -j ACCEPT

Explanation:
Deletes rule number 2 from the INPUT chain.
(Get rule numbers using sudo iptables -L --line-numbers.)

sudo iptables -D INPUT 2

Explanation:
Blocks all incoming connections by default.
(You must manually allow required services after this.)

sudo iptables -P INPUT DROP

Explanation:
Saves and restores firewall rules after system reboots.

sudo iptables-save > /etc/iptables.rules
or
sudo iptables-restore < /etc/iptables.rules

Explanation:
Removes all firewall rules.

sudo iptables -F

Manages IPv6 firewall rules.

sudo ip6tables -L

Configures IPv4 packet filtering and NAT rules.

sudo iptables -L

DevOpsSchool
Typically replies within an hour

DevOpsSchool
Hi there 👋

How can I help you?
×
Chat with Us