一、Selinux的三种状态
1,Disable,禁用
2,permissive,不真正实施selinux功能,仅将违反策略的行为记录进audit.log
3,enforcing,强制实施selinux;
二、启用selinux
Selinux从disabled切换为permissive启用
打标:从禁用转换为启用,必须重新启动系统对每一个文件进行打标操作,为每一个文件赋予安全属性。打标属于内核级别的操作,属于必须重新启动系统。
1,Selinux的配置文件/etc/selinux/config--->/etc/sysconfig/selinux连接文件,属于同一个文件
修改配置文件/etc/selinux/config
SELINUX=permissive
重启系统即可
2,系统启动时通过grub传递内核参数:selinux=1或enforcing=1
Permissive----->enforcing
方法:setenforce 1启用selinux
Getenforce 查看selinux状态
三、命令
ls -Z 显示安全上下文
ps -Z 同上
Sebool : selinux bool 进程的功能开关
Getsebool
Setsebool
每一个subject能够访问哪些object,取决于策略库以及subject和object的安全属性,一个subject有很多功能,这些功能有些安全,有些危险,这些功能也需要监控。使用sebool。
# ls -Z
-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 install.log
解释,system_u----selinux的用户,object_r------selinux的role,admin_home_t----selinux的类型
四、在selinux启用的时,修改站点根目录,如何让Apache能够方位改变目录后的web首页
# setenforce 1
# cd /var/www/html/
# ls -lZ
-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html
可以看到,在目录/var/www/html/下系统自动为之下所创建的文件打标为httpd_sys_content类型,编辑index.html,将web的首页设置为该index.html文件,在浏览器中打开,可以显示。
而将web首页设置在另一个目录下边,如/web/htdocs下的index.html再打开浏览器,将无法访问里面的内容,我们看一下/web/htdocs下的index.html的类型
# ls -Z
-rw-r--r--. root root system_u:object_r:default_t:s0 index.html
可以看出,类型与默认的Apache可以访问的类型不一样,所以,启动了selinux后,Apache是无法访问这个类型的,它只能访问类型为httpd_sys_content_t的网页。
通过打标命令,修改文件的类型就可让Apache访问网页了。
打标命令
Chcon -t file 修改文件的类型/标记
Restorecon files 恢复文件的类型/标记
# chcon -t httpd_sys_content_t index.html
# ls -Z
-rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html
再访问网页,发现可以浏览了。
# restorecon index.html--------恢复文件的类型标记
# ll -Z
-rw-r--r--. root root system_u:object_r:default_t:s0 index.html
五、如何让暱名用户能够上传文件到ftp服务器?
1,修改ftp目录的访问权限
# cd /var/ftp/
# setfacl -m u:ftp:rwx pub
2,修改配置文件/etc/vsftp/vsftp.conf
anonymous_enable=YES-----------暱名登录ftp
anon_upload_enable=YES---------暱名上传
anon_mkdir_write_enable=YES------暱名创建目录
anon_other_write_enable=YES------暱名进行其他操作
3,登录ftp上传文件
# lftp localhost -----------登录ftp服务器
lftp localhost:/pub>cd pub ----------------------- 进入pub目录
lftp localhost:/pub>lcd /etc-------------------------进入文件系统的/etc目录,注意lcd命令,对于cd命令,这里使用lcd,其他命令可以使用!命令,如!Ls,显示的就是进入ftp前的当前目录。
lftp localhost:/pub> put shadow
868 bytes transferred
然而,开启了selinux后,这些功能形同样会失效。
这时,就需要用到getsebool和setsebool了
# getsebool -a | grep ftp--------------显示所有与ftp有关的功能开关
allow_ftpd_anon_write --> off-----------开启selinux后,默认暱名用户无写权限
allow_ftpd_full_access --> off-----------默认无访问权限
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> off
ftpd_connect_db --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
要想暱名用户能够上传,这时就需要启用这两个功能了。
# setsebool -P allow_ftpd_anon_write=1
# setsebool -P allow_ftpd_anon_write=on
以上两种任选一种。
-P选项,表示永久有效。
# setsebool -P allow_ftpd_full_access=1
这样,暱名用户就可以再次上传了。