本文共 8192 字,大约阅读时间需要 27 分钟。
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
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、将刚才拷贝出来的用户和用户组信息迁移至目录服务:
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
[root@localhost Desktop]# wget -O /etc/openldap/cacerts/cert.pem
[root@localhost Desktop]# authconfig-tui
注释:因为客户端没有提供ldapuser用户的家目录,所有切换到用户时会提示没有该用户的家目录,
以下两个方法选其一即可。
4、挂在ldap用户的目录
[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
1)、安装autofs服务:
[root@localhost Desktop]# yum install autofs -y2)、在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.misc4)、重启autofs服务并加入开机自启动
[root@localhost Desktop]# systemctl restart autofs.service [root@localhost Desktop]# systemctl enable autofs.service
6、分别测试fstab文件和autofs服务挂在后的效果
[root@localhost ~]# su - ldapuser
方法一:
方法二:
转载地址:http://fdqcn.baihongyu.com/