在安装完openldap之后,默认ldap中是没有数据的,需要管理员进行添加,当然添加的方法也不止一种,这里先介绍第一种方法:从本地系统添加用户到ldap中。

[root@ldapsrv01 ~]# ldapsearch -x -b "dc=contoso,dc=com"
# extended LDIF
#
# LDAPv3
# base  with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# search result
search: 2
result: 32 No such object
# numResponses: 1

这是安装完openldap之后对ldap进行的数据查询的结果。

步骤一:安装migrationtools软件包

从本地系统添加用户到ldap的方法,其实就是先添加用户到本地操作系统中,然后通过pl脚本将这些用户转换为ldap能够识别的ldif文件格式,最后通过ldapadd命令导入到ldap中,从而完成ldap数据的导入,要使用pl脚本将本地用户信息转换为ldif文件格式,首先需要安装一个软件,名字为migrationtools。

yum install migrationtools -y
# 直接yum安装即可
[root@ldapsrv01 ~]# rpm -ql migrationtools
/usr/share/doc/migrationtools-47
/usr/share/doc/migrationtools-47/README
/usr/share/doc/migrationtools-47/migration-tools.txt
/usr/share/migrationtools
/usr/share/migrationtools/migrate_aliases.pl
/usr/share/migrationtools/migrate_all_netinfo_offline.sh
/usr/share/migrationtools/migrate_all_netinfo_online.sh
/usr/share/migrationtools/migrate_all_nis_offline.sh
/usr/share/migrationtools/migrate_all_nis_online.sh
/usr/share/migrationtools/migrate_all_nisplus_offline.sh
/usr/share/migrationtools/migrate_all_nisplus_online.sh
/usr/share/migrationtools/migrate_all_offline.sh
/usr/share/migrationtools/migrate_all_online.sh
/usr/share/migrationtools/migrate_automount.pl
/usr/share/migrationtools/migrate_base.pl
/usr/share/migrationtools/migrate_common.ph
/usr/share/migrationtools/migrate_fstab.pl
/usr/share/migrationtools/migrate_group.pl
/usr/share/migrationtools/migrate_hosts.pl
/usr/share/migrationtools/migrate_netgroup.pl
/usr/share/migrationtools/migrate_netgroup_byhost.pl
/usr/share/migrationtools/migrate_netgroup_byuser.pl
/usr/share/migrationtools/migrate_networks.pl
/usr/share/migrationtools/migrate_passwd.pl
/usr/share/migrationtools/migrate_profile.pl
/usr/share/migrationtools/migrate_protocols.pl
/usr/share/migrationtools/migrate_rpc.pl
/usr/share/migrationtools/migrate_services.pl
/usr/share/migrationtools/migrate_slapd_conf.pl


使用rpm -ql命令可以看到,该软件安装后在/usr/share/migrationtools/目录下生成了很多脚本文件,有shell也有perl,而我们就是使用其中的perl脚本完成ldap文件的生成。

步骤二:修改migrationtools配置文件

cp /usr/share/migrationtools/migrate_common.ph /usr/share/migrationtools/migrate_common.ph.bak
#备份一下脚本
sed -i 's/padl.com/contoso.com/g' /usr/share/migrationtools/migrate_common.ph
#修改默认的域名padl.com为我的域名,这里为contoso.com,所以只把padl替换为contoso
sed -i 's/dc=padl,dc=com/dc=contoso,dc=com/g' /usr/share/migrationtools/migrate_common.ph
#还是域名信息的修改

修改之后,要确保下面两条的值为服务器真实的域名,如下:

$DEFAULT_MAIL_DOMAIN = "contoso.com";
$DEFAULT_BASE = "dc=contoso,dc=com";

步骤三:生成base.ldif文件并导入到ldap中

然后首先生成base.ldif文件

/usr/share/migrationtools/migrate_base.pl > base.ldif
[root@ldapsrv01 ~]# cat base.ldif
dn: dc=contoso,dc=com
dc: contoso
objectClass: top
objectClass: domain
dn: ou=Hosts,dc=contoso,dc=com
ou: Hosts
objectClass: top
objectClass: organizationalUnit
dn: ou=Rpc,dc=contoso,dc=com
ou: Rpc
objectClass: top
objectClass: organizationalUnit
dn: ou=Services,dc=contoso,dc=com
ou: Services
objectClass: top
objectClass: organizationalUnit
dn: nisMapName=netgroup.byuser,dc=contoso,dc=com
nismapname: netgroup.byuser
objectClass: top
objectClass: nisMap
dn: ou=Mounts,dc=contoso,dc=com
ou: Mounts
objectClass: top
objectClass: organizationalUnit
dn: ou=Networks,dc=contoso,dc=com
ou: Networks
objectClass: top
objectClass: organizationalUnit
dn: ou=People,dc=contoso,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
dn: ou=Group,dc=contoso,dc=com
ou: Group
objectClass: top
objectClass: organizationalUnit
dn: ou=Netgroup,dc=contoso,dc=com
ou: Netgroup
objectClass: top
objectClass: organizationalUnit
dn: ou=Protocols,dc=contoso,dc=com
ou: Protocols
objectClass: top
objectClass: organizationalUnit
dn: ou=Aliases,dc=contoso,dc=com
ou: Aliases
objectClass: top
objectClass: organizationalUnit
dn: nisMapName=netgroup.byhost,dc=contoso,dc=com
nismapname: netgroup.byhost
objectClass: top
objectClass: nisMap

但是这其中有很多信息都不是我所需要的,所以可以删除不需要的信息,修改完成之后,如下:

[root@ldapsrv01 ~]# cat base.ldif
dn: dc=contoso,dc=com
dc: contoso
objectClass: top
objectClass: domain
dn: ou=People,dc=contoso,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
dn: ou=Group,dc=contoso,dc=com
ou: Group
objectClass: top
objectClass: organizationalUnit
dn: cn=tech,ou=group,dc=contoso,dc=com
objectClass: posixGroup
description: Technology
gidNumber: 10001
cn: tech

现在,把修改好的base.ldif导入到ldap中,通过使用ldapadd命令来完成。

[root@ldapsrv01 ~]# ldapadd -x -D "cn=admin,dc=contoso,dc=com" -W -f base.ldif 
Enter LDAP Password: 
adding new entry "dc=contoso,dc=com"
adding new entry "ou=People,dc=contoso,dc=com"
adding new entry "ou=Group,dc=contoso,dc=com"
adding new entry "cn=tech,ou=group,dc=contoso,dc=com"

导入完成之后,我再进行一次简单查询:

[root@ldapsrv01 ~]# ldapsearch -x -b "dc=contoso,dc=com"
# extended LDIF
#
# LDAPv3
# base  with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# contoso.com
dn: dc=contoso,dc=com
dc: contoso
objectClass: top
objectClass: domain
# People, contoso.com
dn: ou=People,dc=contoso,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
# Group, contoso.com
dn: ou=Group,dc=contoso,dc=com
ou: Group
objectClass: top
objectClass: organizationalUnit
# tech, Group, contoso.com
dn: cn=tech,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
description: Technology
gidNumber: 10001
cn: tech
# search result
search: 2
result: 0 Success
# numResponses: 5
# numEntries: 4

可以看到,ldap中已经存在之前导入的ou和group了,下面准备用户和组的信息的导入。

步骤四:添加本地系统用户

这里我使用一个脚本(这个脚本不是我写的,而且批量生成用户也算是个简单的脚本吧,怪不得RHCE考这个,原来这里能用到啊。)来批量生成几个用户,然后将这些生成的用户导入到ldap中。

[root@ldapsrv01 ~]# cat addusers.sh 
#!/bin/bash  
# Add system user  
for ldap in {1..5};do  
    if id user${ldap} &> /dev/null;then  
        echo "System account already exists"  
    else  
        adduser user${ldap} -u $((10000 + $ldap))  
        echo user${ldap} | passwd --stdin user${ldap} &> /dev/null  
        echo "user${ldap} system add finish"  
    fi  
done  
[root@ldapsrv01 ~]# ./addusers.sh 
user1 system add finish
user2 system add finish
user3 system add finish
user4 system add finish
user5 system add finish
[root@ldapsrv01 ~]# id user1
uid=10001(user1) gid=10001(user1) groups=10001(user1)
[root@ldapsrv01 ~]# id user2
uid=10002(user2) gid=10002(user2) groups=10002(user2)
[root@ldapsrv01 ~]# id user3
uid=10003(user3) gid=10003(user3) groups=10003(user3)
[root@ldapsrv01 ~]# id user4
uid=10004(user4) gid=10004(user4) groups=10004(user4)
[root@ldapsrv01 ~]# id user5
uid=10005(user5) gid=10005(user5) groups=10005(user5)

步骤五:生成people.ldif和group.ldif文件

[root@ldapsrv01 ~]# tail -5 /etc/passwd
user1:x:10001:10001::/home/user1:/bin/bash
user2:x:10002:10002::/home/user2:/bin/bash
user3:x:10003:10003::/home/user3:/bin/bash
user4:x:10004:10004::/home/user4:/bin/bash
user5:x:10005:10005::/home/user5:/bin/bash
[root@ldapsrv01 ~]# tail -5 /etc/passwd > testuser

将这5个用户保存到testuser中,稍后会对这5个本地账户生成ladp用户文件。

[root@ldapsrv01 ~]# tail -5 /etc/group
user1:x:10001:
user2:x:10002:
user3:x:10003:
user4:x:10004:
user5:x:10005:
[root@ldapsrv01 ~]# tail -5 /etc/group > testgroup

将这5个组保存到testgroup中,同样稍后会用到。

/usr/share/migrationtools/migrate_passwd.pl testuser people.ldif
#根据testuser生成ldap用户数据文件people.ldif
/usr/share/migrationtools/migrate_group.pl testgroup group.ldif
#根据testgroup生成ldap组的数据文件group.ldif

注意,这两条命令的执行一定要在testuser/testgroup目录下,否则要加上全路径,不然找不到文件。

[root@ldapsrv01 ~]# cat people.ldif 
dn: uid=user1,ou=People,dc=contoso,dc=com
uid: user1
cn: user1
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}$6$Zs9mXyPO$QdqHS8nA/vyjUdnSWLIpM4QiaPtsca1WllXu0dWQOgTza2Qr4TcC3sHbAbJsWI0KannwnM.zFFroqxzglFfJT0
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10001
gidNumber: 10001
homeDirectory: /home/user1
dn: uid=user2,ou=People,dc=contoso,dc=com
uid: user2
cn: user2
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}$6$0dHBettP$u4vVTWuVkRN8NGIecG5O7VSnFDCtcQF/623lLY4Ih6RXORfFcRxh9fFdvssYeg6QAw1KY4LKtk.zwzdVXH2rh1
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10002
gidNumber: 10002
homeDirectory: /home/user2
dn: uid=user3,ou=People,dc=contoso,dc=com
uid: user3
cn: user3
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}$6$CXSotuEN$QSo5gJFlsJ6uZhqaK8qDto.5u2WXwvWSV1JqVP9WMOUVhxZXY8I.XMEUojwGGwpSYY1OtY2Sl/J7dzSk1ey6F/
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10003
gidNumber: 10003
homeDirectory: /home/user3
dn: uid=user4,ou=People,dc=contoso,dc=com
uid: user4
cn: user4
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}$6$ycSPE1gY$ofWgCYtRk9s5LnLKnR4FR5ld10wI6riT2u0jA8wytMm07am1NZreNdhEAJb8.erz4HasyhlsGqecBNdfx5x6S0
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10004
gidNumber: 10004
homeDirectory: /home/user4
dn: uid=user5,ou=People,dc=contoso,dc=com
uid: user5
cn: user5
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword: {crypt}$6$xVZ5hNeH$GzlC19E6Tiq/4Y3HmtCwRjjKVzvcI2IA.z.QPqhwHU.mSV1UT7uZiaVG8HelvbgZJsn3Z1te75C/DNZ.C0Ov50
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10005
gidNumber: 10005
homeDirectory: /home/user5

这是生成的people.ldif文件,里面是用户的信息,包括密码等信息,如果某些字段不需要,也可以删除。

[root@ldapsrv01 ~]# cat group.ldif 
dn: cn=user1,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user1
userPassword: {crypt}x
gidNumber: 10001
dn: cn=user2,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user2
userPassword: {crypt}x
gidNumber: 10002
dn: cn=user3,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user3
userPassword: {crypt}x
gidNumber: 10003
dn: cn=user4,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user4
userPassword: {crypt}x
gidNumber: 10004
dn: cn=user5,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user5
userPassword: {crypt}x
gidNumber: 10005

这是生成的group.ldif文件,里面是用户组的信息,这里的gidNumber就是在本地操作系统中的gid。

因为我之前已经添加了一个tech的用户组,它的gidNumber是10001,会跟即将导入的user1组的gidNumber冲突,所以我先删除tech用户组。

[root@ldapsrv01 ~]# ldapdelete -x -D "cn=admin,dc=contoso,dc=com" -W "cn=tech,ou=Group,dc=contoso,dc=com"
Enter LDAP Password: 
[root@ldapsrv01 ~]# ldapsearch -x -b "dc=contoso,dc=com"
# extended LDIF
#
# LDAPv3
# base  with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# contoso.com
dn: dc=contoso,dc=com
dc: contoso
objectClass: top
objectClass: domain
# People, contoso.com
dn: ou=People,dc=contoso,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
# Group, contoso.com
dn: ou=Group,dc=contoso,dc=com
ou: Group
objectClass: top
objectClass: organizationalUnit
# search result
search: 2
result: 0 Success
# numResponses: 4
# numEntries: 3

已经没有tech用户组了,可以开始导入用户和用户组了。

步骤六:导入ldif文件到ldap中

[root@ldapsrv01 ~]# ldapadd -x -D "cn=admin,dc=contoso,dc=com" -W -f people.ldif 
Enter LDAP Password: 
adding new entry "uid=user1,ou=People,dc=contoso,dc=com"
adding new entry "uid=user2,ou=People,dc=contoso,dc=com"
adding new entry "uid=user3,ou=People,dc=contoso,dc=com"
adding new entry "uid=user4,ou=People,dc=contoso,dc=com"
adding new entry "uid=user5,ou=People,dc=contoso,dc=com"

导入people.ldif文件,操作成功。

[root@ldapsrv01 ~]# ldapadd -x -D "cn=admin,dc=contoso,dc=com" -W -f group.ldif 
Enter LDAP Password: 
adding new entry "cn=user1,ou=Group,dc=contoso,dc=com"
adding new entry "cn=user2,ou=Group,dc=contoso,dc=com"
adding new entry "cn=user3,ou=Group,dc=contoso,dc=com"
adding new entry "cn=user4,ou=Group,dc=contoso,dc=com"
adding new entry "cn=user5,ou=Group,dc=contoso,dc=com"

导入group.ldif文件,操作成功。

步骤七:搜索ldap用户和组,验证操作是否成功

再对ldap进行一次用户检索:

[root@ldapsrv01 ~]# ldapsearch -LLL -x -D "cn=admin,dc=contoso,dc=com" -W -b "dc=contoso,dc=com" '(uid=*)'
Enter LDAP Password: 
dn: uid=user1,ou=People,dc=contoso,dc=com
uid: user1
cn: user1
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword:: e2NyeXB0fSQ2JFpzOW1YeVBPJFFkcUhTOG5BL3Z5alVkblNXTElwTTRRaWFQdHN
 jYTFXbGxYdTBkV1FPZ1R6YTJRcjRUY0Mzc0hiQWJKc1dJMEthbm53bk0uekZGcm9xeHpnbEZmSlQw
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10001
gidNumber: 10001
homeDirectory: /home/user1
dn: uid=user2,ou=People,dc=contoso,dc=com
uid: user2
cn: user2
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword:: e2NyeXB0fSQ2JDBkSEJldHRQJHU0dlZUV3VWa1JOOE5HSWVjRz×××1ZTbkZEQ3R
 jUUYvNjIzbExZNEloNlJYT1JmRmNSeGg5ZkZkdnNzWWVnNlFBdzFLWTRMS3RrLnp3emRWWEgycmgx
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10002
gidNumber: 10002
homeDirectory: /home/user2
dn: uid=user3,ou=People,dc=contoso,dc=com
uid: user3
cn: user3
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword:: e2NyeXB0fSQ2JENYU290dUVOJFFTbzVnSkZsc0o2dVpocWFLOHFEdG8uNXUyV1h
 3dldTVjFKcVZQOVdNT1VWaHhaWFk4SS5YTUVVb2p3R0d3cFNZWTFPdFkyU2wvSjdkelNrMWV5NkYv
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10003
gidNumber: 10003
homeDirectory: /home/user3
dn: uid=user4,ou=People,dc=contoso,dc=com
uid: user4
cn: user4
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword:: e2NyeXB0fSQ2JHljU1BFMWdZJG9mV2dDWXRSazlzNUxuTEtuUjRGUjVsZDEwd0k
 2cmlUMnUwakE4d3l0TW0wN2FtMU5acmVOZGhFQUpiOC5lcno0SGFzeWhsc0dxZWNCTmRmeDV4NlMw
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10004
gidNumber: 10004
homeDirectory: /home/user4
dn: uid=user5,ou=People,dc=contoso,dc=com
uid: user5
cn: user5
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
userPassword:: e2NyeXB0fSQ2JHhWWjVoTmVIJEd6bEMxOUU2VGlxLzRZM0htdEN3UmpqS1Z6dmN
 JMklBLnouUVBxaHdIVS5tU1YxVVQ3dVppYVZHOEhlbHZiZ1pKc24zWjF0ZTc1Qy9ETlouQzBPdjUw
shadowLastChange: 17053
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 10005
gidNumber: 10005
homeDirectory: /home/user5

对用户组进行数据检索:

[root@ldapsrv01 ~]# ldapsearch -LLL -x -D "cn=admin,dc=contoso,dc=com" -W -b "dc=contoso,dc=com" '(objectClass=posixGroup)'
Enter LDAP Password: 
dn: cn=user1,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user1
userPassword:: e2NyeXB0fXg=
gidNumber: 10001
dn: cn=user2,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user2
userPassword:: e2NyeXB0fXg=
gidNumber: 10002
dn: cn=user3,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user3
userPassword:: e2NyeXB0fXg=
gidNumber: 10003
dn: cn=user4,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user4
userPassword:: e2NyeXB0fXg=
gidNumber: 10004
dn: cn=user5,ou=Group,dc=contoso,dc=com
objectClass: posixGroup
objectClass: top
cn: user5
userPassword:: e2NyeXB0fXg=
gidNumber: 10005

已经将在本地使用shell脚本批量创建的5个用户(user1--user5)以及对应的5个用户组(user1--user5)都导入到ldap中,从本地系统添加用户到ldap中的操作完成。