selinux和防火墙的管理
一:selinux
1:selinux的基础
1、selinux的概念
就是保护Linux系统的机制,对内的
2、三种模式
强制模式(enforcing):最严格的selinux模式(阻挡的操作)
允许模式(permissive):不会阻挡操作,但是会记录操作信息
关闭模式(disabled) :不会阻挡操作,也不会记录操作信息
其中的targeted这个策略,专门针对网路的
3、三种模式转换
临时修改,setenforce 0|1
通过修改内核的参数也能实现selinux切换
但是在开机的状态下,不能实现切换到关闭模式下,只能通过修改配置文件实现,然后reboot重启即可
4、查看selinux的标签
使用-Z就能查看到标签
[root@server ~]# ll -Z total 4 -rw-------. 1 root root system_u:object_r:admin_home_t:s0 1376 Feb 4 21:44 anaconda-ks.cfg drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Feb 5 13:35 Desktop drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Feb 5 13:35 Documents drwxr-xr-x. 2 root root unconfined_u:object_r:admin_home_t:s0 6 Feb 5 13:35 Downloads
2:管理文本的标签
1、查看标签
[root@server ~]# semanage fcontext -l |grep home /home directory system_u:object_rhome_root_t:s0 /home symbolic link system_u:object_rhome_root_t:s0 /home/(.*/)?\.snapshots(/.*)? all files system_u:object_r:snapperd_data_t:s0 /home/[^/]+ directory unconfined_u:object_r:user_home_dir_t:s0
2、临时修改:(chcon)
[root@server mnt]# mkdir dir1 [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:mnt_t:s0 6 Mar 16 19:28 dir1 [root@server mnt]# chcon -t httpd_sys_content_t dir1/ [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_sys_content_t:s0 6 Mar 16 19:28 dir1 [root@server mnt]# restorecon -v dir1/ Relabeled /mnt/dir1 from unconfined_u:object_r:httpd_sys_content_t:s0 to unconfined_u:object_r:mnt_t:s0 [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:mnt_t:s0 6 Mar 16 19:28 dir1
加上-R这个选项就能递归的改变了
使用restorecon刷新一下,这个就恢复到原来的样子了
临时修改的话,selinux的状态发生了改变,就会恢复原样,开启模式到关闭模式,再到开启模式
3、永久修改:(semanage)
#使用永久修改的话,要使用绝对路径才行 [root@server mnt]# mkdir dir2 [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:mnt_t:s0 6 Mar 16 19:35 dir2 [root@server mnt]# semanage fcontext -a -t httpd_sys_content_t /mnt/dir2 [root@server mnt]# restorecon -v /mnt/dir2/ Relabeled /mnt/dir2 from unconfined_u:object_r:mnt_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0 [root@server mnt]# ll -Z total 0 drwxr-xr-x. 2 root root unconfined_u:object_r:httpd_sys_content_t:s0 6 Mar 16 19:35 dir2 [root@server mnt]#
restorecon这个命令的解释
| -R | 递归的修改 |
| -F | 强制的恢复上下文 |
| -v | 显示详细的信息 |
继承selinux的权限
就是创建了一个文件夹,里面的文件自动继承里面的标签
#记住使用通配符,然后一定要使用单引号和绝对路径即可 [root@server mnt]# mkdir dir3 [root@server mnt]# semanage fcontext -a -t httpd_sys_content_t '/mnt/dir3(/*)?' [root@server mnt]# restorecon -RFv /mnt/dir3/ Relabeled /mnt/dir3 from unconfined_u:object_r:mnt_t:s0 to system_u:object_r:httpd_sys_content_t:s0 [root@server mnt]# cd dir3/ [root@server dir3]# touch file1 [root@server dir3]# ll -Z total 0 -rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 0 Mar 16 19:41 file1
查看selinux的修改文件
使用命令修改了标签实质就是修改了对应的配置文件
/etc/selinux/targeted/contexts/files
3:管理端口的标签
1、查看端口的标签
[root@server files]# semanage port -l |grep 22 freeipmi_port_t tcp 9225 freeipmi_port_t udp 9225 hplip_port_t tcp 1782, 2207, 2208, 8290, 8292, 9100, 9101, 9102, 9220, 9221, 9222, 9280, 9281, 9282, 9290, 9291, 50000, 50002
2、修改端口的标签
[root@server files]# semanage port -a -t http_port_t -p tcp 8888 [root@server files]# semanage port -l | grep -w 8888 http_port_t tcp 8888, 80, 81, 443, 488, 8008, 8009, 8443, 9000
4:布尔值
1、查看布尔值
[root@server files]# semanage boolean -l | head -n 10 SELinux boolean State Default Description abrt_anon_write (off , off) Allow abrt to anon write abrt_handle_event (off , off) Allow abrt to handle event abrt_upload_watch_anon_write (on , on) Allow abrt to upload watch anon write antivirus_can_scan_system (off , off) Allow antivirus to can scan system antivirus_use_jit (off , off) Allow antivirus to use jit auditadm_exec_content (on , on) Allow auditadm to exec content [root@server files]# getsebool -a|head -n 10 abrt_anon_write --> off abrt_handle_event --> off abrt_upload_watch_anon_write --> on antivirus_can_scan_system --> off antivirus_use_jit --> off auditadm_exec_content --> on authlogin_nsswitch_use_ldap --> off authlogin_radius --> off authlogin_yubikey --> off awstats_purge_apache_log_files --> off [root@server files]# getsebool -a|head -n 10 abrt_anon_write --> off abrt_handle_event --> off
第一个为临时的修改(当前的状态),第二个为永久修改
2、修改布尔值
#不加上-P 就是临时修改,加上了-P就是永久修改 [root@server files]# setsebool httpd_enable_homedirs on [root@server files]# semanage boolean -l | grep -w httpd_enable_homedirs httpd_enable_homedirs (on , off) Allow httpd to enable homedirs [root@server files]# setsebool -P httpd_enable_homedirs on [root@server files]# semanage boolean -l | grep -w httpd_enable_homedirs httpd_enable_homedirs (on , on) Allow httpd to enable homedirs [root@server files]#
5:selinux的排错
审计日志:/var/log/audit
这个就是记录selinux的信息
步骤:
安装这个setroubleshoot包,能自动的将审计日志的消息发送到messages文件里面去,然后使用sealert-l UUID用于生成事件报告(可以在messages里面看到),就ok了
sealert -a可以查看所有的报告
当然也可以使用图形化工具查看,安装x86的setroubleshoot包即可
6:semanage的使用
选项:
| -a | 添加 |
| -d | 删除 |
| -l | 查看 |
| -m | 修改 |
| -t | 类型 |
记住已经修改过的可以使用-m进行修改,不能使用-a了,初次可以使用-a
二:firewalld
1:基础概念
内核模块:netfilter提供防火墙,而firewalld和iptables只是管理防火墙的工具
1)linux对外的,阻止外面的主机访问自己,控制流量的
2)是按照区域进行划分的
2:模式
防火墙有2种模式,一个是临时模式,一个是永久模式
临时模式:就是重启防火墙,或者重新加载的话,临时模式就恢复原样了
永久模式:加上了--permanent就能实现永久修改了,还要重新加载一下即可
3:查看防火墙
[root@client ~]# firewall-cmd --list-all public (active) ##默认区域名称 target: default ##区域的动作 icmp-block-inversion: no ##icmp类型黑白名单开关(no|yes) interfaces: ens160 ##网卡接口 sources: ##来源ip地址,可以是mac地址 services: cockpit dhcpv6-client ssh ##放行的服务,其实就是端口 ports: ##允许放行的目标端口 protocols: ##允许通过的协议 forward: yes ##允许在此区域中转发ip地址 masquerade: no ##伪装ip地址 forward-ports: ##端口转发 source-ports: ##允许的来源端口 icmp-blocks: ##可添加的icmp类型,如果上面的icmp-block-inversion为no的话,添加的都被拒绝了 rich rules: ##更为详细的富规则,优先级最高
4:防火墙命令
关于服务,端口,源地址等的操作
|
--get-default-zone |
查访默认的区域名称 |
|
--set-default-zone=<区域名称> |
设置默认的区域,使其永久生效 |
|
--list-all |
显示当前区域的网卡配置参数、资源、端口以及服务等信息 |
|
--list-all-zones |
显示所有区域的网卡配置参数、资源、端口以及服务等信息 |
|
--get-zones |
显示可用的区域 |
|
--get-active-zones |
显示当前正在使用的区域、来源地址和网卡名称 |
|
--add-source= |
将源自此IP或子网的流量导向指定的区域 |
|
--remove-source= |
不再将源自此IP或子网的流量导向这个区域 |
|
--change-source= |
将源自此IP或子网的流量导向指定到新的区域 |
|
--add-interface=<网卡名称> |
将源自该网卡的所有流量都导向某个指定区域 |
|
--change-interface=<网卡名称> |
将某个网卡与区域进行关联 |
|
--get-services |
显示预定义的服务 |
|
--add-service=<服务名> |
设置默认区域允许该服务的流量 |
|
--add-port=<端口号/协议> |
设置默认区域允许该端口的流量 |
|
--remove-service=<服务名> |
设置默认区域不再允许该服务的流量 |
|
--remove-port=<端口号/协议> |
设置默认区域不再允许该端口的流量 |
|
--permanent |
让配置永久生效 |
|
--reload |
让“永久生效”的配置规则立即生效,并覆盖当前的配置规则 |
5:区域
firewalld使用区域来管理,每个区域处理不同的事情
|
trusted |
可信区域,防火墙放行一切流量。等同于关闭防火墙功能。 |
|
home |
区域内主动发起连接的流入回程数据包允许通过,默认放行ssh,mdns,ipp-client,samba-client或dhcpv6-client服务。 |
|
internal |
与home区域相同 |
|
work |
区域内主动发起连接的流入回程数据包允许通过,放行ssh,dhcpv6-client服务 |
|
external |
区域内主动发起连接的流入回程数据包允许通过,放行ssh服务匹配,开启地址伪装功能 |
|
dmz |
区域内主动发起连接的流入回程数据包允许通过,放行ssh服务匹配 |
|
block |
区域内主动发起连接的流入回程数据包允许通过 |
|
drop |
对进入该区域的所有数据包丢弃,并且不进行任何回包,区域内主动发起连接的流入回程数据包允许通过 |
默认的区域是public,
6:动作
每个区域用不同的动作
四个动作default,accpet,drop,regect,
其中default,accpet,是接收,后面的drop是拒绝,不会返回消息,reject也是拒绝,但是会返回消息
设置target的策略动作:必须使用永久这个选项
[root@client ~]# firewall-cmd --list-all public (active) target: DROP icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@client ~]# firewall-cmd --permanent --set-target=ACCEPT success [root@client ~]# firewall-cmd --reload success [root@client ~]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@client ~]#
7:防火墙规则
就是有优先级的存在,显示来源地址,然后是网络接口,最后是默认的区域,优先级的存在,决定了执行的顺序
来源地址:拒绝本地的主机访问虚拟机的设置
这个区域的动作要设置为drop策略,拒绝访问
#虚拟机的设置 [root@client ~]# firewall-cmd --add-source=10.112.224.254 --permanent --zone=public success [root@client ~]# firewall-cmd --reload success [root@client ~]# firewall-cmd --list-all public (active) target: DROP icmp-block-inversion: no interfaces: ens160 sources: 10.112.224.254 services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: #使用win+r ping虚拟机地址 正在 Ping 192.168.20.20 具有 32 字节的数据: 请求超时。 请求超时。
网络的接口:将ens160设置成drop区域
默认区域:
如果前面的都没有匹配到的话,就走默认区域里面的行为
设置默认区域的
8:防火墙命令配置
1、服务
放行服务:就是将服务添加进来
[root@client /]# firewall-cmd --permanent --add-service=http success [root@client /]# firewall-cmd --reload success [root@client /]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: sources: 10.112.224.254 services: cockpit dhcpv6-client http ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: #拒绝服务,就是将这个服务移走 [root@client /]# firewall-cmd --permanent --remove-service=http success [root@client /]# firewall-cmd --reload success [root@client /]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: sources: 10.112.224.254 services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
2、端口
放行端口
[root@client /]# firewall-cmd --add-port=80/tcp --permanent success [root@client /]# firewall-cmd --reload success [root@client /]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: sources: 10.112.224.254 services: cockpit dhcpv6-client ssh ports: 80/tcp protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: #拒绝某个端口 [root@client /]# firewall-cmd --remove-port=80/tcp --permanent success [root@client /]# firewall-cmd --reload success [root@client /]# firewall-cmd --list-all public (active) target: ACCEPT icmp-block-inversion: no interfaces: sources: 10.112.224.254 services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
3、富规则
更加的清晰的设置规则,明确ip地址能不能访问等
案例:
#拒绝另外一台主机的访问本地主机,禁止192.168.20.20/24这个ip地址访问 [root@server ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.20.20" reject' success [root@server ~]# firewall-cmd --reload success [root@server ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="192.168.20.20" reject #使用另外一台主机发现ping不通 [root@client ~]# ping 192.168.20.10 PING 192.168.20.10 (192.168.20.10) 56(84) bytes of data. From 192.168.20.10 icmp_seq=1 Destination Port Unreachable ^C --- 192.168.20.10 ping statistics --- 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms #拒绝来自本地的网段访问22端口 由于本地的主机使用通过192.168.20.1这个ip地址进行登录的,所以只需要拒绝这个ip网段即可,使用xsehll都是通过这个ip来进行访问的 [root@server ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.20.0/24" port protocol="tcp" port="22" reject' success [root@server ~]# firewall-cmd --reload success [root@server ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client ssh ports: protocols: forward: yes masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: rule family="ipv4" source address="192.168.20.0/24" port port="22" protocol="tcp" reject #远程端口转发
#就是将访问本地的1000端口,然后转发到192.168.20.20上的22端口实现连接
#当然要将伪地址转发这个功能开启,才能实现
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.20.0/24" forward-port port=1000 protocol=tcp to-port=22 to-addr=192.168.20.20'
#开启伪地址转发
firewall-cmd --add-masquerade
#退出xhsell后,实现连接
[C:\~]$ ssh root@192.168.20.10 1000
Connecting to 192.168.20.10:1000...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Tue Mar 19 14:03:28 2024 from 192.168.20.10
[root@client ~]# ifconfig | grep netmask
inet 192.168.20.20 netmask 255.255.255.0 broadcast 192.168.20.255
inet 127.0.0.1 netmask 255.0.0.0
4、本地转发
本地转发:访问本地的2222端口,实际上是转发到本地22端口上
设置后,无需要有2222端口,只是个虚的
firewall-cmd --add-forward-port=port=2222:proto=tcp:toport=22 [root@server ~]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens160 sources: services: cockpit dhcpv6-client http ssh ports: protocols: forward: yes masquerade: no forward-ports: port=2222:proto=tcp:toport=22:toaddr= source-ports: icmp-blocks: rich rules: #使用xshell连接2222端口吗,会自动的跳转到22端口上面,实现连接 [C:\~]$ ssh root@192.168.20.10 2222 Connecting to 192.168.20.10:2222... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Activate the web console with: systemctl enable --now cockpit.socket Register this system with Red Hat Insights: insights-client --register Create an account or view all your systems at https://red.ht/insights-dashboard Last login: Mon Mar 18 13:38:11 2024 from 192.168.20.1 [root@server ~]#
5、远程转发
访问本地的9999端口,能够连接到远程机器,这便是远程转发,
#注意要添加协议名
firewall-cmd --add-forward-port=port=9999:proto=tcp:toport=22:toaddr=192.168.20.20
[root@server ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client http ssh
ports:
protocols:
forward: yes
masquerade: yes
forward-ports:
port=9999:proto=tcp:toport=22:toaddr=192.168.20.20
source-ports:
icmp-blocks:
rich rules:
#使用xshell要退出去,再来连接,否则连接不上
[C:\~]$ ssh root@192.168.20.10 9999
Connecting to 192.168.20.10:9999...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Tue Mar 19 13:50:47 2024 from 192.168.20.10
[root@client ~]# ifconfig | grep netmask
inet 192.168.20.20 netmask 255.255.255.0 broadcast 192.168.20.255
inet 127.0.0.1 netmask 255.0.0.0
9:其余修改防火墙的三种方式
1、图形化(firewall-config)
2、web控制台
3、自定义编写xml配置文件
全局配置文件:/usr/lib/firewalld
用户的配置文件:/etc/firewalld

浙公网安备 33010602011771号