###############################################################################
#
Name : Mahavairocana
#
Author : Mahavairocana
#
QQ : 10353512
#
WeChat : shenlan-qianlan
#
Blog : http://www.cnblogs.com/Mahavairocana/
#
Description : You are welcome to reprint, or hyperlinks to indicate the
#
source of the article, as well as author
information.
###############################################################################
第一步 下载安装
[root@s ~]# yum install openldap-servers openldap-clients openldap openldap-devel
第二步 配置LDAP
1. 拷贝配置文件到相应的目录 [root@s ~]# cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf [root@s ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG 2. 设置密码 [root@s ~]# slappasswd New password: Re-enter new password: {SSHA}lSYJYCeKzk+EebNtqyqHGdc16sTrlAng 3. 修改配置文件 [root@s ~]# vi /etc/openldap/slapd.conf database bdb suffix "dc=google,dc=cn" checkpoint 1024 15 rootdn "cn=Manager,dc=google,dc=cn" rootpw {SSHA}lSYJYCeKzk+EebNtqyqHGdc16sTrlAng #这里就是上面生成的 4. 建立 root-unit.ldif来定义整个树状结构(注意: 每个冒号后都要多一个空格) [root@s ~]# mkdir -p /etc/openldap/data [root@s ~]# vi /etc/openldap/data/root-unit.ldif #root node dn: dc=google,dc=cn dc: google objectClass: dcObject objectClass: organizationalUnit ou: google cn #login top dn: ou=login,dc=google,dc=cn ou: login objectClass: organizationalUnit 5. 刚 root-unit.ldif 加入到LDAP的数据库 [root@s ~]# mv /etc/openldap/slapd.d /etc/openldap/slapd.d.bak [root@s ~]# mkdir /etc/openldap/slapd.d [root@s ~]# slapadd -v -l /etc/openldap/data/root-unit.ldif The first database does not allow slapadd; using the first available one (2) bdb_db_open: DB_CONFIG for suffix "dc=google,dc=cn" has changed. Performing database recovery to activate new settings. added: "dc=google,dc=cn" (00000001) added: "ou=login,dc=google,dc=cn" (00000002) _#################### 100.00% eta none elapsed none fast! Closing DB... ---------------------------------------如果出现问题----------------------------------------- bdb_db_open: database "dc=google,dc=cn": database already in use. backend_startup_one (type=bdb, suffix="dc=google,dc=cn"): bi_db_open failed! (-1) slap_startup failed 就说明,程序已经被你启动,先关闭 service slapd stop -------------------------------------------------------------------------------------------- 6. 将LDAP新的数据库产生到 slapd.d 目录 [root@s ~]# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d config file testing succeeded 让目录有执行权限 [root@s ~]# chown -R ldap:ldap /var/lib/ldap [root@s ~]# chown -R ldap:ldap /etc/openldap/slapd.d ----------------------------如果你要修改 slapd.conf 内容,执行如下-------------------------- # rm -rf /etc/openldap/slapd.d/* # slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d # chown -R ldap:ldap /etc/openldap/slapd.d # service slapd restart -------------------------------------------------------------------------------------------- 7. 启动 ldap server 并开机启动 [root@s ~]# service slapd start 正在启动 slapd: [确定] [root@s ~]# chkconfig --level 345 slapd on 8. 确认树状结构是否完成 [root@s ~]# ldapsearch -x -b "dc=google,dc=cn" # extended LDIF # # LDAPv3 # base dc=google,dc=cn with scope subtree # filter: (objectclass=*) # requesting: ALL # # google.cn dn: dc=google,dc=cn dc: google objectClass: dcObject objectClass: organizationalUnit ou: google cn # login, google.cn dn: ou=login,dc=google,dc=cn ou: login objectClass: organizationalUnit # search result search: 2 result: 0 Success # numResponses: 3 # numEntries: 2 9. 创建用户 [root@s ~]# vi /etc/openldap/data/users.ldif dn: uid=test,dc=google,dc=cn uid: test cn: test objectClass: account objectClass: posixAccount objectClass: top objectClass: shadowAccount userPassword: {SSHA}lSYJYCeKzk+EebNtqyqHGdc16sTrlAng shadowLastChange: 14335 shadowMax: 99999 shadowWarning: 7 loginShell: /bin/bash uidNumber: 10000 gidNumber: 10000 homeDirectory: /home/test gecos: test [root@s ~]# ldapadd -a -x -D 'cn=Manager,dc=google,dc=cn' -W -f /etc/openldap/data/users.ldif Enter LDAP Password: adding new entry "uid=test,dc=google,dc=cn" 10. 重启下 ldap 服务器 [root@s ~]# service slapd restart 第三步 客户端使用SSH登录 1. 先安装 ldap [root@c2 ~]# yum install fprintd-pam openldap-clients nss_ldap nss-pam-ldapd 2. 先登录到客户端 [root@c2 ~]# authconfig-tui 选择“Use LDAP”和“Use LDAP Authentication”,其它默认,下一步 (建议:这里最好全部删除,重新输入,不然老多字母什么的,有错可以看 /var/logs/messages ) Server: ldap://192.168.100.254 #这里就是你LDAP服务器的IP Base DN: dc=google,dc=cn #这里填服务器一样的,然后完成 3. 用命令查看是否连接成功 [root@c2 ~]# id test #这个帐号 test 是在 ldap 中的,上面配置了,在客户端可以看到成功了 uid=10000(zaici) gid=10000 groups=10000 然后你可以 ssh 登录了