LoginSignup
29
28

More than 3 years have passed since last update.

LDAPクラインアントからLDAPサーバへログインしよう

Last updated at Posted at 2018-04-30
  • アジェンダ
    • openldap 2.4をインストールし, LDAPサーバの基本設定をする
    • ldapaddでLDAPユーザを作成する
    • LDAPクライアントからSSHログインする
  • サーバ環境(LDAPサーバ/LDAPクライアント共通)
    • CentOS Linux release 7.6.1810
    • openldap-servers-2.4.44-21.el7_6.x86_64
    • openldap-clients-2.4.44-21.el7_6.x86_64

2020/5/5 NEW!
この記事が23000viewと意外にも多くの方に読んでいただいているようなので, GW中に大幅加筆修正しました. あと以下の記事も投稿してみました. phpLDAPadminというWeb U/Iを使ってユーザ管理をしています.
[openldap 2.4]ldapadd, ldapmodify, ldapsearch, phpldapadminでユーザ追加, LDAPユーザアカウントでSSHログイン

LDAPサーバセットアップ

openldapインストール

  • yumでopenldapの関連pkgをインストール
    • openldap-serversopenldap-clientsをインストールする
    • 依存関係でlibtool-ltdlというpkgもインストールされる
# yum install openldap-servers openldap-clients
~
~
Dependencies Resolved

===============================================================================================================
 Package                        Arch                 Version                          Repository          Size
===============================================================================================================
Installing:
 openldap-clients               x86_64               2.4.44-21.el7_6                  base               190 k
 openldap-servers               x86_64               2.4.44-21.el7_6                  base               2.2 M
Installing for dependencies:
 libtool-ltdl                   x86_64               2.4.2-22.el7_3                   base                49 k

Transaction Summary
===============================================================================================================
Install  2 Packages (+1 Dependent package)

Total download size: 2.4 M
Installed size: 5.7 M
Is this ok [y/d/N]:
  • インストールしたpkgのバージョン確認
# rpm -qa | grep openldap*
openldap-2.4.44-21.el7_6.x86_64
openldap-clients-2.4.44-21.el7_6.x86_64
openldap-servers-2.4.44-21.el7_6.x86_64

LDAPサーバ基本設定

  • slapd起動前にDBのチューニング設定をする
    • slapdのDBファイルは/var/lib/ldap配下に作成される
    • /var/lib/ldap配下にDBチューニングのための設定ファイルを配置する
    • サンプルファイル(DB_CONFIG.example)が準備されているため, そのままコピーして利用する
# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/
# ll /var/lib/ldap/
total 4
-rw-r--r-- 1 root root 845 May  4 16:46 DB_CONFIG.example
  • slapd起動/自動起動設定
# systemctl start slapd
# systemctl enable slapd

こんな感じ↓で/usr/sbin/slapdが稼動していたらok.

# ps aux | grep slapd
ldap      5178  0.0  1.6 114980  8424 ?        Ssl  16:49   0:00 /usr/sbin/slapd -u ldap -h ldapi:/// ldap:///

LDAP DB設定

openldapの設定は設定ファイルをテキストエディタで直接編集するのではなく, ldapadd, ldapmodify等LDAPクライアントユーティリティを利用する.

  • ldapsearchでデフォルト設定を確認
# ldapsearch -LLL -Y EXTERNAL -H ldapi:/// -b 'olcDatabase={2}hdb,cn=config'
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
dn: olcDatabase={2}hdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcHdbConfig
olcDatabase: {2}hdb
olcDbDirectory: /var/lib/ldap
olcSuffix: dc=my-domain,dc=com
olcRootDN: cn=Manager,dc=my-domain,dc=com
olcDbIndex: objectClass eq,pres
olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
  • LDAPディレクトリツリーのDN(olcSuffix)設定変更

デフォルト設定
olcSuffix: dc=my-domain,dc=com

  • 管理用の特権DN(olcRootDN)設定変更

デフォルト設定
olcRootDN: cn=Manager,dc=my-domain,dc=com

  • 特権DNのパスワード(olcRootPW)設定変更

特権DNのパスワードはslappasswdコマンドで生成
*ソルト付きのハッシュパスワードが生成される

# slappasswd
New password:
Re-enter new password:
{SSHA}xxxx

作業用ディレクトリ配下にhdb-init.ldifファイルを作成する

hdb-init.ldif
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp

-

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp

-

dn: olcDatabase={2}hdb,cn=config
add: olcRootPW
olcRootPW: {SSHA}xxxx

ldapmodifyコマンドでldifファイルを指定し, 設定変更

# ldapmodify -Y EXTERNAL -H ldapi:/// -f hdb-init.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={2}hdb,cn=config"

modifying entry "olcDatabase={2}hdb,cn=config"

modifying entry "olcDatabase={2}hdb,cn=config"

こんな感じ↓でDB設定が変更される

# ldapsearch -LLL -Y EXTERNAL -H ldapi:/// -b 'olcDatabase={2}hdb,cn=config'
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
dn: olcDatabase={2}hdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcHdbConfig
olcDatabase: {2}hdb
olcDbDirectory: /var/lib/ldap
olcDbIndex: objectClass eq,pres
olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
olcRootPW: {SSHA}xxxx
olcSuffix: dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
olcRootDN: cn=Manager,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp

LDAPスキーマ登録

LDAPスキーマはLDAP DBのエントリに登録できるデータの種類/型を定義する.
/etc/openldap/schema/配下にスキーマ登録に利用するldifファイルがある.

# ls /etc/openldap/schema/
collective.ldif    core.schema     dyngroup.ldif         java.schema  openldap.ldif    ppolicy.schema
collective.schema  cosine.ldif     dyngroup.schema       misc.ldif    openldap.schema
corba.ldif         cosine.schema   inetorgperson.ldif    misc.schema  pmi.ldif
corba.schema       duaconf.ldif    inetorgperson.schema  nis.ldif     pmi.schema
core.ldif          duaconf.schema  java.ldif             nis.schema   ppolicy.ldif

デフォルトではcoreスキーマしか利用できないため, nis, cosineスキーマを追加登録する
*nisはユーザを管理するためのスキーマ. nisの依存でcosineも必要になる.

  • cosine, nis, inetOrgPersonスキーマの登録
# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=cosine,cn=schema,cn=config"

# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=nis,cn=schema,cn=config"

# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=inetorgperson,cn=schema,cn=config"

LDAPエントリ登録

  • 基本組織, 特権DN, ユーザアカウント組織, グループ組織を登録
base.ldif
dn: dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
objectClass: dcObject
objectClass: organization
dc: xxxx
o: xxxx

-

dn: cn=Manager,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
objectclass: organizationalRole
cn: Manager

-

dn: ou=People,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
objectClass: organizationalUnit
ou: People

-

dn: ou=Group,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
objectClass: organizationalUnit
ou: Group

dn: dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp・・・xxxx.vs.sakura.ne.jp組織の基本となるエントリ
dn: cn=Manager,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp・・・特権DNのエントリ
dn: ou=People,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp・・・ユーザを管理する組織単位
dn: ou=Group,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp・・・グループを管理する組織単位

毎度のことながらldapaddコマンドでLDAPエントリを登録.

# ldapadd -x -D "cn=Manager,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp" -W -f b
ase.ldif
Enter LDAP Password:
adding new entry "dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp"

adding new entry "cn=Manager,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp"

adding new entry "ou=People,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp"

adding new entry "ou=Group,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp"

ちゃんとエントリが登録されたか確認.

# ldapsearch -x -LLL -b "dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp" "(objectCl
ass=*)"
dn: dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
objectClass: dcObject
objectClass: organization
dc: xxxx
o: xxxx

dn: cn=Manager,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
objectClass: organizationalRole
cn: Manager

dn: ou=People,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
objectClass: organizationalUnit
ou: People

dn: ou=Group,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
objectClass: organizationalUnit
ou: Group

LDAPユーザ追加

グループアカウントの作成

グループアカウントを作成する時も1. ldifファイル作成, 2. ldapaddで追加の流れは一緒.

  • グループアカウント用のldifファイル作成
group-toystory2.ldif
dn: cn=toystory2,ou=Group,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
cn: toystory2
gidNumber: 501
objectClass: posixGroup
objectClass: top
  • ldapaddコマンドでグループアカウントエントリ登録
# ldapadd -x -D "cn=Manager,dc=xxx,dc=vs,dc=sakura,dc=ne,dc=jp" -W -f group-toystory2.ldif
Enter LDAP Password:
adding new entry "cn=toystory2,ou=Group,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp"
  • ldapsearchでグループが作成できたか確認
# ldapsearch -x -LLL -b "dc=xxx,dc=vs,dc=sakura,dc=ne,dc=jp" "(gidNumber=501)"
dn: cn=toystory2,ou=Group,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
cn: toystory2
gidNumber: 501
objectClass: posixGroup
objectClass: top

ユーザアカウントの作成

  • ユーザアカウント用のldifファイル作成

userPasswordはslappasswdコマンドで生成した文字列を埋め込む.

user-bullseye.ldif
dn: cn=bullseye,ou=People,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
givenName: bullseye
sn: bullseye
cn: bullseye
uid: bullseye
userPassword: {SSHA}xxxx
gidNumber: 500
homeDirectory: /home/bullseye
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: top
uidNumber: 1004
loginShell: /bin/bash
  • ldapaddコマンドでユーザアカウントエントリ登録
# ldapadd -x -D "cn=Manager,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp" -W -f user-bullseye.ldif
Enter LDAP Password:
adding new entry "cn=bullseye,ou=People,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp"
  • ldapsearchでユーザが作成できたか確認
# ldapsearch -x -LLL -b "dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp" "(uid=bullseye)"
dn: cn=bullseye,ou=People,dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp
givenName: bullseye
sn:: YnVsbHNleWUg
cn: bullseye
uid: bullseye
userPassword:: xxxx
gidNumber: 501
homeDirectory: /home/bullseye
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: top
uidNumber: 1004
loginShell: /bin/bash

LDAPユーザーストア設定

  • nss-pam-ldapdインストール

nss-pam-ldapdはユーザがローカルのldapクエリを実行できるようにするローカルldapネームサービス.

# yum install nss-pam-ldapd
# rpm -qa | grep nss-pam-ldapd
nss-pam-ldapd-0.8.13-22.el7.x86_64
  • authconfigコマンドでLDAPユーザストアの接続情報を設定
# authconfig --enableldap --enableldapauth --ldapserver=xxx.xxx.xxx.xxx --ldapbasedn="dc=xxxx,dc=vs,dc=sakura,dc=ne,dc=jp" --update

ホームディレクトリ自動作成設定

  • oddjob-mkhomedirインストール

oddjob-mkhomedirはホームディレクトリ作成のoddjobヘルパー.

# yum install oddjob-mkhomedir
# rpm -qa | grep oddjob-mkhomedir
oddjob-mkhomedir-0.31.5-4.el7.x86_64
  • authconfigコマンドでホームディレクトリ作成を有効化
# authconfig --enablemkhomedir --update
  • oddjobd起動/自動起動設定
# systemctl start oddjobd
# systemctl enable oddjobd

LDAPクライアントからSSHログイン

  • ひとまず作成したLDAPユーザアカウントにユーザ切り替えできるか確認
# su - bullseye
Creating home directory for bullseye.

Creating home directory for woody.と表示されたので, ホームディレクトリが自動作成できたようです. pwd, idコマンドで現在位置, ユーザ情報を確認してみる.

$ pwd
/home/bullseye
$ id bullseye
uid=1004(bullseye) gid=501(toystory2) groups=501(toystory2)

*/etc/nsswitch.confの設定を見てみると, 下記のように書換えされていました.

/etc/nsswitch.conf
# diff -u /etc/nsswitch.conf.bak /etc/nsswitch.conf
--- /etc/nsswitch.conf.bak      2019-04-10 01:36:08.000000000 +0900
+++ /etc/nsswitch.conf  2020-05-05 11:24:02.929418319 +0900
@@ -30,13 +30,13 @@
 #shadow:    db files nisplus nis
 #group:     db files nisplus nis

-passwd:     files sss
-shadow:     files sss
-group:      files sss
+passwd:     files sss ldap
+shadow:     files sss ldap
+group:      files sss ldap
 #initgroups: files sss

 #hosts:     db files nisplus nis dns
-hosts:      files dns
+hosts:      files dns myhostname

 # Example - obey only what nisplus tells us...
 #services:   nisplus [NOTFOUND=return] files
@@ -55,10 +55,10 @@
 rpc:        files
 services:   files sss

-netgroup:   nisplus sss
+netgroup:   files sss ldap

 publickey:  nisplus

-automount:  files nisplus sss
+automount:  files ldap
 aliases:    files nisplus
  • LDAPクライアントからSSHログイン
$ ssh bullseye@xxxx.vs.sakura.ne.jp
~
~
$ pwd
/home/bullseye
29
28
2

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
29
28