簡單介紹一下ldapsearch命令,在ldap搜索條目時很有用,只要適當調整filter就可以。
命令如下:
ldapsearch -h hostname -p port -b baseDN -D BIND_DN [options] filter [attribute]...
參數說明:
-h:主機名或ip地址
-p:端口號
-b:suffix dn
-D:使用搜索綁定的用戶
其他參數一般很少使用。下面舉幾個例子看一下:
1)搜索所有條目:
ldapsearch -h host -b "dc=example,dc=com" "(objectclass=*)"
2)搜索cn為指定名字的條目
ldapsearch -h host -b "dc=example,dc=com" "(cn=Babs Jensen)"
3)使用filter文件進行搜索
filter文件內容:
sn=Francis
givenname=Barbara
指定搜索:
ldapsearch -b "dc=example,dc=com" -h host -f filters cn uid
4)模糊查詢,uid包含test的條目
ldapsearch -h host -b "dc=example,dc=com" "(uid=*test*)"
5)同時滿足多個屬性,uid包含test,givenName包含test的條目
ldapsearch -h host -b "dc=example,dc=com" "(|(uid=*test*)(givenName=*test*))"
這里filter的書寫規則和aci的filter中書寫規則相同。
通過ldapmodify可以更改條目屬性,下面簡單介紹一下,ldapmodify在添加,刪除,更新,更改dn的操作
1,添加條目,在使用ldapmodify添加條目的時候,可以手動輸入要添加的條目的所有屬性,也可以通過使用文件的方式進行添加;
文件內容如下(add.ldif):在編輯文件時須將必填屬性,例如uid,cn,sn等寫完整
dn: uid=bcubbins,ou=People,dc=example,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: bcubbins
givenName: Bartholomew
sn: Cubbins
cn: Bartholomew Cubbins
mail: bcubbins@example.com
userPassword: bcubbins
facsimiletelephonenumber: +1 234 567 8910
使用命令:
ldapmodify -a -h host -D uid=bjensen,ou=people,dc=example,dc=com -w - -f add.ldif
如果不是用文件,則需要在命令行下手動輸入如上內容,完成后按回車鍵。例如:
dn: uid=bcubbins,ou=People,dc=example,dc=com
changeType:add
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
uid: bcubbins
givenName: Bartholomew
sn: Cubbins
cn: Bartholomew Cubbins
....
2,修該條目,修改條目和刪除條目操作類似
通過文件修改:
dn: uid=bcubbins,ou=People,dc=example,dc=com
changetype: modify
add: description
description: Added with ldapmodify
-
replace: mail
mail: bart@example.com
執行ldapmodify命令
ldapmodify -h host -c -v -D uid=bjensen,ou=People,dc=example,dc=com -w - -f modify.ldif
如果不通過文件修改,則需手動輸入如上所有紅色字體內容
3,刪除條目,刪除條目是可以通過指定文件內容刪除,手動輸入需刪除條目
ldapmodify -h host -D uid=bjensen,ou=People,dc=example,dc=com -w -
dn: uid=bcubbins,ou=People,dc=example,dc=com
changetype: delete
deleting entry uid=bcubbins,ou=People,dc=example,dc=com
4,移動條目,即更改條目uid,dn
ldapmodify -h host -D uid=hmiller,ou=people,dc=example,dc=com -w -
changetype: modrdn
newrdn: uid=jwallace
deleteoldrdn: 0
newsuperior: ou=special users,dc=example,dc=com
deleteoldrdn:0,保留原dn;1,刪除原dn。再進行條目移動時,需具有import和export權限。