配置防火墙和SELinux(1)

systemctl命令速查

root@client \~\]# systemctl unmask firewalld //执行命令,即可实现取消服务的锁定 \[root@client \~\]# systemctl mask firewalld //下次需要锁定该服务时执行 \[root@client \~\]# systemctl start firewalld.service //启动防火墙 \[root@client \~\]# systemctl stop firewalld.service //禁用防火墙 \[root@client \~\]# systemctl reload firewalld.service //重载配置(需要先开启防火墙) \[root@client \~\]# systemctl restart firewalld.service //重启服务 \[root@client \~\]# systemctl status firewalld.service //显示服务的状态 \[root@client \~\]# systemctl enable firewalld.service //在开机时启用服务 \[root@client \~\]# systemctl disable firewalld.service //在开机时禁用服务 \[root@client \~\]# systemctl is-enabled firewalld.service //查看服务是否开机启动 \[root@client \~\]# systemctl list-unit-files \| grep enabled //查看已启动的服务列表 \[root@client \~\]# systemctl --failed //查看启动失败的服务列表

1.启动防火墙之前要先关闭

2.服务的状态

firewall-cmd命令速查

root@client \~\]# firewall-cmd --state //查看防火墙状态 \[root@client \~\]# firewall-cmd --reload //更新防火墙规则 \[root@client \~\]# firewall-cmd --state //查看防火墙状态 \[root@client \~\]# firewall-cmd --reload //重载防火墙规则 \[root@client \~\]# firewall-cmd --list-ports //查看所有打开的端口 \[root@client \~\]# firewall-cmd --list-services //查看所有允许的服务 \[root@client \~\]# firewall-cmd --get-services //获取所有支持的服务

区域相关命令速查

root@client \~\]# firewall-cmd --list-all-zones //查看所有区域信息 \[root@client \~\]# firewall-cmd --get-active-zones //查看活动区域信息 \[root@client \~\]# firewall-cmd --set-default-zone=public //设置 public 为默认区域 \[root@client \~\]# firewall-cmd --get-default-zone //查看默认区域信息 \[root@client \~\]# firewall-cmd --zone=public --add-interface=eth0 //将接口 eth0 加入区域 publid

接口相关命令速查

root@client \~\]# firewall-cmd --zone=public --remove-interface=ens160 //从区域public中删除接口ens160 \[root@client \~\]# firewall-cmd --zone=external --change-interface=ens160 //修改接口ens160 所属区域为 default \[root@client \~\]# firewall-cmd --get-zone-of-interface=ens160 //查看接口 ens160 所属区域

端口控制命令速查

root@client \~\]# firewall-cmd --add-port=80/tcp --permanent //永久开启 80 端口(全局) \[root@client \~\]# firewall-cmd --remove-port=80/tcp --permanent //永久关闭 80 端口(全局) \[root@client \~\]# firewall-cmd --add-port=65001-65010/tcp --permanent //永久开启 65001-65010端口(全局) \[root@client \~\]# firewall-cmd --zone=public --add-port=80/tcp --permanent //永久开启 80 端口(区域 public) \[root@client \~\]# firewall-cmd --zone=public --remove-port=80/tcp --permanent //永久关闭 80端口(区域 public) \[root@client \~\]# firewall-cmd --zone=public --add-port=65001-65010/tcp --permanent //永久开启 65001-65010 端口(区域 public) \[root@client \~\]# firewall-cmd --query-port=8080/tcp //查询端口是否开放 \[root@client \~\]# firewall-cmd --permanent --add-port=80/tcp //开放 80 端口 \[root@client \~\]# firewall-cmd --permanent --remove-port=8080/tcp //移除端口 \[root@client \~\]# firewall-cmd --reload //重启防火墙(修改配置后要重启防火墙)

使用终端管理工具实例

1.查看firewalld服务当前状态和使用的区域

root@client \~\]# firewall-cmd --state //查看防火墙状态 \[root@client \~\]# systemctl restart firewalld \[root@client \~\]# firewall-cmd --get-default-zone //查看默认区域

2.查询防火墙生效ens160网卡在firewalld服务中的区域

root@client \~\]# firewall-cmd --get-active-zones //查看当前防火墙中生效的区域 \[root@client \~\]# firewall-cmd --set-default-zone=trusted //设定默认区域

3.把firewalld服务中ens160网卡默认区域修改为external,并在系统重启后生效。分别查看运行时模式与永久模式下的区域名称

root@client \~\]# firewall-cmd --list-all --zone=work //查看指定区域的防火墙策略 \[root@client \~\]# firewall-cmd --permanent --zone=external --change-interface=ens160 \[root@client \~\]# firewall-cmd --get-zone-of-interface=ens160 \[root@client \~\]# firewall-cmd --permanent --get-zone-of-interface=ens160

4.把firewalld服务的当前默认区域设置为public

root@client \~\]# firewall-cmd --set-default-zone=public \[root@client \~\]# firewall-cmd --get-default-zone

5.启动/关闭firewalld服务的应急状况模式,阻断一切网络连接

root@client \~\]# firewall-cmd --panic-on \[root@client \~\]# firewall-cmd --panic-off

6.查询public区域是否允许请求SSH和HTTPS的流量

root@client \~\]# firewall-cmd --zone=public --query-service=ssh \[root@client \~\]# firewall-cmd --zone=public --query-service=https

7.把firewalld服务中请求https的流量设置为永久允许,并立即生效

root@client \~\]# firewall-cmd --get-services //查看所有可以设定的服务 \[root@client \~\]# firewall-cmd --zone=public --add-service=https \[root@client \~\]# firewall-cmd --permanent --zone=public --add-service=https \[root@client \~\]# firewall-cmd --reload \[root@client \~\]# firewall-cmd --list-all //查看生效的防火墙策略

8.把firewalld服务中请求https的流量设置为永久拒绝,并立即生效

root@client \~\]# firewall-cmd --permanent --zone=public --remove-service=https \[root@client \~\]# firewall-cmd --reload \[root@client \~\]# firewall-cmd --list-all //查看生效的防火墙策略

9.把在firewalld服务中访问8088和8089端口的流量策略设置为允许,但仅限当前生效

root@client \~\]# firewall-cmd --zone=public --add-port=8088-8089/tcp \[root@client \~\]# firewall-cmd --zone=public --list-ports

图像化的配置

先配置yum仓库

root@server \~\]#cd /etc/yum.repos.d/ \[root@server \~\]#rm -f \* \[root@server \~\]#curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo \[root@server \~\]#sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo \[root@server \~\]# mount /dev/cdrom /media/ \[root@server \~\]# dnf install firewall-config -y

设置SELinux的模式

使用配置文件设置SELinux的模式

root@client \~\]# cat /etc/selinux/config -n

使用命令行命令设置SELinux的模式

root@client \~\]# getenforce //检查当前SELinux的运行状态 \[root@client \~\]# setenforce permissive //切换到宽容模式(Permissive) \[root@client \~\]# setenforce 1 //1代表强制模式(Enforcing) \[root@client \~\]# setenforce 0 //0代表宽容模式(Permissive) \[root@client \~\]# sestatus //查看SELinux的运行状态

查看用户、文件和进程的安全上下文

Z都是大写!!!

root@client \~\]# id -Z //查看用户的安全上下文 \[root@client \~\]# ls -Zl //查看文件的安全上下文 \[root@client \~\]# ps -Z //查看进程的安全上下文

相关推荐
cosX+sinY几秒前
ubuntu 20.04 编译运行lio-sam,并保存为pcd
linux·ubuntu·机器人
诡异森林。3 分钟前
Docker--Docker网络原理
网络·docker·容器
ALex_zry17 分钟前
Docker Macvlan网络配置实战:解决“network already exists“错误
网络·docker·php
半路_出家ren21 分钟前
流量抓取工具(wireshark)
网络·网络协议·测试工具·网络安全·wireshark·流量抓取工具
Lary_Rock1 小时前
Android 编译问题 prebuilts/clang/host/linux-x86
android·linux·运维
熬夜学编程的小王1 小时前
【Linux篇】理解信号:如何通过信号让程序听从操作系统的指令
linux·信号产生·软件条件产生信号
子非衣1 小时前
Windows云主机远程连接提示“出现了内部错误”
服务器·windows
绵绵细雨中的乡音1 小时前
Linux进程学习【基本认知】
linux·运维·学习
Johny_Zhao1 小时前
MySQL 高可用集群搭建部署
linux·人工智能·mysql·信息安全·云计算·shell·yum源·系统运维·itsm
Gazer_S2 小时前
【HTTP/2:信息高速公路的革命】
网络·网络协议·http