1. 使用useradd 命令結合shell命令來實現
1)創建一個名為“userlist”的文件,並在該文件中添加要創建的用戶名。
[root@server ~]#
cat userlist
test1 test2 test3 test4
2)編寫腳本
[root@server ~]# for i in $(cat userlist);do useradd -p `cat /etc/shadow | grep root | gawk -F: '{print $2}'` $i;done
3)測試結果
[root@server ~]# cat /etc/passwd | grep test
test1:x:500:500::/home/test1:/bin/bash
test2:x:501:501::/home/test2:/bin/bash
test3:x:502:502::/home/test3:/bin/bash
test4:x:503:503::/home/test4:/bin/bash
2. 使用newusers、pwunconv、chpasswd、pwconv等工具來實現
1)創建一個名為“userlist”的文件,並在該文件中寫入每個新用戶的用戶名、UID、自家目錄等,每一行都要按照“/etc/passwd”文件中的格式來寫,其中口令這欄可以為空或者用“X”代替,如下:
[root@server ~]# cat userlist
test1:x:500:500::/home/test1:/bin/bash
test2:x:501:501::/home/test2:/bin/bash
test3:x:502:502::/home/test3:/bin/bash
test4:x:503:503::/home/test4:/bin/bash
2)執行newusers(/usr/sbin/newusers)命令,從“userlist”文件中導入用戶,如下:
[root@server ~]# newusers < userlist
3)創建一個名為“password”的文件,在該文件中寫入每個新用戶的用戶名及其對應的口令,如下:
[root@server ~]# cat password
test1:redhat
test2:redhat
test3:redhat
test4:redhat
4)使用pwunconv(/usr/sbin/pwunconv)對“/etc/shadow”中的口令進行解密,把解密後的口令存放於“/etc/passwd”中,並刪除“/etcshadow”中的口令這一欄,如下:
[root@server ~]# pwunconv
[root@server ~]#cat /etc/passwd | grep test
test1:$1$Koy28tZu$lNv69ov6wrxH39FTbVlk3/:500:500::/home/test1:/bin/bash
test2:$1$Koy28tZu$lNv69ov6wrxH39FTbVlk3/:501:501::/home/test2:/bin/bash
test3:$1$Koy28tZu$lNv69ov6wrxH39FTbVlk3/:502:502::/home/test3:/bin/bash
test4:$1$Koy28tZu$lNv69ov6wrxH39FTbVlk3/:503:503::/home/test4:/bin/bash
5)使用chpasswd(/usr/sbin/chpasswd)導入用戶的口令到“/etc/passwd”中,如下:
[root@server ~]# chpasswd < password
6)使用pwconv(/usr/sbin/pwconv)將已導入到“/etc/passwd”中的口令加密到“/etc/shadown”中,並將“/etc/passwd”中的口令為用“X”代替,如下
[root@server ~]# pwconv
此時可以看到,“/etc/passwd”中的口令已經顯示為“X”,而“/etc/shadow”中的口令已為經過加密後的口令,任務完成
3. 通過腳本(使用useradd、awk、pwunconv、chpasswd、pwconv等工具)來實現
1)創建一個名為“userlist”的文件,編輯該文件,並在該文件中添加用戶名和口令,對應格式如下:
[root@server ~]# cat userlist
test1:redhat
test2:redhat
test3:redhat
test4:redhat
2)創建名為“create_user.sh”的腳本文件,編輯該文件內容,如下:
[root@server ~]# cat create_user.sh
#!/bin/bash
for t in `gawk -F: '{print $1}' $1`
do
useradd $t
done
pwunconv
chpasswd < $1
pwconv
3)添加執行權限,執行腳本,如下:
[root@server ~]#
chmod +x ./creat_user.sh ;./create_user.sh userlist
4. 改進後的批量用戶創建和刪除腳本
1)編寫“useradd.sh”腳本,如下:
#! /bin/bash
for i in `seq 1 5`
do
case "$1" in
add|[aA][dD][dD])
useradd test$i
echo "redhat" | passwd --stdin test$i
;;
del|[dD][eE][lL])
userdel -r test$i
;;
*)
echo $"Usage: $0 {add|del}"
exit 1
esac
done
exit $?
2)添加執行權限,執行腳本,如下:
[root@server ~]# chmod +x ./useradd.sh;./useradd.sh add
Changing password for user test1.
passwd: all authentication tokens updated successfully.
Changing password for user test2.
passwd: all authentication tokens updated successfully.
Changing password for user test3.
passwd: all authentication tokens updated successfully.
Changing password for user test4.
passwd: all authentication tokens updated successfully.
Changing password for user test5.
passwd: all authentication tokens updated successfully.
3)查看結果
4)刪除
[root@server ~]# ./useradd.sh del
5)查看刪除結果
注意:方法4的腳本僅限於類redhat系統,其他linux系統passwd命令無—stdin參數