Difference between revisions of "How to block someone's IP"

From Kolmisoft Wiki
Jump to navigationJump to search
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== How to block one specific IP ==
First of all install iptables if needed:
First of all install iptables if needed:


Line 6: Line 8:
To block incoming IP, use this command:
To block incoming IP, use this command:


    iptables -A INPUT -s IP -j DROP
iptables -A INPUT -s IP -j DROP


For example:
For example:


    iptables -A INPUT -s 123.123.123.123 -j DROP
iptables -A INPUT -s 123.123.123.123 -j DROP


To save the rules, run:
To save the rules (On Centos), run:
service iptables save


On Centos)
Question: I want to allow only specified IPs to my server and disallow any other connections, how I could do that?
  /etc/init.d/iptables save
On Debian)


Please read this link http://www.debian-administration.org/articles/445
First of all enter all friendly ips:


iptables -A INPUT -s friendlyip. -j ACCEPT
iptables -A INPUT -s another.friendly.ip -j ACCEPT
iptables -A INPUT -s 127.0.0.1 -j ACCEPT # yes, accept connections from localhost.


Question: I want allow only specified IPs to my server and dissallow any other connections, how I could do that?
And at the most end enter:


First of all enter all friendly ips:
iptables -A INPUT -s 0/0 -j DROP
 
That's it.
 
If in future you will want to add some more IPs, just first reject rule with DROP, and some friendly IP and then apply DROP rule again.
 
'''Very important:''' Don't forget to add ISP/router to your friendly IP address list!!!
 
 
<br><br>
==How to block the range of IPs==


  iptables -A INPUT -s friendlyip. -j ACCEPT
For example if you want to block the range 178.159.10.xx
  iptables -A INPUT -s another.friendly.ip -j ACCEPT
  iptables -A INPUT -s 127.0.0.1 -j ACCEPT # yes, accept connections from localhost.


And the most end enter:
Use this command:


  iptables -A INPUT -s 0/0 -j DROP
iptables -A INPUT -s 178.159.10.0/24 -j DROP


Thats it.
And then:


If in future you will want to add some more ips, just first reject rule with DROP, and some friendly ip and then apply DROP rule again.
service iptables save


'''Very important:''' Don't forget to add ISP/router to your friendly ip address list!!!
<br><br>
<br><br>
===How to unblock IP===
==How to unblock the IP==


Search for the IP under /etc/sysconfig/iptables:
Search for the IP under /etc/sysconfig/iptables:
Line 55: Line 67:


Now IP 123.123.123.123 is unblocked.
Now IP 123.123.123.123 is unblocked.
<br><br>
== How to check all blocked IPs ==
iptables -L -n
<br><br>
== How to check if IP is blocked ==
iptables -L -n | grep IP
<br><br>
<br><br>
= See also =
* [[Call is not going through and not shown in Last Calls]]
* [http://www.fail2ban.org/wiki/index.php/Whitelist Whitelist IP for Fail2Ban]
* [[Blocked IPs]]
* [[Image:flag_esp.jpg|alt="flag"|link=]] [https://fututel.com/es/tutoriales-y-videotutoriales-sobre-linux-centos-ubuntu-windows-administracion-de-servidores-servidores-virtuales-vps/2374-como-asegurar-mor-con-iptables Cómo Asegurar MOR con IPTables]

Latest revision as of 14:10, 22 June 2020

How to block one specific IP

First of all install iptables if needed:

yum -y install iptables


To block incoming IP, use this command:

iptables -A INPUT -s IP -j DROP

For example:

iptables -A INPUT -s 123.123.123.123 -j DROP

To save the rules (On Centos), run:

service iptables save

Question: I want to allow only specified IPs to my server and disallow any other connections, how I could do that?

First of all enter all friendly ips:

iptables -A INPUT -s friendlyip. -j ACCEPT
iptables -A INPUT -s another.friendly.ip -j ACCEPT
iptables -A INPUT -s 127.0.0.1 -j ACCEPT # yes, accept connections from localhost.

And at the most end enter:

iptables -A INPUT -s 0/0 -j DROP

That's it.

If in future you will want to add some more IPs, just first reject rule with DROP, and some friendly IP and then apply DROP rule again.

Very important: Don't forget to add ISP/router to your friendly IP address list!!!




How to block the range of IPs

For example if you want to block the range 178.159.10.xx

Use this command:

iptables -A INPUT -s 178.159.10.0/24 -j DROP

And then:

service iptables save



How to unblock the IP

Search for the IP under /etc/sysconfig/iptables:

grep 123.123.123.123 /etc/sysconfig/iptables

This would give the following output:

-A INPUT -s 123.123.123.123 -j DROP
-A INPUT -s 123.123.123.123 -j ACCEPT

Then you can delete the rule using the '-D' option in iptables:

iptables -D INPUT -s 123.123.123.123 -j DROP

Now IP 123.123.123.123 is unblocked.



How to check all blocked IPs

iptables -L -n



How to check if IP is blocked

iptables -L -n | grep IP




See also