大网数据广告活动
服务器运维问答

Changing root password via script fails but reports success

· 52CE 我爱测

I have a project which requires me to make a script / sequence of commands to change the root password on a CentOS 7 installation. I can assume the user that will be executing the script is already root. Currently, I'm using the …

问题原文

原帖链接 · example6 · CC BY-SA 3.0

I have a project which requires me to make a script / sequence of commands to change the root password on a CentOS 7 installation.

I can assume the user that will be executing the script is already root.

Currently, I'm using the following, and getting a success message:

[root@localhost]# echo -e ‘password\npassword’ | (passwd --stdin root)
Changing password for user root.
passwd: all authentication tokens updated successfully.

However, this doesn't actually change the password. Upon further inspection (manually setting the password), I notice the password password fails a dictionary check, but still reports success at the end:

[root@localhost]# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.

One possible solution is to try hashing the given password based on the method and salt provided in /etc/shadow, but I would like to avoid an overly complicated solution. I could also check whether the value in /etc/shadow actually changed, but that ignores the case in which the password is set to what it already was, which I would like to support.

Does anyone have a suggestion on trying to validate the password before actually setting it, or, preferably, getting the FULL output from passwd ?

已采纳解答

原帖链接 · Paul Haldane · CC BY-SA 3.0

When using passwd --stdin you only need to send the password once. So correct version is

echo password | passwd --stdin

If your code sample is accurate then (a) you've got smart quotes there when you need straightforward forward quotes and (b) the password is probably being set to something like `password\npassword' (possibly with smart quotes either side).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。