getenforce ##查看狀態
setenforce 0|1 ##0:permissive警告模式 1:enforcing強制模式
enforcing ##強制模式
permissive ##警告模式
disabled ##關閉模式
ls -Zd /dream ##查看目錄的安全上下文
ls -Z /dream/aa ##查看文件的安全上下文
當selinux爲disable狀態時ls -Z會看到一個問號 我們將selinux的狀態改爲enforce強制狀態重啓機器再看
臨時修改安全上下文
過程:新建一個文件並修改安全上下文,把其他目錄下的文件移過來刷新目錄發現移過來的的文件的安全上下文並沒有改變
[root@localhost dream]# ls -Zd /var/ftp/pub/
drwxrwxr-x. root ftp system_u:object_r:public_content_t:s0 /var/ftp/pub/
[root@localhost dream]# mkdir /westos
[root@localhost dream]# ls -Zd /westos
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos
[root@localhost dream]# chcon -t public_content_t /westos ##改變westos的安全上下文
[root@localhost dream]# ls -Zd /westos
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /westos
[root@localhost dream]# touch /mnt/file1
[root@localhost dream]# ls -Z /mnt/file1
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0 /mnt/file1
[root@localhost dream]# mv /mnt/file1 /westos
[root@localhost dream]# restorecon -RvvF ##刷新安全上下文 VV顯示過程
[root@localhost dream]# ls -Z /westos
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0 file1
永久修改安全上下文
[root@localhost dream]# semanage fcontext -l |grep /var/ftp/
##可以知道目錄安全上下文的寫法 /var/ftp/bin(/.*)?
[root@localhost dream]# semanage fcontext -a -t public_content_t '/westos'
##永久更改安全上下文
[root@localhost dream]# restorecon -RvvF /westos/
restorecon reset /westos context unconfined_u:object_r:public_content_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file1 context unconfined_u:object_r:mnt_t:s0->system_u:object_r:default_t:s0
[root@localhost dream]# restorecon -RvvF ##刷新安全上下文,臨時修改刷新後不生效
[root@localhost dream]# ls -Z /westos/ ##可以發現從其他地方來的文件安全上下文也和目錄一致拉
-rw-r--r--. root root system_u:object_r:default_t:s0 file1
在enforcing的條件下匿名用戶ftp上傳文件
1.首先要給默認發佈目錄的ftp組和權限
chgrp ftp /var/ftp/pub/
chmod 775 /var/ftp/pub/
2.查看並修改布爾值
getsebool -a|grep ftp
setsebool -P ftpd_anon_write on
3.將默認發佈目錄改爲讀寫
chcon -t public_content_rw_t /var/ftp/pub/
當出現錯誤時我們可以通過查看系統日誌找出問題
如果系統下安裝了setroubleshoot-sercver軟件時系統日誌會報錯並指出解決方案
可以考慮
rpm -qa |grep setrouble
yum remove setroubleshoot-server-3.2.17-2.el7.x86_64 ###卸載內核分析軟件
cat /var/log/message
當沒有安裝時這裏會顯示出錯信息
cat /var/log/audit/audit.log
改變服務端口
vim /etc/httpd/conf/httpd.conf
41 #Listen 12.34.56.78:80
42 Listen 6666
[root@dream /]# systemctl restart httpd ###發現服務起不來,看日誌報錯
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
semanage port -l | grep http ###發現端口6666不再使用範圍中
semanage port -a -t http_cache_port_t -p tcp 6666
systemctl restart httpd ###發現可以啓動