鸟哥的linux私房菜学习笔记《三十》手动新增用户

来源:互联网 发布:如何评价沈光远 知乎 编辑:程序博客网 时间:2024/05/16 10:32
  1. 检查工具:pwck, pwconv, pwunconv, chpasswd
    pwck 这个指令在检查 /etc/passwd 这个帐号配置文件内的信息,与实际的主文件夹是否存在等信息, 还可以比对 /etc/passwd /etc/shadow 的信息是否一致,另外,如果 /etc/passwd 内的数据字段错误时,会提示使用者修订。 一般来说,我只是利用这个玩意儿来检查我的输入是否正确就是了。

    [root@CentOS ~]# pwckuser 'adm': directory '/var/adm' does not existuser 'uucp': directory '/var/spool/uucp' does not existuser 'gopher': directory '/var/gopher' does not existuser 'oprofile': directory '/home/oprofile' does not existuser 'avahi-autoipd': directory '/var/lib/avahi-autoipd' does not existuser 'saslauth': directory '/var/empty/saslauth' does not existuser 'pulse': directory '/var/run/pulse' does not existpwck: no changes

    pwconv这个指令主要的目的是在“将 /etc/passwd 内的帐号与密码,移动到 /etc/shadow 当中!” 早期的 Unix 系统当中并没有 /etc/shadow 呢,所以,使用者的登陆密码早期是在 /etc/passwd 的第二栏,后来为了系统安全,才将密码数据移动到 /etc/shadow 内的。使用 pwconv 后,可
    以:
    (1)比对 /etc/passwd 及 /etc/shadow ,若 /etc/passwd 内存在的帐号并没有对应的/etc/shadow 密码时,则 pwconv 会去 /etc/login.defs 取用相关的密码数据,并创建该帐号的 /etc/shadow 数据;
    (2)若 /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
    chpasswd 是个挺有趣的指令,他可以“读入未加密前的密码,并且经过加密后, 将加密后的密码写入 /etc/shadow 当中。”这个指令很常被使用在大量创建帐号的情况中喔! 他可以由Standard input 读入数据,每笔数据的格式是“ username:password ”。 举例来说,我的系统当中有个使用者帐号为 vbird3 ,我想要更新他的密码 (update) , 假如他的密码是abcdefg 的话,那么我可以这样做:

    [root@CentOS ~]# echo "kevin:zbcdefg" | chpasswd -m# 使用MD5加密# 再将密码改回原来的[root@CentOS ~]# passwd kevinChanging password for user kevin.New password: Retype new password: passwd: all authentication tokens updated successfully.
  2. 特殊账号的手工新建
    脚本下载地址链接

    #!/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:# http://linux.vbird.org/linux_basic/0410accountmanager.php#manual_amount# 2015/07/22    VBirdexport PATH=/bin:/sbin:/usr/bin:/usr/sbin# 0. userinputusergroup=""                   # 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 fileaction="${1}"                  # "create" is useradd and "delete" is userdel.if [ ! -f accountadd.txt ]; then    echo "There is no accountadd.txt file, stop here."        exit 1fi[ "${usergroup}" != "" ] && groupadd -r ${usergroup}rm -f outputpw.txtusernames=$(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]"            ;;    esacdone

    脚本输入文件:

    [root@study ~]# vim accountadd.txtstd01std02std03std04std05[root@study ~]# sh accountadd.sh createChanging password for user std01.passwd: all authentication tokens updated successfully.
  3. 批量新建账号的范例(适用于数字学号)
    脚本下载地址链接

    #!/bin/bash## 這支程式主要在幫您建立大量的帳號之用,更多的使用方法請參考:# http://linux.vbird.org/linux_basic/0410accountmanager.php#manual_amount## 本程式為鳥哥自行開發,在 CentOS 5.x 上使用沒有問題,# 但不保證絕不會發生錯誤!使用時,請自行負擔風險~## History:# 2005/09/05    VBird   剛剛才寫完,使用看看先~# 2009/03/04    VBird   加入一些語系的修改與說明,修改密碼產生方式 (用 openssl)export LANG=zh_TW.big5export PATH=/sbin:/usr/sbin:/bin:/usr/binaccountfile="user.passwd"# 1. 進行帳號相關的輸入先!echo ""echo "例如我們崑山四技的學號為: 4960c001 到 4960c060 ,那麼:"echo "帳號開頭代碼為         :4"echo "帳號層級或年級為       :960c"echo "號碼數字位數為(001~060):3"echo "帳號開始號碼為         :1"echo "帳號數量為             :60"echo ""read -p "帳號開頭代碼 ( Input title name, ex> std )======> " username_startread -p "帳號層級或年級 ( Input degree, ex> 1 or enter )=> " username_degreeread -p "號碼部分的數字位數 ( Input \# of digital )======> " nu_nuread -p "起始號碼 ( Input start number, ex> 520 )========> " nu_startread -p "帳號數量 ( Input amount of users, ex> 100 )=====> " nu_amountread -p "密碼標準 1) 與帳號相同 2)亂數自訂 ==============> " pwmif [ "$username_start" == "" ]; then        echo "沒有輸入開頭的代碼,不給你執行哩!" ; exit 1fi# 判断数字系统testing0=$(echo $nu_nu     | grep '[^0-9]' )testing1=$(echo $nu_amount | grep '[^0-9]' )testing2=$(echo $nu_start  | grep '[^0-9]' )if [ "$testing0" != "" -o "$testing1" != "" -o "$testing2" != "" ]; then        echo "輸入的號碼不對啦!有非為數字的內容!" ; exit 1fiif [ "$pwm" != "1" ]; then        pwm="2"fi# 2. 开始输入账号与面文件![ -f "$accountfile" ] && mv $accountfile "$accountfile"$(date +%Y%m%d)nu_end=$(($nu_start+$nu_amount-1))for (( i=$nu_start; i<=$nu_end; i++ ))do    nu_len=${#i}    if [ $nu_nu -lt $nu_len ]; then        echo "数值的位数($i->$nu_len)已經比你設定的位數($nu_nu)還大!"        echo "程序无法继续"        exit 1    fi    nu_diff=$(( $nu_nu - $nu_len ))    if [ "$nu_diff" != "0" ]; then        nu_nn=0000000000        nu_nn=${nu_nn:1:$nu_diff}    fi        account=${username_start}${username_degree}${nu_nn}${i}        if [ "$pwm" == "1" ]; then                password="$account"        else                password=$(openssl rand -base64 6)        fi        echo "$account":"$password" | tee -a "$accountfile"done# 3. 开始建立账号与密码!echo "账号面建立中,请稍等!"cat "$accountfile" | cut -d':' -f1 | xargs -n 1 useradd -mchpasswd < "$accountfile"pwconvecho "OK!建立完成!"

    将上述建立的账号删除

    #!bin/bashusername=$( cat user.passwd | cut -d ':' -f 1 )for username in $usernamesdo    echo "userdel -r $username"    userdel -r $usernamedone
  4. 情景模拟
    情境仿真题一:想将本服务器的帐号分开管理,分为单纯邮件使用,与可登陆系统帐号两种。其中若为纯邮件帐号时, 将该帐号加入 mail 为初始群组,且此帐号不可使用bash 等 shell 登陆系统。若为可登陆帐号时, 将该帐号加入 youcan 这个次要群组。
    目标:了解 /sbin/nologin 的用途;
    前提:可自行观察使用者是否已经创建等问题;
    需求:需已了解 useradd, groupadd 等指令的用法; 解决方案如下:
    预先察看一下两个群组是否存在?

    [root@study ~]# grep mail /etc/group[root@study ~]# grep youcan /etc/group[root@study ~]# groupadd youcan

    可发现 youcan 尚未被创建,因此如上表所示,我们主动去创建这个群组啰。开始创建三个邮件帐号,此帐号名称为 pop1, pop2, pop3 ,且密码与帐号相同。可使用如下的程序来处理:

    [root@study ~]# vim popuser.sh#!/bin/bashfor username in pop1 pop2 pop3douseradd -g mail -s /sbin/nologin -M $usernameecho $username &#124; passwd --stdin $usernamedone[root@study ~]# sh popuser.sh

    开始创建一般帐号,只是这些一般帐号必须要能够登陆,并且需要使用次要群组的支持!所以:

    [root@study ~]# vim loginuser.sh#!/bin/bashfor username in youlog1 youlog2 youlog3douseradd -G youcan -s /bin/bash -m $usernameecho $username &#124; passwd --stdin $usernamedone[root@study ~]# sh loginuser.sh
0 0