Method One, filter some IP access to this server
To shut down an IP, use this command:
The code is as follows |
Copy Code |
Iptables-i input-s ***.***.***.***-j DROP |
To unlock an IP, use the following command:
The code is as follows |
Copy Code |
iptables-d input-s ***.***.***.***-j DROP
|
The argument-I is the insert (add), and-D represents the delete (deletion). followed by the rules, input represents the inbound, ***.***.***.*** indicates that the Ip,drop to be closed indicates that the connection was discarded.
In addition, you can use the following command to view the current IP rule table:
The code is as follows |
Copy Code |
Iptables-list |
For example, now to 123.44.55.66 this IP block, enter:
The code is as follows |
Copy Code |
Iptables-i input-s 123.44.55.66-j DROP |
To unpack, replace-I with-D only if iptables already has this record. If you want to empty the blocked IP address, you can enter:
The code is as follows |
Copy Code |
Iptables-flush |
To add an IP segment to the closure list, use the following command:
The code is as follows |
Copy Code |
Iptables-i input-s 121.0.0.0/8-j DROP |
In fact, that is, the IP part of a single IP block switched to linux IP segment expression. About IP segment Expression Online There are a lot of detailed explanations, here is not mentioned.
Method two, using a script to mask
1. Download the IP address file first
Let's go to IPDeny to download a list of IP addresses in country code, such as download Cn.zone:
wget Http://www.ipdeny.com/ipblocks/data/countries/cn.zone
Now that you have all the IP addresses of the country, it's easy to block these IPs by writing a script to read Cn.zone files line-by-row and add them to the iptables:
Iptables Shield a country IP segment
The code is as follows |
Copy Code |
#!/bin/bash # block traffic from a specific country # Written by blog.slogra.com COUNTRY = "cn" IPTABLES =/sbin/iptables Egrep =/bin/egrep If ["$ (id-u)"!= "0"]; Then echo "You must be root" 1>&2 Exit 1 Fi Resetrules () { $IPTABLES-F $IPTABLES-T Nat-f $IPTABLES-T Mangle-f $IPTABLES-X } Resetrules For C in $COUNTRY Todo Country_file = $c. Zone IPS = $ ($EGREP-V "^#|^$" $country _file) For IP in $IPS Todo echo "Blocking $ip" $IPTABLES-A input-s $ip-j DROP Done Done Exit 0 |
Method Three, the above two examples can but are not concise, let me introduce
In Iptables, two commonly used IP segments are added, and packets from other segments are drop instead of reject (reject also sends ICMP response packets to the connector).
The code is as follows |
Copy Code |
# iptables-a input-p TCP--dport 22-s 120.0.0.0/8-j # iptables-a input-p TCP--dport 22-s 183.0.0.0/8-j # iptables-a input-p TCP--dport 22-j DROP |
Test:
In another connection to the VPS, the packet was successfully drop.
The code is as follows |
Copy Code |
root@293621:~# IPTABLES-VL Chain INPUT (Policy ACCEPT packets, 6257 bytes) Pkts bytes Target prot opt in Out source destination 222 16280 ACCEPT TCP--any any 120.0.0.0/8 anywhere TCP Dpt:ssh 0 0 ACCEPT TCP--any any 183.0.0.0/8 anywhere TCP Dpt:ssh 4 A DROP TCP--any anywhere anywhere TCP Dpt:ssh |
Once again realize the power of iptables, it seems this piece of content, we need to find a time to strengthen, easy to use AH