Linux防火墙与SElinux

发布于:2024-05-10 ⋅ 阅读:(23) ⋅ 点赞:(0)


一、防火墙介绍

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。

缺点:

  • 相对较新:相比iptables,firewalld是较新的工具,可能在一些老旧的文档和教程中不太常见。
  • 潜在的性能影响:虽然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服务。

# 关闭防火墙
[root@centos ~]# systemctl stop firewalld
# 防火墙开机不自启
[root@centos ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
# 查看防火墙状态
[root@centos ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: 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 ago
     Docs: man:firewalld(1)
 Main PID: 26914 (code=exited, status=1/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, code=exited, status=1/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
[root@centos ~]# 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 Resolution

Dependencies Resolved

==========================================================================================================================================================================================
 Package                                            Arch                                    Version                                           Repository                             Size
==========================================================================================================================================================================================
Installing:
 iptables-services                                  x86_64                                  1.4.21-35.el7                                     base                                   52 k

Transaction Summary
==========================================================================================================================================================================================
Install  1 Package

Total 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 transaction
  Installing : 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!
# 查看状态,目前是未启动状态
[root@centos ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

6.3、启动iptables

# 启动iptables
[root@centos ~]# systemctl start iptables
# 设置开机自启
[root@centos ~]# 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)

[root@centos ~]# 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链:
# 	策略(policy):ACCEPT,表示默认情况下允许所有数据包进入。
# 	规则:
# 		ACCEPT all:允许与已建立连接或相关联的数据包通过。
# 		ACCEPT icmp:允许ICMP协议的数据包通过。
# 		ACCEPT all:允许所有其他数据包通过。
# 		ACCEPT tcp dpt:ssh:允许目标端口为SSH(22)的TCP数据包通过。
# 		REJECT all:拒绝所有其他数据包,并返回ICMP主机禁止响应。

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

# FORWARD链:
# 	策略(policy):ACCEPT,表示默认情况下允许所有数据包转发。
# 	规则:
# 		REJECT all:拒绝所有数据包,并返回ICMP主机禁止响应。

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    
     
# OUTPUT链:
# 策略(policy):ACCEPT,表示默认情况下允许所有数据包输出。
# 规则:无特定规则,表示允许所有数据包输出。

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 连接.

[root@centos ~]# 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,即接受该数据包。
[root@centos ~]# 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

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain 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:表示目标端口为SSH(22)。   

6.5、删除一条规则(iptables -D)

  • 指定规则链:需要确定要从哪个规则链中删除规则,常见的规则链有 INPUT、OUTPUT 和 FORWARD。
  • 指定规则选项:通过 -s 指定源地址,-p 指定协议类型(如 tcp、udp),–dport 指定目标端口等。
  • 指定动作:最后,通过 -j 指定动作,如 ACCEPT(接受)、DROP(丢弃)等。
# 删除源地址为192.168.10.224的这条规则
[root@centos ~]# 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:telnet

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@centos ~]# iptables -D INPUT -s 192.168.10.224 -p tcp -m state --state NEW --dport 23 -j ACCEPT
[root@centos ~]# 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

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

6.6、保存规则(iptables-save)

# 将现有规则保存到iptables.txt文件中
[root@centos /]# iptables-save > iptables.txt
[root@centos /]# ls
bin  boot  dev  etc  git-2.31.1  git-2.31.1.tar.gz  home  iptables.txt  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  test  tmp  top.txt  usr  var
[root@centos /]# 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)

# 从规则文件夹中加载规则
[root@centos /]# iptables-restore iptables.txt 

6.8、清除规则

# 删除INPUT链下的所有规则
[root@centos /]# iptables -F INPUT
[root@centos /]# 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-prohibited

Chain 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
[root@centos /]# iptables -t nat -A POSTROUTING -s 192.168.1.10 -j SNAT --to-source 1.2.3.4

# 查看已经转换多的地址
[root@centos /]# 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.4

6.10、端口转换

# 将外部端口 8080 上的流量转发到内部 IP 地址 192.168.1.100 的端口 80 上
[root@centos /]# iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80

# iptables:Linux 系统上用于配置内核防火墙规则的命令。
# -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。

# 查看端口转发
[root@centos /]# 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的状态
[root@centos ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: active (exited) since Mon 2024-04-29 17:34:46 CST; 15h ago
 Main PID: 12599 (code=exited, status=0/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
[root@centos ~]# systemctl stop iptables
[root@centos ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.

# 启动firewalld,并设置自启动
[root@centos ~]# systemctl start firewalld
[root@centos ~]# 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 服务的默认端口
[root@centos ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success

# 使配置立即生效
[root@centos ~]# firewall-cmd --reload
success

# 查看已经生效的配置
[root@centos ~]# firewall-cmd --list-all-zones
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 22/tcp 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
  • firewall-cmd 命令是 Firewalld 提供的一个命令行工具,用于直接从命令行配置和管理防火墙
  • –zone 参数指定要操作的区域
  • –add-port 参数添加一个端口到指定区域允许流量通过
  • –permanent表示永久生效

7.3、关闭端口

# 使public区域的80端口失效
[root@centos ~]# firewall-cmd --zone=public --remove-port=80/tcp --permanent
success

# 立即生效
[root@centos ~]# firewall-cmd --reload
success

# 查看当前生效的配置
[root@centos ~]# firewall-cmd --list-all-zones
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 22/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

7.4、开放服务

# 开放http服务,这里是将--add-port改为--add-service即可
[root@centos ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@centos ~]# firewall-cmd --reload
success
[root@centos ~]# firewall-cmd --list-all-zones
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client http ssh
  ports: 22/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

7.5、关闭服务

# 关闭http服务
[root@centos ~]# firewall-cmd --zone=public --remove-service=http --permanent
success
[root@centos ~]# firewall-cmd --reload
success
[root@centos ~]# firewall-cmd --list-all-zones
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 22/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

7.6、配置策略

# 在 public 区域中允许来自 IP 地址为 192.168.0.100 的主机访问 HTTP 服务
[root@centos ~]# firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.0.100" service name="http" accept'
success
[root@centos ~]# firewall-cmd --reload
success
[root@centos ~]# firewall-cmd --list-all-zones
public
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: dhcpv6-client ssh
  ports: 22/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
        rule family="ipv4" source address="192.168.0.100" service name="http" accept

八、SElinux

SELinux(Security-Enhanced Linux)是一个安全模块,它通过强制访问控制(MAC)机制来限制进程对资源的访问,以此提高系统的安全性。在没有使用SELinux的操作系统中,资源的访问控制是基于自主访问控制(DAC),即用户拥有的权限决定资源是否能被访问。这种机制存在缺陷,尤其是root用户几乎可以无限制地访问所有资源。SELinux通过赋予每个文件、目录、进程等一个安全上下文,配合事先定义好的安全策略,来判断哪些操作是允许的,哪些是禁止的,从而防止未经授权的访问和潜在的安全威胁。

SELinux的主要用途是提供最小权限原则的安全策略,确保系统中的服务进程只能访问必要的资源。

8.1、SElinux的工作流程

为资源分配安全上下文:这涉及到文件、目录和进程,安全上下文包含了类型、角色、用户等信息,用于标识所属的安全域和访问权限。

定义安全策略:安全策略是SELinux的核心,它包含了一系列的规则,用于判断进程能否访问某个资源或执行某个操作。

实施访问控制:当进程尝试访问资源时,SELinux会根据进程的安全上下文、资源的安全上下文以及安全策略来决定是否允许此访问。
要配置SELinux,通常需要进行以下操作:

修改配置文件:编辑/etc/sysconfig/selinux文件来设置SELinux的运行模式。

更改运行模式:可以使用setenforce命令临时切换SELinux的模式,0代表宽容模式(permissive),1代表强制模式(Enforcing)。

管理安全上下文:使用ls -Z、ps -Z等命令查看安全上下文,使用mv、cp等命令管理文件的安全上下文。

布尔值配置:通过getsebool、setsebool等命令调整SELinux的各项布尔值设置,以开启或关闭某些服务的功能。

管理策略模块:利用semanage等工具来管理策略模块,进一步细化安全策略的应用。

8.2、SElinux配置

[root@centos ~]# cd /etc/sysconfig/

# 修改SELINUX=disabled可以决定是否使用selinux,目前是进制状态
[root@centos 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.
SELINUX=disabled
# 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.
SELINUXTYPE=targeted