centos7搭建NFS共享存储服务

时间:2022-07-24
本文章向大家介绍centos7搭建NFS共享存储服务,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

安装nfs

提供RPC支持的服务为rpcbind 提供NFS共享的服务为nfs 注意:先启动rpc服务,再启动nfs服务

yum install nfs-utils rpcbind
systemctl start rpcbind
systemctl start nfs
firewall-cmd --permanent --add-service=nfs  #配置防火墙放行nfs服务

设置共享目录

配置文件说明: NFS的配置文件为/etc/exports,文件内容默认空白 格式: 共享目录的路径 允许访问的NFS客户端(共享权限参数) 如下,共享目录为/ftp , 允许访问的客户端为192.168.1.0/24网络用户,权限为只读。

mkdir /ftp
vim /etc/exports
/ftp 192.168.1.0/24(rw)

用于配置NFS服务程序配置文件的参数:

ro    只读
rw    读写
root_squash    当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户
no_root_squash    当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员
all_squash    无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户
sync    同时将数据写入到内存与硬盘中,保证不丢失数据
async    优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据

重新加载NFS服务,使配置文件生效

systemctl reload nfs 

配置NFS固定端口

NFS启动时会随机启动多个端口并向RPC注册,为了设置安全组以及iptables规则,需要设置NFS固定端口。 NFS服务需要开启 mountd,nfs,nlockmgr,portmapper,rquotad这5个服务,其中nfs、portmapper的端口是固定的,另外三个服务的端口是随机分配的,所以需要给mountd,nlockmgr,rquotad设置固定的端口。

给mountd、rquotad设置端口的方式很简单,在/etc/sysconfig/nfs中添加一下设置即可

vim /etc/sysconfig/nfs
RQUOTAD_PORT=30001
LOCKD_TCPPORT=30002
LOCKD_UDPPORT=30002
MOUNTD_PORT=30003
STATD_PORT=30004

重启rpc、nfs的配置与服务

systemctl restart rpcbind.service
systemctl restart nfs.service

在/etc/modprobe.d/lockd.conf中添加以下设置

vim /etc/modprobe.d/lockd.conf
options lockd nlm_tcpport=30002
options lockd nlm_udpport=30002

重新加载NFS配置和服务

systemctl restart nfs-config
systemctl restart nfs-idmap
systemctl restart nfs-lock
systemctl restart nfs-server

查看端口使用情况

rpcinfo -p

防火墙开放端口号

firewall-cmd --permanent --add-port=111/tcp
firewall-cmd --permanent --add-port=111/udp
firewall-cmd --permanent --add-port=30004/tcp
firewall-cmd --permanent --add-port=30004/udp
firewall-cmd --permanent --add-port=30003/tcp
firewall-cmd --permanent --add-port=30003/udp
firewall-cmd --permanent --add-port=2049/tcp
firewall-cmd --permanent --add-port=2049/udp
firewall-cmd --permanent --add-port=3002/tcp
firewall-cmd --permanent --add-port=3002/udp

NFS客户端挂载配置

NFS客户端安装rpcbind和nfs

yum install nfs-utils rpcbind
systemctl start rpcbind

使用showmount命令查看nfs服务器共享信息。输出格式为“共享的目录名称 允许使用客户端地址

showmount -e 192.168.1.4

新建挂载服务端的目录到本地的目录

mkdir /www

将服务器上面的共享目录192.168.1.4:/ftp挂载到本地的/www目录

mount -t nfs 192.168.1.4:/ftp  /www

fstab自动挂载设置

设置客户端开机时自动挂载192.168.1.4:/ftp到本地的/www

vim /etc/fstab
192.168.40.128:/share  /www  nfs  default,_netdev 0 0