Fedora 21
Sponsored Link

Add User Accounts
2014/12/31
 
Add LDAP User accounts to the OpenLDAP Server.
[1] Add a User.
[root@dlp ~]#
slappasswd
# generate encrypted password

New password:
# input any password

Re-enter new password:
{SSHA}xxxxxxxxxxxxxxxxx
# remember (specify it to the [userPassword] section below)

[root@dlp ~]#
vi ldapuser.ldif
# create new
# replace the section "dc=***,dc=***" to your own suffix
dn: uid=fedora,ou=people,dc=srv,dc=world
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: fedora
cn: fedora
sn: fedora
userPassword: {SSHA}xxxxxxxxxxxxxxxxx
loginShell: /bin/bash
uidNumber: 1000
gidNumber: 1000
homeDirectory: /home/fedora

dn: cn=fedora,ou=groups,dc=srv,dc=world
objectClass: posixGroup
cn: fedora
gidNumber: 1000
memberUid: fedora

[root@dlp ~]#
ldapadd -x -D cn=admin,dc=srv,dc=world -W -f ldapuser.ldif

Enter LDAP Password:
# admin password

adding new entry "uid=fedora,ou=people,dc=srv,dc=world"

adding new entry "cn=fedora,ou=groups,dc=srv,dc=world"
[2] Add Existing local Users to LDAP Directory.
[root@dlp ~]#
vi ldapuser.sh
# extract local users who have 1000-9999 digit UID
# replace "SUFFIX=***" to your own suffix
# this is an example

#!/bin/bash

SUFFIX='dc=srv,dc=world'
LDIF='ldapuser.ldif'

echo -n > $LDIF
for line in `grep "x:[1-9][0-9][0-9][0-9]:" /etc/passwd | sed -e "s/ /%/g"`
do
   UID1=`echo $line | cut -d: -f1`
   NAME=`echo $line | cut -d: -f5 | cut -d, -f1`
   if [ ! "$NAME" ]
   then
      NAME=$UID1
   else
      NAME=`echo $NAME | sed -e "s/%/ /g"`
   fi
   SN=`echo $NAME | awk '{print $2}'`
   if [ ! "$SN" ]
   then
      SN=$NAME
   fi
   GIVEN=`echo $NAME | awk '{print $1}'`
   UID2=`echo $line | cut -d: -f3`
   GID=`echo $line | cut -d: -f4`
   PASS=`grep $UID1: /etc/shadow | cut -d: -f2`
   SHELL=`echo $line | cut -d: -f7`
   HOME=`echo $line | cut -d: -f6`
   EXPIRE=`passwd -S $UID1 | awk '{print $7}'`
   FLAG=`grep $UID1: /etc/shadow | cut -d: -f9`
   if [ ! "$FLAG" ]
   then
      FLAG="0"
   fi
   WARN=`passwd -S $UID1 | awk '{print $6}'`
   MIN=`passwd -S $UID1 | awk '{print $4}'`
   MAX=`passwd -S $UID1 | awk '{print $5}'`
   LAST=`grep $UID1: /etc/shadow | cut -d: -f3`

   echo "dn: uid=$UID1,ou=people,$SUFFIX" >> $LDIF
   echo "objectClass: inetOrgPerson" >> $LDIF
   echo "objectClass: posixAccount" >> $LDIF
   echo "objectClass: shadowAccount" >> $LDIF
   echo "uid: $UID1" >> $LDIF
   echo "sn: $SN" >> $LDIF
   echo "givenName: $GIVEN" >> $LDIF
   echo "cn: $NAME" >> $LDIF
   echo "displayName: $NAME" >> $LDIF
   echo "uidNumber: $UID2" >> $LDIF
   echo "gidNumber: $GID" >> $LDIF
   echo "userPassword: {crypt}$PASS" >> $LDIF
   echo "gecos: $NAME" >> $LDIF
   echo "loginShell: $SHELL" >> $LDIF
   echo "homeDirectory: $HOME" >> $LDIF
   echo "shadowExpire: $EXPIRE" >> $LDIF
   echo "shadowFlag: $FLAG" >> $LDIF
   echo "shadowWarning: $WARN" >> $LDIF
   echo "shadowMin: $MIN" >> $LDIF
   echo "shadowMax: $MAX" >> $LDIF
   echo "shadowLastChange: $LAST" >> $LDIF
   echo >> $LDIF
done
[root@dlp ~]#
sh ldapuser.sh

[root@dlp ~]#
ldapadd -x -D cn=admin,dc=srv,dc=world -W -f ldapuser.ldif

Enter LDAP Password:
# admin password

adding new entry "uid=redhat,ou=people,dc=srv,dc=world"

adding new entry "uid=ubuntu,ou=people,dc=srv,dc=world"

adding new entry "uid=debian,ou=people,dc=srv,dc=world"
[3] Add existing local groups to LDAP directory.
[root@dlp ~]#
vi ldapgroup.sh
# extract local groups who have 1000-9999 digit UID
# replace "SUFFIX=***" to your own suffix
# this is an example

#!/bin/bash

SUFFIX='dc=srv,dc=world'
LDIF='ldapgroup.ldif'

echo -n > $LDIF
for line in `grep "x:[1-9][0-9][0-9][0-9]:" /etc/group`
do
   CN=`echo $line | cut -d: -f1`
   GID=`echo $line | cut -d: -f3`
   echo "dn: cn=$CN,ou=groups,$SUFFIX" >> $LDIF
   echo "objectClass: posixGroup" >> $LDIF
   echo "cn: $CN" >> $LDIF
   echo "gidNumber: $GID" >> $LDIF
   users=`echo $line | cut -d: -f4 | sed "s/,/ /g"`
   for user in ${users} ; do
      echo "memberUid: ${user}" >> $LDIF
   done
   echo >> $LDIF
done
[root@dlp ~]#
sh ldapgroup.sh

[root@dlp ~]#
ldapadd -x -D cn=admin,dc=srv,dc=world -W -f ldapgroup.ldif

Enter LDAP Password:
# admin password

adding new entry "cn=redhat,ou=groups,dc=srv,dc=world"

adding new entry "cn=ubuntu,ou=groups,dc=srv,dc=world"

adding new entry "cn=debian,ou=groups,dc=srv,dc=world"
[4] If you'd like to delete LDAP User or Group, Do as below.
[root@dlp ~]#
ldapdelete -x -W -D 'cn=admin,dc=srv,dc=world' "uid=fedora,ou=people,dc=srv,dc=world"

Enter LDAP Password:
[root@dlp ~]#
ldapdelete -x -W -D 'cn=admin,dc=srv,dc=world' "cn=fedora,ou=groups,dc=srv,dc=world"

Enter LDAP Password:
Matched Content
 
Tweet