linux编写一个创建用户的脚本
时间: 2023-12-03 11:03:14 浏览: 82
linux用户创建
您好!以下是一个创建用户的脚本示例:
```
#!/bin/bash
# This script creates a new user account
# Make sure the script is being executed with superuser privileges
if [ $(id -u) -ne 0 ]; then
echo "This script must be run with superuser privileges." >&2
exit 1
fi
# Get the username
read -p "Enter username: " USERNAME
# Get the password
read -s -p "Enter password: " PASSWORD
echo
#Create the user
useradd -m $USERNAME
# Set the password for the user
echo "$USERNAME:$PASSWORD" | chpasswd
# Force the user to change their password on first login
passwd -e $USERNAME
# Display the username, password, and host where the account was created
echo "Account created:"
echo "Username: $USERNAME"
echo "Password: $PASSWORD"
echo "Host: $(hostname)"
```
您可以通过在终端中运行`bash create_user.sh`来运行上述脚本。运行之后,您会被要求输入新用户的用户名和密码。脚本会自动为您创建该新用户,并将其密码设置为您刚刚输入的密码。最后,脚本会显示创建的新用户的用户名、密码和主机名。
希望这可以帮到你!
阅读全文