LoginSignup
0
0

CentOS7 Virtualbox OpenLDAPを構築したみた

Last updated at Posted at 2023-11-04

はじめに

業務でOpenLDAPを使用することになり、CentOS7でOpenLDAPを構築してみたため、
記事にしました。

以下のサイトから参考にしております。
https://infracollege.vamdemicsystem.black/linux/

以下のサイトも参考にしました。
https://qiita.com/y-araki-qiita/items/6b2dcbf1a39a969d8024

LDAPサーバー側でldap389 ポートを空ける必要があります。

構築手順

1 LDAPサーバー構築

SElinuxを無効にする

vi /etc/selinux/config

SELINUX=disabled

再起動する

reboot

OpenLDAPインストールする

yum -y install openldap-servers openldap-clients

DB設定ファイルをセット

openldapをインストールした時に自動的に作成されたDB_CONFIG.example
のデータベースをコピーしてldapのデータベースを作成する

cp -p /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
chown ldap. /var/lib/ldap/DB_CONFIG

slapdサービスを起動する

systemctl start slapd
systemctl enable slapd

管理者アカウントのパスワード設定

データベース全体の管理者のパスワードになります。

slappasswd

※パスワードは暗号化した文字列で生成されます。
同じパスワードでも時間も含めた暗号化アルゴリズムになっているため、暗号化の文字列になりません。

ldifファイル作成 olcRootPWにslappasswdコマンドで出力されたパスワードを入れる

vi chrootpw.ldif
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxx <slappasswd>で出力したパスワードを設定する>

ldifファイルをldapに追加する
ldapのデータベースにldifファイルの定義内容を流し込んでいます。

ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif

※-Y EXTERNALオプションでrootパスワード入力をスキップ

標準スキーマの追加

ユーザーに持たせる属性を定義したものがスキーマです。
ldapをインストールしたらインストールされる標準的なスキーマをldapに登録する。

以下は一般的ななスキーマになります。

以下のサイトに標準スキーマの説明が記載されていたため、引用しました。
https://qiita.com/leomaro7/items/f87fe43e372d48f739b7

# COSINEとInternet X.500では必須のスキーマ
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
 
# UNIXアカウントを扱うために使用するスキーマ
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
 
# 組織に関連する人を表すオブジェクトクラスを定義するスキーマ
組織で運用する時によく使われる標準的なスキーマ
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif

Managerアカウントパスワード変更

adminアカウントに該当するものです。

slappasswd

olcRootPW に生成したディレクトリマネージャーのパスワードを指定する

vi chdomain.ldif
dn: olcDatabase={1}monitor,cn=config
changetype: modify
replace: olcAccess
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth"
  read by dn.base="cn=Manager,dc=example,dc=com" read by * none

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=example,dc=com

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=example,dc=com

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}**********<Managerアカウントパスワードを入れる>

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to attrs=userPassword,shadowLastChange by
  dn="cn=Manager,dc=example,dc=com" write by anonymous auth by self write by * none
olcAccess: {1}to dn.base="" by * read
olcAccess: {2}to * by dn="cn=Manager,dc=example,dc=com" write by * read

ldapに上記のldifを登録する

ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif

OU作成

vi basedomain.ldif
dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectclass: organization
o: example
dc: example

dn: cn=Manager,dc=example,dc=com
objectClass: organizationalRole
cn: Manager
description: Directory Manager

dn: ou=People,dc=example,dc=com
objectClass: organizationalUnit
ou: People

dn: ou=Group,dc=example,dc=com
objectClass: organizationalUnit
ou: Group

上記のldifをldapに登録する

ldapadd -x -D cn=Manager,dc=example,dc=com -W -f basedomain.ldif

ファイアウォールでldapのポートを許可

firewall-cmd --add-service=ldap --permanent
firewall-cmd --reload

ユーザー作成

slappasswd
vi ldapuser.ldif
dn: uid=test,ou=People,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: test
sn: Linux
userPassword: {SSHA}********<ユーザーのパスワード>
loginShell: /bin/bash
uidNumber: 1000
gidNumber: 1000
homeDirectory: /home/test
 
dn: cn=system,ou=Group,dc=example,dc=com
objectClass: posixGroup
cn: system
gidNumber: 1000
memberUid: test

上記のldifをldapに登録する

ldapadd -x -D cn=Manager,dc=example,dc=com -W -f ldapuser.ldif

testユーザーがレスポンスでかえってくること

ldapsearch -x -b "ou=Group,dc=example,dc=com" cn=system
# system, Group, example.com
dn: cn=system,ou=Group,dc=example,dc=com
objectClass: posixGroup
cn: system
gidNumber: 1000
memberUid: test

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

ldapsearchを試してみた

-D バインドDN ログインユーザー
-b dc=example,dc=comのobjectclassで検索する。

ldapsearch -LLL -x -h localhost -D "cn=Manager,dc=example,dc=com" -w 'パスワード' -b dc=example,dc=com objectclass

出力結果

dn: dc=example,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization

dn: cn=Manager,dc=example,dc=com
objectClass: organizationalRole

dn: ou=People,dc=example,dc=com
objectClass: organizationalUnit

dn: ou=Group,dc=example,dc=com
objectClass: organizationalUnit

dn: uid=test,ou=People,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount

dn: cn=system,ou=Group,dc=example,dc=com
objectClass: posixGroup

まとめ

OpenLDAPについて構築することでイメージを付けることが出来ました。
複数の各サーバーでOSユーザーを作成するよりはOpenLDAPでユーザー作成した方が
認証情報が管理出来て手間が少ないです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0