Linux防火墙与SElinux

2024-04-30 15:20
文章标签 linux 防火墙 selinux

本文主要是介绍Linux防火墙与SElinux,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、防火墙介绍
  • 二、iptables和firewalld的区别
    • 操作方式:
    • 配置层面:
    • 性能和管理:
  • 三、iptables与firewalld的优缺点
    • iptables
    • firewalld
  • 四、iptables的工作流程
  • 五、firewalld的工作流程
  • 六、iptables安装与使用
    • 6.1、关闭firewalld服务
    • 6.2、安装iptables防火墙
    • 6.3、启动iptables
    • 6.4、显示当前规则(iptables -L)
    • 6.5、添加一条规则(iptables -A)
    • 6.5、删除一条规则(iptables -D)
    • 6.6、保存规则(iptables-save)
    • 6.7、加载规则(iptables-restore)
    • 6.8、清除规则
    • 6.9、配置网络地址转换(iptables -t nat -A)
    • 6.10、端口转换
  • 七、firewalld的使用
    • 7.1、关闭iptables,启动firewalld
    • 7.2、开放端口
    • 7.3、关闭端口
    • 7.4、开放服务
    • 7.5、关闭服务
    • 7.6、配置策略
  • 八、SElinux
    • 8.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。

缺点:

  • 相对较新:相比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 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 (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 ResolutionDependencies Resolved==========================================================================================================================================================================================Package                                            Arch                                    Version                                           Repository                             Size
==========================================================================================================================================================================================
Installing:iptables-services                                  x86_64                                  1.4.21-35.el7                                     base                                   52 kTransaction Summary
==========================================================================================================================================================================================
Install  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!
# 查看状态,目前是未启动状态
[root@centos ~]# 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
[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: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:表示目标端口为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: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         
[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: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文件中
[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-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
[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 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 (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
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端口失效
[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
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即可
[root@centos ~]# firewall-cmd --zone=public --add-service=http --permanent
success
[root@centos ~]# firewall-cmd --reload
success
[root@centos ~]# 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服务
[root@centos ~]# firewall-cmd --zone=public --remove-service=http --permanent
success
[root@centos ~]# firewall-cmd --reload
success
[root@centos ~]# 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 服务
[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
publictarget: defaulticmp-block-inversion: nointerfaces: sources: services: dhcpv6-client sshports: 22/tcpprotocols: masquerade: noforward-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

这篇关于Linux防火墙与SElinux的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/949224

相关文章

linux-基础知识3

打包和压缩 zip 安装zip软件包 yum -y install zip unzip 压缩打包命令: zip -q -r -d -u 压缩包文件名 目录和文件名列表 -q:不显示命令执行过程-r:递归处理,打包各级子目录和文件-u:把文件增加/替换到压缩包中-d:从压缩包中删除指定的文件 解压:unzip 压缩包名 打包文件 把压缩包从服务器下载到本地 把压缩包上传到服务器(zip

Linux 网络编程 --- 应用层

一、自定义协议和序列化反序列化 代码: 序列化反序列化实现网络版本计算器 二、HTTP协议 1、谈两个简单的预备知识 https://www.baidu.com/ --- 域名 --- 域名解析 --- IP地址 http的端口号为80端口,https的端口号为443 url为统一资源定位符。CSDNhttps://mp.csdn.net/mp_blog/creation/editor

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

【Linux 从基础到进阶】Ansible自动化运维工具使用

Ansible自动化运维工具使用 Ansible 是一款开源的自动化运维工具,采用无代理架构(agentless),基于 SSH 连接进行管理,具有简单易用、灵活强大、可扩展性高等特点。它广泛用于服务器管理、应用部署、配置管理等任务。本文将介绍 Ansible 的安装、基本使用方法及一些实际运维场景中的应用,旨在帮助运维人员快速上手并熟练运用 Ansible。 1. Ansible的核心概念

Linux服务器Java启动脚本

Linux服务器Java启动脚本 1、初版2、优化版本3、常用脚本仓库 本文章介绍了如何在Linux服务器上执行Java并启动jar包, 通常我们会使用nohup直接启动,但是还是需要手动停止然后再次启动, 那如何更优雅的在服务器上启动jar包呢,让我们一起探讨一下吧。 1、初版 第一个版本是常用的做法,直接使用nohup后台启动jar包, 并将日志输出到当前文件夹n

[Linux]:进程(下)

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:Linux学习 贝蒂的主页:Betty’s blog 1. 进程终止 1.1 进程退出的场景 进程退出只有以下三种情况: 代码运行完毕,结果正确。代码运行完毕,结果不正确。代码异常终止(进程崩溃)。 1.2 进程退出码 在编程中,我们通常认为main函数是代码的入口,但实际上它只是用户级

【Linux】应用层http协议

一、HTTP协议 1.1 简要介绍一下HTTP        我们在网络的应用层中可以自己定义协议,但是,已经有大佬定义了一些现成的,非常好用的应用层协议,供我们直接使用,HTTP(超文本传输协议)就是其中之一。        在互联网世界中,HTTP(超文本传输协议)是一个至关重要的协议,他定义了客户端(如浏览器)与服务器之间如何进行通信,以交换或者传输超文本(比如HTML文档)。

如何编写Linux PCIe设备驱动器 之二

如何编写Linux PCIe设备驱动器 之二 功能(capability)集功能(capability)APIs通过pci_bus_read_config完成功能存取功能APIs参数pos常量值PCI功能结构 PCI功能IDMSI功能电源功率管理功能 功能(capability)集 功能(capability)APIs int pcie_capability_read_wo