LoginSignup
0
0

CentOS7.9にdockerでOpenLDAPとphpLDAPadminを導入する

Last updated at Posted at 2022-09-16

はじめに

・OpenLDAPの環境をphpLDAPadminでアカウントを作成し、ホスト含め複数端末にてログインできるように設定するところまでがゴール。
・ググったところあまり見当たらず苦心したのでまとめています。初心者がネットの海を溺れながら探り当てた方法につき過不足があることをご了承ください。
・セキュリティ未考慮です。
・参考画像はありません。すみません。

※2023/10/02追記
playbookにもありましたがクライアント設定のnsswitch.conf編集が抜けてましたので追記。

・LDAPホスト(CentOS7.9)

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]# 

・クライアント(Ubuntu22.04)
20.04でも可能なことを確認しています。
また、CentOSでのクライアント接続手順も記載しています。

root@settingtest3-VirtualBox:/home/settingtest3/デスクトップ# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04 LTS"
root@settingtest3-VirtualBox:/home/settingtest3/デスクトップ# 

dockerコンテナ起動

Bashでコンテナ起動。

vi ldap.bash
ldap.bash
SERVICE=ldap-service
HOST_NAME=ldap-host
PASSWORD=adminpass
docker run \
    -p 389:389 \
    --restart=always \
    --name $SERVICE \
    --hostname $SERVICE \
    --env 'LDAP_ORGANISATION=Setumei' \
    --env LDAP_DOMAIN=example.com \
    --env LDAP_ADMIN_PASSWORD=$PASSWORD \
    --detach \
    osixia/openldap
docker run \
    -p 443:443 \
    --restart=always \
    --name ${SERVICE}-admin \
    --hostname ${SERVICE}-admin \
    --link $SERVICE:$HOST_NAME \
    --env PHPLDAPADMIN_LDAP_HOSTS=$HOST_NAME \
    --detach \
    osixia/phpldapadmin
bash ldap.bash

-p が無いと設定が正しくてもホストからでも接続できなくなった
docker-composeはよくわかりませんでした。

phpLDAPadminにてLDAPアカウント作成

  1. https://localhostにて接続。
    上記ldap.bashでたてた場合、以下でログイン。
Login DN:cn=admin,dc=example,dc=com
Password:adminpass
0. ログイン後左ペインのツリーをクリック後、右ペインのCreate a child entryで以下作成

※左ペインのCreate a child entryはなにか一つ作成してRefreshボタンを押さないと出現しない

1. グループディレクトリ作成

Generic: Organisational Unit
名前例:Groups
#のちに作成するPosixGroupのまとめ役としてouを作成

2. ユーザーディレクトリ

Generic: Organisational Unit
名前例:Users
#アカウントのまとめ役

3. LDAPアカウントグループ

Generic: Posix Group
名前例:LDAPgroup

4. LDAPアカウント

Generic: User Account
名前例:ldap
#Login ShellはBashにしておく

アカウントは作成後2つあるように見えることがあるが、Refreshボタンを押すと1つになる。
最初のアカウントはUIDが1000になるが、ローカルのアカウントで被っている可能性が高いので二つ目以降がおすすめ
もしくはGeneric: User Accountではなく、DefaultからObjectClassをCtrl押しながら複数選択しすべて手動入力して作成か

LDAP接続手順 on CentOS with nslcd※LDAPサーバ本体でも可能

①必要パッケージインストール
yum -y install openldap-clients nss-pam-ldapd
②初期設定
authconfig --enableldap --enableldapauth --ldapserver=127.0.0.1 --ldapbasedn="dc=example,dc=com" --enablemkhomedir --update

#basednとアドレスを書き換えて実行
authconfigで変更するとpam.dのconfやnsswitch.confなどの設定が上書きされるため初回以降は注意

③uriとbaseを設定 ※④参考
vi /etc/openldap/ldap.conf

binddnとbindpwはssh/suするだけなら不要

④nslcd.conf編集
vi /etc/nslcd.conf

#以下例のようにuri、base、binddn、bindpwを追記する
 base dc=example,dc=com
 uri ldap://10.0.2.114
 binddn cn=admin,dc=example,dc=com
 bindpw adminpass

⑤nslcdサービススタート
systemctl restart nslcd
systemctl enable nslcd
⑥nslcd正常性確認
getent passwd
#まだ表示されない
systemctl status nslcd
#エラーが発生していないか確認(getentなどで接続を試みないとエラーが表示されない可能性あり)

Can't contact LDAP server なんちゃら...のエラーが出る場合は設定が足りない可能性あり

⑦nsswitch.conf編集
vi /etc/nsswitch.conf

#passwd:shadow:group:に ldapを追記。sssはそのままでもok
==============
passwd: files systemd ldap
group: files systemd ldap
shadow: files ldap
==============

⑧LDAPアカウントが表示されるか確認
getent passwd

#LDAPアカウントが一番下に*付きで表示されることを確認
表示されない場合はsystemctl status nslcdでエラー内容を見てみる

⑨suでログインできるか確認
su ldap

#ログイン後「bash: /home/users/****/.bashrc: 許可がありません」と表示された場合は、UIDがサーバ本体の他アカウントなどと被っている可能性
sh-4.2$ と表示される場合はLoginShellが/bin/bashに指定されていない可能性

⑩sshでログインできるようにしたい場合
vi /etc/pam.d/password-auth
vi /etc/pam.d/password-auth-ac

#sssをldapに置換 :s/sss/ldap/g
念のためpassword-auth-acも同じようにldapに置換

⑪sshできるか確認
ssh ldap@localhost

LDAP接続手順 on Ubuntu with pam(?)

①必要パッケージインストール
apt install libnss-ldap libpam-ldap ldap-utils

#ldap://アドレス 、デフォルトはldapi:///になっていることに注意
ドメインを入力、no,noを選択

②/ldap.conf編集
vi /etc/ldap.conf

#以下例のようにbinddn,bindpwを入力し、
 binddn cn=admin,dc=example,dc=com
 bindpw adminpass
その他設定がLDAPサーバの設定とあっているか確認。
 base dc=example,dc=com
 uri ldap://10.0.2.114
 ldap_version 3

③nsswitch.conf編集
vi /etc/nsswitch.conf

#passwd:shadow:group:に ldapを追記。
==============
passwd: files systemd ldap
group: files systemd ldap
shadow: files ldap
==============

③接続できるか確認
getent passwd
su ldap

#LDAPアカウントが見える、ログインできることを確認

参考文献(勝手に)

雑記

・筆者はLDAPについて、PAMについて、ログイン認証の仕組みについてなどよく理解していません。
・basednもbinddnも同じでとりあえず問題なし。
・dockerの-pオプションはdocker updateコマンド未対応で--ENVオプションでもないので後付けはできないハズ。
・おまけ① centos7.9でのdockerインストール手順

#参考:https://docs.docker.jp/engine/installation/linux/docker-ce/centos.html
yum install -y yum-utils 
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin 
systemctl enable docker 
systemctl start docker

・おまけ② AnsibleでのLDAP設定Playbook※Ubuntu18.04以降

    - name: apt install libnss-ldap libpam-ldap ldap-utils
      apt:
        name:
          - libnss-ldap
          - libpam-ldap
          - ldap-utils

    - name: "/etc/ldap.conf.org.d/ の存在確認"
      stat:
        path: "/etc/ldap.conf.org.d/"
      register: ldap_conf_reg

    - name: "ldap.confをバックアップ"
      synchronize:
        src: "/etc/"
        dest: "/etc/ldap.conf.org.d/"
        rsync_opts:
             - "--include=ldap.conf"
             - "--exclude=*"
        times: no #タイムスタンプ無視
        checksum: yes #タイムスタンプ無視
      delegate_to: "{{ inventory_hostname }}"
      when: ldap_conf_reg.stat.exists == false

    - name: "ldap.conf編集_uri"
      lineinfile:
        path: "/etc/ldap.conf"
        regexp: "^uri ldap"
        line: "uri ldap://10.0.2.114/"

    - name: "ldap.conf編集_base dc=example,dc=com"
      lineinfile:
        path: "/etc/ldap.conf"
        regexp: "^base dc="
        line: "base dc=example,dc=com"

    - name: "ldap.conf編集_ldap_version 3"
      lineinfile:
        path: "/etc/ldap.conf"
        regexp: "^ldap_version"
        line: "ldap_version 3"

    - name: "ldap.conf編集_binddn,bindpw"
      blockinfile:
        path: "/etc/ldap.conf"
        block: |
          binddn cn=admin,dc=example,dc=com
          bindpw adminpass

    - name: "/etc/nsswitch.conf.org.d/ の存在確認"
      stat:
        path: "/etc/nsswitch.conf.org.d/nsswitch.conf"
      register: nsswitch_conf_reg

    - name: "nsswitch.confをバックアップ"
      synchronize:
        src: "/etc/"
        dest: "/etc/nsswitch.conf.org.d/"
        rsync_opts:
             - "--include=nsswitch.conf"
             - "--exclude=*"
        times: no #タイムスタンプ無視
        checksum: yes #タイムスタンプ無視
      delegate_to: "{{ inventory_hostname }}"
      when: nsswitch_conf_reg.stat.exists == false

    - name: "nsswitch.conf編集_shadow"
      lineinfile:
        path: "/etc/nsswitch.conf"
        regexp: "^shadow:         "
        line: "shadow:         files ldap"

    - name: "nsswitch.conf編集_passwd"
      lineinfile:
        path: "/etc/nsswitch.conf"
        regexp: "^passwd:         "
        line: "passwd:         files systemd ldap"

    - name: "nsswitch.conf編集_group"
      lineinfile:
        path: "/etc/nsswitch.conf"
        regexp: "^group:          "
        line: "group:          files systemd ldap"

以上

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