NFS (Network File System) 是一种分布式文件系统协议,允许通过网络在不同主机间共享文件系统。以下是完整的NFS挂载设置步骤:
# Ubuntu/Debian
sudo apt update
sudo apt install nfs-kernel-server
# RHEL/CentOS
sudo yum install nfs-utils
sudo mkdir -p /shared/directory
sudo chown nobody:nogroup /shared/directory # 设置权限
sudo chmod 777 /shared/directory # 简化权限设置(生产环境应根据需要调整)
编辑 /etc/exports
文件:
sudo nano /etc/exports
添加以下内容(根据需求调整):
/shared/directory client_ip(rw,sync,no_subtree_check)
或允许整个子网访问:
/shared/directory 192.168.1.0/24(rw,sync,no_subtree_check)
选项说明:
- rw
:读写权限
- ro
:只读权限
- sync
:同步写入
- no_subtree_check
:提高可靠性
- no_root_squash
:允许root访问(有安全风险)
sudo exportfs -a # 重新导出所有共享
sudo systemctl restart nfs-kernel-server # 重启服务
# RHEL/CentOS 7+
sudo systemctl enable nfs-server
sudo systemctl start nfs-server
# Ubuntu UFW
sudo ufw allow from client_ip to any port nfs
# RHEL/CentOS firewalld
sudo firewall-cmd --permanent --add-service=nfs
sudo firewall-cmd --permanent --add-service=mountd
sudo firewall-cmd --permanent --add-service=rpc-bind
sudo firewall-cmd --reload
# Ubuntu/Debian
sudo apt update
sudo apt install nfs-common
# RHEL/CentOS
sudo yum install nfs-utils
sudo mkdir -p /mnt/nfs/share
sudo mount -t nfs server_ip:/shared/directory /mnt/nfs/share
df -h
mount | grep nfs
编辑 /etc/fstab
文件:
sudo nano /etc/fstab
添加以下行:
server_ip:/shared/directory /mnt/nfs/share nfs rw,soft,intr 0 0
常用选项:
- rw
:读写
- ro
:只读
- soft
:超时后放弃挂载
- hard
:持续尝试(默认)
- intr
:允许中断挂载操作
- noexec
:禁止执行二进制文件
- nosuid
:忽略suid/sgid位
sudo mount -a
连接被拒绝:
权限问题:
/etc/exports
中的权限设置all_squash
和anonuid
/anongid
选项性能问题:
async
选项(有数据丢失风险)rsize
和wsize
参数(如mount -o rsize=32768,wsize=32768
)查看NFS状态:
# 服务端
showmount -e localhost
# 客户端
showmount -e server_ip
日志查看:
# 服务端日志
tail -f /var/log/syslog | grep nfs
# RHEL/CentOS
journalctl -u nfs-server
no_root_squash
选项通过以上步骤,您应该能够成功设置和使用NFS网络文件系统。根据您的具体需求调整权限和配置选项。