iptables配置过程:
防火墙初始化配置:
iptables -F 删除链上所有信息
iptables -Z 清除所有计数器信息
iptables -X 清除用户自定义链信息
防火墙查看方法:
iptables -nL -v --line-numbers -t xxx
查看防火墙配置信息
-L 列表显示防火墙配置的规则信息
-n 规则中地址或者端口信息以数字方式显示
-v 详细配置信息
-t 指定表中的配置信息
–line-numbers 显示配置规则需要的信息
删除已有配置规则信息
方式一:根据已有规则进行删除
iptables -D INPUT ! -s 10.0.0.1 -p tcp --dport 22 -j DROP
方式二:根据规则序号进行删除
iptables -D INPUT 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16-A 指定在那个链上添加规则 -I 指定在那个链上插入规则 -D 指定在那个链上删除规则 -R 指定在那个链上修改规则 -p 指定规则的协议(tcp udp icmp) --dport 目标端口信息 --sport 源端口信息 -s 源地址 -d 目标地址 -i 数据包进入网卡 -o 数据包离开网卡 -j 对匹配信息做什么处理 -m 匹配扩展防火墙配置 multiport 识别多端口配置信息 limit 实现流量限速功能
nat表配置方法(映射)
postrouting原理:先路由再映射
内网用户访问外网服务器
云主机-----eth1 iptables eth0-----内网用户
内网用户访问外网服务器时
①:172.16.1.1经过eth0路由到外网网卡eth1中
②:eth1将路由过来的数据包进行映射,将172.16.1.1映射为10.0.0.1 发送给外网云主机
③:云主机响应的数据包从eth1进入,先反向映射为内网地址172.16.1.1,再路由给eth0,发送到内网主机
prerouting 先映射再路由
外网用户访问内网服务器
用户-----eth1 iptables eth0 -----内网主机
①:用户访问内网先访问到iptables外网网卡eht1 ;
②:eht1外网接收到数据包时,先根据链中的信息进行映射,在发送给内网网卡eth0
③:内网网卡eth0接收到数据包时,进行路由给内网主机。
案例一:内网主机访问外网
第一个历程:配置内网主机路由信息
关闭外网网卡eth0
1
2ifdown eth0
此时会出现无法使用xshell登录的情况
使用ssh命令利用linux远程到内网主机上
修改eth1的网卡配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22[root@web01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=172.16.1.7 PREFIX=16 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth1 UUID=a2dcc8cb-069f-3e39-b0b3-401a6996c822 DEVICE=eth1 ONBOOT=yes AUTOCONNECT_PRIORITY=-999 DNS1=223.5.5.5 GATEWAY=172.16.1.81
DNS1=223.5.5.5
GATEWAY=172.16.1.81
这两条是新加的,其中172.16.1.81表示iptables的内网IP地址;
使用iptables的外网网卡作为网关,使得数据包经过内网主机发送给网关(172.16.1.81)
第二个历程:让iptables服务器开启路由转发功能
1
2
3
4
5
6
7
8
9
10
11
12
13[root@iptables ~]# cat /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). net.ipv4.ip_forward = 1
1
2
3[root@iptables ~]# sysctl -p net.ipv4.ip_forward = 1
使配置生效
第三历程:iptables服务器的配置:
1
2iptables -A INPUT -s 10.0.0.0/24 -p tcp --dport 22 -j ACCEPT(防止将自己给提出门外)
1
2iptables -A INPUT -s 172.16.1.0/16 -j ACCEPT(允许内网主机访问)
1
2
3iptables -P INPUT DROP (其余禁止) iptables -P FORWARD DROP(其余禁止)
百度外网------eth1-iptables–eth0------内网用户
1
2
3
4①:内网用户的数据包172.16.1.7发送给自己的网关(也就是iptables的内网主机地址172.16.1.81) ②:iptables主机开启路由转发功能,使用nat表做映射,将172.16.1.7映射为10.0.0.81 发送给百度外网
1
2iptables -t nat -P POSTROUTING -s 172.16.1.7/16 -o eth0 -j SNAT --to-source 10.0.0.81
解释:
-t 指定nat表
-P 指定表中的链
-s 指定源地址
-o 指定出去的网卡
-j 指定转换的动作
–to-source 指定转换后的地址
如果此时访问(内网用户访问外网)其实是不通的
排错思路:
1 查看nat表的映是否起作用
iptables -t nat -nL -v
2 防火墙各种表和链的匹配顺序关系
简单理解匹配顺序(表和链的匹配顺序)
目的:
①–数据–>nat:prerouting----路由分叉口----filter:INPUT------路由选择-----①nat OUTPUT ②filter output
流经:
②—数据—>nat:prerouting----->路由分岔口—>filter:FORWARD ---->nat:postrouting
提示:利用匹配原理图时,站在最终想实现的结果上去运用匹配流程图,比如案例一中,eth0是流经的网卡,所以要查看filter:forward是否通畅。
问题原因就是filter的forward链改为accept,这也是解决方式之一,将filter链改为放行。
方式二:写filter表中的forward链中的规则。
流经也是又去有回的。
1
2
3
4
5
6iptables -A FORWAED -i eth1 -s 172.16.1.7 -j ACCEPT iptables -A FORWARD -o eth0 -s 172.16.1.7 -j ACCEPT [root@iptables ~]# iptables -A FORWARD -i eth0 -d 172.16.1.7 -j ACCEPT [root@iptables ~]# iptables -A FORWARD -o eth1 -d 172.16.1.7 -j ACCEPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17[root@iptables ~]# iptables -nL -v Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 7668 568K ACCEPT tcp -- * * 10.0.0.0/24 0.0.0.0/0 tcp dpt:22 298 38232 ACCEPT all -- * * 172.16.0.0/16 0.0.0.0/0 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 9 600 ACCEPT all -- eth1 * 172.16.1.7 0.0.0.0/0 0 0 ACCEPT all -- * eth0 172.16.1.7 0.0.0.0/0 1 76 ACCEPT all -- eth0 * 0.0.0.0/0 172.16.1.7 0 0 ACCEPT all -- * eth1 0.0.0.0/0 172.16.1.7 Chain OUTPUT (policy ACCEPT 17 packets, 1772 bytes) pkts bytes target prot opt in out source destination [root@iptables ~]#
案例二:实现外网用户访问内网主机
prerouting 先映射再路由
用户-----eth0–iptables–eth1-----内网172.16.1.7
用户访问 内网172.16.1.7 从eth0进入数据包
,eth0接受数据包后先进行映射再进行路由转发。eth0为映射,eth0(内网网卡)为路由。
iptables服务器上配置
1
2[root@iptables ~]# iptables -t nat -A PREROUTING -d 10.0.0.81 -p tcp --dport 9000 -i eth0 -j DNAT --to-destination 172.16.1.7:22
解释:指定nat表 指定prerouting链 -d 目标地址 -p tcp 协议 --dort 目标端口 -i eth0 进入 -j 动作 作为目标转换 DNAT --to-destination 转换为172.16.1.7:22
保证可以ping通10.0.0.81防火墙外网地址
1
2iptables -A INPUT -p icmp -j ACCEPT
测试:
1
2[c:~]$ ssh 10.0.0.81 9000
如果:外网想访问内网多台主机;建议配置vpn
iptables防火墙配置过程:
a 防火墙包过滤配置方法filter表
b 防火墙包映射配置方法nat表
iptables防火墙企业应用
第一个历程:保存自己已有的默认配置信息
cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
第二个历程:重新初始配置
iptables -Z /-F/-X
第三历程:将自己不要提出门外,注意一开始的默认配置规则
第四个历程:默认策略规则
iptables -P INPUT DROP
iptables -P FORWARD DROP
第五个历程:是否需要实现禁用ping功能
允许自己ping自己 iptables -A INPUT -i lo -p icmp -j ACCEPT
第六个历程:设置允许访问的白名单
打开dns解析:
1
2
3
4iptables -A INPUT -p tcp --sport 53 -j ACCEPT iptables -A INPUT -p udp --sport 53 -j ACCEPT
允许公司合作方访问:iptables -A INPUT -s 合作方地址 -p 协议 --dport 服务端口 -j ACCEPT
第七个历程:实现nat地址映射
iptables -t nat -A POSTROUTING XXX
内网主机访问外网
iptables -t nat -A PREROUTING XXX
实现外网主机访问内网
第九个历程:配置完毕,进行保存
iptables配置是立即生效的,但是重启会丢失配置
iptables-save > /etc/sysconfig/iptables
最后
以上就是爱笑蜜粉最近收集整理的关于iptables配置nat表以及iptables在企业中的使用的全部内容,更多相关iptables配置nat表以及iptables在企业中内容请搜索靠谱客的其他文章。
发表评论 取消回复