Can we use iptables to block a port range?
This is a common query related to iptables among server owners.
Since open ports can cause major security breaches, it is better to block unwanted connections.
At Bobcares, we often get requests to block ports, as a part of our Server Management Services.
Today, let’s see how our Support Engineers block a range of ports using iptables.
Why should we block port range in iptables?
Iptables is the built-in firewall for Linux systems. It can decide on the incoming and outgoing traffic on the server.
Just like an open door, unwanted open ports create server security risks. Many times this can be a possible way to attack systems. In such cases, blocking ports using iptables improves security.
As Server Administrators, we always recommend closing unwanted ports. Iptables allow us to block both incoming and outgoing connections. Let’s see how we do this.
How we use Iptables to block ports?
Being in the server administration for a decade, our customers approach us to block ports as per their requirements. This varies greatly from a single port to a port range. In addition, some customers may need to block all connections except a few.
Our Security Experts fulfill customer requirements in all possible ways. Here, let’s see a few instances of port blocks.
Block incoming port using Iptables
Incoming ports are the most vulnerable to attacks. In this situation, we block the incoming connection from ports. For this, we make use of the command,
iptables -A INPUT -p tcp --dport <port number> -j DROP
This command blocks the connection from a single port. Here we make use of INPUT the built-in chain of iptables. But blocking ports one by one is a hectic task. So, to save both time and script size we use the command,
iptables -A INPUT -p tcp --dport xxxx:xxxx -j DROP
Another possible way to block a range of ports is a multiport module. Here we make use of the command,
iptables -A INPUT -p tcp --match multiport --dports xxxx:xxxx -j DROP
Usually, we use the multiport module to specify a set of ports. We can specify the port range by replacing xxxx in the command above.
Finally, our Support Engineers ensure to save iptables and make changes permanent.
To block outgoing port
Similarly, we can block outgoing connections from the server using iptables. Here we use the OUTPUT chain to notify outgoing connection. So the command our Support Engineers use is,
iptables -A OUTPUT -p tcp --dport <port number> -j DROP
The commands to block incoming connections are applicable here with a small change. Instead of the INPUT chain, we need to use the OUTPUT chain.
After making any update in iptables, we always make sure to save the changes.
Block all ports except a few
We often get requests to block ports except a few. In this case, our Support Team makes use of the iptables and set the default action to DROP. Then create exception rules to allow 80 and 443. For instance, to block all ports except 80,443 we use the commands,
iptables -P INPUT DROP
# Exceptions to default policy
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # HTTP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT # HTTPS
So, this blocks the incoming connections to all ports other than 80,443.
[Need assistance to block ports using Iptables? – We are available 24/7.]
Conclusion
In short, the Iptables block port range is one of the easiest ways to secure the system by dropping both incoming and outgoing connections. Today, we saw how our Support Engineers block one or more ports using iptables.
Hi, this was the first usable explanation I found with google – our mail server is overloaded an did rash
thanks
Hello Gert,
Our experts can help you with the issue.we will be happy to talk to you through our live chat(click on the icon at right-bottom).