批量创建Linux用户账号

本shell脚本用于批量创建Linux用户账号并加入群组

使用环境:在windows中使用Excel编辑好账号、群组,使用Xmanager或其它方式上传至Linux系统,,然后使用sh运行该脚本即可。

批量创建Linux用户账号脚本下载:

免费下载地址在

用户名与密码都是

具体下载目录在 /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 luokun0810@hotmail.com.

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) 账号:

痛苦留给的一切,请细加回味!苦难一经过去,苦难就变为甘美。

批量创建Linux用户账号

相关文章:

你感兴趣的文章:

标签云: