Linux日常运维(二)

10.4 Linux的防火墙


SELinux

SELinux是linux系统特有的安全机制,这种机制限制较多,配置也比较繁琐,所以一般把SELinux关闭。

  • 查看selinux的状态有两种方法:

getenforce命令可查看selinux状态:

# getenforce
Enforcing

/usr/sbin/sestatus也可查看selinux状态:

# /usr/sbin/sestatus

SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

上面,

SELinux status:selinux防火墙的状态,enabled 表示启用selinux防火墙;
Current mode:表示selinux防火墙当前的安全策略,enforcing 表示强。


  • 关闭SELinux:

临时关闭selinux:

# setenforce 0

# getenforce
Permissive

setenforce 0用于关闭selinux防火墙,重启失效;setenforce 1用于打开selinux,重启失效。

永久关闭selinux:

# vim /etc/selinux/config

SELINUX=disabled

# getenforce
Enforcing

# reboot

# getenforce
Disabled

netfilter

在CentOS7之前的CentOS版本的防火墙为netfilter,CentOS7的防火墙为firewalld

iptables是针对防火墙的一个工具,iptables用来设置、维护和检查Linux内核的IP包过滤规则的。在了解netfilter之前,需要先把firewalld关闭,然后开启之前版本的iptables。

# systemctl stop firewalld				#关闭firewalld服务

# systemctl disable firewalld				#禁止firewalld服务开机启动

# yum install -y iptables-services				#安装iptables-services

# systemctl enable iptables				#让iptables开机启动

# systemctl start iptables				#启动iptables服务

CentOS上默认设有iptables规则,一般建议先清除规则,然后把清除后的规则保存一下。

# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  190 16296 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2   148 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    1    52 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    52 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    52 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    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 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_IN_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_IN_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_OUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 FORWARD_OUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 192 packets, 17907 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  192 17907 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

-nvL选项表示查看规则,其中-n表示不针对IP反解析主机名,-L表示列出,-v表示列出的信息更加详细。

# iptables -F;service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

-F选项表示清除当前所有规则,但清除只是暂时的,重启iptables服务后还会加载已经保存的规则,所以使用service iptables save保存一下。从上面可以看到,防火墙规则保存在/etc/sysconfig/iptables中。


netfilter的5个表

1.filter表

filter表是iptables的默认表,用于过滤包,如果你没有自定义表,那么就默认使用filter表,它具有3个内建链:

INPUT链		处理来自外部的数据

OUTPUT链	处理向外发送的数据

FORWARD链	将数据转发到本机的其他网卡设备上

2.nat表

nat表用于网络地址转换,它也有3个内建链:

PREROUTING链		处理刚到达本机并在路由转发前的数据包。它会转换数据包中的目标IP地址(destination ip address),通常用于DNAT(destination NAT)

OUTPUT链			改变本机产生的包的目的地址

POSTROUTING链		处理即将离开本机的数据包。它会转换数据包中的源IP地址(source ip address),通常用于SNAT(source NAT)

3.mangle表

mangle表用于指定如何处理数据包,它能改变TCP头中的QoS位,Mangle表具有5个内建链:

PREROUTING链

INPUT链

FORWARD链

OUTPUT链

POSTROUTING链

4.raw表

raw表可以实现不追踪某些数据包,默认系统的数据包都会被追踪,用于处理异常,它具有2个内建链:

PREROUTING链

OUTPUT链

5.security表

security表用于强制访问控制(MAC)的网络规则,在CentOS6中没有该表。


netfilter的5个链

PREROUTING		数据包进入路由器之前

INPUT			通过路由表后,目的地为本机

FORWARD			通过路由表后,目的地不为本机

OUTPUT			由本级产生,向外转发

POSTROUTING		发送到网卡接口之前

iptables基本语法

  • 查看规则以及清除规则:
# iptables -t nat -nvL

Chain PREROUTING (policy ACCEPT 1 packets, 52 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    1    52 PREROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    52 PREROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    1    52 PREROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

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

Chain OUTPUT (policy ACCEPT 88 packets, 5914 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   88  5914 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain POSTROUTING (policy ACCEPT 88 packets, 5914 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   88  5914 POSTROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   88  5914 POSTROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   88  5914 POSTROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0  

-t选项后面跟表名,不加-t选项默认指filter表

# iptables -F

# iptables -Z

-F选项表示删除所有规则,-Z选项把包以及流量计数器置零。

  • 增加/删除/插入一条规则:
# iptables -A INPUT -s 192.168.200.1 -p tcp --sport 1234 -d 192.168.200.200 --dport 80 -j DROP
  • 参数:
-A/-D/-I		表示增加/删除/插入一条规则(-I可以插入到最前面,-A只能增加到最后面)

-p				指定协议,可以是tcp、udp、icmp或all

--dport			必须和-p一起使用,表示指定目标端口

--sport			必须和-p一起使用,表示指定源端口

-s				指定源ip(可以是一个ip段)

-d				指定目的ip(可以是一个ip段)

-j				后面跟动作,其中ACCEPT表示允许包,DROP表示丢掉包,REJECT表示拒绝包

-i				指定接收进来数据包的网卡

-o				指定发送出去数据包的网卡

--line-number	显示规则编号(可以根据编号来删除规则)

-P				默认规则,默认为ACCEPT所有包(可以更改为DROP,表示丢弃所有包,不过这是危险操作,轻易不要尝试)  
  • 示例:
# iptables -I INPUT -s 1.1.1.1 -j DROP

表示插入一条规则,把来自1.1.1.1的数据包全部丢掉。

# iptables -D INPUT -s 1.1.1.1 -j DROP

表示删除把来自1.1.1.1的数据包全部丢掉这条规则(删除一条规则时,必须和插入或者增加的规则一致,除了-A-D-I的区别)。

# iptables -I INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP

表示把来自2.2.2.2并且是TCP协议到本机80端口的数据包丢掉(--dport/--sport必须和-p选项一起使用,否则会报错)。

# iptables -I OUTPUT -p tcp --dport 22 -d 10.0.1.14 -j DROP

表示把发送到10.0.1.14的22端口的数据包丢掉。

# iptables -A INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT

# iptables -nvL |grep '192.168.1.0'
    0     0 ACCEPT     all  --  eth0   *       192.168.1.0/24       0.0.0.0/0    

表示把来自192.168.1.0/24这个网段且作用在eth0网卡上的包放行。

# iptables -nvL --line-numbers

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     1594  115K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        2   148 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3       25  1924 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
4       25  1924 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
5       25  1924 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
6        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
7       24  1872 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
8        0     0 ACCEPT     all  --  eth0   *       192.168.1.0/24       0.0.0.0/0           

表示查看iptables规则,显示规则编号。

# iptables -D INPUT 6

# iptables -nvL --line-numbers

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     1792  128K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        2   148 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3       25  1924 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
4       25  1924 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
5       25  1924 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
6       24  1872 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
7        0     0 ACCEPT     all  --  eth0   *       192.168.1.0/24       0.0.0.0/0

-D后面依次跟链名、规则编号,表示删除对应编号的规则。

# iptables -P INPUT ACCEPT

-P表示预设策略,后面跟链名,策略内容为ACCEPT,或者DROP(如果是远程服务器时,切勿执行DROP操作,否则远程连接就会被断开)。

# iptables -I INPUT -m iprange -src 192.168.100.0-192.168.188.255 -j DROP

上面,

-m 后面跟模块名字,iprange是一个模块名字,用来支持一个网段;
--src-range 指定源ip范围;
--dst-range 指定目标ip范围。

# iptables -I INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT

--state指定要匹配包的的状态。

在iptables上有四种状态:NEWESTABLISHEDINVALIDRELATID

NEW				conntrack模块看到的某一连接的第一个包

ESTABLISHED		只要发送并接收到应答,连接就是EATABLISHED状态

RELATID			当一个连接和某个已处于EATABLISHED状态的连接有关系时,就可以认为是RELATID状态

INVALID			INVALID意味着这个包没有已知的流或连接与之关联,也可能是它包含的数据或包头有问题
# iptables -I INPUT -p icmp --icmp-type 8 -j DROP

--icmp-type需要跟-p icmp一起使用,后面指定类型编号,8表示能在本机ping通其他机器,而其他机器无法ping通本机。


iptables规则备份和恢复

  • 保存iptables规则:
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

将规则保存到/etc/sysconfig/iptables中。

  • 备份iptables规则:
# iptables-save > zx.ipt

# ls
Desktop    Downloads  Music     Public  scripts  Templates  zx.ipt
Documents  htop       Pictures  rsync   sed      Videos

# cat zx.ipt

# Generated by iptables-save v1.4.21 on Thu Jun 14 13:54:30 2018
*nat
:PREROUTING ACCEPT [10:1511]
:INPUT ACCEPT [7:807]
:OUTPUT ACCEPT [11:855]
:POSTROUTING ACCEPT [11:855]
COMMIT
# Completed on Thu Jun 14 13:54:30 2018
# Generated by iptables-save v1.4.21 on Thu Jun 14 13:54:30 2018
*filter
:INPUT ACCEPT [244:22087]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [175:19538]
COMMIT
# Completed on Thu Jun 14 13:54:30 2018

将当前规则备份到zx.ipt中。

  • 恢复iptables规则:
# iptables-restore < zx.ipt

zx.ipt中的规则恢复为当前规则。


nat表的应用

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

  • A机器配置:
# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.20.128  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::b76:caa8:3d7c:71bc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e4:fc:a5  txqueuelen 1000  (Ethernet)
        RX packets 313  bytes 28159 (27.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 264  bytes 33270 (32.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.20.130  netmask 255.255.255.0  broadcast 192.168.100.255
        ether 00:0c:29:e4:fc:a5  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.133.255
        ether 00:0c:29:e4:fc:af  txqueuelen 1000  (Ethernet)
        RX packets 48  bytes 14388 (14.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 9360 (9.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • B机器配置:
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.100  netmask 255.255.255.0  broadcast 192.168.133.255
        ether 00:0c:29:e4:fc:af  txqueuelen 1000  (Ethernet)
        RX packets 48  bytes 14388 (14.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 9360 (9.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 需求:可以让B机器连接外网:
  1. A机器上打开路由转发:echo "1">/proc/sys/net/ipv4/ip_forward
  2. A上执行:iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
  3. B上设置网关为192.168.100.1

A机器

# cat /proc/sys/net/ipv4/ip_forward
0

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

# cat /proc/sys/net/ipv4/ip_forward
1
# iptables -t nat -A POSTROUTING -s 192.168.133.0/24 -o ens33 -j MASQUERADE

# iptables -t nat -nvL

Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   42 13256 PREROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   42 13256 PREROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   42 13256 PREROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

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         
  104  7831 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

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

iptables对nat表做IP转发操作,-o指定发送出去数据包的网卡,这里就是ens33,MASQUERADE表示伪装。

B机器

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.20.2   0.0.0.0         UG    100    0        0 ens33
192.168.20.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 ens34

# route add default gw 198.168.100.1

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.1   0.0.0.0         UG    0    0        0 ens34
0.0.0.0         192.168.20.2   0.0.0.0         UG    100    0        0 ens33
192.168.20.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 ens34

route -n查看网关 ,route add default gw设置默认网关。

# ping 192.168.100.1
PING 192.168.100.2 (192.168.100.2) 56(84) bytes of data.
64 bytes from 192.168.100.2: icmp_seq=1 ttl=128 time=0.178 ms
64 bytes from 192.168.100.2: icmp_seq=2 ttl=128 time=0.284 ms
64 bytes from 192.168.100.2: icmp_seq=3 ttl=128 time=0.197 ms
64 bytes from 192.168.100.2: icmp_seq=4 ttl=128 time=0.216 ms
64 bytes from 192.168.100.2: icmp_seq=5 ttl=128 time=0.227 ms
64 bytes from 192.168.100.2: icmp_seq=6 ttl=128 time=0.166 ms
# vi /etc/resolv.conf

# Generated by NetworkManager
nameserver 119.29.29.29
# ping www.baidu.com

PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=1 ttl=128 time=36.10 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=2 ttl=128 time=34.81 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=3 ttl=128 time=34.07 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=4 ttl=128 time=35.81 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=5 ttl=128 time=39.24 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=6 ttl=128 time=34.4 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=7 ttl=128 time=34.27 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=8 ttl=128 time=35.25 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=9 ttl=128 time=35.89 ms
^C
--- www.a.shifen.com ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 14821ms
rtt min/avg/max/mdev = 34.07/36.770/34.458/33.038 ms

可以看到,B机器可以ping通 www.baidu.com,实现B机器通过nat借助A机器来上网,需求满足。


更多资料参考:

iptables详解

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值