參考: http://blog.51yip.com/linux/1404.htmlhtml
參考: https://aliang.org/Linux/iptables.html
linux
用途 | 鏈/表 |
---|---|
過濾到本機的流量: | input鏈 filter表 |
過濾到本地發出的流量: | output鏈 filter表 |
用途 | 鏈/表 |
---|---|
過濾轉發的流量: | forward鏈 filter表 |
過濾轉發的數據的源/目標進行修改(NAT) | pre-outing, post-routing |
iptables -L --list -t filter/nat -n, --numeric Numeric output. IP addresses and port numbers will be printed in numeric format. -F, --flush-chain -X, --delete-chain -p, --protocol protocol tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh
echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -s 172.16.93.0/24 -j SNAT --to-source 10.0.0.1 iptables -t filter -L iptables -t nat -L -n sudo iptables -F && sudo iptables -X && sudo iptables -F -t nat && sudo iptables -X -t nat iptables -P FORWARD ACCEPT iptables -t nat -L -n --line-number iptables save #保存cli規則 iptables -F #清除全部iptables規則 iptables -F -t mat # 清空nat表 參考: http://quenywell.com/10-usefull-iptables-rules-examples/ 清除IPTables規則 容許SSH入站鏈接 設置IPTables默認策略 只容許從特定網段訪問SSH服務器 容許訪問Loopback 容許外網Ping服務器 容許HTTP和HTTPS入站鏈接 容許使用郵件服務 容許使用外網DNS解析 同一條規則設置多個端口 默認是accept: 通常狀況下,INPUT、OUTPUT、FORWARD鏈的默認策略都是ACCEPT。若是想更改默認策略爲DROP,能夠用如下命令(注意大小寫): #iptables -P INPUT DROP #iptables -P OUTPUT DROP #iptables -P FORWARD DROP
- 列出規則 iptables -L - 插入一個規則 iptables -I INPUT 3 -p tcp -dport 22 -j ACCEPT - 刪除一個規則 iptables -D INPUT 3 iptables -D INPUT -s 192.168.1.2 -j DROP - 刪除全部規則 iptables -F -s ‘!’ 192.168.1.x/24 加上!排除地址
- 基於ip地址 -s 192.168.1.1 -d 10.0.0.0/8 - 基於接口 -i eth0 -o eth1 -排除參數 -s '!'192.168.1.0/24
控制到本機的網絡流量安全
iptables -A INPUT -s 192.168.1.100 -j DROP iptables -A INPUT -p tcp --dport 80 -j DROP iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j DROP iptables -A INPUT -i eth0 -j ACCEPT 注意大小i,小i是指定接口。-I -D -A
- 禁止192.168.1.0/24到10.1.1.0/24的流量 iptables -A FORWARD -s 192.168.1.0/24 -d 10.0.0.0/24 -j DROP iptables save # 報錯cli規則 iptables -F #清除全部iptables規則
iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -o eth0 -j SNAT --to-source 218.29.30.31 不加-t時默認是filter 語法參數: -I:第一行插入 -A:最後追加 -i/o:指的是數據要進入或出去所要通過的端口,如eth1,eth0,pppoe等 -p:你所要指定的協議 -s:指定來源ip,能夠是單個ip如192.168.109.131,也能夠是一個網絡 192.168.109.0/24,還能夠是一個域名如163.com,若是你填寫的是域名系統會自動解析出他的ip並在iptables裏顯示 --sport:來源端口 -d:指定目標ip --dport:目標端口 -j:執行參數ACCEPT或DROP,REJECT通常不用 -A 在指定鏈的末尾添加(append)一條新的規則 -D 刪除(delete)指定鏈中的某一條規則,能夠按規則序號和內容刪除 -I 在指定鏈中插入(insert)一條新的規則,默認在第一行添加 -R 修改、替換(replace)指定鏈中的某一條規則,能夠按規則序號和內容替換 -L 列出(list)指定鏈中全部的規則進行查看 -E 重命名用戶定義的鏈,不改變鏈自己 -F 清空(flush) -N 新建(new-chain)一條用戶本身定義的規則鏈 -X 刪除指定表中用戶自定義的規則鏈(delete-chain) -P 設置指定鏈的默認策略(policy) -Z 將全部表的全部鏈的字節和數據包計數器清零 -n 使用數字形式(numeric)顯示輸出結果 -v 查看規則表詳細信息(verbose)的信息 -V 查看版本(version) -h 獲取幫助(help) 若是配置的是INPUT(進入),則來源ip是運程ip,目標端口就是本機;OUTPUT相反 iptables的執行優先級: iptables的執行順序是自上而下,當有配置產生衝突時,前面執行的生效。
INPUT: dst IP is on the host, even it has multiple port with multiple subnet網絡
OUTPUT: src IP is from the host, either portapp
FORWARD: Neither dst IP on the host nor src IP from the host
tcp
For example, to router A INPUT is: 192.168.10.1 -> 192.168.10.199 192.168.10.1 -> 192.168.2.1 OUTPUT is: 192.168.10.199 -> x.x.x.x 192.168.2.1 -> x.x.x.x FORWARD is: 192.168.10.1 -> 192.168.2.199 192.168.10.1 -> 192.168.8.1 192.168.10.1 -> 192.168.8.199
參考: http://blog.51cto.com/lustlost/943110oop
#開啓22端口 iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT #關閉全部端口 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT DROP #開啓80端口,HTTP服務 iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT #開啓3306端口,MYSQL服務 iptables -A INPUT -p tcp --dport 3306 -j ACCEPT iptables -A OUTPUT -p tcp --sport 3306 -j ACCEPT #開啓53端口,DNS服務 iptables -A OUTPUT -p udp --dport 53 -j ACCEPT iptables -A INPUT -p udp --sport 53 -j ACCEPT iptables -A INPUT -p udp --dport 53 -j ACCEPT iptables -A OUTPUT -p udp --sport 53 -j ACCEPT #開啓20,21端口,FTP服務 iptables -A INPUT -p tcp --dport 21 -j ACCEPT iptables -A INPUT -p tcp --dport 20 -j ACCEPT iptables -A OUTPUT -p tcp --sport 21 -j ACCEPT iptables -A OUTPUT -p tcp --sport 20 -j ACCEPT #因爲FTP在上傳下載中會使用到任意的端口,故先設置FTP使用的端口,再打開端口 vi /etc/vsftpd.conf #在配置文件的最下面 加入 pasv_min_port=30001 pasv_max_port=31000 iptables -A INPUT -p tcp --dport 30001:31000 -j ACCEPT iptables -A OUTPUT -p tcp --sport 30001:31000 -j ACCEPT #打開PING iptables -A OUTPUT -p icmp -j ACCEPT iptables -A INPUT -p icmp -j ACCEPT #替換安全的22,80輸出端口 iptables -R OUTPUT 1 -p tcp --sport 22 -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -R OUTPUT 2 -p tcp --sport 80 -m state --state ESTABLISHED,RELATED -j ACCEPT #保存IPTABLES設置 service iptables save #查看是否保存成功 cat /etc/sysconfig/iptables #出現如下內容表明設置成功: # Generated by iptables-save v1.3.5 on Thu Sep 8 19:41:30 2011 *filter :INPUT DROP [103:6135] :FORWARD DROP [0:0] :OUTPUT DROP [12:964] -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT -A INPUT -p udp -m udp --sport 53 -j ACCEPT -A INPUT -p udp -m udp --dport 53 -j ACCEPT -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT -A INPUT -p tcp -m tcp --dport 20 -j ACCEPT -A INPUT -p tcp -m tcp --dport 30001:31000 -j ACCEPT -A INPUT -p icmp -j ACCEPT -A OUTPUT -p tcp -m tcp --sport 22 -m state --state RELATED,ESTABLISHED -j ACCEPT -A OUTPUT -p tcp -m tcp --sport 80 -m state --state RELATED,ESTABLISHED -j ACCEPT -A OUTPUT -p tcp -m tcp --sport 3306 -j ACCEPT -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT -A OUTPUT -p udp -m udp --sport 53 -j ACCEPT -A OUTPUT -p tcp -m tcp --sport 21 -j ACCEPT -A OUTPUT -p tcp -m tcp --sport 20 -j ACCEPT -A OUTPUT -p tcp -m tcp --sport 30001:31000 -j ACCEPT -A OUTPUT -p icmp -j ACCEPT COMMIT # Completed on Thu Sep 8 19:41:30 2011 #另外,按編號查看規則使用如下命令 iptables -L -n --line-number