linux添加用户批处理脚本
划重点:
curl http://{host}/add_user.sh |sh -s "password"
分步执行
添加用户并设置密码
- 添加用户developer,uid与groupId均为1024,登录默认终端为bash,主目录为/home/developer.
useradd -u 1024 -U -s /bin/bash -m -d /home/developer developer
查看添加结果:
cat /etc/passwd |grep developer
输出如下:
developer:x:1024:1024::/home/developer:/bin/bash
- 设置登录密码
echo "mypwd" |passwd --stdin unicom
或者:
echo "developer:mypwd" |chpasswd
赋予用户docker权限
usermod -aG docker developer
如果提示用户组docker不存在,新建即可:
groupadd docker
赋予用户sudo权限
echo -e "\ndeveloper ALL=(ALL) ALL" >> /etc/sudoers
批处理及在线执行
命令整合
mkdir static
cat > add_user.sh <<-EOF
#!/bin/bash
useradd -u 1024 -U -s /bin/bash -m -d /home/developer developer
cat /etc/passwd |grep developer
echo \$1 |passwd --stdin developer
echo "developer:\$1" |chpasswd
echo "add user developer,default password:\$1"
groupadd docker
usermod -aG docker developer
echo "append user to group docker"
chattr -ia /etc/sudoers
echo -e "\ndeveloper 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 -s "password"