网站建设前台功能设计与实现,seo免费系统,wordpress wp_term_taxonomy,在线制作图网站文章目录 一、防火墙介绍二、iptables和firewalld的区别操作方式#xff1a;配置层面#xff1a;性能和管理#xff1a; 三、iptables与firewalld的优缺点iptablesfirewalld 四、iptables的工作流程五、firewalld的工作流程六、iptables安装与使用6.1、关闭firewalld服务6.2… 文章目录 一、防火墙介绍二、iptables和firewalld的区别操作方式配置层面性能和管理 三、iptables与firewalld的优缺点iptablesfirewalld 四、iptables的工作流程五、firewalld的工作流程六、iptables安装与使用6.1、关闭firewalld服务6.2、安装iptables防火墙6.3、启动iptables6.4、显示当前规则iptables -L6.5、添加一条规则iptables -A6.5、删除一条规则iptables -D6.6、保存规则iptables-save6.7、加载规则iptables-restore6.8、清除规则6.9、配置网络地址转换iptables -t nat -A6.10、端口转换 七、firewalld的使用7.1、关闭iptables启动firewalld7.2、开放端口7.3、关闭端口7.4、开放服务7.5、关闭服务7.6、配置策略 八、SElinux8.1、SElinux的工作流程8.2、SElinux配置 一、防火墙介绍
Linux防火墙是一种网络安全系统用于控制进出计算机网络的数据包以保护内部网络不受外部威胁。
Linux中主要有两种防火墙工具iptables和firewalld。 iptables 是一种更接近数据原始操作的防火墙工具提供了较高的精确度。 它是基于命令行的防火墙管理工具用户可以通过一系列规则来定义如何处理进入或离开网络接口的数据包。 firewalld 是CentOS 7及之后版本默认的防火墙管理工具它取代了之前的iptables防火墙。 firewalld提供了基于区域和服务的防火墙管理方式使得操作更加容易。 它支持动态防火墙管理可以根据网络连接的安全等级来自动调整防火墙规则。 二、iptables和firewalld的区别
操作方式
iptables是一个较为底层的工具它允许用户直接操作Linux内核的防火墙规则提供了很高的灵活性和控制精度。firewalld则是一个更高级的工具它通过命令行或图形界面提供了一个更易用的配置方式使得非专业用户也能较容易地进行防火墙管理。
配置层面
iptables的规则是静态的一旦设置完成除非手动修改否则不会随网络环境的变化而自动调整。firewalld支持动态防火墙管理可以根据网络连接的安全等级来自动调整防火墙规则这使得它能够更好地适应不断变化的网络环境。
性能和管理
由于iptables直接与内核交互它在处理大量规则时可能会有性能上的优势。firewalld通过提供基于区域和服务的配置简化了规则的管理使得维护更加简单直观。
三、iptables与firewalld的优缺点
iptables
优点
灵活性和控制性iptables提供了非常细致和低层次的控制允许管理员精确地定义各种复杂的网络流量规则。性能由于iptables直接与内核交互处理规则时可能会有更小的性能开销。即时生效iptables的规则一旦配置立即生效无需重启服务。
缺点
复杂性对于非专业用户来说iptables的规则可能难以理解和管理需要一定的专业知识。静态配置iptables不会根据网络环境的变化自动调整规则这可能导致维护上的不便。
firewalld
优点
易用性firewalld提供了基于命令行和图形界面的配置方式使得规则的设置更加直观和容易。动态管理支持基于区域和服务的配置能够根据网络连接的安全等级自动调整防火墙规则。工作层次firewalld工作在网络层提供包过滤功能并且支持IPv4和IPv6。
缺点
相对较新相比iptablesfirewalld是较新的工具可能在一些老旧的文档和教程中不太常见。潜在的性能影响虽然firewalld易用但其高层次的操作可能会引入一些性能开销。
四、iptables的工作流程
iptables的工作流程涉及多个组件包括表tables、链chains和规则rules它们共同协作来处理网络数据包。 数据包进入当数据包到达Linux系统时首先经过的是PREROUTING链。这个链属于NAT表通常用于做DNAT目的地网络地址转换。 路由判断内核根据数据包的目标地址进行路由判断确定数据包是发送给本机还是其他主机。如果是发送给本机的数据包会被送到INPUT链如果需要转发则会被送到FORWARD链。 本地处理对于发送给本机的数据包会通过INPUT链进行处理。在INPUT链中可以根据规则定义允许或拒绝特定的数据包进入本机。 转发处理对于需要转发的数据包会通过FORWARD链进行处理。在FORWARD链中可以根据规则定义允许或拒绝特定的数据包通过本机转发。 本地发出对于本机发出的数据包会通过OUTPUT链进行处理。在OUTPUT链中可以根据规则定义允许或拒绝特定的数据包从本机发出。 地址转换在数据包被发送出去之前还会经过POSTROUTING链。这个链也属于NAT表通常用于做SNAT源网络地址转换。 规则匹配在整个流程中数据包会根据定义好的规则进行匹配。如果匹配成功就会执行相应的操作如允许、拒绝或者修改数据包。 默认策略如果在所有的规则中都没有找到匹配项那么会根据链的默认策略来处理数据包。默认策略可能是接受ACCEPT或丢弃DROP。 命令使用iptables的命令格式为iptables [-t table] COMMAND [chain] CRETIRIA -j ACTION其中-t指定表名COMMAND定义对规则的管理操作CRETIRIA是匹配参数ACTION是触发动作。 规则管理可以使用各种命令来添加、删除、插入、清空或列出防火墙规则。例如-A用于添加规则-D用于删除规则-I用于插入规则-F用于清空规则-L用于列出规则。 五、firewalld的工作流程
firewalld的工作流程主要涉及定义网络区域、设置安全等级和服务规则以及与内核的netfilter子系统交互来实现包过滤功能。 网络区域和安全等级 firewalld基于网络区域来设置规则而不是像iptables那样基于接口。网络区域定义了一组规则这些规则决定了数据包是否可以进入或离开该区域。每个网络区域都有一个安全等级这个等级决定了该区域的信任程度。例如公共区域的安全等级较低而内部区域的安全等级较高。 服务和应用程序规则 firewalld支持预定义的服务这些服务有一组固定的端口号可以快速地允许或拒绝相关流量。应用程序也可以直接添加防火墙规则这样用户可以轻松地为特定的服务或应用程序配置防火墙。 两种配置模式 运行时配置是临时的重启后会丢失。永久配置会保存在系统中即使重启也不会丢失。 IPv4和IPv6支持 firewalld同时支持IPv4和IPv6防火墙设置以及以太网桥接。 数据包处理 当数据包进入或离开系统时它们会经过firewalld定义的规则。这些规则基于数据包的来源、目的地、协议类型等因素来决定是否允许数据包通过。如果数据包与任何规则匹配将执行相应的操作如允许、拒绝或重定向。 命令行和图形界面 firewalld提供了命令行工具和图形界面来管理防火墙规则。这使得非专业用户也能较容易地进行配置。
六、iptables安装与使用
6.1、关闭firewalld服务
想要在默认使用firewalld的linux系统中使用iptables首先需要关闭firewalld服务然后安装并配置iptables服务。
# 关闭防火墙
[rootcentos ~]# systemctl stop firewalld
# 防火墙开机不自启
[rootcentos ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
# 查看防火墙状态
[rootcentos ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: failed (Result: exit-code) since Sat 2024-04-27 15:29:35 CST; 2 days agoDocs: man:firewalld(1)Main PID: 26914 (codeexited, status1/FAILURE)Apr 18 15:20:39 centos systemd[1]: Starting firewalld - dynamic firewall daemon...
Apr 18 15:20:40 centos systemd[1]: Started firewalld - dynamic firewall daemon.
Apr 18 15:20:40 centos firewalld[26914]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release...ling it now.
Apr 27 15:29:35 centos systemd[1]: firewalld.service: main process exited, codeexited, status1/FAILURE
Apr 27 15:29:35 centos systemd[1]: Unit firewalld.service entered failed state.
Apr 27 15:29:35 centos systemd[1]: firewalld.service failed.
Hint: Some lines were ellipsized, use -l to show in full.6.2、安装iptables防火墙
# 安装iptables
[rootcentos ~]# yum install -y iptables iptables-services
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package iptables-1.4.21-35.el7.x86_64 already installed and latest version
Resolving Dependencies
-- Running transaction check
--- Package iptables-services.x86_64 0:1.4.21-35.el7 will be installed
-- Finished Dependency ResolutionDependencies ResolvedPackage Arch Version Repository SizeInstalling:iptables-services x86_64 1.4.21-35.el7 base 52 kTransaction SummaryInstall 1 PackageTotal download size: 52 k
Installed size: 23 k
Downloading packages:
iptables-services-1.4.21-35.el7.x86_64.rpm | 52 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transactionInstalling : iptables-services-1.4.21-35.el7.x86_64 1/1 Verifying : iptables-services-1.4.21-35.el7.x86_64 1/1 Installed:iptables-services.x86_64 0:1.4.21-35.el7 Complete!
# 查看状态目前是未启动状态
[rootcentos ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptablesLoaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)Active: inactive (dead)6.3、启动iptables
# 启动iptables
[rootcentos ~]# systemctl start iptables
# 设置开机自启
[rootcentos ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.6.4、显示当前规则iptables -L
[rootcentos ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited# INPUT链
# 策略policyACCEPT表示默认情况下允许所有数据包进入。
# 规则
# ACCEPT all允许与已建立连接或相关联的数据包通过。
# ACCEPT icmp允许ICMP协议的数据包通过。
# ACCEPT all允许所有其他数据包通过。
# ACCEPT tcp dpt:ssh允许目标端口为SSH22的TCP数据包通过。
# REJECT all拒绝所有其他数据包并返回ICMP主机禁止响应。Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited# FORWARD链
# 策略policyACCEPT表示默认情况下允许所有数据包转发。
# 规则
# REJECT all拒绝所有数据包并返回ICMP主机禁止响应。Chain OUTPUT (policy ACCEPT)
target prot opt source destination # OUTPUT链
# 策略policyACCEPT表示默认情况下允许所有数据包输出。
# 规则无特定规则表示允许所有数据包输出。6.5、添加一条规则iptables -A
指定规则链需要确定要向哪个规则链添加规则常见的规则链有 INPUT、OUTPUT 和 FORWARD。指定规则选项可以通过 -s 指定源地址-p 指定协议类型如 tcp、udp–dport 指定目标端口-m state --state 指定连接状态等。指定动作最后通过 -j 指定匹配到规则后的动作如 ACCEPT接受、DROP丢弃等。
# 添加一条 iptables 规则用于允许来自 IP 地址为 192.168.10.233 的主机通过端口 22 进行新的 TCP 连接.[rootcentos ~]# iptables -A INPUT -s 192.168.10.233 -p tcp -m state --state NEW --dport 22 -j ACCEPT# iptables: 这是 iptables 命令的名称用于配置 Linux 内核防火墙。
# -A INPUT: -A 表示添加一条新规则到指定的规则链中这里指定了 INPUT 链。INPUT 链用于处理进入本机的数据包。
# -s 192.168.10.233: -s 表示源地址这里指定了源地址为 192.168.10.233。
# -p tcp: -p 表示协议类型这里指定了协议为 TCP。
# -m state --state NEW: -m 表示使用扩展模块这里使用了 state 模块来匹配数据包的状态。--state NEW 表示只匹配状态为 NEW新建的数据包。
# --dport 22: --dport 表示目标端口这里指定了目标端口为 22。
# -j ACCEPT: -j 表示动作这里指定了动作为 ACCEPT即接受该数据包。[rootcentos ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
ACCEPT tcp -- 192.168.10.233 anywhere state NEW tcp dpt:sshChain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibitedChain OUTPUT (policy ACCEPT)
target prot opt source destination # ACCEPT表示允许数据包通过。
# tcp表示协议类型为TCP。
# --表示没有指定源地址和目标地址。
# 192.168.10.233表示源IP地址为192.168.10.233。
# anywhere表示目标地址可以是任意地址。
# state NEW表示只匹配状态为新建NEW的TCP连接。
# tcp dpt:ssh表示目标端口为SSH22。 6.5、删除一条规则iptables -D
指定规则链需要确定要从哪个规则链中删除规则常见的规则链有 INPUT、OUTPUT 和 FORWARD。指定规则选项通过 -s 指定源地址-p 指定协议类型如 tcp、udp–dport 指定目标端口等。指定动作最后通过 -j 指定动作如 ACCEPT接受、DROP丢弃等。
# 删除源地址为192.168.10.224的这条规则
[rootcentos ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
ACCEPT tcp -- 192.168.10.233 anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- 192.168.10.234 anywhere state NEW tcp dpt:http
ACCEPT tcp -- 192.168.10.234 anywhere state NEW tcp dpt:81
ACCEPT tcp -- 192.168.10.224 anywhere state NEW tcp dpt:telnetChain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibitedChain OUTPUT (policy ACCEPT)
target prot opt source destination
[rootcentos ~]# iptables -D INPUT -s 192.168.10.224 -p tcp -m state --state NEW --dport 23 -j ACCEPT
[rootcentos ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
ACCEPT tcp -- 192.168.10.233 anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- 192.168.10.234 anywhere state NEW tcp dpt:http
ACCEPT tcp -- 192.168.10.234 anywhere state NEW tcp dpt:81Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibitedChain OUTPUT (policy ACCEPT)
target prot opt source destination 6.6、保存规则iptables-save
# 将现有规则保存到iptables.txt文件中
[rootcentos /]# iptables-save iptables.txt
[rootcentos /]# ls
bin boot dev etc git-2.31.1 git-2.31.1.tar.gz home iptables.txt lib lib64 lostfound media mnt opt proc root run sbin srv sys test tmp top.txt usr var
[rootcentos /]# cat iptables.txt
# Generated by iptables-save v1.4.21 on Mon Apr 29 18:19:41 2024
*nat
:PREROUTING ACCEPT [66997:2941494]
:INPUT ACCEPT [62419:2738538]
:OUTPUT ACCEPT [56263:4129762]
:POSTROUTING ACCEPT [56263:4129762]
:OUTPUT_direct - [0:0]
:POSTROUTING_ZONES - [0:0]
:POSTROUTING_ZONES_SOURCE - [0:0]
:POSTROUTING_direct - [0:0]
:POST_public - [0:0]6.7、加载规则iptables-restore
# 从规则文件夹中加载规则
[rootcentos /]# iptables-restore iptables.txt 6.8、清除规则
# 删除INPUT链下的所有规则
[rootcentos /]# iptables -F INPUT
[rootcentos /]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibitedChain OUTPUT (policy ACCEPT)
target prot opt source destination 6.9、配置网络地址转换iptables -t nat -A
指定链需要确定要添加规则的链常见的链有 PREROUTING、POSTROUTING 和 OUTPUT。指定选项可以通过 -s 指定源地址-d 指定目标地址–source-port 指定源端口–destination-port 指定目标端口等。指定动作最后通过 -j 指定动作如 DNAT目标网络地址转换、SNAT源网络地址转换或 MASQUERADE伪装等。
# 将来自 192.168.1.10 的数据包的源地址转换为 1.2.3.4
[rootcentos /]# iptables -t nat -A POSTROUTING -s 192.168.1.10 -j SNAT --to-source 1.2.3.4# 查看已经转换多的地址
[rootcentos /]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
PREROUTING_direct all -- anywhere anywhere
PREROUTING_ZONES_SOURCE all -- anywhere anywhere
PREROUTING_ZONES all -- anywhere anywhere Chain INPUT (policy ACCEPT)
target prot opt source destination Chain OUTPUT (policy ACCEPT)
target prot opt source destination
OUTPUT_direct all -- anywhere anywhere Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
POSTROUTING_direct all -- anywhere anywhere
POSTROUTING_ZONES_SOURCE all -- anywhere anywhere
POSTROUTING_ZONES all -- anywhere anywhere
SNAT all -- 192.168.1.10 anywhere to:1.2.3.46.10、端口转换
# 将外部端口 8080 上的流量转发到内部 IP 地址 192.168.1.100 的端口 80 上
[rootcentos /]# iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80# iptablesLinux 系统上用于配置内核防火墙规则的命令。
# -t nat指定操作 NAT 表。
# -A PREROUTING在 PREROUTING 链中添加规则。PREROUTING 链是在数据包进入网络堆栈之前进行处理的规则链。
# -p tcp指定要匹配的协议类型为 TCP。
# --dport 8080指定要匹配的目标端口号为 8080。
# -j DNAT指定动作为 DNAT目标网络地址转换。
# --to-destination 192.168.1.100:80指定转换后的目标 IP 地址和端口号为 192.168.1.100 和 80。# 查看端口转发
[rootcentos /]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
PREROUTING_direct all -- anywhere anywhere
PREROUTING_ZONES_SOURCE all -- anywhere anywhere
PREROUTING_ZONES all -- anywhere anywhere
DNAT tcp -- anywhere anywhere tcp dpt:webcache to:192.168.1.100:80七、firewalld的使用
centos中默认是安装firewalld防火墙的所以不需要再自行下载。
7.1、关闭iptables启动firewalld
# 查看iptables的状态
[rootcentos ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptablesLoaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)Active: active (exited) since Mon 2024-04-29 17:34:46 CST; 15h agoMain PID: 12599 (codeexited, status0/SUCCESS)Apr 29 17:34:46 centos systemd[1]: Starting IPv4 firewall with iptables...
Apr 29 17:34:46 centos iptables.init[12599]: iptables: Applying firewall rules: [ OK ]
Apr 29 17:34:46 centos systemd[1]: Started IPv4 firewall with iptables.# 停止并禁止自启iptables
[rootcentos ~]# systemctl stop iptables
[rootcentos ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.# 启动firewalld并设置自启动
[rootcentos ~]# systemctl start firewalld
[rootcentos ~]# systemctl enable firewalld
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.7.2、开放端口
# 在 public 区域中打开 http 服务的默认端口
[rootcentos ~]# firewall-cmd --zonepublic --add-port80/tcp --permanent
success# 使配置立即生效
[rootcentos ~]# firewall-cmd --reload
success# 查看已经生效的配置
[rootcentos ~]# firewall-cmd --list-all-zones
publictarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client sshports: 22/tcp 80/tcpprotocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules: firewall-cmd 命令是 Firewalld 提供的一个命令行工具用于直接从命令行配置和管理防火墙–zone 参数指定要操作的区域–add-port 参数添加一个端口到指定区域允许流量通过–permanent表示永久生效
7.3、关闭端口
# 使public区域的80端口失效
[rootcentos ~]# firewall-cmd --zonepublic --remove-port80/tcp --permanent
success# 立即生效
[rootcentos ~]# firewall-cmd --reload
success# 查看当前生效的配置
[rootcentos ~]# firewall-cmd --list-all-zones
publictarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client sshports: 22/tcpprotocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules: 7.4、开放服务
# 开放http服务这里是将--add-port改为--add-service即可
[rootcentos ~]# firewall-cmd --zonepublic --add-servicehttp --permanent
success
[rootcentos ~]# firewall-cmd --reload
success
[rootcentos ~]# firewall-cmd --list-all-zones
publictarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client http sshports: 22/tcpprotocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules: 7.5、关闭服务
# 关闭http服务
[rootcentos ~]# firewall-cmd --zonepublic --remove-servicehttp --permanent
success
[rootcentos ~]# firewall-cmd --reload
success
[rootcentos ~]# firewall-cmd --list-all-zones
publictarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client sshports: 22/tcpprotocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules: 7.6、配置策略
# 在 public 区域中允许来自 IP 地址为 192.168.0.100 的主机访问 HTTP 服务
[rootcentos ~]# firewall-cmd --permanent --zonepublic --add-rich-rulerule familyipv4 source address192.168.0.100 service namehttp accept
success
[rootcentos ~]# firewall-cmd --reload
success
[rootcentos ~]# firewall-cmd --list-all-zones
publictarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client sshports: 22/tcpprotocols: masquerade: noforward-ports: source-ports: icmp-blocks: rich rules: rule familyipv4 source address192.168.0.100 service namehttp accept八、SElinux
SELinuxSecurity-Enhanced Linux是一个安全模块它通过强制访问控制MAC机制来限制进程对资源的访问以此提高系统的安全性。在没有使用SELinux的操作系统中资源的访问控制是基于自主访问控制DAC即用户拥有的权限决定资源是否能被访问。这种机制存在缺陷尤其是root用户几乎可以无限制地访问所有资源。SELinux通过赋予每个文件、目录、进程等一个安全上下文配合事先定义好的安全策略来判断哪些操作是允许的哪些是禁止的从而防止未经授权的访问和潜在的安全威胁。
SELinux的主要用途是提供最小权限原则的安全策略确保系统中的服务进程只能访问必要的资源。
8.1、SElinux的工作流程 为资源分配安全上下文这涉及到文件、目录和进程安全上下文包含了类型、角色、用户等信息用于标识所属的安全域和访问权限。 定义安全策略安全策略是SELinux的核心它包含了一系列的规则用于判断进程能否访问某个资源或执行某个操作。 实施访问控制当进程尝试访问资源时SELinux会根据进程的安全上下文、资源的安全上下文以及安全策略来决定是否允许此访问。 要配置SELinux通常需要进行以下操作 修改配置文件编辑/etc/sysconfig/selinux文件来设置SELinux的运行模式。 更改运行模式可以使用setenforce命令临时切换SELinux的模式0代表宽容模式permissive1代表强制模式Enforcing。 管理安全上下文使用ls -Z、ps -Z等命令查看安全上下文使用mv、cp等命令管理文件的安全上下文。 布尔值配置通过getsebool、setsebool等命令调整SELinux的各项布尔值设置以开启或关闭某些服务的功能。 管理策略模块利用semanage等工具来管理策略模块进一步细化安全策略的应用。 8.2、SElinux配置
[rootcentos ~]# cd /etc/sysconfig/# 修改SELINUXdisabled可以决定是否使用selinux目前是进制状态
[rootcentos sysconfig]# cat selinux# This file controls the state of SELinux on the system.
# SELINUX can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUXdisabled
# SELINUXTYPE can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPEtargeted