偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

如何解決SELinux問題?

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

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

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

首先需要確認(rèn)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ǔ)知識(shí)介紹請(qǐng)參考鳥哥的Linux私房菜中相關(guān)的介紹。

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

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

然后把這個(gè)測試文件拷貝到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. 

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

同樣還是那個(gè)測試文件test.html,不過這次不再是拷貝,而是移動(dòng),如下:

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

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

  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> 

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

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

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

  1. shell> yum install setroubleshoot 

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

  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)限錯(cuò)誤,并且在結(jié)尾處給出了修復(fù)命令。

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

  1. shell> ls -Z /path 

回到問題的開始,拷貝之所以沒出現(xiàn)問題,是因?yàn)閏p自動(dòng)修改上下文屬性,而移動(dòng)之所以出現(xiàn)問題是因?yàn)閙v保留原文件的上下文屬性。

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

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

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

2013-04-22 14:00:21

SELinux

2010-04-29 17:46:31

Oracle死鎖

2017-07-20 07:30:16

大數(shù)據(jù)數(shù)據(jù)互聯(lián)網(wǎng)

2013-05-21 10:49:59

Windows硬件沖突

2024-10-29 16:41:24

SpringBoot跨域Java

2021-06-06 13:05:15

前端跨域CORS

2017-10-17 09:21:06

2011-08-29 10:34:00

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

2011-03-23 14:42:47

CPU過度消耗

2023-10-30 18:35:47

MySQL主從延時(shí)

2010-07-16 13:52:26

telnet漏洞

2024-11-21 16:47:55

2009-09-21 17:10:14

struts Hibe

2021-10-20 20:27:55

MySQL死鎖并發(fā)

2011-09-05 13:32:56

2019-07-30 08:28:44

VirtualBox橋接網(wǎng)絡(luò)

2025-02-11 12:29:58

2023-05-25 08:00:36

阿?云DNS重試機(jī)制

2024-02-21 14:35:38

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

2010-03-18 14:54:57

Java SynDem
點(diǎn)贊
收藏

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