1. 程式人生 > >Linux下使用iptables禁止指定IP地址的網路連線

Linux下使用iptables禁止指定IP地址的網路連線

在Linux下,使用iptables來維護IP規則表。要封停或者是解封IP,其實就是在IP規則表新增規則。

要禁止指定IP地址的網路連線,可以使用以下兩種方法來快速實現。

1.禁止特定IP的連線

要禁止一個IP,使用下面這條命令:

iptables -I INPUT -s ***.***.***.*** -j DROP

要解封一個IP,使用下面這條命令:

iptables -D INPUT -s ***.***.***.*** -j DROP

引數-I是表示 Insert (新增),-D表示 Delete (刪除)。後面跟的是規則, INPUT 表示入站,***.***.***.***

表示要封停的IP, DROP 表示放棄連線。

可以使用下面的命令來檢視當前的IP規則表:
iptables -list

[email protected]:~$ sudo iptables -L
[sudo] password for wsliu: 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       all  --  120.203.229.36       anywhere            
DROP       all  --  117.169
.67.5 anywhere DROP all -- 103.52.217.131 anywhere Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination

2. 禁止IP段的網路連線

新增IP段到封停列表中:
iptables -I INPUT -s 121.0.0.0/8 -j DROP

參考資料

iptables(8) - Linux man page

IptablesHowTo - Ubuntu Help Center

iptables - Wikipedia