用 `usermod -L` 锁定(和解锁)用户密码

离职员工或暂时停用的账号,`usermod -L` 一个参数即可禁用密码登录——但它拦不住 SSH 密钥,这一点常被忘记。

Linux禁用用户usermod 用户管理passwd 密码修改账户中风险
运行 `sudo usermod -L 用户名` 锁定密码,`sudo usermod -U 用户名` 解锁。

01命令

命令
$ sudo usermod -L username

02作用说明

`-L` 会在 /etc/shadow 中的密码哈希前加一个 `!`,使所有密码校验失败,而账号本身、文件和 UID 都保持原样,无需删除重建。手册里容易被忽略的提醒:SSH 公钥登录不受影响,所以要彻底禁用用户还需将 shell 换成 `usermod -s /usr/sbin/nologin 用户名` 或设置过期日期(`usermod -e 1 用户名`)。`passwd -l` 效果与 `-L` 相同,但对无密码账号其解锁命令 `passwd -u` 会失败。

03风险

风险中风险
使用条件Requires root; usermod is part of the shadow package, preinstalled on all Linux distros
!注意: 锁定密码不会终止已登录的会话,也拦不住 SSH 密钥——彻底禁用请配合 `usermod -s /usr/sbin/nologin`。

04撤销

撤销
$ sudo usermod -U username

05来源

  • usermod(8) — modify a user account
    man7.org · technical · 2026-09-17
    依据: Documents -L (prepend ! to encrypted password, disabling it) and -U (unlock); notes SSH key auth may still allow login

最后验证: 2026-09-17 · 分类: 安全与隐私