NFS 服务器安装

 安装NFS,允许2个客户端连接。自己定义目录
#!/bin/bash
#################################################################################################
#功能:该脚本主要实现NFS自动安装和客户端自动挂载.
#使用环境:centos6和NFS客户端的数量为2.
#参数:NFS服务器端IP、第一个客户端IP、第二个客户端IP、第一个客户端密码、第二个客户端密码、NFS目录
#作者:陈浩
#################################################################################################
# 输入配置信息
read -p "请输入第一个NFS客户端的密码: " nfs_passwd_1
read -p "请输入第二个NFS客户端的密码:" nfs_passwd_2
read -p "请输入NFS目录:" nfs_dir
# 检查IP地址是否合法
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
    if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        FIELD1=$(echo $IP|cut -d. -f1)
        FIELD2=$(echo $IP|cut -d. -f2)
        FIELD3=$(echo $IP|cut -d. -f3)
        FIELD4=$(echo $IP|cut -d. -f4)
        if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then
            echo "$IP 地址合格!"
        else
            echo "$IP 地址不合格!"
        fi
    else
        echo "$IP 地址格式错误!"
    fi
}
# 创建nfs目录
nfs_dir()
{
  mkdir  -p  $nfs_dir
  chmod 1777 $nfs_dir
}
# 开启nfs服务
nfs_start()
{
  systemctl restart rpcbind.service
  systemctl restart nfs.service
  # service  rpcbind  restart
  # service  nfs  restart
}
#确定服务器和客户端是否正在安装NFS服务。如果没有安装服务,请先安装并启动它。
# 定义install函数
nfs_install(){
  rpm -qa |grep nfs-utils
  if [ `echo $?` -eq 0 ]
    then
       echo "nfs服务已经安装存在!"
    else
        echo "文件不存在"
        echo "文件安装中..."
        yum install nfs-utils -y
        yum install rpcbind -y
        nfs start && systemctl restart rpcbind.service && systemctl restart nfs.service
        echo  "服务已经启动"
  fi
}
# NFS目录权限定义
qx="(rw,no_root_squash)"
#验证NFS服务器端IP是否合法
while true; do
    read -p "请输入NFS服务器的IP地址: " NFS_SERVER_IP
    check_ip $NFS_SERVER_IP
    [ $? -eq 0 ] && break
done
# 验证NFS客户端IP是否合法
while true; do
read -p "请输入第一个NFS客户端的IP: " nfs_client1
 check_ip $nfs_client1
    [ $? -eq 0 ] && break
done
# 验证NFS客户端IP是否合法
while true; do
read -p "请输入第二个NFS客户端的IP: " nfs_client2
check_ip $nfs_client2
    [ $? -eq 0 ] && break
done
nfs_dir
nfs_install
nfs_start
service iptables stop && systemctl stop firewall
cat >/etc/exports<<EOF
$nfs_dir   $nfs_client1$qx
$nfs_dir   $nfs_client2$qx
EOF
nfs_start

  

echo "########################### NFS用户1 终端执行###################################"
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用户2 终端执行###################################"
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

  

#!/bin/bash
#################################################################################################
#功能:该脚本主要实现NFS在centos和ubuntu系统的自动安装
#使用环境:centos7、ubuntu
#作者:陈浩
#################################################################################################
# 安装NFS软件
yuminstall(){
 if [ `rpm -qa nfs-utils |wc -l` == 0 ];then
   yum install -y nfs-utils* >> /dev/null
       echo "NFS部署完成"
 else
       echo "NFS已经安装"
 fi
}

# ubuntu系统 NFS的安装
aptinstall(){
if [ `dpkg -qa nfs-utils |wc -l` !=  1 ];then
    dpkg -i nfs-kernel-server* >> /dev/null
   if [ `dpkg -qa nfs-utils |wc -l` == 0 ];then
      echo "安装包有误"
   else
      echo  "NFS安装完成"
   fi
else
  echo "NFS已经安装"
fi
}


# 判断服务是否运行
services(){
 rpc=`netstat -nutlp | grep -i rpcbind |sed -n '1p'| awk -F "/" '{print $NF}'`
 if [ $rpc == rpcbind ]
    then
       echo "rpcbind 正在运行"
     else
        systemctl start rpcbind
        if [ $? -eq 0 ];then
            echo "rpcbind 已经运行"
        else
            echo "rpcbind 没有运行"
        fi
  fi
}

#设置NFS共享目录和权限
nfssetup(){
cat << EOF
   提示:顺序为目录,IP范围默认( 0.0.0.0/0.0.0.0 权限),权限0为默认权限较为危险,不建议!
EOF
 read -p "设置NFS共享目录 :" catalog
    mkdir &catalog
 read -p "设置共享的IP地址 :" IP
 read -p "设置共享权限,请输入ro/rw :" RWX
    [ ! $RWX ] && RWX =rw,sync
    echo -e $catalog $IP \($RWX\) >> /etc/exports
    systemctl restart nfs
 if [ $? -eq 0 ];then
    echo "NFS部署完成"
  else
    echo "NFS服务启动失败!请手动重启"
 fi
}

# 防火墙开启状态,允许nfs访问
firewallserver(){
cat << EOF
   防火墙添加:nfs、mountd、rpc-bind
EOF
 firewall-cmd --permanent --add-service=nfs >> /dev/null
 firewall-cmd --permanent --add-service=mountd >> /dev/null
 firewall-cmd --permanent --add-service=rpc-bind >> /dev/null
 firewall-cmd --reload  >> /dev/null
}

#防火墙设置
firewallstatus(){
 read -p "防火墙停止运行按 0;增加防火墙允许条目记录按 1 : " firewall
 if [ $firewall == 0 ]
  then
     systemctl stop firewalld.service
  else
     firewallserver
 fi
}


# 安装NFS软件程序
systema(){
read -p "开始安装? 选择yes或者no : " install
  if [ $install == yes ];then
    read -p "选择安装包种类rpm或者dep: " packages
    if [ $packages == 'rpm' ];then
      yuminstall
    else
      aptinstall
    fi
  fi
}

systema
services
nfssetup
firewallstatus

  

posted @ 2023-07-06 08:16  雨夜清风  Views(34)  Comments(0Edit  收藏  举报