当您将自定义文件添加到已经由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.htmlRelabeled/var/www/html/index.htmlfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s03。使用通配符处理下面的多个对象实例将修改目录下所有文件的安全上下文。[root@localhost~]#restorecon-v/var/www/html/*4。文件和目录的递归处理您还可以使用-R选项递归地重置文件的安全上下文。[root@localhost~]#restorecon-Rv/var/www/html/Relabeled/var/www/html/salesfromunconfined_u:object_r:admin_home_t:s0tounconfined_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:s05。根据输入的文件恢复上下文,可以在文件中保存需要恢复安全上下文的文件或文件夹路径,使用-f选项指定要恢复的文件。下面的/var/www/html/testdir目录和下面指定的文件需要恢复默认的安全上下文:首先创建一个文件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.txt下面使用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.txtfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_t:sys_contented/var/www/html/testdir/file2.txtfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file3.txtfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled//www/html/testdir/file4.txtfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file5.txtfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file6.txtfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file7.object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file8.txtfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file9.txtfromunconfined_u:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s0Relabeled/var/www/html/testdir/file10.txtfromunconfined_u:object_r:admin_home_t:s0tounconfined_u:object_r:httpd_sys_content_t:s06。排除某个目录,也可以使用security-e选项排除不需要的在下面的例子中,我们处理的是/var/www/html目录下的所有文件,不包括/var/www/html/sales中的文件子目录。[root@localhosthtml]#restorecon-e/var/www/html/sales-Rv/var/www/html也可以提供多个-e选项来排除多个文件或文件夹。总结restorecon命令将文件和目录的SELinux安全上下文重置为默认值。这只是重置了SELinux上下文的类型属性。本文转载自微信公众号《Linux就该这么学》,可以通过以下二维码关注。转载本文,请联系Linux像这样学习公众号。
