脚本中实现修改用户密码
平时在写脚本的时候, 有时候会需要使用passwd
命令来修改用户的密码.
例如为NIS
的测试服务器新增一些测试用户, 并且为这些用户设置密码.
由于passwd
需要交互式的输入, 所以在脚本中使用passwd
命令会报错.
一个潜在的解决方案是加入--stdin
参数但是有的linux
发行版中passwd
命令中的--stdin
不存在.
如果你还这样执行的话将会显示以下错误.
passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]
Options:
-a, --all report password status on all accounts
-d, --delete delete the password for the named account
-e, --expire force expire the password for the named account
-h, --help display this help message and exit
-k, --keep-tokens change password only if expired
-i, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-l, --lock lock the password of the named account
-n, --mindays MIN_DAYS set minimum number of days before password
change to MIN_DAYS
-q, --quiet quiet mode
-r, --repository REPOSITORY change password in REPOSITORY repository
-R, --root CHROOT_DIR directory to chroot into
-S, --status report password status on the named account
-u, --unlock unlock the password of the named account
-w, --warndays WARN_DAYS set expiration warning days to WARN_DAYS
-x, --maxdays MAX_DAYS set maximum number of days before password
change to MAX_DAYS
由于passwd
命令中没有--stdin
选项, 所以我们需要使用其他的方式来解决这个问题.