#!/bin/bash
# 定义目标用户名和新密码
username="root"
new_password="123465"
# 使用 passwd 命令修改目标用户的密码并检查结果
if echo -e "$new_password\n$new_password" | passwd $username; then
echo "Password changed successfully for user $username."
else
echo "Failed to change password for user $username."
fi
当我们逐行解释这个脚本的功能时,可以更详细地了解其作用:
#!/bin/bash
这是一个 bash 脚本,用于指定脚本使用的 shell 解释器。
# 定义目标用户名和新密码
username="your_username"
new_password="123465"
username 和 new_password。您需要将 your_username123465 替换为您想要设置的新密码。# 使用 passwd 命令修改目标用户的密码并检查结果
if echo -e "$new_password\n$new_password" | passwd $username; then
echo "Password changed successfully for user $username."
else
echo "Failed to change password for user $username."
fi
echo -e "$new_password\n$new_password" | passwd $usernameecho -e表示启用转义字符的解析,以便将两次密码输入作为多行字符串传递给 passwd 命令。$new_password的值将替换为实际的新密码,而 $username 的值将替换为目标用户名。"your_username" 替换为目标用户名,将 "123465"替换为所需的密码,并执行脚本即可修改目标用户的密码。