Fedora 21
Sponsored Link

ユーザーアカウントを追加する2014/12/31

 
LDAP サーバーにユーザーアカウントを追加します。
[1] ユーザーを個別に追加します。
[root@dlp ~]#
slappasswd
   
# パスワード生成

New password:    
# 生成するパスワード入力

Re-enter new password:
{SSHA}xxxxxxxxxxxxxxxxx    
# 控えておく (次のファイルの [userPassword] に使う)

[root@dlp ~]#
vi ldapuser.ldif
# 新規作成
# 「dc=***,dc=***」には自身の 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:    
# 管理者パスワード

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

adding new entry "cn=fedora,ou=groups,dc=srv,dc=world"
[2] ローカル の /etc/passwd に登録されているユーザーを LDAP ディレクトリに一括追加します。
[root@dlp ~]#
vi ldapuser.sh
# ローカルの UID が 1000-9999番のユーザーを抽出する
# 「SUFFIX=***」には自身の suffix に置き換えてください
# 一例ですのでご自由に改変してください

#!/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:    
# 管理者パスワード

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] ローカル の /etc/group に登録されているグループを LDAP ディレクトリに一括追加します。
[root@dlp ~]#
vi ldapgroup.sh
# ローカルの GID が 1000-9999番のグループを抽出する
# 「SUFFIX=***」には自身の suffix に置き換えてください
# 一例ですのでご自由に改変してください

#!/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:    
# 管理者パスワード

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] 追加したユーザーとグループを削除する場合は以下のようにします。
[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:
関連コンテンツ