出于安全原因禁用用户 shell

出于安全原因禁用用户 shell

我们为需要细粒度权限的自动化任务创建了多个用户帐户,例如跨系统的文件传输、监控等。

我们如何锁定这些用户帐户,使这些“用户”没有 shell,无法登录?我们希望防止有人可以通过 SSH 作为这些用户帐户之一登录。

答案1

您可以使用该usermod命令更改用户的登录 shell。

usermod -s /sbin/nologin myuser

或者

usermod -s /usr/sbin/nologin myuser

如果您的操作系统不提供 /sbin/nologin,您可以将 shell 设置为 NOOP 命令,例如 /bin/false:

usermod -s /bin/false myuser

答案2

更改登录 shell 并不一定会阻止用户进行身份验证(除了在某些检查 中提到的用户 shell 的服务中/etc/shells)。

人们可能仍然能够对您的系统向 UNIX 用户提供的各种服务进行身份验证,并且仍然可能被授权执行某些操作,尽管可能无法直接运行任意命令。

将 shell 更改为/bin/false/usr/sbin/nologin只会阻止它们在那些可用于运行命令的服务上运行命令(控制台登录、ssh、telnet、rlogin、rexec...),因此影响授权仅适用于某些服务。

例如ssh,这仍然允许他们进行端口转发。

passwd -l将禁用密码身份验证,但仍然允许用户使用其他身份验证方法(例如authorized_keyswith ssh)。

pam至少在 Linux 上,您可以使用该模块pam_shells来限制对具有允许的 shell 的用户进行身份验证或授权(参考资料中提到的那些/etc/shells)。对于ssh,您需要在授权 ( account) 级别执行此操作,以用于身份验证sshd用途pam 此外到其他身份验证方法(例如authorized_keys),或者您可以使用(例如和朋友)sshd_config中的指令来执行此操作。/etc/ssh/sshd_configAllowUsers

但请注意,在全局 pam 授权中添加一些限制可能会阻止cron以这些用户身份运行作业。

答案3

首先,使用 禁用密码passwd -l username

另请注意for 选项man的页面:passwd-l

   -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).

       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.

答案4

您可以使用 chsh 命令:

~# chsh myuser

根据要求输入新的 shell 详细信息:

Login Shell [/bin/sh]: /bin/nologin

或更短的版本:

~# chsh myuser -s /bin/nologin

相关内容