Linux SElinux

SElinux
前提:将之前的操作恢复原状态

[root@foundation68 ~]# rm -fr /etc/vsftpd/vsftpd.conf
[root@foundation68~]# 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状态

[root@foundation68 ~]# getenforce 
Disabled

2、SElinux状态为disable

此时新建文件

[root@foundation68 ~]# mkdir /test
[root@foundation68 ~]# touch /test/testfile
[root@foundation68 ~]# 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
[root@foundation68 ~]# mv /t
test/ tmp/  
[root@foundation68 ~]# mv /test/testfile  /var/ftp/
[root@foundation68 ~]# 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
[root@foundation68 ~]# ls -Z /var/ftp/  ##查询/var/ftp的安全上下文,Disabled时无安全上下文,因此不需要进行安全上下文匹配,可以在/var/ftp中看到
drwxrwxr-x root ftp  ?                                pub
-rw-r--r-- root root ?                                testfile
[root@foundation68 ~]#  

查询/var/ftp的安全上下文,Disabled时无安全上下文,因此不需要进行安全上下文
匹配,可以在/var/ftp中看到

3、开启SElinux

保存selinux状态文件

[root@foundation68 ~]# vim /etc/sysconfig/selinux 
[root@foundation68 ~]# 

修改状态;SELINUX=enforcing
在这里插入图片描述
修改完后必须重启
重新新建文件,验证lftp登陆后是否能看到

[root@foundation68 ~]# touch /test/testfile1
[root@foundation68 ~]# mv /test/testfile1 /var/ftp
[root@foundation68 ~]# 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看不到新建的文件
[root@foundation68 ~]# 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的安全上下文

[root@foundation68 ~]# chcon -t public_content_t /var/ftp/testfile1
[root@foundation68 ~]# 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
[root@foundation68 ~]# 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:/> 

重启后,之前修改的安全上下文失效,现在永久生效安全上下文

[root@foundation68 ~]# vim /etc/vsftpd/vsftpd.conf 
[root@foundation68 ~]# systemctl restart vsftpd
[root@foundation68 ~]# 

添加anon_root=/ftphome

[root@foundation68 ~]# semanage fcontext -a -t public_content_t '/ftphome(/.*)?'
[root@foundation68 ~]# 

semanage命令

semanage命令时用来查询与修改SElinux默认目录的安全上下文
semanage -l 查询
semanage fcontext:主要用在安全上下文方面
semanage -a :增加,可以增加一些目录的默认安全上下文类型设置
semanage -m: 修改
semanage -d:删除

[root@foundation68 ~]# 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:将过程显示到屏幕上

在这里插入图片描述

[root@foundation68 ~]# 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
[root@foundation68 ~]# 

2、文件的上传
用久设置好后匿名用户文件无法上传(状态不是disable)

(1)查看各个配置的布尔值

[root@foundation68 ~]# 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
[root@foundation68 ~]# 

家目录以及写权限都为关闭状态,现在打开他们的权限

[root@foundation68 ~]# setsebool -P ftpd_anon_write on
[root@foundation68 ~]# setsebool -P ftpd_connect_all_unreserved on
[root@foundation68 ~]# 
[root@foundation68 ~]# vim /etc/vsftpd/vsftpd.conf

打开29行
注销刚才家的家目录,此时匿名用户家目录为/var/ftp/
给/var/ftp/pub/设置安全组

[root@foundation68 ~]# chgrp ftp /var/ftp/pub/
[root@foundation68 ~]# chmod 775 /var/ftp/pub/
[root@foundation68 ~]# 

给安全上下文添加读权限

[root@foundation68 ~]# ls -Zd /var/ftp/pub/
drwxrwxr-x. root ftp system_u:object_r:public_content_t:s0 /var/ftp/pub/
[root@foundation68 ~]# ls -Zd /var/ftp/pub/
drwxrwxr-x. root ftp system_u:object_r:public_content_rw_t:s0 /var/ftp/pub/
[root@foundation68 ~]# 

客户端上传文件测试

[root@foundation68 ~]# 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版本

[root@foundation68 ~]# 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
[root@foundation68 ~]# 

安装setroubleshoot

[root@foundation68 ~]# 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连接后看不到就会有报错日至)

[root@foundation68 ~]# touch /test/testfile3
[root@foundation68 ~]# mv /test/testfile3 /etc/ftp

[root@foundation68 ~]# mv /test/testfile3 /etc/ftp
[root@foundation68 ~]# 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:/> 

查看日至

[root@foundation68 ~]# 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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值