当前位置:   article > 正文

linux防火墙,netfilter机制_防火墙 netfilter 要关闭吗

防火墙 netfilter 要关闭吗

##linux防火墙

  1. selinux
    在配制远程连接时是要关闭selinux的。
    临时关闭selinux:

      setenforce 0
    
    • 1

永久关闭要编辑配置文件

	 vim /etc/selinux/config
	
	 SELINUX=disabled
  • 1
  • 2
  • 3

平时一般关闭selinux

2.iptables

iptables是linux的防火墙机制。在centos6时,是用的netfilter机制,centos7时用的是firewalld机制。
由于我们装的是centos7,防火墙机制就是firewalld,想使用netfilter,就必须将firewald停掉,开启netfilter.
关闭firewalld:

不让firewalld开机启动:

[root@shuai-01 network-scripts]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
  • 1
  • 2
  • 3

停掉firewalld服务:

[root@shuai-01 network-scripts]# systemctl stop firewalld
  • 1

安装netfilter:

[root@shuai-01 network-scripts]# yum install -y iptables-services
  • 1

开启iptables服务:

[root@shuai-01 network-scripts]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@shuai-01 network-scripts]# systemctl start iptables
  • 1
  • 2
  • 3

关闭firewalld开启netfiler服务的步骤:

firewalld --> close / disable + stop
netfilter --> enable + start

netfilter的五个表:(filter ,nat , mangle , raw , security)
这里主要了解filter和nat 。
filter表主要用于过滤包,包含三个链INPUT , OUTPUT , FORRWARD.
nat表主要用于网络地址转换,包含三个链PREROUTING , OUTPUT , POSTROUTING 。
PREROUTING:数据包进来的那一刻我要对他进行更改
POSTROUTING:数据包出去的那一刻要进行更改

当数据包进来,是本机数据包:PREROUTING , INPUT ,localhost , OUTPUT , POSTROUTING
不是本机数据包: PREROUTING , FORWARD , POSTROUTING
一个讲IPtables的五个表的文章:
http://blog.51cto.com/907832555/1944919

netfilter

netfilter 是linux操作系统核心层内部的一个数据包处理模块

netfilter 和IPtables

在这里插入图片描述

iptables 的4张表和5条链

4张表:filter 表,nat 表 ,mangle 表 , raw 表
5条链:INPUT , OUTPUT , FIRWARD , PREROUTING , POSTROUTING

在这里插入图片描述
这张图,表示了iptables的4张表和5条链的关系,也表示数据包进来后的一些运转。

iptables的规则的组成

数据包的访问控制:ACCEPT , DROP(直接丢弃数据包) , REJECT (丢弃数据包后,返回对应的消息)

数据包的改写:SNAT(对源地址的改写) , DNAT(对目的地址的改写)

在这里插入图片描述

iptables相关的命令和用法:

  • 查看iptables相关规则:

    命令:iptables
    选项:
    -t 后接表名,表示选择那张表(默认是filter表)
    -nvL 表示查看表的规则
    -F 表示清空规则
    -Z 表示把包的流量计数器置零

      [root@shuai-01 network-scripts]# iptables -nvL
    
    • 1

防火墙的规则保存在/etc/sysconfig/iptables中

- 查看nat表的规则:

				[root@shuai-01 network-scripts]# iptables -t nat -nvL
				Chain PREROUTING (policy ACCEPT 2 packets, 152 bytes)
				 pkts bytes target     prot opt in     out     source               destination         
	
		- 清空filter表的规则:
	
		[root@shuai-01 network-scripts]# iptables -F
		[root@shuai-01 network-scripts]# iptables -nvL
		Chain INPUT (policy ACCEPT 5 packets, 356 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 4 packets, 448 bytes)
		 pkts bytes target     prot opt in     out     source  

- 重启标的规则:

		[root@shuai-01 network-scripts]# service iptables restart
		Redirecting to /bin/systemctl restart  iptables.service
		[root@shuai-01 network-scripts]# iptables -nvL
		Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
		 pkts bytes target     prot opt in     out     source               destination         
		   24  1688 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  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 增加/删除一条规则

    命令:iptables
    选项:
    -A/-D :表示增加/删除一条规则(后接INPUT,OUTPUT)
    -I : 表示插入一条规则(后接INPUT,OUTPUT)
    -P :表示预设策略
    -p :表示指定协议(tcp , udp , icmp)
    –sport :表示指定源端口,跟-p一起使用
    –dport : 表示指定目标端口,跟-p一起使用
    -s : 表示指定源IP
    -d :表示指定目的IP
    -j :后跟动作(ACCEPT表示允许包,DROP表示丢掉包,REJECT表示拒绝包)
    -i :指定网卡

    • 增加一条规则
      将来源10.12.11.13的22端口到目的12.11.13.14的80端口的tcp包丢掉

        [root@shuai-01 network-scripts]# iptables -A INPUT -s 10.12.11.13 -p tcp --sport 22 -d 12.11.13.14 --dport 80 -j DROP
      
      • 1
    • 插入一条规则 将来源1.1.1.1的所有数据包丢掉

       [root@shuai-01 network-scripts]# iptables -I INPUT -s 1.1.1.1 -j DROP
      
      • 1
    • 添加一条规则 将来源2.2.2.2的到本机80端口的tcp包丢掉

        [root@shuai-01 network-scripts]# iptables -A INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP
      
      • 1
    • 删除一条规则(将来源1.1.1.1的所有数据包丢掉)

       [root@shuai-01 network-scripts]# iptables -D INPUT -s 1.1.1.1 -j DROP
      
      • 1
    • 按行删除规则

       	  [root@shuai-01 network-scripts]# iptables -nvL --line-number
       Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
       num   pkts bytes target     prot opt in     out     source               destination         
       1      534 37016 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
       2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
       3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
       4        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
       5        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
       6        0     0 DROP       tcp  --  *      *       10.12.11.13          12.11.13.14          tcp spt:22 dpt:80
       7        0     0 DROP       tcp  --  *      *       2.2.2.2              0.0.0.0/0            tcp dpt:80
       
       Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
       num   pkts bytes target     prot opt in     out     source               destination         
       1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
       
       Chain OUTPUT (policy ACCEPT 31 packets, 2932 bytes)
       num   pkts bytes target     prot opt in     out     source               destination         
       [root@shuai-01 network-scripts]# iptables -D INPUT 7
       [root@shuai-01 network-scripts]# iptables -nvL
       Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
        pkts bytes target     prot opt in     out     source               destination         
         605 41764 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  --  *      *       10.12.11.13          12.11.13.14          tcp spt:22 dpt:80
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27

    ##iptables 小案例

  1. 需求 :只针对filter表,预设策略INPUT链DROP,其他两个链ACCEPT,然后针对192.168.137.0/24开通22端口,对所有网段开放80端口,对所有网段开放21端口。
    这需求不复杂,但是有许多条规则,写成脚本形式。
    脚本内容:

    #! /bin/bash
    ipt="/usr/sbin/iptables" //定义一个变量(关于iptables命令的绝对路径)
    $ipt -F //删除规则
    $ipt -P INPUT DROP // filter表INPUT链丢包
    $ipt -P OUTPUT ACCEPT //flter 表OUTPUT链允许包
    $ipt -P FORWARD ACCEPT //filter 表的FORWARD链允许包
    $ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT //添加规则对RELATED,ESTABLISHED状态进行放行,这两个是通信连接上了之后的包来往
    $ipt -A INPUT -s 192.168.137.0/24 -p tcp --dport 22 -j ACCEPT //添加规则对INPUT表的来源 192.168.137.0/24网段的22端口放行
    $ipt -A INPUT -p tcp --dport 80 -j ACCEPT //对全网段80端口放行
    $ipt -A INPUT -p tcp --dport 21 -j ACCEPT //对全网端的21端口放行

2.关于icmp包(ping本机不同,本机ping外网能通)

iptables -I INPIUT -p -icmp --icmp-type 8 -j DROP
  • 1

##iptables nat表的应用

这里写图片描述(将A机器做个路由器,能上外网,能连内网)
给A机器添加一块网卡
这里写图片描述
选择LAN区段
这里写图片描述
给B机器添加网卡LAN区段
这里写图片描述
给A机器网卡(内网网卡)设置IP(ens37 192.168.100.1)
编辑ens37 的配置文件
/etc/sysconfig/network-scripts/ifcfg-ens37

	TYPE=Ethernet
	BOOTPROTO=static
	DEFROUTE=yes
	PEERDNS=yes
	PEERROUTES=yes
	IPV4_FAILURE_FATAL=no
	IPV6INIT=yes
	IPV6_AUTOCONF=yes
	IPV6_DEFROUTE=yes
	IPV6_PEERDNS=yes
	IPV6_PEERROUTES=yes
	IPV6_FAILURE_FATAL=no
	IPV6_ADDR_GEN_MODE=stable-privacy
	NAME=ens37
	UUID=d23205fd-677c-4490-92f0-dd3d07af7abf
	DEVICE=ens37
	ONBOOT=yes
	IPADDR=192.168.100.1
	NETMASK=255.255.255.0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

保存退出后重启网络服务

	[root@shuai-01 network-scripts]# service network restart
  • 1

查看网卡ip

	[root@shuai-01 network-scripts]# ifconfig
	ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
	        inet 192.168.176.135  netmask 255.255.255.0  broadcast 192.168.176.255
	        inet6 fe80::16f5:8ee4:b971:e6fb  prefixlen 64  scopeid 0x20<link>
	        ether 00:0c:29:a1:0e:2c  txqueuelen 1000  (Ethernet)
	        RX packets 735  bytes 66440 (64.8 KiB)
	        RX errors 0  dropped 0  overruns 0  frame 0
	        TX packets 592  bytes 74403 (72.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.176.150  netmask 255.255.255.0  broadcast 192.168.176.255
	        ether 00:0c:29:a1:0e:2c  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::2224:2bb4:6539:1022  prefixlen 64  scopeid 0x20<link>
	        ether 00:0c:29:a1:0e:36  txqueuelen 1000  (Ethernet)
	        RX packets 80  bytes 27360 (26.7 KiB)
	        RX errors 0  dropped 0  overruns 0  frame 0
	        TX packets 218  bytes 36144 (35.2 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 76  bytes 6204 (6.0 KiB)
	        RX errors 0  dropped 0  overruns 0  frame 0
	        TX packets 76  bytes 6204 (6.0 KiB)
	        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

B 机器设置IP(ens37 192.168.100.100),设置网关192.168.100.1

ifconfig ens37 192.168.100.100
  • 1

A机器pingB机器

	[root@shuai-01 network-scripts]# ping 192.168.100.100
	PING 192.168.100.100 (192.168.100.100) 56(84) bytes of data.
	64 bytes from 192.168.100.100: icmp_seq=1 ttl=64 time=0.668 ms
	64 bytes from 192.168.100.100: icmp_seq=2 ttl=64 time=0.261 ms
	64 bytes from 192.168.100.100: icmp_seq=3 ttl=64 time=0.471 ms
	^C
	--- 192.168.100.100 ping statistics ---
	3 packets transmitted, 3 received, 0% packet loss, time 2000ms
	rtt min/avg/max/mdev = 0.261/0.466/0.668/0.168 ms
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

B机器pingA机器 都能通

  1. 要使B机器能连上外网
    A机器打开路由转发(/proc/sys/net/ipv4/ip_forward为0表示没打开)

    [root@shuai-01 network-scripts]# cat /proc/sys/net/ipv4/ip_forward
    0
    
    [root@shuai-01 network-scripts]# echo "1" > /proc/sys/net/ipv4/ip_forward
    [root@shuai-01 network-scripts]# cat /proc/sys/net/ipv4/ip_forward
    1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

B机器上设置网关
查看网关命令

	route -n 
  • 1

添加网关:

	route add default gw 192.168.100.1
  • 1

这是B机器就能ping通外网了。
B机器上设置网关为内网网关,A机器上网关为外网网卡的网关

  1. 需求
    这里写图片描述
    这里写图片描述
    两台机器之间不能直接通信要借助另一台机器来通信,
    任意两台机器之间要通信,要有源IP,目的IP。c和a 不能直接通信,a 和 b 能通信, b和 c能通信。c要想和 a 通信,从c出去的给a的包经过b时要改变源地址(改成b的地址),从a 发出来给c的包要经过b时要改变目的地址(改成c的地址)
    在A机器上加两条规则

    #这条是进来的包改目的地址
    [root@shuai-01 ~]# iptables -t nat -A PREROUTING -d 192.168.176.135 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
    #这条是出去的包改源地址
    [root@shuai-01 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.176.135
    
    • 1
    • 2
    • 3
    • 4

B机器添加网关

保存和备份iptables规则

防火墙规则保存在/etc/sysconfig/iptables文件中,系统启动时,会自动加载文件中的规则

  • 保存规则的命令

     service iptables save    这个是保存到默认文件中
    
    • 1

    保存大指定文件中

     iptables-save  > myipt.rule
    
    • 1
  • 恢复规则的命令

     iptables-restore  < myipt.rule
    
    • 1
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号