RHEL 7 配置openldap服務

 

一、服務端配置

 

1、安裝openldap 主程序、openldap-clients 客戶端、openldap-servers 服務端、migrationtools 遷移工具

[root@localhost Desktop]#  yum install openldap openldap-clients openldap-servers migrationtools -y

 

2、生成密鑰文件

[root@localhost Desktop]# slappasswd -s linuxcool -n > /etc/openldap/passwd

[root@localhost Desktop]# cat /etc/openldap/passwd

-s:將後面的明文信息生成一個hash值; -n 略後面的換行符;將憑據導入到其它文件

 

3、定義主機名,名字無所謂,後面用到的時候對應就行

[root@localhost Desktop]# echo "instructor.linuxcool.com" > /etc/hostname

[root@localhost Desktop]# hostname

 

4、因爲LDAP目錄服務是以明文的方式在網絡中傳輸數據的,所以這裏採用TLS加密機制,使用openssl工具生成X509格式的證書文件(有效期爲365天):

[root@localhost Desktop]# openssl req -new -x509 -nodes -out /etc/openldap/certs/cert.pem -keyout /etc/openldap/certs/priv.pem -days 365

 

Generating a 2048 bit RSA private key

......+++

........+++

writing new private key to '/etc/openldap/certs/priv.pem'

-----

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) [XX]:回車(國家名稱)

State or Province Name (full name) []:回車(省名名稱)

Locality Name (eg, city) [Default City]:回車(城市名稱)

Organization Name (eg, company) [Default Company Ltd]:回車(公司名稱)

Organizational Unit Name (eg, section) []:回車(單位名稱)

Common Name (eg, your name or your server's hostname) []:instructor.linuxcool.com  //輸入剛修改的主機名

Email Address []:回車(郵寄地址)

 

5、修改/etc/openldap/certs/目錄下所有文件的權限,讓其它用戶對這個目錄沒有任何權限

[root@localhost Desktop]# chown ldap:ldap /etc/openldap/certs/*

[root@localhost ~]# chmod 600 /etc/openldap/certs/*

[root@localhost Desktop]# ls -l /etc/openldap/certs/

total 8

-rw-------. 1 600 ldap 1314 Oct 11 13:51 cert.pem

-rw-------. 1 600 ldap 1704 Oct 11 13:51 priv.pem

 

  • 配置sladp用來保存數據

6、複製一份sladp的配置文件

[root@localhost certs]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

 

7、生成數據庫文件(初始化),有報錯不用管

[root@localhost Desktop]# slaptest

[root@localhost Desktop]# ls -l /var/lib/ldap/

total 19076

-rw-r--r--. 1 root root     2048 Oct 11 14:09 alock

-rw-------. 1 root root  2416640 Oct 11 14:09 __db.001

-rw-------. 1 root root 17465344 Oct 11 14:09 __db.002

-rw-------. 1 root root  1884160 Oct 11 14:09 __db.003

-rw-r--r--. 1 root root      845 Oct 11 14:08 DB_CONFIG

 

8、修改目錄下所有文件的權限,重啓slapd服務並加入開機自啓動

[root@localhost Desktop]# chown ldap:ldap /var/lib/ldap/*

[root@localhost Desktop]# systemctl restart slapd

[root@localhost Desktop]# systemctl enable slapd

 

9、添加cosine和nis模塊

[root@localhost schema]# cd /etc/openldap/schema/

[root@localhost schema]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f cosine.ldif

[root@localhost schema]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f nis.ldif

 

10、創建/etc/openldap/changes.ldif文件,並將下面的模板複製進去,標紅內容不要複製:

[root@localhost schema]# vim /etc/openldap/changes.ldif

dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcSuffix

olcSuffix: dc=linuxcool,dc=com   //定義頂級域

 

dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcRootDN

olcRootDN: cn=Manager,dc=linuxcool,dc=com   //定義根域

 

dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcRootPW

olcRootPW:{SSHA}8/eURCPyDBFc2t77snVeOIAa9jX2q/Jn   //輸入之前生成的密碼

 

 

dn: cn=config

changetype: modify

replace: olcTLSCertificateFile

olcTLSCertificateFile: /etc/openldap/certs/cert.pem     //公鑰保存的位置

 

dn: cn=config

changetype: modify

replace: olcTLSCertificateKeyFile

olcTLSCertificateKeyFile: /etc/openldap/certs/priv.pem   //私鑰保存的位置

 

dn: cn=config

changetype: modify

replace: olcLogLevel

olcLogLevel: -1

 

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=linuxcool,dc=com" read by * none

 

11、將新的配置文件更新到slapd服務程序:

[root@localhost schema]# ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/openldap/changes.ldif

 

12、創建/etc/openldap/base.ldif文件,並將下面的模板複製進去:

[root@localhost schema]# vim /etc/openldap/base.ldif

dn: dc=linuxcool,dc=com

dc: linuxcool

objectClass: top

objectClass: domain

 

dn: ou=People,dc=linuxcool,dc=com

ou: People

objectClass: top

objectClass: organizationalUnit

 

dn: ou=Group,dc=linuxcool,dc=com

ou: Group

objectClass: top

objectClass: organizationalUnit

 

13、創建目錄結構

[root@localhost schema]# ldapadd -x -w linuxcool -D cn=Manager,dc=linuxcool,dc=com -f /etc/openldap/base.ldif

 

14、設置帳戶的遷移(修改第71與74行):

[root@localhost schema]# vim /usr/share/migrationtools/migrate_common.ph

 $DEFAULT_MAIL_DOMAIN = "linuxcool.com";

 $DEFAULT_BASE = "dc=linuxcool,dc=com";

 

15、創建一個測試用戶

[root@localhost schema]# useradd ldapuser

 

16、將passwd文件裏剛創建的用戶信息拷貝到/usr/share/migrationtools/passwd文件:

[root@instructor Desktop]# cat /etc/passwd | grep ldapuser

ldapuser:x:1001:1001::/home/ldapuser:/bin/bash

[root@instructor Desktop]#  echo "ldapuser:x:1001:1001::/home/ldapuser:/bin/bash" > /usr/share/migrationtools/passwd

 

17、將用戶組信息拷貝到/usr/share/migrationtools/group

[root@instructor Desktop]# cat /etc/group | grep ldapuser

ldapuser:x:1001:

[root@instructor Desktop]# echo "ldapuser:x:1001:" >  /usr/share/migrationtools/group

 

18、將剛纔拷貝出來的用戶和用戶組信息遷移至目錄服務:

  1. [root@instructor Desktop]# /usr/share/migrationtools/migrate_passwd.pl  /usr/share/migrationtools/passwd  /usr/share/migrationtools/users.ldif
  2. [root@instructor Desktop]# /usr/share/migrationtools/migrate_group.pl  /usr/share/migrationtools/group  /usr/share/migrationtools/groups.ldif
  3. [root@instructor migrationtools]# ldapadd -x -w linuxcool -D cn=Manager,dc=linuxcool,dc=com -f /usr/share/migrationtools/users.ldif
  4. [root@instructor migrationtools]# ldapadd -x -w linuxcool -D cn=Manager,dc=linuxcool,dc=com -f /usr/share/migrationtools/groups.ldif

 

19、搜索用戶信息,測試linuxcool用戶的配置文件

[root@instructor migrationtools]# ldapsearch -x cn=ldapuser -b dc=linuxcool,dc=com

 

20、安裝網站服務

[root@instructor migrationtools]# yum install httpd -y

 

21、將密鑰文件上傳至網站目錄,修改網站目錄的權限讓其它用戶擁有相應權限:

[root@localhost schema]# cp /etc/openldap/certs/cert.pem /var/www/html/

[root@localhost schema]# chmod 777 /var/www/html/*

 

22、重啓httpd並加入開機自啓動項中:

[root@instructor migrationtools]# systemctl restart httpd

[root@instructor migrationtools]# systemctl enable httpd

 

23、清空iptables的配置,以免有所影響

[root@instructor migrationtools]# iptables -F

[root@instructor migrationtools]# service iptables save

 

24、配置nfs服務,用來掛在ldap用戶目錄

[root@instructor migrationtools]# echo "/home/ldapuser 192.168.10.*(rw,sync,root_squash)" > /etc/exports

[root@localhost schema]# exportfs -a

[root@localhost schema]# export -a

[root@instructor migrationtools]# systemctl restart nfs-server.service

[root@instructor migrationtools]# systemctl enable nfs-server.service

 

 

二、客戶端

 

1、客戶端要有一條域名解析到instructor.linuxprobe.com主機名:

[root@localhost ~]# echo "192.168.10.10 instructor.linuxcool.com" >>/etc/hosts

 

2、安裝相應的軟件包

[root@localhost Desktop]# yum install openldap-clients nss-pam-ldapd authconfig-gtk authconfig-tui pam_krb5

// authconfig-gtk:如果沒有圖形化工具可以不裝,使用authconfig-tui代替

 

3、運行系統認證工具,並填寫LDAP服務信息

方法一:使用圖形化工具配置

[root@localhost ~]# authconfig-gtk

LDAP SEARCH Base DN:dc=linuxcool,dc=com

LDAP Server:ldap://instructor.linuxcool.com/

Certificate URL:http://instructor.linuxcool.com/cert.pem

 

 

 

方法二:沒有圖形化的時候使用該方法

  1. 創建cacerts目錄以保存證書,默認是沒有該目錄
    [root@localhost Desktop]# mkdir /etc/openldap/cacerts
  2. 下載證書到該目錄

     [root@localhost Desktop]# wget -O /etc/openldap/cacerts/cert.pem http://instructor.linuxcool.com/cert.pem

  1. 最後按以下截圖配置即可

     [root@localhost Desktop]# authconfig-tui

 

 

 

 

 

註釋:因爲客戶端沒有提供ldapuser用戶的家目錄,所有切換到用戶時會提示沒有該用戶的家目錄,

以下兩個方法選其一即可。

4、掛在ldap用戶的目錄

方法一:修改fstab文件進行永久掛載

[root@localhost ~]# echo "192.168.10.10:/home/ldapuser /home/ldapuser nfs defaults 0 0" >>/etc/fstab

[root@localhost ~]# mkdir /home/ldapuser

[root@localhost ~]# mount -a

 

方法二:配置autofs服務自動掛載用戶目錄

1)、安裝autofs服務:
[root@localhost Desktop]# yum install autofs -y

2)、在autofs的主配置文件中添加一行內容
[root@localhost Desktop]# vim /etc/auto.master
/home        /etc/autoldap.misc

 

3)、配置autofs的子配置文件
[root@localhost Desktop]# echo "*   -fstype=nfs,rw instructor.linuxcool.com:/home/&" > /etc/autoldap.misc

4)、重啓autofs服務並加入開機自啓動
[root@localhost Desktop]# systemctl restart autofs.service
[root@localhost Desktop]# systemctl enable autofs.service

 

6、分別測試fstab文件和autofs服務掛在後的效果

[root@localhost ~]# su - ldapuser

方法一:

 

方法二:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章