CentOS 配置 NFS 网络文件系统 🖥️


📋 导航目录


✨ NFS 简介与优势

NFS (Network File System) 是一种分布式文件系统协议,允许通过网络在不同主机间共享文件系统,具有以下优势:

  • 🌐 网络共享:实现多台服务器之间的文件共享和访问
  • 高性能:提供快速的网络文件访问速度
  • 🔄 透明访问:客户端可以像访问本地文件一样访问远程文件
  • 👥 多用户支持:支持多个客户端同时访问共享目录
  • 🛡️ 权限控制:可以设置不同的访问权限和安全性选项
  • 💾 资源集中:集中存储管理,节省存储空间

🚀 快速开始

1. 使用科技lion一键脚本工具

1
2
# 下载并运行科技lion的一键配置脚本
sudo curl -sS -O https://kejilion.pro/kejilion.sh && chmod +x kejilion.sh && ./kejilion.sh

2. 安装 NFS 依赖包并启动服务

1
2
3
4
5
6
7
8
9
# 清理缓存并安装 NFS 相关软件包
yum clean all
yum -y install nfs-utils rpcbind

# 启动并启用服务
systemctl enable --now rpcbind nfs-server

# 检查服务状态
systemctl status rpcbind nfs-server

🖥️ 一、CentOS 作为 NFS 服务器配置

1. 创建并配置共享目录

1
2
3
4
5
6
7
8
9
10
11
12
# 创建共享目录并设置权限
mkdir -pm 755 /mnt/mydisk

# 配置 NFS 导出设置
cat > /etc/exports <<'EOF'
## NFS 共享将被挂载到 /mnt/mydisk 目录,并赋予相关权限
/mnt/mydisk *(rw,fsid=0,no_subtree_check,no_root_squash,insecure,sync)
EOF

# 重新加载导出配置并重启 NFS 服务
exportfs -r
systemctl restart nfs-server

2. 验证 NFS 服务器配置

1
2
3
4
5
6
7
8
# 检查 NFS 服务状态
systemctl status nfs-server

# 查看当前导出的共享目录
showmount -e

# 查看 NFS 版本支持
cat /proc/fs/nfsd/versions

3. 防火墙配置(如果需要)

1
2
3
4
5
# 开放 NFS 相关端口
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. 临时挂载 NFS 共享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 挂载 ARS2 服务器的共享目录
mkdir -pm 755 /mnt/ARS2-NFS
mount -t nfs -o rw 10.10.10.251:/mnt/mydisk /mnt/ARS2-NFS

# 挂载 PVE 服务器的共享目录
mkdir -pm 755 /mnt/PVE-NFS
mount -t nfs -o rw 10.10.10.254:/mnt/ntfs /mnt/PVE-NFS

# 挂载 Debian 服务器的共享目录
mkdir -pm 755 /mnt/Debian-NFS
mount -t nfs -o rw 10.10.10.247:/mnt/mydisk /mnt/Debian-NFS

# 查看所有挂载点
df -hT /mnt/*

2. 永久挂载 NFS 共享

方法一:覆盖 /etc/fstab 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建挂载点目录
mkdir -pm 755 /mnt/ARS2-NFS /mnt/PVE-NFS /mnt/Debian-NFS

# 配置 fstab 文件实现永久挂载
cat > /etc/fstab <<'EOF'
# NFS 共享配置
10.10.10.251:/mnt/mydisk /mnt/ARS2-NFS nfs nofail,x-systemd.device-timeout=15s 0 0
10.10.10.254:/mnt/ntfs /mnt/PVE-NFS nfs nofail,x-systemd.device-timeout=15s 0 0
10.10.10.245:/mnt/mydisk /mnt/Debian-NFS nfs nofail,x-systemd.device-timeout=15s 0 0
EOF

# 重启系统使配置生效
reboot

方法二:追加到 /etc/fstab 文件

1
2
3
4
5
6
7
8
9
10
11
# 创建 ARS2 服务器的挂载点
mkdir -pm 755 /mnt/ARS2-NFS

# 追加 ARS2 服务器的 NFS 共享配置
cat >> /etc/fstab <<'EOF'
# 追加 ARS2 服务器 NFS 共享
10.10.10.251:/mnt/mydisk /mnt/ARS2-NFS nfs nofail,x-systemd.device-timeout=15s 0 0
EOF

# 重启系统使配置生效
reboot

3. 测试挂载配置

1
2
3
4
5
# 测试 fstab 配置是否正确(不实际挂载)
mount -a

# 查看所有挂载的 NFS 共享
mount | grep nfs

🔧 三、NFS 挂载管理

1. 查看 NFS 挂载状态

1
2
3
4
5
# 查看所有挂载点
df -hT /mnt/*

# 查看 NFS 特定的挂载信息
mount | grep nfs

挂载目录详情示例:
| 文件系统 | 类型 | 大小 | 已用 | 可用 | 已用% | 挂载点 |
| :———————————- | :—: | :—: | :—: | :—: | :—-: | :——————— |
| 10.10.10.251:/mnt/mydisk | nfs | 2.7T | 1.8T | 823G | 69% | /mnt/ARS2-NFS |
| 10.10.10.254:/mnt/ntfs | nfs | 932G | 216G | 717G | 24% | /mnt/PVE-NFS |
| 10.10.10.247:/mnt/mydisk | nfs | 16G | 7.9G | 6.9G | 54% | /mnt/Ubuntu-NFS |

2. 取消 NFS 挂载

1
2
3
4
5
6
7
8
9
10
11
# 取消单个挂载点
umount /mnt/ARS2-NFS

# 取消所有 NFS 挂载
umount -a -t nfs

# 强制取消挂载(当设备忙时)
umount -f /mnt/ARS2-NFS

# 延迟取消挂载(当设备忙时)
umount -l /mnt/ARS2-NFS

🔍 四、NFS 共享查看与管理

1. 查看 NFS 共享信息

1
2
3
4
5
6
7
8
# 查看远程服务器的 NFS 共享
showmount -e 10.10.10.254

# 查看本机的 NFS 共享
showmount -e

# 查看已连接本机 NFS 共享的客户端
showmount -a

2. NFS 状态监控

1
2
3
4
5
6
7
8
# 查看 NFS 服务器状态
nfsstat -s

# 查看 NFS 客户端状态
nfsstat -c

# 实时监控 NFS 活动
nfsstat -m

3. 自动检查 NFS 共享脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 创建检查脚本
cat > ./check_nfs_shares.sh <<'EOF'
#!/bin/bash

# 定义要检查的服务器列表
servers=(
"10.10.10.242"
"10.10.10.252"
"10.10.10.243"
"10.10.10.254"
"10.10.10.251"
"10.10.10.245"
"10.10.10.246"
"10.10.10.247"
)

# 循环检查每个服务器的 NFS 共享
for server in "${servers[@]}"; do
echo "-----------------------"
echo -e "\033[1;31m检查服务器: $server\033[0m"

# 尝试获取共享列表,设置超时防止长时间等待
timeout 5s showmount -e $server 2>/dev/null && \
echo -e "\033[1;32m✓ 连接成功\033[0m" || \
echo -e "\033[1;31m✗ 连接失败或超时\033[0m"

echo "-----------------------"
done
EOF

# 赋予脚本执行权限并运行
chmod +x ./check_nfs_shares.sh
./check_nfs_shares.sh

⚙️ 五、高级配置与优化

1. NFS 性能优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 编辑 /etc/nfs.conf 文件进行高级配置
cat >> /etc/nfs.conf <<'EOF'
[nfsd]
threads=16

[mountd]
manage-gids=y

[statd]
state-directory-path=/var/lib/nfs/statd
EOF

# 重启 NFS 服务使配置生效
systemctl restart nfs-server

2. 自动挂载 (autofs) 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安装 autofs
yum install -y autofs

# 配置 autofs
cat > /etc/auto.master <<'EOF'
/mnt/nfs /etc/auto.nfs --timeout=300
EOF

# 创建 NFS 自动挂载配置
cat > /etc/auto.nfs <<'EOF'
ars2 -fstype=nfs,rw,soft,intr 10.10.10.251:/mnt/mydisk
pve -fstype=nfs,rw,soft,intr 10.10.10.254:/mnt/ntfs
debian -fstype=nfs,rw,soft,intr 10.10.10.247:/mnt/mydisk
EOF

# 重启 autofs 服务
systemctl restart autofs

3. SELinux 配置(如果需要)

1
2
3
4
5
6
# 检查 SELinux 状态
sestatus

# 如果 SELinux 启用,设置 NFS 相关布尔值
setsebool -P nfs_export_all_rw on
setsebool -P nfs_export_all_ro on

🔒 六、安全注意事项

1. 限制 NFS 访问权限

1
2
3
4
5
6
7
# 只允许特定 IP 段访问 NFS 共享
cat > /etc/exports <<'EOF'
/mnt/mydisk 10.10.10.0/24(rw,sync,no_subtree_check,no_root_squash)
EOF

# 重新加载配置
exportfs -r

2. 使用更安全的 NFSv4

1
2
3
4
# 在 /etc/fstab 中使用 NFSv4
cat >> /etc/fstab <<'EOF'
10.10.10.251:/mnt/mydisk /mnt/ARS2-NFS nfs4 nofail,x-systemd.device-timeout=15s,sec=sys 0 0
EOF

3. 定期检查 NFS 安全

1
2
3
4
5
6
7
8
# 检查 NFS 共享权限
showmount -e

# 检查谁在访问 NFS 共享
showmount -a

# 检查 NFS 日志
tail -f /var/log/messages | grep nfs

🐛 七、故障排除

1. 常见问题解决

1
2
3
4
5
6
7
8
9
10
11
# 如果挂载失败,检查网络连通性
ping 10.10.10.251

# 检查 NFS 服务是否运行
systemctl status nfs-server

# 检查防火墙设置
firewall-cmd --list-all

# 查看详细的错误信息
dmesg | grep nfs

2. 强制重新挂载

1
2
3
# 强制重新挂载所有 NFS 共享
umount -a -t nfs
mount -a

3. 清除 NFS 缓存

1
2
3
4
5
# 清除 NFS 客户端缓存
systemctl restart nfs-client.target

# 清除 NFS 服务器缓存
systemctl restart nfs-server

📊 总结

通过本指南,你已经学会了在 CentOS 系统上配置和管理 NFS 共享的完整流程:

  1. NFS 服务器配置:设置共享目录和访问权限
  2. NFS 客户端配置:临时和永久挂载远程共享
  3. 挂载管理:查看、取消和监控 NFS 挂载
  4. 高级配置:性能优化和安全设置
  5. 故障排除:解决常见的 NFS 连接问题

现在你可以轻松地在多个服务器之间共享文件了!🌐📂

💡 提示:在生产环境中,建议使用更安全的 NFSv4 协议,并限制只有可信的 IP 地址可以访问 NFS 共享。定期检查 NFS 日志和权限设置,确保系统安全。