Ubuntu iptables 設置


在ubuntu中由於不存在 /etc/init.d/iptales文件,所以無法使用service等命令來啟動iptables,需要用modprobe命令。 

啟動iptables  

modprobe ip_tables  

關閉iptables(關閉命令要比啟動復雜)  

iptalbes -F  

iptables -X  

iptables -Z  

iptables -P INPUT ACCEPT  

iptables -P OUTPUT ACCEPT  

iptables -P FORWARD ACCEPT  

modprobe -r ip_tables  

依次執行以上命令即可關閉iptables,否則在執行modproble -r ip_tables時將會提示  FATAL: Module ip_tables is in use

 

代碼:
#刪除原來 iptables 里面已經有的規則
iptables -F
iptables -X

#拋棄所有不符合三種鏈規則的數據包
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

#設置:本地進程 lo  的 INPUT 和 OUTPUT 鏈接 ; eth1的INPUT鏈
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i eth1 -m state --state ESTABLISHED,RELATED -jACCEPT
iptables -A INPUT -i eth1 -m state --state NEW,INVALID -j LOG
iptables -A OUTPUT -o lo -j ACCEPT

#對其他主要允許的端口的 OUTPUT設置:
# DNS
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 53 -jACCEPT
iptables -A OUTPUT -o eth1 -p UDP --sport 1024:65535 --dport 53 -jACCEPT

#HTTP
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 80 -jACCEPT

#HTTPS
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 443 -jACCEPT

#Email 接受 和發送
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 110 -jACCEPT
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 25 -jACCEPT

# FTP 數據和控制
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 20 -jACCEPT
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 21 -jACCEPT

#DHCP
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 68 -jACCEPT
iptables -A OUTPUT -o eth1 -p UDP --sport 1024:65535 --dport 68 -jACCEPT

#POP3S Email安全接收
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 995 -jACCEPT

#時間同步服務器 NTP
iptables -A OUTPUT -o eth1 -p TCP --sport 1024:65535 --dport 123 -jACCEPT

#拒絕 eth1 其他剩下的
iptables -A OUTPUT -o eth1 --match state --state NEW,INVALID -jLOG



最后是有關於iptables存儲的命令:

代碼:
iptables-save >/etc/iptables.up.rule # 存在你想存的地方
代碼:
iptables-restore </etc/iptables.up.rules #調用
 
因為iptables 在每次機器重新啟動以后,需要再次輸入或者調用,為了方便操作,使用
代碼:
sudo gedit /etc/network/interfaces


代碼:
auto ath0
iface ath0 inet dhcp

后面加上

代碼:
pre-up iptables-restore </etc/iptables.up.rules #啟動自動調用已存儲的iptables
代碼:
post-down iptables-save >/etc/iptables.up.rule #關機時,把當前iptables 儲存


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM