如何禁用用户登录?

Jes*_*erg 4 ubuntu

我用--disabled-login命令创建了一个用户。然后我不得不更改密码,以便我可以登录用户并测试一些东西。我现在想再次禁用登录,我看到了这篇文章: `adduser --disabled-login` 有什么作用?

所以我使用sudo passwd user然后将其设置为!. 但是,当我尝试登录时,我可以使用密码登录!。那么我如何再次禁用它?

请注意,我也尝试过*作为密码。

Ark*_*zyk 7

我认为你的困惑源于你不明白是什么!

加密的密码存储在/etc/shadow. 例如,在创建一个名为的新用户new-user并为其提供12345678密码后,我们会得到以下条目:

$ sudo cat /etc/shadow
(...)
new-user:$6$zVbJcpZE$Bqnxr5cDkwjKOE06iAZu7/qIuH9UGXex28TU/aD0osft9DfdPVzcVwq2j410YxoPlZR310.heZyxaQq4iwWy9.:18038:0:99999:7:::
Run Code Online (Sandbox Code Playgroud)

您现在可以new-user通过执行su new-user并输入12345678密码来 切换到。您可以禁用密码 new-user通过前面加上!这样的:

$ sudo cat /etc/shadow
(...)
new-user:!$6$zVbJcpZE$Bqnxr5cDkwjKOE06iAZu7/qIuH9UGXex28TU/aD0osft9DfdPVzcVwq2j410YxoPlZR310.heZyxaQq4iwWy9.:18038:0:99999:7:::
Run Code Online (Sandbox Code Playgroud)

从现在开始,new-user即使提供了正确的密码,您也将无法切换到:

$ su new-user
Password:
su: Authentication failure
Run Code Online (Sandbox Code Playgroud)

请注意,/etc/shadow手动修改非常危险,不建议使用。您可以使用sudo passwd -l new-user. 正如man passwd所说:

   -l, --lock
       Lock the password of the named account. This option
       disables a password by changing it to a value which matches
       no possible encrypted value (it adds a ´!´ at the beginning
       of the password).
Run Code Online (Sandbox Code Playgroud)

例如:

$ sudo passwd -l new-user
passwd: password expiry information changed.
Run Code Online (Sandbox Code Playgroud)

但是,请注意,passwd -l不会禁用帐户,它只会禁用密码,这意味着用户仍然可以使用其他方法登录系统,如下所述man passwd

       Note that this does not disable the account. The user may
       still be able to login using another authentication token
       (e.g. an SSH key). To disable the account, administrators
       should use usermod --expiredate 1 (this set the account's
       expire date to Jan 2, 1970).

       Users with a locked password are not allowed to change
       their password.
Run Code Online (Sandbox Code Playgroud)