Install and Use Iptables on Debian Linux

In this guide, we want to teach you to Install and Use Iptables on Debian Linux distros. You can use all the Debian versions such as Debian 10, Debian 11, and the newly released Debian 12 Bookworm.

You can use iptables to configure your IP traffic filter rules and control the incoming and outgoing packets.

How To Install and Use Iptables on Debian Linux?

To configure iptables on Debian, you must have access to your server as a non-root user with sudo privileges. For this purpose, you can visit the Orcacore website and check for the Debian Initials Server Setup.

Now follow the steps below to start your Iptables configuration on Debian Linux.

Step 1 – How To Check iptables in Debian?

By default, iptables come pre-installed in most Linux distributions.

Note: If you don’t have it installed on your server, then, you can use the following command to install the iptables firewall on your server:

sudo apt install iptables -y

Then, you can check your iptables configuration with the following command:

sudo iptables -L -v

The -L option used in the above command is for the list of all rules, and the -v option is used to show information in more detail.

In your output you will see:

Output
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 

Step 2 – How To Use Iptables on Debian Linux?

At this point, you can follow the steps below to learn the basic usage of Iptables on the Debian Linux distro.

We want to start with defining a rule with iptables.

How To Define a Rule with Iptables?

You can easily define a rule with Iptables by using the -A option in the following Iptables command on Debian:

sudo iptables -A [argument]

Iptables Command Options

Also, you can combine the command with other options like:

  • -i: The network interface whose traffic you want to filter, such as eth0, lo, ppp0, etc.
  • -p:  The network protocol where your filtering process takes place. It can be either TCP, UDP, udplite, ICMP, SCTP, icmpv6 and so on. Also, you can type all to choose every protocol.
  • -s: The address from which traffic comes. You can add a hostname or IP address.
  • -dport: The destination port number of a protocol, such as 22 (SSH), 443 (HTTPS), etc.
  • -j:  The target name (ACCEPT, DROP, RETURN). You need to insert this every time you make a new rule.

How To Use Full Iptables Command in Order?

If you want to use all of the parameters, you need to type the iptables command on Debian Linux in the order shown below:

sudo iptables -A <chain> -i <interface> -p <protocol (tcp/udp) > -s <source> --dport <port no.>  -j <target>

How To Allow Traffic on LocalHost with Iptables?

To allow traffic on localhost, you can use the iptables firewall command like the below command:

sudo iptables -A INPUT -i lo -j ACCEPT

How To Enable a Connection Through Iptables?

You can enable a connection like SSH on iptables command like this:

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

Then, you can check the rule that has been appended in iptables on Debian Linux with the command below:

sudo iptables -L -v

In your output you will see:

Output
0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh

How To Filter Packets with Iptables on Debian Linux?

Iptables allow you to filter packets on an IP address or a range of IP addresses. To do this, you need to use the -s option in the iptables command on Debian. For example:

sudo iptables -A INPUT -s 10.10.0.1 -j ACCEPT

How To Reject Packets with Iptables?

You can also reject the packets with the command below:

sudo iptables -A INPUT -s 10.10.0.1 -j DROP

How To Drop All Traffics with Iptables?

Also, you can drop all other traffic simply by using the following command:

sudo iptables -A INPUT -j DROP

Delete Rules with Iptables on Debian Linux

To delete all current rules on your iptables firewall you can use the following command:

sudo iptables -F

To delete a specific rule, you need to first check the available rules of iptables on Debian by typing the following command:

sudo iptables -L --line-numbers

In your output you will see:

Output
num target prot opt source destination
1 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
2 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh

Here to delete a specific rule, you also need to specify the rule number in the command below:

sudo iptables -D INPUT 2

To apply the changes that have been saved on boot you can use the following command:

sudo /sbin/iptables-save

Step 3 – How To Disable Iptables on Debian Linux?

If you want to disable iptables, you can use the following commands:

$ sudo iptables -F
$ sudo /sbin/iptables-save

In your output you will see:

Output
:INPUT ACCEPT [19:2597]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [6:528]
...

If you want to find more detailed information about iptables and its options it is highly recommended to read its manual:

man iptables

Conclusion

At this point, you have learned to Install and Use Iptables on Debian Linux. Iptables is a powerful firewall tool for Linux. As you saw, you can easily configure Iptables rules and use them on your server.

Hope you enjoy it. You may be like these articles too:

Configure Firewall with UFW on Debian 12 Bookworm

FirewallD Configuration on AlmaLinux 9

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!