• CentOS 7环境下大量创建账号的方法


    1 一些账号相关的检查工具

    1.1 pwck命令

    pwck 这个指令在检查 /etc/passwd 这个账号配置文件内的信息,与实际的家目录是否存在等信息,还可以比对 /etc/passwd /etc/shadow 的信息是否一致,另外,如果 /etc/passwd 内的数据字段错误时,会提示使用者修订。

     

    上面仅是告知我,这些账号并没有家目录,由于那些账号绝大部分都是系统账号,确实也不需要家目录的,所以,那是正常的错误!

     

    1.2 pwconv命令

    这个指令主要的目的是在将 /etc/passwd 内的账号与密码,移动到 /etc/shadow 当中!

    使用 pwconv 后,可以:

    •        比对 /etc/passwd 及 /etc/shadow ,若 /etc/passwd 内存在的账号并没有对应的 /etc/shadow 密码时,则 pwconv 会去 /etc/login.defs 取用相关的密码数据,并建立该账号的 /etc/shadow 数据;

    •        若 /etc/passwd 内存在加密后的密码数据时,则 pwconv 会将该密码栏移动到 /etc/shadow 内,并将原本的/etc/passwd 内相对应的密码栏变成 x !

    一般来说,如果您正常使用 useradd 增加使用者时,使用 pwconv 并不会有任何的动作,因为/etc/passwd 与 /etc/shadow 并不会有上述两点问题。

    1.3   pwunconv命令

     pwunconv 则是将 /etc/shadow 内的密码栏数据写回 /etc/passwd 当中, 并且删除 /etc/shadow 文件。这个指令说实在的,最好不要使用。

     

    1.4 chpasswd命令

    chpasswd 可以读入未加密前的密码,并且经过加密后, 将加密后的密码写入 /etc/shadow 当中。这个指令很常被使用在大量建置账号的情况中。他可以由 标准输入中读入数据,每笔数据的格式是『 username:password 』。 举例来说,我的系统当中有个用户账号为 vbird3 ,我想要更新他的密码 , 假如他的密码是 abcdefg 的话,那么我可以这样做:

     

     在预设的情况中, chpasswd 会去读取 /etc/login.defs 文件内的加密机制,我们 CentOS 7.x 用的是 SHA512, 因此 chpasswd 就预设会使用 SHA512 来加密!如果你想要使用不同的加密机制,那就得要使用 -c 以及 -e 等方式来处理了。 不过从 CentOS 5.x 开始之后,passwd 已经默认加入了 --stdin 的选项,因此这个 chpasswd 就变得英雄无用武之地了,不过,在其他非 Red Hat 衍生的 Linux 版本中,或许还是可以参考这个指令功能来大量建置账号。

     

    2.大量建置账号模板(适用 passwd --stdin 选项)

    一个简单的 script 来执行新增用户的功能:

    #!/bin/bash
    # This shell script will create amount of linux login accounts for you.
    # 1. check the "accountadd.txt" file exist? you must create that file manually.
    #    one account name one line in the "accountadd.txt" file.
    # 2. use openssl to create users password.
    # 3. User must change his password in his first login.
    # 4. more options check the following url:
    
    export PATH=/bin:/sbin:/usr/bin:/usr/sbin
     
    # 0. userinput
    usergroup=""                   # if your account need secondary group, add here. 
    pwmech="openssl"               # "openssl" or "account" is needed.
    homeperm="no"                  # if "yes" then I will modify home dir permission to 711 
    
    # 1. check the accountadd.txt file 
    action="${1}"                  # "create" is useradd and "delete" is userdel.
    if [ ! -f accountadd.txt ]; then
        echo "There is no accountadd.txt file, stop here."
        exit 1 
    fi
    [ "${usergroup}" != "" ] && groupadd -r ${usergroup} 
    rm -f outputpw.txt
    usernames=$(cat accountadd.txt)
    for username in ${usernames} 
     do
        case ${action} in
            "create")
                [ "${usergroup}" != "" ] && usegrp=" -G ${usergroup} " || usegrp=""
                  useradd ${usegrp} ${username}               # 新增账号
                [ "${pwmech}" == "openssl" ] && usepw=$(openssl rand -base64 6) || usepw=${username}
                  echo ${usepw} | passwd --stdin ${username}  # 建立密码             chage -d 0 ${username}                      # 强制登入修改密码             [ "${homeperm}" == "yes" ] && chmod 711 /home/${username}        echo "username=${username}, password=${usepw}" >> outputpw.txt
                ;;
             "delete")
                echo "deleting ${username}"
                userdel -r ${username}
                ;;
              *)             
                echo "Usage: $0 [create|delete]"
                ;;     
        esac 
     done

    接下来只要建立 accountadd.txt 这个文件即可。内容每一行一个账号。 而是否需要修改密码?是否与账号相同的信息等等,你可以自由选择!若使用 openssl 自动猜密码时,用户的密码请由 outputpw.txt 去找。

  • 相关阅读:
    游戏开发热门技术浅析
    SpringMVC文件分片上传,断点续传
    浏览器文件分片上传,断点续传
    网页文件分片上传,断点续传
    Web文件分片上传,断点续传
    JavaScript文件分片上传,断点续传
    js文件分片上传,断点续传
    html5文件分片上传,断点续传
    vue文件分片上传,断点续传
    csharp文件分片上传,断点续传
  • 原文地址:https://www.cnblogs.com/ericz2j/p/12045969.html
Copyright © 2020-2023  润新知