欢迎来到之路教程(on itroad-com)

使用 chcon 命令的示例

  1. 更改网页目录的类型
# chcon -R -t httpd_sys_content_t /web/
  1. 要使用参考文件更改安全上下文:
# chcon --reference=/tmp/file2 /tmp/file2
  1. 递归设置文件的安全上下文:
# chcon -R httpd_sys_content_t /web/
  1. 要更改文件用户安全上下文:
# chcon -u mike_u /file
  1. 更改文件角色安全上下文:
# chcon -u object_r /file
  1. 要更改文件类型安全上下文:
# chcon -u admin_home_t /file
  1. 更改文件级安全上下文:
# chcon -u s0 /file
Linux 中的 chcon 命令示例

chcon 命令有助于更改 SELinux 上下文或者通常是单个或者有时可能是几个文件的 SELinux 上下文或者类型,这些文件可以通过某种形式的通配符轻松引用。
chcon 连同 semanage 和 restorecon 可用于修复不正确的 SELinux 上下文。
我们可以通过两种方式使用 chcon 命令来修复或者更改 SELinux 上下文。

手动指定正确上下文的方法

这样,我们可以使用 -t 选项来更改文件的上下文。
不推荐这种修改文件上下文的方法,因为在使用它时可能会发生错误。

# chcon -t httpd_sys_content_t index.html

我们可以在“ls -Z”输出中看到命令成功。

# ls -Z
-rw-rw-r--. apacheuser apacheuser unconfined_u:object_r:httpd_sys_content_t:s0 index.html

具有适当上下文的参考文件

使用 chcon 的另一种方法是引用具有正确上下文的文件。

# chcon --reference some_file.html index.html

因此,我使用了 -reference 选项并指定了我想用作参考的文件。
我想更改的文件列在命令的末尾。

日期:2020-09-17 00:14:27 来源:oir作者:oir