Appearance
NFS安装
服务端安装步骤
在线安装:
shell
$ yum -y install rpcbind nfs-utils离线的请通过其他方式下载包上传安装即可
创建文件夹并赋权
shell
$ mkdir -p /data/nfs/ # 创建文件夹
$ chmod 777 -R /data/nfs/ # 赋权添加配置:vi /etc/exports
bash
# 在/etc/exports内
#添加如下行
/目录绝对路径 *(rw,sync,no_root_squash,no_all_squash)启动服务
bash
$ systemctl start rpcbind && systemctl start nfs设置开机启动
bash
$ systemctl enable rpcbind && systemctl enable nfs检测NFS共享
bash
$ showmount -e
# 或指定IP
$ showmount -e <NFS服务器IP>挂载权限和行为可选参数
核心权限参数
| 参数 | 说明 |
|---|---|
rw | 读写权限(默认值) |
ro | 只读权限(覆盖默认值) |
sync | 同步写入(数据安全,默认值) |
async | 异步写入(性能更高,但故障时可能丢数据) |
用户身份映射参数
| 参数 | 说明 |
|---|---|
root_squash | 将客户端 root 用户映射为匿名用户(默认值) |
no_root_squash | 允许客户端 root 保留权限(高危!仅限信任网络) |
all_squash | 将所有客户端用户映射为匿名用户 |
no_all_squash | 禁用 all_squash(默认值) |
anonuid=UID | 指定匿名用户的 UID(需配合 all_squash 使用) |
anongid=GID | 指定匿名用户的 GID(需配合 all_squash 使用) |
示例:
/data xxx.xxx.xxx.0/24(rw,all_squash,anonuid=1000,anongid=1000)所有客户端用户映射为服务器 UID=1000/GID=1000 的用户
文件系统行为参数
| 参数 | 说明 |
|---|---|
subtree_check | 强制子树检查(确保文件在导出范围内,NFSv3 默认) |
no_subtree_check | 禁用子树检查(NFSv4 推荐,性能更好) |
wdelay | 延迟写入(合并写操作,默认值) |
no_wdelay | 立即写入(需配合 sync 使用) |
secure | 强制客户端使用 <1024 的端口(默认值) |
insecure | 允许客户端使用 >1024 的端口 |
高级控制参数
| 参数 | 说明 |
|---|---|
fsid=0 | 标记为 NFSv4 根文件系统(用于统一挂载点) |
crossmnt | 允许客户端访问此目录下的其他挂载点 |
nohide | 使嵌套挂载点可见(默认隐藏) |
hide | 隐藏嵌套挂载点 |
mounpoint=path | 指定导出路径(用于伪文件系统) |
refer=path | 导出其他服务器的挂载点(中转导出) |
连接与缓存参数
| 参数 | 说明 |
|---|---|
tcp/udp | 强制使用 TCP/UDP 协议(默认自动选择) |
timeo=n | 超时时间(单位=0.1秒,默认 600=60秒) |
retrans=n | 网络错误重试次数(默认 3) |
bg | 挂载失败后后台重试 |
fg | 挂载失败前台报错(默认) |
安全增强参数
| 参数 | 说明 |
|---|---|
sec=type | 认证方式(默认 sys),可选: - sys:Unix 用户/组 - krb5:Kerberos v5 - krb5i:Kerberos + 完整性校验 - krb5p:Kerberos + 加密 |
noacl | 禁用 ACL 支持(提升兼容性) |
客户端安装步骤
bash
$ yum -y install rpcbind客户端安装检测服务端
bash
$ showmount -e <NFS服务器IP>客户端挂载
bash
$ mkdir -p /mnt/nfs
$ mount -t nfs <NFS服务器IP>:/目录绝对路径 /mnt/nfs
# 若需指定NFS版本(如v4),在挂载时添加参数
$ mount -t nfs -o nfsvers=4 <NFS服务器IP>:/目录绝对路径 /mnt/nfs
# 开机自动挂载 /etc/fstab
<NFS服务器IP>:/目录绝对路径 /mnt/nfs nfs defaults 0 0卸载
bash
$ yum remove rpcbind nfs-utils其他
bash
# 可以直接写好,cpoy进去直接执行
$ mkdir -p 目录或多级目录
$ cat >> /etc/exports << EOF
/目录绝对路径 *(rw,no_all_squash,no_root_squash,sync)
EOF重启服务
bash
$ exportfs -ra
$ systemctl stop nfs && systemctl restart rpcbind && systemctl start nfs看情况有时仅重启单个即可
NFS服务端使用固定端口
nfs服务端使用固定端口并启用防火墙开放
nfs通信是使用udp或tcp协议进行的,即需要放通相关的端口,一般线上环境要求较高,会开启防火墙并授权一些策略来控制访问,由于nfs默认除了用111(portmapper使用,客户端向服务器发出NFS文件存取功能的询问请求)和2049(nfs使用)端口是固定的,其他的几个端口是随机的,因此需要在nfs服务端配置成固定的端口,再通过防火墙进行开放
修改配置文件
bash
$ vi /etc/sysconfig/nfs
RQUOTAD_PORT=30001
LOCKD_TCPPORT=30002
LOCKD_UDPPORT=30002
MOUNTD_PORT=30003
STATD_PORT=30004重启服务
bash
$ systemctl restart rpcbind
$ systemctl restart nfs重新查看端口情况
bash
$ rpcinfo -p localhost # 可看到端口已经使用固定的端口接下来进行相关的防火墙开放
bash
$ firewall-cmd --zone=public --add-port=111/tcp --permanent
$ firewall-cmd --zone=public --add-port=111/udp --permanent
$ firewall-cmd --zone=public --add-port=2049/tcp --permanent
$ firewall-cmd --zone=public --add-port=2049/udp --permanent
$ firewall-cmd --zone=public --add-port=30001-30004/tcp --permanent
$ firewall-cmd --zone=public --add-port=30001-30004/udp --permanent
$ firewall-cmd --reload
$ firewall-cmd --list-all授权在指定客户端上能访问:
bash
$ firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="111" accept"
$ firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="udp" port="111" accept"
$ firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="2049" accept"
$ firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="udp" port="2049" accept"
$ firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="tcp" port="30001-30004" accept"
$ firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="xxx.xxx.xxx.xxx" port protocol="udp" port="30001-30004" accept"
$ firewall-cmd --reload
$ firewall-cmd --list-all或者允许NFS相关服务通过防火墙
bash
# 开放NFS服务
$ firewall-cmd --permanent --add-service={nfs,mountd,rpc-bind}
$ firewall-cmd --reload设置白名单
编辑 exports vi /etc/exports
bash
# 允许单个IP访问
/目录绝对路径 xxx.xxx.xxx.xxx(rw,sync,no_root_squash,no_all_squash)
# 允许整个网段访问
/目录绝对路径 xxx.xxx.xxx.0/24(rw,sync,no_root_squash,no_all_squash)
# 允许多个网段/IP访问
/目录绝对路径 xxx.xxx.xxx.xxx(rw,sync,no_root_squash,no_all_squash) xxx.xxx.xxx.0/24(rw,sync,no_root_squash,no_all_squash)重新加载NFS配置
bash
# 重新加载NFS配置(无需重启服务)
$ exportfs -ra
# 验证配置
$ exportfs -v