selinux安全上下文的管理及布尔值的设置

selinux安全上下文的管理

 1. ls -Z  filename                        #查看文件的安全上下文 chcon -t
 2. public_content_t filename     #临时更改文件的安全上下文的类型

semanage fcontext -a -t public_content_t '/westos(/.)?’ #永久更改文件的安全上下文的类型; -a 添加,-t 类型,(/ 本身内容;. 所有隐藏文件;)
在这里插入图片描述
实验:selinux对文件的影响

1.当selinux的状态为Disabled时,lftp连接后可以查看到外来文件
##查看selinux的状态
[root@localhost ~]# getenforce
Disabled
[root@localhost ~]# touch /mnt/westosfile1
##将外来文件移动到ftp服务的默认发布目录/var/ftp中
[root@localhost ~]# mv /mnt/westosfile1 /var/ftp
[root@localhost ~]# cd /var/ftp
[root@localhost ftp]# ls
pub  westosfile1
##可以查看到外来文件
[root@localhost ~]# lftp 172.25.254.234 
lftp 172.25.254.234:~> ls
drwxr-xr-x    3 0        0              69 Aug 03  2015 pub
-rw-r--r--    1 0        0               0 Nov 03 06:12 westosfile1
lftp 172.25.254.234:/> exit

在这里插入图片描述

2.默认本地用户可以登陆,上传,建立目录,下载(不能下载自己创建的文件,但可以下载自己上传的文件),删除(不能删除自己创建的文件,但可以删除自己上传的文件)
##建立用户
[root@localhost ~]# useradd westos
##设定用户密码
[root@localhost ~]# passwd westos

[root@localhost ~]# lftp 172.25.254.234 -u westos
Password: 
lftp westos@172.25.254.234:~> ls       
lftp westos@172.25.254.234:~> put /etc/passwd
2101 bytes transferred
lftp westos@172.25.254.234:~> ls
-rw-r--r--    1 1001     1001         2101 Nov 06 16:11 passwd
lftp westos@172.25.254.234:~> mkdir linux
mkdir ok, `linux' created
lftp westos@172.25.254.234:~> ls
drwxr-xr-x    2 1001     1001            6 Nov 06 16:11 linux
-rw-r--r--    1 1001     1001         2101 Nov 06 16:11 passwd
lftp westos@172.25.254.234:~> get /etc/passwd
2101 bytes transferred
lftp westos@172.25.254.234:~> get linux/
get: Access failed: 550 Failed to open file. (linux/)
lftp westos@172.25.254.234:~> rm linux/
rm: Access failed: 550 Delete operation failed. (linux/)
lftp westos@172.25.254.234:~> rm passwd 
rm ok, `passwd' removed
lftp westos@172.25.254.234:~> ls
drwxr-xr-x    2 1001     1001            6 Nov 06 16:11 linux
lftp westos@172.25.254.234:~> put /etc/passwd
2101 bytes transferred                             
lftp westos@172.25.254.234:~> ls
drwxr-xr-x    2 1001     1001            6 Nov 06 16:11 linux
-rw-r--r--    1 1001     1001         2101 Nov 06 16:12 passwd
lftp westos@172.25.254.234:~> exit
selinux的状态为关闭模式时,文件和程序的安全上下文都会被隐藏起来

查看 /var/ftp里的所有文件的安全上下文

[root@localhost ~]# ls -Z /var/ftp

[root@localhost ~]# ps auxZ | grep ftp

在这里插入图片描述

3.当selinux的状态为enforcing时,此时lftp连接便无法访问外来文件,并有警告信息
##更改selinux的状态
[root@localhost ~]# vim /etc/sysconfig/selinux 

SELINUX=enforcing   ##强制模式(警告并拒绝)

[root@localhost ~]# getenforce 
Disabled
##必须重启,否则不生效
[root@localhost ~]# reboot

##查看selinux的状态
[root@localhost ~]# getenforce
Enforcing



将外来文件移动到ftp服务的默认发布目录/var/ftp中

[root@localhost ~]# touch /mnt/westosfile2
[root@localhost ~]# mv /mnt/westosfile2 /var/ftp
[root@localhost ~]# cd /var/ftp/
[root@localhost ftp]# ls
pub  westosfile1  westosfile2



##先清空日志
[root@localhost ~]# > /var/log/messages
[root@localhost ~]# cat /var/log/messages
##此时无法访问外来文件!!!
[root@localhost ~]# lftp 172.25.254.234
lftp 172.25.254.234:~> ls
drwxr-xr-x    3 0        0              69 Aug 03  2015 pub
-rw-r--r--    1 0        0               0 Nov 03 06:12 westosfile1
lftp 172.25.254.234:/> exit


 ##查看日志,发现有警告信息
 [root@localhost ~]# cat /var/log/messages

在这里插入图片描述

4.并且本地用户不能删除,上传,下载,建立目录
5.当selinux的状态为Permissive时,可以访问外来文件但有警告
  [root@localhost ~]# getenforce 
    Enforcing
    ##更改selinux的为警告状态
    [root@localhost ~]# setenforce 0
    [root@localhost ~]# getenforce
    Permissive
    [root@localhost ~]# > /var/log/messages
    [root@localhost ~]# cat /var/log/messages
    [root@localhost ~]# lftp 172.25.254.234
    lftp 172.25.254.234:~> ls
    drwxr-xr-x    2 0        0               6 Mar 07  2014 pub
    -rw-r--r--    1 0        0               0 Nov 06 12:29 westosfile1
    -rw-r--r--    1 0        0               0 Nov 06 12:49 westosfile2
    lftp 172.25.254.234:/> exi
##查看日志,发现有警告信息    
[root@localhost ~]# cat /var/log/messages
6.文件的安全上下文
[root@localhost ~]# cd /var/ftp
##查看所有文件的安全上下文
[root@localhost ftp]# ls -Z
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub
-rw-r--r--. root root system_u:object_r:public_content_t:s0 westosfile1
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   westosfile2




[root@localhost mnt]# cd /var/ftp
##内核记录过的安全上下文
[root@localhost ftp]# semanage fcontext -l | grep /var/ftp

在这里插入图片描述

1.临时更改安全上下文

[root@localhost ftp]# chcon -t public_content_t westosfile2
##查看单个文件的安全上下文
[root@localhost ftp]# ls -Z westosfile2
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westosfile2


##此时即使selinux的状态为enforecing仍然可查看到外来文件
[root@localhost ftp]# getenforce 
Permissive
[root@localhost ftp]# setenforce 1
[root@localhost ftp]# getenforce 
Enforcing
[root@localhost ftp]# lftp 172.25.254.234
lftp 172.25.254.234:~> ls
-rw-r--r--    1 0        0               0 Nov 06 13:11 file1
drwxr-xr-x    2 0        0               6 Mar 07  2014 pub

在这里插入图片描述

##查看目录的安全上下文

[root@localhost ~]# ls -Z /linux -d
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /linux
##临时更改 /linux目录的安全上下文为public_content_t类型
[root@localhost ~]# chcon -t public_content_t /linux -R
[root@localhost ~]# ls -Z /linux -d
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /linux

##永久性更改安全上下文

[root@localhost ~]# semanage fcontext -a -t public_content_t '/linux(/.*)?'
[root@localhost ~]# restorecon -FvvR /linux/
[root@localhost ~]# ls -Zd /linux/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /linux/

测试:

##此时即使selinux的状态为强制模式,仍然可以看到家目录里的文件
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# lftp 172.25.254.234
lftp 172.25.254.234:~> ls
-rw-rw-r--    1 0        0               0 Nov 06 13:27 linuxfile
lftp 172.25.254.234:/> exit

3.管理linux布尔值

setenforce 0 #强制模式,有警告,但操作不会被阻止
setenforce 1 #禁止模式,有警告,且操作会被阻止
getsebool -a |grep ftp #查看目前系统上面ftp服务的所有布尔值条款
setsebool -P ftp_home_dir on #打开ftp的布尔值

selinux对程序的影响

当selinux处于强制模式时,本地用户将无法put上传文件

[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# lftp 172.25.254.234 -u westos
Password: 
lftp westos@172.25.254.234:~> ls       
drwxr-xr-x    2 1001     1001            6 Nov 06 12:35 linux
-rw-r--r--    1 0        0            2048 Nov 06 12:58 passwd
lftp westos@172.25.254.234:~> put /etc/group
put: Access failed: 553 Could not create file. (group)
lftp westos@172.25.254.234:~> exit

解决方案:

1)直接将selinux的状态更改为警告模式即可,但用在这种模式下上传文件不安全
2)直接打开ftp的布尔值

#查看ftp服务的布尔值条款
[root@localhost ~]# getsebool -a | grep ftp





##打开ftp家目录的布尔值
[root@localhost ~]# setsebool -P ftp_anon_write on
[root@localhost ~]# getsebool -a | grep ftp

测试:
设置组为ftp
同时更改权限使组满权限
在这里插入图片描述

注意:此实验需要/etc/vsftpd/vsftpd.conf
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值