对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

自管理Kerberos身份验证故障排除

mongokerberos 程序提供了一种便捷的方法,以验证您的平台的 Kerberos 配置是否与 MongoDB 一起使用,并测试 MongoDB 客户端的 Kerberos 身份验证是否按预期运行。

mongokerberos工具可以帮助诊断常见配置问题,并且是对 Kerberos 配置进行故障排除时建议入手的工具。 有关更多信息,请参阅mongokerberos文档。

mongokerberos 仅在 MongoDB Enterprise 中可用。

如果您在使用 Kerberosmongod mongos启动 或 或对其进行身份验证时遇到困难,请考虑以下操作:

  • 确保您运行的是 MongoDB Enterprise,而不是 MongoDB Community Edition。 Kerberos 身份验证是 MongoDB Enterprise 的一项功能,不适用于 MongoDB Community Edition 二进制文件。

    要验证您是否使用 MongoDB Enterprise,请将 --version 命令行选项传递给 mongodmongos

    mongod --version

    在该命令的输出中,请查找字符串 modules: subscriptionmodules: enterprise,以确认您使用的是 MongoDB Enterprise 二进制文件。

  • 确保mongodmongos实例的规范系统主机名是可解析的完全限定域名。

    在 Linux 上,您可以在系统提示符下使用hostname -f命令验证系统主机名解析。

  • On Linux, ensure that the primary component of the service principal name (SPN) of the SPN is mongodb. If the primary component of the SPN is not mongodb, you must specify the primary component using --setParameter saslServiceName.

  • On Linux, ensure that the instance component of the service principal name (SPN) in the keytab file matches the canonical system hostname of the mongod or mongos instance. If the mongod or mongos instance's system hostname is not in the keytab file, authentication will fail with a GSSAPI error acquiring credentials. error message.

    如果 返回的mongodmongos 实例的主机名不是完全限定的,请在启动hostname -f --setParameter saslHostNamemongod 时使用mongos 设置实例的完全限定域名。

  • 确保运行mongodmongos实例的每台主机都有APTR DNS 记录,以提供正向和反向 DNS 查找。 A记录应映射到mongodmongos的 FQDN。

  • 确保托管 MongoDB 实例和 Kerberos 基础架构的服务器上的时钟在最大时间偏差内:默认为 5 分钟。 大于最大时间偏差的时间差会阻止身份验证成功。

  • 确保Linux KRB5 密钥表包含以 @<KERBEROS REALM> 结尾的主体名称。要验证 SPN,请在 Active Directory 上运行setspn -Q <spn>。如果配置正确,此命令会为附加到该 SPN 的帐户返回一个标识名。如果在Linux上运行klist -k <keytab>,则 <spn>@<KERBEROS REALM> 会出现在密钥表中。

  • 如果使用 Active Directory 作为KDC ,请确保 MongoDB 服务帐户是用户帐户,而不是计算机帐户。

  • If you use AES encryption with Active Directory, enable AES on the MongoDB service account with either the msDS-SupportedEncryptionTypes property or the "Network Security: Configure Encryption types allowed for Kerberos" policy setting.

  • Kerberos 对其密钥生成算法进行盐化处理,以确保具有相同密码的两个用户生成不同的密钥。 Linux 和 Active Directory (AD) 上的ktutil不使用相同的进程来生成盐。 跨 Linux 和 Windows 环境工作时,这种差异可能会导致身份验证失败。 要缓解此问题,您可以:

    • 在 AD 服务器上生成 keytab 文件,并将生成的文件移动到 Linux 服务器。

      ktpass /out <outfile.keytab> /princ <spn>@<KERBEROS REALM> /mapuser <current userPrincipalName> /crypto ALL /ptype KRB5_NT_PRINCIPAL +rndpass

      注意

      这会将userPrincipalName更改为/princ中的值。

    • 在 Linux 上使用ktutil并强制其使用正确的盐。 要强制ktutil使用正确的盐:

      1. 生成使用userPrincipalName作为主体名称的密钥表条目。

        ktutil: add_entry -password -p <userPrincipalName>@<KERBEROS REALM> -e aes256-cts-hmac-sha1-96 -k <KVNO>
        Password for <userPrincipalName>@<KERBEROS REALM>:
        ktutil: list -k
        slot KVNO Principal
      2. Hexdump 密钥。

      3. 创建一个新的 keytab 条目,使用<spn>@<KERBEROS REALM>作为主体名称,并使用十六进制转储的密钥。

        1 <KVNO> <userPrincipalName>@<KERBEROS REALM>(0x<HEXDUMP>)
        ktutil: add_entry -key -p <spn>@<KERBEROS REALM> -e aes256-cts-hmac-sha1-96 -k <KVNO>
        Key for <spn>@<KERBEROS REALM> (hex): <HEXDUMP>
        ktutil: write_kt mongodb_ad.keytab
    • 使用 RC 4 -HMAC,它不使用盐,但不建议这样做。

MIT Kerberos provides the KRB5_TRACE environment variable for trace logging output. If you are having persistent problems with MIT Kerberos on Linux, you can set KRB5_TRACE when starting your mongod, mongos, or mongosh instances to produce verbose logging.

例如,以下命令启动一个独立运行的实例mongod ,其密钥表文件位于默认路径/etc/krb5.keytab,并将KRB5_TRACE设置为写入/logs/mongodb-kerberos.log

env KRB5_KTNAME=/etc/krb5.keytab \
KRB5_TRACE=/logs/mongodb-kerberos.log \
mongod --dbpath /data/db --logpath /data/db/mongodb.log \
--auth --setParameter authenticationMechanisms=GSSAPI \
--bind_ip localhost,<hostname(s)|ip address(es)> --fork

在某些情况下,如果 Kerberos 服务出现问题,MongoDB 会从 GSSAPI 接口返回错误消息。 一些常见的错误消息如下:

GSSAPI error in client while negotiating security context.

此错误发生在客户端上,反映凭证不足或恶意进行身份验证尝试。

如果收到此错误,请确保在连接到主机时使用正确的档案和正确的完全限定域名。

GSSAPI error acquiring credentials.
此错误发生在mongodmongos启动期间,反映了系统主机名配置不当、keytab 文件丢失或配置错误。