歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 批量創建Linux用戶賬號

批量創建Linux用戶賬號

日期:2017/2/28 16:02:58   编辑:Linux教程

本shell腳本用於批量創建Linux用戶賬號並加入群組

使用環境:在windows中使用Excel編輯好賬號、群組,使用Xmanager或其它方式上傳至Linux系統,然後使用sh運行該腳本即可。

批量創建Linux用戶賬號腳本下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/2011/11/21/批量創建Linux用戶賬號/

1. 使用excel准備賬號群組文件,A欄為賬號名,B欄為其加入的群組名:

2. 將其另存為:adduser.txt(默認使用ANSI編碼方式)

3. 使用xmanager等工具將adduser.txt上傳至Linux系統中(以/tmp/script/為例)。

4. 預先創建相應群組:

[root@LKCentOS ~]#groupadd group100

[root@LKCentOS ~]#groupadd group200

[root@LKCentOS ~]#groupadd group300

[root@LKCentOS ~]#groupadd group400

(以上也可用script完成)

5. 制作以下script,存為adduser.sh,並在root下運行sh adduser.sh:

#!/bin/bash

#Program:

# This program shows how to create accounts using a text file which include a lot of accounts and groups,etc.

#History:

#2011/11/20 LuoKun V1.0

#If you have any questions or suggestions,please write to [email protected].

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

# grep -v '^$' /tmp/script/adduser.txt 將空白行過濾掉;tr -d '\r'去除 DOS 檔案留下來的 ^M 斷行的符號。;expand -t 1將TAB按鍵轉成1個空白按鍵;cut -d ' ' -f1將adduser.txt第一欄(賬號)取出。

usernames=$(grep -v '^$' /tmp/script/adduser.txt|tr -d '\r'|expand -t 1|cut -d ' ' -f1)

i=0

for username in $usernames

do

#根據$i將adduser.txt中每個賬號對應的群組名取出。

i=$((i+1))

groupname=$(grep -v '^$' /tmp/script/adduser.txt|tr -d '\r'|expand -t 1|cut -d ' ' -f2|sed -n "${i}p")

#將賬號後面的群組設置為初始群組。

useradd $username -g $groupname

#將每個賬號密碼都設置成P@ssw0rd,並強制下次登錄時修改密碼。

echo "P@ssw0rd"|passwd --stdin $username

chage -d 0 $username

echo $username "has been created!"

done

6. 驗證:

1) 群組:

2) 賬號:

Copyright © Linux教程網 All Rights Reserved