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

本文涉及的产品
云防火墙,500元 1000GB
简介:

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


相关文章
|
2月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
203 3
Linux系统禁用swap
|
2月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
274 3
|
30天前
|
安全 Linux iOS开发
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
123 6
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
264 0
Linux系统初始化脚本
|
2月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
245 1
|
2月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
913 1
|
安全 Linux 应用服务中间件
在Linux中,包过滤防火墙与代理应用防火墙有什么区别?有哪些相应的产品?
在Linux中,包过滤防火墙与代理应用防火墙有什么区别?有哪些相应的产品?
|
机器学习/深度学习 安全 网络协议
Linux防火墙iptables命令管理入门
本文介绍了关于Linux防火墙iptables命令管理入门的教程,涵盖了iptables的基本概念、语法格式、常用参数、基础查询操作以及链和规则管理等内容。
440 73
|
11月前
|
监控 安全 Linux
启用Linux防火墙日志记录和分析功能
为iptables启用日志记录对于监控进出流量至关重要
365 1