配置镜像源
# 备份文件
cd /etc/yum.repos.d/ && mkdir backup
mv CentOS-* backup/
mv epel* backup/
# 下载源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 安装epel-release源
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
# 更新检索密钥
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
# 清理并生成缓存
yum clean all && yum makecache
# 安装 Linux 工具
yum -y install wget bash-completion net-tools vim tree
# 更新检索密钥
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
安装NFS服务
# 安装 NFS 服务组件和 RPC 绑定服务
yum install -y nfs-utils rpcbind
# 创建共享目录
mkdir -p /data/nfs
# 设置目录权限(例如允许读写)
chmod -R 775 /data/nfs
配置 NFS 共享目录
编辑 /etc/exports
文件,添加共享目录及客户端访问权限
# 在文件中添加以下内容(根据需求调整参数):
/data/nfs 192.168.1.0/24(rw,sync,no_root_squash) # 允许子网内的客户端读写
# 或指定单个客户端 IP:
# /mnt/nfs 192.168.1.10(rw,sync,no_root_squash)
参数说明:
rw
:允许读写(ro
为只读)。sync
:同步写入(确保数据一致性,推荐使用)。no_root_squash
:允许客户端以 root 身份访问共享目录(生产环境需谨慎)。
加载并验证配置
# 重新加载 exports 配置
exportfs -ra
# 查看共享目录状态
exportfs -v
启动服务
# 启动 RPC 绑定服务和 NFS 服务
sudo systemctl start rpcbind
sudo systemctl start nfs-server
# 设置开机自启
sudo systemctl enable rpcbind
sudo systemctl enable nfs-server
# 开放 NFS 必要端口
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
客户端挂载测试
安装nfs工具
# 检查是否已安装nfs-utils
rpm -qa | grep nfs-utils # 或 yum list installed nfs-utils
# 若未安装,执行安装(CentOS/RHEL)
sudo yum install nfs-utils -y
# 对于Debian/Ubuntu系统:
sudo apt-get install nfs-common -y
挂载程序
mkdir -p /tmp/testnfs \
&& mount -t nfs nfs.server:/data/nfs /tmp/testnfs \
&& echo "hello nfs" >> /tmp/testnfs/test.txt \
&& cat /tmp/testnfs/test.txt
# 取消挂载
umount /tmp/testnfs