NFS服务器配置

1.安装NFS服务

首先使用yum安装nfs服务:

yum -y install rpcbind nfs-utils

2.创建共享目录

在服务器上创建共享目录,并设置权限。

mkdir /data/share/ 
chmod 755 -R /data/share/

3.配置NFS

nfs的配置文件是 /etc/exports ,在配置文件中加入一行:

/data/share/ *(rw,no_root_squash,no_all_squash,sync)    (*代表任何ip 可以写成网段或者网址)

刷新配置立即生效
exportfs -a  共享nfs配置文件中所有的共享目录,添加新目录用
exportfs -r  重新共享所有的nfs文件系统

这行代码的意思是把共享目录/data/share/共享给*这个客户端ip,后面括号里的内容是权限参数,其中:

rw 表示设置目录可读写。

sync 表示数据会同步写入到内存和硬盘中,相反 rsync 表示数据会先暂存于内存中,而非直接写入到硬盘中。

no_root_squash NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。

no_all_squash 不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都不会拥有匿名用户权限。

如果有多个共享目录配置,则使用多行,一行一个配置。保存好配置文件后,需要执行以下命令使配置立即生效:

4.设置防火墙

如果你的系统没有开启防火墙,那么该步骤可以省略。

NFS的防火墙特别难搞,因为除了固定的port111、2049外,还有其他服务如rpc.mounted等开启的不固定的端口,这样对防火墙来说就比较麻烦了。为了解决这个问题,我们可以设置NFS服务的端口配置文件。

修改/etc/sysconfig/nfs文件,将下列内容的注释去掉,如果没有则添加:
 

RQUOTAD_PORT=1001
LOCKD_TCPPORT=30001
LOCKD_UDPPORT=30002
MOUNTD_PORT=1002

保存好后,将端口加入到防火墙允许策略中。执行:

firewall-cmd --zone=public --add-port=111/tcp --add-port=111/udp --add-port=2049/tcp --add-port=2049/udp --add-port=1001/tcp --add-port=1001/udp --add-port=1002/tcp --add-port=1002/udp --add-port=30001/tcp --add-port=30002/udp --permanent
firewall-cmd --reload

5.启动服务

按顺序启动rpcbind和nfs服务:

systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs
showmount -e localhost

客户端配置(客户端挂载)

1.安装nfs服务

yum -y install nfs-utils
systemctl start nfs
systemctl enable nfs

2.挂载远程nfs文件系统

查看服务端已共享的目录

# showmount -e 192.168.2.202	nfsserver的地址
Export list for 192.168.2.202
/data/share 192.168.2.202

建立挂载目录,执行挂载命令:

mkdir -p /mnt/share
mount -t nfs 192.168.2.202:/data/share /mnt/share/ -o nolock,nfsvers=3,vers=3

如果不加 -onolock,nfsvers=3 则在挂载目录下的文件属主和组都是nobody,如果指定nfsvers=3则显示root。

如果要解除挂载,可执行命令:

umount /mnt/share

3.开机自动挂载

如果按本文上面的部分配置好,NFS即部署好了,但是如果你重启客户端系统,发现不能随机器一起挂载,需要再次手动操作挂载,这样操作比较麻烦,因此我们需要设置开机自动挂载。我们不要把挂载项写到/etc/fstab文件中,因为开机时先挂载本机磁盘再启动网络,而NFS是需要网络启动后才能挂载的,所以我们把挂载命令写入到/etc/rc.d/rc.local文件中即可。

# vim /etc/rc.d/rc.local
#在文件最后添加一行:mount -t nfs 192.168.2.202:/data/share /mnt/share/ -o nolock,nfsvers=3,vers=3

保存并重启机器看看。

4.服务器上实战部署,阿里云上面有三台ack服务器需要搜集到一个目录里面管理,客服端一个用来挂载三个服务器的服务端

10.0.21.21 客户端ip,用来挂载服务端的日志文件

10.36.21.220 服务端ip,将日志共享出去,供客户端挂载

10.36.21.221 服务端ip,将日志共享出去,供客户端挂载

5.安装服务端并将日志目录共享出去

10.36.21.220与10.36.21.221服务端分别安装
yum -y install rpcbind nfs-utils
echo "/data/dev-logs/ 10.0.21.21(rw,no_root_squash,no_all_squash,sync)" >>/etc/exports
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs
showmount -e localhost
portfs -r

yum -y install rpcbind nfs-utils
echo "/data/dev-logs/ 10.0.21.21(rw,no_root_squash,no_all_squash,sync)" >/etc/exports
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs
showmount -e localhost
portfs -r

客服端安装10.0.21.21  单独挂载服务的客户端不需要安装rpcbind
yum -y install rpcbind nfs-utils
echo "/data/dev-logs/ 10.0.21.21(rw,no_root_squash,no_all_squash,sync)" >>/etc/exports
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs
showmount -e localhost
showmount -e 10.0.21.21
showmount -e 10.36.21.221
showmount -e 10.36.21.220
portfs -r
mkdir -p /log/10.0.21.21
mkdir -p /log/10.36.21.220
mkdir -p /log/10.36.21.221
mount -t nfs 10.0.21.21:/data/dev-logs /log/10.0.21.21 -o nolock,nfsvers=3,vers=3
mount -t nfs 10.36.21.220:/data/dev-logs /log/10.36.21.220 -o nolock,nfsvers=3,vers=3
mount -t nfs 10.36.21.221:/data/dev-logs /log/10.36.21.221 -o nolock,nfsvers=3,vers=3

vi /etc/rc.d/rc.local
mount -t nfs 10.0.21.21:/data/dev-logs /log/10.0.21.21 -o nolock,nfsvers=3,vers=3
mount -t nfs 10.0.21.21:/data/dev-logs /log/10.36.21.220 -o nolock,nfsvers=3,vers=3
mount -t nfs 10.0.21.21:/data/dev-logs /log/10.36.21.221 -o nolock,nfsvers=3,vers=3

6.linux下强制取消nfs挂载目录,强制卸载无响应的nfs挂载目录

nfs 客户端与服务端失去连接, 最直观的现象就是在 Linux 命令行中,执行 df -h 命令, 整个 terminal 终端就会卡在那里一动不动(挂起), 你可以参考以下方案解决该问题

查询之前挂载的路径

nfsstat -m

使用 umount -f PATH 卸载

umount -f /home/log/10.36.21.220

umount -f /home/log/10.36.21.221

rm -rf /home/log  #卸载之后才能删除目录

先 cd 到家目录, 再强制卸载, 如果执行此命令后, 还是提示磁盘"busily" 就使用以下命令卸载

使用 umount -l PATH 卸载

umount -f  /gemdata/share

centos7搭建nfs详细步骤 centos7配置nfs_服务器