

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Winbind 手动将亚马逊 EC2 Linux 实例加入你的 AWS 托管微软 AD 活动目录
<a name="join_linux_instance_winbind"></a>

您可以使用 Winbind 服务将 Amazon EC2 Linux 实例手动加入 AWS Managed Microsoft AD Active Directory 域。这使您现有的本地 Active Directory 用户能够在访问加入您的 AWS 托管 Microsoft AD 活动目录的 Linux 实例时使用其活动目录凭据。支持以下 Linux 实例分发版和版本：
+ Amazon Linux AMI 2018.03.0
+ Amazon Linux 2（64 位 x86）
+ Amazon Linux 2023 AMI
+ Red Hat Enterprise Linux 8 (HVM)（64 位 x86）
+ Ubuntu Server 18.04 LTS 和 Ubuntu Server 16.04 LTS
+ CentOS 7 x86-64
+ SUSE Linux 企业服务器 15 SP1

**注意**  
其他 Linux 分发版和版本可能会正常运行，但未经过测试。

## 将 Linux 实例加入你的 AWS 托管微软 AD 活动目录
<a name="join_linux_winbind_prereq"></a>

**重要**  
以下某些过程如果未正确执行，可能会使实例无法访问或不可用。因此，我们强烈建议在执行这些过程之前对实例创建备份或拍摄快照。

**将 Linux 实例加入目录**  
使用以下选项卡之一对特定 Linux 实例执行步骤：

------
#### [ Amazon Linux/CENTOS/REDHAT ]<a name="amazonlinux"></a>

1. 使用任何 SSH 客户端连接到实例。

1. 配置 Linux 实例以使用 Directory Service提供的 DNS 服务器的 DNS 服务器 IP 地址。可以通过在附加到 VPC 的 DHCP 选项集中进行设置，或是通过在实例上手动设置，来执行此操作。如果您想手动设置，请参阅 AWS 知识中心中的[如何将静态 DNS 服务器分配给私有 Amazon EC2 实例，以](https://aws.amazon.com/premiumsupport/knowledge-center/ec2-static-dns-ubuntu-debian/)获取有关为您的特定 Linux 发行版和版本设置永久 DNS 服务器的指导。

1. 确保 Linux 实例为最新状态。

   ```
   sudo yum -y update
   ```

1. 在 Linux 实例上安装所需 Samba/Winbind 软件包。

   ```
   sudo yum -y install authconfig samba samba-client samba-winbind samba-winbind-clients
   ```

1. 对主 `smb.conf` 文件进行备份，以便在出现任何故障时可以恢复到该文件：

   ```
   sudo cp /etc/samba/smb.conf /etc/samba/smb.bk
   ```

1. 在文本编辑器中打开原始配置文件 [`/etc/samba/smb.conf`]。

   ```
   sudo vim /etc/samba/smb.conf
   ```

   填写 Active Directory 域环境信息，如下例所示：

   ```
   [global]
    workgroup = example
    security = ads
    realm = example.com
    idmap config * : rangesize = 1000000
    idmap config * : range = 1000000-19999999
    idmap config * : backend = autorid
    winbind enum users = no
    winbind enum groups = no
    template homedir = /home/%U@%D
    template shell = /bin/bash
    winbind use default domain = false
   ```

1. 在文本编辑器中打开主机文件 [`/etc/hosts`]。

   ```
   sudo vim /etc/hosts
   ```

   按如下方式添加 Linux 实例私有 IP 地址：

   ```
   10.x.x.x  Linux_hostname.example.com Linux_hostname
   ```
**注意**  
如果您未在 `/etc/hosts` 文件中指定 IP 地址，则在将实例加入域时可能会收到以下 DNS 错误。  
`No DNS domain configured for linux-instance. Unable to perform DNS Update. DNS update failed: NT_STATUS_INVALID_PARAMETER`  
此错误表示加入成功，但是 [net ads] 命令无法在 DNS 中注册 DNS 记录。

1. 使用 net 实用程序将 Linux 实例加入 Active Directory。

   ```
   sudo net ads join -U join_account@example.com
   ```  
*join\$1account@example.com*  
*example.com*网域中具有域加入权限的账户。在出现提示时输入账户的密码。有关委托这些权限的更多信息，请参阅[为托 AWS 管 Microsoft AD 委派目录加入权限](directory_join_privileges.md)。  
*example.com*  
目录的完全限定 DNS 名称。

   ```
   Enter join_account@example.com's password:
   Using short domain name -- example
   Joined 'IP-10-x-x-x' to dns domain 'example.com'
   ```

1. 修改 PAM 配置文件，使用以下命令添加 Winbind 身份验证所需的条目：

   ```
   sudo authconfig --enablewinbind --enablewinbindauth  --enablemkhomedir   --update
   ```

1. 通过修改 `/etc/ssh/sshd_config` 文件设置 SSH 服务以允许进行密码身份验证。

   1. 在文本编辑器中打开 `/etc/ssh/sshd_config` 文件。

      ```
      sudo vi /etc/ssh/sshd_config
      ```

   1. 将 `PasswordAuthentication` 设置为 `yes`。

      ```
      PasswordAuthentication yes
      ```

   1. 重新启动 SSH 服务。

      ```
      sudo systemctl restart sshd.service
      ```

      或者：

      ```
      sudo service sshd restart
      ```

1. 重新启动实例之后，使用任何 SSH 客户端连接到它，然后通过执行以下步骤将要为域用户或组授予的根权限添加到 sudoers 列表：

   1. 使用以下命令打开 `sudoers` 文件：

      ```
      sudo visudo
      ```

   1. 按如下方式从信任或可信域中添加所需的组或用户，然后将其保存。

      ```
      ## Adding Domain Users/Groups.
      %domainname\\AWS\ Delegated\ Administrators ALL=(ALL:ALL) ALL
      %domainname\\groupname ALL=(ALL:ALL) ALL
      domainname\\username ALL=(ALL:ALL) ALL
      %Trusted_DomainName\\groupname ALL=(ALL:ALL) ALL
      Trusted_DomainName\\username ALL=(ALL:ALL) ALL
      ```

      （以上示例使用“\$1<space>”形成 Linux 空格字符。）

------
#### [ SUSE ]<a name="suse"></a>

1. 使用任何 SSH 客户端连接到实例。

1. 配置 Linux 实例以使用 Directory Service提供的 DNS 服务器的 DNS 服务器 IP 地址。可以通过在附加到 VPC 的 DHCP 选项集中进行设置，或是通过在实例上手动设置，来执行此操作。如果您想手动设置，请参阅 AWS 知识中心中的[如何将静态 DNS 服务器分配给私有 Amazon EC2 实例，以](https://aws.amazon.com/premiumsupport/knowledge-center/ec2-static-dns-ubuntu-debian/)获取有关为您的特定 Linux 发行版和版本设置永久 DNS 服务器的指导。

1. 确保您的 SUSE Linux 15 实例为最新状态。

   1. 连接程序包存储库。

      ```
      sudo SUSEConnect -p PackageHub/15.1/x86_64
      ```

   1. 更新 SUSE。

      ```
      sudo zypper update -y
      ```

1. 在 Linux 实例上安装所需 Samba/Winbind 软件包。

   ```
   sudo zypper in -y samba samba-winbind
   ```

1. 对主 `smb.conf` 文件进行备份，以便在出现任何故障时可以恢复到该文件：

   ```
   sudo cp /etc/samba/smb.conf /etc/samba/smb.bk
   ```

1. 在文本编辑器中打开原始配置文件 [`/etc/samba/smb.conf`]。

   ```
   sudo vim /etc/samba/smb.conf
   ```

   填写 Active Directory 域环境信息，如下例所示：

   ```
   [global]
    workgroup = example
    security = ads
    realm = example.com
    idmap config * : rangesize = 1000000
    idmap config * : range = 1000000-19999999
    idmap config * : backend = autorid
    winbind enum users = no
    winbind enum groups = no
    template homedir = /home/%U@%D
    template shell = /bin/bash
    winbind use default domain = false
   ```

1. 在文本编辑器中打开主机文件 [`/etc/hosts`]。

   ```
   sudo vim /etc/hosts
   ```

   按如下方式添加 Linux 实例私有 IP 地址：

   ```
   10.x.x.x  Linux_hostname.example.com Linux_hostname
   ```
**注意**  
如果您未在 `/etc/hosts` 文件中指定 IP 地址，则在将实例加入域时可能会收到以下 DNS 错误。  
`No DNS domain configured for linux-instance. Unable to perform DNS Update. DNS update failed: NT_STATUS_INVALID_PARAMETER`  
此错误表示加入成功，但是 [net ads] 命令无法在 DNS 中注册 DNS 记录。

1. 使用以下命令将 Linux 实例加入目录。

   ```
   sudo net ads join -U join_account@example.com
   ```  
*join\$1account*  
具有域加入权限的*example.com*域中的 s AMAccount 名称。在出现提示时输入账户的密码。有关委托这些权限的更多信息，请参阅[为托 AWS 管 Microsoft AD 委派目录加入权限](directory_join_privileges.md)。  
*example.com*  
目录的完全限定 DNS 名称。

   ```
   Enter join_account@example.com's password:
   Using short domain name -- example
   Joined 'IP-10-x-x-x' to dns domain 'example.com'
   ```

1. 修改 PAM 配置文件，使用以下命令添加 Winbind 身份验证所需的条目：

   ```
   sudo pam-config --add --winbind --mkhomedir
   ```

1. 在文本编辑器中打开 Name Service Switch 配置文件 [`/etc/nsswitch.conf`]。

   ```
   vim /etc/nsswitch.conf
   ```

   添加 Winbind 指令，如下所示。

   ```
   passwd: files winbind
   shadow: files winbind
   group:  files winbind
   ```

1. 通过修改 `/etc/ssh/sshd_config` 文件设置 SSH 服务以允许进行密码身份验证。

   1. 在文本编辑器中打开 `/etc/ssh/sshd_config` 文件。

      ```
      sudo vim /etc/ssh/sshd_config
      ```

   1. 将 `PasswordAuthentication` 设置为 `yes`。

      ```
      PasswordAuthentication yes
      ```

   1. 重新启动 SSH 服务。

      ```
      sudo systemctl restart sshd.service
      ```

      或者：

      ```
      sudo service sshd restart
      ```

1. 重新启动实例之后，使用任何 SSH 客户端连接到它，然后通过执行以下步骤将要为域用户或组授予的根权限添加到 sudoers 列表：

   1. 使用以下命令打开 `sudoers` 文件：

      ```
      sudo visudo
      ```

   1. 按如下方式从信任或可信域中添加所需的组或用户，然后将其保存。

      ```
      ## Adding Domain Users/Groups.
      %domainname\\AWS\ Delegated\ Administrators ALL=(ALL:ALL) ALL
      %domainname\\groupname ALL=(ALL:ALL) ALL
      domainname\\username ALL=(ALL:ALL) ALL
      %Trusted_DomainName\\groupname ALL=(ALL:ALL) ALL
      Trusted_DomainName\\username ALL=(ALL:ALL) ALL
      ```

      （以上示例使用“\$1<space>”形成 Linux 空格字符。）

------
#### [ Ubuntu ]<a name="ubuntu"></a>

1. 使用任何 SSH 客户端连接到实例。

1. 配置 Linux 实例以使用 Directory Service提供的 DNS 服务器的 DNS 服务器 IP 地址。可以通过在附加到 VPC 的 DHCP 选项集中进行设置，或是通过在实例上手动设置，来执行此操作。如果您想手动设置，请参阅 AWS 知识中心中的[如何将静态 DNS 服务器分配给私有 Amazon EC2 实例，以](https://aws.amazon.com/premiumsupport/knowledge-center/ec2-static-dns-ubuntu-debian/)获取有关为您的特定 Linux 发行版和版本设置永久 DNS 服务器的指导。

1. 确保 Linux 实例为最新状态。

   ```
   sudo apt-get -y upgrade
   ```

1. 在 Linux 实例上安装所需 Samba/Winbind 软件包。

   ```
   sudo apt -y install samba winbind libnss-winbind libpam-winbind
   ```

1. 对主 `smb.conf` 文件进行备份，以便在出现任何故障时可以恢复到该文件：

   ```
   sudo cp /etc/samba/smb.conf /etc/samba/smb.bk
   ```

1. 在文本编辑器中打开原始配置文件 [`/etc/samba/smb.conf`]。

   ```
   sudo vim /etc/samba/smb.conf
   ```

   填写 Active Directory 域环境信息，如下例所示：

   ```
   [global]
    workgroup = example
    security = ads
    realm = example.com
    idmap config * : rangesize = 1000000
    idmap config * : range = 1000000-19999999
    idmap config * : backend = autorid
    winbind enum users = no
    winbind enum groups = no
    template homedir = /home/%U@%D
    template shell = /bin/bash
    winbind use default domain = false
   ```

1. 在文本编辑器中打开主机文件 [`/etc/hosts`]。

   ```
   sudo vim /etc/hosts
   ```

   按如下方式添加 Linux 实例私有 IP 地址：

   ```
   10.x.x.x  Linux_hostname.example.com Linux_hostname
   ```
**注意**  
如果您未在 `/etc/hosts` 文件中指定 IP 地址，则在将实例加入域时可能会收到以下 DNS 错误。  
`No DNS domain configured for linux-instance. Unable to perform DNS Update. DNS update failed: NT_STATUS_INVALID_PARAMETER`  
此错误表示加入成功，但是 [net ads] 命令无法在 DNS 中注册 DNS 记录。

1. 使用 net 实用程序将 Linux 实例加入 Active Directory。

   ```
   sudo net ads join -U join_account@example.com
   ```  
*join\$1account@example.com*  
*example.com*网域中具有域加入权限的账户。在出现提示时输入账户的密码。有关委托这些权限的更多信息，请参阅[为托 AWS 管 Microsoft AD 委派目录加入权限](directory_join_privileges.md)。  
*example.com*  
目录的完全限定 DNS 名称。

   ```
   Enter join_account@example.com's password:
   Using short domain name -- example
   Joined 'IP-10-x-x-x' to dns domain 'example.com'
   ```

1. 修改 PAM 配置文件，使用以下命令添加 Winbind 身份验证所需的条目：

   ```
   sudo pam-auth-update --add --winbind --enable mkhomedir
   ```

1. 在文本编辑器中打开 Name Service Switch 配置文件 [`/etc/nsswitch.conf`]。

   ```
   vim /etc/nsswitch.conf
   ```

   添加 Winbind 指令，如下所示。

   ```
   passwd: compat winbind
   group:  compat winbind
   shadow: compat winbind
   ```

1. 通过修改 `/etc/ssh/sshd_config` 文件设置 SSH 服务以允许进行密码身份验证。

   1. 在文本编辑器中打开 `/etc/ssh/sshd_config` 文件。

      ```
      sudo vim /etc/ssh/sshd_config
      ```

   1. 将 `PasswordAuthentication` 设置为 `yes`。

      ```
      PasswordAuthentication yes
      ```

   1. 重新启动 SSH 服务。

      ```
      sudo systemctl restart sshd.service
      ```

      或者：

      ```
      sudo service sshd restart
      ```

1. 重新启动实例之后，使用任何 SSH 客户端连接到它，然后通过执行以下步骤将要为域用户或组授予的根权限添加到 sudoers 列表：

   1. 使用以下命令打开 `sudoers` 文件：

      ```
      sudo visudo
      ```

   1. 按如下方式从信任或可信域中添加所需的组或用户，然后将其保存。

      ```
      ## Adding Domain Users/Groups.
      %domainname\\AWS\ Delegated\ Administrators ALL=(ALL:ALL) ALL
      %domainname\\groupname ALL=(ALL:ALL) ALL
      domainname\\username ALL=(ALL:ALL) ALL
      %Trusted_DomainName\\groupname ALL=(ALL:ALL) ALL
      Trusted_DomainName\\username ALL=(ALL:ALL) ALL
      ```

      （以上示例使用“\$1<space>”形成 Linux 空格字符。）

------

## 连接到 Linux 实例
<a name="linux_winbind_connect"></a>

当用户使用 SSH 客户端连接到实例时，系统会提示他们输入用户名。用户可以采用 `username@example.com` 或 `EXAMPLE\username` 格式输入用户名。响应将类似于以下内容，具体取决于您使用的 Linux 发行版：

**Amazon Linux、Red Hat Enterprise Linux 和 CentOS Linux**

```
login as: johndoe@example.com
johndoe@example.com's password:
Last login: Thu Jun 25 16:26:28 2015 from XX.XX.XX.XX
```

**SUSE Linux**

```
SUSE Linux Enterprise Server 15 SP1 x86_64 (64-bit)

As "root" (sudo or sudo -i) use the:
  - zypper command for package management
  - yast command for configuration management

Management and Config: https://www.suse.com/suse-in-the-cloud-basics
Documentation: https://www.suse.com/documentation/sles-15/
Forum: https://forums.suse.com/forumdisplay.php?93-SUSE-Public-Cloud

Have a lot of fun...
```

**Ubuntu Linux**

```
login as: admin@example.com
admin@example.com@10.24.34.0's password:
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-1057-aws x86_64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

  System information as of Sat Apr 18 22:03:35 UTC 2020

  System load:  0.01              Processes:           102
  Usage of /:   18.6% of 7.69GB   Users logged in:     2
  Memory usage: 16%               IP address for eth0: 10.24.34.1
  Swap usage:   0%
```