全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-21 6 分钟 ✍️ juanwangdev

AppArmor/Seccomp 配置

AppArmor 和 Seccomp 是 Linux 容器安全的重要机制,下面介绍配置方法。

Seccomp 配置

什么是 Seccomp

Seccomp (Secure Computing) 限制容器可使用的系统调用。

Docker 默认配置

Bash
# Docker 默认启用 Seccomp
docker info | grep "Security"

# 输出
Security Options:
  seccomp
   Profile: default

自定义 Seccomp

JSON
// seccomp-profile.json
{
  "defaultAction": "SCMP_ACT_ERRNO",
  "architectures": [
    "SCMP_ARCH_X86_64",
    "SCMP_ARCH_X86",
    "SCMP_ARCH_X32"
  ],
  "syscalls": [
    {
      "names": [
        "accept",
        "accept4",
        "access",
        "alarm",
        "bind"
      ],
      "action": "SCMP_ACT_ALLOW"
    }
  ]
}
Bash
# 使用自定义配置
docker run -d \
  --security-opt seccomp=/path/to/seccomp-profile.json \
  my-app

禁用 Seccomp

Bash
# 不推荐,仅用于调试
docker run -d \
  --security-opt seccomp=unconfined \
  my-app

AppArmor 配置

什么是 AppArmor

AppArmor 限制进程可访问的文件、网络、能力等。

使用 AppArmor

Bash
# 使用自定义配置
docker run -d \
  --security-opt apparmor=docker-default \
  my-app

# 使用自定义配置
docker run -d \
  --security-opt apparmor=my-custom-profile \
  my-app

AppArmor 配置示例

Bash
#include <tunables/global>

profile docker-custom flags=(attach_disconnected,mediate_deleted) {
  #include <abstractions/base>

  # 允许访问
  /usr/bin/my-app ixr,
  /var/lib/my-app/** rw,

  # 拒绝访问
  deny /etc/shadow w,
  deny /proc/** w,
}

安全选项对比

安全机制作用配置方式推荐度
Seccomp限制系统调用JSON 配置✅ 必须
AppArmor限制资源访问Profile 配置✅ 推荐
NoNewPrivileges禁止提权布尔值✅ 推荐

NoNewPrivileges

Bash
# 禁止容器内提权
docker run -d \
  --security-opt no-new-privileges=true \
  my-app

# 容器内无法使用 sudo/ su 提权

综合安全配置

Bash
# 完整安全配置
docker run -d \
  --security-opt seccomp=/path/to/seccomp.json \
  --security-opt apparmor=docker-custom \
  --security-opt no-new-privileges=true \
  --read-only \
  --cap-drop ALL \
  --cap-add NET_BIND_SERVICE \
  my-app

查看安全配置

text
# 查看容器安全选项
docker inspect my-app | grep -A 10 "SecurityOpt"

# 输出
"SecurityOpt": [
    "seccomp=/path/to/seccomp.json",
    "apparmor=docker-custom",
    "no-new-privileges=true"
]

要点总结

  • Seccomp 限制系统调用,Docker 默认启用
  • AppArmor 限制进程资源访问,提供 Profile 配置
  • NoNewPrivileges 禁止容器内提权(sudo/su)
  • 生产环境应配置安全选项,增强容器隔离
  • 使用 --cap-drop ALL 移除所有能力,按需添加

📝 发现内容有误?点击此处直接编辑

← 上一篇 镜像签名与验证
下一篇 → Secret 敏感信息管理
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库