2018-08-25笔记

firewalld和netfilter

Linux防火墙:netfilter
Selinux 临时关闭:setenforce 0

[root@linux-131 ~]# getenforce   --查看selinux状态
Enforcing                       --打开
[root@linux-131 ~]# setenforce 0  –-临时关闭
[root@linux-131 ~]# getenforce
Permissive                      --关闭

Selinux 永久关闭:/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=enforcing
--将这里的值改为disabled,然后重启系统,注意不要改下面的值,否则启动不来。
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

centos7以前使用的防火墙是netfilter
centos7以后使用的防火墙是firewalld

在centos7上先把firewalld关闭,在开启netfilter

systemctl disable firewalld          --不让开机启动
systemctl stop firewalld            --停止服务
yum install -y iptables-services      --开启netfilter,安装iptables包
systemctl enable iptables           --设置开机启动
systemctl start iptables             --开启服务
iptables –nvL  --查看iptables规则

netfilter五表五链

5个表

filter:
                  This is the default table (if no -t option  is  passed).  It  contains  the  built-in
                  chains  INPUT  (for  packets  destined  to local sockets), FORWARD (for packets being
                  routed through the box), and OUTPUT (for locally-generated packets).

nat:
                  This table is consulted when a packet that creates a new connection  is  encountered.
                  It consists of three built-ins: PREROUTING (for altering packets as soon as they come
                  in), OUTPUT (for altering locally-generated packets before routing), and  POSTROUTING
                  (for  altering  packets  as they are about to go out).  IPv6 NAT support is available
                  since kernel 3.7.

mangle:
                  This table is used for specialized packet alteration.  Until kernel 2.4.17 it had two
                  built-in chains: PREROUTING (for altering incoming packets before routing) and OUTPUT
                  (for altering locally-generated packets before routing).  Since kernel 2.4.18,  three
                  other  built-in  chains  are  also  supported: INPUT (for packets coming into the box
                  itself), FORWARD (for altering packets being routed through the box), and POSTROUTING
                  (for altering packets as they are about to go out).

raw:
                  This table is used mainly for configuring exemptions from connection tracking in com‐
                  bination with the NOTRACK target.  It registers at the netfilter  hooks  with  higher
                  priority and is thus called before ip_conntrack, or any other IP tables.  It provides
                  the following built-in chains: PREROUTING  (for  packets  arriving  via  any  network
                  interface) OUTPUT (for packets generated by local processes)
security:
                  This table is used for Mandatory Access Control (MAC) networking rules, such as those
                  enabled by the SECMARK and CONNSECMARK targets.  Mandatory Access Control  is  imple‐
                  mented by Linux Security Modules such as SELinux.  The security table is called after
                  the filter table, allowing any Discretionary Access Control (DAC) rules in the filter
                  table  to  take  effect before MAC rules.  This table provides the following built-in
                  chains: INPUT (for packets coming into the box itself), OUTPUT (for altering locally-
                  generated  packets  before  routing),  and FORWARD (for altering packets being routed
                  through the box).

filter 表用于过滤包,最常用的表有INPUT丶FORWARD丶OUTPUT三个链

INPUT

数据包进入的时候,我们要经过的一个链。进入本机后我们要进行一些操作,比如访问我们80端口的数据包,我们要检查一下源IP,发现可疑IP我们要禁掉,这就是INPUT的作用。

FORWARD

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

数据进入到我们的设备,但它并不会进入我们的内核里,因为这个数据包不是给你处理的,而是给另外一台机器处理的,所以他要判断一下目标地址是不是本机,如果不是本机,它就要经过FORWARD。经过FORWARD时我们也要进行一些操作,比如我们要把目标地址做一些更改,或者做一个转发。

OUTPUT

在本机产生的包,出去之前做的一些操作。比如说我这个包是发给某一个IP的,这个IP我要禁掉,把这个IP加入黑名单,只要是发往这个IP的数据包我们都给他禁掉。

nat表用于网络地址转换,有PREROUTING丶OUTPUT丶POSTROUTING三个链
OUTPUT:它和filter表的OUTPUT作用相同。
PREROUTING:这个链用来更改这个数据包,在进来的瞬间更改。
POSTROUTING:而POSTROUTING也是要更改数据包,在他们出去的那一刻更改。

managle表用于给数据包做标记,几乎用不到
raw表可以实现不追踪某些数据包
security表在centos6中并没有,用于强制访问控制(MAC)的网络规则

数据包流向分为经过本机和不经过本机两种。
经过本机:数据包—>PREROUTING—>INPUT—>OUTPUT—>POSTROUTING
不经过本机:数据包—>PREROUTING—>FORWARD—>POSTROUTING
然后我们再针对数据包所在的那一个链做一些相应的规则处理,那这个就是iptables

iptables传输数据包的过程
① 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。
② 如果数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包会经过OUTPUT链,然后到达POSTROUTING链输出。
③ 如果数据包是要转发出去的,且内核允许转发,数据包就会如图所示向右移动,经过FORWARD链,然后到达POSTROUTING链输出。
这里写图片描述

iptables的规则表和链:
表(tables)提供特定的功能,iptables内置了4个表,即filter表、nat表、mangle表和raw表,分别用于实现包过滤,网络地址转换、包重构(修改)和数据跟踪处理。
链(chains)是数据包传播的路径,每一条链其实就是众多规则中的一个检查清单,每一条链中可以有一 条或数条规则。当一个数据包到达一个链时,iptables就会从链中第一条规则开始检查,看该数据包是否满足规则所定义的条件。如果满足,系统就会根据 该条规则所定义的方法处理该数据包;否则iptables将继续检查下一条规则,如果该数据包不符合链中任一条规则,iptables就会根据该链预先定 义的默认策略来处理数据包。

Iptables采用“表”和“链”的分层结构。在REHL4中是三张表五个链。现在REHL5成了四张表五个链了,不过多出来的那个表用的也不太多,所以基本还是和以前一样。下面罗列一下这四张表和五个链。注意一定要明白这些表和链的关系及作用。
这里写图片描述
规则表:
1.filter表——三个链:INPUT、FORWARD、OUTPUT
作用:过滤数据包 内核模块:iptables_filter.
2.Nat表——三个链:PREROUTING、POSTROUTING、OUTPUT
作用:用于网络地址转换(IP、端口) 内核模块:iptable_nat
3.Mangle表——五个链:PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
作用:修改数据包的服务类型、TTL、并且可以配置路由实现QOS内核模块:iptable_mangle(别看这个表这么麻烦,咱们设置策略时几乎都不会用到它)
4.Raw表——两个链:OUTPUT、PREROUTING
作用:决定数据包是否被状态跟踪机制处理 内核模块:iptable_raw
(这个是REHL4没有的,不过不用怕,用的不多)
规则链:

1.INPUT——进来的数据包应用此规则链中的策略
2.OUTPUT——外出的数据包应用此规则链中的策略
3.FORWARD——转发数据包时应用此规则链中的策略
4.PREROUTING——对数据包作路由选择前应用此链中的规则
(记住!所有的数据包进来的时侯都先由这个链处理)
5.POSTROUTING——对数据包作路由选择后应用此链中的规则
(所有的数据包出来的时侯都先由这个链处理)
参考地址:http://www.cnblogs.com/metoy/p/4320813.html

iptables语法

1.查看iptables规则 iptables -nvL

[root@linux-131 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  375 36732 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
   11  1925 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 211 packets, 89716 bytes)
 pkts bytes target     prot opt in     out     source               destination

2.规则保存地址 /etc/sysconfig/iptables

[root@linux-131 ~]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

3.清空规则 iptables –F

[root@linux-131 ~]# iptables -F
[root@linux-131 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 10 packets, 928 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

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

这时候配置文件里规则还是存在的,要把规则保存到配置文件里去,需要使用
service iptables save保存规则
4. 重启规则,将配置文件里的规则加载到规则表里 service iptables restart

[root@linux-131 ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@linux-131 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   41  4052 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 21 packets, 3828 bytes)
 pkts bytes target     prot opt in     out     source               destination   

5.iptables –t nat -nvL//-t查看指定表,默认查看的是filter表

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

6.把计数器清零 iptables -Z

[root@linux-131 ~]# iptables -Z;iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 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 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination   

可以看到上面bytes就为0了,再次查看iptables还是会产生数据的,因为他是随时随刻都有数据产生。

7.增加一条规则(增加到最后)
iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
这里我么们没有加-t,默认就是针对filter表
iptables -A INPUT -s 192.168.188.1 -p tcp –sport 1234 -d 192.168.188.128 –
(针对的链)(指定的来源ip)(指定协议)(来源端口)(目标的ip)
-dport 80 -j DROP
(目标端口) 操作(数据直接扔掉)

[root@linux-131 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@linux-131 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  121 12012 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
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

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, 3648 bytes)
 pkts bytes target     prot opt in     out     source               destination

8.插入一条规则(插入的最前面)
iptables -I INPUT -p tcp –dport 80 -j DROP
我们不写来源ip,也不写目标ip,我们只写目标的端口,但是我们一定要指定-ptcp,一旦我们用了–dport,我们前面一定要指定协议,如果不指定,就会报错。

[root@linux-131 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@linux-131 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
  134 13224 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
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

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, 736 bytes)
 pkts bytes target     prot opt in     out     source               destination         

前后的区别:优先匹配前面的规则,一旦匹配立即执行,不会继续往下。
9.删除规则
用法和前面增加插入相同,使用-D进行删除,这种删除一般适用于记得当时添加或者插入的规则的命令
iptables -I/D INPUT -p tcp --dport 80 -j DROP
iptables -A/D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
另一种方法删除:
iptables -nvL --line-number将规则前面加上序号
iptables -D INPUT 7删除第七条规则

10.更改默认策略 iptables -P OUTPUT DROP
最好不要直接去操作,保持默认即可,有可能导致远程终端断开

所有的数据包如果没有具体规则来匹配,就按照默认策略来,比如OUTPUT链没有规则,那么他就要按照ACCEPT这个默认策略来执行,使用上面命令更改策略,那么他的默认策略就变成了DROP。

iptables filter表小案例

需求:将80端口,22端口和21端口放行,22端口需要指定IP段,只有指定IP段访问才可以,其他段一概拒绝。
脚本:
vi /usr/local/sbin/iptables.sh

#! /bin/bash
ipt="/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
$ipt -A INPUT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT

ipt是定义了一个变量,如果要执行命令,要写全局绝对路径,这样在脚本当中才不会因为环境变量问题导致命令无法执行。所以以后路写shell脚本时一定要写全局绝对路径。我们来定义一个变量,目的就是后面有许多的地方要加载它,如果写很长一段命令会很繁琐。所以我们要定义一个变量,用变量去代替他,这样看起来就更加的简单。
我们没有参数-t,所以针对的是filter表。

注释:

$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(添加规则,放行前面的状态。特别是RELATED和ESTABLISHED这两个状态,是保持通信的重要状态。所以一定要加上)
$ipt -A INUPT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT(添加规则,放行,此时IP网段可以是自己系统的网段,这样执行命令时才不会断开)
$ipt -A INUPT -P tcp --dport 80 -j ACCEPT(80端口数据包放行)
$ipt -A INUPT -P tcp --dport 21 -j ACCEPT(21端口数据包放行)

执行脚本:
sh /usr/local/sbin/iptables.sh

[root@linux-131 ~]# sh /usr/local/sbin/iptables.sh
[root@linux-131 ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   29  2852 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     tcp  --  *      *       172.16.182.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 15 packets, 2236 bytes)
 pkts bytes target     prot opt in     out     source               destination

icmp示例:
iptables -I INPUT -p icmp --icmp-type 8 -j DROP
就是ping外网的时候可以ping通,但是ping本机就ping不通了,但是不通不代表不能连接,我仅仅是将它做了一个禁ping而已,你可以ping出去,但是外面ping不进来。
做这项操作需要先将iptables恢复默认,前面的脚本是把icmp禁掉的

[root@linux-131 ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@linux-131 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[root@linux-131 ~]# ping -c 4 www.baidu.com
PING www.a.shifen.com (119.75.213.61) 56(84) bytes of data.
64 bytes from 119.75.213.61 (119.75.213.61): icmp_seq=1 ttl=128 time=36.4 ms
64 bytes from 119.75.213.61 (119.75.213.61): icmp_seq=2 ttl=128 time=32.9 ms
64 bytes from 119.75.213.61 (119.75.213.61): icmp_seq=3 ttl=128 time=67.5 ms
64 bytes from 119.75.213.61 (119.75.213.61): icmp_seq=4 ttl=128 time=33.4 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 32.935/42.613/67.527/14.448 ms

ping本机是ping不通的

bogon:~ hushuaiyang$ ping 172.16.182.131
PING 172.16.182.131 (172.16.182.131): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
Request timeout for icmp_seq 4
Request timeout for icmp_seq 5
Request timeout for icmp_seq 6
Request timeout for icmp_seq 7
^C
--- 172.16.182.131 ping statistics ---
9 packets transmitted, 0 packets received, 100.0% packet loss

删掉刚才设置的规则

[root@linux-131 ~]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP
[root@linux-131 ~]# ping -c 4 www.baidu.com
PING www.a.shifen.com (119.75.213.61) 56(84) bytes of data.
64 bytes from 119.75.213.61 (119.75.213.61): icmp_seq=1 ttl=128 time=63.1 ms
64 bytes from 119.75.213.61 (119.75.213.61): icmp_seq=2 ttl=128 time=50.5 ms
64 bytes from 119.75.213.61 (119.75.213.61): icmp_seq=3 ttl=128 time=64.6 ms
64 bytes from 119.75.213.61 (119.75.213.61): icmp_seq=4 ttl=128 time=62.9 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3009ms
rtt min/avg/max/mdev = 50.516/60.319/64.644/5.705 ms

本机也可以ping通

bogon:~ hushuaiyang$ ping 172.16.182.131
PING 172.16.182.131 (172.16.182.131): 56 data bytes
64 bytes from 172.16.182.131: icmp_seq=0 ttl=64 time=0.540 ms
64 bytes from 172.16.182.131: icmp_seq=1 ttl=64 time=0.447 ms
64 bytes from 172.16.182.131: icmp_seq=2 ttl=64 time=0.475 ms
64 bytes from 172.16.182.131: icmp_seq=3 ttl=64 time=0.439 ms
^C
--- 172.16.182.131 ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.439/0.475/0.540/0.040 ms

iptables nat表应用

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

需求1

可以让B机器连接外网

首先,给A机器先添加一块网卡,网卡添加完成后在右侧设置选择为LAN区段,然后添加一个自定义名字

然后,B机器也要添加一块网卡(也可以修改原先网卡,为了保存原先IP此时选择新建一块,并且将以前的网卡取消启动时连接)

这里新添加的网卡要选择和A机器相同的LAN区段,然后开启两台机器。
启动完成后,由于A机器保留了之前的网卡,所有使用Xshell可以继续连接,而B机器只启用了新增加的网卡,还没有配置IP,是不能够使用Xshell连接的。

配置A机器新增网卡IP

[root@linux7-128 ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.220.128  netmask 255.255.255.0  broadcast 192.168.220.255
        inet6 fe80::c6d3:ed0a:87b0:ac8e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:61:da:b9  txqueuelen 1000  (Ethernet)
        RX packets 5235  bytes 332034 (324.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 341  bytes 37082 (36.2 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.220.122  netmask 255.255.255.0  broadcast 192.168.220.255
        ether 00:0c:29:61:da:b9  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:61:da:c3  txqueuelen 1000  (Ethernet)
        RX packets 21  bytes 7182 (7.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1085  bytes 193662 (189.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

此时的ens37就是我们刚才新增加的网卡,可以使用命令配置IP,但是这样重启之后就会失效
ifconfig ens37 192.168.100.1/24

[root@linux7-128 ~]# ifconfig ens37 192.168.100.1/24
[root@linux7-128 ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.220.128  netmask 255.255.255.0  broadcast 192.168.220.255
        inet6 fe80::c6d3:ed0a:87b0:ac8e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:61:da:b9  txqueuelen 1000  (Ethernet)
        RX packets 5372  bytes 342657 (334.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 392  bytes 45760 (44.6 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.220.122  netmask 255.255.255.0  broadcast 192.168.220.255
        ether 00:0c:29:61:da:b9  txqueuelen 1000  (Ethernet)

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.1  netmask 255.255.255.0  broadcast 198.168.100.255
        inet6 fe80::fd34:6e2:9342:f45b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:61:da:c3  txqueuelen 1000  (Ethernet)
        RX packets 21  bytes 7182 (7.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1106  bytes 197784 (193.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

如果使用配置文件修改,默认它是不存在的,需要拷贝一份重新编辑一下。

配置B机器网卡

B机器需要到虚拟机里配置,虽然我们之前把B机器以前的网卡没有设置启动时连接,但是此时B机器使用ifconfig命令还是能查看到原先网卡的信息。为了安全起见,使用ifdown ens33禁用掉。

然后在继续配置新增加的ens37
ifconfig ens37 192.168.100.100/24

配置好以后ping一下A机器刚才配置的IP

OK,可以ping通,准备工作完成。

检查环境

根据需求,A机器有两块网卡,ens33(192.168.220.128)和ens37(192.168.100.1),B机器有一块网卡ens37(192.168.100.100),并且B机器不能上外网,A机器只有ens33可以上外网,两台机器互通。

经过检查,发现我们此时满足所有条件。

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

要想使用网络转发,使用nat表,必须要修改内核参数。默认此文件值为0,表示没有开启路由转发。

[root@linux7-128 ~]# cat /proc/sys/net/ipv4/ip_forward
0
[root@linux7-128 ~]# echo "1">!$
echo "1">/proc/sys/net/ipv4/ip_forward
[root@linux7-128 ~]# cat /proc/sys/net/ipv4/ip_forward
1

A机器增加一条规则
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

[root@linux7-128 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
[root@linux7-128 ~]# iptables -t nat -nvL
省略         

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           
省略

这里表示192.168.100.0这个网段可以上网

B上设置网关为192.168.100.1


然后可以ping一下A机器的ens33网卡试试

可以ping通

设置DNS看能否和外网通信

vi /etc/resolv.conf

nameserver 119.29.29.29

设置好之后可以正常连接外网,但是使用Windows命令行ping 192.168.100.100是不通的,他可以连接外面,但是外面不可以连接里面。

需求2

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

A上打开路由转发
echo “1”>/ proc/sys/net/ipv4/ip_forward

这一步前面我们已经操作过了,不要再次操作

A上执行
iptables -t nat -A PREROUTING -d 192.168.220.128 -p tcp –dport 1122 -j DNAT –to 192.168.100.100:22

此处需要将之前的规则先清空,再次添加
iptables -t nat -F

A上执行
iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT –to 192.168.220.128

设置源IP

B上设置网关为192.168.100.1

此步骤之前也已操作过

连接

此时,就可以使用Xshell连接B机器

连接IP为192.168.220.128
端口号为1122

可以正常连接

拓展

提供一个iptables系列文章的博客
https://www.zsythink.net/archives/tag/iptables/page/2/
anacron https://www.jianshu.com/p/3009a9b7d024?from=timeline
systemd自定义启动脚本 http://www.jb51.net/article/100457.htm

猜你喜欢

转载自blog.csdn.net/u013946328/article/details/82050062