因爲最近總是搭建NFS,雖然不復雜,可是很繁瑣。安裝服務、修改配置文件、手動掛載、寫入開機自動掛載等因而就寫了一個腳本git
做用:該腳本主要實現NFS自動安裝,客戶端的自動掛載、寫入開機自動掛載
使用環境:centos六、nfs客戶端的個數爲2個
參數:nfs服務端ip、第1個客戶端IP、第2個客戶端IP、第1個客戶端密碼、第2個客戶端密碼、NFS目錄
申明:該腳本在本人的服務器上跑是正常的,若是你要用於本身的環境需先測試,該腳本徹底處於做者本身愛好,使用腳本請三思。腳本中的參數請根據實際的狀況填寫,腳本內容以下:centos
#!/bin/bash ####################################################################################################################################### ## ## ##Function: this script mainly realizes NFS automatic installation and automatic mounting of client. ## ##Usage environment: the number of centos6 and NFS clients is 2. ## ##Parameters: NFS server side IP, first client IP, second client IP, first client password, second client password, NFS directory ## ##Author: heruiguo ## ####################################################################################################################################### #Check whether the IP address is legitimate PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin source /etc/rc.d/init.d/functions function check_ip() { IP=$1 VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}') if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then if [ $VALID_CHECK == "yes" ]; then echo "IP $IP Correct!" return 0 else echo "IP $IP no Correct!" return 1 fi else echo "IP error!" return 1 fi } #mkdir nfs dir mkdir_nfs_dir() { mkdir -p $nfs_dir } #start nfs server nfs_start() { service rpcbind restart service nfs restart } #Determine whether the server and client are installing NFS services. If there is no installation service, install and start it first. pd_nfs_install() { rpm -aq |grep nfs-utils >/dev/null if [ $? -eq 0 ];then echo "NFS service has been installed" else echo "############################The NFS service is being installed############################" yum install nfs-utils -y >/dev/null echo "############################The NFS service is being started############################" nfs_start fi } #NFS directory permissions definition qx="(rw,no_root_squash)" #Verify that NFS server side IP is legitimate while true; do read -p "Please enter the IP address of the NFS server: " NFS_SERVER_IP check_ip $NFS_SERVER_IP [ $? -eq 0 ] && break done #Verify whether the NFS client IP is legitimate while true; do read -p "Please enter the IP of the first NFS client: " nfs_client1 check_ip $nfs_client1 [ $? -eq 0 ] && break done #Verify whether the NFS client IP is legitimate while true; do read -p "Please enter the IP of second NFS clients: " nfs_client2 check_ip $nfs_client2 [ $? -eq 0 ] && break done read -p "Please enter the password for the first NFS client: " nfs_passwd_1 read -p "Please enter the second NFS client's password: " nfs_passwd_2 read -p "Please enter the NFS directory: " nfs_dir echo "###########################Server execution###################################" #1、Close the firewall service iptables stop #2、Create the NFS directory mkdir_nfs_dir #3、Increase the access rights of the client cat >/etc/exports<<EOF $nfs_dir $nfs_client1$qx $nfs_dir $nfs_client2$qx EOF #4、Start the NFS service nfs_start echo "###########################NFS customer 1 terminal execution###################################" sshpass -p $nfs_passwd_1 ssh root@$nfs_client1 -o StrictHostKeyChecking=no <<EOF server iptabes stop yum install nfs-utils -y service rpcbind start service nfs start mkdir -p $nfs_dir umount $nfs_dir mount -t nfs $NFS_SERVER_IP:$nfs_dir $nfs_dir sed -i '/nfs/d' /etc/fstab echo "$NFS_SERVER_IP:$nfs_dir $nfs_dir nfs defaults 0 0 " >>/etc/fstab EOF echo "###########################NFS customer 2 terminal execution###################################" sshpass -p $nfs_passwd_2 ssh root@$nfs_client2 -o StrictHostKeyChecking=no <<EOF server iptabes stop yum install nfs-utils -y service rpcbind start service nfs start mkdir -p $nfs_dir umount $nfs_dir mount -t nfs $NFS_SERVER_IP:$nfs_dir $nfs_dir sed -i '/nfs/d' /etc/fstab echo "$NFS_SERVER_IP:$nfs_dir $nfs_dir nfs defaults 0 0 " >>/etc/fstab EOF