温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎样使用semanage管理SELinux安全策略

发布时间:2021-11-09 09:32:20 来源:亿速云 阅读:171 作者:柒染 栏目:建站服务器

这篇文章将为大家详细讲解有关怎样使用semanage管理SELinux安全策略,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

导读Semanage是用于配置SE Linux策略某些元素而无需修改或重新编译策略源的工具。 这包括将Linux用户名映射到SELinux用户身份以及对象(如网络端口,接口和主机)的安全上下文映射。

简介

Semanage是用于配置SELinux策略某些元素而无需修改或重新编译策略源的工具。 这包括将Linux用户名映射到SELinux用户身份以及对象(如网络端口,接口和主机)的安全上下文映射。

实验环境

Centos7.7操作系统

Selinux已经开启
开启方式:

[root@localhost ~]# sed -i '/^SELINUX/s/disabled/enforcing/g' /etc/selinux/config # 然后重启一下操作系统 [root@localhost ~]# reboot # 重启完成之后检查一下是否是enforcing模式 [root@localhost ~]# getenforce  Enforcing

常用参数

  • port: 管理定义的网络端口类型

  • fcontext: 管理定义的文件上下文

  • -l: 列出所有记录

  • -a: 添加记录

  • -m: 修改记录

  • -d: 删除记录

  • -t: 添加的类型

  • -p: 指定添加的端口是tcp或udp协议的,port子 命令下使用

  • -e: 目标路径参考原路径的上下文类型,fcontext子 命令下使用

列出所有定义的端口

使用semanage port命令列出所有端口

[root@localhost ~]# semanage port -l SELinux Port Type              Proto    Port Number afs3_callback_port_t           tcp      7001 afs3_callback_port_t           udp      7001 afs_bos_port_t                 udp      7007 afs_fs_port_t                  tcp      2040 afs_fs_port_t                  udp      7000, 7005 afs_ka_port_t                  udp      7004 afs_pt_port_t                  tcp      7002 afs_pt_port_t                  udp      7002 afs_vl_port_t                  udp      7003 agentx_port_t                  tcp      705 agentx_port_t                  udp      705 amanda_port_t                  tcp      10080-10083 amanda_port_t                  udp      10080-10082 … …

怎样使用semanage管理SELinux安全策略
列出指定的端口类型的端口

[root@localhost ~]# semanage port -l|grep -w http_port_t http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000

怎样使用semanage管理SELinux安全策略
通过查询端口号来列出端口类型

[root@localhost ~]# semanage port -l|grep -w 53 dns_port_t                     tcp      53 dns_port_t                     udp      53 [root@localhost ~]# semanage port -l|grep -w 20 ftp_data_port_t                tcp      20 [root@localhost ~]# semanage port -l|grep -w 21 ftp_port_t                     tcp      21, 989, 990

怎样使用semanage管理SELinux安全策略

创建、添加、修改端口

通过下面的命令为http添加新端口

[root@localhost ~]#  [root@localhost ~]# semanage port -a -t http_port_t -p tcp 8888 [root@localhost ~]# # 查看新添加的端口 [root@localhost ~]# semanage port -l|grep -w 8888 http_port_t                    tcp      8888, 80, 81, 443, 488, 8008, 8009, 8443, 9000 # 也可以使用-C参数查看自定义的端口号 [root@localhost ~]# semanage port -lC SELinux Port Type              Proto    Port Number http_port_t                    tcp      8888

怎样使用semanage管理SELinux安全策略
添加一个范围的端口

[root@localhost ~]# semanage port -a -t http_port_t -p tcp 11180-11188 [root@localhost ~]#  [root@localhost ~]# semanage port -lC SELinux Port Type              Proto    Port Number http_port_t                    tcp      8888, 11180-11188

怎样使用semanage管理SELinux安全策略

删除端口

[root@localhost ~]# semanage port -d -t http_port_t -p tcp 8888 [root@localhost ~]#  [root@localhost ~]# semanage port -d -t http_port_t -p tcp 11180-11188 [root@localhost ~]#  # 查看一下,已经没有自定义的端口了 [root@localhost ~]# semanage port -lC

怎样使用semanage管理SELinux安全策略

修改安全上下文

为samba共享目录添加安全上下文

# 没添加安全上下文之前是default_t [root@localhost ~]# ll -dZ /share/ drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /share/ [root@localhost ~]# semanage fcontext -a -t samba_share_t '/share(/.*)?' # 恢复文件默认的安全上下文 [root@localhost ~]# restorecon -Rv /share restorecon reset /share context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:samba_share_t:s0 # 查看一下文件夹已经变成samba_share_t了 [root@localhost ~]# ll -dZ /share drwxr-xr-x. root root unconfined_u:object_r:samba_share_t:s0 /share

怎样使用semanage管理SELinux安全策略
为nfs共享目录添加读写

[root@localhost ~]# ll -dZ /nfsshare/ drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /nfsshare/ [root@localhost ~]#  [root@localhost ~]# semanage fcontext -a -t public_content_rw_t '/nfsshare(/.*)?'  [root@localhost ~]# restorecon -Rv /nfsshare [root@localhost ~]# ll -dZ /nfsshare/ drwxr-xr-x. root root unconfined_u:object_r:public_content_rw_t:s0 /nfsshare/

怎样使用semanage管理SELinux安全策略

总结

本文讲述了添加、修改和删除端口,修改安全上下文。如果你的系统有安装桌面,可以安装图形化管理软件 policycoreutils-gui来进行管理。

[root@localhost ~]# yum -y install policycoreutils-gui # system-config-selinux执行该命令打开图形化管理界面 [root@localhost ~]# system-config-selinux

怎样使用semanage管理SELinux安全策略

关于怎样使用semanage管理SELinux安全策略就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI