コンテンツにスキップ

Linuxユーザー認証の設定(TLSなし)

LDAP サーバーに登録したユーザーを、別マシンの Linux 認証クライアントから NSS / PAM 経由で使えるようにする、TLS を使用しない最小手順。

LDAP サーバーと、LDAP ユーザーでログインさせる Linux 認証クライアントを別のマシンに構成する。

マシン 役割 例
LDAP サーバー slapd を動かし、LDAP エントリを保存する ldap.example.com
認証クライアント NSS / PAM で LDAP ユーザーを参照し、ログイン認証に使う auth-client.example.com

認証クライアント側には nslcd、libnss-ldapd、libpam-ldapd を使う。nslcd が LDAP への問い合わせを受け持ち、NSS と PAM がユーザー情報の参照とログイン認証に利用する。SSSD を使う構成もある。


項目 例
LDAP サーバー ldap://ldap.example.com/
認証クライアント auth-client.example.com
Base DN dc=example,dc=com
ユーザー配置先 ou=people,dc=example,dc=com
グループ配置先 ou=groups,dc=example,dc=com
LDAP 管理者 DN cn=admin,dc=example,dc=com
認証クライアントの接続方式 Simple Bind(TLSなし)

LDAP サーバー側では、Base DN のデータベースと、そのデータベースを管理する rootDN を設定しておく。


LDAP サーバーに slapd と確認用コマンドを入れる。

Terminal window
sudo apt install slapd ldap-utils

Debian 系では、 dpkg-reconfigure slapd で通常データベースの suffix と rootDN を設定できる。

Terminal window
sudo dpkg-reconfigure slapd

設定例は次のとおり。

質問 入力例 意味
Omit OpenLDAP server configuration? No slapd の初期設定を行う
DNS domain name example.com Base DN が dc=example,dc=com になる
Organization name Example Base DN エントリの組織名
Administrator password 任意の管理者パスワード cn=admin,dc=example,dc=com の Bind パスワード
Database backend MDB 通常のバックエンド
Remove database when slapd is purged? No purge 時に DB を消すか
Move old database? Yes 既存 DB がある場合に退避する

この設定により、通常データベースの suffix は dc=example,dc=com、管理用 rootDN は cn=admin,dc=example,dc=com になる。


2. LDAPサーバーでBase DNを確認する

Section titled “2. LDAPサーバーでBase DNを確認する”

LDAP サーバー上で、通常データベースに Base DN が作成されていることを確認する。

Terminal window
sudo slapcat

成功の目安は、 dn: dc=example,dc=com が表示されること。


3. LDAPサーバーでコンテナを作る

Section titled “3. LDAPサーバーでコンテナを作る”

LDAP サーバーで、ユーザー用の ou=people とグループ用の ou=groups を作る。

base-ou.ldif
dn: ou=people,dc=example,dc=com
objectClass: organizationalUnit
ou: people
dn: ou=groups,dc=example,dc=com
objectClass: organizationalUnit
ou: groups
Terminal window
ldapadd \
-x \
-D "cn=admin,dc=example,dc=com" \
-W \
-f base-ou.ldif

追加後、LDAP サーバー上で ou=people と ou=groups が入ったことを確認する。

Terminal window
sudo slapcat

ou=people や ou=groups は必須ではないが、検索範囲や ACL を分けやすいためよく使われる。詳しくは LDAPツリー設計 を参照する。


4. LDAPサーバーでグループとユーザーを作る

Section titled “4. LDAPサーバーでグループとユーザーを作る”

LDAP サーバーで、主グループとして使う posixGroup と、ログインに使うユーザーエントリを作る。

先に userPassword に入れるハッシュを slappasswd で作る。

Terminal window
slappasswd

対話的にパスワードを入力すると、次のようなハッシュが出力される。

{SSHA}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

この値を account.ldif の userPassword に貼り付ける。

グループとユーザーは、同じ LDIF ファイルに書いて一度に追加できる。

account.ldif
dn: cn=developers,ou=groups,dc=example,dc=com
objectClass: posixGroup
cn: developers
gidNumber: 10000
dn: uid=ldapuser,ou=people,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: ldapuser
cn: LDAP User
sn: User
uidNumber: 10000
gidNumber: 10000
homeDirectory: /home/ldapuser
loginShell: /bin/bash
userPassword: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

posixGroup の必須属性は cn と gidNumber である。

主グループは、ユーザー側の gidNumber で決まる。主グループにするだけなら、通常 memberUid は不要である。

この LDIF で使っている objectClass と必須属性の対応は次のとおり。

objectClass 種類 必須属性
inetOrgPerson STRUCTURAL cn、 sn
posixAccount AUXILIARY cn、 uid、 uidNumber、 gidNumber、 homeDirectory
shadowAccount AUXILIARY uid

loginShell と userPassword は、Linux ログインとパスワード認証に使用する任意属性である。

グループとユーザーを追加する。

Terminal window
ldapadd \
-x \
-D "cn=admin,dc=example,dc=com" \
-W \
-f account.ldif

uidNumber と gidNumber は Linux 側で意味を持つ数値である。ローカルユーザーや他の LDAP ユーザーと重複しないようにする。

追加後、LDAP サーバー上でグループとユーザーが入ったことを確認する。

Terminal window
sudo slapcat

成功の目安は、 dn: cn=developers,ou=groups,dc=example,dc=com と dn: uid=ldapuser,ou=people,dc=example,dc=com が表示されること。


5. 認証クライアントへパッケージを入れる

Section titled “5. 認証クライアントへパッケージを入れる”

認証クライアントで、NSS / PAM と LDAP をつなぐパッケージを入れる。

Terminal window
sudo apt install nslcd libnss-ldapd libpam-ldapd

役割は次のとおり。

パッケージ 役割
nslcd NSS / PAM と LDAP サーバーの間をつなぐデーモン
libnss-ldapd NSS から LDAP ユーザー・グループを検索する
libpam-ldapd PAM から LDAP 認証を使う

6. 認証クライアントでnslcdを設定する

Section titled “6. 認証クライアントでnslcdを設定する”

認証クライアントの /etc/nslcd.conf に、LDAP サーバーの接続先と検索ベースを書く。

/etc/nslcd.conf
uid nslcd
gid nslcd
uri ldap://ldap.example.com/
base dc=example,dc=com
filter passwd (objectClass=posixAccount)
filter group (objectClass=posixGroup)

この形は、LDAP サーバー側で匿名検索が許可されている環境向けである。

/etc/nslcd.conf
binddn cn=readonly,dc=example,dc=com
bindpw read-only-password

cn=readonly,dc=example,dc=com は例であり、自動的に存在する DN ではない。実際に使うには、そのエントリを作成し、NSS が必要とする属性を読める ACL を設定しておく。

bindpw を書く場合は、ファイル権限に注意する。

Terminal window
sudo chown root:nslcd /etc/nslcd.conf
sudo chmod 640 /etc/nslcd.conf

設定後、 nslcd を再起動する。

Terminal window
sudo systemctl restart nslcd

7. 認証クライアントでNSSを設定する

Section titled “7. 認証クライアントでNSSを設定する”

認証クライアントの /etc/nsswitch.conf で passwd、 group、 shadow に ldap を追加する。

/etc/nsswitch.conf
passwd: files ldap
group: files ldap
shadow: files ldap

files ldap は、まずローカルファイルを見て、見つからなければ LDAP を検索するという意味である。


8. 認証クライアントでPAMを設定する

Section titled “8. 認証クライアントでPAMを設定する”

認証クライアントで、LDAP 認証を PAM に組み込む。

Debian 系では、 pam-auth-update で PAM 設定を更新できる。

Terminal window
sudo pam-auth-update

少なくとも次を有効にする。

  • LDAP Authentication
  • Create home directory on login

ホームディレクトリを初回ログイン時に作る設定は、概念的には次のような pam_mkhomedir.so の設定である。

session optional pam_mkhomedir.so skel=/etc/skel umask=077

9. 認証クライアントで動作確認する

Section titled “9. 認証クライアントで動作確認する”

NSS、PAM の順に確認する。

Terminal window
getent passwd ldapuser
id ldapuser
getent group developers

成功の目安は、次のように LDAP ユーザーが Linux ユーザーとして解決されること。

ldapuser:x:10000:10000:LDAP User:/home/ldapuser:/bin/bash
uid=10000(ldapuser) gid=10000(developers) groups=10000(developers)

認証クライアントへ LDAP ユーザーでログインできることを確認する。SSH、コンソールログイン、 su など、実際に使うログイン経路で確認する。

Terminal window
su - ldapuser

成功の目安は、LDAP ユーザーのパスワードでログインでき、必要ならホームディレクトリが作成されること。



  • nslcd.conf(5) nslcd の LDAP URI、Base DN、Bind、TLS、検索フィルター設定。

  • nslcd(8) nslcd デーモンの役割。

  • nsswitch.conf(5) NSS の参照先と順序。

  • pam(8) PAM の認証、アカウント、パスワード、セッション処理。