昨天晚上在centos学习Samba配置共享文件服务器,还算顺利:二三个小时左右完成了SAMBA服务的配置,昨天根据头条文章依次执行和配置,只有一点点小错!
今天事情并不多,在学习shell脚本,就想能不能用shell脚本,一次性就把CentOS linux 的文件共享服务器完成呢?
shell脚本需要完成的实验需求:
网络拓扑结构图
shell脚本运行目标:
- 1、建立WORKGROUP工作组,建立Manage、Human、Salas帐号和访问密码;
- 2、建立拓扑图对应的之public、doc、soft共享文件夹、权限、访问;
- 3、自动完成smb.conf文件的修改,达到网络访问的目标;
- 4、自动运行smb服务,并加载到对应的启动项;
shell脚本在CentOS 7运行后的历史命令
Win7访问CentOS7 Samba文件服务器示意图
【Shell文件smbinstall.sh】
#!bai/bin/bash
sourcepack='samba'
smbpw="duoluo123"
group="WORKGROUP"
user1="Manage"
user2="Human"
user3="Salas"
share01="public"
share02="soft"
share03="doc"
#### 建立共享目录####
if [ -f "/etc/samba/smb.conf" ];then
echo "Samba is install...."
else
yum -y install samba*
fi
###建立共享目录######
if [ -d "/home/Data" ];then
echo "/home/Data is exist...."
else
mkdir /home/Data
fi
if [ -d "/home/Data/public" ];then
echo "/home/Data/public is exist...."
else
mkdir /home/Data/public
fi
if [ -d "/home/Data/doc" ];then
echo "/home/Data/doc is exist...."
else
mkdir /home/Data/doc
fi
if [ -d "/home/Data/soft" ];then
echo "/home/Data/soft is exist...."
else
mkdir /home/Data/soft
fi
if [ -f "/etc/samba/smb.conf_bak" ];then
cp -f /etc/samba/smb.conf_bak /etc/samba/smb.conf
else
cp /etc/samba/smb.conf /etc/samba/smb.conf_bak
fi
########建立用户组和对应的用户#############
egroup "^$group" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
groupadd "$group"
else
echo "hello"
fi
###################################################
##create user if not exists
#id $user >& /dev/null
#if [ $? -ne 0 ]
#then
# useradd -g $group $user
#fi
#create user if not exists
#egrep "^$user" /etc/passwd >& /dev/null
#if [ $? -ne 0 ]
#then
# useradd -g $group $user
#fi
#create user if not exists
##################################################
id $user1>& /dev/null
if [ $? -ne 0 ]
then
useradd "$user1" -g "$group"
else
echo "User is exist...."
fi
id $user2>& /dev/null
if [ $? -ne 0 ]
then
useradd "$user2" -g "$group"
else
echo "User is exist...."
fi
id $user3>& /dev/null
if [ $? -ne 0 ]
then
useradd "$user3" -g "$group"
else
echo "User is exist...."
fi
####################修改密码################################
#(echo 123;echo 123) | smbpasswd -s -a MySQL
#echo -e "123n123" | smbpasswd -s -a mysql
(echo "$smbpw";echo "$smbpw") | smbpasswd -a -s "$user1"
(echo "$smbpw";echo "$smbpw") | smbpasswd -a -s "$user2"
(echo "$smbpw";echo "$smbpw") | smbpasswd -a -s "$user3"
(echo "$user1" password is "$smbpw")
(echo "$user2" password is "$smbpw")
(echo "$user3" password is "$smbpw")
chgrp "$group" /home/Data
chgrp "$group" /home/Data/soft
chgrp "$group" /home/Data/doc
chgrp "$group" /home/Data/public
chmod 777 /home/Data
chmod 770 /home/Data/soft
chmod 770 /home/Data/doc
chmod 770 /home/Data/public
#########'workgroup = Samba'########
sed -i 's/workgroup = SAMBA/workgroup = '"$group"'/g' /etc/samba/smb.conf
####################################
sed -i '$a ['"$share01"']' /etc/samba/smb.conf
sed -i '$a comment = '"$share01"' Home Directories' /etc/samba/smb.conf
sed -i '$a path = /home/Data/'"$share01"'' /etc/samba/smb.conf
sed -i '$a valid users = '"$user1"','"$user2","$user3"',root' /etc/samba/smb.conf
sed -i '$a browseable = yes' /etc/samba/smb.conf
sed -i '$a read only = no' /etc/samba/smb.conf
sed -i '$a inherit acls = yes' /etc/samba/smb.conf
#########/etc/samba/smb.conf [soft]#######
sed -i '$a ['"$share02"']' /etc/samba/smb.conf
sed -i '$a comment = '"$share02"' Home Directories' /etc/samba/smb.conf
sed -i '$a path = /home/Data/'"$share02"'' /etc/samba/smb.conf
sed -i '$a valid users = '"$user1"','"$user2","$user3"',root' /etc/samba/smb.conf
sed -i '$a browseable = yes' /etc/samba/smb.conf
sed -i '$a read only = no' /etc/samba/smb.conf
sed -i '$a inherit acls = yes' /etc/samba/smb.conf
########/etc/samba/smb.conf [doc]#######
sed -i '$a ['"$share03"']' /etc/samba/smb.conf
sed -i '$a comment = '"$share03"' Home Directories' /etc/samba/smb.conf
sed -i '$a path = /home/Data/'"$share03"'' /etc/samba/smb.conf
sed -i '$a valid users = '"$user1"','"$user2","$user3"',root' /etc/samba/smb.conf
sed -i '$a browseable = yes' /etc/samba/smb.conf
sed -i '$a read only = no' /etc/samba/smb.conf
sed -i '$a inherit acls = yes' /etc/samba/smb.conf
####################################################################################
systemctl restart smb
systemctl enable smb
作者:王维翰,资深IT运维工程师,具备20多年IT及相关技术支持,为上海近千家中小企业、家庭用户提供过专业的IT技术支持服务;曾多次获“中小企业十佳项目经理”、“中小企业IT专家”!