Linux中的防火墙

Linux中的防火墙

Linux中的防⽕墙介绍

Linux本身是不具备防⽕墙功能的。虽然Linux不具备防⽕墙功能,但是Linux的内核管控着所有的⽹络流量。

Linux的防⽕墙功能是通过内核的net_filter模块提供的。只要内核有这个模块,那么就有防⽕墙功能。

iptables介绍和基础操作

iptables(ip->tables) ip指的就是我们的⽹络(ip协议),tables就是表的意思

iptables就是⼀个客户端,该客户端直接和内核空间的net_filter交互,告诉net_filter什么样的流量需要放⾏,什么样的流量需要阻塞.

Linux的防⽕墙功能是net_fifilter模块提供的

iptables是⼀个客户端,客户端是⽤来接收⽤户命令的

iptables将⽤户的命令转换成对应的net_fifilter⾥⾯的过滤规则

[root@server ~]# systemctl stop firewalld

查看iptables的表

iptables -L能看INPUT,OUTPUT和FORWARD三个规则的链

[root@server ~]# iptables -L

iptables -L -t nat能看到prerouting和postrouting的规则

[root@server ~]# iptables -L -t nat

iptables -F # 清空所有的防⽕墙规则

iptables -X # 删除⽤户⾃定义的空链

iptables -Z # 清空计数

iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

  • A表示添加 INPUT表示添加的位置
  • s表示源 192.168.1.0/24表示数据包中源ip是192.168.1.0/24⽹段将会被匹配
  • p表示协议 tcp就表示匹配的协议是tcp将会被匹配,一般操作的是4层协议
  • -dport表示⽬的端⼝ 22就表示⽬的端⼝是22将会被匹配
  • j表示动作 ACCEPT就表示接受(允许)

#iptables默认规则

iptables -P INPUT DROP # 配置默认的不让进

iptables -P FORWARD DROP # 默认的不允许转发

iptables -P OUTPUT ACCEPT # 默认的可以出去

⿊名单和⽩名单的概念

⿊名单就是默认放⾏所有,把想拒绝规则写进去

⽩名单就是默认拒绝所有,把想放⾏的规则写进去

⿊名单好还是⽩名单好?

⽩名单永远优于⿊名单。

⼀般iptables是不会针对output和forward流量进⾏控制的,因为⼀般服务器操作系统不会开启流量转发,第⼆就是output的流量没有必要限制。

firewalld介绍和原理

firewalld是⼀个服务,这个服务提供了防⽕墙配置的⼯具。

NetworkManager是⼀个服务,这个服务提供了⽹络配置的⼯具

只要开启了firewalld服务,那么就可以通过firewalld服务提供的⼯具来配置防⽕墙。

Linux本身不具备防⽕墙功能,⽽是通过内核的net_filter模块来实现软防⽕墙功能,⽽且你必须通过iptables才能和net_filter进⾏交互。

默认在rhel7和fedora20,centos7以上的版本默认就安装了firewalld服务。

且默认firewalld服务是开启的.

[root@server ~]# systemctl start firewalld

[root@server ~]# systemctl status firewalld

firewalld服务提供了⼀个更加简单的⽅式来配置防⽕墙,原理就是

firewalld服务将你的防⽕墙配置命令转换成iptables规则。

[root@server ~]# firewall-cmd --add-service=http

[root@server ~]# firewall-cmd --list-all

firewalld服务给我们提供了两种⼯具,⼀种是图形化的防⽕墙管理⼯具,⼀种是命令⾏的管理⼯具。

在rhel8已经不提供防⽕墙的命令⾏⼯具

[root@server yum.repos.d]# yum -y install firewall-config

安装完上⾯的软件包就可以使⽤firewall-config命令调出防⽕墙的图形界⾯。

RHEL7的时候中⽂的界⾯有问题,有可能发⽣⽆法应⽤规则的问题

命令⾏⼯具是firewall-cmd

firewalld提供了zone的概念,zone是区域的意思,firewalld将我们的系统划分成了⼀个个的zone。

zone的边界取决于⽹卡。

firewalld提供了zone,⼀个⽹卡只能属于⼀个zone,那zone的边界就是属于该的zone的⽹卡。

firewalld的zone⾥⾯有⽹卡,有规则(rule)。如果⼀个⽹卡属于firewalld的某个zone,那么这个zone⾥⾯的所有规则都会应⽤到该zone内的⽹卡

⽹卡,zone,规则

zone包含⽹卡和规则

⽹卡在zone⾥⾯就应⽤这个zone⾥⾯的规则

我们想把firewalld学明⽩,就搞定下⾯三个概念和操作就ok了。

zone

⽹卡

规则

zone的概念⽐较简单,将⽹卡划⼊zone也很简单,复杂的只剩规则了。

firewalld操作

[root@server ~]# firewall-cmd --list-all-zones

只要不是特别复杂的⽹络场景,基本上⼀个zone可以解决我们的所有问题。

[root@server ~]# firewall-cmd --list-all

firewall-cmd --list-all是列出缺省zone的规则。

firewall-cmd添加规则正常来说必须要指定zone,因为规则是在zone⾥⾯的,那你肯定添加规则的时候要指定这个规则在哪个zone⾥⾯。

有些时候如果每⼀次添加规则都指定zone会显得⽐较复杂。因为⼤多数情况⼀个zone就能满⾜我的需求。所以firewalld给我们提供⼀个缺省的zone,没指定zone的操作,都相当于操作缺省的zone.

[root@server ~]# firewall-cmd --list-all --zone=public

[root@server ~]# firewall-cmd --list-all --zone=home

[root@server ~]# firewall-cmd --get-default-zone

[root@server ~]# firewall-cmd --set-default-zone=home

[root@server ~]# firewall-cmd --list-all

默认Linux的所有⽹卡都在默认的zone⾥⾯。

注意:像我们讲的这种防⽕墙,⽆论是iptables还是firewalld,都只能应⽤于内核管理的⽹卡,有些⽹卡如果不受内核管理,那防⽕墙的规则也⽆法应⽤。

[root@server ~]# firewall-cmd --list-interfaces

[root@server ~]# firewall-cmd --list-interfaces --zone=public

[root@server ~]# firewall-cmd --remove-interface=ens161

[root@server ~]# firewall-cmd --list-interfaces

[root@server ~]# firewall-cmd --add-interface=ens161

[root@server ~]# firewall-cmd --list-interfaces

firewalld的规则

#查看所有规则

[root@server ~]# firewall-cmd --list-all

[root@server ~]# firewall-cmd --list-all --zone=home

如果⼀个zone⾥⾯没有⽹卡,那么这个zone就是⾮active状态,⼀个zone⾥⾯必须要有⽹卡,这个zone才有实际意义。

firewall我们讲的规则有3种。

第⼀种就是service规则

第⼆种就是port规则

第三种就是rich rule(富规则)

添加service规则

[root@server ~]# firewall-cmd --add-service=ftp

[root@server ~]# firewall-cmd --list-all

[root@server ~]# firewall-cmd --list-services

[root@server ~]# firewall-cmd --remove-service=ftp

[root@server ~]# firewall-cmd --list-services

[root@server ~]# yum -y install httpd

[root@server ~]# echo firewall-test >>/var/www/html/index.html

[root@server ~]# systemctl restart httpd

[root@server ~]# systemctl enable httpd

[root@server ~]# firewall-cmd --list-services

[root@server ~]# firewall-cmd --add-service=http

[root@server ~]# firewall-cmd --list-services

[root@server ~]# ip -4 a show ens161

[root@client ~]# ip -4 a show ens224

[root@client ~]# curl 1.1.1.1

问题:

http代表的到底是多少号端⼝?

[root@server ~]# ls /usr/lib/firewalld/

[root@server services]# pwd

/usr/lib/firewalld/services

[root@server services]# ls -l | grep http

[root@server services]# cp http.xml ergou.xml

[root@server services]# vim ergou.xml

[root@server services]# cat ergou.xml

version=“1.0” encoding=“utf-8”?>

WWW (HTTP)

HTTP is the protocol used to serve Web

pages. If you plan to make your Web server publicly

available, enable this option. This option is not

required for viewing pages locally or developing Web

pages.

protocol=“tcp” port=“33333”/>

[root@server services]# systemctl restart firewalld

[root@server services]# firewall-cmd --add-service=ergou

[root@server services]# firewall-cmd --list-services

放⾏ergou服务相当于放⾏tcp协议的33333端⼝

也就是说firewalld的service规则只是⼀个名字⽽已,⾄于service到底放⾏的是什么,要看这个service代表的端⼝号。

如果你要放⾏的端⼝并没有在service规则的xml⽂件⾥⾯定义,那么你就不能⽤service规则满⾜你的条件。

所以如果是复杂的流量的话,你需要通过port规则放⾏的流量.

port规则的放⾏

[root@server ~]# firewall-cmd --add-port=33333/tcp

[root@server ~]# firewall-cmd --add-port=33333/udp

[root@server ~]# firewall-cmd --list-ports

33333/tcp 33333/udp

[root@server ~]# firewall-cmd --list-all

rich rule(富规则)

有点像iptables

[root@server ~]# firewall-cmd --add-rich-rule="rule family=ipv4 source address=2.2.2.0/24 port port=8089 protocol=tcp reject"

[root@server ~]# firewall-cmd --list-rich-rules

富规则可以通过界面配置,生成之后,复制出来直接使用

上⾯做的所有规则都是临时的

如果想让上⾯的临时规则变成永久规则,可以使⽤下⾯的命令

[root@server ~]# firewall-cmd --runtime-to-permanent

如何确定规则是永久的呢?

如果使⽤firewall-cmd --reload命令之后规则还会存在,就是永久的规则

如果想做永久规则,只需要在规则后⾯加上–permanent

虽然加上–permanent规则,会让规则永久⽣效,但是并不会⽴刻⽣效

[root@server ~]# firewall-cmd --add-service=ftp --permanent

[root@server ~]# firewall-cmd --list-all

#firewall-cmd --reload表示重载防⽕墙规则

[root@server ~]# firewall-cmd --reload

[root@server ~]# firewall-cmd --list-all

为了避免防⽕墙规则reload,建议敲两次

[root@server ~]# firewall-cmd --add-service=ftp --permanent

[root@server ~]# firewall-cmd --add-service=ftp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值