日常运维(4)iptables filter案例,iptables nat表应用,iptables规则备份和恢复

iptables filter案例

例1:
需求:把80端口,21端口放行。 22端口指定IP段,其他网段的一概拒绝,写一个shell脚本
实现:shell脚本方式,脚本就相当于一串命令。
# vim /usr/local/sbin/iptables.sh 编写脚本
#!/bin/bashipt="/usr/sbin/iptables" #定义变量,应该用绝对路径,防止因为变量导致的执行失败
$ipt -F #清空之前的规则
$ipt -P INPUT DROP #默认策略,直接丢弃进来的数据包
$ipt -P OUTPUT ACCEPT #默认策略,放行出去的数据包
$ipt -P FORWARD ACCEPT #默认策略,放行其他目的地地址的数据包
$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#让相关的数据包更顺利的链接,放行RELATED,和ESTABLISHED状态的链接。
$ipt -A INPUT -s 192.168.192.0/24 -p tcp --dport 22 -j ACCEPT #允许192网段访问22端口$ipt -A INPUT -p tcp --dport 80 -j ACCEPT #把80端口的数据包放行$ipt -A INPUT -p tcp --dport 21 -j ACCEPT #把21端口的数据包放行

写完保存,直接运行/bin/sh /usr/local/sbin/iptables.sh
若要开机启动则在 /etc/rc.d/ rc.local 中加入 /bin/sh /usr/local/sbin/iptables.sh 一行即可

例2:
需求:允许本机ping其他主机,但不允许其他主机ping本机
实现:iptables -I INPUT -p icmp --icmp-type 8 -j DROP
例:
[root@localhost home]# ping -c 4 192.168.6.101 可以ping通另一个主机
PING 192.168.6.101 (192.168.6.101) 56(84) bytes of data.
64 bytes from 192.168.6.101: icmp_seq=1 ttl=128 time=1.12 ms
64 bytes from 192.168.6.101: icmp_seq=2 ttl=128 time=2.84 ms
64 bytes from 192.168.6.101: icmp_seq=3 ttl=128 time=0.951 ms
64 bytes from 192.168.6.101: icmp_seq=4 ttl=128 time=2.38 ms

--- 192.168.6.101 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 0.951/1.827/2.843/0.808 ms
[root@localhost home]# /bin/sh /usr/local/sbin/iptables.sh
[root@localhost home]# ping -c 4 www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=21.2 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=19.6 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=128 time=21.3 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=128 time=19.1 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 19.110/20.358/21.394/0.992 ms
[root@localhost home]#

在Windows系统cmd下ping主机
C:\Users\Administrator> ping 192.168.192.128 未执行脚本前ping,可以ping通

正在 Ping 192.168.192.128 具有 32 字节的数据:
来自 192.168.192.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.192.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.192.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.192.128 的回复: 字节=32 时间<1ms TTL=64

192.168.192.128 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms

C:\Users\Administrator> ping 192.168.192.128 执行脚本后ping,已经ping不通了

正在 Ping 192.168.192.128 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

192.168.192.128 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),


iptables nat表应用
A机器两块网卡ens33(192.168.192.128)、ens37(192.168.100.1),ens33可以连外网,ens37仅有内网,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。

需求1:让只能连接内网的B机器上外网

A机器上打开路由转发 echo "1">/proc/sys/net/ipv4/ip_forward
A上执行iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
B上设置网关为192.168.100.1

先在虚拟机 linux1上添加网卡
例:
网络连接改为LAN区段

重命名LAN区段名

关闭虚拟机linux2的第1块网卡并设置第2块网卡和虚拟机linux1的第2块网卡同一个LAN区段


(linux1虚拟机)临时设定的ens37网卡ip为192.168.100.1,并查看所有网卡信息,且测ping百度网


(linux2虚拟机)关闭ens33网卡,临时设定ens37网卡ip192.168.100.100,并查看所有网卡信息,且测ping linux1虚拟机的ens37网卡(ping 192.168.100.1)和 百度网,可见外网不通,内网通


linux1虚拟机上,设置路由转发
[root@localhost ~]# echo "1">/proc/sys/net/ipv4/ip_forward 打开路由转发功能
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE 将来自192.168.100.0/24 ip段的数据转发至ens33网卡
linux2虚拟机上,设置网关为,192.168.100.1

再在linux2虚拟机上设置NDS为119.29.29.29



已经可以ping通外网了。

需求2:C机器只能和A通信,让C机器可以直接连通B机器的22端口

A机器上打开路由转发 echo "1">/proc/sys/net/ipv4/ip_forward

A机器上执行iptables -t nat -A PREROUTING -d 192.168.192.128 -p tcp --dport 1222 -j DNAT --to 192.168.100.100:22

A机器上执行iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.133.130

B上设置网关为192.168.100.1

例:
A机器上操作
[root@localhost ~]# iptables -F 清除A机器上的所有规则,因为之前有做路由转发,会影响数据返回
[root@localhost ~]# echo "1">/proc/sys/net/ipv4/ip_forward 打开路由转发
[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.192.128 -p tcp --dport 1222 -j DNAT --to 192.168.100.100:22 将访问A机器1122端口的数据转发至B机器22端口
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.192.128
将B机器返回的数据转发至A机器
[root@localhost ~]#

B机器上操作
将B机器的网关设置为192.168.100.1, 上一个例子已经做过,此处略过

测试:重新在xshell创建ssh连接至B机器



已经成功进入B机器

iptables规则备份

保存iptables规则

service iptables save
规则保存在 /etc/sysconfig/iptables文件中

备份iptables规则

把iptables规则备份到文件中
iptables-save > /home/my.ipt

恢复备份的规则
iptables-restore < /home/my.ipt


猜你喜欢

转载自blog.csdn.net/langyue919/article/details/80263269