# passwd renu Changing password for user renu. New password: BAD PASSWORD: The password contains the user name in some form Retype new password: passwd: all authentication tokens updated successfully.
如果希望在一条命令中设置或更改密码,运行以下命令。它允许用户在一条命令中更新密码。
1 2 3
# echo "new_password" | passwd --stdin thanu Changing password for user thanu. passwd: all authentication tokens updated successfully.
方法-2:使用 chpasswd 命令
chpasswd 是另一个命令,允许我们为 Linux 中的用户设置、更改密码。如果希望在一条命令中使用 chpasswd 命令更改用户密码,用以下格式。
#!/bin/sh for user in `more user-list.txt` do echo "[email protected]" | passwd --stdin "$user" chage -d 0 $user done
给 password-update.sh 文件设置可执行权限。
1
# chmod +x password-update.sh
最后运行脚本来实现这一目标。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# ./password-up.sh
magi Changing password for user magi. passwd: all authentication tokens updated successfully. daygeek Changing password for user daygeek. passwd: all authentication tokens updated successfully. thanu Changing password for user thanu. passwd: all authentication tokens updated successfully. renu Changing password for user renu. passwd: all authentication tokens updated successfully.
方法-4:如何为多个用户设置相同的密码
如果要在 Linux 中为多个用户设置、更改相同的密码,使用以下脚本。
1 2 3 4 5 6 7 8
# vi password-update.sh
#!/bin/sh for user in `more user-list.txt` do echo "new_password" | passwd --stdin "$user" chage -d 0 $user done
#!/bin/bash for server in `cat server-list.txt` do ssh [email protected]$server 'passwd --stdin renu <<EOF new_passwd new_passwd EOF'; done
你将得到与我们类似的输出。
1 2 3 4 5 6 7 8 9 10
# ./password-update.sh
New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple Retype new password: Changing password for user renu. passwd: all authentication tokens updated successfully. New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple Retype new password: Changing password for user renu. passwd: all authentication tokens updated successfully.
[1] 07:58:07 [SUCCESS] CentOS.2daygeek.com Changing password for user root. passwd: all authentication tokens updated successfully. Stderr: New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple Retype new password: [2] 07:58:07 [SUCCESS] ArchLinux.2daygeek.com Changing password for user root. passwd: all authentication tokens updated successfully. Stderr: New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple
方法-7:如何使用 chpasswd 命令更改多个服务器中的用户密码
或者,我们可以使用 chpasswd 命令更新多个服务器中的用户密码。
1 2 3 4 5 6 7
# ./password-update.sh
#!/bin/bash for server in `cat server-list.txt` do ssh [email protected]$server 'echo "magi:new_password" | chpasswd' done