linux系统中selinux的简介与用法

SELinux是Linux内核的强制访问控制系统,用于增强系统安全。它的工作模式包括Disabled、Enforcing和Permissive。安全上下文定义了访问控制属性,影响文件和服务的访问。通过`ls -Z`和`ps -Z`可查看上下文,`chcon`用于临时修改,`semanage`配合`restorecon`用于永久修改。在Enforcing模式下,设置bool值如`ftp_home_dir`为1可恢复某些功能。日志文件`/var/log/audit/audit.log`记录违反规则的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.SELINUX

定义:是linux中提供的内核强制访问控制系统,是Linux的内核模块,也是linux系统的一个安全子系统。
状态:Disabled ##不启动控制系统
Enforcing ##启动控制系统,处于强制状态。如果违反selinux的规则则操作被阻止并记录在日志中
permissive ##启动控制系统,处于警告模式。可以继续执行,但是会把违反的操作记录下来
SElinux工作模式可以在==/etc/selinux/config==中设定,如果想从disable切换到enforcing或者permissive,需要重启系统.反过来也一样,enforcing或者permissive模式可以通过setenforce 1|0命令快速切换。

二.安全上下文

1.安全上下文的定义及查看

(1)定义

安全上下文是一个简单的、一致的访问控制属性,主要体现在以下两点:

a.限制文件访问。文件必须拥有相匹配的安全上下文才能being识别访问。

b.对服务,限制认为不安全的服务功能, 设置开关。 开关默认关闭需要时可以打开。
(2)查看

ls -Z ##检查文件、目录的安全上下文;

ps -Z ##检查进程的安全上下文;

mv:安全上下文则不变;

cp:会重新生成安全上下文。

1、环境配置,删除以前的网络配置。将 /etc/vsftpd 删除

rm -rf /etc/vsftpd/
yum install vsftpd.x86_64 -y 重新下载一个
重启服务 systemctl restart vsftpd

[root@server ~]# rm -rf /etc/vsftpd/
[root@server ~]# yum install vsftpd.x86_64 -y
2.disable下文件的安全上下文
  • 查看selinux级别
[root@server ~]# getenforce 
Enforcing

新建文件

[root@server ~]# touch /mnt/file
[root@server ~]# lftp 192.168.100.200
lftp 192.168.100.200:~> ls
drwxr-xr-x    3 0        0              68 Jun 21  2018 pub
lftp 192.168.100.200:/> exit
[root@server ~]# mv /mnt/file /var/ftp    移动到该目录下也是可以查看的
[root@server ~]# lftp 192.168.100.200
lftp 192.168.100.200:~> ls
-rw-r--r--    1 0        0               0 May 03 04:20 file
drwxr-xr-x    3 0        0              68 Jun 21  2018 pub
lftp 192.168.100.200:/> exit
[root@server ~]# lftp 192.168.100.200 -u student       本地用户可写
Password: 
lftp student@192.168.100.200:~> ls      
lftp student@192.168.100.200:~> put /etc/passwd
2460 bytes transferred
lftp student@192.168.100.200:~> ls
-rw-r--r--    1 1001     1001         2460 May 03 04:22 passwd
lftp student@192.168.100.200:~> 

3.enforcing下文件的安全上下文
[root@server ~]# getenforce 
Enforcing
[root@server ~]# touch /mnt/file2
[root@server ~]# mv /mnt/file2  /var/ftp
[root@server ~]# ls -Z /var/ftp
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   file2                  安全上下文不一样
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub
[root@server ~]# lftp 192.168.100.200             打开selinux,会发现安全上下文不一样,也就登陆上去查看不到
lftp 192.168.100.200:~> ls
-rw-r--r--    1 0        0               0 May 03 04:20 file
drwxr-xr-x    3 0        0              68 Jun 21  2018 pub
lftp 192.168.100.200:/> exit

当SELINUX状态为enforcing时,查询上下文时显示上下文,但是SELINUX打开时重新配置了磁盘中的上下文,因此磁盘中原本存在的文件在重启时配置了上下文。新建立的文件没有配置上下文因此lftp看不到新建的文件。

SELINUX的bool值

bool值为0:off 开关关闭。功能无法使用
bool值为1:on 开关开启。功能可以使用
setsebool -P ftp_home_dir 1 ##设置波尔值为1;-P ##永久开启
getsebool -a | grep ftp ##查看ftp波尔值
当将SELINUX状态该为enforcing时,对于普通用户来讲,无法进行文件的上传下载删除等,则需要设置bool值为1来进行恢复

[root@localhost ~]# setsebool -P ftp_home_dir 1  ##设置波尔值为1;-P  ##永久开启
[root@localhost ~]# getsebool -a | grep ftp  ##查看ftp波尔值
ftp_home_dir --> on  ##波尔值为1则设置成功
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
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@localhost ~]# lftp 172.25.4.104 -u student
Password: 
lftp student@172.25.4.104:~> ls      
-rw-r--r--    1 1000     1000         2005 Apr 21 16:11 passwd
lftp student@172.25.4.104:~> put /etc/group
850 bytes transferred
lftp student@172.25.4.104:~> ls
-rw-r--r--    1 1000     1000          850 Apr 21 16:22 group
-rw-r--r--    1 1000     1000         2005 Apr 21 16:11 passwd  ##本的用户上传成功
lftp student@172.25.4.104:~> rm group  ##本地用户删除成功
rm ok, `group' removed
lftp student@172.25.4.104:~> ls
-rw-r--r--    1 1000     1000         2005 Apr 21 16:11 passwd
lftp student@172.25.4.104:~> exit

修改安全上下文对象
  • 暂时性的修改安全上下文
    chcon -t public_content_t /var/ftpdir/ -R 将其改变为public_content_t,保证上下文对象一致,就额可以对其操作的
    ls -Zd /var/ftpdir/ 再一次查看之后,上下文对象一致,都已经变为了 public_content_t
[root@server ~]# mkdir /var/ftpdir     
[root@server ~]# touch /var/ftpdir/file{1..10}
[root@server ~]# ls -l /var/ftpdir/
total 0
-rw-r--r--. 1 root root 0 May  3 13:26 file1
-rw-r--r--. 1 root root 0 May  3 13:26 file10
-rw-r--r--. 1 root root 0 May  3 13:26 file2
-rw-r--r--. 1 root root 0 May  3 13:26 file3
-rw-r--r--. 1 root root 0 May  3 13:26 file4
-rw-r--r--. 1 root root 0 May  3 13:26 file5
-rw-r--r--. 1 root root 0 May  3 13:26 file6
-rw-r--r--. 1 root root 0 May  3 13:26 file7
-rw-r--r--. 1 root root 0 May  3 13:26 file8
-rw-r--r--. 1 root root 0 May  3 13:26 file9
[root@server ~]# vim /etc/vsftpd/vsftpd.conf  
[root@server ~]# systemctl restart vsftpd
[root@server ~]#  ls -Zd  /var/ftpdir/
drwxr-xr-x. root root unconfined_u:object_r:var_t:s0   /var/ftpdir/
[root@server ~]# chcon -t public_content_t /var/ftpdir  -R     临时性的修改安全上下文 
[root@server ~]#  ls -Zd  /var/ftpdir/
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /var/ftpdir/

vim /etc/sysconfig/selinux 编辑文件内容,将其enforcing 强制级别改变为disabled 改为普通的,
重启

[root@server ~]# vim /etc/sysconfig/selinux 
You have new mail in /var/spool/mail/root
[root@server ~]# reboot

在这里插入图片描述

  • 永久性的更改安全上下文
    ls -Zd /var/ftpdir 查看该上下文的对象不是public_content_t 但是查看 ls -Zd /var/ftp 还是为public_content_t
    semanage fcontext -l |grep ftpdir 筛选有没有在/var/目录下的ftpdir 目录。/var/ftpdir 是不存在的
    semanage fcontext -a -t public_content_t “/var/ftpdir(/.*)?” 将ftpdir放置到/var 目录下的
    ls -Zd /var/ftpdir/ 上下文还不是public_content_t
    restorecon /var/ftpdir 刷新
    再次进行查看 ls -Zd /var/ftpdir/ 上下文就是public_content_t
[root@server ~]# getenforce 
Enforcing
[root@server ~]# ls -Zd /var/ftpdir/     查看/var/ftpdir  
drwxr-xr-x. root root unconfined_u:object_r:var_t:s0   /var/ftpdir/
[root@server ~]# ls -Zd  /var/ftp
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp
[root@server ~]# semanage fcontext -a -t public_content_t "/var/ftpdir(/.*)?"      永久更改安全上文
[root@server ~]# ls -Zd /var/ftpdir/
drwxr-xr-x. root root unconfined_u:object_r:var_t:s0   /var/ftpdir/
[root@server ~]# restorecon -RvvF  /var/ftpdir/
restorecon reset /var/ftpdir context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file1 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file2 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file3 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file4 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file5 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file6 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file7 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file8 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file9 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /var/ftpdir/file10 context unconfined_u:object_r:var_t:s0->system_u:object_r:public_content_t:s0
[root@server ~]# ls -Zd /var/ftpdir/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftpdir/

插件的查看

setlinux的日志的文件为 /var/log/audit/audit.log,此文件中只存放报错信息不提供解决方案/var/log/messages可以提供解决方案。只有安装了setroubleshoot才在massage里面看到具体的报错解决方案
整个实验的核心就是在强制级上,从一个目录上转移到另一个目录上去,lftp登陆查看是没有的,而且是一致拒绝的状态 日志才会有显示的。才可以知道是那个插件可以提供帮助

有setroubleshoot-server.x86_64
[root@localhost ~]# rpm -q setroubleshoot-server.x86_64
setroubleshoot-server-3.2.17-2.el7.x86_64
[root@localhost ~]# > /var/log/audit/audit.log 
[root@localhost ~]# cat /var/log/audit/audit.log 
[root@localhost ~]# > /var/log/messages
[root@localhost ~]# cat /var/log/messages
[root@localhost ~]# touch /mnt/file
[root@localhost ~]# mv /mnt/file /var/ftp
[root@localhost ~]# ls /var/ftp
file  pub  test1
[root@localhost ~]# lftp 172.25.4.104
lftp 172.25.4.104:~> ls
drwxr-xr-x    2 0        0               6 Jun 23  2016 pub
-rw-r--r--    1 0        0               0 Apr 21 18:10 test1
lftp 172.25.4.104:/> exit
[root@localhost ~]# cat /var/log/audit/audit.log   ##出现报错信息
type=AVC msg=audit(1555870599.776:396): avc:  denied  { getattr } for  pid=2204 comm="vsftpd" path="/file" dev="vda1" ino=8845805 scontext=system_u:system_r:ftpd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:mnt_t:s0 tclass=file
type=SYSCALL msg=audit(1555870599.776:396): arch=c000003e syscall=6 success=no exit=-13 a0=7f58a2d65790 a1=7f58a2d657b0 a2=7f58a2d657b0 a3=2020202020203020 items=0 ppid=2202 pid=2204 auid=4294967295 uid=14 gid=50 euid=14 suid=14 fsuid=14 egid=50 sgid=50 fsgid=50 tty=(none) ses=4294967295 comm="vsftpd" exe="/usr/sbin/vsftpd" subj=system_u:system_r:ftpd_t:s0-s0:c0.c1023 key=(null)
type=SERVICE_STOP msg=audit(1555870741.566:397): pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg=' comm="systemd-localed" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
type=SERVICE_STOP msg=audit(1555870747.380:398): pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 msg=' comm="systemd-hostnamed" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
[root@localhost ~]# cat /var/log/messages  ##出现报错并提出解决方法

  • 没有 setroubleshoot-server.x86_64
[root@localhost ~]# yum remove setroubleshoot-server.x86_64  ##将其卸载
[root@localhost ~]# > /var/log/audit/audit.log  ##再次清空
[root@localhost ~]# cat /var/log/audit/audit.log 
[root@localhost ~]# > /var/log/messages
[root@localhost ~]# cat /var/log/messages
[root@localhost ~]# lftp 172.25.4.104
lftp 172.25.4.104:~> ls
drwxr-xr-x    2 0        0               6 Jun 23  2016 pub
-rw-r--r--    1 0        0               0 Apr 21 18:10 test1
lftp 172.25.4.104:/> exit
[root@localhost ~]# cat /var/log/audit/audit.log  ##仍旧出现报错日志
type=AVC msg=audit(1555871079.338:407): avc:  denied  { getattr } for  pid=2539 comm="vsftpd" path="/file" dev="vda1" ino=8845805 scontext=system_u:system_r:ftpd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:mnt_t:s0 tclass=file
type=SYSCALL msg=audit(1555871079.338:407): arch=c000003e syscall=6 success=no exit=-13 a0=7f58a2d65790 a1=7f58a2d657b0 a2=7f58a2d657b0 a3=2020202020203020 items=0 ppid=2537 pid=2539 auid=4294967295 uid=14 gid=50 euid=14 suid=14 fsuid=14 egid=50 sgid=50 fsgid=50 tty=(none) ses=4294967295 comm="vsftpd" exe="/usr/sbin/vsftpd" subj=system_u:system_r:ftpd_t:s0-s0:c0.c1023 key=(null)
[root@localhost ~]# cat /var/log/messages  ##无报错日志及解决日志产生

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值