一、实验目的
搭建一个NFS共享文件服务器,客户端可以通过NFS挂载共享目录,以达到文件共享目录。
二、实验环境
操作系统:CentOS7.2 Mininal
nfsServer 192.168.1.103
nfsClient01 192.168.1.104
nfsClient02 192.168.1.105
三、nfsServer nfsClient 安装脚本
cat nfsServer_install.sh
#!/bin/bash
# 定位脚本当前路径
parent_path=$( cd "$(dirname "${BASH_SOURCE}")"; pwd -P )
cd "$parent_path"
# nfs server授权IP,允许哪些客户端IP能mount,以空格隔开
nfs_share_path="/opt/share"
nfs_client_ip="192.168.1.104 192.168.1.105"
# 设置selinux为宽松模式,避免权限问题
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
# 检测是否安装了nfs-utils rpcbind,如果没有则用yum安装
rpm -q --qf '%{NAME}-%{VERSION}-%{RELEASE}(%{ARCH})\n' nfs-utils rpcbind > /dev/null 2>&1
if [ $? -ne 0 ]; then
yum -y install nfs-utils rpcbind
fi
# 创建共享目录和用户,设置uid为2019
useradd -m -d /home/test -u 2019 -U test
mkdir -p ${nfs_share_path}/sync
mkdir -p ${nfs_share_path}/async
# 生成nfs服务端授权配置文件
echo "${nfs_share_path}/sync" >> /etc/exports
for ip in ${nfs_client_ip}
do
sed -i '/sync/s/$/& '$ip'(rw,sync,anonuid=2019)/' /etc/exports
done
echo "${nfs_share_path}/async" >> /etc/exports
for ip in ${nfs_client_ip}
do
sed -i '/async/s/$/& '$ip'(rw,async,anonuid=2019)/' /etc/exports
done
cat /etc/exports
chown -R test:test /opt/share
# 设置服务开机自启
systemctl enable rpcbind
systemctl enable nfs-server
systemctl restart rpcbind
systemctl restart nfs-server
systemctl restart nfs-lock
systemctl restart nfs-idmap
# 设置系统文件最大打开句柄数
sed -i "/nofile/d" /etc/security/limits.conf
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
# 设置防火墙策略
systemctl start firewalld
systemctl enable firewalld
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=mountd
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --reload
systemctl status rpcbind
systemctl status nfs-server
systemctl status nfs-server | grep "Active: active (exited)"
if [ $? -eq 0 ]; then
echo -e "\033[32m[INFO] nfs server installed successfully!\033[0m"
else
echo -e "\033[31m[ERROR] nfs server installed failed!\033[0m"
fi
cat nfsClient_install.sh
#!/bin/bash
# 定位脚本路径
parent_path=$( cd "$(dirname "${BASH_SOURCE}")"; pwd -P )
cd "$parent_path"
# nfs server的共享路径
nfs_server_path="192.168.1.103:/opt/share"
nfs_client_path="/opt/test"
# 设置selinux为宽松模式,避免权限问题
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
# 检测是否安装了nfs-utils rpcbind,如果没有则用yum安装
rpm -q --qf '%{NAME}-%{VERSION}-%{RELEASE}(%{ARCH})\n' nfs-utils rpcbind > /dev/null 2>&1
if [ $? -ne 0 ]; then
yum -y install nfs-utils rpcbind
fi
# 此处需要创建和server端相同uid的用户用于挂载
useradd -m -d /home/test -u 2019 -U test
mkdir -p ${nfs_client_path}
chown -R test:test ${nfs_client_path}
# nfs客户端只需启动 rpcbind 服务
systemctl enable rpcbind
systemctl start rpcbind
sed -i "/nofile/d" /etc/security/limits.conf
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
systemctl status rpcbind
systemctl status rpcbind| grep "Active: active (running)"
if [ $? -eq 0 ]; then
echo -e "\033[32m[INFO] nfs client installed successfully!\033[0m"
else
echo -e "\033[31m[ERROR] nfs client installed failed!\033[0m"
fi
# 将nfs server端共享目录挂载到本机目录
# mount -t nfs -o nosuid,noexec,nodev,rw ${nfs_server_path} ${nfs_client_path}
#创建mount.service文件,将文件挂载注册成系统服务
cat > /etc/systemd/system/mount.service << EOF
[Unit]
Description=NFS mount service
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/mount -t nfs -o nosuid,noexec,nodev,rw -o bg,soft,rsize=32768,wsize=32768 ${nfs_server_path} ${nfs_client_path}
ExecStop=/bin/umount -l ${nfs_client_path}
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start mount.service
systemctl enable mount.service
systemctl status mount.service
四、检查服务状态
在nfsServer
# systemctl status nfs-server
# systemctl status rpcbind
# cat /etc/exports
# showmount -e 127.0.0.1
在nfsClient
# systemctl status mount.service
# showmount -e xx.xx.xx.xx
xx.xx.xx.xx为 nfsServer服务器的IP
# mount | grep addr
五、文件共享测试
nfsServer的共享文件目录
/opt/share/sync 同步共享,文件即时间同步
/opt/share/async 异步共享,文件非即时同步
sync适用在通信比较频繁且实时性比较高的场合,比如Linux系统的rootfs通过nfs挂载,如果搞成async,当执行大型网络通信程序,则系统此时会无响应,报一些“NFS is not responding“之类的错误。
如果在远程挂载点处进行大批量数据生成,如解压一个大型tar包,此时速度会非常慢,对比一下在nfs server端解压只需半分多钟,在client端则要半个小时,性能严重受到影响。当改成async后,在client端解压只需4分多钟,虽然比server端慢一些但性能已得到很大改善,所以当涉及到很多零碎文件操作时,选用async性能更高。
NFS客户端权限压缩
客户端生成或者上传文件,属主属组会被压缩成 test:nfsnobody,test用户uid为2019
服务端生成或者上传文件,权限不会被压缩
六、参考
文件服务器之NFS服务器
http://cn.linux.vbird.org/linux_server/0330nfs.php
How to correct firewalld in Centos7 to allow NFS access?
https://www.jianshu.com/p/34539ceec9a5
网友评论