原简书主页 简书广告也太多了,转移阵地到这里

linux添加用户批处理脚本

划重点:

curl http://{host}/add_user.sh |sh -s "password"

分步执行

添加用户并设置密码

  1. 添加用户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
  1. 设置登录密码
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"
posted @ 2024-06-21 09:22  伊丽莎白菜  阅读(2)  评论(0编辑  收藏  举报