在 Debian 里,“设置 context”通常跟 SELinux 或 文件/进程的 security context 有关。不过需要先说明一点:
Debian 默认使用的是 AppArmor,而不是 SELinux
所以如果你说的“context”是指 SELinux context,在 Debian 上通常需要先手动启用 SELinux。
下面分几种常见情况说明。
sudo apt update
sudo apt install selinux-basics selinux-policy-default auditd
启用 SELinux:
sudo selinux-activate
sudo reboot
查看当前模式:
getenforce
# Enforcing / Permissive / Disabled
ls -Z /var/www/html
ps -Z
例如,把目录设为 httpd_sys_content_t:
sudo semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
sudo restorecon -Rv /web
临时修改(重启可能失效):
sudo chcon -t httpd_sys_content_t /web/index.html
runcon -t httpd_t /usr/sbin/apache2
Debian 默认用 AppArmor,不是 SELinux context。
查看状态:
sudo aa-status
启用/禁用 profile:
sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
sudo aa-complain /etc/apparmor.d/usr.sbin.nginx
比如 service 文件里的:
[Service]
User=www-data
SELinuxContext=system_u:system_r:httpd_t:s0
或:
NoNewPrivileges=true
ProtectSystem=strict
比如 SELinux 用户映射:
semanage login -a -s user_u username
| 需求 | Debian 推荐方式 |
|---|---|
| SELinux context | 手动安装 SELinux |
| 安全隔离 | 默认 AppArmor |
| systemd context | service 文件配置 |
| 容器 context | Docker / Podman label |
如果你能补充说明:
我可以给你更精确的操作命令。