linux 修改用户密码脚本
时间: 2023-05-10 13:50:41 浏览: 405
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用户密码的脚本。