前言(SELinux 的作用)
SELinux 主要作用就是最大限度地减小系统中服务进程可访问的资源(最小权限原则)。设想一下,
如果一个以 root 身份运行的网络服务存在 0day 漏洞,黑客就可以利用这个漏洞,以 root 的
身份在您的服务器上为所欲为了。是不是很可怕?SELinux 就是来解决这个问题的。
1.selinux的功能了解
(1)文件方面:程序的安全上下文和文件的安全上下文必须相匹配才会被允许
(2)服务的功能方面:用setsebool ,把程序的功能加了开关,并且把开关全部关掉
# selinux=disabled时,文件和程序的安全上下文都会被隐藏起来
[root@localhost Desktop]# cd /var/ftp/
[root@localhost ftp]# touch file
[root@localhost ftp]# ls -Z file
-rw-r--r-- root root ? file
[root@localhost ftp]# ps auxZ | grep ftp
- root 5915 0.0 0.0 52796 564 ? Ss 22:47 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
# selinux=enforcing时
[root@localhost ftp]# vim /etc/sysconfig/selinux
7 SELINUX=enforcing
[root@localhost ftp]# reboot
[root@localhost Desktop]# cd /var/ftp/
[root@localhost ftp]# touch file
[root@localhost ftp]# ls -Zd /var/ftp/
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/
[root@localhost ftp]# touch /mnt/file
[root@localhost ftp]# ls -Zd /mntdrwxr-xr-x. root root system_u:object_r:mnt_t:s0 /mnt
[root@localhost ftp]# ps auxZ | grep ftp

2.selinux的功能开关
setsebool # 功能开关
getenforce # 查看selinux的布尔值
enforcing / permissive # 开启
enforcing # 强制(拒绝+警告)
permissive # 警告(接受+警告)
两者之间的转换(不需要重启就可以生效)
setenforce 0 # 转换为警告模式,
setenforce 1 # 转换为强制模式
disabled # 关闭
# 把selinux设置成警告模式,虽然会有警告,但是动作依然会执行成功
[root@localhost ftp]# setenforce 0 # 把seliinux的功能开关设置为警告模式
[root@localhost ftp]# getenforce
Permissive
# 在客户端测试
[kiosk@foundation78 Desktop]$ lftp 172.25.254.138 -u westos
Password: lftp westos@172.25.254.138:~> ls
-rw------- 1 1001 1001 970 Oct 30 11:54 group
-rw-r--r-- 1 1001 1001 2281 Oct 30 11:52 passwd
-rw-r--r-- 1 0 0 0 Nov 03 16:18 westosfile
lftp westos@172.25.254.1