当前位置:网站首页>如何使用FirewallD限制网络访问
如何使用FirewallD限制网络访问
2022-07-30 05:23:00 【yuyuyuliang00】
作为一个Linux用户,你可以使用firewalld防火墙选择允许或限制对某些服务或者IP地址的网络访问,这是CentOS/RHEL 8以及诸如Fedora的大部分基于RHEL发行版原生的。
firewalld防火墙使用firewall-cmd命令行工具配置防火墙规则。
在我们执行任何配置前,首先使用systemctl工具启用firewalld服务,如下:
[[email protected] blctrl]# systemctl enable firewalld
一旦启用了,我们现在可以通过执行以下命令启动firewalld:
[[email protected] blctrl]# systemctl start firewalld
你可以通过运行以下命令验证firewalld的状态并且以下输出确认了firewalld启动了并且在运行。
[[email protected] blctrl]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-07-21 19:40:25 CST; 4h 8min left
Docs: man:firewalld(1)
Main PID: 689 (firewalld)
CGroup: /system.slice/firewalld.service
└─689 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
使用firewalld配置规则
现在我们使firewalld运行了,我们直接制定一些规则。firewalld允许你添加和阻塞端口,黑名单,以及白名单IP,提供对服务器访问的地址。一旦完成这些配置,总是确保你为了使新规则生效重载这个防火墙。
添加一个TCP/UDP端口
添加一个端口,用于HTTPS的443,使用以下语法。注意:你必须在这个端口号之后指定端口是TCP或UDP。
[[email protected] blctrl]# firewall-cmd --add-port=443/tcp --permanent
success
类似地,要添加一个UDP端口,按如下指定这个UDP选项:
[[email protected] blctrl]# firewall-cmd --add-port=53/udp --permanent
success
--permanent标记确保即使在重启后规则有效。
阻塞一个TCP/UDP端口
TCP 80端口先前已经被添加到了规则中了,用浏览器测试:
要阻塞一个TCP端口,如TCP 80,运行以下命令:
[[email protected] blctrl]# firewall-cmd --remove-port=80/tcp --permanent
success
[[email protected] blctrl]# firewall-cmd --reload
success
再次用浏览器测试:
类似地,封锁一个UDP端口,将按照相同语法:
[[email protected] blctrl]# firewall-cmd --remove-port=53/udp --permanent
success
[[email protected] blctrl]# firewall-cmd --reload
success
允许一个服务
在/etc/services文件中定义了网络服务。要允许像https地服务,执行命令:
[[email protected] blctrl]# firewall-cmd --add-service=https
success
阻塞一个服务
要阻塞一个服务,例如,https,执行:
[[email protected] blctrl]# firewall-cmd --remove-service=https
success
白名单一个IP地址
要允许单个IP地址穿过防火墙,执行命令:
[[email protected] blctrl]# firewall-cmd --permanent --add-source=192.168.3.244
success
[[email protected] blctrl]# firewall-cmd --reload
success
你也可以使用CIDR标注允许一个IPs范围或者整个子网。例如,以255.255.255.0允许一整个子网。
[[email protected] blctrl]# firewall-cmd --permanent --add-source=192.168.3.0/24
success
移除一个白名单IP地址
如果你在防火墙上移除一个白名单IP,使用--remove-source标记:
[[email protected] blctrl]# firewall-cmd --permanent --remove-source=192.168.3.244
success
对于整个子网:
[[email protected] blctrl]# firewall-cmd --permanent --remove-source=192.168.3.0/24
success
阻塞一个IP地址:
到目前为止,我们已经看到了你如何添加和移除端口和服务以及添加和移除白名单IPs。要阻塞一个IP地址,'rich rules'用于这个目的。
例如,要阻塞这个IP 192.168.3.244,运行命令:
[[email protected] blctrl]# firewall-cmd --add-rich-rule="rule family='ipv4' source address='192.168.3.244' reject"
success
在192.168.3.244上进行测试:
运行阻塞命令前:可以ping通
[[email protected] ~]$ ping -c2 192.168.3.246
PING 192.168.3.246 (192.168.3.246) 56(84) bytes of data.
64 bytes from 192.168.3.246: icmp_seq=1 ttl=64 time=0.466 ms
64 bytes from 192.168.3.246: icmp_seq=2 ttl=64 time=0.559 ms
--- 192.168.3.246 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.466/0.512/0.559/0.051 ms
运行阻塞命令后,ping不通了
[[email protected] ~]$ ping -c2 192.168.3.246
PING 192.168.3.246 (192.168.3.246) 56(84) bytes of data.
From 192.168.3.246 icmp_seq=1 Destination Port Unreachable
From 192.168.3.246 icmp_seq=2 Destination Port Unreachable
--- 192.168.3.246 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1000ms
要阻塞整个子网,运行:
[[email protected] blctrl]# firewall-cmd --add-rich-rule="rule family='ipv4' source address='192.168.3.0/24' reject"
保存防火墙规则
如果你对防火墙规则做了任何修改,为了立即应用更改,你需要运行以下命令:
[[email protected]st blctrl]# firewall-cmd --reload
success
查看防火墙规则
要看一下在防火墙中所有规则,执行这个命令:
[[email protected] blctrl]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ssh
ports: 443/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
rule family="ipv4" source address="192.168.3.244" reject
边栏推荐
- 程序员赚钱实操,手把手教你做付费课程,自媒体,付费文章及付费技术课赚钱
- ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program
- 从字节码角度带你彻底理解i++与++i
- 力扣20-有效的括号——栈实现
- 无代码开发平台重新申请入门教程
- GO语言学习笔记一
- JVM 内存结构 超详细学习笔记(一)
- RadonDB MySQL on K8s 2.1.3 发布!
- leetcode hot 100(刷题篇11)(231/235/237/238/292/557/240/36)offer/3/4/5
- pycharm上的tensorflow环境搭载
猜你喜欢
MySql string splitting realizes the split function (field splitting, column switching, row switching)
[Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset
Acwing perfect number
Kyligence 出席华为全球智慧金融峰会,加速拓展全球市场
Thymeleaf简介
el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能
mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
MySQL索引常见面试题(2022版)
This article will take you through js to deal with the addition, deletion, modification and inspection of tree structure data
暴力递归到动态规划 05 (贴纸拼词)
随机推荐
美国再次加息75个基点 陷入“技术性衰退”?加密市场却呈现复苏力量
丑陋的程序员
即刻报名|前沿技术探索:如何让 Spark 更强劲、更灵活
Unity踩坑记录 —— GetComponent的使用
路径依赖:穷人很难逆袭突破的科学道理
翻译 | 解读首部 Kubernetes 纪录片
The use of Conluce, an online document management system
ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
MySQL Basics (DDL, DML, DQL)
leetcode刷题
最新版MySQL 8.0 的下载与安装(详细教程)
分布式事务之 Seata框架的原理和实战使用(三)
MySQL安装配置教程(超级详细)
2022 Pengcheng Cup web
剑指offer(刷题篇12)
[High Performance Computing] openMP
ms project2010项目管理软件使用技巧总结
mysql基础(4)
Nacos 原理
容器化 | 构建 RadonDB MySQL 集群监控平台