linux学习-CentOS 7 环境下大量建置账号的方法

一些账号相关的检查工具


  • pwck

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

[root@study ~]# pwck
user 'ftp': directory '/var/ftp' does not exist
user 'avahi-autoipd': directory '/var/lib/avahi-autoipd' does not exist
user 'pulse': directory '/var/run/pulse' does not exist
pwck: no changes

相对应的群组检查可以使用 grpck 这 个指令的啦!


  • pwconv

这个指令主要的目的是在『将 /etc/passwd 内的账号与密码,移动到 /etc/shadow 当中!』早期的 Unix 系统当中并没有 /etc/shadow 呢,所以,用户的登入密码早期是在 /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 并不会有上述两点问题啊! _。不过,如果手动设定账号,这个 pwconv 就很重要啰!


  • pwunconv

对于 pwconv , pwunconv 则是『将 /etc/shadow 内的密码栏数据写回 /etc/passwd 当中, 并且 删除 /etc/shadow 文件。』这个指令说实在的,最好不要使用啦! 因为他会将你的 /etc/shadow 删除 喔!如果你忘记备份,又不会使用 pwconv 的话,很严重呢!


  • chpasswd

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

[root@study ~]# echo "vbird3:abcd

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

#!/bin/bash
# This shell script will create amount of linux login accounts for you.
# 1. check the "account.txt" file exits? 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:
# http://linux.vbird.org/linux_basic/0410accountmanager.php#manual_amount
# 2015/07/22    uetucci

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 need.
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
                "ceate")
                        [ "${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
原文地址:https://www.cnblogs.com/uetucci/p/7678285.html