练习——shell脚本自动搭建nfs服务

项目思路:

  1. 关闭Selinux和防火墙(或者设置Selinux和防火墙策略)
  2. 测试网络是否畅通?
  3. 确认软件是否安装(rpcbind,Redhat8自动安装)
  4. 创建和发布共享目录(共享目录+共享网段+共享权限)
  5. 启动服务并设置开机自启动搭建完成后提示:
    NFS共享服务已搭建完成,欢迎下次使用。
  6. 客户端测试:
    showmount -e IP
    mount.nfs IP:/sharedir /your_dir
#!/bin/bash
#关闭防火墙和selinux
systemctl stop firewalld &> /dev/null
setenforce 0 &>/dev/null
echo "########防火墙和selinux已经关闭########"
#测试网络
ping -c 1 192.168.1.10 &>/dev/null
if [ $? -eq 0 ];then
    echo "########网络ok########"
else
   echo "########请检查你的网络########"
   exit
fi
##配置内网yum源
cd /etc/yum.repos.d/
cat >>zx.repo<<OK
[AppStream]
name=zx AppStream
baseurl=file:///run/media/root/RHEL-8-0-0-BaseOS-x86_64/AppStream
gpgcheck=0

[BaseOS]
name=zx BaseOS
baseurl=file:///run/media/root/RHEL-8-0-0-BaseOS-x86_64/BaseOS
gpgcheck=0
OK
dnf repolist
#安装相关软件
rpm -q rpcbind &> /dev/null
if [ $? -eq 0 ];then
	echo "Rpcbind has installed"
else
	dnf install rpcbind -y &> /dev/null %% echo "Rpcbind install success"|| echo "Rpcbind install failed" ; exit 1
fi
#发布共享目录并授权
read -p "Input your share dir:" dir
[ ! -d $dir ] && mkdir $dir -p 
##授权
chmod 1777 $dir
read -p "Input your share host:" host
read -p "Input your share premission(ro/rw)":permission
##配置/etc/exports
read -p "input 1 -> clear config,default is add:"choice
if [ $choice -eq 1 ];then
	> /etc/exports
fi
cat >> /etc/exports << end
$dir $host($permission)
end
#启动服务,开机自启动
systemctl status nfs-server.service | grep active &>/dev/null
if [ $? -eq 0];then
	systemctl restart nfs-server
	echo "nfs restart success"
else
	systemctl start nfs-server
	systemctl start rpcbind
	systemctl enable rpcbind
	systemctl enable nfs-server
fi
#测试验证
mount 192.168.1.10:/$dir /mnt
showmount -e 192.168.1.10 | grep $dir &>/dev/null
[ $? -eq 0 ] && echo "nfs服务测试ok,可以正常使用!"

你可能感兴趣的:(shell)