编译环境:Centos6.4.x86_64
要求:
创建账号 不能使用useradd 可以使用password 一次性创建user10-------user20
编辑shell脚本:
1 #!/bin/bash
2 read -p "please input first account " FIRSTUSER
3 read -p "please input last account " LASTUSER
4 FIRST=${FIRSTUSER##*[[:alpha:]]}
5 LAST=${LASTUSER##*[[:alpha:]]}
6 NAME=${LASTUSER%%[0-9]*}
7 # find current max uid and max gid
8 MAXUID=`cut -d: -f 3 /etc/passwd |sort -n |tail -n 1`
9 MAXGID=`cut -d: -f 4 /etc/passwd |sort -n |tail -n 1`
10 #create user account,modify /etc/passwd,/etc/shadow,/etc/group
11 for I in $(seq $FIRST $LAST);do
12 echo "$NAME$I:x:$[$MAXUID+1]:$[$MAXGID+1]::/home/$NAME$I:/bin/bash">>/etc/passwd
13 chmod 600 /etc/shadow
14 echo "$NAME$ID::::::::" >>/etc/shadow
15 echo "$NAME$ID:x:$[$NAME$ID+1]:" >>/etc/group
16 #create home dir and mail file
17 mkdir /home/$NAME$I
18 cp -r /etc/skel/. /home/$NAME$I
19 chown -R $NAME$I:$NAME$I /home/$NAME$I
20 touch /var/spool/mail/$NAME$I
21 chown $NAME$I:mail /var/spool/mail/$NAME$I
22 #create user password
23 echo "123" |passwd --stdin $NAME$I >/dev/null
24 let MAXUID=$MAXUID+1
25 let MAXGID=$MAXGID+1
26 done
执行bash命令:
[root@huangzhong ~]# bash useradd.sh
please input first account u10
please input last account u20
查看所创建的用户
[root@huangzhong ~]# tail /etc/passwd
u10:x:507:508::/home/u10:/bin/bash
u11:x:508:509::/home/u11:/bin/bash
u12:x:509:510::/home/u12:/bin/bash
u13:x:510:511::/home/u13:/bin/bash
u14:x:511:512::/home/u14:/bin/bash
u15:x:512:513::/home/u15:/bin/bash
u16:x:513:514::/home/u16:/bin/bash
u17:x:514:515::/home/u17:/bin/bash
u18:x:515:516::/home/u18:/bin/bash
u19:x:516:517::/home/u19:/bin/bash
u20:x:517:518::/home/u20:/bin/bash
找一个用户登录看一下:
转载于:https://blog.51cto.com/tyjhz/1396816