一、首先熟悉一些名詞
容器:瓶子叶组,放東西的
表(table): 存放鏈的容器
鏈(chain): 存放規(guī)則的容器
規(guī)則(policy): 準(zhǔn)許ACCPT或拒絕DROP規(guī)則
二拯田、iptables執(zhí)行過程
1.防火墻是層層過濾的,實(shí)際是按照配置規(guī)則的順序從上到下扶叉,從前到后進(jìn)行過濾的勿锅。
2.匹配 表示 阻止還是通過,數(shù)據(jù)包就不再向下匹配新的規(guī)則 枣氧。
3.如果規(guī)則中沒有明確表明是阻止還是通過的溢十,也就是沒有匹配規(guī)則,向下進(jìn)行匹配达吞,直到匹配默認(rèn)規(guī)則得到明 確的阻止還是通過张弛。
4.防火墻的默認(rèn)規(guī)則是所有規(guī)則執(zhí)行完才執(zhí)行的。
image.png
三酪劫、四表五鏈
表:
filter(默認(rèn)吞鸭,防火墻功能,準(zhǔn)許 拒絕)
nat (nat功能:共享上網(wǎng)覆糟,端口映射)
mangle
raw
四表五鏈流程:
四刻剥、安裝部署
下載
yum install -y iptables-services
加載內(nèi)核模塊(臨時生效,寫入rc.local永久生效)
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
lsmod | egrep 'ipt|nat|filter'
systemctl start iptables.service
systemctl enable iptables.service
五滩字、常用命令-filter表
1.查看規(guī)則
查看默認(rèn)表規(guī)則
iptables -nL
查看nat表規(guī)則
iptables -t nat -nL
2.清空規(guī)則
iptables -F
iptables -X
iptables -Z
-F造虏,--?ush 清除所有規(guī)則,不會處理默認(rèn)的規(guī)則
-X 刪除用戶自定義的鏈
-Z 鏈的計(jì)數(shù)器清零(數(shù)據(jù)包計(jì)數(shù)器與數(shù)據(jù)包字節(jié)計(jì)數(shù)器)
3.刪除INPUT的第一條規(guī)則
iptables -D INPUT 1
依此類推
3.配置規(guī)則
3.1.IP相關(guān)
1.禁止10.0.0.7訪問22端口
iptables -I INPUT -s 10.0.0.7 -p tcp --dport 22 -j DROP
2.禁止172.16.1.0網(wǎng)段訪問8080端口
iptables -I INPUT -s 172.16.1.0/24 -p tcp --dport 8080 -j DROP
3.只允許10.0.0.0網(wǎng)段訪問(設(shè)置白名單)
iptables -I input ! -s 10.0.0.0/24 -j DROP
3.2.端口相關(guān)
禁止用戶訪問1024-65535端口
iptables -I INPUT -p tcp --dport 1024:65535 -j DROP
禁止用戶訪問80端口和443端口
iptables -I INPUT -p tcp -m multiport --dport 80,443 -j DROP
3.3禁止被ping
通過內(nèi)核參數(shù)設(shè)置禁止被ping
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
sysctl -p
通過防火墻設(shè)置禁止被ping
iptables -I INPUT -p icmp--icmp-type 8 -j DROP
3.4生產(chǎn)用法
允許22端口接入
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
流量進(jìn)來出去走lo網(wǎng)卡
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
允許80麦箍,443端口接入
iptables -I INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT
修改默認(rèn)規(guī)則為拒絕
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
設(shè)置10.0.0.0網(wǎng)段為白名單
iptables -A INPUT -s 10.0.0.0/24 -j ACCEPT
六漓藕、常用命令-nat表
1.共享上網(wǎng)
把172.16.1.7轉(zhuǎn)換為10.0.0.61訪問外網(wǎng)
iptables -t nat -A POSTROUTING -s 172.16.1.7 -j SNAT --to-source 10.0.0.61
把172.16.1.0網(wǎng)段轉(zhuǎn)換為10.0.0.61訪問外網(wǎng)
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 10.0.0.61
開啟轉(zhuǎn)發(fā)規(guī)則
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p
2.端口轉(zhuǎn)發(fā)
客戶端訪問10.0.0.61 9000端口 跳轉(zhuǎn)至172.16.1.7 22端口
iptables -t nat -A POSTROUTING -d 10.0.0.61 -p tcp --dport 9000 -j DNAT --to-destination 172.16.1.7:22
注意查看是否配置了轉(zhuǎn)發(fā)規(guī)則
tail -1 /etc/sysctl.conf
net.ipv4.ip_forward=1
#注意:內(nèi)網(wǎng)的服務(wù)器需要把網(wǎng)關(guān)設(shè)置為防火墻的IP
3.IP轉(zhuǎn)發(fā)
添加臨時IP10.0.0.62
ip a a 10.0.0.62/24 dev eth0 lable eth0:0
訪問10.0.0.62,自動跳轉(zhuǎn)至172.16.1.7
iptables -t nat -A PREROUTING -d 10.0.0.62 -j DNAT --to-destination 172.16.1.7
刪除臨時IP
ip a del 10.0.0.62/24 dev eth0