今天配置服務(wù)覺得有些麻煩纤怒,想著寫一個能一鍵配置nfs服務(wù)端的shell腳本糯而,當(dāng)然在安裝之前需要配置一個yum源,所以我一并將這些功能寫在了一個shell腳本里泊窘。
原文地址:RHEL7(Centos7)下使用shell腳本一鍵部署服務(wù)
腳本如下:
#author:Roya
#script name:RyumAnfs
#creation time:2020-01-23
#action:One click deployment of Yum source and NFS server
#!/bin/bash
echo "yum means Configure Yum source and nfs means Configure NFS server." #介紹yum和nfs的意思
read -p "please input yum or nfs: " a? #請輸入yum或者nfs
case $a in #使用case語句判斷輸入
yum)
echo "Prepare to configure Yum source...."
sleep 1? #等一秒執(zhí)行下面命令
echo "One moment please"
sleep 5
echo "/dev/cdrom /mnt iso9660 defaults 0 0" >> /etc/fstab && mount -a &> /dev/null && echo 'mount success!' # 將掛載信息寫入fstab
echo '[rhel]
name=rhel
baseurl=file:///mnt
gpgcheck=0
enabled=1' >> /etc/yum.repos.d/rhel.repo #yum源的配置文件
yum makecache &> /dev/null
if [ $? -eq 0 ]
then
echo "Yum source configuration successfully!"
else echo "Yum source configuration failed!"
fi
yum repolist | grep repolist
;;
nfs)
yum install -y nfs-utils &> /dev/null #安裝nfs服務(wù)端
if [ $? -eq 0 ]
then echo 'NFS service installed successfully!'
read -p 'Please enter NFS directory path you want to share: (Tips:/nfsdir)' nfsdir #輸入nfs共享的目錄
sleep 1
read -p 'Please enter the IP address of the host allowed to be shared: (Tips:192.168.1.1 or 192.168.1.*)' nfsIP #輸入允許共享的主機(jī)IP地址或者網(wǎng)段
sleep 1
read -p 'Please enter the permission of the shared host: (Tips:sync,rw,ro)' nfspwr #輸入允許共享的主機(jī)的權(quán)限
sleep 1
echo "Configuring..... Please wait."
sleep 10
if [ -e $nfsdir ] #判斷nfs共享目錄是否存在
then echo 'File Exists'
else mkdir $nfsdir
chmod -Rf 777 $nfsdir
fi
echo "$nfsdir $nfsIP($nfspwr)" > /etc/exports
systemctl restart nfs-server
if [ $? -eq 0 ]
then
IP=`ifconfig | awk -F ' ' 'NR==2{print$2}'` #使用awk命令提取出IP地址
exportfs -r
showmount -e $IP
if [ $? -eq 0 ] #判斷服務(wù)是否配置正確
then echo "NFS server has been configured successfully!"
else echo "NFS server has been configured failed!"
fi
fi
systemctl restart rpcbind
systemctl enable rpcbind &> /dev/null
systemctl enable nfs-server &> /dev/null #加入到開機(jī)自啟動
firewall-cmd --add-service=nfs --permanent &> /dev/null
firewall-cmd --add-service=rpc-bind --permanent &> /dev/null
firewall-cmd --reload &> /dev/null
else echo 'NFS service installion failed!'
fi
;;
*) #輸入yum或者nfs以外的任何字符都退出腳本
exit 0
;;
esac
chmod +x RyumAnfs.sh \\需要加一個執(zhí)行權(quán)限?
目前的功能:
一鍵配置本地yum源
配置nfs服務(wù)端
在客戶端只需掛載即可
echo "IP地址:共享目錄 掛載目錄 nfs defaults 0 0" >> /etc/fstab
mount -a
本文到此也就結(jié)束了熄驼,希望能對大家有所幫助像寒,由于水平有限,在很多地方肯定有些不足之處瓜贾,如有紕漏或者錯誤诺祸,還請斧正,定當(dāng)改進(jìn)祭芦。文章寫的很不容易筷笨,大家的評論和點(diǎn)贊就是我的最大動力,謝謝支持龟劲!