天天看點

iptable詳解

檢視iptables狀态-重新開機

iptables 所在目錄 /etc/sysconfig/iptables

service iptables status 檢視iptables狀态

service iptables restart iptables服務重新開機

service iptables stop iptables服務禁用

啟動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 -L -n

 iptables -F 清除預設表filter中的所有規則鍊的規則

 iptables -X 清除預設表filter中使用者自定鍊中的規則

 #抛棄所有不符合三種鍊規則的資料包

iptables -P INPUT DROP

iptables -P OUTPUT DROP

iptables -P FORWARD DROP

#設定:本地程序 lo 的 INPUT 和 OUTPUT 連結 ; eth0的INPUT鍊

iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -jACCEPT

iptables -A INPUT -i eth0 -m state --state NEW,INVALID -j LOG

iptables -A OUTPUT -o lo -j ACCEPT

#開放22端口ssh

 iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT

#開放80端口web

iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT

#開放21、20端口ftp

iptables -A INPUT -p tcp --dport 20 -j ACCEPT

iptables -A INPUT -p tcp --dport 21 -j ACCEPT

#開放其他一些端 口  

iptables -A INPUT -p tcp --dport 1935 -j ACCEPT

iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

iptables -A INPUT -p tcp --dport 443 -j ACCEPT

#同上,開放需要端口的出口

iptables -A OUTPUT -p tcp --sport 1935 -j ACCEPT

。。。。

# 如使用vsftpd 使用了pasv 方式,如 pasv_min_port=6000 mx=7000 pasv_enable=YES之類

 iptables -A INPUT -p tcp --dport 6000:7000 -j ACCEPT

 iptables -A OUTPUT -p TCP --sport 6000:7000 -j ACCEPT

# 2個都要設,隻設第一個不能下載下傳,隻設第二個不能上傳

#限制 .37 可以連接配接哪些端 口,

 iptables -A INPUT -s 192.168.0.37 -p tcp --dport 21 -j ACCEPT

 iptables -A INPUT -s 192.168.0.37 -p tcp --dport 20 -j ACCEPT

 #注:因上方設定的iptables -A INPUT -p tcp --dport 20 -j ACCEPT  & iptables -A INPUT -p tcp --dport 21 -j ACCEPT

 #允許開放20.21到所有使用者

 #是以要删除掉該規則

 iptables -D INPUT -p tcp --dport 20 -j ACCEPT

 iptables -D INPUT -p tcp --dport 21 -j ACCEPT

#允許loopback!(不然會導緻DNS無法正常關閉等問題)

IPTABLES -A INPUT -i lo -p all -j ACCEPT (如果是INPUT DROP)

IPTABLES -A OUTPUT -o lo -p all -j ACCEPT(如果是OUTPUT DROP)

 #将以上規則儲存到 檔案 sudo 是不行的,需要root權限(沒有設過的話, sudo passwd root 輸入新的root密碼即可。 然後su )

 iptables-save > /etc/iptables.up.rules

 修改 /etc/network/interfaces 腳本自動應用這些規則(末行是添加的)

auto eth0

iface eth0 inet dhcp

pre-up iptables-restore <  /etc/iptables.up.rules

post-down iptables-save >/etc/iptables.up.rules #關機時,把目前iptables 儲存

附 vsftpd.conf 主要項

listen=YES

anonymous_enable=NO

local_enable=YES

write_enable=YES

chroot_local_user=YES

chroot_list_enable=YES

chroot_list_file=/etc/vsftpd.chroot_list

pasv_min_port=6000

pasv_max_port=7000

pasv_enable=YES

ls_recurse_enable=YES

local_umask=022

file_open_mode=0755

這個FTP隻供于管理者進行管理及上傳工作,是以本地賬号權限較大,要注意。

在/etc/vsftpd.chroot_list 隻放root及該賬号

代碼:

#删除原來 iptables 裡面已經有的規則

iptables -F

iptables -X

#抛棄所有不符合三種鍊規則的資料包

#設定:本地程序 lo 的 INPUT 和 OUTPUT 連結 ; eth1的INPUT鍊

iptables -A INPUT -i eth1 -m state --state ESTABLISHED,RELATED -jACCEPT

iptables -A INPUT -i eth1 -m state --state NEW,INVALID -j LOG

#對其他主要允許的端口的 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 儲存

本文轉自 運維小當家 51CTO部落格,原文連結:http://blog.51cto.com/solin/1888820,如需轉載請自行聯系原作者

繼續閱讀