shell脚本示例(二)
在这个脚本中我将会一步一步的完全手动添加用户,并不使用useradd和passwd两个命令,而是直接通过在/etc/passwd,/etc/shadow,/etc/group,几个文件中添加数据而创建用户。
通过本示例,你可以完全了解linux创建用户,创建密码的完整详细过程,以及各个字段的意义,帮助你理解linux对用户的管理。脚本有点长,如果你能专心的读完比理解肯定会有一点收获的!
首先我先说一下脚本的流程:
1. 输入一个用户名,判断是否存在
2. 输入密码通过openssl生成密码
3. 判断其他是否采取默认,如果非默认,则执行一下步骤
4. 输入UID,并判断
5. 同理GID
6. 让用户输入想要创建的家目录,如果存在,告诉用户,是否创建新的目录。
7. 通过查询/etc/shells让用户输入希望选取的shell
8. 输入备注
9. 创建用户,
10. 修改用户的家目录权限,及属主属组
#!/bin/bash
#option this script will enable you to add a user!
#USERNAME
CONDITION=y
while [ $CONDITION == 'y' ];do
#实现添加多个用户
read -p "Input username: " TUSERNAME
#确定用户名时,必须确定名字唯一,注意这里并没有判断名字的格式
grep "^$TUSERNAME:" /etc/passwd > /dev/null
RESULT1=`echo $?`
while [ $RESULT1 -eq 0 ];do
read -p "The user is exist!,Please input is again: " TUSERNAME
grep "^$TUSERNAME:" /etc/passwd > /dev/null
RESULT1=`echo $?`
done
echo "$TUSERNAME is your username! "
#END OF USERNAME
#CREATE PASSWD
read -p "Passwd: " TPASSWD
while [ $TPASSWD -lt 100 ];do
read -p "Passwd must longer than three character! " TPASSWD
done
SALT=`head -1 /dev/urandom | md5sum | cut -d' ' -f1 | sed 's@/(./{8/}/).*@/1@g'`
#此为选取八位随机数,作为生成密码的杂质
FPASSWD=`openssl passwd -1 -salt $SALT $TPASSWD`
#这是生成密码的过程,注意密码的格式
CURR=`date '+%s'`
CURRENTTIME=`expr $CURR / 86400`
echo "$TUSERNAME:$FPASSWD:$CURRENTTIME:0:99999:7:::" >> /etc/shadow
#注意/etc/shadow中每一行的格式
#END OF CREATE PASSWD
read -p "Would like to set others as default(y/n)? " RESULT6
if [ $RESULT6 == 'y' ];then
UGID=500
cut -d: -f3 /etc/passwd | grep "^$UGID$" > /dev/null
RESULT5=`echo $?`
while [ $RESULT5 -eq 0 ];do
let UGID+=1
cut -d: -f3 /etc/passwd | grep "^$UGID$" > /dev/null
RESULT5=`echo $?`
done
echo "$TUSERNAME:x:$UGID:$UGID::/home/$TUSERNAME:/bin/bash" >> /etc/passwd
#这里我采取了让UID和GID相同的方法,也是默认用passwd的结果
TDIR="/home/$TUSERNAME"
mkdir "$TDIR" > /dev/null
rsync /etc/skel/.* $TDIR > /dev/null
#创建一个新用户家目录中的文件来源于/etc/skel/中
cut -d: -f3 /etc/passwd | grep "^$TGID$" > /dev/null
RESULT7=`echo $?`
if [ $RESULT7 -eq 1 ];then
echo "$TUSERNAME:x:$UGID:" >> /etc/group
TGROUPNAME=$TUSERNAME
else
TGROUPNAME=`cut -d: -f1,3 /etc/group | grep "^$TGID$" | cut -d: -f1 `
fi
else
#UID
read -p "Input uid(between 500 and 65535): " TUID
#输入的UID必须在500和65535之间,但必须不能存在
cut -d: -f3 /etc/passwd | grep "^$TUID$" > /dev/null
RESULT2=`echo $?`
if [ $TUID -lt 500 ];then
RESULT2=0
fi
if [ $TUID -gt 65535 ];then
RESULT2=0
fi
while [ $RESULT2 -eq 0 ]
do
read -p "The uid is exist in the /etc/passwd(or notin 500~65535),please input another uid: " TUID
cut -d: -f3 /etc/passwd | grep "^$TUID$" > /dev/null
RESULT2=`echo $?`
if [ $TUID -lt 500 ];then
RESULT2=0
fi
if [ $TUID -gt 65535 ];then
RESULT2=0
fi
done
echo $TUID
#END OF UID
#create group
#用户输入的GID必须在500~65535之间,如果GID存在则告诉用户组的名字,否则直接创建
read -p "and the gid: " TGID
while [ $TGID -lt 500 ] || [ $TGID -gt 65535 ]
do
read -p "Please input another gid(between 500 and 65535): " TGID
done
cut -d: -f3 /etc/passwd | grep "^$TGID$" > /dev/null
RESULT3=`echo $?`
if [ $RESULT3 -eq 1 ];then
echo "$TGID is a new group ID! and groupname is : $TUSERNAME"
echo "$TUSERNAME:x:$TGID:" >> /etc/group
TGROUPNAME=$TUSERNAME
else
TGROUPNAME=`cut -d: -f1,3 /etc/group | grep "^$TGID$" | cut -d: -f1 `
echo "$TGID is exist in group,and group name is $TGROUPNAME . "
fi
#END OF GROUP
#输入家目录,存在的话告诉用户,并确认是否重新创建,同时并同步默认的几个文件
# the follow is the home of user's DIR
read -p "Which dir you want to as a home dir: " TDIR
if [ -e $TDIR ];then
read -p "the $TDIT is exist,would like to make a new dir?(y/n) " RESULT4
if [ $RESULT4 == 'Y' ] || [ $RESULT4 == 'y' ];then
read -p "Input the new dir: " TDIR
mkdir "$TDIR" >> /dev/null
fi
else
mkdir "$TDIR"
fi
rsync /etc/skel/.* $TDIR > /dev/null
#同步文件
#EDN OF CREATE DIR
#CREATE SHELL
read -p "The shell?`cat /etc/shells` " TSHELL
#END OF SHELL
#用户输入的shell必须可用,这里我没有判断,你可以添加几行代码,判断输入的shell是否可用
#creat user
read -p "you can write any commentary: " $TCOM
echo $TUSERNAME $TUID $TGID $TDIR $TSHELL $TPASSWD
echo "$TUSERNAME:x:$TUID:$TGID:$TCOM:$TDIR:$TSHELL" >> /etc/passwd
#手动添加用户的最重要的步骤!
echo
# useradd -u "558" -g "503" -d "$TDIR" -s "$TSHELL" $TUSERNAME
#
#end of creat user
fi
#更改用户的属主属组
chown $TUSERNAME:$TGROUPNAME $TDIR
chmod 700 $TDIR
echo "add user successfuly! "
read -p "if you like to continue input y others to quit: " CONDITION
done
不知道这CSDN怎么上传附件,只好给个链接了。
http://237654601.blog.51cto.com/2984671/559458
是我在51cto上的博客,内容一样,但多个附件,大家可以下载下来,共同学习!