如何去除 SELinux 标签?

m.s*_*.s. 5 linux fedora permissions selinux

我将 SELinux 标签添加svirt_sandbox_file_t/home

chcon -Rt svirt_sandbox_file_t /home
Run Code Online (Sandbox Code Playgroud)

标签显示使用:

[user@localhost ~]$ ls -Z
unconfined_u:object_r:svirt_sandbox_file_t:s0 Desktop
unconfined_u:object_r:svirt_sandbox_file_t:s0 Documents
...
Run Code Online (Sandbox Code Playgroud)

我怎样才能svirt_sandbox_file_t再次去除标签?

我尝试重新启动,我添加了一个/home/.autorelabel来触发重新标记,但标签不会消失。我正在使用 Fedora 23。

Gra*_*avy 4

我相信如果你设置/etc/selinux/config重新disabled启动。然后将其设置为enforcing并重新启动,如果您在重新标记时遇到困难,它将重新标记。奇怪的是,restorecon 不起作用。

如果你想以困难的方式重置一切, /home 目录本身应该是:

system_u:object_r:home_root_t
Run Code Online (Sandbox Code Playgroud)

每个用户主目录(及其中的文件)应该是:

unconfined_u:object_r:user_home_dir_t:s0
Run Code Online (Sandbox Code Playgroud)

您可以使用命令或使用和chcon的组合来设置它们semanagerestorecon

chcon -t home_root_t /home
chcon -Rt user_home_dir_t /home/*
Run Code Online (Sandbox Code Playgroud)

或者

 semanage fcontext -a -t home_root_t /home
 semanage fcontext -a -t user_home_dir_t /home/*
 restorecon -R /home
Run Code Online (Sandbox Code Playgroud)

请注意,一般来说, chcon用于强制立即更改,同时保留默认值,以便restorecon将其恢复到默认上下文。就您而言,由于某种原因,这似乎出了问题。

通常semanage fcontext旨在将本地上下文文件写入/etc/selinux/targeted/contexts/files/file_contexts.local

有关当前上下文和默认上下文的大量信息可以在以下位置找到:

/etc/selinux/targeted/contexts/default_contexts
/etc/selinux/targeted/contexts/files/file_contexts
/etc/selinux/targeted/contexts/files/file_contexts.homedirs
Run Code Online (Sandbox Code Playgroud)

这些文件可能以某种方式被损坏,总体而言,有许多子上下文可能无法通过上述操作完全恢复,具体取决于这些文件的修改方式。检查这些文件并查看是否可以找到添加的上下文映射并以这种方式将其删除可能是个好主意。

理论上,您还可以使用虚拟机或另一台机器(或者可能只是在网上找到它们)并将已知的良好默认值复制到其正确的目录中,并允许系统重新标记以获得正确的默认值。但这也有一些缺点。

归根结底,需要进行一些试验和错误,上面列出的 chcon/semanage 命令应该为您提供大致的思路,但您的某些子目录可能会有自己的上下文。

一些可能有用的附加上下文(所有这些都在 /home/username 中:

ls -laZ /home/username

##context###########################  Directory##
unconfined_u:object_r:cache_home_t:s0 .cache
unconfined_u:object_r:config_home_t:s0 .config
unconfined_u:object_r:dbus_home_t:s0 .dbus
unconfined_u:object_r:gconf_home_t:s0 .gconf
unconfined_u:object_r:gconf_home_t:s0 .gconfd
unconfined_u:object_r:gpg_secret_t:s0 .gnupg
unconfined_u:object_r:gconf_home_t:s0 .local
unconfined_u:object_r:ssh_home_t:s0 .ssh
Run Code Online (Sandbox Code Playgroud)

请注意,这是基于我的主目录,您可能需要寻找更多内容,但如果您获得了大部分正确的内容,您应该或多或少回到正轨。