重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
目录服务就是按照树状存储信息的模式。LDAP是轻量目录访问协议(LightweightDirectory Access Protocol)的缩写,LDAP标准实际上是在X.500标准基础上产生的一个简化版本。
<1>在LDAP中目录是按照树型结构组织——目录信息树(DIT) ,DIT是一个主要进行读操作的数据库。
<2>DIT由条目(Entry)组成,条目相当于关系数据库中表的记录;条目是具有分辨名DN(Distinguished Name)的属性-值对(Attribute-value,简称AV)的集合。
基于cn(姓名) | cn=test,ou=auth,dc=baike,dc=com最常见的cn是/etc/group转来的条目 |
基于uid(User ID) | uid=test, ou=auth,dc=baike,dc=com最常见的uid是/etc/passwd转来的条目 |
一、检查环境:
×××服务器:10.0.18.122
[root@TEST1~]# cat /etc/redhat-release
CentOSrelease 5.8 (Final)
[root@TEST1~]# uname -a
Linux TEST12.6.18-308.el5 #1 SMP Tue Feb 21 20:06:06 EST 2012 x86_64 x86_64 x86_64GNU/Linux
二、安装openldap软件
[root@TEST1~]# yum -y install openldap-*
######################################################################
Yum安装openlap时安装和升级的软件包如下:
Install:
openldap-servers.x86_640:2.3.43-25.el5_8.1
openldap-servers-overlays.x86_640:2.3.43-25.el5_8.1
openldap-servers-sql.x86_640:2.3.43-25.el5_8.1
DependencyInstalled:
libtool-ltdl.x86_640:1.5.22-7.el5_4
unixODBC-libs.x86_640:2.2.11-10.el5
Updated:
openldap.i3860:2.3.43-25.el5_8.1
openldap.x86_640:2.3.43-25.el5_8.1
openldap-clients.x86_640:2.3.43-25.el5_8.1
openldap-devel.i3860:2.3.43-25.el5_8.1
openldap-devel.x86_640:2.3.43-25.el5_8.1
######################################################################
三、配置openldap
<1>备份配置文件sldap.conf和ldap.conf
[root@TEST1~]# cd /etc/openldap/
[root@TEST1openldap]# ls
cacerts DB_CONFIG.example ldap.conf schema slapd.conf
[root@TEST1openldap]# cp slapd.conf slapd.conf_liqunyan_$(date +%F)
[root@TEST1openldap]# cp ldap.conf ldap.conf_liqunyan_$(date +%F)
[root@TEST1openldap]# ls
cacerts ldap.conf schema slapd.conf_liqunyan_2013-02-27
DB_CONFIG.example ldap.conf_liqunyan_2013-02-27 slapd.conf
<2>用哈希散列的方式生成root管理员密码
[root@TEST1openldap]# slappasswd -h {SSHA}
Newpassword:
Re-enter newpassword:
{SSHA}jT3zYgVa1Ys7x4Bp2g/Iec5wDTLQfk4t
<3>修改配置文件slapd.conf
[root@TEST1openldap]# cat slapd.conf|egrep -v "^#|^$"
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/inetorgperson.schema
include /etc/openldap/schema/nis.schema
allowbind_v2
pidfile /var/run/openldap/slapd.pid
argsfile /var/run/openldap/slapd.args
TLSCACertificateFile/etc/pki/tls/certs/ca-bundle.crt
TLSCertificateFile /etc/pki/tls/certs/slapd.pem
TLSCertificateKeyFile /etc/pki/tls/certs/slapd.pem
[l1] access todn.base="cn=Subschema" by * read
access to *
by self write
by users read
by anonymous auth
database bdb
suffix "dc=abc,dc=com"
rootdn "cn=Manager,dc=abc,dc=com"
[l2] rootpw {SSHA}jT3zYgVa1Ys7x4Bp2g/Iec5wDTLQfk4t[l3]
directory /var/lib/ldap
indexobjectClass eq,pres
indexou,cn,mail,surname,givenname eq,pres,sub
indexuidNumber,gidNumber,loginShell eq,pres
indexuid,memberUid eq,pres,sub
indexnisMapName,nisMapEntry eq,pres,sub
<4>启动ldap服务
[root@TEST1openldap]# /etc/init.d/ldap start
Startingslapd: [ OK ]
[root@TEST1openldap]# netstat -lant|grep 389
tcp 0 0 0.0.0.0:389 0.0.0.0:* LISTEN
[root@TEST1openldap]# ps -ef|grep ldap
ldap 26976 1 0 04:37 ? 00:00:00 /usr/sbin/slapd -h ldap:///ldaps:/// -u ldap
root 26987 3148 0 04:40 pts/0 00:00:00 grep ldap
四、迁移用户数据到目录服务数据库
[root@TEST1~]# cd /usr/share/openldap/migration/
[root@TEST1migration]#vi migrate_common.ph
# DefaultDNS domain
$DEFAULT_MAIL_DOMAIN = "abc.com";
# Default base
$DEFAULT_BASE = "dc=abc,dc=com"
[l4] <1>首先创建基本的数据库文件
[root@TEST1migration]#./migrate_base.pl >base.ldif[l5]
修改生成的base.ldif文件
(修改成自己想要的结构,本实验中的结构如下:
)
[root@TEST1migration]# cat base.ldif
dn:dc=abc,dc=com
dc: abc
objectClass:domain
[root@TEST1migration]# cp base.ldif base1.ldif
[root@TEST1migration]# cat base1.ldif
dn: ou=accounts,dc=abc,dc=com
ou: accounts
objectClass: top
objectClass: organizationalUnit
dn: ou=sysusers,dc=abc,dc=com
ou: sysusers
objectClass: top
objectClass: organizationalUnit
[l6] objectClass中organizationalUnit属性的说明:
objectclass ( 2.5.6.5 NAME 'organizationalUnit'
DESC'RFC2256: an organizational unit'
SUPtop STRUCTURAL
MUSTou
MAY (userPassword $ searchGuide $ seeAlso $ businessCategory $
x121Address $ registeredAddress $ destinationIndicator $
preferredDeliveryMethod $ telexNumber $ teletexTerminalIdentifier $
telephoneNumber $ internationaliSDNNumber $
facsimileTelephoneNumber $ street $ postOfficeBox $ postalCode $
postalAddress $ physicalDeliveryOfficeName $ st $ l $ description ) )
以上内容在/etc/openldap/schema/core.schema中有相关说明。
[root@TEST1migration]# cp base1.ldif base2.ldif
[root@TEST1migration]# cat base2.ldif
dn:ou=users,ou=accounts,dc=abc,dc=com
ou: users
objectClass:top
objectClass:organizationalUnit
dn:ou=groups,ou=accounts,dc=abc,dc=com
ou: groups
objectClass:top
objectClass:organizationalUnit
<2>从/etc/group中迁移ldap中用户组的信息
[root@TEST1migration]# grep root /etc/group >group.in
[root@TEST1 migration]# ./migrate_group.pl group.in>group.ldif
[l7] [root@TEST1migration]# cat groups.ldif
dn:cn=staff,ou=groups,ou=accounts,dc=abc,dc=com
objectClass:posixGroup
cn: staff
gidNumber: 1
posixGroup的相关信息:
objectclass ( 1.3.6.1.1.1.2.2 NAME 'posixGroup'
DESC'Abstraction of a group of accounts'
SUPtop STRUCTURAL
MUST (cn $ gidNumber )
MAY (userPassword $ memberUid $ description ) )
在/etc/openldap/schema/nis.schema中可以查看到相关信息。
<3>从/etc/passwd和/etc/shadow中迁移ldap中用户的信息:
[root@TEST1migration]# grep root /etc/passwd > passwd.in
[root@TEST1 migration]# ./migrate_passwd.plpasswd.in >users.ldif
[l8] [root@TEST1 migration]# cat users.ldif
dn:cn=liqunyan,ou=users,ou=accounts,dc=abc,dc=com
objectClass:posixAccount
objectClass:inetOrgPerson
givenName::576k6Imz
sn:: 5p2O
cn: liqunyan
uid:liqunyan
uidNumber:3236
gidNumber: 1
homeDirectory:/home/liqunyan
loginShell:/bin/sh
mail:liqunyan@aa.com
employeeType:staff
userPassword::e1NIQX1GN1ZLZVRFWUIvY0NqUkJjV0NodTdrWW2275pxc9
dn:cn=aa,ou=users,ou=accounts,dc=abc,dc=com
objectClass:posixAccount
objectClass:inetOrgPerson
givenName::55Ge
sn:: 546L
cn: aa
uid: aa
uidNumber:2872
gidNumber: 1
homeDirectory:/home/aa
loginShell:/bin/sh
mail:aa@aa.com
employeeType:staff
userPassword::e1NIQX1mWFVTMEtPVG81R21sd25RRnc3cjRjWEZ5UXc9
[root@TEST1migration]# cat sysusers.ldif
dn:uid=lsync,ou=sysusers,dc=abc,dc=com
uid: lsync
userPassword::e01ENX1wMnB5WWVqTmZkb0hYT1VLcWFFb3JBPT0=
objectClass:account
objectClass:simpleSecurityObject
objectClass:top
dn:uid=mail,ou=sysusers,dc=abc,dc=com
uid: mail
userPassword::e01ENX05ZG5DcnU5ZFhnSDN4Sk02cWkyOGV3PT0=
objectClass:account
objectClass:simpleSecurityObject
objectClass:top
dn:uid=auth,ou=sysusers,dc=abc,dc=com
uid: auth
userPassword::e01ENX1KeCtKMmNQZWJYOGV3L3VjS0NsOTFBPT0=
objectClass:account
objectClass:simpleSecurityObject
objectClass:top
account和simpleSecurityObject的相关信息:
objectclass ( 0.9.2342.19200300.100.4.5 NAME'account'
SUPtop STRUCTURAL
MUSTuserid
MAY (description $ seeAlso $ localityName $
organizationName $ organizationalUnitName $ host )
)
objectclass ( 0.9.2342.19200300.100.4.19 NAME'simpleSecurityObject'
SUP topAUXILIARY
MUSTuserPassword )
<4>将以上的结构一层一层导入到ldap中
[root@TEST1migration]# ldapadd -x -D "cn=Manager,dc=abc,dc=com" -W -f base.ldif
Enter LDAPPassword:[l9]
adding newentry "dc=abc,dc=com"
[root@TEST1migration]# ldapadd -x -D "cn=Manager,dc=abc,dc=com" -W -f base1.ldif
Enter LDAPPassword:
adding newentry "ou=accounts,dc=abc,dc=com"
adding newentry "ou=sysusers,dc=abc,dc=com"
[root@TEST1migration]# ldapadd -x -D "cn=Manager,dc=abc,dc=com" -W -fbase2.ldif
Enter LDAPPassword:
adding newentry "ou=users,ou=accounts,dc=abc,dc=com"
adding newentry "ou=groups,ou=accounts,dc=abc,dc=com"
[root@TEST1migration]# ldapadd -x -D "cn=Manager,dc=abc,dc=com" -W -fgroups.ldif
Enter LDAPPassword:
adding newentry "cn=staff,ou=groups,ou=accounts,dc=abc,dc=com"
[root@TEST1migration]# ldapadd -x -D "cn=Manager,dc=abc,dc=com" -W -fsysusers.ldif
Enter LDAPPassword:
adding newentry "uid=lsync,ou=sysusers,dc=abc,dc=com"
adding newentry "uid=mail,ou=sysusers,dc=abc,dc=com"
adding newentry "uid=auth,ou=sysusers,dc=abc,dc=com"
ldapadd -x-D "cn=Manager,dc=abc,dc=com" -W -f users.ldif
Enter LDAPPassword:
adding newentry "cn=liqunyan,ou=users,ou=accounts,dc=abc,dc=com"
adding newentry "cn=aa,ou=users,ou=accounts,dc=abc,dc=com"
<5>查看导入的用户信息。
[root@TEST1migration]# /usr/bin/ldapsearch-LLL -W -x -H ldap://127.0.0.1 -D "cn=Manager,dc=abc,dc=com" -b"ou=sysusers,dc=abc,dc=com" "(uid=lsync)"[l10]
Enter LDAPPassword:
dn:uid=lsync,ou=sysusers,dc=abc,dc=com
uid: lsync
userPassword::e01ENX1wMnB5WWVqTmZkb0hYT1VLcWFFb3JBPT0=
objectClass:account
objectClass:simpleSecurityObject
objectClass:top
[root@TEST1migration]#/usr/bin/ldapsearch -LLL -W -x -H ldap://127.0.0.1 -D"cn=Manager,dc=abc,dc=com" -b"ou=users,ou=accounts,dc=abc,dc=com" "(uid=liqunyan)"[l11]
Enter LDAPPassword:
dn:cn=liqunyan,ou=users,ou=accounts,dc=abc,dc=com
objectClass:posixAccount
objectClass:inetOrgPerson
givenName::576k6Imz
sn:: 5p2O
cn: liqunyan
uid:liqunyan
uidNumber:3236
gidNumber: 1
homeDirectory:/home/liqunyan
loginShell:/bin/sh
mail:liqunyan@aa.com
employeeType:staff
userPassword::e1NIQX1GN1ZLZVRFWUIvY0NqUkJjV0NodTdrWW2275pxc9
说明:
《1》 修改用户的密码:
知道自己的密码,给自己设置新的密码:
[root@TEST1openldap]# ldappasswd -x -H ldap://127.0.0.1 -D"cn=liqunyan,ou=users,ou=accounts,dc=abc,dc=com" -W"cn=liqunyan,ou=users,ou=accounts,dc=abc,dc=com" -S
Newpassword:
Re-enter newpassword:
Enter LDAPPassword:[l12]
Result:Success (0)
[root@TEST1openldap]# /usr/bin/ldapsearch -LLL -W -x -H ldap://127.0.0.1 -D"cn=Manager,dc=abc,dc=com" -b"ou=users,ou=accounts,dc=abc,dc=com" "(uid=liqunyan)"
Enter LDAPPassword:
dn: cn=liqunyan,ou=users,ou=accounts,dc=abc,dc=com
objectClass:posixAccount
objectClass:inetOrgPerson
givenName::576k6Imz
sn:: 5p2O
cn: liqunyan
uid:liqunyan
uidNumber:3236
gidNumber: 1
homeDirectory:/home/liqunyan
loginShell:/bin/sh
mail: liqunyan@aa.com
employeeType:staff
userPassword::e1NTSEF9RktlNDRtSXhiaDRKMlZ4RDFFa2lTaW5jQmRpZXdzcGI=
管理员给任何一个用户修改密码:
[root@TEST1openldap]# ldappasswd -x -H ldap://127.0.0.1 -D"cn=Manager,dc=abc,dc=com" -W"cn=aa,ou=users,ou=accounts,dc=abc,dc=com" -S
Newpassword:
Re-enter newpassword:
Enter LDAPPassword:[l13]
Result:Success (0)
[root@TEST1openldap]# /usr/bin/ldapsearch -LLL -W -x -H ldap://127.0.0.1 -D"cn=Manager,dc=abc,dc=com" -b"ou=users,ou=accounts,dc=abc,dc=com" "(uid=aa)"
Enter LDAPPassword:
dn:cn=aa,ou=users,ou=accounts,dc=abc,dc=com
objectClass:posixAccount
objectClass:inetOrgPerson
givenName::55Ge
sn:: 546L
cn: aa
uid: aa
uidNumber:2872
gidNumber: 1
homeDirectory:/home/aa
loginShell:/bin/sh
mail:aa@aa.com
employeeType:staff
userPassword::e1NTSEF9cXhPajMzemNpYkJUWVpBN2R3OHpsMGV3ZkJ3bXA3ZTc=
[l1]此三行最前端不要有空格
[l2]Suffix和rootdn修改成自己的域后缀。
[l3]管理员密码,注意rootpw和密码之间不能存在空格,要用tab键隔开。
[l4]将此选项中的域名修改成自己的域名。
[l5]基本的数据库文件可以用此命令生成,也可以自己手写,如果手写,手写内容必须符合相关属性。
[l6]添加ou account和sysusers(就是abc.com的分支机构)
[l7]同样,group.ldif可以使用命令生成,也可以自己手写创建。
[l8]同样,sysusers.ldif可以使用命令生成,也可以自己手写创建。
[l9]此密码为上面设置的root密码。
[l10]查询lsync用户信息
[l11]查看liqunyan用户信息。
[l12]此处输入的是自己的密码
[l13]此处输入的就是管理员root的密码。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。