OpenLDAP : LDAP サーバーの構築
![](https://korodes.com/wp-content/uploads/2020/01/profile.jpg)
LDAP サーバーの構築
OpenLDAPによるディレクトリサービスであるLDAP サーバーの構築を行います。
今回は次のようなイメージで構築します。
Server #1 LDAPサーバー
・Linux : Debian11.x
・IPアドレス : 192.168.11.100
・ホスト : koro.korochan.com
Server #2 LDAPクライアントサーバー
・Linux : Debian11.x
・IPアドレス : 192.168.11.200
1. LDAPサーバー側設定
1.1 OpenLDAP インストール
1 |
# apt -y install slapd ldap-utils |
インストール過程でLDAP管理者パスワードを設定します
![](https://korodes.com/wp-content/uploads/2023/02/73e34336afc2c09a2dd8d80c188ba394.jpg)
![](https://korodes.com/wp-content/uploads/2023/02/d4a254242d34331636b062a4373dffce.jpg)
1.2 初期設定
1 |
# dpkg-reconfigure slapd |
データベースを作成するので「いいえ」を選択
![](https://korodes.com/wp-content/uploads/2023/02/0ee2a529924e48cdebe2896fa1d482a2.jpg)
任意でよい、今回は"korochan.com"
![](https://korodes.com/wp-content/uploads/2023/02/0e69aa4b194bdc3b6ccff3774427cbec.jpg)
組織名は任意でよい
![](https://korodes.com/wp-content/uploads/2023/02/1d36e0494864b2892aeb9c2bf726f53b.jpg)
管理者のパスワードは「OpenLDAP インストール」で設定したパスワード
![](https://korodes.com/wp-content/uploads/2023/02/c812595665dc520120a4927dc63d5a1b.jpg)
再度同じパスワード入力
![](https://korodes.com/wp-content/uploads/2023/02/6b87c2bf2dbe89a3f761344264ab9ac7.jpg)
「いいえ」を選択
![](https://korodes.com/wp-content/uploads/2023/02/97f3cd2e5931a3d21d2528936e22d8d8.jpg)
「はい」を選択
![](https://korodes.com/wp-content/uploads/2023/02/9f79a1cbf7b23212c172f244ad0adfa7.jpg)
1.3 設定の確認
1 |
# slapcat |
dn: dc=koro,dc=korochan,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: korochan
dc: koro
structuralObjectClass: organization
entryUUID: f61c38d0-3ae4-103d-8d98-e90d1c5dc147
creatorsName: cn=admin,dc=koro,dc=korochan,dc=com
createTimestamp: 20230207034051Z
entryCSN: 20230207034051.034886Z#000000#000#000000
modifiersName: cn=admin,dc=koro,dc=korochan,dc=com
modifyTimestamp: 20230207034051Z
1.4 ユーザー組織とグループ組織の追加
base.ldifを新規作成する
1 |
# vi base.ldif |
記入内容
dn: ou=people,dc=korochan,dc=com
objectClass: organizationalUnit
ou: people
dn: ou=groups,dc=korochan,dc=com
objectClass: organizationalUnit
ou: groups
設定を追加し、反映させる
1 2 3 4 5 |
# ldapadd -x -D cn=admin,dc=koro,dc=korochan,dc=com -W -f base.ldif Enter LDAP Password: ←「OpenLDAP インストール」で設定したパスワード adding new entry "ou=people,dc=koro,dc=korochan,dc=com" adding new entry "ou=groups,dc=koro,dc=korochan,dc=com" ※「people」「groups」が追加された |
1.5 ユーザーを追加
①ユーザーのパスワードを作成
1 2 3 4 |
# slappasswd New password: 新規ユーザー設定するパスワード Re-enter new password: 同上パスワード {SSHA}xxxxxxxxxxxxxxxxxxxxxxxxxx ←このキーを忘れないようにメモをしておく |
➁ldapuser.ldifを新規作成する(今回は新しくユーザーbettyを作成する)
1 |
# vi ldapuser.ldif |
記入内容
dn: uid=betty,ou=people,dc=korochan,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: betty
sn: betty
userPassword: {SSHA}xxxxxxxxxxxxxxxxxxxxxx ←上記で作成したパスワードのキー
loginShell: /bin/bash
uidNumber: 2000
gidNumber: 2000
homeDirectory: /home/betty
dn: cn=betty,ou=groups,dc=korochan,dc=com
objectClass: posixGroup
cn: betty
gidNumber: 2000
memberUid: betty
➂設定を追加・反映させる
1 2 3 4 |
# ldapadd -x -D cn=admin,dc=koro,dc=korochan,dc=com -W -f ldapuser.ldif Enter LDAP Password: ←「OpenLDAP インストール」で設定したパスワード adding new entry "uid=betty,ou=people,dc=korochan,dc=com" adding new entry "cn=betty,ou=groups,dc=korochan,dc=com" |
1.6 参考 : ユーザー・グループ情報の削除
ユーザー情報の削除
1 |
# ldapdelete -x -W -D cn=admin,dc=korochan,dc=com uid=betty,ou=people,dc=korochan,dc=com |
グループ情報の削除
1 |
# ldapdelete -x -W -D cn=admin,dc=korochan,dc=com cn=betty,ou=groups,dc=korochan,dc=com |
2.LDAPサーバークライアント側設定
2.1 LDAPクライアントのインストール
1 |
# apt install libnss-ldapd libpam-ldapd ldap-utils |
LDAPサーバーのIPアドレスを指定する
![](https://korodes.com/wp-content/uploads/2023/02/bc5cff12e44877610e81606092e1e9e8.jpg)
ドメイン名をdc=区切りで記載する
![](https://korodes.com/wp-content/uploads/2023/02/0e69aa4b194bdc3b6ccff3774427cbec.jpg)
passwd, group, shadowにチェックを入れる
![](https://korodes.com/wp-content/uploads/2023/02/4f624a0b8146ebdff83d3959f3c6d255.jpg)
2.2 設定の確認
/etc/nslcd.confの内容を確認する、また、変更したい場合はこの内容を書き換える
1 |
# vi /etc/nslcd.conf |
![](https://korodes.com/wp-content/uploads/2023/02/829f1bb8d480416dd8ce50c800fd5461.jpg)
/etc/nsswitch.confの内容を確認する、また、変更したい場合はこの内容を書き換える
1 |
# vi /etc/nsswitch.conf |
![](https://korodes.com/wp-content/uploads/2023/02/4be061560cab83712e8b707e11d15cfc.jpg)
2.3 動作確認
LDAPクライアントからLDAPサーバーに登録したユーザー(betty)でログインする
Passwordはユーザー(betty)のパスワード
![](https://korodes.com/wp-content/uploads/2023/02/40933b63cbd5e8a4a7c0457d5d31d3f9.jpg)
ログインに成功すると下図のように betty@[LDAPクライアントのホスト名]になる
![](https://korodes.com/wp-content/uploads/2023/02/21d93cc16413ce343b7d09a1bce33bf9.jpg)
LDAP 通信 over SSL/TLS
LDAP 通信を保護する為、SSL/TLS サーバ証明を用いて LDAPS の設定を行います。
今回は自己証明書を作成し利用します。
1.LDAPサーバー側の設定
1.1自己証明書の作成
1 |
# cd /etc/ssl/private |
1 |
# openssl genrsa -aes128 -out server.key 2048 |
作成内容
Ignoring -days; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
Country Name (2 letter code) [AU]:JP 国コード
State or Province Name (full name) [Some-State]:Osaka 都道府県
Locality Name (eg, city) []:Sakai 市町村
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Korodes 組織名
Organizational Unit Name (eg, section) []:Korocahn 部門名
Common Name (e.g. server FQDN or YOUR name) []:koro.korochan.com FQDN
Email Address []:koro@korochan.com 管理者メールアドレス
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
作成されているか確認
1 2 |
# ls server.crt server.csr server.key |
LDAP構成所定ディレクトリーにコピー
1 2 3 |
# cp /etc/ssl/private/server.key /etc/ldap/sasl2/ # cp/etc/ssl/private/server.crt /etc/ldap/sasl2/ # cp/etc/ssl/certs/ca-certificates.crt /etc/ldap/sasl2/ |
SSL設定ファイル新規作成
1 |
# vi ssl.ldif |
dn: cn=config
changetype: modify
add: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/ldap/sasl2/ca-certificates.crt
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/ldap/sasl2/server.crt
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/ldap/sasl2/server.key
設定ファイル有効化
1 2 3 4 5 |
# ldapmodify -Y EXTERNAL -H ldapi:/// -f ssl.ldif SASL/EXTERNAL authentication started SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth SASL SSF: 0 modifying entry "cn=config" |
再起動
1 |
# systemctl restart slapd |
2.クライアント側の設定
# vi /etc/nslcd.conf
1 2 3 |
29行目 : 追記 ssl start_tls tls_reqcert allow |
1 |
# systemctl restart nslcd |
LDAPサーバーで作成したユーザーでログインすると下記のようにログインできる
![](https://korodes.com/wp-content/uploads/2023/02/40933b63cbd5e8a4a7c0457d5d31d3f9.jpg)
![](https://korodes.com/wp-content/uploads/2023/02/21d93cc16413ce343b7d09a1bce33bf9.jpg)