防火墙(上)

第十三次课 防火墙(上)

目录linux

1、 firewalld和iptables
2、 netfilter5表5链介绍
3、 iptables语法
4、 iptables filter表案例
5、 iptables nat表应用vim


1、 firewalld和iptables

1.Selinux(Security Enhanced Linux)windows

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

查看selinux状态:网络

[root@bluequark ~]# getenforce 
Enforcing

临时关闭selinuxssh

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

永久关闭selinuxtcp

[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及之前版本3d

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.

2、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


3、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

4、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通其余机器。


5、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机器验证

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

相关文章
相关标签/搜索