Bobcares

Iptables block port range – Easy way to do it

by | Oct 24, 2019

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.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

2 Comments

  1. Gert

    Hi, this was the first usable explanation I found with google – our mail server is overloaded an did rash

    thanks

    Reply
    • Hiba Razak

      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).

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF