Announcement

Collapse
No announcement yet.

How to block specific IP's using iptables?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to block specific IP's using iptables?

    How can I block specific IP's? But I want to block it permanently by using iptables rules.
    Does anyone here know how to do it?

  • #2
    You can add the following rule. In --src you also can define multiple IPs separated by , without spaces

    iptables -A INPUT --src <the specific IP> -j DROP
    Be careful do not add yourself out though!

    Comment


    • #3
      Nice. So I have to do this in /etc/iptables/rules.v4/v6 file?

      Comment


      • #4
        No. Run it as command in the terminal.

        Comment


        • #5
          If you're receiving permission denied message when you are trying to use > then this Stack Overflow answer will be useful.

          Comment


          • #6
            The easiest way is to use iptables-save and iptables-restore to save the currently-defined iptables rules to the file and load or reload them (E.g., upon reboot).

            So, for example, you can run
            sudo iptables-save > /etc/iptables.conf
            To save your existing iptables rules to /etc/iptables.conf and then add these lines in /etc/rc.local:
            # Load iptables rules from this file
            iptables-restore < /etc/iptables.conf

            Comment


            • #7
              Better than /etc/rc.local is to insert a line in /etc/network/interfaces after saving the iptable's rules, as follows

              Code:
              post-up iptables-restore < /etc/iptables.up.rules
              Or it's the same to place the file inside /etc/network/if-down.d/ or /etc/network/if-post-down.d/ or /etc/network/if-pre-up.d/ or /etc/network/if-up.d/.

              Comment


              • #8
                Use sudo sh -c "iptables-save > /etc/iptables.conf" instead of sudo iptables-save > /etc/iptables.conf

                Comment

                Working...
                X