2018-06-11 第十三次课

第十三次课 Iptables防火墙(上)

目录

一、 firewalld和iptables
二、 netfilter5表5链介绍
三、 iptables语法
四、 iptables filter表案例
五、 iptables nat表应用


一、 firewalld和iptables

1.Selinux(Security Enhanced Linux)

初学者最好是先关闭Selinux,等有一定经验之后再考虑开启selinux。

查看selinux状态:

[root@bluequark ~]# getenforce 
Enforcing

临时关闭selinux

[root@bluequark ~]# setenforce 0
[root@bluequark ~]# getenforce 
Permissive

永久关闭selinux

[root@bluequark ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
//此处修改为SELINUX=disabled
SELINUX=enforcing           
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

或者

[root@bluequark ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@bluequark ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
//两种方法修改完配置文件/etc/selinux/config后均需要重启才能生效,可结合临时关闭selinux的方法,等有空闲再重启。

2.防火墙

两种防火墙管理机制(二者均基于Linux内核的 Netfilter)

iptables Centos6及以前版本

扫描二维码关注公众号,回复: 1579566 查看本文章

firewalld Centos7开始引进

两种防火墙管理机制的区别

gMwGd.png

firewalld 和 iptables service之间最本质的不同是

iptables service 在 /etc/sysconfig/iptables 中储存配置,而 firewalld 将配置储存在
/usr/lib/firewalld/ 和 /etc/firewalld/ 中的各种XML文件里

使用 iptables service,每一个单独更改意味着清除所有旧有的规则和从/etc/sysconfig/iptables里读取所有新的规则 ,然而使用firewalld却不会再创建任何新的规则 ;仅仅运行规则中的不同之处。

在Centos7中使用iptables服务

//停止firewalld服务
[root@lanquark ~]# systemctl stop firewalld.service 
//取消firewalld开机启动
[root@lanquark ~]# systemctl disable firewalld.service 
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//安装iptables服务
[root@lanquark ~]# yum -y install iptables-services
//设定iptables开机启动
[root@lanquark ~]# systemctl enable iptables.service 
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
//启动iptables
[root@lanquark ~]# systemctl start iptables.service 
[root@lanquark ~]# systemctl status iptables.service 
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
Active: active (exited) since Tue 2018-06-12 20:55:50 CST; 47s ago
Main PID: 1913 (code=exited, status=0/SUCCESS)

Jun 12 20:55:50 lanquark.com systemd[1]: Starting IPv4 firewall with iptables...
Jun 12 20:55:50 lanquark.com iptables.init[1913]: iptables: Applying firewall rules: [  OK  ]
Jun 12 20:55:50 lanquark.com systemd[1]: Started IPv4 firewall with iptables.


二、netfilter5表5链介绍

默认情况下,Linux系统的iptables中定义了五个表:filter表、nat表、mangle表、raw表、security表。

1.filter为默认的表。主要用于一般的包过滤,包含三个链:

INPUT:主要与进入Linux主机的包有关

OUTPUT:主要与Linux要发送的包有关

FORWARD:与本机无关,只是将包转发到其他主机中,与nat表的关系比较大

2.nat表主要用于要转发的包,一般与本机无关。包括三个链:

PREROUTING:在进行路由判断前所要进行的规则(DNAT/REDIRECT)

POSTROUTING:进行路由判断后要进行的规则(SNAT/MASQUERADE)

OUTPUT:与发送出去的包有关

3.mangle:主要用于改写分组数据的相关包,一般来说使用较少。

4.raw:可以对收到的数据包在连接跟踪前进行处理。raw表的优先级最高,经常用在那些不需要nat的情况下,以提高性能。

5.security用于强制访问MAC的网络规则。

iptables常用的五个链

PREROUTING:在内核进行IP路由计算前所要进行的规则(DNAT/REDIRECT)

INPUT:主要与进入Linux主机的包有关

FORWARDING:与本机无关,只是将包转发到其他主机中,与nat表的关系比较大

OUTPUT:由本机产生,向外发送

POSTROUTING:在内核进行IP路由判断后要进行的规则(SNAT/MASQUERADE)

iptables处理数据包的过程:

当一个数据包进入网卡时,他首先进入PREOUTING链(数据包进入路由之前)-然后判断目标IP是否本机。

如果数据包是进入本机的,他会到达INPUT链(通过路由表后目的地为本机),数据包到达INPUT链后

进入本机内核,然后内核进行处理,处理完到OUTPUT链(由本机产生,向外转变)最后到

POSTROUTING(发送到网卡接口之前)如果不是进入本机的,他会到FORWARDING链(通过路由

表后,目的地不为本机)最后POSTROUTING链(发送到网卡接口之前)。

gOKRR.png


三、iptables语法

常用语法

iptables [-t table] command [match] [target]

-t table表示所使用的表,如果未指定该项则使用filter作为默认表。

command表示iptables所要做的任务,如插入规则 ,将规则添加到链的末尾或删除规则等。

match表示信息包与规则匹配所应具有的特征(如源地址,源ip,目标地址,源末端口,协议等)。

target表示对于匹配的包要进行的处理动作。

iptables常用参数

-A 将新的规则添加到链的最后。
-I 在现存规则链中插入一条规则。
-F 如果指定链名,则删除该链中的所有规则,否则清除所有链中的规则。
-N 创建一个新的链。
-X 删除一个用户自定义的链。
-Z 将所有chain的封包计数器归零。
-P 定义链的默认策略。
-D 通过指定要删除的规则或在链中的位置来删除规则。
-R 取代现行规则,规则被取代后并不会改变顺序。
-L 列出指定链中所有的规则
-n 不进行IP与主机名的反查,可以提高信息显示速度
-v 提供更多的信息,包括每条策略或每条规则,每条链的简单流量统计

iptables通用匹配说明

-p 匹配通信协议类型是否相符
-s 匹配数据包的源IP,可以匹配单机或网络
-d 匹配数据包的目的地IP地址
-i 匹配数据包从哪块网卡进入
-o 匹配数据包从哪块网卡送出
--sport 匹配数据包的源端口,可以匹配单个端口或一个范围
--dport 匹配数据包的目的地端口号

iptables的处理动作说明

ACCEPT 接收数据包,并且不再匹配其他规则,直接跳往下一个规则链。
DROP 丢弃数据包不予处理,并且不再匹配其他规则,直接中断过滤程序
REJECT 阻止数据包,并通知发送方,此后不再匹配其他规则,直接中断过滤程序。
REDIRECT 将数据包重新导向另一个端口(PNAT),此后将会继续匹配其他规则。
MASQUERADE 改写数据的源IP为防火墙NIC IP,也可以指定端口号对应的范围,此后直接跳往下一个规则链。
LOG 将数据包的相关信息记录到日志中,并且之后会继续匹配其他规则。
SNAT 改写数据包的源IP为某一指定的IP或IP范围。也可以指定端口号对应的范围,此后将直接跳往下一个规则链。
DNAT 改写数据包的目的IP为某一指定的IP或IP范围,也可以指定端口号对应的范围,些后将会直接跳往下一个规则链。
MIRROR 将源IP与目的IP对调,将数据包送回,此后中断过滤程序。
QUEUE 将数据包放入队列中,交给其他程序处理,不再进行其他规则匹配,直接中断过滤程序。
RETURN 结束当前规则链中的过滤,返回主规则链继续过滤。

查看iptables规则: iptables -nvL

[root@lanquark ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  560 43280 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
 1792  350K 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 411 packets, 93020 bytes)
 pkts bytes target     prot opt in     out     source               destination        

列出filter表的规则:iptables -L

[root@lanquark ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

列出nat表规则

[root@lanquark ~]# 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        

清空规则:iptables -F

[root@lanquark ~]# iptables -F
[root@lanquark ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    

清零数据包计数器:iptables -Z

[root@lanquark ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   30  1980 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
    2   402 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 16 packets, 1536 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@lanquark ~]# iptables -Z
[root@lanquark ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    6   428 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
    1   201 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 4 packets, 432 bytes)
 pkts bytes target     prot opt in     out     source               destination         

使用-P参数定义默认策略

//将本机的默认INPUT设为DROP,
//将本机的默认OUTPUT、FORWARD设为ACCEPT
[root@lanquark ~]# iptables -P INPUT DROP
[root@lanquark ~]# iptables -P OUTPUT ACCEPT
[root@lanquark ~]# iptables -P FORWARD ACCEPT
[root@lanquark ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

保存规则

[root@lanquark ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
//规则保存配置文件:/etc/sysconfig/iptables

载入默认规则

[root@lanquark ~]# systemctl restart iptables.service 

增加规则

在规则链的最后附加一条新规则:iptables -A chain rules -j target
在指定位置添加一条新规则:iptables -I chain rulenum rules -j target

//拒绝192.168.122.0及10.1.10.0网络主机访问
[root@lanquark ~]# iptables -A INPUT -s 192.168.122.0/24 -j DROP
[root@lanquark ~]# iptables -A INPUT -s 10.1.10.0/24 -j DROP
//由于已经拒绝了所有来自10.1.10.0网段的主机数据,现在添外 10.1.10.22
[root@lanquark ~]# iptables -I INPUT 1 -s 10.1.10.22 -j ACCEPT
[root@lanquark ~]# iptables -nvL
Chain INPUT (policy ACCEPT 3 packets, 603 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      *       10.1.10.22           0.0.0.0/0           
  462 33904 ACCEPT     tcp  --  *      *       192.168.1.9          0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       192.168.1.0/24       0.0.0.0/0            tcp dpt:22
    0     0 DROP       all  --  *      *       192.168.122.0/24     0.0.0.0/0           
    0     0 DROP       all  --  *      *       10.1.10.0/24         0.0.0.0/0           

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

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

阻止所有流向攻击者IP地址的数据包

[root@lanquark ~]# iptables -A OUTPUT -d 123.45.78.0/24 -j DROP

删除规则:-D

删除规则的两种方式

一种是指定要删除规则在规则链的位置(这种比较方便,尤其是不记得原始规则是如何写的情况下)

//技巧:查看规则的编号
[root@lanquark ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 140 packets, 28002 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       10.1.10.22           0.0.0.0/0           
2      595 43784 ACCEPT     tcp  --  *      *       192.168.1.9          0.0.0.0/0            tcp dpt:22
3        0     0 ACCEPT     tcp  --  *      *       192.168.1.0/24       0.0.0.0/0            tcp dpt:22
4        0     0 DROP       all  --  *      *       192.168.122.0/24     0.0.0.0/0           
5        0     0 DROP       all  --  *      *       10.1.10.0/24         0.0.0.0/0           

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

Chain OUTPUT (policy ACCEPT 104 packets, 11360 bytes)
num   pkts bytes target     prot opt in     out     source               destination   
//删除3号规则  iptables -D 链 数字
[root@lanquark ~]# iptables -D INPUT 2
[root@lanquark ~]# iptables -nvL
Chain INPUT (policy ACCEPT 3 packets, 603 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      *       10.1.10.22           0.0.0.0/0           
   29  1940 ACCEPT     tcp  --  *      *       192.168.1.0/24       0.0.0.0/0            tcp dpt:22
    0     0 DROP       all  --  *      *       192.168.122.0/24     0.0.0.0/0           
    0     0 DROP       all  --  *      *       10.1.10.0/24         0.0.0.0/0           

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

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

一种是指定要删除的规则

//使用指定要删除的规则的方法来删除相应的规则
[root@lanquark ~]# iptables -D INPUT -s 10.1.10.22 -j ACCEPT


四、iptables filter表案例

#!/bin/bash
ipt=/usr/sbin/iptables
//清空默认策略
$ipt -F
//INPUT链默认规则拒绝
$ipt -P INPUT  DROP
//OUTPUT链默认规则允许
$ipt -P OUTPUT ACCEPT
//FORWARD链默认规则允许
$ipt -P FORWARD ACCEPT
//INPUT链NEW,RELATED,ESTABLISHED状态的允许
$ipt -A INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
//允许源地址是192.168.133.0网段的主机访问本机的22
$ipt -A INPUT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT
//本机80端口的访问允许
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT
//本机21端口的访问允许
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT
#$ipt -A INPUT -p udp --sport 53 -j ACCEPT
#$ipt -A INPUT -p tcp --sport 80 -j ACCEPT
#$ipt -A INPUT -p tcp --sport 443 -j ACCEPT
//icmp允许
$ipt -A INPUT -p icmp -j ACCEPT
[root@lanquark ~]# vim iptables_cmd.sh
"iptables_cmd.sh" [New File]                                                      0,0-1         All
  1 #!/bin/bash
  2 ipt=/usr/sbin/iptables
  3 $ipt -F
  4 $ipt -P INPUT  DROP
  5 $ipt -P OUTPUT ACCEPT
  6 $ipt -P FORWARD ACCEPT
  7 $ipt -A INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
  8 $ipt -A INPUT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT                                   
  9 $ipt -A INPUT -p tcp --dport 80 -j ACCEPT
 10 $ipt -A INPUT -p tcp --dport 21 -j ACCEPT
 11 #$ipt -A INPUT -p udp --sport 53 -j ACCEPT
 12 #$ipt -A INPUT -p tcp --sport 80 -j ACCEPT
 13 #$ipt -A INPUT -p tcp --sport 443 -j ACCEPT
 14 $ipt -A INPUT -p icmp -j ACCEPT

[root@lanquark ~]# chmod u+x iptables_cmd.sh 
[root@lanquark ~]# ./iptables_cmd.sh 
[root@lanquark ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   26  1986 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW,RELATED,ESTABLISHED
    0     0 ACCEPT     tcp  --  *      *       192.168.133.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
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

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

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

关于icmp包有一个比较常用的应用

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

这条命令,作用是禁止他人ping你的机器,而你可以ping通其他机器。


五、iptables nat表应用

A机器2块网卡分别是外网和内网,B机器只有内网

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

实验环境准备

A机器

增加一块网卡,网络连接设为lan区段,ip地址设为192.168.2.1

gOfOB.png

[root@lanquark network-scripts]# ifconfig
//外网网卡
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.211  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::5114:2b77:d59a:bc78  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2f:92:ee  txqueuelen 1000  (Ethernet)
        RX packets 5995  bytes 646928 (631.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2964  bytes 403865 (394.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
//内网网卡
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.1  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::1e53:526f:a4af:a29d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:2f:92:f8  txqueuelen 1000  (Ethernet)
        RX packets 208  bytes 17459 (17.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 212  bytes 21453 (20.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

B机器,网卡连接设为lan

测试环境是否ok

//从A机器测试, 可以ping通外网,也可以ping通B机器
[root@lanquark ~]# ping www.163.com
PING 163.xdwscache.ourglb0.com (112.90.246.87) 56(84) bytes of data.
64 bytes from ns.local (112.90.246.87): icmp_seq=1 ttl=58 time=2.03 ms
64 bytes from ns.local (112.90.246.87): icmp_seq=2 ttl=58 time=1.98 ms
^C
--- 163.xdwscache.ourglb0.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 1.983/2.010/2.037/0.027 ms
[root@lanquark ~]# ping 192.168.2.2
PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.
64 bytes from 192.168.2.2: icmp_seq=1 ttl=64 time=0.842 ms
64 bytes from 192.168.2.2: icmp_seq=2 ttl=64 time=1.01 ms
^C
--- 192.168.2.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.842/0.930/1.018/0.088 ms

从B机器测试,B机器ping不通外网,可以pingA机器内网网卡

至此,实验环境准备OK。

若要实现NAT功能,在A机器必须打开路由转发功能

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

还需要在A机器添加一条规则

[root@lanquark ~]# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o ens32 -j MASQUERADE
[root@lanquark ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 31 packets, 2675 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

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

Chain POSTROUTING (policy ACCEPT 10 packets, 760 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   20  1497 MASQUERADE  all  --  *      ens32   192.168.2.0/24       0.0.0.0/0           

将B机器的网关设为A机器的内网网卡,即(192.168.2.1)

从B机器上测试,可以ping通外网。需求满足。

需求2:window主机连接B机器,因为B机器在外面是ping不通,所以我们需要通过A机器做一个端口映射到B来远程连接。

继续利用上一需求的环境。

清空配置

[root@lanquark ~]# iptables -t nat -D POSTROUTING -s 192.168.2.0/24 -o ens32 -j MASQUERADE
[root@lanquark ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 1 packets, 76 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 1 packets, 76 bytes)
 pkts bytes target     prot opt in     out     source               destination  

清空配置后B机器只能ping通A机器的内网网卡。

打开ip转发

//上一实验已经打开,验证一下
[root@lanquark ~]# cat /proc/sys/net/ipv4/ip_forward
1

在A机器上增加规则

[root@lanquark ~]# iptables -t nat -A  PREROUTING -d 192.168.1.211 -p tcp --dport 1122 -j DNAT --to 192.168.2.2:22
[root@lanquark ~]#  iptables -t nat -A POSTROUTING -s 192.168.2.2 -j SNAT --to 192.168.1.211

B机器的默认网关设为A机器内网网卡(192.168.2.1)。

验证

从windows远程ssh到B机器

从B机器验证

可正常实现功能。满足需求

猜你喜欢

转载自www.cnblogs.com/minn/p/9175885.html
今日推荐