page counter next up previous contents
Next: Httpd SELinux Port Up: SELinux 管理 Confined 服務 Previous: 前言   Contents   DYWANG_HOME

Httpd SELinux Context

  1. 安裝套件。
    [root@kvm7 ~]# yum install httpd -y
    
  2. 啟動 httpd 服務,並設定開機啟動。
    [root@kvm7 ~]# systemctl enable httpd.service 
    [root@kvm7 ~]# systemctl start httpd
    
  3. 查看 httpd 服務的 context 的 type 是 httpd_t,運作在 httpd_t domain。
    [root@kvm5 ~]# ps -eZ | grep httpd
    system_u:system_r:httpd_t:s0    10803 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    10804 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    10805 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    10806 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    10807 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    10808 ?        00:00:00 httpd
    
  4. 切換工作目錄到網頁的根目錄 /var/www/html。
    [root@kvm7 ~]# cd /var/www/html/
    
  5. cal 印出月曆,分別存於 root 家目錄及 /var/www/html 目錄。
    [root@kvm7 html]# cal > ~/index.html
    [root@kvm7 html]# cal > index1.html
    
  6. 將 root 家目錄上的 index.html 移到 /var/www/html 目錄。
    [root@kvm7 html]# mv ~/index.html .
    
  7. 查看 /var/www/html 目錄上的 index.html 及 index1.html 的 SELinux contexts,index.html 是由 root 家目錄移過去的,所以是 admin_home_t
    [root@kvm7 html]# ls -Z index*.html
    -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index1.html
    -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html
    
  8. index1.html 的 SELinux context 是 httpd_sys_content_t,所以網頁訪問成功,列出月曆。
    [root@kvm7 html]# curl http://127.0.0.1/index1.html
        October 2019    
    Su Mo Tu We Th Fr Sa
           1  2  3  4  5
     6  7  8  9 10 11 12
    13 14 15 16 17 18 19
    20 21 22 23 24 25 26
    27 28 29 30 31
    
  9. index.html 的 SELinux context 是 admin_home_t,所以出現没有權限存取的訊息,網頁無法訪問。
    [root@kvm7 html]# curl http://127.0.0.1/index.html
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>403 Forbidden</title>
    </head><body>
    <h1>Forbidden</h1>
    <p>You don't have permission to access /index.html
    on this server.</p>
    </body></html>
    
  10. restorecon 將 /var/www/html/ 目錄下的 index.html 恢復到預設的 SELinux context httpd_sys_content_t
    [root@kvm7 html]# restorecon index.html
    [root@kvm7 html]# ls -Z index*.html
    -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index1.html
    -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
    
  11. 再次連線網頁 index.html,已可成功列出月曆。
    [root@kvm7 html]# curl http://127.0.0.1/index.html
        October 2019    
    Su Mo Tu We Th Fr Sa
           1  2  3  4  5
     6  7  8  9 10 11 12
    13 14 15 16 17 18 19
    20 21 22 23 24 25 26
    27 28 29 30 31
    



De-Yu Wang 2020-05-14