自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何解決SELinux問題?

運維 系統(tǒng)運維
說起SELinux,多數(shù)Linux發(fā)行版缺省都激活了它,可見它對系統(tǒng)安全的重要性,可惜由于它本身有一定的復雜性,如果不熟悉的話往往會產(chǎn)生一些看似莫名其妙的問題,導致人們常常放棄使用它,為了不因噎廢食,學學如何解決SELinux問題是很有必要的。

說起SELinux,多數(shù)Linux發(fā)行版缺省都激活了它,可見它對系統(tǒng)安全的重要性,可惜由于它本身有一定的復雜性,如果不熟悉的話往往會產(chǎn)生一些看似莫名其妙的問題,導致人們常常放棄使用它,為了不因噎廢食,學學如何解決SELinux問題是很有必要的。

我們以CentOS環(huán)境為例重現(xiàn)一個非常常見的SELinux問題:

首先需要確認SELinux處于激活狀態(tài),可以使用getenforce或sestatus命令:

  1. shell> getenforce  
  2. Enforcing  
  3.  
  4. shell> sestatus  
  5. SELinux status:                 enabled  
  6. SELinuxfs mount:                /selinux  
  7. Current mode:                   enforcing  
  8. Mode from config file:          enforcing  
  9. Policy version:                 24  
  10. Policy from config file:        targeted 

注:關(guān)于SELinux的基礎(chǔ)知識介紹請參考鳥哥的Linux私房菜中相關(guān)的介紹。

我們還需要確認系統(tǒng)已經(jīng)安裝并啟動了Apache,沒有的話就YUM裝一個,這很簡單,就不多說了,接著在root目錄創(chuàng)建一個測試文件test.html,如下:

  1. shell> cat /root/test.html  
  2. hello, world. 

然后把這個測試文件拷貝到Apache的DocumentRoot目錄,我的Apache是通過YUM安裝的話,缺省是/var/www/html目錄,如下:

  1. shell> cp /root/test.html /var/www/html 

接著瀏覽一下,如果沒出什么幺蛾子,應(yīng)該一切都在意料之中,如下:

  1. shell> curl http://localhost/test.html  
  2. hello, world. 

看到這,你可能覺得我廢話連篇,別著急,下面就是見證奇跡的時候了:

同樣還是那個測試文件test.html,不過這次不再是拷貝,而是移動,如下:

  1. shell> mv /root/test.html /var/www/html 

接著瀏覽一下,怎么樣,結(jié)果很出人意料吧,竟然提示權(quán)限錯誤,如下:

  1. shell> curl http://localhost/test.html  
  2. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
  3. <html><head> 
  4. <title>403 Forbidden</title> 
  5. </head><body> 
  6. <h1>Forbidden</h1> 
  7. <p>You don't have permission to access /test.html  
  8. on this server.</p> 
  9. </body></html> 

當然,我們現(xiàn)在知道這個問題是由于SELinux引起的,但還不知其所以然,實際上問題的原因此時已經(jīng)被audit進程記錄到了相應(yīng)的日志里,可以這樣查看:

  1. shell> audit2why < /var/log/audit/audit.log 

如果看不懂的話,推薦安裝setroubleshoot套件:

  1. shell> yum install setroubleshoot 

它本身是一個GUI套件,不過其中包含的一個sealert命令對我們命令行用戶很有用:

  1. shell> sealert -a /var/log/audit/audit.log  
  2. Summary:  
  3.  
  4. SELinux is preventing /usr/sbin/httpd "getattr" access to  
  5. /var/www/html/test.html.  
  6.  
  7. Detailed Description:  
  8.  
  9. SELinux denied access requested by httpd. /var/www/html/test.html may be a  
  10. mislabeled. /var/www/html/test.html default SELinux type is httpd_sys_content_t,  
  11. but its current type is admin_home_t. Changing this file back to the default  
  12. type, may fix your problem.  
  13.  
  14. File contexts can be assigned to a file in the following ways.  
  15.  
  16.   * Files created in a directory receive the file context of the parent  
  17.     directory by default.  
  18.   * The SELinux policy might override the default label inherited from the  
  19.     parent directory by specifying a process running in context A which creates  
  20.     a file in a directory labeled B will instead create the file with label C.  
  21.     An example of this would be the dhcp client running with the dhclient_t type  
  22.     and creating a file in the directory /etc. This file would normally receive  
  23.     the etc_t type due to parental inheritance but instead the file is labeled  
  24.     with the net_conf_t type because the SELinux policy specifies this.  
  25.   * Users can change the file context on a file using tools such as chcon, or  
  26.     restorecon.  
  27.  
  28. This file could have been mislabeled either by user error, or if an normally  
  29. confined application was run under the wrong domain.  
  30.  
  31. However, this might also indicate a bug in SELinux because the file should not  
  32. have been labeled with this type.  
  33.  
  34. If you believe this is a bug, please file a bug report against this package.  
  35.  
  36. Allowing Access:  
  37.  
  38. You can restore the default system context to this file by executing the  
  39. restorecon command. restorecon '/var/www/html/test.html', if this file is a  
  40. directory, you can recursively restore using restorecon -R  
  41. '/var/www/html/test.html'.  
  42.  
  43. Fix Command:  
  44.  
  45. /sbin/restorecon '/var/www/html/test.html' 

這次應(yīng)該看懂了吧!原因是說Apache下文件上下文類型應(yīng)該是httpd_sys_content_t,但是現(xiàn)在是admin_home_t,所以權(quán)限錯誤,并且在結(jié)尾處給出了修復命令。

可httpd_sys_content_t,admin_home_t都怎么看???很簡單,借助ls命令的-Z參數(shù)即可:

  1. shell> ls -Z /path 

回到問題的開始,拷貝之所以沒出現(xiàn)問題,是因為cp自動修改上下文屬性,而移動之所以出現(xiàn)問題是因為mv保留原文件的上下文屬性。

注:關(guān)于SELinux和Apache的詳細介紹,可以參考『man httpd_selinux』。

知道了如何解決SELinux問題,以后如果遇到類似的情況不要急著武斷的關(guān)閉SELinux。

責任編輯:黃丹 來源: huoding.com
相關(guān)推薦

2013-04-22 14:00:21

SELinux

2017-10-17 09:21:06

2023-10-30 18:35:47

MySQL主從延時

2011-08-29 10:34:00

網(wǎng)絡(luò)安全云安全云計算

2011-03-23 14:42:47

CPU過度消耗

2021-06-06 13:05:15

前端跨域CORS

2010-07-16 13:52:26

telnet漏洞

2010-04-29 17:46:31

Oracle死鎖

2019-11-26 14:30:20

Spring循環(huán)依賴Java

2010-03-24 09:25:36

Nginx配置

2010-08-25 13:06:53

IP地址故障

2020-04-24 16:01:26

物聯(lián)網(wǎng)數(shù)據(jù)IOT

2023-03-27 11:33:37

人工智能物聯(lián)網(wǎng)

2010-03-18 14:54:57

Java SynDem

2020-06-29 15:03:34

遠程工作網(wǎng)絡(luò)安全網(wǎng)絡(luò)攻擊

2009-06-30 15:22:55

JSP頁面

2024-02-21 14:35:38

區(qū)塊鏈智慧城市數(shù)字化身份證

2023-07-18 16:05:00

IP地址

2017-08-02 15:51:24

PHP中文亂碼

2009-02-05 10:33:27

設(shè)備碎片Java ME移動
點贊
收藏

51CTO技術(shù)棧公眾號