一键安装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