关于linux:restorecon-命令使用实例

9次阅读

共计 3716 个字符,预计需要花费 10 分钟才能阅读完成。

当你将自定义文件增加到曾经由 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–. 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 命令应用实例

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

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

总结
restorecon 命令将文件和目录的 SELinux 平安上下文重置为默认值。这只会重置 SELinux 上下文的类型属性。

正文完
 0