linux创建添加用户脚本,RHCE认证考试-创建一个添加用户脚本
时间: 2024-01-07 15:05:27 浏览: 134
RHCE认证考试中可以使用以下脚本来创建和添加用户:
#!/bin/bash
# This script adds a new user to the Linux system.
# Check if the user is root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# Prompt for the username
read -p "Enter the username: " username
# Check if the username already exists
if id "$username" >/dev/null 2>&1; then
echo "User already exists"
exit 1
fi
# Prompt for the password
read -sp "Enter the password: " password
# Create the user
useradd $username -m -s /bin/bash
# Set the password for the user
echo "$username:$password" | chpasswd
# Print the username and password
echo "User $username created with password $password"
这个脚本会提示用户输入用户名和密码,并检查用户是否已经存在。如果用户不存在,脚本会创建一个新用户并设置密码。最后,脚本会输出新用户的用户名和密码。
相关推荐
















