SELinux常用设置小结

  • SELinux是一个安全子系统,不是一个简单的服务,是进一步保护系统安全的子系统。它是通过限制服务程序的功能来保护系统的。SELinux安全上下文技术是限制文件只能被谁(服务程序)所获取,禁止服务程序做超出服务范围内的事情。
  • SELinux的安装过程如下,在安装前请先配置好yum仓库。安装过程如下:
[root@localhost ~]# yum install policycoreutils-python	#先安装semanage的依赖包
#安装过程省略
[root@localhost ~]# yum provides semanage	#安装SELinux管理工具 semanage,过程如下
base/7/x86_64/filelists_db                              | 7.2 MB  00:00:03     
extras/7/x86_64/filelists_db                            | 259 kB  00:00:00     
updates/7/x86_64/filelists_db                           | 6.5 MB  00:00:03     
policycoreutils-python-2.5-34.el7.x86_64 : SELinux policy core python utilities
Repo        : base
Matched from:
Filename    : /usr/sbin/semanage

policycoreutils-python-2.5-34.el7.x86_64 : SELinux policy core python utilities
Repo        : @base
Matched from:
Filename    : /usr/sbin/semanage
  • SELinux有3种配置模式:enforcing 是强制启用安全策略模式,拦截服务的不合法请求。permissive 是遇到服务越权访问时,只发出警告而不强制拦截。disabled 对于越权的行为不警告也不拦截。配置文件是 /etc/selinux/config,在这个配置文件中,如果 SELINUX 不等于 enforcing,就将其修改为 enforcing,表示启用SELinux,保护系统更安全。
  • 临时关闭SELinux命令是:setenforce 0。临时开启命令是:setenforce 0。getenforce 命令查看当前SELinux状态。示例如下:
[root@localhost selinux]# getenforce 		#查看SELinux状态,默认是开启的
Enforcing
[root@localhost selinux]# setenforce 0		#临时关闭SELinux
[root@localhost selinux]# getenforce 
Permissive
[root@localhost selinux]# setenforce 1		#临时开启SELinux
[root@localhost selinux]# getenforce 
Enforcing
  • semanage 命令中常用参数及作用如下表所示:
参数 作用
-l 查询
-a 添加
-m 修改
-d 删除
  • 使用semanage 命令查询ssh服务端口、添加端口、删除端口示例如下:
[root@localhost ~]# semanage port -l | grep ssh		#查看当前ssh端口
ssh_port_t                     tcp      22
[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 5500	#向SELinux添加ssh端口
[root@localhost ~]# semanage port -l | grep ssh		#验证端口是否添加成功
ssh_port_t                     tcp      5500, 22

#删除端口示例如下
[root@localhost ~]# semanage port --delete -t ssh_port_t -p tcp 5500
[root@localhost ~]# semanage port -l | grep ssh		#验证是否删除成功
ssh_port_t                     tcp      22
  • semanage 命令的 fcontext 参数用于编辑服务对目录的访问权限。在Linux中服务有对应的默认访问目录,如果要修改默认目录,就要涉及到SELinux域权限。例如 http 服务使用的默认目录是 /var/www/html,当修改这个默认目录后,要给新目录添加SELinux权限才能生效。ls命令的 -Z 参数可以查看目录的SELinux权限。下面的示例是修改 http 服务的默认目录:
[root@localhost ~]# ls -ldZ /var/www/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
#上面输出可看到 /var/www/html 目录有 httpd_sys_content_t 权限
[root@localhost ~]# ls -ldZ /home/wwwroot
drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot

#在给目录添加 SELinux 域权限时,目录最后面不能是 /,如果有子目录,还要对子目录做同样的操作
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
# restorecon 命令可以不用对每个子目录进行设置
[root@localhost ~]# restorecon -Rv /home/wwwroot	#让SELinux值生效
restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  • getsebool命令查询SELinux策略内各项规则的布尔值。setsebool命令用来设置SELinux策略内各项规则的布尔值。例如查询 httpd_enable_homedirs 是否为关闭,若是关闭状态,则httpd服务没有开启个人用户家目录主机功能。示例如下:
[root@localhost ~]# getsebool -a | grep httpd_enable_homedirs
httpd_enable_homedirs --> off
[root@localhost ~]# setsebool -P httpd_enable_homedirs=on	# -P表示永久生效
[root@localhost ~]# getsebool -a | grep httpd_enable_homedirs
httpd_enable_homedirs --> on
原文地址:https://www.cnblogs.com/Micro0623/p/15650506.html