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

📋 导航目录
✨ NFS 简介与优势
NFS (Network File System) 是一种分布式文件系统协议,允许通过网络在不同主机间共享文件系统,具有以下优势:
- 🌐 网络共享:实现多台服务器之间的文件共享和访问
- ⚡ 高性能:提供快速的网络文件访问速度
- 🔄 透明访问:客户端可以像访问本地文件一样访问远程文件
- 👥 多用户支持:支持多个客户端同时访问共享目录
- 🛡️ 权限控制:可以设置不同的访问权限和安全性选项
- 💾 资源集中:集中存储管理,节省存储空间
🚀 快速开始
1. 使用科技lion一键脚本工具
1 2
| 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
| 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
cat > /etc/exports <<'EOF'
/mnt/mydisk *(rw,fsid=0,no_subtree_check,no_root_squash,insecure,sync) EOF
exportfs -r systemctl restart nfs-server
|
2. 验证 NFS 服务器配置
1 2 3 4 5 6 7 8
| systemctl status nfs-server
showmount -e
cat /proc/fs/nfsd/versions
|
3. 防火墙配置(如果需要)
1 2 3 4 5
| 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
| mkdir -pm 755 /mnt/ARS2-NFS mount -t nfs -o rw 10.10.10.251:/mnt/mydisk /mnt/ARS2-NFS
mkdir -pm 755 /mnt/PVE-NFS mount -t nfs -o rw 10.10.10.254:/mnt/ntfs /mnt/PVE-NFS
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
cat > /etc/fstab <<'EOF'
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
| mkdir -pm 755 /mnt/ARS2-NFS
cat >> /etc/fstab <<'EOF'
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
| mount -a
mount | grep nfs
|
🔧 三、NFS 挂载管理
1. 查看 NFS 挂载状态
1 2 3 4 5
| df -hT /mnt/*
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
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
| showmount -e 10.10.10.254
showmount -e
showmount -a
|
2. NFS 状态监控
1 2 3 4 5 6 7 8
| nfsstat -s
nfsstat -c
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'
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" )
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
| cat >> /etc/nfs.conf <<'EOF' [nfsd] threads=16
[mountd] manage-gids=y
[statd] state-directory-path=/var/lib/nfs/statd EOF
systemctl restart nfs-server
|
2. 自动挂载 (autofs) 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| yum install -y autofs
cat > /etc/auto.master <<'EOF' /mnt/nfs /etc/auto.nfs --timeout=300 EOF
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
systemctl restart autofs
|
3. SELinux 配置(如果需要)
1 2 3 4 5 6
| sestatus
setsebool -P nfs_export_all_rw on setsebool -P nfs_export_all_ro on
|
🔒 六、安全注意事项
1. 限制 NFS 访问权限
1 2 3 4 5 6 7
| 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
| 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
| showmount -e
showmount -a
tail -f /var/log/messages | grep nfs
|
🐛 七、故障排除
1. 常见问题解决
1 2 3 4 5 6 7 8 9 10 11
| ping 10.10.10.251
systemctl status nfs-server
firewall-cmd --list-all
dmesg | grep nfs
|
2. 强制重新挂载
1 2 3
| umount -a -t nfs mount -a
|
3. 清除 NFS 缓存
1 2 3 4 5
| systemctl restart nfs-client.target
systemctl restart nfs-server
|
📊 总结
通过本指南,你已经学会了在 CentOS 系统上配置和管理 NFS 共享的完整流程:
- ✅ NFS 服务器配置:设置共享目录和访问权限
- ✅ NFS 客户端配置:临时和永久挂载远程共享
- ✅ 挂载管理:查看、取消和监控 NFS 挂载
- ✅ 高级配置:性能优化和安全设置
- ✅ 故障排除:解决常见的 NFS 连接问题
现在你可以轻松地在多个服务器之间共享文件了!🌐📂
💡 提示:在生产环境中,建议使用更安全的 NFSv4 协议,并限制只有可信的 IP 地址可以访问 NFS 共享。定期检查 NFS 日志和权限设置,确保系统安全。