文、小州老師

D018.jpg 

§章節:

  1. 前言
  2. 使用者帳號清單的格式
  3. 設定使用者帳號本身的密碼
  4. 開始寫 shell script
  5. 其他使用技巧作法
  6. 總結

§前言 

對於有用過 linux 的人來說,使用者帳號的建立應該是很常見的操作,比方如下的操作方式:

         # adduser jonhy

         # passwd jonhy

      要建立的使用者帳號很少時候可以簡單用命令列快速敲入幾個指令即可。不過若是要建立的帳號數量很多時候,手動命令輸入的方式會顯得很不方便也容易出錯。所以下面來簡單介紹一下如何寫個 shell script 達成該需求。

§使用者帳號清單的格式:

要大量建立使用者帳號,前提當然是需要有一份帳號清單資訊,所以暫定格式規劃如下:

         username:password

 其中 username 表示帳號名稱,password 表示該帳號應對的密碼。該兩個欄位使用 : 為分隔即可。比方一份清單內容如下:

         peter:hello123

           allen:cvs1211oz

實際上可能會有人在 windows 上的 excel 建立帳號與密碼資訊,那可以匯出成為 csv 等格式。不過 csv 格式是以逗點為分隔字元,所以下面要使用的話只要適當改一下程式碼就可以直接使用。

§設定使用者帳號本身的密碼:

    傳統使用者新增用 adduser 命令後,還需要用 passwd 設定密碼。不過使用 passwd 設定密碼通常需要輸入兩次確認,若是要用於 shell script 非交談的環境,可能不是很方便,所以得找個方式應對。

 

 

 

方式1:

 

        passwd 命令在大多的 linux 發行版本內,有支援 --stdin 的參數可以使用。

 

 

        [root@linux ~]# passwd --help

        Usage: passwd [OPTION...] <accountName>

          -k, --keep-tokens       keep non-expired authentication tokens

          -d, --delete            delete the password for the named account (root only)

          -l, --lock              lock the named account (root only)

          -u, --unlock            unlock the named account (root only)

          -f, --force             force operation

          -x, --maximum=DAYS      maximum password lifetime (root only)

          -n, --minimum=DAYS      minimum password lifetime (root only)

          -w, --warning=DAYS      number of days warning users receives before

                                  password expiration (root only)

          -i, --inactive=DAYS     number of days after password expiration when an

                                  account becomes disabled (root only)

          -S, --status            report password status on the named account (root only)

              --stdin             read new tokens from stdin (root only)

 

實際使用方式如下:

         # passwd --stdin peter

 這時候只要輸入密碼按下 enter 後即可完成密碼的異動。不過考慮於 shell script 非交談環境使用,轉向用管線方式導入:

         # echo "hello123" | passwd --stdin peter

方式2:

 

 

linux 發行版本內可以找到 chpasswd 命令,該命令可以透過 stdin 讀取一對使用者帳號與密碼,最後完成密碼的異動。使用方式如下:

         # echo "peter:hello123" | chpasswd

要注意的是,chpasswd 預設使用 des 的密碼加密格式,所以若要使用 md5 或者是其他密碼加密格式,要看一下自己系統該命令的參數支援。

 

[root@linux ~]# chpasswd --help

Usage: chpasswd [options]

 

Options:

  -c, --crypt-method        the crypt method (one of DES MD5 SHA256 SHA512)

  -e, --encrypted   supplied passwords are encrypted

  -h, --help                display this help message and exit

  -m, --md5         use MD5 encryption instead DES when the supplied passwords are not encrypted

 §開始寫 shell script

 這邊開始動工寫 shell script,程式碼很單純沒有考慮太多很細部問題,先要求可以工作為主:

 

        #!/bin/bash

 

 

        DATA_FILE="account.txt"

 

        while read line

        do

                username=$(echo "$line" | cut -d : -f 1)

                passwd=$(echo "$line" | cut -d : -f 2)

                adduser $username

                echo "$passwd" | passwd --stdin $username

        done < $DATA_FILE

 

程式碼透過迴圈來完成讀入檔案每一行內容,然後透過 cut 命令以逗點為分隔字元取出第 1,2 個使用者帳號與密碼欄位,最後呼叫 adduser passwd 命令完成使用者新增的動作。若是要改成呼叫 chpasswd 命令設定密碼,程式碼如下一份:

         #!/bin/bash

         DATA_FILE="account.txt"

         while read line

        do

                username=$(echo "$line" | cut -d : -f 1)

                passwd=$(echo "$line" | cut -d : -f 2)

                adduser $username

                echo "$username:$passwd" | chpasswd    # 這邊同 echo "$line" | chpasswd

        done < $DATA_FILE

§其他使用技巧作法:

 

除了剛剛方式之外,這邊順便補充幾個額外的東西可以看情況使用。大多發行版本內使用者新增都有 adduser useraddadduser 命令各家使用方式會有點差別,不過 useradd 命令應該都差不多使用方式。在 useradd 命令有個 -p 參數可用:

 

[root@linux ~]# useradd

Usage: useradd [options] LOGIN

Options:

  -b, --base-dir BASE_DIR       base directory for the new user account home directory

        ......

  -p, --password PASSWORD       use encrypted password for the new user account

 

依據說明來看可以在新增時候直接把密碼套用進去,不過密碼需要是 DES/MD5 等等這類已經加密過可用的格式,那剛好可以透過 openssl 套件包提供的 openssl 命令達成該需求。

         [root@linux ~]# openssl passwd --help

        Usage: passwd [options] [passwords]

        where options are

        -crypt             standard Unix password algorithm (default)

        -1                 MD5-based password algorithm

        -apr1              MD5-based password algorithm, Apache variant

        -salt string       use provided salt

        -in file           read passwords from file

        -stdin             read passwords from stdin

        -noverify          never verify when reading password from terminal

        -quiet             no warnings

        -table             format output as table

        -reverse           switch table columns hello123 的話,可以這樣使用:

     # openssl passwd -crypt "hello123"             (這是 DES 加密演算法格式)

     # openssl passwd -1 "hello123"                    (這是 MD5 加密演算法格式)

     # echo "hello123" | openssl passwd -1 -stdin     (這是 MD5 加密演算法格式,密碼由標準輸入讀入)

不過很可惜這個版本的 openssl 命令還找不到如何產生 SHA256 等更高級的演算法加密格式.... 最終程式碼改改如下:

 

        #!/bin/bash

 

        DATA_FILE="account.txt"

 

        while read line

        do

                username=$(echo "$line" | cut -d : -f 1)

                passwd=$(echo "$line" | cut -d : -f 2)

                passwd2=$(openssl passwd -1 "$passwd")

                useradd -p "$passwd2" $username

        done < $DATA_FILE 

§總結:

    這篇簡單談到 linux 上面如何大量新增使用者帳號的方式,過程中談到幾個小指令的應用,也可以簡單了解到熟悉 shell script 對於系統管理的優點,提供給有需要的朋友們參考。

20LOGO.jpg  

 

arrow
arrow

    聯成電腦 發表在 痞客邦 留言(0) 人氣()