聊聊Restorecon 命令使用实例

系统 Linux
当你将自定义文件添加到已经由SELinux策略管理的目录中时,如果自定义文件没有适当的SELinux上下文,那么将不会得到预期的结果。restorerecon命令的意思是恢复SELinux上下文。

[[404011]]

当你将自定义文件添加到已经由SELinux策略管理的目录中时,如果自定义文件没有适当的SELinux上下文,那么将不会得到预期的结果。restorerecon命令的意思是恢复SELinux上下文。restorecon命令将文件和目录的SELinux安全上下文重置为默认值。这只会重置SELinux上下文的类型属性。

1. 恢复文件的SELinux上下文

在以下示例中,index.html文件在SELinux上下文中具有“user_home_t”上下文类型。这个上下文类型,apache服务将无法访问。

  1. [root@localhost ~]# ll -Z /var/www/html/index.html  
  2. -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将能够为该文件提供服务,而不会出现任何错误。

  1. [root@localhost ~]# restorecon /var/www/html/index.html  
  2. [root@localhost ~]# ll -Z /var/www/html/index.html  
  3. -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命令时,它不会提示是否更改了文件的安全上下文。

  1. [root@localhost ~]# restorecon -v /var/www/html/index.html  
  2. 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 

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

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

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

4. 递归处理文件和目录

还可以使用-R选项递归重置文件的安全上下文。

  1. [root@localhost ~]# restorecon -Rv /var/www/html/ 
  2. Relabeled /var/www/html/sales from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0 
  3. 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 

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

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

首先创建一个文件input.txt,在里面填入需要恢复默认安全上下文的目录或者文件的完整路径。

  1. [root@localhost ~]# vim input.txt  
  2. [root@localhost ~]# cat input.txt  
  3. /var/www/html/testdir 
  4. /var/www/html/testdir/file1.txt 
  5. /var/www/html/testdir/file3.txt 
  6. /var/www/html/testdir/file5.txt 
  7. /var/www/html/testdir/file7.txt 
  8. /var/www/html/testdir/file9.txt 

下面使用restorecon来恢复:

  1. [root@localhost ~]# restorecon -Rvf input.txt  
  2. Relabeled /var/www/html/testdir from unconfined_u:object_r:admin_home_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0 
  3. 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 
  4. 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 
  5. 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 
  6. 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 
  7. 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 
  8. 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 
  9. 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 
  10. 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 
  11. 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 
  12. 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 

6. 排除某个目录

还可以使用-e选项排除不需要恢复安全上下文的目录。

在以下示例中,我们正在处理/var/www/html 目录下的所有文件,但不包括/var/www/html/sales子目录中的文件。

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

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

总 结

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

本文转载自微信公众号「Linux就该这么学」,可以通过以下二维码关注。转载本文请联系Linux就该这么学公众号。

 

责任编辑:武晓燕 来源: Linux就该这么学
相关推荐

2022-04-11 07:50:58

top 命令Java 系统JVM 进程

2010-10-26 10:02:05

oracle备份命令

2021-08-12 18:49:41

DataStreamAPI注册

2021-02-11 08:27:28

数据

2021-05-31 15:53:57

CPU Top命令

2010-06-22 13:23:18

Linux at命令详

2022-02-08 12:19:36

LinuxJQ命令

2021-01-04 05:43:59

LinuxBasename命令

2021-08-10 11:45:57

topCPULinux

2022-02-15 07:26:31

Ncat工具Linux

2022-02-22 08:00:48

JavaNIOBuffer

2023-09-27 07:33:48

Docker命令容器

2022-07-12 08:00:31

命令Kubernetes应用程序

2022-01-19 22:14:36

Apache APIAPI 网关插件

2021-11-29 10:24:56

WasmEnvoy 负载均衡

2024-03-06 11:38:12

Appwrite方式Supabase

2022-03-11 20:46:01

机制命令kerberos

2010-06-22 10:28:04

linux at命令

2015-07-21 12:43:58

Dockerfile命令实例

2021-02-07 23:58:10

单例模式对象
点赞
收藏

51CTO技术栈公众号