如何設置 OpenLDAP 服務器和驗證客戶端工作站

LDAP 或輕量級目錄訪問協議允許任何人定位並連接到組織、人員和其他資源,如網絡(公共/私有)中的文件和設備。 LDAP 遵循 X.500 標準,這是網絡中目錄服務的標準,通常使用通常的客戶端/服務器範例。 LDAP 較輕,因為在其初始版本中,它不包含安全功能。 目錄服務的主要用途是將用戶和對像數據存儲在一個中央系統中,並使這些數據可用於其他應用程序,主要用於身份驗證或作為地址簿,我們可以使用 OpenLDAP 服務器來實現這一點。 本文介紹瞭如何在 Ubuntu 16.04 中使用輕量級目錄訪問協議設置 OpenLDAP 服務器和驗證客戶端工作站

本教程介紹瞭如何在 Ubuntu 16.04 中使用輕量級目錄訪問協議設置 OpenLDAP 服務器和驗證客戶端工作站。

LDAP 的用途

→LDAP 將用戶和其他網絡對象保存在中央數據庫中。

→LDAP 在中央數據庫中存儲諸如純文本信息、圖像、二進制數據、公鑰證書等信息。

→LDAP 提供登錄管理等身份驗證和授權服務。

→LDAP 還可以在其數據庫中存儲 DNS 記錄。

→LDAP 可用作任何組織的黃頁目錄服務。

LDAP 術語

LDAP 術語是 X.500 目錄規範的一部分,它定義了 LDAP 目錄中的節點。

CN通用名
L 地名
ST州或省名
O 組織名稱
OU組織單位名稱
C國名
街道街道地址
DC域組件
用戶名
DN 專有名稱

最後一個,即 DN(專有名稱)是一系列以逗號分隔的鍵/值對,用於在目錄層次結構中唯一地標識條目。 DN 實際上是條目的完全限定名稱。 例如,字符串“CN=India,OU=Distribution Groups, DC=gp, DC=gl, DC=linoxide, DC=com”是來自稱為層次結構的路徑 目錄信息樹 並且應該從右(根)到左(葉)讀取。

在本文中,我們將在 Ubuntu 16 中設置 OpenLDAP 服務器並配置 OpenLDAP 客戶端,該客戶端將從服務器檢索登錄憑據並對用戶進行身份驗證。

IP地址
OpenLDAP 服務器
10.0.0.196
OpenLDAP 客戶端
10.0.0.33

安裝 OpenLDAP 服務器

使用 apt-get 安裝 OpenLDAP 及其實用程序並在啟動期間啟用它。 安裝時,它會要求提供 admin 密碼。

# sudo apt-get update
# apt-get install slapd ldap-utils
# systemctl enable slapd

使用 netstat,檢查 slapd 是否在 389 端口運行

# netstat -pltn

默認情況下,Ubuntu 16 附帶防火牆 UFW。 如果啟用了 UFW,則使用以下命令打開 389 號端口。

# sudo ufw allow 389/tcp
# sudo ufw reload

OpenLDAP 軟件包已安裝,現在我們將重新配置 ubuntu 附帶的所有默認設置。 執行以下命令,調出包配置工具。

# sudo dpkg-reconfigure slapd

包配置工具會詢問重新配置 OpenLDAP 的一系列問題

→省略 OpenLDAP 服務器配置? <否>

→DNS域名:linoxide.com

→組織名稱:linoxide

→Enter 密碼並確認:密碼

→要使用的數據庫後端:HDB

→您是否希望在清除 slapd 時刪除數據庫? <否>

→移動舊數據庫? <是>

→允許LDAPv2協議? <否>

重啟 OpenLDAP

# systemctl restart slapd

你可以改變 admin 稍後使用以下命令為 OpenLDAP 設置密碼。

# ldappassword

在這個階段,我們已經安裝並重新配置了 OpenLDAP 服務器。 查找條目 admin 在 OpenLDAP 數據庫中,我們將使用 ldapsearch 命令。 ldapsearch 將提示輸入 admin 我們在重新配置 OpenLDAP 期間提供的密碼。

# ldapsearch -x -W -D cn=admin,dc=linoxide,dc=com -b dc=linoxide,dc=com -LLL

Enter LDAP Password:
dn: dc=linoxide,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: linoxide
dc: linoxide

dn: cn=admin,dc=linoxide,dc=com
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator
userPassword:: e1NTSEF9MkdIK2p1enlxQ3hFNmtMSE56TUE5NzZzOFQxVGdxSUE=

添加組織單位 (OU)

要添加 OU,我們將創建一個 LDIF(LDAP 數據交換格式)文件,該文件是設計用於從 LDAP 服務器交換信息的標准文本格式。 添加名為“組”的組織單位。

# vi ou_group.ldif

dn: ou=groups,dc=linoxide,dc=com
changetype: add
objectClass: organizationalUnit
objectClass: top
ou: groups

我們將使用 ldapadd 添加上述組織單位。

# ldapadd -W -D "cn=admin,dc=linoxide,dc=com" -f ou_group.ldif

修改組織單位 (OU)

要修改組織單位,請創建一個包含以下內容的 ldif 文件。 在這 example,我們正在向現有 OU 添加郵政編碼條目。

# vi modify_ou.ldif
dn: ou=groups,dc=linoxide,dc=com
changetype: modify
add: postalCode
postalCode: 788109
-

使用 ldapmodify 修改 OU

# ldapmodify -x -W -D "cn=admin,dc=linoxide,dc=com" -f modify_ou.ldif
Enter LDAP Password:
modifying entry "ou=groups,dc=linoxide,dc=com"

刪除組織單位 (OU)

要刪除組織單位,請使用 ldapdelete 指定 OU 的專有名稱

# ldapdelete -W -D "cn=admin,dc=linoxide,dc=com" "ou=groups,dc=linoxide,dc=com"
Enter LDAP Password:

添加群組

要添加 posix 組,我們將為它創建一個 LDIF 文件。

# vi irc_users.ldif
dn: cn=ircusers,ou=groups,dc=linoxide,dc=com
objectClass: posixGroup
objectClass: top
cn: ircusers
gidNumber: 4000

像以前一樣使用 ldapadd 命令添加組

# ldapadd -x -W -D "cn=admin,dc=linoxide,dc=com" -f irc_users.ldif
Enter LDAP Password:
adding new entry "cn=ircusers,ou=groups,dc=linoxide,dc=com"

修改組

定義用於修改組的 ldif 文件,我們將為現有的 ircusers 組添加“描述”。

# vi modify_irc_users.ldif
dn: cn=ircusers,ou=groups,dc=linoxide,dc=com
changetype: modify
add: description
description: Groups under OU

使用 ldapmodify 修改組

# ldapmodify -x -W -D "cn=admin,dc=linoxide,dc=com" -f modify_irc_users.ldif
Enter LDAP Password:
modifying entry "cn=ircusers,ou=groups,dc=linoxide,dc=com"

刪除群組

要刪除組,請使用 ldapdelete 指定組的專有名稱。

# ldapdelete -W -D "cn=admin,dc=linoxide,dc=com" "cn=ircusers,ou=groups,dc=linoxide,dc=com"
Enter LDAP Password:

添加用戶

首先使用slappasswd為用戶生成SSHA密碼

# slappasswd -h {SSHA} -s mypass
{SSHA}d9NeiNx4RLSEtXNuMxq7+jWK/5yxwCWT

接下來為用戶創建一個 ldif 文件

# vi mike_user.ldif
dn: uid=mike,ou=groups,dc=linoxide,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: mike
sn: smith
givenName: mike
cn: mike
uidNumber: 4000
gidNumber: 4000
userPassword: {SSHA}d9NeiNx4RLSEtXNuMxq7+jWK/5yxwCWT
loginShell: /bin/bash
homeDirectory: /home/mike

確保提供正確的組 ID 號 (gidNumber),在我們的示例中為 4000。 使用 ldapadd 命令添加上述用戶。

# ldapadd -x -W -D "cn=admin,dc=linoxide,dc=com" -f mike_user.ldif
Enter LDAP Password:
adding new entry "uid=mike,ou=users,dc=linoxide,dc=com"

刪除用戶

要刪除用戶,請使用 ldapdelete 命令

# ldapdelete -W -D "cn=admin,dc=linoxide,dc=com" "uid=mike,ou=groups,dc=linoxide,dc=com"

使用以下命令檢查條目是否已被刪除。

# ldapsearch -x -b "dc=linoxide,dc=com"

修改用戶

要修改用戶,請創建一個 ldif 文件,然後使用 ldapmodify 來實現它。

# vi modify_mike.ldif
dn: uid=mike,ou=groups,dc=linoxide,dc=com
changetype: modify
replace: smith
sn: smt
-
add: title
title: Grand Poobah
-
add: jpegPhoto
jpegPhoto: /tmp/smith.png

現在執行 ldapmodify 命令

# ldapmodify -x -W -D "cn=admin,dc=linoxide,dc=com" -f modify_mike.ldif
Enter LDAP Password:
modifying entry "uid=mike,ou=users,dc=linoxide,dc=com"

搜索 OpenLDAP 數據庫

從服務器本身,您現在可以檢查是否可以讀取數據庫。 下面的命令將轉儲整個目錄。

# ldapsearch -x -LLL -H ldap:/// -b dc=linoxide,dc=com
dn: dc=linoxide,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: linoxide
dc: linoxide

dn: cn=admin,dc=linoxide,dc=com
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator

dn: ou=groups,dc=linoxide,dc=com
objectClass: organizationalUnit
objectClass: top
ou: groups

dn: ou=users,dc=linoxide,dc=com
objectClass: organizationalUnit
objectClass: top
ou: users

dn: cn=dbagrp,ou=groups,dc=linoxide,dc=com
objectClass: top
objectClass: posixGroup
gidNumber: 678
cn: dbagrp

dn: cn=ircusers,ou=groups,dc=linoxide,dc=com
objectClass: posixGroup
objectClass: top
cn: ircusers
gidNumber: 4000

dn: uid=mike,ou=users,dc=linoxide,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: mike
sn: smith
givenName: mike
cn: mike
uidNumber: 4000
gidNumber: 4000
loginShell: /bin/bash
homeDirectory: /home/mike

使用 phpMyAdmin

我們已經通過命令行創建/編輯/搜索 OU、組、用戶。 但是,您可以使用名為 phpldapadmin 的 Web 界面執行相同操作。 默認情況下,phpldapadmin 與 ubuntu 一起提供。 使用 apt-get 安裝它。

# sudo apt-get install phpldapadmin

編輯 phpldapadmin 的配置文件以反映我們之前創建的目錄結構。

# vi /etc/phpldapadmin/config.php
$servers->setValue('server','name','My LDAP Server');
$servers->setValue('server','host','LDAP-SERVER-IP');
$servers->setValue('server','base',array('dc=linoxide,dc=com'));
$servers->setValue('login','auth_type','session');
$servers->setValue('login','bind_id','cn=admin,dc=linoxide,dc=com');

您現在可以通過 https://LDAP-SERVER-IP/phpldapadmin 訪問 phpldapadmin。 使用用戶作為默認目錄結構和密碼作為“密碼”登錄。 要密碼保護 phpldapadmin 位置,請使用 apache utils htpasswd 創建一個用戶。

# sudo htpasswd -c /etc/apache2/htpasswd ldapadminuser
New password:
Re-type new password:
Adding password for user ldapadminuser

在apache的主配置文件/etc/apache2/apache2.conf中追加以下部分

# vi /etc/apache2/apache2.conf

<Location /phpldapadmin>
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/apache2/htpasswd
Require valid-user
</Location>

重新開始 Apache

# systemctl restart apache2

刷新 phpldapadmin 頁面,您將看到您使用 htpasswd utils 配置的密碼提示。

安裝 OpenLDAP 客戶端

我們將在客戶端機器上安裝幾個軟件包,以使用 OpenLDAP 服務器正確地進行身份驗證。

# apt-get install ldap-auth-client nscd

您將被問到一系列與服務器配置期間所問的問題類似的問題。

→ LDAP 服務器統一資源標識符:ldap://10.0.0.196

→ 搜索庫的專有名稱:dc=linoxide,dc=com

→LDAP 版本:3

→製作本地根數據庫 admin: <是>

→LDAP 數據庫需要登錄嗎? <否>

→root 的 LDAP 帳戶:cn=admin,dc=linoxide,dc=com

→LDAP root賬號密碼:密碼

您始終可以通過在終端中執行以下命令來更改配置。

#  sudo dpkg-reconfigure ldap-auth-config

配置 OpenLDAP 客戶端

我們需要編輯文件 /etc/nsswitch.conf 以通知身份驗證文件有關 OpenLDAP 服務器的存在。 編輯 /etc/nsswitch.conf 文件並將以 passwd、group、shadow 開頭的行修改為如下所示。

# vi /etc/nsswitch.conf
passwd:         ldap compat
group:            ldap compat
shadow:         ldap compat

編輯 /etc/pam.d/common-session 和文件末尾的以下行。

# vi /etc/pam.d/common-session
....................
....................

session required        pam_mkhomedir.so skel=/etc/skel umask=0022

使用帶有 ldap 的 auth-client-config 設置 nss

# auth-client-config -t nss -p lac_ldap
# cd /usr/share/pam-configs/
# vi mkhomedir

Name: Create home directory on login for Linoxide
Default: yes
Priority: 0
Session-Type: Additional
Session-Interactive-Only: yes
Session:
required                        pam_mkhomedir.so umask=0022 skel=/etc/skel

當 LDAP 用戶登錄並且沒有主目錄時,上述文件的最後一行將在客戶端計算機上創建一個主目錄。 現在更新 pam 身份驗證。

# pam-auth-update

啟用“登錄時創建主目錄……”行並選擇“確定”。 重新啟動 nscd。

# /etc/init.d/nscd restart
[ ok ] Restarting nscd (via systemctl): nscd.service.

使用 getent 列出密碼文件的條目。 該列表將包括我們之前在服務器中創建的 LDAP 用戶“mike”。

# getent passwd
mike:x:4000:4000:mike:/home/mike:/bin/bash

如果您之前沒有安裝 SSH,請使用 SSH 安裝它。

# apt-get install ssh

確保您已在 /etc/ssh/sshd_config 中將以下內容設置為 yes

PermitRootLogin yes
UsePAM yes

使用 SSH 連接到 LDAP 服務器

# ssh [email protected]

獲取邁克外殼的另一種方法是使用 sudo 在客戶端機器中。

# su - mike
mike@ip-10-0-0-33:~$

在配置 OpenLDAP 服務器時,我們創建了 LDAP 管理員,其專有名稱為“cn=admin,dc=linoxide,dc=com” 這個值 admin 與 admin 默認情況下,Ubuntu 中存在的組。 我們創建的 LDAP 用戶 admin 組將有權訪問 sudo 命令,因為 /etc/sudoers 文件中有一個條目,如下所示 –

%admin ALL=(ALL) ALL

撤銷訪問 sudo 為了 admin 組,通過在行的開頭放置一個散列來註釋上面的行。 你也可以授予 sudo 通過將 %user ALL=(ALL) ALL 添加到 /etc/sudoers 文件來訪問特定用戶。

結論

使用 OpenLDAP 服務器的優點是可以將整個組織的信息放在一個中央存儲庫中。 LDAP 可用作可從網絡上的任何位置訪問的中央目錄,而不是單獨管理每個組的用戶。 此外,LDAP 支持安全套接字層 (SSL) 和傳輸層安全性 (TLS),因此可以保護敏感數據免遭窺探。 瀏覽 OpenLDAP 文檔 了解有關 OpenLDAP 管理的更多信息。