1.何为SELinux
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到。
2.SELinux的模式
enforcing: 强制模式,代表SELinux运作中,且已经正确的开始限制domain/type了。
permissive; 宽容模式,代表SELinux运作中,不过仅会有警告讯息并不会实际限制domain/type的存取.这种模式可以运来作为SELinux的debug之用(看下什么原因导致无法访问)。
disabled: 关闭模式
可以用命令 getenforce
查看当前selinux的模式。
安全上下文(security context)
这个是我们主要修改的地方,进程必须和文件的安全上下文对应(不是必须一样)才能对其进行访问。
ls -Z 文件名 # 查看文件的安全上下文
ps -Z 进程pid # 查看进程的安全上下文
3.selinux在系统中的作用。
首先在 /mnt
目录建立一个文件,并且把文件移动到匿名用户的/pub目录里。
分析:文件file的确移动到了/pub目录里,但是通过lftp连接后看不到
context共分为五个部分,以:分隔。
身份识别 | 文件、进程、用户 | 数据类型 | 安全级别 | 划分的不同分类 |
user | role | type | sensitivity | category |
unconfined_u不受限的用户或文件system_u受限的进程或文件 | object_r文件,system_r进程和用户 | 何种类型进程访问何种文件 | s0最低,只有在msl才有意义 | 这个没有什么大的作用 |
1.临时改变文件的安全上下文。
chcon -t public_content_t /var/ftp/fire ##更改fire文件的安全上下文为public_content_t
分析:将file文件的安全上下文,更改为vsftpd可识别的安全上下文后,再通过lftp远程连接,就可以看到fire文件了。
2.永久更改文件的安全上下文。
semanage fcontext -a -t public_content_t '/redhat(/.*)?' ##永久更改文件的安全上下文
semanage fcontext -l | grep redhat ## 查看redhat目录与目录中文件的安全上下文
[root@150 ftp]# mkdir /var/ftp/xiaoma -p
[root@150 ftp]# semanage fcontext -a -t public_content_t '/xiaoma/' ##永久修改安全上下文
[root@150 ~]# mkdir /xiaoma
[root@150 ~]# ls -Zd /xiaoma/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /xiaoma/
[root@150 ~]# semanage fcontext -a -t public_content_t '/xiaoma(/.*)?'
[root@150 ~]# semanage fcontext -l | grep xiaoma
/xiaoma(/.*)? all files system_u:object_r:public_content_t:s0
/xiaoma/ all files system_u:object_r:public_content_t:s0
[root@150 ~]# ls -Zd /xiaoma/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /xiaoma/
[root@150 ~]# restorecon -FvvR /xiaoma/
restorecon reset /xiaoma context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@150 ~]# ls -Zd /xiaoma/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /xiaoma/
[root@150 ~]#
4.selinux的相关作用
1.本地用户上传开关
这里selinux的模式为enforcing。
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/ ##刷新安全上下文
5.selinux 的两种模式
setenforce 1 ##Enforcing,拒绝访问
setenforce 0 ##permissive警告,不拒绝
例子:
[root@150 ~]# cd /mnt/
[root@150 mnt]# touch test
[root@150 mnt]# mv test /var/ftp/
[root@150 mnt]# lftp 172.25.254.150
lftp 172.25.254.150:~> ls
drwxrwxr-x 2 0 50 20 Feb 14 18:58 pub
lftp 172.25.254.150:/> quit
[root@150 mnt]# getenforce
Enforcing
[root@150 mnt]# setenforce 0
[root@150 mnt]# getenforce
Permissive
[root@150 mnt]# lftp 172.25.254.150
lftp 172.25.254.150:~> ls
drwxrwxr-x 2 0 50 20 Feb 14 18:58 pub
-rw-r--r-- 1 0 0 0 Feb 14 19:35 test
lftp 172.25.254.150:/> quit
6.selinux 如何获取报错的解决方案。
[root@localhost ~]# yum install setroubleshoot-server-3.2.17-2.el7.x86_64
[root@localhost ~]# > /var/log/messages ##清空日志
[root@localhost ~]# lftp 172.25.254.150
lftp 172.25.254.150:~> ls
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.150:/> cd pub/ ##进入没有写权限的pub/
lftp 172.25.254.150:/pub> ls
lftp 172.25.254.150:/pub> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd) ##权限太小被拒绝
lftp 172.25.254.150:/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 *******************
关于Linux内核级防火墙selinux的总结就是这些。