第7周第4课:iptables filter表案例、iptables nat表应用

10.15 iptables filter表案例

  • 使用脚本设定规则

案例一:只针对filter表,预设策略INPUT链DROP,然后针对192.168.159.0/24开通22端口,对所有网段开放80端口,对所有网段开放21端口。因为规则较多,可以采用写脚本。

[root@greenfinch ~]# vi /usr/local/sbin/iptables.sh      ## 创建脚本

#!/bin/bash
ipt="/usr/sbin/iptables"
      ## 定义一个变量——iptables命令(定义变量时尽量使用绝对路径,避免环境变量的影响)

$ipt -F            ## 清空原有规则
$ipt -P INPUT DROP     ## 定义INPUT链的默认策略为全部丢掉
$ipt -P OUTPUT ACCEPT    ## 定义OUTPUT链的默认策略为全部放行
$ipt -P FORWARD ACCEPT  ## 定义FORWARD链的默认策略为全部放行

$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
      ## -m是指定检测状态,--state指定数据包状态(配合-m使用)。对RELATED,ESTABLISHED两种状态放行。

$ipt -A INPUT -s 192.168.159.0/24 -p tcp --dport 22 -j ACCEPT  ## 对这个网段的数据包放行
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT             ## 80端口放行
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT           ## 21端口放行

执行该脚本中的规则:sh /usr/local/sbin/iptables.sh

[root@greenfinch ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   32  2212 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     tcp  --  *      *       192.168.159.0/24     0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:21

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 20 packets, 2832 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  • icmp示例

案例二:只允许本机访问外网,不允许外网访问本机。关键命令:

iptables -I INPUT -p icmp --icmp-type 8 -j DROP

[root@greenfinch ~]# service iptables restart
Redirecting to /bin/systemctl restart  iptables.service


[root@greenfinch ~]# iptables -nvL          ## 查看防火墙是否关闭
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   32  2212 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 20 packets, 3072 bytes)
 pkts bytes target     prot opt in     out     source               destination         


[root@greenfinch ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[root@greenfinch ~]# ping www.gov.cn
PING uz91.v.qingcdn.com (112.25.62.147) 56(84) bytes of data.
64 bytes from 112.25.62.147 (112.25.62.147): icmp_seq=1 ttl=128 time=12.6 ms
64 bytes from 112.25.62.147 (112.25.62.147): icmp_seq=2 ttl=128 time=12.5 ms
64 bytes from 112.25.62.147 (112.25.62.147): icmp_seq=3 ttl=128 time=12.0 ms
64 bytes from 112.25.62.147 (112.25.62.147): icmp_seq=4 ttl=128 time=12.5 ms
^C
--- uz91.v.qingcdn.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 12.034/12.449/12.666/0.257 ms

Markdown

10.16/10.17/10.18 iptables nat表应用

nat表应用案例

A机器两块网卡ens33(192.168.133.130)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络。B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。

准备工作:

  • 准备两台虚拟机

A机器添加一块网卡ens37,其网络连接方式选择“LAN区段模式”(VM软件,编辑虚拟机设置-添加-网络适配器,取消“启动时连接”-LAN区段-添加),开机后并设定IP为192.168.100.1

Markdown

B机器关闭原有网卡连接,新增网卡ens37,开机后设定IP为192.168.100.100(网络连接模式选择LAN区段,并和A机器中的ens37网卡选择相同区段)

  • 设置ens37的IP:

方法1:复制ens33的配置文件,然后在配置文件内进行设置。

方法2:直接在命令行使用ifconfig命令:ifconfig ens37 192.168.100.1/24

该方法只是临时设定IP,重启后会丢失。要永久设置,只能把参数存于配置文件中。可以转用方法1。

  1. 设置A机器的ens37网卡的IP
[root@greenfinch ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.159.128  netmask 255.255.255.0  broadcast 192.168.159.255
        inet6 fe80::6b6b:ba8d:598a:f7a3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:13:dd:f4  txqueuelen 1000  (Ethernet)
        RX packets 559  bytes 52364 (51.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 409  bytes 59549 (58.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.159.159  netmask 255.255.255.0  broadcast 192.168.159.255
        ether 00:0c:29:13:dd:f4  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.1  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::20c:29ff:fe13:ddfe  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:13:dd:fe  txqueuelen 1000  (Ethernet)
        RX packets 74  bytes 11920 (11.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 113  bytes 15338 (14.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  1. 设置B机器的ens37网卡的IP。在VM软件的B虚拟机上操作
ifdown ens33
ifconfig ens37 192.168.100.100/24

验证能否和A机器ens37进行通信互联

Markdown

需求1:可以让B机器连接外网

echo "1" > /proc/sys/net/ipv4/ip_forward

注释:该命令是更改内核设置,打开路由转发功能。默认值是0。

步骤一:A机器打开路由转发

[root@greenfinch ~]# cat /proc/sys/net/ipv4/ip_forward
0
[root@greenfinch ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
[root@greenfinch ~]# !cat
cat /proc/sys/net/ipv4/ip_forward
1

步骤二:在A机器的nat表中增加一条规则,-o选项后面跟设备名称,表示出口网卡,MASQUERADE是伪装、冒充的意思。

[root@greenfinch ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

[root@greenfinch ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      ens33   192.168.100.0/24     0.0.0.0/0 

步骤三:为B设置网关(为A机器ens37的IP):

Markdown

步骤四:配置DNS(编辑DNS配置文件)

编辑文件:vim /etc/resolv.conf,在文件中增加一行:nameserver 119.29.29.29

然后使用ping命令检测网络是否通畅。

注意:此时B机器可以连通外网,但是外网机器无法访问B机器,A机器的作用就类似于一个路由器。

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

步骤一:A机器打开路由转发

echo "1" > /proc/sys/net/ipv4/ip_forward

该命令是更改内核设置,打开路由转发功能,默认值是0。

步骤二:在A机器的nat表中增加2条规则

执行该步骤前先清除nat表原有规则

iptables -t nat -D POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

iptables -t nat -nvL

增加2条规则:

iptables -t nat -A PREROUTING -d 192.168.159.128 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22

iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.159.128

步骤三:为B机器设置网关为A机器ens37的IP:

route add default gw 192.168.100.1

转载于:https://my.oschina.net/greenfinch/blog/1613220

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值