溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何配置Debian系統(tǒng)的VPS上iptables

發(fā)布時(shí)間:2021-09-26 15:30:42 來(lái)源:億速云 閱讀:300 作者:iii 欄目:系統(tǒng)運(yùn)維

本篇內(nèi)容介紹了“如何配置Debian系統(tǒng)的VPS上iptables”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

代碼如下:


#配置,禁止進(jìn),允許出,允許回環(huán)網(wǎng)卡
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
#允許ping,不允許刪了就行
iptables -A INPUT -p icmp -j ACCEPT
#允許ssh
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
#允許ftp
iptables -A INPUT -p tcp -m tcp --dport 20 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
#允許ftp被動(dòng)接口范圍,在ftp配置文件里可以設(shè)置
iptables -A INPUT -p tcp --dport 20000:30000 -j ACCEPT
#學(xué)習(xí)felix,把smtp設(shè)成本地
iptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT -s 127.0.0.1
iptables -A INPUT -p tcp -m tcp --dport 25 -j REJECT
#允許DNS
iptables -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT
#允許http和https
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
#允許狀態(tài)檢測(cè),懶得解釋
iptables -A INPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p all -m state --state INVALID,NEW -j DROP</p> <p>#保存配置
iptables-save > /etc/iptables

我是把上面那段和下面這段都寫(xiě)到sh里了,start{}和stop{}。需要修改規(guī)則的時(shí)候直接清空了重建比較好,因?yàn)橐?guī)則有順序問(wèn)題。

代碼如下:


#清空配置
iptables -F
iptables -X
iptables -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT

iptables防火墻開(kāi)機(jī)自動(dòng)加載實(shí)現(xiàn)
iptables命令輸完之后會(huì)立刻生效,但重啟之后配置就會(huì)消失,Debian提供了一個(gè)iptables-save程序快速保存配置,但還需要我們做一些工作才能讓iptables配置實(shí)現(xiàn)開(kāi)機(jī)自啟動(dòng),自加載。
1、將iptables配置保存到/etc/iptables,這個(gè)文件名可以自己定義,與下面的配置一致即可

代碼如下:


iptables-save > /etc/iptables


2、創(chuàng)建自啟動(dòng)配置文件,并授于可執(zhí)行權(quán)限

代碼如下:


touch /etc/network/if-pre-up.d/iptables
chmod +x /etc/network/if-pre-up.d/iptables


3、編輯該自啟動(dòng)配置文件,內(nèi)容為啟動(dòng)網(wǎng)絡(luò)時(shí)恢復(fù)iptables配置

代碼如下:


vim /etc/network/if-pre-up.d/iptables


文件內(nèi)容如下:

代碼如下:


#!/bin/sh
/sbin/iptables-restore < /etc/iptables


4、:wq保存配置文件并退出即可,以后在修改完iptables配置之后只要再次執(zhí)行下面的命令保存即可

代碼如下:


iptables-save > /etc/iptables

“如何配置Debian系統(tǒng)的VPS上iptables”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI