一、什么是selinux?
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到。
二、selinux的模式
enforcing: 强制模式,代表SELinux运作中,且已经正确的开始限制domain/type了。
permissive; 宽容模式,代表SELinux运作中,不过仅会有警告讯息并不会实际限制domain/type的存取.这种模式可以运来作为SELinux的debug之用(看下什么原因导致无法访问)。
disabled: 关闭模式
可以用命令getenforce查看当前selinux的模式。
安全上下文(security context)
这个是我们主要修改的地方,进程必须和文件的安全上下文对应(不是必须一样)才能对其进行访问。
ls -Z 文件名 # 查看文件的安全上下文
ps -Z 进程pid # 查看进程的安全上下文
三、selinux在系统中的作用。
我们通过一系列的示例让大家自己体会到selinux模块的强大功能。
首先给大家看一个现象:
我们在/mnt目录建立一个文件,并且把文件移动到匿名用户的pub目录里。
分析:这里我们看到文件fire的确移动到了pub目录里,但是通过lftp连接后que看不到,这是为什么呢?
context共分为五个部分,以:分隔。
user |
role |
type |
sensitivity |
category |
身份识别 |
文件、进程、用户 |
数据类型 |
安全级别 |
划分的不同分类 |
unconfined_u不受限的用户或文件system_u受限的进程或文件 |
object_r文件,system_r进程和用户 |
何种类型进程访问何种文件 |
s0最低,只有在msl才有意义 |
这一位没有什么大的作用 |
分析:可以看到redhat文件和fire文件的安全上下文不同,vsftpd进程无法识别fire文件的安全上下文,因此lftp远程连接时无法看到。
那么,怎呢才能lftp远程连接时能够看到该文件呢?
1、临时改变文件的安全上下文。
chcon -t public_content_t /var/ftp/fire ##更改fire文件的安全上下文为public_content_t
示例:
分析:将fire文件的安全上下文,更改为vsftpd可识别的安全上下文后,再通过lftp远程连接,就可以看到fire文件了。
2、永久更改文件的安全上下文。
semanage fcontext -a -t public_content_t '/redhat(/.*)?' ##永久更改文件的安全上下文
semanage fcontext -l | grep redhat ## 查看redhat目录与目录中文件的安全上下文
示例:
[root@localhost ~]# ls -Zd /redhat/
drwxr-xr-x. root root system_u:object_r:default_t:s0 /redhat/
[root@localhost ~]# semanage fcontext -a -t public_content_t '/redhat(/.*)?' ##永久修改安全上下文
[root@localhost ~]# semanage fcontext -l | grep redhat ##查看redhat与内部文件修改的上下文关系
/etc/redhat-lsb(/.*)? all files system_u:object_r:bin_t:s0
/usr/libexec/ipa/com\.redhat\.idm\.trust-fetch-domains regular file system_u:object_r:ipa_helper_exec_t:s0
/usr/libexec/ipa/oddjob/com\.redhat\.idm\.trust-fetch-domains regular file system_u:object_r:ipa_helper_exec_t:s0
/redhat(/.*)? all files system_u:object_r:public_content_t:s0
[root@localhost ~]# ls -Zd /redhat/ ##查看上下文关系,并未改变
drwxr-xr-x. root root system_u:object_r:default_t:s0 /redhat/
[root@localhost ~]# restorecon -FvvR /redhat/ ##手动刷新安全上下文
restorecon reset /redhat context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /redhat/redhat1 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /redhat/redhat2 context unconfined_u:object_r:user_tmp_t:s0->system_u:object_r:public_content_t:s0
[root@localhost ~]# ls -Zd /redhat/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /redhat/
[root@localhost ~]#
四、selinux的相关作用
1、本地用户上传开关
这里selinux的模式为enforcing。
本地用户上传文件会报错。
打开开关:
setsebool -P ftp_home_dir on ##打开上传
2、匿名用户上传
首先这里selinux的模式为enforcing。
vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
anon_upload_enable=YES ##允许匿名用户上传
systemctl restart vsftpd.service
打开selinux匿名用户上传开关。
getsebool -a | grep ftp ##查询权限开关
setsebool -P ftpd_anon_write on ##打开ftp匿名用户写权限
更改匿名用户家目录权限和安全上下文可写。
chgrp ftp /var/ftp/pub/
chmod 775 /var/ftp/pub/
ls -Zd /var/ftp/pub/ ##查看目录的安全上下文
semanage fcontext -a -t public_content_rw_t /var/ftp/pub ##开启安全上下文写权限
semanage fcontext -l | grep /var/ftp/pub ##查看pub目录的安全上下文更改。
restorecon -RvvF /var/ftp/pub/ ##刷新安全上下文
五、selinux 的两种模式
setenforce 1 ##Enforcing,拒绝访问
setenforce 0 ##peremissive警告,不拒绝
测试:
[root@localhost ~]# cd /mnt
[root@localhost mnt]# touch test
[root@localhost mnt]# mv test /var/ftp/
[root@localhost mnt]# lftp 172.25.254.233
lftp 172.25.254.233:~> ls
drwxrwxr-x 2 0 50 44 Jan 28 02:47 pub
lftp 172.25.254.233:/> quit
[root@localhost mnt]# geten
getenforce getent
[root@localhost mnt]# getenforce
Enforcing
[root@localhost mnt]# setenforce 0
[root@localhost mnt]# getenforce
Permissive
[root@localhost mnt]# lftp 172.25.254.233
lftp 172.25.254.233:~> ls
drwxrwxr-x 2 0 50 44 Jan 28 02:47 pub
-rw-r--r-- 1 0 0 0 Jan 28 02:52 test
lftp 172.25.254.233:/> quit
[root@localhost mnt]# > /var/log/audit/audit.log
[root@localhost mnt]# lftp 172.25.254.233
lftp 172.25.254.233:~> ls
drwxrwxr-x 2 0 50 44 Jan 28 02:47 pub
-rw-r--r-- 1 0 0 0 Jan 28 02:52 test
六、selinux 如何获取报错的解决方案。
[root@localhost ~]# rpm -qa | grep setroubleshoot
setroubleshoot-server-3.2.17-2.el7.x86_64 ##提供解决方案的软件
setroubleshoot-3.2.17-2.el7.x86_64
setroubleshoot-plugins-3.0.59-1.el7.noarch
[root@localhost ~]# yum install setroubleshoot-server-3.2.17-2.el7.x86_64
[root@localhost ~]# > /var/log/messages ##清空日志
[root@localhost ~]# lftp 172.25.254.232
lftp 172.25.254.232:~> ls
-rw-r--r-- 1 0 0 0 Jan 24 08:13 file
drwxr-xr-x 2 0 0 6 Jan 24 08:25 pub
-rw-r--r-- 1 0 0 0 Jan 24 06:17 test
lftp 172.25.254.232:/> cd pub/ ##进入没有写权限的pub/
lftp 172.25.254.232:/pub> ls
lftp 172.25.254.232:/pub> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd) ##权限太小被拒绝
lftp 172.25.254.232:/pub> quit
[root@localhost ~]# cat /var/log/messages ##查看日志
***** Plugin catchall_boolean (57.6 confidence) suggests ******************
If you want to allow ftpd to full access
Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.
You can read 'None' man page for more details.
Do
setsebool -P ftpd_full_access 1 ##出现解决方案
***** Plugin catchall_labels (36.2 confidence) suggests *******************