温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux的restorecon命令怎么使用

发布时间:2022-01-21 08:22:14 来源:亿速云 阅读:122 作者:iii 栏目:开发技术

本文小编为大家详细介绍“Linux的restorecon命令怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Linux的restorecon命令怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

Linux的restorecon命令怎么使用

Linux常用命令restorecon命令 用来恢复SELinux文件属性即恢复文件的安全上下文。

语法

 restorecon [-iFnrRv] [-e excludedir ] [-o filename ] [-f filename | pathname...]

选项

 -i:忽略不存在的文件。
 -f:infilename 文件 infilename 中记录要处理的文件。
 -e:directory 排除目录。
 -R/-r:递归处理目录。
 -n:不改变文件标签。
 -o/outfilename:保存文件列表到 outfilename,在文件不正确情况下。
 -v:将过程显示到屏幕上。
 -F:强制恢复文件安全语境。

实例

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

Linux的restorecon命令怎么使用

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

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

 [root@localhost ~]# restorecon -v /var/www/html/*

Linux的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

Linux的restorecon命令怎么使用

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

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

Linux的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

Linux的restorecon命令怎么使用

6.排除某个目录

还可以使用-e选项排除不需要恢复安全上下文的目录。 在以下示例中,我们正在处理/var/www/html 目录下的所有文件,但不包括/var/www/html/sales子目录中的文件。

 [root@localhost html]# restorecon -e /var/www/html/sales -Rv /var/www/html

Linux的restorecon命令怎么使用 

还可以提供多个-e选项来排除多个文件或文件夹。

读到这里,这篇“Linux的restorecon命令怎么使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI