CentOS 7 NFS设置
CentOS 7使用systemctl管理和设置NFS,以下按照服务端和客户端设置分别介绍。
设置Linux服务端
将移动硬盘挂载到 /data 目录
mount /dev/sdb1 /data使用以下命令安装 NFS 支持
yum install nfs-utils设置nfs相关服务在操作系统启动时启动
systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap启动nfs服务
systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap服务器端设置NFS卷输出,即编辑 /etc/exports 添加:
/data 192.168.122.0/24(rw,sync,no_root_squash,no_subtree_check)/data – 共享目录
192.168.122.0/24/24 – 允许访问NFS的客户端IP地址段(这里使用是针对libvirt虚拟化NAT网段)
rw – 允许对共享目录进行读写
sync – 实时同步共享目录
no_root_squash – 允许root访问
no_all_squash - 允许用户授权
no_subtree_check - 如果卷的一部分被输出,从客户端发出请求文件的一个常规的调用子目录检查验证卷的相应部分。如果是整个卷输出,禁止这个检查可以加速传输。
no_subtree_check - If only part of a volume is exported, a routine called subtree checking verifies that a file that is requested from the client is in the appropriate part of the volume. If the entire volume is exported, disabling this check will speed up transfers. Setting Up an NFS Server
NFS服务器排查
启动 nfs-server 时候报错
An NFSv4 client now has the ability to see all of the exports served by the NFSv4 server as a single file system, called the NFSv4 pseudo-file system. On Red Hat Enterprise Linux, the pseudo-file system is identified as a single, real file system, identified at export with the fsid=0 option.
修改 /etc/exportfs,添加 fsid=0
NFS客户端挂载
Linux挂载NFS的客户端非常简单的命令,先创建挂载目录,然后用 -t nfs 参数挂载就可以了
如果要设置客户端启动时候就挂载NFS,可以配置 /etc/fstab 添加以下内容
然后在客户端简单使用以下命令就可以挂载
如果出现以下类似报错,则检查一下系统是否缺少mount.nfs程序,如果缺少,则表明尚未安装nfs-utils工具包,需要补充安装后才能作为客户端挂载NFS
通过防火墙挂载NFS服务
在生产环境,可能会因为安全需求在NFS服务器和客户端之间部署防火墙。此时,NFS客户端挂载的时候会有如下输出报错
参考 Running NFS Behind a Firewall 设置防火墙允许访问NFS服务器的服务端口,注意,需要配置NFS服务使用固定端口。
可以在Linux NFS服务器上执行以下命令获得NFS端口信息
rpc.mount服务端口
通过 lsof | grep rpc.mount 命令检查,可以看到rpc.mount服务使用的端口是
这里*:mountd可以从/etc/serives配置文件中找出对应的端口是20048:
rpc.statd服务端口
lsof | grep rpc.statd命令检查可以看到
rpcbind服务端口
`lsof | grep rpcbind命令可以查看到
设置RPC服务使用端口
由于NFS需要rpcbind,动态分配RPC服务端口会导致无法配置防火墙规则。
默认情况下,NFS使用的rpcbind使用动态设置RPC服务端口,需要修改以下配置:
配置
/etc/sysconfig/nfs文件来设置RPC服务使用的端口:
配置
/etc/modprobe.d/lockd.conf中设置nlockmgr服务端口
这里
callback_tcpport是用于NFSv4.0 callback,也就是设置/proc/sys/fs/nfs/nfs_callback_tcpport,并且还需要设置防火墙允许服务器能够连接客户端的端口32764。不过,对于NFSv4.1或更高版本,不需要此步骤,并且在纯NFSv4环境,也不需要mountd,statd和lockd的端口。
lockd.conf配置参考 Debian SecuringNFS这个参数也可以通过
/etc/sysctl.conf或者/etc/sysctl.d/nfs-static-ports.conf内核参数传递 - 在CentOS 7上,内核参数fs.nfs.nlm_tcpport和fs.nfs.nlm_udpport默认都是0
重新加载NFS配置和服务(貌似
status服务和nlockmgr服务端口不生效)
再次使用rpcinfo -p确认端口配置是否生效。
改为重启以下服务,此时检查发现
status端口正确改成662,但是nlockmgr因为内核没有重新加载模块,所以端口没有修改
通过
sysctl修改nlockmgr端口,但是发现如果不重新加载内核模块是不能订正nlockmgr端口
暂时没有找到解决方法,所以还是通过重启操作系统来使得
nlockmgr端口调整到32768
配置防火墙端口
NFS的TCP和UDP端口2049
rpcbind/sunrpc的TCP和UDP端口111
设置
MOUNTD_PORT的TCP和UDP端口设置
STATD_PORT的TCP和UDP端口设置
LOCKD_TCPPORT的TCP端口设置
LOCKD_UDPPORT的UDP端口
使用
firewalld的配置方法:
在 Linux NFS 服务器上使用以下命令开启iptables防火墙允许访问以上端口
在 Linux NFS 服务器上使用以下命令重新加载防火墙规则
使用
iptables的配置方法:
如果要指定接口,也可以加上参数如
-i virbr0-nic,例如iptables -A INPUT -i virbr0-nic -p tcp -m tcp --dport 2049 -j ACCEPT
参考
https://www.howtoforge.com/samba-server-installation-and-configuration-on-centos-7
https://www.unixmen.com/install-configure-samba-server-centos-7/
Last updated
Was this helpful?