NFS(网络文件系统)概念及操作命令

服务端

  1. 安装依赖
sudo yum install nfs-utils rpcbind -y
  1. 创建共享目录
sudo mkdir -p /webapps/NFS_Files
sudo chmod 755 /webapps/NFS_Files
sudo chown nobody:nobody /webapps/NFS_Files  # 或根据需求设置所有者
  1. 配置共享参数
    编辑 /etc/exports 文件:
sudo vi /etc/exports

添加共享配置(示例):

/webapps/NFS_Files 192.168.1.0/24(rw,sync,all_squash)

括号内的参数说明:

  • rw:读写权限
  • ro:只读权限
  • sync:同步写入
  • async:异步写入
  • no_root_squash:不将 root 用户映射为匿名用户
  • root_squash:将 root 用户映射为匿名用户(默认)
  • all_squash:将所有用户映射为匿名用户
  • anonuid/anongid:指定匿名用户的 UID/GID

4.启动服务

# 启动 rpcbind 服务
sudo systemctl start rpcbind
# 添加自启动
sudo systemctl enable rpcbind

# 启动 NFS 服务
sudo systemctl start nfs-server
# 添加自启动
sudo systemctl enable nfs-server
# 对于 Ubuntu 系统,还需启动 nfs-common
sudo systemctl start nfs-common
sudo systemctl enable nfs-common
  1. 防火墙配置(可选)
    开放 NFS 相关端口:
# CentOS/RHEL 系统
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 系统(使用 ufw)
sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw reload

6.检查NFS 服务端状态

sudo systemctl status nfs-server

至此,服务端设置完毕。

客户端

1.查看共享服务端目录,要提前知道服务端IP地址(192.168.1.20)

showmount -e 192.168.1.20

2.临时挂载服务端共享文件

sudo mount -t nfs -o rw 192.168.1.20:/webapps/NFS_Files /webapps/NFS_Files

验证方法:

访问挂载点文件夹目录,触发自动挂载文件同步

ls /webapps/NFS_Files

查看挂载状态

mount | grep NFS_Files

2.永久挂载: 在/etc/fstab中添加配置,添加_netdev选项,确保网络就绪后再挂载。

192.168.1.20:/webapps/NFS_Files /webapps/NFS_Files nfs defaults,_netdev 0 0

2.1.检查fstab语法,并挂载

sudo mount -a

2.2查看挂载状态

mount | grep nfs

至此,客户端设置完毕,多个客户端,重复参照客户端设置。

---------------------------后面的内容有时间再看吧------------------------------

详解:

NFS 操作手册

1. NFS 简介

1.1 定义

NFS(Network File System)即网络文件系统,是一种基于 TCP/IP 协议的分布式文件系统协议,由 Sun 公司开发,允许客户端通过网络访问服务器上的文件,如同访问本地文件一样。

1.2 核心功能

  • 实现跨主机文件共享
  • 支持多用户同时访问同一文件系统
  • 提供文件的一致性访问
  • 允许不同操作系统间共享文件

1.3 应用场景

  • 企业级文件共享平台
  • 集群环境下配置文件共享
  • Web 服务器静态资源共享
  • 大型应用程序数据存储
  • 备份与恢复系统

1.4 工作原理

NFS 通过 RPC(Remote Procedure Call)机制实现客户端与服务器之间的通信,主要组件包括:

  • NFS 服务:管理文件系统访问
  • RPC 绑定器:管理端口映射
  • 锁管理器:处理文件锁定
  • 状态监控器:维护连接状态

1.5 优缺点

优点

  • 简化文件管理
  • 节省存储资源
  • 易于扩展
  • 支持异构环境

缺点

  • 依赖网络稳定性
  • 性能受网络带宽限制
  • 权限管理较复杂
  • 缺乏高级数据冗余功能

2. NFS 服务端

2.1 服务端安装

2.1.1 CentOS/RHEL 系统
sudo yum install nfs-utils rpcbind -y
2.1.2 Ubuntu/Debian 系统
sudo apt-get install nfs-kernel-server -y

2.2 服务端配置

2.2.1 创建共享目录
sudo mkdir -p /data/nfs_share
sudo chmod 755 /data/nfs_share
sudo chown nobody:nobody /data/nfs_share  # 或根据需求设置所有者
2.2.2 配置共享参数

编辑 /etc/exports 文件:

sudo vi /etc/exports

添加共享配置(示例):

/data/nfs_share 192.168.1.0/24(rw,sync,no_root_squash)
/public        *(ro,sync,all_squash,anonuid=1001,anongid=1001)

参数说明:

  • rw:读写权限
  • ro:只读权限
  • sync:同步写入
  • async:异步写入
  • no_root_squash:不将 root 用户映射为匿名用户
  • root_squash:将 root 用户映射为匿名用户(默认)
  • all_squash:将所有用户映射为匿名用户
  • anonuid/anongid:指定匿名用户的 UID/GID
2.2.3 启动服务
# 启动 rpcbind 服务
sudo systemctl start rpcbind
sudo systemctl enable rpcbind

# 启动 NFS 服务
sudo systemctl start nfs-server
sudo systemctl enable nfs-server

# 对于 Ubuntu 系统,还需启动 nfs-common
sudo systemctl start nfs-common
sudo systemctl enable nfs-common
2.2.4 防火墙配置

开放 NFS 相关端口:

# CentOS/RHEL 系统
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 系统(使用 ufw)
sudo ufw allow from 192.168.1.0/24 to any port nfs
sudo ufw reload

2.3 服务端管理命令

2.3.1 查看导出列表
sudo exportfs -v
2.3.2 重新导出共享
sudo exportfs -ra  # 重新导出所有共享
sudo exportfs -r   # 重新导出指定共享
2.3.3 查看 NFS 服务状态
sudo systemctl status nfs-server
2.3.4 查看连接客户端
sudo showmount -a

2.4 服务端优化建议

2.4.1 性能优化参数

/etc/sysctl.conf 中添加:

sunrpc.tcp_max_slot_table_entries = 128
sunrpc.tcp_slot_table_entries = 128

应用配置:

sudo sysctl -p
2.4.2 使用 NFSv4

编辑 /etc/default/nfs-kernel-server(Ubuntu)或 /etc/sysconfig/nfs(CentOS):

RPCNFSDCOUNT=8  # 增加 NFS 服务线程数
2.4.3 监控 NFS 性能
nfsstat -s  # 服务器统计信息
nfsstat -c  # 客户端统计信息

3. NFS 客户端

3.1 客户端安装

3.1.1 CentOS/RHEL 系统
sudo yum install nfs-utils rpcbind -y
3.1.2 Ubuntu/Debian 系统
sudo apt-get install nfs-common -y

3.2 客户端挂载

3.2.1 手动挂载
# 创建挂载点
sudo mkdir -p /mnt/nfs_share

# 挂载 NFS 共享
sudo mount -t nfs 192.168.1.100:/data/nfs_share /mnt/nfs_share

# 指定挂载选项
sudo mount -t nfs -o rw,hard,intr,rsize=8192,wsize=8192 192.168.1.100:/data/nfs_share /mnt/nfs_share

常用挂载选项:

  • rw/ro:读写/只读
  • hard/soft:硬挂载/软挂载
  • intr:允许中断挂载操作
  • rsize/wsize:读取/写入块大小
  • timeo:超时设置(以 0.1 秒为单位)
3.2.2 自动挂载(/etc/fstab)

编辑 /etc/fstab 文件:

sudo vi /etc/fstab

添加挂载项:

192.168.1.100:/data/nfs_share /mnt/nfs_share nfs defaults 0 0

参数说明:

  • defaults:使用默认选项(rw,suid,dev,exec,auto,nouser,async)
  • 0 0:不备份,不自检

挂载所有 fstab 中的文件系统:

sudo mount -a
3.2.3 使用 autofs 动态挂载

安装 autofs:

# CentOS/RHEL
sudo yum install autofs -y

# Ubuntu/Debian
sudo apt-get install autofs -y

配置 auto.master:

sudo vi /etc/auto.master

添加:

/mnt/nfs    /etc/auto.nfs

创建 auto.nfs 文件:

sudo vi /etc/auto.nfs

添加:

share   -rw,hard,intr    192.168.1.100:/data/nfs_share

重启 autofs 服务:

sudo systemctl restart autofs

3.3 客户端管理命令

3.3.1 查看可用共享
showmount -e 192.168.1.100
3.3.2 卸载 NFS 挂载
sudo umount /mnt/nfs_share

# 强制卸载
sudo umount -f /mnt/nfs_share

# 卸载繁忙的挂载点
sudo umount -l /mnt/nfs_share  # 懒卸载(lazy unmount)
3.3.3 检查挂载状态
mount | grep nfs
df -h | grep nfs
3.3.4 测试 NFS 性能
time dd if=/dev/zero of=/mnt/nfs_share/testfile bs=1M count=100 oflag=direct

3.4 客户端权限管理

3.4.1 用户映射配置

确保客户端和服务器 UID/GID 一致性:

# 服务器和客户端创建相同 UID/GID 的用户
sudo useradd -u 1001 -g 1001 nfsuser
3.4.2 特殊挂载选项
# 挂载时指定用户和组
sudo mount -t nfs -o uid=1001,gid=1001 192.168.1.100:/data/nfs_share /mnt/nfs_share

# 禁用 setuid 权限
sudo mount -t nfs -o nosuid 192.168.1.100:/data/nfs_share /mnt/nfs_share

4. NFS 命令汇总

4.1 服务端命令

命令描述
exportfs -a导出所有 /etc/exports 中定义的共享目录
exportfs -r重新导出所有共享目录
exportfs -v显示当前导出的共享目录及选项
exportfs -u取消导出指定的共享目录
showmount -e显示本地 NFS 服务器的导出列表
showmount -a显示当前连接到本地 NFS 服务器的客户端
rpcinfo -p显示 RPC 服务的端口映射信息
systemctl start nfs-server启动 NFS 服务
systemctl enable nfs-server设置 NFS 服务开机自启

4.2 客户端命令

命令描述
showmount -e <server>显示远程 NFS 服务器的导出列表
mount -t nfs <server>:/path /mount/point挂载 NFS 共享目录
umount /mount/point卸载 NFS 挂载点
umount -f /mount/point强制卸载 NFS 挂载点
umount -l /mount/point懒卸载 NFS 挂载点
df -hT查看挂载的文件系统类型和空间使用情况
`mountgrep nfs`

4.3 通用命令

命令描述
nfsstat显示 NFS 统计信息
nfsstat -s显示 NFS 服务器统计信息
nfsstat -c显示 NFS 客户端统计信息
rpcdebug -m nfs -s all启用 NFS 调试模式
rpcdebug -m nfs -c all禁用 NFS 调试模式
rpcbind -h显示 RPC 绑定器帮助信息
systemctl restart rpcbind重启 RPC 绑定器服务

4.4 故障排除命令

命令描述
ping <server>测试与 NFS 服务器的网络连通性
telnet <server> 111测试 RPC 服务端口连通性
telnet <server> 2049测试 NFS 服务端口连通性
rpcinfo -p <server>检查服务器 RPC 服务注册情况
lsof +D /mount/point查看占用 NFS 挂载点的进程
fuser -mv /mount/point查看使用 NFS 挂载点的用户和进程
journalctl -u nfs-server查看 NFS 服务器日志
`dmesggrep nfs`

附录:常见问题及解决方案

Q1:挂载时提示 “mount.nfs: Connection refused”

原因

  • NFS 服务器未启动
  • RPC 服务未启动
  • 防火墙阻止连接
  • 服务器 IP 地址错误

解决方案

# 检查服务器 NFS 服务
sudo systemctl status nfs-server

# 检查 RPC 服务
sudo systemctl status rpcbind

# 开放防火墙端口
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

Q2:挂载时提示 “mount.nfs: No route to host”

原因

  • 网络不通
  • 服务器 IP 地址错误
  • 网关配置错误

解决方案

# 检查网络连通性
ping 192.168.1.100

# 检查路由配置
route -n

# 检查网卡状态
ifconfig

Q3:挂载后文件权限异常

原因

  • UID/GID 不匹配
  • 服务器配置了 root_squash 或 all_squash
  • 客户端挂载选项与服务器不一致

解决方案

# 查看文件所有者 UID/GID
ls -l /mnt/nfs_share

# 同步客户端和服务器的 UID/GID
sudo useradd -u 1001 nfsuser

# 修改挂载选项
sudo mount -t nfs -o uid=1001,gid=1001 192.168.1.100:/data/nfs_share /mnt/nfs_share

Q4:卸载时提示 “device is busy”

原因

  • 有进程正在访问挂载点
  • 文件被锁定
  • NFS 连接挂起

解决方案

# 查找占用进程并终止
sudo lsof +D /mnt/nfs_share
sudo fuser -kmv /mnt/nfs_share

# 尝试懒卸载
sudo umount -l /mnt/nfs_share

# 重启 NFS 服务
sudo systemctl restart nfs-server

Q5:NFS 性能不佳

解决方案

# 调整挂载选项
sudo mount -t nfs -o rsize=32768,wsize=32768,hard,intr 192.168.1.100:/data/nfs_share /mnt/nfs_share

# 启用 TCP 窗口缩放
echo 1 > /proc/sys/net/ipv4/tcp_window_scaling

# 增加 NFS 服务线程数
echo "RPCNFSDCOUNT=16" >> /etc/sysconfig/nfs
sudo systemctl restart nfs-server
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值