iptables的基本使用方法

iptables使用方法

1.查看iptables

#詳細查看iptalbes

[root@shiyan6~ ]# iptables -vnL

#查看iptables 並帶序號

[root@shiyan6~ ]# iptables -vnL --line-number

#超詳細查看iptables

[root@shiyan6~ ]# iptables -vvnL

2.iptables的基本使用方法

#添加

[root@shiyan7 ~ ]# iptables -t filter -A INPUT -s 172.18.17.0/16 -d172.18.17.32 -p tcp  --dport 22  -j ACCEPT

#在filter表中的INPUT鏈上添加:

源地址是172.18.17.0/16的隨機端口,可以訪問目標地址爲172.18.17.32的tcp協議22號端口的記錄

 

[root@shiyan7~ ]# iptables -t filter -A OUTPUT -s 172.18.17.32 -d 172.18.17.0/16  -p tcp --sport 22  -j ACCEPT

#在filter表中的OUPPUT鏈上添加:

源地址是172.18.17.32的tcp協議22號端口,可以訪問目標地址172.18.17.0/16隨機端口的記錄

 

[root@shiyan7~ ]# iptables -t filter -A INPUT -d 172.18.17.32 -j REJECT

#在filter表中的INPUT鏈上添加:

源地址是所有的IP地址所有端口,不能訪問目標地址爲172.18.17.32的記錄

-t : 指定表

-A : 添加,向後添加,指定添加的規則連

-s : 指定源地址

-d : 指定目的地址

-p : 指定協議

--dport : 指定端口

-j : 指定允許或者拒絕[ACCEPT]|[EROP]|[REJECT]


ACCEPT

允許訪問

EROP

拒絕訪問(不回覆)

REJECT

拒絕訪問(給予恢復)

 

#查看之前添加的iptables記錄

[root@shiyan7~ ]# iptables -vnL --line-number

ChainINPUT (policy ACCEPT 7 packets, 697 bytes)

num   pkts bytes target     prot opt in     out    source              destination        

1     1621 118K ACCEPT     tcp  --  *      *      172.18.0.0/16       172.18.17.32         tcp dpt:22

2        0    0        REJECT       all --   *      *      0.0.0.0/0                172.18.17.32         reject-with icmp-port-unreachable

 

ChainFORWARD (policy ACCEPT 0 packets, 0 bytes)

num   pkts bytes target     prot opt in     out    source              destination        

 

ChainOUTPUT (policy ACCEPT 0 packets, 0 bytes)

num   pkts bytes target     prot opt in     out    source              destination        

1      576 67656 ACCEPT     tcp --  *      *      172.18.17.32        172.18.0.0/16        tcp spt:22

結果:172.18.17.32的主機除了22號端口只能是172.18.0.0/16段的IP能訪問,其他所有的IP地址,都不能訪問172.18.17.32,包括所有的端口號

測試:使用172.18.17.11訪問172.18.17.32ssh端口

[root@yum~ ]# ssh 172.18.17.32

[email protected]'spassword:

Lastlogin: Thu Apr 27 22:15:59 2017 from 172.18.17.100

[root@shiyan7~ ]#

測試成功

 

#插入

[root@shiyan7~ ]# iptables -t filter -I INPUT 1 -s 172.18.17.11 -d 172.18.17.32 -p tcp--dport 22 -j REJECT

#在fileter表的INPUT鏈上插入爲1編號的記錄:

172.18.17.11的所有端口都不能訪問172.18.17.3222端口

 

#查看iptables

[root@shiyan7~ ]# iptables -vnL --line-number

ChainINPUT (policy ACCEPT 1 packets, 229 bytes)

num   pkts bytes target     prot opt in     out    source              destination        

1       22 2904 REJECT     tcp  --  *      *      172.18.17.11        172.18.17.32         tcp dpt:22reject-with icmp-port-unreachable

 

 

測試:使用172.18.17.11172.18.17.12分別登陸

172.18.17.11主機

[root@yum~ ]# ssh 172.18.17.32

ssh:connect to host 172.18.17.32 port 22: Connection refused

登陸失敗

172.18.17.12主機

[root@shiyan6~ ]# ssh 172.18.17.32

[email protected]'spassword:

Lastlogin: Thu Apr 27 22:16:12 2017 from 172.18.17.11

[root@shiyan7~ ]#

登陸成功

測試成功

 

 

#刪除

#刪除INPUT鏈上的第一條記錄

[root@shiyan7~ ]# iptables -D INPUT 1

#剛剛插入拒絕172.18.17.11訪問ssh服務的記錄已經被刪除

[root@shiyan7~ ]# iptables -vnL --line-number

ChainINPUT (policy ACCEPT 1 packets, 136 bytes)

num   pkts bytes target     prot opt in     out    source              destination        

1     2175 164K ACCEPT     tcp  --  *      *      172.18.0.0/16       172.18.17.32         tcp dpt:22

2        6  596 REJECT     all  --  *      *      0.0.0.0/0           172.18.17.32         reject-withicmp-port-unreachable

測試:使用172.18.17.11登陸

172.18.17.11主機

[root@yum~ ]# ssh 172.18.17.32

[email protected]'spassword:

Lastlogin: Thu Apr 27 22:22:26 2017 from 172.18.17.12

[root@shiyan7~ ]#

登陸成功


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章