> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/centos/shell/pi-liang-tian-jia-yong-hu.md).

# 批量添加用户

**批量创建user1至user10，要求设置随机16位密码，包含数字、大小写字母、符号。并要求用户使用密码首次登录**

```shell
#!/usr/bin/env bash
for i in {1..10}
do
    # 创建十个用户
    useradd user${i} && echo user${i} is created
    # 随机生成密码
    pass=$(cat /dev/urandom |tr -dc '0-9a-zA-Z!@_#?.,'|head -c 16)
    # 将用户和密码导出
    echo user${i}:---pass:${pass} >> /root/user.log
    # 设置密码
    echo ${pass} |passwd --stdin user${i} &>/dev/null
    # 强制下次登录修改密码
    passwd -e user${i} &>/dev/null
done
```
