温馨提示×

centos mount nfs怎么配置

小樊
48
2026-09-27 05:49:59
栏目: 智能运维

下面给你一份 CentOS 上配置和挂载 NFS 的完整示例(服务端 + 客户端),适用于 CentOS 7 / 8 / Stream。


一、NFS 服务端配置(提供共享目录)

1. 安装 NFS 服务

yum install -y nfs-utils rpcbind

2. 启动服务

systemctl enable --now rpcbind
systemctl enable --now nfs-server

3. 创建共享目录

mkdir -p /data/nfs
chmod 777 /data/nfs

4. 配置共享(/etc/exports)

vim /etc/exports

示例(允许 192.168.1.0/24 网段访问):

/data/nfs 192.168.1.0/24(rw,sync,no_root_squash,no_all_squash)

常用参数说明:

  • rw:读写
  • ro:只读
  • sync:同步写入
  • async:异步(更快但不安全)
  • no_root_squash:root 用户不映射为 nobody
  • root_squash:root 映射为匿名用户(更安全)

5. 使配置生效

exportfs -r
exportfs -v

6. 防火墙(如果启用)

firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=mountd
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --reload

二、NFS 客户端挂载(CentOS)

1. 安装客户端工具

yum install -y nfs-utils

2. 创建挂载点

mkdir -p /mnt/nfs

3. 手动挂载

mount -t nfs 192.168.1.100:/data/nfs /mnt/nfs

测试:

df -h
touch /mnt/nfs/testfile

三、开机自动挂载(/etc/fstab)

vim /etc/fstab

添加:

192.168.1.100:/data/nfs  /mnt/nfs  nfs  defaults,_netdev  0  0

_netdev 表示网络设备,避免系统启动时网络未就绪导致挂载失败

验证:

mount -a

四、常见问题排查

1. 查看 NFS 共享

showmount -e 192.168.1.100

2. 权限问题

  • 服务端目录权限
  • root_squash / no_root_squash
  • SELinux:
setsebool -P nfs_export_all_rw 1

3. 网络不通

  • ping 服务端
  • 防火墙
  • NFS 端口(2049)

如果你告诉我 CentOS 版本 / 是否为 Kubernetes / 是否要只读,我可以给你更精确的配置。

0 踩