AlloyDB Omni 中的 Active Directory 配置是可选项,默认处于停用状态。只有使用 Active Directory 服务器进行身份验证的环境才能使用此配置机制。
准备工作
在集成 Active Directory 之前,请先确保满足以下要求:
确保已设置 Active Directory。
获取 Active Directory 服务器的 REALM。
获取 Active Directory 服务器的密钥分发中心 (KDC) 主机名。该主机名存储在 Docker 映像中。
获取 Active Directory 服务器的管理服务器主机名。此主机名存储在 Docker 映像中。
确保您有权访问 Active Directory 服务器,以便生成 .keytab 文件。
选择用于测试和登录的 Active Directory 用户。
从现有 Active Directory 服务器获取 .keytab 文件。
从现有 Active Directory 服务器获取 .keytab 文件
如需从 Active Directory 服务器获取 keytab,请按以下步骤操作:
以管理员身份登录 Active Directory 服务器的 PowerShell 终端。
运行以下命令或使用 Active Directory 界面创建名为 postgres 的用户。
New-ADUser -Name "postgres" `
-SamAccountName "postgres" `
-UserPrincipalName "postgres@REALM" `
-Description "Service Account for AlloyDB Omni PostgreSQL Kerberos Authentication" `
-AccountPassword (Read-Host -AsSecureString "Set a strong password for the postgres service account") `
-Enabled $true `
-PasswordNeverExpires $true
生成映射到此 Active Directory 服务器的服务主账号 keytab。
ktpass /princ postgres/ALLOYDB_HOST_NAME@REALM /Pass ChangeMe123 /mapuser postgres /crypto ALL /ptype KRB5_NT_PRINCIPAL /mapOp set /out C:\Users\Public\postgres.keytab
其中,<HOST> 是您计划在其中部署 AlloyDB Omni 的服务器的完全限定域名,例如 alloydb-server.ad.example.com。您必须在网域领域映射的 krb5.conf 文件中配置相同的主机。
将 keytab 文件复制到 Linux 机器。
启用 Active Directory 身份验证
如需在 AlloyDB Omni 中启用 Active Directory 身份验证,请按照以下步骤操作,其中包括配置通用安全服务应用编程接口 (GSSAPI),该接口是一种可让程序访问安全服务的应用编程接口。
将以下条目添加到 /var/lib/postgresql/data/pg_hba.conf 文件中,放在 host all all all scram-sha-256 条目之前。
运行以下 Docker 命令,以在容器内添加 gss:
docker exec CONTAINER_NAME sed -i 's;^host all all all scram-sha-256$;hostgssenc all all 0.0.0.0/0 gss map=gssmap\n&;' /var/lib/postgresql/data/pg_hba.conf
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Integrate Active Directory user support with AlloyDB Omni\n\nSelect a documentation version: Current (16.8.0)keyboard_arrow_down\n\n- [Current (16.8.0)](/alloydb/omni/current/docs/integrate-active-directory)\n- [16.8.0](/alloydb/omni/16.8.0/docs/integrate-active-directory)\n- [16.3.0](/alloydb/omni/16.3.0/docs/integrate-active-directory)\n\n\u003cbr /\u003e\n\nThis page describes how to integrate AlloyDB Omni with your existing [Active Directory](https://en.wikipedia.org/wiki/Active_Directory) implementation so that you can use your existing usernames and passwords to access your AlloyDB Omni database. Active Directory integration provides Kerberos as the default authentication mechanism to communicate with AlloyDB Omni. For more information, see [Active Directory overview](/alloydb/omni/current/docs/active-directory-overview).\n\n\u003cbr /\u003e\n\nActive Directory configuration in AlloyDB Omni is optional and is\ndisabled by default. Only environments using Active Directory Server for\nauthentication can use this configuration mechanism.\n\nBefore you begin\n----------------\n\nBefore you integrate Active Directory, make sure that you meet the following\nrequirements:\n\n- Make sure that the Active Directory is set up.\n- Obtain the `REALM` of the Active Directory server.\n- Obtain the Key Distribution Center (KDC) hostname of the Active Directory server. The hostname is stored in the Docker images.\n- Obtain the admin server hostname of the Active Directory server. This hostname is stored in the Docker images.\n- Make sure that you have access to the Active Directory server so that you can generate a `.keytab` file.\n- Choose an Active Directory user to use for testing and signin.\n- Get a `.keytab` file from the existing Active Directory server.\n\n### Obtain a .keytab file from the existing Active Directory server\n\nTo get a keytab from the Active Directory Server, follow\nthese steps:\n\n1. Sign in as the administrator to the powershell terminal of the Active Directory server.\n2. Create a user called `postgres` by running the following command, or by using the Active Directory user interface. \n\n```\n New-ADUser -Name \"postgres\" `\n -SamAccountName \"postgres\" `\n -UserPrincipalName \"postgres@REALM\" `\n -Description \"Service Account for AlloyDB Omni PostgreSQL Kerberos Authentication\" `\n -AccountPassword (Read-Host -AsSecureString \"Set a strong password for the postgres service account\") `\n -Enabled $true `\n -PasswordNeverExpires $true\n \n```\n3. Generate a service principal keytab that maps to this Active Directory server. \n\n```\n ktpass /princ postgres/ALLOYDB_HOST_NAME@REALM /Pass ChangeMe123 /mapuser postgres /crypto ALL /ptype KRB5_NT_PRINCIPAL /mapOp set /out C:\\Users\\Public\\postgres.keytab\n \n```\n4. Where `\u003cHOST\u003e` is the fully qualified domain name of the server where you plan to deploy AlloyDB Omni---for example, `alloydb-server.ad.example.com`. You must configure the same host in the `krb5.conf` file in your domain realm mapping.\n5. Copy the keytab file to your linux machine.\n\n| **Important:** The keytab file is highly sensitive. It contains the secret key for your service. Transfer the keytab to your Linux machine using a secure method---like SCP or WinSCP---and make sure that its file permissions on the Linux server are highly restrictive---for example, that they are readable only by the PostgreSQL user.\n\nEnable Active Directory authentication\n--------------------------------------\n\nTo enable Active Directory authentication in AlloyDB Omni,\nfollow these steps, which include configuring the\n[Generic Security Service Application Program Interface](https://en.wikipedia.org/wiki/Generic_Security_Services_Application_Program_Interface)\n(GSSAPI), which is an application programming interface that enables programs to\naccess security services.\n\n1. Add the following entries to the `/var/lib/postgresql/data/pg_hba.conf` file,\n before the \n\n `host all all all scram-sha-256` entry.\n\n 1. Run the following Docker command to add `gss` inside the container:\n\n ```\n docker exec CONTAINER_NAME sed -i 's;^host all all all scram-sha-256$;hostgssenc all all 0.0.0.0/0 gss map=gssmap\\n&;' /var/lib/postgresql/data/pg_hba.conf\n ```\n 2. Run the following Docker command to verify that the `pg_hba.conf` file\n is inside the container:\n\n ```\n docker exec CONTAINER_NAME cat /var/lib/postgresql/data/pg_hba.conf\n ```\n 3. Verify that the following entry is in the file:\n\n ```\n hostgssenc all all 0.0.0.0/0 gss map=gssmap\n ```\n\n For more information, see\n [The pg_hba.conf File](https://www.postgresql.org/docs/current/auth-pg-hba-conf.html).\n2. Copy the key tab file to the data directory inside the AlloyDB Omni image.\n\n ```\n docker cp PATH TO KEYTAB FILE CONTAINER_NAME:/var/lib/postgresql/data/alloydb.keytab\n docker exec CONTAINER_NAME chmod 600 /var/lib/postgresql/data/alloydb.keytab\n docker exec CONTAINER_NAME chown postgres:postgres /var/lib/postgresql/data/alloydb.keytab\n ```\n | **Note:** The keytab file must be owned by the PostgreSQL user and must have 600 permissions.\n\n The keytab file is generated by Kerberos for the PostgreSQL server. To\n learn more about authentication, see\n [GSSAPI Authentication](https://www.postgresql.org/docs/current/gssapi-auth.html).\n3. Add an entry for the keytab file to the `/var/lib/postgresql/data/`\u003cvar translate=\"no\"\u003eDATA_DIR\u003c/var\u003e`/postgresql.conf`\n file.\n\n 1. Run the following Docker command to add the entry inside the container:\n\n ```\n docker exec CONTAINER_NAME sed -i '$akrb_server_keyfile='\"'\"'/var/lib/postgresql/data/alloydb.keytab'\"'\" /var/lib/postgresql/data/postgresql.conf\n ```\n 2. Run the following Docker command verify the `postgresql.conf` file\n inside the container:\n\n ```\n docker exec CONTAINER_NAME tail /var/lib/postgresql/data/postgresql.conf\n ```\n 3. Ensure that the following entry is in the file:\n\n ```\n krb_server_keyfile=/var/lib/postgresql/data/alloydb.keytab\n ```\n\n For more information, see\n [krb_server_keyfile](https://www.postgresql.org/docs/current/runtime-config-connection.html#GUC-KRB-SERVER-KEYFILE).\n4. Optional: Add entries to the `/var/lib/postgresql/data/`\u003cvar translate=\"no\"\u003eDATA_DIR\u003c/var\u003e`/pg_ident.conf` file.\n\n When you use an external authentication system like GSSAPI, the name of\n the operating system user that initiated the connection might not be the\n same as the database user (role) that you want to use.\n\n In this case, specify the system user-to-PostgreSQL user mapping in the\n `/var/lib/postgresql/data/`\u003cvar translate=\"no\"\u003eDATA_DIR\u003c/var\u003e`/pg_ident.conf` file: \n\n ```\n docker exec -it CONTAINER_NAME bash\n $ echo -e \"\n gssmap /^(.*)@EXAMPLE\\.COM$ \\1\n gssmap /^(.*)@example\\.com$ \\1\n \" | column -t | tee -a /var/lib/postgresql/data/pg_ident.conf\n ```\n\n To implement username mapping, specify `map=gssmap` in the options field in\n the `pg_hba.conf` file.\n\n For more information about ident-based authentication, see\n [Ident Maps](https://www.postgresql.org/docs/8.0/auth-methods.html#:%7E:text=19.2.4.3.%20Ident%20Maps).\n5. Reload the PostgreSQL configurations using the following command:\n\n ```\n docker exec -it CONTAINER_NAME psql -h localhost -U postgres\n psql (16.3)\n Type \"help\" for help.\n postgres=# select pg_reload_conf();\n ```\n\nTest Active Directory authentication\n------------------------------------\n\nTo verify that Active Directory authentication is working, follow\nthese steps:\n\n1. Sign into Active Directory using `kinit`.\n2. Run the following `psql` command from the machine where you usually run `kinit`:\n\n ```\n root@4f6414ad02ef:/# kinit AD_USER_NAME\n Password for user1@YOUR.REALM:\n\n root@4f6414ad02ef:/# psql --h ALLOYDB_SERVER_HOST_NAME -U AD_USER_NAME\n psql (16.6 (Ubuntu 16.6-0ubuntu0.24.04.1), server 16.3)\n GSSAPI-encrypted connection\n Type \"help\" for help.\n\n user1=#\n ```\n | **Note:** To ensure that Active Directory authentication is working, make sure that the user in Active Directory has a role in AlloyDB Omni, or ensure that the user has a valid mapping in the `pg_ident.conf` file.\n\nDisable Active Directory authentication\n---------------------------------------\n\nTo disable Active Directory authentication in AlloyDB Omni,\nfollow these steps, which disable the GSSAPI:\n\n1. Remove entries in the `pg_hba.conf` file that point to the `gss`\n authentication method:\n\n ```\n docker exec CONTAINER_NAME sed -i '/hostgssenc all all 0.0.0.0\\/0 gss map=gssmap/d' /var/lib/postgresql/data/pg_hba.conf\n ```\n2. Reload the PostgreSQL configurations using the following command:\n\n ```\n docker exec -it CONTAINER_NAME psql -h localhost -U postgres\n psql (16.3)\n Type \"help\" for help.\n postgres=# select pg_reload_conf();\n ```\n\nWhat's next\n-----------\n\n- [Integrate Active Directory user support on Kubernetes](/alloydb/omni/current/docs/integrate-active-directory-kubernetes-operator).\n- [Troubleshoot Active Directory in AlloyDB Omni](/alloydb/omni/current/docs/troubleshoot-active-directory-integration).\n- [Integrate Active Directory group support with AlloyDB Omni](/alloydb/omni/current/docs/integrate-ad-group-support-alloydb-omni).\n- [Integrate Active Directory group support on Kubernetes](/alloydb/omni/current/docs/integrate-ad-group-support-kubernetes-operator).\n- [Troubleshoot Active Directory integration in AlloyDB Omni](/alloydb/omni/current/docs/troubleshoot-active-directory-integration)."]]