iptables是Linux上常用的防火墻軟件,下面vps偵探給大家說一下iptables的安裝、清除iptables規(guī)則、iptables只開放指定端口、iptables屏蔽指定ip、ip段及解封、刪除已添加的iptables規(guī)則等iptables的基本應(yīng)用。
1、安裝iptables防火墻
如果沒有安裝iptables需要先安裝,CentOS執(zhí)行:
yum install iptables
Debian/Ubuntu執(zhí)行:
apt-get install iptables
2、清除已有iptables規(guī)則
iptables -F
iptables -X
iptables -Z
3、開放指定的端口
#允許本地回環(huán)接口(即運行本機(jī)訪問本機(jī))
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
# 允許已建立的或相關(guān)連的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允許所有本機(jī)向外的訪問
iptables -A OUTPUT -j ACCEPT
# 允許訪問22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#允許訪問80端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#允許FTP服務(wù)的21和20端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#如果有其他端口的話,規(guī)則也類似,稍微修改上述語句就行
#禁止其他未允許的規(guī)則訪問
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT
4、屏蔽IP
#如果只是想屏蔽IP的話“3、開放指定的端口”可以直接跳過。
#屏蔽單個IP的命令是
iptables -I INPUT -s 123.45.6.7 -j DROP
#封整個段即從123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP
#封IP段即從123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP
#封IP段即從123.45.6.1到123.45.6.254的命令是
iptables -I INPUT -s 123.45.6.0/24 -j DROP
4、查看已添加的iptables規(guī)則
iptables -L -n
v:顯示詳細(xì)信息,包括每條規(guī)則的匹配包數(shù)量和匹配字節(jié)數(shù)
x:在 v 的基礎(chǔ)上,禁止自動單位換算(K、M) vps偵探
n:只顯示IP地址和端口號,不將ip解析為域名
5、刪除已添加的iptables規(guī)則
將所有iptables以序號標(biāo)記顯示,執(zhí)行:
iptables -L -n --line-numbers
比如要刪除INPUT里序號為8的規(guī)則,執(zhí)行:
iptables -D INPUT 8
6、iptables的開機(jī)啟動及規(guī)則保存
CentOS上可能會存在安裝好iptables后,iptables并不開機(jī)自啟動,可以執(zhí)行一下:
chkconfig --level 345 iptables on
將其加入開機(jī)啟動。
CentOS上可以執(zhí)行:service iptables save保存規(guī)則。
另外更需要注意的是Debian/Ubuntu上iptables是不會保存規(guī)則的。
需要按如下步驟進(jìn)行,讓網(wǎng)卡關(guān)閉是保存iptables規(guī)則,啟動時加載iptables規(guī)則:
創(chuàng)建/etc/network/if-post-down.d/iptables 文件,添加如下內(nèi)容:
#!/bin/bash
iptables-save > /etc/iptables.rules
執(zhí)行:chmod +x /etc/network/if-post-down.d/iptables 添加執(zhí)行權(quán)限。
創(chuàng)建/etc/network/if-pre-up.d/iptables 文件,添加如下內(nèi)容:
#!/bin/bash
iptables-restore < /etc/iptables.rules
執(zhí)行:chmod +x /etc/network/if-pre-up.d/iptables 添加執(zhí)行權(quán)限。
關(guān)于更多的iptables的使用方法可以執(zhí)行:iptables --help或網(wǎng)上搜索一下iptables參數(shù)的說明。
億恩科技地址(ADD):鄭州市黃河路129號天一大廈608室 郵編(ZIP):450008 傳真(FAX):0371-60123888
聯(lián)系:億恩小凡
QQ:89317007
電話:0371-63322206 本文出自:億恩科技【www.allwellnessguide.com】
服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|