shell脚本自动创建用户举例
时间: 2023-11-17 13:01:53 浏览: 185
shell脚本 自动创建用户详解及实例代码
可以使用以下的shell脚本自动创建用户:
```shell
#!/bin/bash
# This script creates a new user on the local system.
# You will be prompted to enter the username (login), the person name, and a password.
# The username, password, and host for the account will be displayed.
# Make sure the script is being executed with superuser privileges.
if [[ "${UID}" -ne 0 ]]
then
echo 'Please run with sudo or as root.' >&2
exit 1
fi
# Get the username (login).
read -p 'Enter the username to create: ' USER_NAME
# Get the real name (contents for the description field).
read -p 'Enter the name of the person or application that will be using this account: ' COMMENT
# Get the password.
read -p 'Enter the password to use for the account: ' PASSWORD
# Create the user with the password.
useradd -c "${COMMENT}" -m ${USER_NAME}
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
# Force password change on first login.
passwd -e ${USER_NAME}
# Display the username, password, and the host where the user was created.
HOSTNAME=$(hostname)
echo "Username: ${USER_NAME}"
echo "Password: ${PASSWORD}"
echo "Host: ${HOSTNAME}"
```
这个脚本会提示你输入用户名、用户描述和密码,然后创建一个新用户,并将用户名、密码和主机名显示出来。
阅读全文