linux用脚本修改用户密码

记录一下linux自动化脚本小操作

echo "修改后的密码" | passwd --stdin root

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux修改用户密码脚本,可以用shell脚本编写,主要功能是以管理员权限修改系统中指定用户密码。 首先用户需要输入目标用户用户名和新的密码,可以通过以下代码实现: ``` #!/bin/bash read -p "Enter the username: " username read -p "Enter the new password: " password ``` 然后需要判断用户是否存在,该用户是否为超级管理员,以及密码的复杂度是否符合要求。可以使用以下代码: ``` if ! id "$username" >/dev/null 2>&1; then echo "User $username does not exist" exit 1 fi if [ $(id -u) -ne 0 ]; then echo "Only root can run this script" exit 2 fi if [[ ${#password} -lt 8 ]] || [[ ! $password =~ [[:lower:]] ]] || [[ ! $password =~ [[:upper:]] ]] || [[ ! $password =~ [[:digit:]] ]]; then echo "Password does not meet complexity requirements" exit 3 fi ``` 接下来,需要使用passwd命令来修改密码,如下所示: ``` echo "$username:$password" | chpasswd ``` 最后需要输出修改结果,以及清除敏感数据: ``` echo "Password for $username has been changed successfully" unset password ``` 完整的修改用户密码脚本如下: ``` #!/bin/bash read -p "Enter the username: " username read -p "Enter the new password: " password if ! id "$username" >/dev/null 2>&1; then echo "User $username does not exist" exit 1 fi if [ $(id -u) -ne 0 ]; then echo "Only root can run this script" exit 2 fi if [[ ${#password} -lt 8 ]] || [[ ! $password =~ [[:lower:]] ]] || [[ ! $password =~ [[:upper:]] ]] || [[ ! $password =~ [[:digit:]] ]]; then echo "Password does not meet complexity requirements" exit 3 fi echo "$username:$password" | chpasswd echo "Password for $username has been changed successfully" unset password ``` 这样,我们就可以轻松实现修改Linux用户密码脚本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hobby云说

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值