如何在 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-tool而是使用ldb-tools

安装ldb-tools

apt install ldb-tools

现在我们可以使用ldb-toolsldbadd、、、、、、)直接搜索或修改 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 登录)

相关内容