思路:
涉及到的文件:
1、/etc/passwd
echo“ $ACCOUNT:x:$[$MAXUID+1]:$[$MAXGID+1]::/home/$ACCOUNT:/bin/bash” >>/etc/passwd
2、/etc/shadow
chmod u+w /etc/shadow
echo “$ACCOUNT::::::::”>>/etc/shasow
chmod u-w /etc/shadow
3、/etc/group
echo “$ACCOUNT:x:$[$MAXGID+1]:” >> /etc/group
4、家目录
mkdir /home/$ACCOUNT
cp -a /etc/skel/. /home/$ACCONT
chown -R $ACCOUNT:$ACCOUNT /home/$ACCOUNT
chmod 700 /home/$ACCOUNT
5、MAIL
touch /var/spool/mail/$ACCOUNT
chmod 660 /var/spool/mail/$ACCOUNT
chown $ACCOUNT:mail /var/spool/mail/$ACCOUNT
6、口令
echo “123” | passwd --stdin $ACCOUNT
执行脚本时,遇到错误的时候最好打开调试开关:bash -x 脚本文件,或者在脚本中可能出现错误的地方添加一条 set -x 指令。
[root@localhost ~]# vim useradd.sh
#!bin/bash
read -p "please input one user : " ACCOUNT
grep -q $ACCOUNT /etc/passwd && echo "the user $ACCOUNT is exist" && exit
#test current max uid and gid
MAXUID=`cut -f3 -d: /etc/passwd |sort -n |tail -n 2 |head -n 1`
if [ $MAXUID -lt 500 ];then
MAXUID=500
fi
#!bin/bash
read -p "please input one user : " ACCOUNT
grep -q $ACCOUNT /etc/passwd && echo "the user $ACCOUNT is exist" && exit
#test current max uid and gid
MAXUID=`cut -f3 -d: /etc/passwd |sort -n |tail -n 2 |head -n 1`
if [ $MAXUID -lt 500 ];then
MAXUID=500
fi
MAXGID=`cut -f4 -d: /etc/passwd |sort -n |tail -n 2 |head -n 1`
if [ $MAXGID -lt 500 ];then
MAXGID=500
fi
#change /etc/passwd
echo "$ACCOUNT:x:$[$MAXUID+1]:$[$MAXGID+1]::/home/$ACCOUNT:/bin/bash" >>/etc/passwd
#change /etc/shawdow
chmod u+w /etc/shadow
echo "$ACCOUNT::::::::" >>/etc/shadow
chmod u-w /etc/shadow
#change /etc/group
echo "$ACCOUNT:x:$[$MAXGID+1]:" >>/etc/group
#create home dir
mkdir /home/$ACCOUNT
cp -a /etc/skel/. /home/$ACCOUNT
chown -R $ACCOUNT:$ACCOUNT /home/$ACCOUNT
chmod 700 /home/$ACCOUNT
#create user mailbox
touch /var/spool/mail/$ACCOUNT
chmod 660 /var/spool/mail/$ACCOUNT
chown -R $ACCOUNT:mail /var/spool/mail/$ACCOUNT
#change user passwd
echo "123" |passwd --stdin $ACCOUNT &>/dev/null