如何重命名samba4網域中的使用者?

如何重命名samba4網域中的使用者?

如何變更基於 samba4 的網域中使用者的登入名稱?

我嘗試閱讀 samba-tool 手冊頁,但它似乎沒有顯示任何我可以使用的內容。

答案1

假設我們使用 Ubuntu,並且 Samba 4 配置為 DC(Active Directory 網域控制器),並且我們想要變更使用者名稱Old User和登入名稱olduser。要重新命名使用者登入名,我們可以使用samba-tool

test-smb:~# samba-tool user edit olduser

這將開啟一個編輯器,顯示 LDAP 條目的內容。更改屬性sAMAccountNameuserPrincipalName,儲存並退出。您可能還想重新命名使用者的任何現有主目錄。

我們也可以直接編輯 LDAP 條目,而無需使用samba-toolbut 和ldb-tools.

安裝ldb-tools

apt install ldb-tools

現在我們可以使用ldb-tools( ldbadd,,,,,, )直接搜尋或修改LDAP資料庫ldbdelldbeditldbmodifyldbrenameldbsearch

找到 Samba LDAP 資料庫:

如果您安裝了 Ubuntu 打包版本samba,則應在 中找到此檔案/var/lib/samba/private/sam.ldb

我們先來看看 LDAP 資料庫中的該使用者:

搜尋資料庫:

我們使用ldbsearch以下語法:

ldbsearch -H <database-file> <ldap-filter>

使用 ,<ldap-filter>我們可以指定一個表達式來過濾搜尋傳回的條目。例如,我們可以sAMAccountName=olduser根據登入名稱屬性進行篩選或CN=Old User根據 CN(通用名稱)屬性進行篩選:

test-smb:~# ldbsearch -H /var/lib/samba/private/sam.ldb 'CN=Old User'
# record 1
dn: CN=Old User,CN=Users,DC=test-smb,DC=example,DC=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Old User
sn: User
givenName: Old
instanceType: 4
whenCreated: 20180904091809.0Z
whenChanged: 20180904091809.0Z
displayName: Old User
uSNCreated: 3841
name: Old User
objectGUID: 038979ea-107d-4c97-85bf-76d1f2326608
badPwdCount: 0
codePage: 0
countryCode: 0
badPasswordTime: 0
lastLogoff: 0
lastLogon: 0
primaryGroupID: 513
objectSid: S-1-5-21-3075026989-1808589244-366107480-1105
accountExpires: 9223372036854775807
logonCount: 0
sAMAccountName: olduser
sAMAccountType: 805306368
userPrincipalName: [email protected]
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=test-smb,DC=phys,DC=et
 hz,DC=ch
mail: [email protected]
loginShell: /bin/bash
pwdLastSet: 131805262894707270
userAccountControl: 512
uSNChanged: 3844
distinguishedName: CN=Old User,CN=Users,DC=test-smb,DC=example,DC=com

...

更改登入名屬性

rename-login.ldif建立一個包含以下內容的文字檔案 ( ):

dn: CN=Old User,CN=Users,DC=test-smb,DC=phys,DC=ethz,DC=ch
changetype: modify
replace: sAMAccountName
sAMAccountName: newuser
-
replace: userPrincipalName
userPrincipalName: [email protected]

這將修改屬性sAMAccountNameuserPrincipalName

test-smb:~# ldbmodify -H /var/lib/samba/private/sam.ldb rename-login.ldif
Modified 1 records successfully

透過重新命名 RDN(相對可分辨名稱)來重新命名 LDAP 條目

看起來無法使用 重新命名 LDAP 條目,samba-tool我們必須使用ldb-tools

test-smb:~# ldbrename -H /var/lib/samba/private/sam.ldb 'CN=Old User,CN=Users,DC=test-smb,DC=example,DC=com' 'CN=New User,CN=Users,DC=test-smb,DC=example,DC=com'
Renamed 1 record

這也將更改屬性cnname,但不會更改其他一些屬性,仍包含舊用戶名,如下一個搜尋所示:

test-smb:~# ldbsearch -H /var/lib/samba/private/sam.ldb 'CN=New User'
# record 1
dn: CN=New User,CN=Users,DC=test-smb,DC=example,DC=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
sn: User
givenName: Old
instanceType: 4
whenCreated: 20180904091809.0Z
displayName: Old User
uSNCreated: 3841
objectGUID: 038979ea-107d-4c97-85bf-76d1f2326608
badPwdCount: 0
codePage: 0
countryCode: 0
badPasswordTime: 0
lastLogoff: 0
primaryGroupID: 513
objectSid: S-1-5-21-3075026989-1808589244-366107480-1105
accountExpires: 9223372036854775807
sAMAccountType: 805306368
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=test-smb,DC=phys,DC=et
 hz,DC=ch
mail: [email protected]
loginShell: /bin/bash
pwdLastSet: 131805262894707270
userAccountControl: 512
lastLogonTimestamp: 131805264616461980
sAMAccountName: newuser
userPrincipalName: [email protected]
lastLogon: 131805271152497360
logonCount: 12
cn: New User
name: New User
whenChanged: 20180904100228.0Z
uSNChanged: 3847
distinguishedName: CN=New User,CN=Users,DC=test-smb,DC=example,DC=com

修改其餘屬性

要更改其他一些屬性,例如givenName,displayNamemail,我們可以使用:

samba-tool user edit newuser

並以互動方式編輯用戶或使用另一個用戶,ldbmodify如下所示:

rename-other-attrs.ldif建立一個包含以下內容的文字檔案 ( ):

dn: CN=New User,CN=Users,DC=test-smb,DC=phys,DC=ethz,DC=ch
changetype: modify
replace: givenName
givenName: New
-
replace: displayName
displayName: New User
-
replace: mail
mail: [email protected]

修改 LDAP 條目::

test-smb:~# ldbmodify -H /var/lib/samba/private/sam.ldb rename-other-attrs.ldif
Modified 1 records successfully

答案2

人們也可以嘗試顯而易見的方法:

/ # samba-tool user rename  --help
Usage: samba-tool user rename <username> [options]

Rename a user and related attributes.

This command allows to set the user's name related attributes. The user's
CN will be renamed automatically.
The user's new CN will be made up by combining the given-name, initials
and surname. A dot ('.') will be appended to the initials automatically
if required.
Use the --force-new-cn option to specify the new CN manually and the
--reset-cn option to reset this change.

Use an empty attribute value to remove the specified attribute.

The username specified on the command is the sAMAccountName.

The command may be run locally from the root userid or another authorized
userid.

The -H or --URL= option can be used to execute the command against a remote
server.

Example1:
samba-tool user rename johndoe --surname='Bloggs'

Example1 shows how to change the surname of a user 'johndoe' to 'Bloggs' on
the local server. The user's CN will be renamed automatically, based on
the given name, initials and surname.

Example2:
samba-tool user rename johndoe --force-new-cn='John Bloggs (Sales)' \
    --surname=Bloggs -H ldap://samba.samdom.example.com -U administrator

Example2 shows how to rename the CN of a user 'johndoe' to 'John Bloggs
(Sales)'.
Additionally the surname ('sn' attribute) is set to 'Bloggs'.
The -H parameter is used to specify the remote target server.

我發布此內容是因為 Active Directory 有許多「登入」定義(即可以使用 userPrincipalName,並且還有其他選項,例如我正在開發的應用程式允許用戶僅使用其 CN 登入)

相關內容