新聞中心
restorecon命令的作用是可以修復SELinux文件屬性即恢復文件的安全上下文。

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:申請域名、雅安服務器托管、營銷軟件、網(wǎng)站建設(shè)、和平網(wǎng)站維護、網(wǎng)站推廣。
在Selinux中,一切皆對象,由存放在節(jié)點的擴展屬性域的安全元素所控制其訪問。所有文件和端口資源和進程都具備安全標簽就是安全上下文。
1.恢復文件的SELinux上下文
在以下示例中,index.html文件在SELinux上下文中具有“user_home_t”上下文類型。這個上下文類型,apache服務將無法訪問。
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-rw-r--. root root unconfined_u:object_r:user_home_t:s0 13 Jan 7 11:14 /var/www/html/index.html
注意:上面的ls命令中的-Z選項將顯示特定文件的SELinux上下文。當我們使用restorecon命令時,我們實際上不需要知道文件的原始安全上下文。 restorecon會自動修正。
以下示例將把index.html的安全性上下文恢復為適當?shù)闹怠?如下所示,它已將SELinux上下文的類型重置為“httpd_sys_content_t”, 現(xiàn)在apache將能夠為該文件提供服務,而不會出現(xiàn)任何錯誤。
[root@localhost ~]# restorecon /var/www/html/index.html
[root@localhost ~]# ll -Z /var/www/html/index.html
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 13 Jan 7 11:14 /var/www/html/index.html
2.更改安全上下文時輸出信息
默認情況下,執(zhí)行restorecon命令時,它不會提示是否更改了文件的安全上下文。
[root@localhost ~]# restorecon -v /var/www/html/index.html
Relabeled /var/www/html/index.html from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
3.使用通配符處理多個對象
下面實例將修改目錄下面所有文件的安全上下文。
[root@localhost ~]# restorecon -v /var/www/html/*
4.遞歸處理文件和目錄
還可以使用-R選項遞歸重置文件的安全上下文。
[root@localhost ~]# restorecon -Rv /var/www/html/
Relabeled /var/www/html/sales from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/sales/graph.html from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
5.根據(jù)輸入文件還原上下文
可以將需要恢復安全上下文的文件或文件夾路徑保存在文件里,使用-f選項指定文件,來恢復。在下面的/var/www/html/testdir目錄和下面的指定的文件需要恢復默認安全上下文:
首先創(chuàng)建一個文件input.txt,在里面填入需要恢復默認安全上下文的目錄或者文件的完整路徑。
[root@localhost ~]# vim input.txt
[root@localhost ~]# cat input.txt
/var/www/html/testdir
/var/www/html/testdir/file1.txt
/var/www/html/testdir/file3.txt
/var/www/html/testdir/file5.txt
/var/www/html/testdir/file7.txt
/var/www/html/testdir/file9.txt
下面使用restorecon來恢復:
[root@localhost ~]# restorecon -Rvf input.txt
Relabeled /var/www/html/testdir from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file1.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file2.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file3.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file4.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file5.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file6.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file7.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file8.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file9.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /var/www/html/testdir/file10.txt from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
6.排除某個目錄
還可以使用-e選項排除不需要恢復安全上下文的目錄。 在以下示例中,我們正在處理/var/www/html 目錄下的所有文件,但不包括/var/www/html/sales子目錄中的文件。
[root@localhost html]# restorecon -e /var/www/html/sales -Rv /var/www/html
restorecon 命令使用實例restorecon 命令使用實例
還可以提供多個-e選項來排除多個文件或文件夾。
總結(jié)
restorecon命令將文件和目錄的SELinux安全上下文重置為默認值。這只會重置SELinux上下文的類型屬性。
標題名稱:restorecon命令使用實例
文章位置:http://www.5511xx.com/article/djeoopi.html


咨詢
建站咨詢
