shell自动新建用户
时间: 2023-06-29 09:04:56 浏览: 140
在 Linux 系统中,可以使用 `useradd` 命令来创建新用户。以下是一个创建新用户的示例命令:
```
sudo useradd -m -s /bin/bash username
```
其中,`-m` 参数表示在创建用户时同时创建用户的家目录,`-s` 参数指定用户的默认 shell,`/bin/bash` 表示使用 Bash 作为默认 shell。`username` 则是要创建的新用户的用户名。
执行该命令后,系统会自动创建新用户,并在 `/home` 目录下创建与用户名相同的目录作为用户的家目录。如果需要为新用户设置密码,可以使用 `passwd` 命令。例如:
```
sudo passwd username
```
然后输入两次密码即可完成新用户的创建。
相关问题
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}"
```
这个脚本会提示你输入用户名、用户描述和密码,然后创建一个新用户,并将用户名、密码和主机名显示出来。
shell脚本创建用户
要在Linux系统上使用shell脚本创建用户,可以按照以下步骤进行:
1. 打开一个文本编辑器,例如Vi或Nano。
2. 在文本编辑器中输入以下命令:
```
#!/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 generated automatically.
read -p "Enter the username for the new account: " username
read -p "Enter the full name of the user: " fullname
read -s -p "Enter the password for the new account: " password
echo
useradd -c "${fullname}" -m ${username}
echo "${username}:${password}" | chpasswd
echo "User account has been created successfully!"
```
3. 保存脚本文件并退出编辑器。
4. 授予脚本执行权限,使用以下命令:
```
chmod +x scriptname.sh
```
5. 运行脚本,使用以下命令:
```
./scriptname.sh
```
6. 按照提示输入新用户的用户名、全名和密码,然后按Enter键。
7. 脚本将自动创建新用户并设置密码。
注意:使用脚本创建用户时要小心,确保只为授权的用户创建帐户,并且密码必须是安全的。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)