NFS 伺服器安裝和用戶端連接配接-腳本
- 1. NFS 伺服器安裝
-
- 2. 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