在linux里面,所有的文件和进程都有一个security context的,而SELinux就是通过security context就用来对文件和进程进行控制的。
如何去查看文件的security context值呢: #ls -Z
[root@localhost ~]# ls -Z
-rw——- root root system_u:object_r:user_home_t anaconda-ks.cfg
drwxr-xr-x root root root:object_r:user_home_t Desktop
-rw-r–r– root root root:object_r:user_home_t file
-rw-r–r– root root root:object_r:user_home_t Finished
-rw-r–r– root root root:object_r:user_home_t install.log
-rw-r–r– root root root:object_r:user_home_t install.log.syslog
-rw-r–r– root root root:object_r:user_home_t Package
-rw-r–r– root root root:object_r:user_home_t Running
[root@localhost ~]#
这个就是我们文件的security context值,
root:object_r:user_home_t
root 用户的用户名(一般文件是谁创建的,这个用户名就是谁)
object_r 文件的角色
user_home_t context值的类型
context值的类型会随着文件在不同的位置,会跟着发生变化。
现在通过试验来验证一下
[root@localhost ~]# ls -Z | grep file
-rw-r–r– root root root:object_r:user_home_t file
[root@localhost ~]# cp file /var/ftp/pub/
cp: overwrite `/var/ftp/pub/file’? y
[root@localhost ~]#
[root@localhost ~]# cd /var/ftp/pub/
[root@localhost pub]#
[root@localhost pub]# ls -Z | grep file
-rw-r–r– root root system_u:object_r:public_content_t file
[root@localhost pub]#
可以看到,这个文件的context值就发生变化了。
[root@localhost ~]#
[root@localhost ~]# cd /var/ftp/
[root@localhost ftp]# ls -Z
drwxr-xr-x root root system_u:object_r:public_content_t pub
这个文件的context值会随着目录的作用和环境的不同而发生改变。它会继承上一级目录的context值。
如何去查看一个进程的security context值呢:ps -Z
[root@localhost ~]# ps -Z
LABEL PID TTY TIME CMD
root:system_r:unconfined_t:SystemLow-SystemHigh 18408 pts/1 00:00:00 bash
root:system_r:unconfined_t:SystemLow-SystemHigh 1927 4 pts/1 00:00:00 tail
root:system_r:unconfined_t:SystemLow-SystemHigh 21330 pts/1 00:00:00 ps
如何去修改一个文件的context值呢,使用chcon命令来修改文件的context值,
# chcon -t context值的类型 文件名
[root@localhost ~]# ls -Z | grep file
-rw-r–r– root root root:object_r:user_home_t file
[root@localhost ~]#
[root@localhost ~]# chcon -t tmp_t file
[root@localhost ~]# ls -Z | grep file
-rw-r–r– root root root:object_r:tmp_t file
可以看到,file这个文件的context值就改变了。也可以改变目录的context值,要加上-R参数,表示递归。
如何去恢复一个文件的context值呢,
#restorecon -v file
-v 是查看改变的过程
[root@localhost ~]#
[root@localhost ~]# restorecon -v file
restorecon reset /root/file context root:object_r:tmp_t:s0->root:object_r:user_home_t:s0
[root@localhost ~]#
[root@localhost ~]# ls -Z | grep file
-rw-r–r– root root root:object_r:user_home_t file
OK,file这个文件的context值就改变回来了。同样的,恢复目录的context值也需要加上-R参数,表示递归。
SELinux的管理
关于SELinux的三种模式
Enforcing 强制模式
Permissive 警告模式(运用排错中)
Disabled 禁用模式
从强制模式切换到禁用模式,或者禁用模式切换到强制模式,都必须等到计算机下次启动的时候才会生效的。
但是强制模式和警告模式在切换的时候当前就可以生效。
如何去修改SELinux的模式呢,
可以通过文件来修改
在/etc/sysconfig/selinux这个文件中可以修改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 – SELinux is fully disabled.
SELINUX=enforcing
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted – Only targeted network daemons are protected.
# strict – Full SELinux protection.
SELINUXTYPE=targeted
也可以通过图形界面修改:system-config-securitylevel
那么如何去查看计算机中正在生效的模式呢:使用getenforce命令
[root@localhost ~]# getenforce
Enforcing
可以看到,系统正在处于SELinux的强制模式。
如何通过命令来实现强制模式和警告模式之间的切换,使用setenforce命令来切换
#setenforce 0
切换成警告模式
#setenforce 1
切换成强制模式
[root@localhost ~]#
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce
Enforcing
这就是强制模式和警告模式之间的切换,这些切换都是立即生效的。
关于selinux的Boolean值
如何去查看Boolean值呢,
# getsebool -a
[root@localhost ~]# getsebool -a | grep ftp
allow_ftpd_anon_write –> on
allow_ftpd_full_access –> off
allow_ftpd_use_cifs –> off
allow_ftpd_use_nfs –> off
allow_tftp_anon_write –> off
ftp_home_dir –> on
ftpd_connect_db –> off
ftpd_disable_trans –> off
ftpd_is_daemon –> on
httpd_enable_ftp_server –> off
tftpd_disable_trans –> off
[root@localhost ~]#
这样可以查看关于服务的Boolean值,
怎么设置服务的Boolean值
# setsebool -P allow_ftpd_anon_write=0
[root@localhost ~]# setsebool -P allow_ftpd_anon_write=0
[root@localhost ~]# getsebool -a | grep ftp
allow_ftpd_anon_write –> off
allow_ftpd_full_access –> off
allow_ftpd_use_cifs –> off
allow_ftpd_use_nfs –> off
allow_tftp_anon_write –> off
ftp_home_dir –> on
ftpd_connect_db –> off
ftpd_disable_trans –> off
ftpd_is_daemon –> on
httpd_enable_ftp_server –> off
tftpd_disable_trans –> off
[root@localhost ~]#
0代表关闭,1代表打开。
在linux系统中,如果我们的SELinux是处于强制状态的,一旦用户触犯了selinux的规则,那么SELinux将会阻挡用户的访问。但是linux也提供
给用户提供了一个解决方案,当用户触犯了SELinux的规则后,系统会跳出一个黄色五角星,并给出解决的方案。如果这个黄色五角星没有跳出
来,可以使用sealert –b这条命令让其强制跳出来。如果这个软件包没有安装,那么就需要安装下,
这个功能是由setroubleshoot这个软件包提供的。
[root@localhost ~]# service setroubleshoot restart
Stopping setroubleshootd: [ OK ]
Starting setroubleshootd: [ OK ]
[root@localhost ~]#
联系客服