shell脚本一键搭建NFS服务脚本

一键安装NFS服务脚本
环境:centos7

#!/bin/bash
#作者:张小白
#联系方式:2367127577@qq.com
#日期:2021年11月4日
#版本:NFS服务一键搭建脚本
#	两部分,1.安装服务(判断是否安装服务) 2.配置文件/etc/exports
#设置全局变量
PATH=${PATH}:/root/
LANG=zh_CN.UTF-8
hong="\033[31m"
huang="\033[33m"
lv="\033[32m"
se="\033[0m"


#来个检查程序是否成功执行函数
check_ok(){
	if [ $? != 0 ]
	then 
	    echo -e ""$hong"程序出现错误,请检查日志"$se""
	exit 1
	fi
	}

#安装服务函数
myyum(){
	s=`rpm -qa |grep "^$1"|wc -l`
	if [ "$s" == 0 ]
	then 
	    yum install -y  $1
	    check_ok
	else
	    echo -e " serveice  $1  already occure yuo can use "$huang"systemctl start $1 "$se""
	fi
check_ok
	}
#配置环境
myyum iptables-services                                  #是否需要下载iptables服务
iptables-save > /etc/sysconfig/iptables_`date +%F`      #备份原有防火墙规则
iptables -F                                             #清空防火墙规则
check_ok
#关闭selinux
sed 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config -i
s_num=`getenforce`
if [ "$s_num" == Enforcing ];
then 
    setenforce 0
fi
check_ok

#安装服务
nfs_n=`rpm -qa |grep nfs-utils|wc -l`
if [ $nfs_n -gt 0 ]
then
    echo "nfs-utils与rpcbind服务已存在,不需要安装,你可以通过编辑 /etc/exports来添加客户端" 
    echo "/etc/exports配置格式:dir  ip(rw,sync,no_root_squash,anonuid="uid",anongid="gid",)"
    systemctl restart nfs-utils
    systemctl restart nfs
    systemctl restart rpcbind
    check_ok
    exit
fi
check_ok
myyum nfs-utils 
systemctl start nfs-utils
systemctl start nfs

myyum rpcbind
systemctl start rpcbind
check_ok
#配置文件
while :
do
read -p "请输入想要共享的目录(默认为你的家目录):" home
read -p "是否加入新的客户端IP或网络,添加完毕直接按 ENTER:" ip
if [ -z $home ]
then 
    home="~/"
fi
check_ok	
if [ -z $ip ]
then
    break
else
    uid=`cat /etc/passwd|grep nfsnobody|awk -F ":" {'print $3'}`
    gid=`cat /etc/passwd|grep nfsnobody|awk -F ":" {'print $4'}`
    echo " $home  $ip(rw,sync,no_root_squash,anonuid="$uid",anongid="$gid",)" >> /etc/exports 
fi
done
check_ok
echo "NFS 服务安装完成,请在客户端安装nfs-utils 并使用showmount 192.168.127.10(服务端ip)来查看可以挂载的目录。 "
exit
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值