≡ Menu

Linux: Block Port With IPtables

How do I block port number with iptables under Linux operating systems?

Port numbers which are recognized by Internet and other network protocols, enabling the computer to interact with others. Each Linux server has a port number (see /etc/services file). For example:

  1. TCP port 80 - HTTP Server
  2. TCP port 443 - HTTPS Server
  3. TCP port 25 - Mail Server
  4. TCP port 22 - OpenSSH (remote) secure shell server
  5. TCP port 110 - POP3 (Post Office Protocol v3) server
  6. TCP port 143 - Internet Message Access Protocol (IMAP) — management of email messages
  7. TCP / UDP port 53 - Domain Name System (DNS)

Block Incoming Port

The syntax is as follows to block incoming port using IPtables:

 
/sbin/iptables -A INPUT -p tcp --destination-port {PORT-NUMBER-HERE} -j DROP
 
### interface section use eth1 ###
/sbin/iptables -A INPUT -i eth1 -p tcp --destination-port {PORT-NUMBER-HERE} -j DROP
 
### only drop port for given IP or Subnet ##
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP-ADDRESS-HERE} -j DROP
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP/SUBNET-HERE} -j DROP
 

To block port 80 (HTTP server), enter (or add to your iptables shell script):
# /sbin/iptables -A INPUT -p tcp --destination-port 80 -j DROP
# /sbin/service iptables save

Block Incomming Port 80 except for IP Address 1.2.3.4

# /sbin/iptables -A INPUT -p tcp -i eth1 -s ! 1.2.3.4 --dport 80 -j DROP

Block Outgoing Port

The syntax is as follows:

 
/sbin/iptables -A OUTPUT -p tcp --dport {PORT-NUMBER-HERE} -j DROP
 
### interface section use eth1 ###
/sbin/iptables -A OUTPUT -i eth1 -p tcp --dport {PORT-NUMBER-HERE} -j DROP
 
### only drop port for given IP or Subnet ##
/sbin/iptables -A OUTPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP-ADDRESS-HERE} -j DROP
/sbin/iptables -A OUTPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP/SUBNET-HERE} -j DROP
 

To block outgoing port # 25, enter:
# /sbin/iptables -A OUTPUT -p tcp --dport 25 -j DROP
# /sbin/service iptables save

You can block port # 1234 for IP address 192.168.1.2 only:
# /sbin/iptables -A OUTPUT -p tcp -d 192.168.1.2 --dport 1234 -j DROP
# /sbin/service iptables save

How Do I Log Dropped Port Details?

Use the following syntax:

# Logging #
### If you would like to log dropped packets to syslog, first log it ###
/sbin/iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "PORT 80 DROP: " --log-level 7
 
### now drop it ###
/sbin/iptables -A INPUT -p tcp --destination-port 80 -j DROP
 

How Do I Block Cracker (IP: 123.1.2.3) Access To UDP Port # 161?

 
/sbin/iptables -A INPUT -s 123.1.2.3 -i eth1 -p udp -m state --state NEW -m udp --dport 161 -j DROP
 
# drop students 192.168.1.0/24 subnet to port 80
/sbin/iptables -A INPUT -s 192.168.1.0/24 -i eth1 -p tcp -m state --state NEW -m tcp --dport 80 -j DROP
 
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 25 comments… add one }

  • Spyros December 11, 2010, 8:51 am

    I was actually looking for a good iptables reference, this one seems to do the trick, thanx !

  • moinul May 8, 2011, 4:30 am

    i need ip block for internet service.

  • myne July 21, 2011, 8:20 am

    How to block all except one IP.

  • Sux November 13, 2011, 12:50 am

    The “Block Incoming Port 80 except for IP Address 1.2.3.4” section is wrong.
    The ‘-s’ and ‘1’ should be swapped, the correct command is:
    # /sbin/iptables -A INPUT -p tcp -i eth1 ! -s 1.2.3.4 –dport 80 -j DROP

  • Sux November 13, 2011, 12:51 am

    I meant ‘-s’ and ‘!’ :]

  • Mike May 3, 2012, 9:42 pm

    please help me i have maplestory server and someone attack my ports how do i bloc him and where do i put it in?

    • gg March 17, 2014, 4:52 am

      URGENT!! HELP!! SOMEONE HELP THIS MAN RIGHT AWAY.

  • wahyu October 5, 2012, 11:46 am

    What if I want to block some ports? such as 80, 3128 and 22, can I just do that with this line:

    # /sbin/iptables -A INPUT -p tcp ! –destination-port 80 3128 22 -j DROP ?

  • Amauris October 18, 2012, 10:27 pm

    Would this be correct to block port 25 except for ip 1.2.3.4?

    # /sbin/iptables -A OUTPUT -p tcp -i eth1 ! -s 1.2.3.4 –-dport 25 -j DROP

    • FAILWHALE June 2, 2013, 9:56 am

      You cant use -i with output

      :/var/www/www.yomommashole.com# iptables-restore < /etc/iptables.up.rules
      iptables-restore v1.4.14: Can't use -i with OUTPUT
      Error occurred at line: 17

      Way to go dildo.

  • Baldwin October 21, 2012, 4:13 pm

    I am still having a problem on how to configure squid to use iptables for the port redirection 3128 rather than configure the browser to do so. My children understand how to configure the browser to bypass squid blocking sites. Can anyone help me ? Here is my squid.conf and my iptables that I am using.
    squid.conf -> http://pastebin.com/LtXw1ZDT
    iptables script -> http://pastebin.com/iXrb1Xic
    Thanks

    • Pitto March 25, 2013, 8:46 am

      Hi Baldwin!

      Did you solve this?

      I’m stuck in the same identical problem :/

  • mustafa October 23, 2012, 4:26 pm

    Hi,
    Please help me here. When i try to block 443 port on outgoing, i get this error:

    iptables v1.3.5: Can’t use -i with OUTPUT

  • Abdul Basit May 31, 2013, 4:01 pm

    iptables -A OUTPUT -p tcp –dport 443 -j DROP

  • mnenad September 22, 2013, 2:48 pm

    Recently i had problem with spammers whom were using my mail server to send spam mail. Since then i am blocking port 25 for external network, but now i can’t receive mail. My question is, is it possible to allow sending mail, using iptables, from localnetwork like 192.168.1.10/24, and to allow receiving to port 25 from anywhere?

  • marcin November 25, 2013, 5:41 pm

    I have been searching for while and I can not find a definite answer.
    Will blocking IP from single port, interface and protocol v.s. blocking same IP from all ports and all interfaces make a iptables work faster?
    i.e
    iptables -A INPUT -i {INTERFACE} -s {IP address} -p {prot} –dport {port} -j DROP
    v.s.
    iptables -A INPUT -s {IP address} -j DROP

    I have a large (2000+ IPs ) list to be block from my server, but when in place drop all significantly slows down the server and packets throughput.

  • TuxGamer March 4, 2014, 11:54 am

    Thank you for the tutorial :)

  • RK March 10, 2014, 6:28 am

    Hi

    I have blocked port 80 and now question is how to unblock it

    • David April 8, 2014, 10:46 am

      list rules:
      iptables -L

      find where the rule is, and delete it:
      iptables -D INPUT 1
      (removes the first rule from “INPUT”)

  • Serge May 12, 2014, 1:38 pm

    Use -d not -s for outgoing connection!!

    • mian May 29, 2014, 4:41 pm

      I’m trying to block all ports except 80 and 443 to one specific host.For instance i have 10 hosts that should access a server on which i will enable iptables. Now 9 hosts should be able to access all the ports but one host should only be able to access port 80 and 443 and all others should be dropped.How can that be done ?

  • Slim June 16, 2014, 9:46 am

    Hi guys,

    How to block all ports, except one or two specify ports

  • mike June 19, 2014, 7:30 am

    very nice work !

  • A.Lakshmana Shiva` March 18, 2015, 8:41 am

    I am having one doubt in Iptable.I need to block the SSH port in the IPtables at centos 6.5 at minimal mode.I also need to check whether it is working after blocking the port.How to do this?When I try to take the centos machine in Putty the machine in the Centos should not be logged.How to do this?

  • Bv57 June 15, 2015, 8:29 pm

    Can any one tell me how to block a source port

Leave a Comment