How to block an IP in Linux

Home/Linux, Security/How to block an IP in Linux

I run Debian on my server, and I often find that my server is being attacked by other computers. Brute force SSH attacks, viruses scanning for the ability to spread, things like that. I’ll go into the SSH brute force defenses in a later post, but for now I’ll cover how to easily block an IP address.

First, I’ll assume you are already using iptables. If you need help setting that up, use Google, Debian comes with it out of the box.

I have a small script called “block” which looks like this:

sudo iptables -I INPUT -s $1 -j DROP
sudo bash -c "iptables-save > /etc/network/"

Whenever I find a “bad” IP in my logs or notifications, I just run:

block bad.ip.add.18

Substituting the bad ip for that nonesense above. This adds it to the list of IP address which iptables will simply drop any incoming packets from, and saves the in memory iptables configuration, so that it is preserved through reboots.

Then in your /etc/network/interfaces file, just add this at the bottom:

post-up iptables-restore /etc/network/
By | 2017-05-18T15:18:49+00:00 September 16th, 2007|Linux, Security|21 Comments

About the Author:


  1. IP Guy December 28, 2007 at 5:50 am - Reply

    You can get country IP blocks freely and use in your firewall as well.

  2. […] also recommend using the script in my post on blocking IP addresses using iptables to deal with any persistent folks, or people poking too hard on your web site, or other […]

  3. rhalff September 17, 2009 at 5:16 am - Reply

    Apparently you can also do something like:

    ip ro add blackhole

  4. jd July 1, 2010 at 1:03 pm - Reply

    How would you go about adding these blocks to a separate log file? Say /var/log/iptables.log.


    • Devon July 1, 2010 at 1:09 pm - Reply

      If you use this method all of the blocked IPs end up in this file: /etc/network/ so you can easily see them. If you’re asking more generally how to get firewall/iptables log entries segregated into their own log then I recommend using syslog-ng and setting things up a bit like this:

      destination firewall { file(“/var/log/firewall.log”); };
      filter f_firewall { match(“Firewall”); };
      filter f_kernel { facility(kern) and not filter(f_firewall); };
      log {

  5. JD July 1, 2010 at 1:29 pm - Reply

    Thanks for the quick reply!
    Sorry, I was reading many of your articles and ended up posting that comment on the wrong article; it was meant for “Using IPTables to Prevent SSH Brute Force Attacks”.

    Please delete it if you wish, I’ll repost in the correct one if that is Ok.

    Thanks again.

  6. Qoalu August 2, 2010 at 3:23 am - Reply


    I created a file in /home called block, ran it with “block X.X.X.X” and “./block X.X.X.X”. Neither works. What am I doing wrong.

    Let’s say I add the ip manually to the /etc/network/interfaces like this:

    iptables -A INPUT -s X.X.X.X -j DROP

    When will the directive activate, do I need to restart the interfaces?

    Regards, Qoalu.

    • Devon August 3, 2010 at 5:42 am - Reply


      you created the “block” file and it’s contents are the shell script at the top of this post? Are you sure it’s not working? The iptables commands should take effect immediately when called manually or via my script above. If you add the iptables command to the interfaces file you will need to bounce the interface, or you can just run the command yourself manually.


  7. Richard December 15, 2010 at 3:03 am - Reply


    If you have not already figured this out, ensure that you made the script executable: chmod 755 ./block

  8. Richard February 14, 2011 at 5:30 pm - Reply

    Works for me. Just what I was looking for.

  9. Mark August 26, 2011 at 8:37 am - Reply

    Just wanted to add that on RedHat based systems, you can do that last little bit of saving the configuration for use on reboot just using the init scripts, i.e.

    /etc/init.d/iptables save

    which will save the current configuration to the file /etc/sysconfig/iptables, which is always used to restore on reboot.

    i.e. the block script would be (either run as root or with a sudo command):

    iptables -I INPUT -s $1 -j DROP
    /etc/init.d/iptables save

    and then there is no need to do the equivalent of the line in the /etc/network/interfaces file (which doesn’t exist in RedHat based systems).

  10. Ryan February 19, 2012 at 10:52 pm - Reply

    Running the script in Debian Squeeze results in this:

    iptables v1.4.8: host/network `13.0.782.220′ not found
    Try `iptables -h’ or ‘iptables –help’ for more information.

    • Devon February 19, 2012 at 11:13 pm - Reply


      that’s not a valid IP address… The .782. part must be wrong, the maximum value of any of the quartets is 255.

  11. Wolf Halton June 28, 2012 at 3:19 am - Reply

    script needs >> (append) instead of > (redirect)
    The way you have it written, it will overwrite the file every time you use block.

  12. […] by […]

  13. Ed August 27, 2013 at 4:42 am - Reply

    Thank you for such a nice tutorial.

    I just wrote a different kind of tutorial on how to set up Arno IPTABLES firewall, which of course allows to block IPs.
    May be it may help someone to setup his own firewall based on IPTABLES.
    You can find some examples for a mail server and for a Proxy server using SNAT and port forwarding.
    The location of my tutorial is here:

    I wish it is useful to someone.

  14. Rob January 22, 2014 at 12:54 pm - Reply

    Nice script! I added a little command just to instantaneously kill the connection from the host that’s going to be blocked. The tool is called “cutter” and is easy to use, just give the hostname or an IP address as argument.

  15. […] source : How to block an IP in Linux […]

  16. Zane April 16, 2014 at 6:23 am - Reply

    Thank you for this article, I really had some problems with some guys who were trying to connect to my root by bruteforce. This really saved my day :)

  17. zee June 6, 2015 at 10:48 pm - Reply

    Thanks for the great script, saved my day as well.

Leave A Comment