﻿---
title: "只要托管钩子：allow_managed_hooks_only 只能写进 requirements.toml"
summary: "写进普通 config.toml 不会生效。用户、项目、会话和插件钩子会被跳过，MDM 脚本还要自己分发。"
category: hooks
level: advanced
surfaces: [cli, app, ide]
tags: ["hooks", "requirements.toml", "企业"]
canonical: /tips/managed-hooks-only/
---

# 只要托管钩子：allow_managed_hooks_only 只能写进 requirements.toml

写进普通 config.toml 不会生效。用户、项目、会话和插件钩子会被跳过，MDM 脚本还要自己分发。

企业想禁止本机 `~/.codex/hooks.json` 和仓库 `.codex/hooks.json`，只留管理员那一套，把开关写进 `requirements.toml`（或 MDM 的 `requirements_toml_base64`），**不要**写进 `~/.codex/config.toml`。官方 config.md 写明：普通配置层里的同名键会被忽略。

```toml
# requirements.toml（系统层 / MDM / 云托管），不是用户 config.toml
allow_managed_hooks_only = true

[features]
hooks = true

[hooks]
managed_dir = "/enterprise/hooks"
windows_managed_dir = 'C:\enterprise\hooks'

[[hooks.PreToolUse]]
matcher = "^Bash$"

[[hooks.PreToolUse.hooks]]
type = "command"
command = "python3 /enterprise/hooks/pre_tool_use_policy.py"
command_windows = "py -3 C:\\enterprise\\hooks\\pre_tool_use_policy.py"
timeout = 30
statusMessage = "Checking managed Bash command"
```

要点：

- 只跳过用户、项目、会话、插件来源；`requirements.toml` 和其他托管层仍会加载
- 用户把 `[features] hooks = false` 时，还要在 requirements 里钉 `hooks = true`，否则托管钩子也被关掉
- Codex **不会**把 `managed_dir` 里的脚本随安装分发，MDM 必须自己放绝对路径
- 启动时被跳过的本机钩子会打简短警告；用 `/debug-config` 看实际生效层

个人机器不要抄这一段。没有 MDM 时，继续用用户级 `hooks.json` 并走信任流程。

## 来源

- [OpenAI · Managed configuration](https://learn.chatgpt.com/docs/enterprise/managed-configuration)
- [openai/codex docs/config.md](https://github.com/openai/codex/blob/main/docs/config.md)
