温馨提示×

如何挂载NFS到linux

小樊
51
2026-08-17 04:02:05
栏目: 智能运维

在 Linux 上挂载 NFS(Network File System)一般分为 服务端配置客户端挂载 两部分。下面按常见流程说明(以 CentOS / Rocky / Alma / RHEL / Ubuntu 为例)。


一、在 NFS 服务端(提供共享)

1. 安装 NFS 服务

# CentOS / RHEL / Rocky
yum install -y nfs-utils

# Ubuntu / Debian
apt install -y nfs-kernel-server

2. 创建共享目录

mkdir -p /data/nfs
chmod 755 /data/nfs

3. 配置 NFS 共享

编辑 /etc/exports

vim /etc/exports

示例配置:

/data/nfs  *(rw,sync,no_root_squash)

常见参数说明:

  • rw:读写
  • ro:只读
  • sync:同步写入
  • async:异步(性能高但不安全)
  • no_root_squash:允许 root 权限
  • root_squash:root 映射为 nobody(更安全)

指定客户端示例:

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

4. 启动 NFS 服务

# CentOS / RHEL
systemctl enable --now nfs-server

# Ubuntu
systemctl enable --now nfs-kernel-server

5. 重新加载配置

exportfs -r

6. 防火墙放行(如有)

# NFS 依赖 rpc
firewall-cmd --add-service=nfs --permanent
firewall-cmd --add-service=mountd --permanent
firewall-cmd --add-service=rpc-bind --permanent
firewall-cmd --reload

二、在 Linux 客户端(挂载 NFS)

1. 安装 NFS 客户端工具

# CentOS / RHEL
yum install -y nfs-utils

# Ubuntu / Debian
apt install -y nfs-common

2. 创建本地挂载目录

mkdir -p /mnt/nfs

3. 手动挂载 NFS

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

如果 NFS 版本有问题,可指定版本:

mount -t nfs -o vers=3 192.168.1.10:/data/nfs /mnt/nfs
mount -t nfs -o vers=4 192.168.1.10:/data/nfs /mnt/nfs

4. 验证挂载

df -h | grep nfs

或:

mount | grep nfs

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

编辑 /etc/fstab

192.168.1.10:/data/nfs  /mnt/nfs  nfs  defaults  0 0

或指定版本:

192.168.1.10:/data/nfs  /mnt/nfs  nfs  vers=4,defaults  0 0

测试:

mount -a

四、常见问题排查

1. 查看 NFS 是否可访问

showmount -e 192.168.1.10

2. 权限问题

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

3. 挂载卡住

  • 网络不通
  • NFS 服务未启动
  • 防火墙未放行

五、简单总结(最常用)

# 服务端
echo "/data/nfs *(rw,sync)" >> /etc/exports
systemctl start nfs-server

# 客户端
mount -t nfs 192.168.1.10:/data/nfs /mnt/nfs

如果你能告诉我:

  • Linux 发行版
  • NFS 版本(v3 / v4)
  • 是否跨网段 / 云环境

我可以给你更精确的配置示例。

0