昨天晚上在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專家”!