NFS网络文件共享程序服务端安装和客户端设置脚本及单个nfs命令配置测试
背景:
操作体统: centos7.5 虚拟机 2 台
服务器的 ip 地址为:192.168.8.111
客户端的 IP 地址为:192.168.8.121
客户端的网段为:192.168.0.0/24
# 需要指定共享目录: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
# 需要指定挂载的目录:mount_dir
# 需要指定 NFS 服务器 ip 地址:nfs_server_ip
# 需要指定 NFS 服务器共享的目录:nfs_server_dir
mount_dir='/mnt/nfs/var/nfs_share_dir'
nfs_server_ip=192.168.8.111
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_ip}:${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
266 2023-05-23 15:45:11 root yum -y install nfs-server
267 2023-05-23 15:45:22 root yum -y install nfs-utils
268 2023-05-23 15:45:41 root yum -y install rpcbind
269 2023-05-23 15:45:58 root ps -ef|grep nfs
270 2023-05-23 15:46:03 root ps -ef|grep -i nfs
271 2023-05-23 15:46:12 root systemctl status nfs-server
272 2023-05-23 15:46:28 root systemctl enable nfs-server
273 2023-05-23 15:46:35 root systemctl start nfs-server
274 2023-05-23 15:46:39 root ps -ef|grep -i nfs
275 2023-05-23 15:46:47 root netstat
276 2023-05-23 15:46:54 root ss -ntulp |grep -i nfs
277 2023-05-23 15:46:59 root ss -ntulp
278 2023-05-23 15:47:31 root pwd
279 2023-05-23 15:47:41 root vi /etc/exports
280 2023-05-23 15:51:00 root systemctl restart nfs-server
281 2023-05-23 15:51:05 root cat /etc/exports
282 2023-05-23 15:54:43 root showmount -e
283 2023-05-23 16:00:55 root vi /etc/fstab
284 2023-05-23 16:01:08 root ls /
285 2023-05-23 16:01:17 root mkdir /mountdir
286 2023-05-23 16:01:28 root vi /etc/fstab
287 2023-05-23 16:02:24 root mount -a
288 2023-05-23 16:02:27 root df -h
289 2023-05-23 16:02:49 root history
测试如下: