良许Linux教程网 干货合集 详解restorecon命令使用方法

详解restorecon命令使用方法

Linux restorecon命令如何使用?restorecon命令主要用途就是用来恢复SELinux文件属性即恢复文件的安全上下文,下面为大家详细讲解一下restorecon命令使用方法。

详解restorecon命令使用方法

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的安全性上下文恢复为适当的值。 如下所示,它已将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--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 13 Jan  7 11:14 /var/www/html/index.html

2.更改安全上下文时输出信息

默认情况下,执行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
restorecon 命令使用实例restorecon 命令使用实例

3.使用通配符处理多个对象

下面实例将修改目录下面所有文件的安全上下文。

[root@localhost ~]# restorecon -v /var/www/html/*
restorecon 命令使用实例restorecon 命令使用实例
restorecon 命令使用实例restorecon 命令使用实例

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
restorecon 命令使用实例restorecon 命令使用实例
restorecon 命令使用实例restorecon 命令使用实例

5.根据输入文件还原上下文

可以将需要恢复安全上下文的文件或文件夹路径保存在文件里,使用-f选项指定文件,来恢复。在下面的/var/www/html/testdir目录和下面的指定的文件需要恢复默认安全上下文: restorecon 命令使用实例restorecon 命令使用实例 首先创建一个文件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 命令使用实例restorecon 命令使用实例 下面使用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
restorecon 命令使用实例restorecon 命令使用实例

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选项来排除多个文件或文件夹。

以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

img
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部