对于用NIS+NFS+automount来管理服务器群的管理员来说,给服务器建立账户以及HOME目录是件很痛苦的事情,特别说如果有几台HOME目录挂载服务器。过程其实还是比较简单的,先在NIS主服务器上建立账户,添加挂载路径到auto.home文件中。然后再将账户同步到slave NIS服务器上,再然后再登录HOME目录服务器,在其上上建立用户的HOME目录,拷贝环境脚本,更改目录owner权限。整个过程是件很令人郁闷的事情。
干脆就来用脚本来一步到位吧。小小脚本,内容粗俗不堪,不成敬意。
我的设置环境如下:
上海是NIS服务器, 也为上海用户的HOME服务器,IP:192.168.0.2
深圳使用上海NIS账户,自动挂载本地HOME目录。IP: 192.168.1.2
上海与深圳之间已经设置了免ROOT密码输入。
HOME目录所处路径都为: /mnt/home
自动挂载HOME的NIS文件: /etc/auto.home
#!/bin/sh
- ####################################################
- ## 脚本功能:只需要在主服务器上,输入用户名,UID,服务器点,##
- ## 即可自动创建HOME目录,账户,挂载信息,同步NIS,发送邮件 ##
- ## 通知给添加人员。不需要登录各地服务器。算得上减轻负担的 ##
- ## 好帮手,但脚本内容缺陷很多,只适合我的服务器环境, ##
- ## 思路简单易懂, ##
- #####################################################
################################################
######## define variable ########
################################################
HOME="/mnt/home"
DATE=`date +%F-%R`
SOFTWAREGID="502" #新添加用户的GID
SHENZHEN_IP="192.168.1.2" #深圳服务器IP地址
SHANGHAI_IP="192.168.0.2" #上海服务器IP地址
LOGFILE="/tmp/logfile.txt" #临时的日志文件。
INITIAL_PASSWD="Linux2010" #初始化密码
SHENZHEN_BACK_DIRECTORY="/backup/nisdatabase_backup" #用来保存上海的密码文件到深圳。
######===================================######
###### the global function defination ######
######===================================######
######## read input variable ########
create_account_input()
{
echo "What's the username need created:"
read USERNAME ##新用户的用户名
echo "which site it is?[shanghai/shenzhen]:"
while read LOCATION ##新用户是使用上海,还是深圳服务器。
do
case $LOCATION in ##添加个判断
shanghai)
break;;
shenzhen)
break;;
*)
echo "Wrong site, please try again"
;;
esac
done
echo "What's the USERID: "
read USERID #新用户的UID
}
######## create home function ########
mkdir_home_directory() ##建立HOME目录函数。
{
case "$LOCATION" in
shanghai)
if [ ! -d $HOME/$USERNAME ]
then
mkdir $HOME/$USERNAME
cp /etc/skel/.* $HOME/$USERNAME
chown -R $USERID $HOME/$USERNAME
else
exit 1
fi
;;
shenzhen)
ssh $SHENZHEN_IP "mkdir $HOME/$USERNAME"
ssh $SHENZHEN_IP "cp /etc/skel/.* $HOME/$USERNAME"
ssh $SHENZHEN_IP "chown -R $USERID $HOME/$USERNAME"
;;
*)
echo "Seems the wrong server site, please check \
if it's shanghai or shenzhen. "
break
;;
esac
}
######## create home function ########
add_to_homefile() ##添加到auto.home中。
{
case "$LOCATION" in
shanghai)
echo "##### add new user $USERNAME at $DATE by carywu ####" >> /etc/auto.home
echo "$USERNAME 10.18.11.242:/mnt/home/&" >> /etc/auto.home
;;
shenzhen)
ssh $SHENZHEN_IP "echo '##### add new user $USERNAME at $DATE by carywu' >> /etc/auto.home"
ssh $SHENZHEN_IP "echo '$USERNAME 10.28.8.10:/mnt/home/&' >> /etc/auto.home"
;;
esac
}
######## update nis function ########
update_nis() ##同步nis账户函数
{
cd /var/yp
make
#/etc/init.d/nis restart
echo "we are going to backup system account to shenzhen server......"
scp /etc/auto.home root@$SHENZHEN_IP:$SHENZHEN_BACK_DIRECTORY/auto.home_back_$DATE
scp /etc/passwd root@$SHENZHEN_IP:$SHENZHEN_BACK_DIRECTORY/passwd_back_$DATE
scp /etc/shadow root@$SHENZHEN_IP:$SHENZHEN_BACK_DIRECTORY/shadow_back_$DATE
scp /etc/group root@$SHENZHEN_IP:$SHENZHEN_BACK_DIRECTORY/group_back_$DATE
# ssh $SHENZHEN_IP "/etc/init.d/nis restart"
}
######## create account ########
create_account() ##添加账户函数
{
echo "Begin to create account for new user......"
useradd -u $USERID -g $SOFTWAREGID -M -s /bin/bash -p $INITIAL_PASSWD $USERNAME
echo "Begin to create home directory......"
mkdir_home_directory
echo "Begin to create home automount......"
add_to_homefile
update_nis
echo "update NIS database finished......"
}
create_samba_account() ##添加samba账户函数
{
case "$LOCATION" in
shanghai)
echo "Begin to create samba user...... "
(echo $INITIAL_PASSWD; echo $INITIAL_PASSWD ) |smbpasswd -s -a $USERNAME
echo "Checking samba user $USERNAME......"
smbclient -L 127.0.0.1 -U $USERNAME%$INITIAL_PASSWD
;;
shenzhen)
echo "Begin to create samba user......"
ssh $SHENZHEN_IP "(echo $INITIAL_PASSWD; echo $INITIAL_PASSWD ) |smbpasswd -s -a $USERNAME "
echo "Checking samba user $USERNAME......"
smbclient -L $SHENZHEN_IP -U $USERNAME%$INITIAL_PASSWD
;;
esac
}
create_log_mail() ## 添加邮件内容函数
{
echo "Your Linux account is: $USERNAME " >> $LOGFILE
echo "Your Initial password is: $INITIAL_PASSWD " >> $LOGFILE
echo "Your Initial samba password is: $INITIAL_PASSWD " >> $LOGFILE
if [ $LOCATION == "shanghai" ]
then
echo "The Linux server is: $SHANGHAI_IP " >> $LOGFILE
else
echo "The Linux server is: $SHENZHEN_IP " >> $LOGFILE
fi
echo "####################################### " >> $LOGFILE
echo "NOTE: You can type yppasswd to change the initial Linux password, \
or type smbpasswd to change the initial samba password. \
please let me know if you have any question about the server.\
my mail address is: [email protected] " >> $LOGFILE
}
send_mail() #发送邮件函数
{
SUBJECT=" Your Linux Account"
FROM="[email protected]"
echo "Do U wanna send mail? [y/n]:"
read ANSWER
if [ $ANSWER == "y" ]
then
mail -s "$SUBJECT" -r $FROM $USERNAME\@example.com < $LOGFILE
rm $LOGFILE
else
rm $LOGFILE
fi
}
create_account_input && create_account && create_samba_account && create_log_mail && send_mail