首先安装nfs服务,1. 服务器端安装nfs-utils或nfs-kernel-server,2. 创建共享目录并设置权限,3. 配置/etc/exports定义共享规则,4. 启动rpcbind和nfs-server并设置开机自启,5. 防火墙放行nfs、rpc-bind、mountd服务或对应端口;客户端配置:1. 安装nfs-utils或nfs-common,2. 创建挂载点,3. 使用mount命令手动挂载nfs共享目录,4. 在/etc/fstab中添加条目实现开机自动挂载并包含_netdev选项;排查问题时需检查防火墙、服务状态、网络连通性及权限设置,通过showmount和df命令验证共享与挂载状态,最终在客户端写入文件并在服务器端确认内容以测试读写功能,确认nfs共享正常工作后即可用于局域网内的文件共享场景。

配置NFS(Network File System,网络文件系统)可以实现Linux系统之间的文件共享,适合局域网内多台机器访问同一存储目录的场景。以下是基于Linux(以CentOS/RHEL或Ubuntu为例)搭建NFS服务器和客户端的完整步骤。
CentOS/RHEL系统:
sudo yum install nfs-utils -y
Ubuntu/Debian系统:
sudo apt update sudo apt install nfs-kernel-server -y
安装完成后,nfs-utils 或 nfs-kernel-server 会提供核心服务。
选择一个目录作为NFS共享目录,例如
/data/share
sudo mkdir -p /data/share sudo chmod 777 /data/share # 根据实际权限需求调整
编辑NFS配置文件:
sudo vim /etc/exports
添加共享规则,例如:
/data/share 192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check)
参数说明:
rw
sync
no_root_squash
root_squash
no_subtree_check
可指定单个IP:
,或网段:192.168.1.100登录后复制192.168.1.0/24登录后复制
CentOS/RHEL:
sudo systemctl enable rpcbind nfs-server sudo systemctl start rpcbind nfs-server
Ubuntu:
sudo systemctl enable nfs-kernel-server sudo systemctl start nfs-kernel-server
注意:NFS依赖
服务,需确保其运行。rpcbind登录后复制
CentOS/RHEL(firewalld):
sudo firewall-cmd --permanent --add-service=nfs sudo firewall-cmd --permanent --add-service=rpc-bind sudo firewall-cmd --permanent --add-service=mountd sudo firewall-cmd --reload
Ubuntu(ufw):
sudo ufw allow from 192.168.1.0/24 to any port nfs
CentOS/RHEL:
sudo yum install nfs-utils -y
Ubuntu/Debian:
sudo apt install nfs-common -y
sudo mkdir -p /mnt/nfs/share
sudo mount -t nfs 192.168.1.10:/data/share /mnt/nfs/share
替换
为NFS服务器IP192.168.1.10登录后复制
验证是否挂载成功:
df -h | grep nfs
编辑
/etc/fstab
192.168.1.10:/data/share /mnt/nfs/share nfs defaults,_netdev 0 0
表示网络就绪后再挂载,避免开机挂载失败_netdev登录后复制
挂载时报错 mount.nfs: Connection timed out
检查服务器防火墙、NFS服务是否运行、网络是否通(ping + telnet 端口测试)。
权限拒绝(Permission denied)
检查
/etc/exports
root_squash
NFS服务无法启动
确保
rpcbind
systemctl status rpcbind
客户端无法解析共享目录
可执行
showmount -e 192.168.1.10
在客户端执行:
echo "Hello NFS" > /mnt/nfs/share/test.txt
到服务器端查看:
cat /data/share/test.txt
如果内容一致,说明NFS共享正常工作。
基本上就这些。NFS搭建不复杂,但需要注意权限、防火墙和服务依赖。适合内网环境下的文件共享,如开发协同、备份存储等场景。
以上就是如何配置NFS共享 网络文件系统搭建的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号