Linux系统管理初步(五)系统防火墙控制程序firewalld

简介:

CentOS7中,系统自带的netfilter操作程序由iptables变为firewalld。firewall有zone和service两个概念,网口或者说nmcli下的conection可加入某个zone那么这个con来的数据就会遵循zone下的规则,而每个zone下又有一些service,这些service不受zone默认规则的限制。可以通过con、zone、service的搭配来具体对某一业务进行规划,保证系统安全。

一、firewalld中的9个zone

firewalld中有9个zone,各个zone的说明如下
drop
Any incoming network packets are dropped; there is no reply. Only outgoing network connections are possible.
block
Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated from within the system are possible.
public
For use in public areas. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.
external
For use on external networks with masquerading enabled, especially for routers. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.
dmz
For computers in your demilitarized zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted.
work
For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
home
For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
internal
For use on internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted.
trusted
All network connections are accepted.

译文:
由firewalld 提供的区域按照从不信任到信任的顺序排序。
丢弃 drop
任何流入网络的包都被丢弃,不作出任何响应。只允许流出的网络连接。
阻塞 block
任何进入的网络连接都被拒绝,并返回 IPv4 的 icmp-host-prohibited 报文或者 IPv6 的 icmp6-adm-prohibited 报文。只允许由该系统初始化的网络连接。
公开 public
用以可以公开的部分。你认为网络中其他的计算机不可信并且可能伤害你的计算机。只允许选中的连接接入。
外部 external
用在路由器等启用伪装的外部网络。你认为网络中其他的计算机不可信并且可能伤害你的计算机。只允许选中的连接接入。
隔离区dmz
用以允许隔离区(dmz)中的电脑有限地被外界网络访问。只接受被选中的连接。
工作 work
用在工作网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的连接。
家庭 home
用在家庭网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的连接。
内部 internal
用在内部网络。你信任网络中的大多数计算机不会影响你的计算机。只接受被选中的连接。
受信任的 trusted
允许所有网络连接。

二、firewalld的应用

操作前,我们看下当前系统中应用的是哪种防火墙程序。

systemctl list-units --all --type=service |egrep 'firewalld|ip6tables|iptables'

Linux系统管理初步(五)系统防火墙控制程序firewalld

看active那一列,active的就是在用的程序,如果你看到firewalld那一行是inactive的,那我们就用下列命令把他启动

systemctl start firewalld #
systemctl enable firewalld #

如果iptables.service是active的那我们也要把他停用

systemctl stop iptables
systemctl disable iptables

都执行完后可以再重复第一条命令看下服务状态是否跟截图一致。

firewall命令有点像一个英语句子,好理解,但是输入有点烦
比如说

firewall-cmd --get-default-zone
firewall-cmd --set-default-zone=work
firewall-cmd --get-zone-interface=ens33

命令行操作

(一)操作网卡与zone的关系

1、查看新加接口默认的zone

firewall-cmd --get-default-zone

Linux系统管理初步(五)系统防火墙控制程序firewalld

2、设定新接口加入时的默认zone

firewall-cmd --set-default-zone=work

Linux系统管理初步(五)系统防火墙控制程序firewalld
3、查看接口所在的zone

firewall-cmd --get-zone-of-interface=ens33

Linux系统管理初步(五)系统防火墙控制程序firewalld

4、给指定的网卡设置zone

firewall-cmd --zone=dmz --d-interface=ens33

Linux系统管理初步(五)系统防火墙控制程序firewalld

5、更改某个网卡的zone

firewall-cmd --zone=public --change-interface=ens33

Linux系统管理初步(五)系统防火墙控制程序firewalld

6、查看系统所有网卡所在的zone

firewall-cmd --get-active-zones

Linux系统管理初步(五)系统防火墙控制程序firewalld

(二)操作zone的service

1、列出系统中存在的所有网络服务(比如http、shttp、ssh等等)

firewall-cmd --get-services

Linux系统管理初步(五)系统防火墙控制程序firewalld

2、列出默认zone的service

firewall-cmd --list-service

Linux系统管理初步(五)系统防火墙控制程序firewalld

3、列出特定zone的service

firewall-cmd --zone=trusted --list-service

Linux系统管理初步(五)系统防火墙控制程序firewalld

4、将某个服务加入特定的zone

firewall-cmd --zone=work --add-service=ftp

Linux系统管理初步(五)系统防火墙控制程序firewalld

相对的将某个service移出某个zone

firewall-cmd --zone=work --remove-service=ftp

Linux系统管理初步(五)系统防火墙控制程序firewalld

5、将服务的改动写入配置文件重启后也生效

firewall-cmd --zone=work --add-service=ftp --permanent #就是在末尾加个permanent参数

(三)、通过更改配置文件实现service的控制

firewalld的配置文件保存在,同时程序为我们预备了zone控制的模板与service的模板,其中zone模板保存在/usr/lib/firewalld/zones/下,而service的模板保存在/usr/lib/firewalld/services/目录下,都是一些*.xml的文件。

模板路径 控制文件路径
zone /usr/lib/firewalld/zones/ /etc/firewalld/zones
service /usr/lib/firewalld/services/ /etc/firewalld/services

Linux系统管理初步(五)系统防火墙控制程序firewalld

Linux系统管理初步(五)系统防火墙控制程序firewalld

下面我们通过一个例子说明如何用模板控制zone下的service
需求:将FTP service的默认端口改为1121,并且在work zone下放行

首先我们将ftp的模板拷贝到控制文件目录中

cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

Linux系统管理初步(五)系统防火墙控制程序firewalld

然后把默认的21端口改为1121端口

sed -i 's/21/1121/g' ftp.xml

Linux系统管理初步(五)系统防火墙控制程序firewalld

然后把work zone的模板拷贝到控制文件目录下

cp -v /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones #建议加个-v参数,方便检查操作正误

Linux系统管理初步(五)系统防火墙控制程序firewalld

修改控制目录下的work.xml 把ftp服务加入白名单
(玩个花活^^)

sed -i '7i \ \ <service name="ftp"/>' work.xml

Linux系统管理初步(五)系统防火墙控制程序firewalld

重载配置文件让添加的规则生效

firewall-cmd --reload #重新加载firewalld配置文件
firewall-cmd --zone=work --list-services #检查配置结果

Linux系统管理初步(五)系统防火墙控制程序firewalld


 本文转自 whytl 51CTO博客,原文链接:http://blog.51cto.com/11934539/2065873


相关文章
|
1天前
|
Linux 数据安全/隐私保护
Linux系统忘记密码的三种解决办法
这篇博客介绍了三种在Linux忘记密码时重置登录密码的方法:1) 使用恢复模式,通过控制台界面以管理员权限更改密码;2) 利用Linux Live CD/USB启动,挂载硬盘分区并使用终端更改密码;3) 进入单用户模式,自动以管理员身份登录后重置密码。每个方法都提供了详细步骤,提醒用户在操作前备份重要数据。
|
1天前
|
JSON Unix Linux
Linux系统之jq工具的基本使用
Linux系统之jq工具的基本使用
12 1
|
1天前
|
数据采集 监控 安全
linux系统被×××后处理经历
linux系统被×××后处理经历
|
1天前
|
Ubuntu Linux
Linux(Ubuntu)系统临时IP以及静态IP配置(关闭、启动网卡等操作)
请注意,以上步骤是在临时基础上进行配置的。如果要永久保存静态IP地址,通常还需要修改 `/etc/network/interfaces`文件,以便在系统重启后保持配置。同时,确保备份相关配置文件以防止出现问题。
7 1
|
2天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
39 2
|
2天前
|
缓存 Linux
linux系统缓存机制
linux系统缓存机制
|
2天前
|
Linux 网络安全 数据安全/隐私保护
Linux 如何关闭防火墙(开启管理员权限)
Linux 如何关闭防火墙(开启管理员权限)
2 0
|
Linux C语言 C++
在64位linux下编译32位程序
一般情况下我们最好是在一个平台上编译本平台的二进制程序,比如在32位平台上编译32位程序而在64位平台上编译64位程序。    现在64位的系统(这里主要指x86_64系统,包括AMD64和Intel的EM64T)已经开始广泛的使用起来了,甚至于你现在要是想买32位的处理器根本就没有。
1999 0
|
4天前
|
机器学习/深度学习 缓存 监控
linux查看CPU、内存、网络、磁盘IO命令
`Linux`系统中,使用`top`命令查看CPU状态,要查看CPU详细信息,可利用`cat /proc/cpuinfo`相关命令。`free`命令用于查看内存使用情况。网络相关命令包括`ifconfig`(查看网卡状态)、`ifdown/ifup`(禁用/启用网卡)、`netstat`(列出网络连接,如`-tuln`组合)以及`nslookup`、`ping`、`telnet`、`traceroute`等。磁盘IO方面,`iostat`(如`-k -p ALL`)显示磁盘IO统计,`iotop`(如`-o -d 1`)则用于查看磁盘IO瓶颈。
|
1天前
|
监控 Linux Windows
50个必知的Linux命令技巧,你都掌握了吗?(下)
50个必知的Linux命令技巧,你都掌握了吗?(下)