linux添加用户批处理脚本
分步执行
添加用户并设置密码
1.添加用户unicom,uid与groupId均为1024,登录默认终端为bash,主目录为/home/unicom.
useradd -u 1024 -U -s /bin/bash -m -d /home/unicom unicom
查看添加结果:
cat /etc/passwd |grep unicom
输出如下:
unicom:x:1024:1024::/home/unicom:/bin/bash
2.设置登录密码
echo "mypwd" |passwd --stdin unicom
或者:
echo "unicom:mypwd" |chpasswd
赋予用户docker权限
usermod -aG docker unicom
如果提示用户组docker不存在,新建即可:
groupadd docker
赋予用户sudo权限
echo -e "\nunicom ALL=(ALL) ALL" >> /etc/sudoers
批处理及在线执行
命令整合
mkdir static
cat > ./static/add_user.sh <<-EOF
#!/bin/sh
useradd -u 1024 -U -s /bin/bash -m -d /home/unicom unicom
cat /etc/passwd |grep unicom
echo "Cuscri@1109" |passwd --stdin unicom
echo "unicom:Cuscri@1109" |chpasswd
echo "add user unicom,default password:Cuscri@1109"
groupadd docker
usermod -aG docker unicom
echo "append user to group docker"
chattr -ia /etc/sudoers
echo -e "\nunicom ALL=(ALL) ALL" >> /etc/sudoers
echo "append user to sudoers"
EOF
静态资源服务器配置
使用nginx作为静态资源服务器。nginx配置片段:
server {
listen 80;
location ~*\.(sh|svg|png|css|js)$ {
root /usr/share/nginx/static/;
}
}
docker-compose配置:
version: '3.8'
services:
nginx:
image: nginx:1.22.1-perl
container_name: nginx
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./static:/usr/share/nginx/static
fluent风格在线执行
curl http://${NGINX_HOST}/add_user.sh |sh