当您将自定义文件添加到已由SELinux策略管理的目录时,如果自定义文件没有正确的SELinux上下文,那么您将不会获得预期的结果。restorerecon命令表示恢复SELinux上下文。restorecon命令将文件和目录的SELinux安全上下文重置为默认值。这只是重置了SELinux上下文的类型属性。1.恢复文件的SELinux上下文在下面的例子中,index.html文件在SELinux上下文中有一个“user_home_t”上下文类型。使用此上下文类型,将无法访问Apache服务。[root@localhost~]#ll-Z/var/www/html/index.html-rw-rw-r--。rootrootunconfined_u:object_r:user_home_t:s013Jan711:14/var/www/html/index.html注意:上面ls命令中的-Z选项将显示特定文件的SELinux上下文。当我们使用restorecon命令时,我们实际上不需要知道文件的原始安全上下文。restorecon会自动更正它。以下示例将index.html的安全上下文恢复为适当的值。如下所示,它已将SELinux上下文的类型重置为“httpd_sys_content_t”,现在apache将能够无错误地提供该文件。[root@localhost~]#restorecon/var/www/html/index.html[root@localhost~]#ll-Z/var/www/html/index.html-rw-r--r--。1rootrootunconfined_u:object_r:httpd_sys_content_t:s013Jan711:14/var/www/html/index.html2.更改安全上下文时输出信息默认情况下,restorecon命令执行时,不会提示是否文件的安全性已更改上下文。[root@localhost~]#restorecon-v/var/www/html/index.html将/var/www/html/index.html从unconfined_u:object_r:admin_home_t:s0重新标记为unconfined_u:object_r:httpd_sys_content_t:s0Restorecon命令使用示例Restorecon命令用法示例3.使用通配符处理多个对象以下示例将修改目录下所有文件的安全上下文。[root@localhost~]#restorecon-v/var/www/html/*restoreconcommanduseexamplerestoreconcommanduseexample4.递归处理文件和目录也可以使用-R选项递归重置文件的安全上下文.[root@localhost~]#restorecon-Rv/var/www/html/Relabeled/var/www/html/sales从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/sales/graph.htmlfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Restorecon命令使用示例Restorecon命令使用示例5.根据输入文件恢复上下文可以保存需要恢复的文件或文件夹路径文件中的安全上下文,使用-f选项指定要还原的文件。下面/var/www/html/testdir目录和下面指定的文件需要恢复默认安全上下文:restorecon命令使用示例restorecon命令使用示例首先创建一个文件input.txt,填写需要恢复默认安全上下文目录或文件的完整路径。[root@localhost~]#viminput.txt[root@localhost~]#catinput.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.txtrestorecon命令使用实例restorecon命令使用实例下面使用restorecon回复:[root@localhost~]#restorecon-Rvfinput.txtRelabeled/var/www/html/testdirfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file1.txt从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file2.txt从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file3.txt从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file4.txt从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file5.txt从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file6.txtfromunconfined_u:object:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file7.txt从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/8/testd.txt从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0/html/testdir/file10.txt从unconfined_u:object_r:admin_home_t:s0到unconfined_u:object_r:httpd_sys_content_t:s0Restorecon命令使用示例restorecon命令使用示例6.排除一个目录,也可以使用-e选项排除不需要恢复的安全上下文Directories下面的例子中,我们处理的是/var/www/html目录下的所有文件,排除文件在/var/www/html/sales子目录中。[root@localhosthtml]#restorecon-e/var/www/html/sales-Rv/var/www/htmlrestorecon命令使用示例restorecon命令使用示例还可以提供多个-e选项来排除多个文件或文件夹。总结restorecon命令将文件和目录的SELinux安全上下文重置为默认值。这只是重置了SELinux上下文的类型属性。
