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

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值