Linux SElinux
SElinux
前提:将之前的操作恢复原状态
[[email protected] ~]# rm -fr /etc/vsftpd/vsftpd.conf
[[email protected]~]# yum reinstall vsftpd
一、SElinux概念
SElinux:安全增强型linux简称SElinux,它是一个Linux内核模块,也是linux的一个安全子系统。
1、SElinux的三种工作模式
enforcing:强制模式,违反SElinux规则的行为将被阻止并记录在日志中
permissive :宽容模式,违反SElinux规则的行为只会记录到日志中,一般为调试用。
disable:关闭SElinux
SElinux工作模式可以在==/etc/selinux/config==中设定,如果想从disable切换到enforcing或者permissive,需要重启系统.反过来也一样,enforcing或者permissive模式可以通过setenforce 1|0命令快速切换。
注意:如果系统在关闭(disable)SElinux的状态下运行了一段时间,
在打开SElinux之后的第一次重启速度可能比较慢。因为系统必须为磁盘中的文件创建安全上下文。
二、安全上下文
1、暂时修改安全上下文
(1)getenforce 查看selinux状态
[[email protected] ~]# getenforce
Disabled
2、SElinux状态为disable
此时新建文件
[[email protected] ~]# mkdir /test
[[email protected] ~]# touch /test/testfile
[[email protected] ~]# lftp 172.25.254.68
lftp 172.25.254.68:~> ls
drwxrwxr-x 2 0 50 46 Oct 31 12:36 pub
lftp 172.25.254.68:/> exit
[[email protected] ~]# mv /t
test/ tmp/
[[email protected] ~]# mv /test/testfile /var/ftp/
[[email protected] ~]# lftp 172.25.254.68
lftp 172.25.254.68:~> ls
drwxrwxr-x 2 0 50 46 Oct 31 12:36 pub
-rw-r--r-- 1 0 0 0 Nov 05 14:28 testfile
lftp 172.25.254.68:/> exit
[[email protected] ~]# ls -Z /var/ftp/ ##查询/var/ftp的安全上下文,Disabled时无安全上下文,因此不需要进行安全上下文匹配,可以在/var/ftp中看到
drwxrwxr-x root ftp ? pub
-rw-r--r-- root root ? testfile
[[email protected] ~]#
查询/var/ftp的安全上下文,Disabled时无安全上下文,因此不需要进行安全上下文
匹配,可以在/var/ftp中看到
3、开启SElinux
保存selinux状态文件
[[email protected] ~]# vim /etc/sysconfig/selinux
[[email protected] ~]#
修改状态;SELINUX=enforcing
修改完后必须重启
重新新建文件,验证lftp登陆后是否能看到
[[email protected] ~]# touch /test/testfile1
[[email protected] ~]# mv /test/testfile1 /var/ftp
[[email protected] ~]# ls -Z /var/ftp
drwxrwxr-x. root ftp system_u:object_r:public_content_t:s0 pub
-rw-r--r--. root root system_u:object_r:public_content_t:s0 testfile
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 testfile1
当SELINUX=enforcing时,查询上下文时显示上下文,由于
SELINUX打开时重新配置了磁盘中的上下文,因此磁盘中原
本存在的文件在重启时配置了上下文。新建立的文件没有配
置上下文。因此lftp看不到新建的文件
[[email protected] ~]# lftp 172.25.254.68
lftp 172.25.254.68:~> ls
drwxrwxr-x 2 0 50 46 Oct 31 12:36 pub
-rw-r--r-- 1 0 0 0 Nov 05 14:28 testfile
lftp 172.25.254.68:/>
修改testfile1的安全上下文
[[email protected] ~]# chcon -t public_content_t /var/ftp/testfile1
[[email protected] ~]# ls -Z /var/ftp
drwxrwxr-x. root ftp system_u:object_r:public_content_t:s0 pub
-rw-r--r--. root root system_u:object_r:public_content_t:s0 testfile
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 testfile1
[[email protected] ~]# lftp 172.25.254.68
lftp 172.25.254.68:~> ls
drwxrwxr-x 2 0 50 46 Oct 31 12:36 pub
-rw-r--r-- 1 0 0 0 Nov 05 14:28 testfile
-rw-r--r-- 1 0 0 0 Nov 06 02:26 testfile1
lftp 172.25.254.68:/>
重启后,之前修改的安全上下文失效,现在永久生效安全上下文
[[email protected] ~]# vim /etc/vsftpd/vsftpd.conf
[[email protected] ~]# systemctl restart vsftpd
[[email protected] ~]#
添加anon_root=/ftphome
[[email protected] ~]# semanage fcontext -a -t public_content_t '/ftphome(/.*)?'
[[email protected] ~]#
semanage命令
semanage命令时用来查询与修改SElinux默认目录的安全上下文
semanage -l 查询
semanage fcontext:主要用在安全上下文方面
semanage -a :增加,可以增加一些目录的默认安全上下文类型设置
semanage -m: 修改
semanage -d:删除
[[email protected] ~]# ls -Z /ftphome/
drwxr-xr-x. root root system_u:object_r:default_t:s0 westos1
drwxr-xr-x. root root system_u:object_r:default_t:s0 westos2
drwxr-xr-x. root root system_u:object_r:default_t:s0 westos3
此时安全上下文并没有加载,现需要恢复SElinux文件属性恢复文件的安全上下文
restorecon命令用来恢复SElinux文件属性恢复文件的安全上下文
restorecon -i :忽略不存在的文件
restorecon -R/-r:第归处理目录
restorecon -n:不改变文件标签
restorecon -v:将过程显示到屏幕上
[[email protected] ~]# ls -Z /ftphome/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 westos1
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 westos2
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 westos3
[[email protected] ~]#
2、文件的上传
用久设置好后匿名用户文件无法上传(状态不是disable)
(1)查看各个配置的布尔值
[[email protected] ~]# getsebool -a | grep ftp
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> off
[[email protected] ~]#
家目录以及写权限都为关闭状态,现在打开他们的权限
[[email protected] ~]# setsebool -P ftpd_anon_write on
[[email protected] ~]# setsebool -P ftpd_connect_all_unreserved on
[[email protected] ~]#
[[email protected] ~]# vim /etc/vsftpd/vsftpd.conf
打开29行
注销刚才家的家目录,此时匿名用户家目录为/var/ftp/
给/var/ftp/pub/设置安全组
[[email protected] ~]# chgrp ftp /var/ftp/pub/
[[email protected] ~]# chmod 775 /var/ftp/pub/
[[email protected] ~]#
给安全上下文添加读权限
[[email protected] ~]# ls -Zd /var/ftp/pub/
drwxrwxr-x. root ftp system_u:object_r:public_content_t:s0 /var/ftp/pub/
[[email protected] ~]# ls -Zd /var/ftp/pub/
drwxrwxr-x. root ftp system_u:object_r:public_content_rw_t:s0 /var/ftp/pub/
[[email protected] ~]#
客户端上传文件测试
[[email protected] ~]# lftp 172.25.254.68
lftp 172.25.254.68:~> ls
drwxrwxr-x 2 0 50 46 Oct 31 12:36 pub
-rw-r--r-- 1 0 0 0 Nov 05 14:28 testfile
-rw-r--r-- 1 0 0 0 Nov 06 02:26 testfile1
lftp 172.25.254.68:/> cd pub/
lftp 172.25.254.68:/pub> ls
-rwxr--r-- 1 0 0 0 Oct 31 07:26 123.txt
-rw------- 1 14 50 0 Oct 31 09:39 file
-r-----r-- 1 14 50 0 Oct 31 12:36 file1
lftp 172.25.254.68:/pub> put /root/Desktop/test
lftp 172.25.254.68:/pub> ls
-rwxr--r-- 1 0 0 0 Oct 31 07:26 123.txt
-rw------- 1 14 50 0 Oct 31 09:39 file
-r-----r-- 1 14 50 0 Oct 31 12:36 file1
-rw------- 1 14 50 0 Nov 06 04:01 test
lftp 172.25.254.68:/pub>
setlinux的日至
存放setlinux日至的文件为 /var/log/audit/audit.log,但此文件中只存放报错信息不提供解决方案。/var/log/messages也可能不提供解决方案,只有安装了setroubleshoot才在massage李面看到具体的报错解决方案
查看setroubleshoot版本
[[email protected] ~]# rpm -qa | grep setroubleshoot
setroubleshoot-3.2.27.2-3.el7.x86_64
setroubleshoot-plugins-3.0.64-2.1.el7.noarch
setroubleshoot-server-3.2.27.2-3.el7.x86_64
[[email protected] ~]#
安装setroubleshoot
[[email protected] ~]# yum install setsetroubleshoot-server-3.2.24-1.1.el7.x86_64(必须装server的)
Loaded plugins: langpacks, product-id, search-disabled-repos,
: subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel7.2 | 4.1 kB 00:00
software | 2.9 kB 00:00
No package setsetroubleshoot-server-3.2.24-1.1.el7.x86_64 available.
Error: Nothing to do
已经安装,现在清空/var/log/message,连接时连接错误,查看日至(建立一个文件.将文件移动到/etc/ftp/下.lfyp连接后看不到就会有报错日至)
[[email protected] ~]# touch /test/testfile3
[[email protected] ~]# mv /test/testfile3 /etc/ftp
[[email protected] ~]# mv /test/testfile3 /etc/ftp
[[email protected] ~]# lftp 172.25.254.68
lftp 172.25.254.68:~> ls
drwxrwxr-x 2 0 50 58 Nov 06 04:01 pub
-rw-r--r-- 1 0 0 0 Nov 05 14:28 testfile
-rw-r--r-- 1 0 0 0 Nov 06 02:26 testfile1
lftp 172.25.254.68:/>
查看日至
[[email protected] ~]# cat /var/log/messages
Nov 6 14:56:28 foundation68 python: SELinux is preventing /usr/sbin/vsftpd from getattr access on the file /westosfile2.#012#012***** Plugin restorecon (88.2 confidence) suggests ************************#012#012If you want to fix the label. #012/westosfile2 default label should be etc_runtime_t.#012Then you can run restorecon.#012Do#012# /sbin/restorecon -v /westosfile2#012#012***** Plugin catchall_boolean (7.51 confidence) suggests ******************#012#012If you want to allow ftpd to full access#012Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.#012You can read 'None' man page for more details.#012Do#012setsebool -P ftpd_full_access 1#012#012***** Plugin catchall_labels (4.88 confidence) suggests *******************#012#012If you want