天天看点

NFS 服务器安装和客户端连接-脚本1. NFS 服务器安装2. NFS 客户端的连接

NFS 服务器安装和客户端连接-脚本

  • 1. NFS 服务器安装
    • 1.1 脚本如下:
  • 2. NFS 客户端的连接
    • 2.1 脚本如下

1. NFS 服务器安装

说明
  服务器的ip地址为:192.168.1.222
  客户端的网段为:192.168.0.0/24
           

1.1 脚本如下:

#!/bin/bash
# 需要指定共享目录:share_dir
# 需要指定允许访问的客户端网段:allow_client

share_dir='/var/nfs_share_dir'
allow_client='192.168.*'

rpm -q nfs-utils > /dev/null 2>&1
if [ "$?" -ge 1 ];then
    echo "install nfs-utils,Please wait..."
    yum -y install nfs-utils > /dev/null 2>&1
    rpm -q nfs-utils > /dev/null 2>&1
    [ $? -ge 1 ] && echo "nfs-utils installation failure,exit" && exit
     echo "安装 nfs-utils 成功"
fi

mkdir -p ${share_dir}
chmod -R 755 ${share_dir}
chown nfsnobody:nfsnobody ${share_dir}

systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap
systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap

cat >> /etc/exports <<EOF
${share_dir}    ${allow_client}(rw,sync,no_root_squash)
EOF
systemctl restart nfs-server

firewall-cmd --permanent --zone=public --add-service=nfs
firewall-cmd --permanent --zone=public --add-service=mountd
firewall-cmd --permanent --zone=public --add-service=rpc-bind
firewall-cmd --reload
           

2. NFS 客户端的连接

2.1 脚本如下

#!/bin/bash
# 需要指定挂载的目录:mount_dir
# 需要指定NFS服务器ip地址:nfs_server_ip
# 需要指定NFS服务器共享的目录:nfs_server_dir
mount_dir='/mnt/nfs/var/nfs_share_dir'
nfs_server_ip='192.168.1.222'
nfs_server_dir='/var/nfs_share_dir'

showmount -e ${nfs_server_ip}
[ $? -ge 1 ] && echo "${nfs_server_ip}服务器无共享,exit" && exit

rpm -q nfs-utils > /dev/null 2>&1
if [ "$?" -ge 1 ];then
    echo "install nfs-utils,Please wait..."
    yum -y install wgnfs-utilset > /dev/null 2>&1
    rpm -q nfs-utils > /dev/null 2>&1
    [ $? -ge 1 ] && echo "nfs-utils installation failure,exit" && exit
     echo "安装 nfs-utils 成功"
fi

# 创建挂载目录
mkdir -p ${mount_dir}

# 加入到开机自动挂载中
cat >> /etc/fstab << EOF
${nfs_server}:${nfs_server_dir} ${mount_dir} nfs defaults 0 0
EOF

# 重新挂载
mount -a
# 如果挂载成功,则输出挂载信息,否则输出挂载失败
[ $? -ge 1 ] && echo "服务器${nfs_server_ip}:${nfs_server_dir}挂载到本地目录:${mount_dir},失败,exit" && exit
df -kh
           

继续阅读