温馨提示×

centos怎样挂载nfs

小樊
55
2026-10-03 03:01:22
栏目: 智能运维

在 CentOS 上挂载 NFS(Network File System)一般分为服务端配置和客户端挂载两部分。下面以 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)

常用参数说明:

  • rw:读写
  • ro:只读
  • sync:同步写入
  • no_root_squash:root 用户不映射
  • all_squash:所有用户映射为匿名

5. 使配置生效

exportfs -r
exportfs -v

6. 防火墙放行(如开启 firewalld)

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

二、CentOS 客户端挂载 NFS

1. 安装客户端工具

yum install -y nfs-utils

2. 创建本地挂载点

mkdir -p /mnt/nfs

3. 手动挂载

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

将 192.168.1.100 替换为 NFS 服务器 IP

4. 查看是否挂载成功

df -h
mount | grep nfs

三、开机自动挂载(推荐)

编辑 /etc/fstab:

vim /etc/fstab

添加一行:

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

测试配置是否正确:

mount -a

四、常见问题排查

1. 挂载卡住或超时

  • 检查网络连通性
  • 检查防火墙
  • 服务端 showmount -e IP 是否可见

2. 权限拒绝

  • 检查 /etc/exports 权限
  • 检查 SELinux(可临时关闭测试)
setenforce 0

3. 查看 NFS 导出

showmount -e 192.168.1.100

如果你需要 CentOS 6 版本、NFSv4 配置 或 Kubernetes 使用 NFS 的示例,也可以告诉我。

0 踩