Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63

Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63

Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63

Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63

passwd 命令详解

用户与权限 · 设置用户认证信息 · 危险命令,请谨慎使用

语法

passwd [选项] [用户名]

常用选项

  • -d 删除密码(危险)
  • -l 锁定账号
  • -u 解锁账号
  • -e 强制下次登录改密
  • -S 查看密码状态

实用示例

  • Changing password for user linuxde.
  • New UNIX password: # 请输入新密码;
  • Retype new UNIX password: # 再输入一次;
  • passwd: all authentication tokens updated successfully. # 成功;
  • Changing password for user linuxde. # 更改linuxde用户的密码;
  • (current) UNIX password: # 请输入当前密码;

使用提示

批量场景可用 echo "user:pass" | chpasswd,但注意命令历史会留痕

原文摘录

passwd 命令用于设置用户的认证信息,包括用户密码、密码过期时间等。系统管理者则能用它管理系统用户的密码。只有管理者可以指定用户名称,一般用户只能变更自己的密码。
参考:http://man.linuxde.net/passwd

示例:

# 如果是普通用户执行 passwd 只能修改自己的密码。
# 如果新建用户后,要为新用户创建密码,则用 passwd 用户名,注意要以 root 用户的权限来创建。
$ passwd linuxde    # 更改或创建linuxde用户的密码;
Changing password for user linuxde.
New UNIX password:          # 请输入新密码;
Retype new UNIX password:   # 再输入一次;
passwd: all authentication tokens updated successfully. # 成功;

# 普通用户如果想更改自己的密码,直接运行 passwd 即可,比如当前操作的用户是 linuxde。
$ passwd
Changing password for user linuxde. # 更改linuxde用户的密码;
(current) UNIX password:   # 请输入当前密码;
New UNIX password:         # 请输入新密码;
Retype new UNIX password:  # 确认新密码;
passwd: all authentication tokens updated successfully. # 更改成功;

# 比如我们让某个用户不能修改密码,可以用`-l`选项来锁定:
$ passwd -l linuxde    # 锁定用户linuxde不能更改密码;
Locking password for user linuxde.
passwd: Success           # 锁定成功;

$ su linuxde   # 通过su切换到linuxde用户;
$ passwd      # linuxde来更改密码;
Changing password for user linuxde.
Changing password for linuxde
(current) UNIX password:          # 输入linuxde的当前密码;
passwd: Authentication token manipulation error     # 失败,不能更改密码;

$ passwd -d linuxde  # 清除linuxde用户密码;
Removing password for user linuxde.
passwd: Success                         # 清除成功;

$ passwd -S linuxde    # 查询linuxde用户密码状态;
Empty password.                         # 空密码,也就是没有密码;

相关命令

useradd · usermod · chage

内容与示例整理自 dunwu/linux-tutorial,遵循 CC BY-SA 4.0;本站保留原作者署名与许可,命令仅供学习,执行前请核对目标环境。