搭建NFS服务器

ops/2024/9/23 9:50:29/

搭建NFS服务器

记录linux下搭建使用NFS服务器的一般步骤,以ubuntu20.04centos7.9操作进行记录。

1. 安装 NFS 服务器

  1. 运行以下命令安装 NFS 服务器

    # ubuntu下安装
    sudo apt-get update
    sudo apt install nfs-kernel-server
    # 配置服务
    sudo systemctl start nfs-kernel-server
    sudo systemctl enable --now nfs-kernel-server
    sudo systemctl status nfs-kernel-server# centos下载安装
    sudo yum -y update
    sudo yum install nfs-utils	# dnf安装类似
    # 服务为nfs-server或者nfs
    sudo systemctl start nfs-server.service
    sudo systemctl enable nfs-server.service
    sudo systemctl status nfs-server.service# 确认nfs版本,输出的第二列
    rpcinfo -p | grep nfs
    

说明:

可以在 /etc/nfs.conf 找到 NFS 守护进程的配置文件,还可以在 /etc/nfsmount.conf 找到挂载的配置文件。

需要注意的是,在标准的 NFS 配置中,主要的配置文件通常是 /etc/exports 用于 NFS 服务器的共享目录设置,而不是 /etc/nfs.conf。而客户端的配置通常涉及 /etc/fstab 或使用 mount 命令时指定选项,并没有一个专门的配置文件。

2. 创建并导出共享

NFS 客户端将在服务器机器上挂载一个目录,该目录有 NFS 服务器进行配置。

  1. 运行以下命令来指定挂载文件夹名称(例如,/mnt/nfs)。

    sudo mkdir -p /mnt/nfs
    
  2. 设置文件夹的权限。

    sudo chown -R nobody: /mnt/nfs
    sudo chmod -R 777 /mnt/nfs
    

说明:

如果是生产环境,建议按照最小权限进行设置,否则有安全隐患。

  1. 配置NFS共享

linux下nfs共享配置主要通过文件/etc/exports进行。配置语法:

[共享的目录1] [主机名1或IP地址1(参数1,参数2,...)] [主机名2或IP地址2(参数1,参数2,...)]

[共享的目录2] [主机名1或IP地址1(参数1,参数2,...)] [主机名2或IP地址2(参数3,参数4,...)]

编辑NFS的配置文件/etc/exports

sudo vi /etc/exports

将客户端信息添加到文件中:

[root@nfs ~]# cat /etc/exports
/mnt/nfs 10.210.0.0/16(rw,sync,no_all_squash,root_squash,no_subtree_check)

参数说明:

  • /mnt/nfs: 共享的目录路径。

  • 192.168.0.0/24: 设置可以访问的客户端网络;除了网段,也可以设置ip或者域名(client.test.com或者*.test.com),也可以用*允许所有客户端网络。

  • rw,sync,no_all_squash,root_squash,no_subtree_check:

    • rw:表示目录读写权限,如果是ro表示只读。
    • sync:数据写入磁盘模式。sync表示同步写入,数据会同步写入到内存和硬盘中,效率低,但可以保证数据一致性。相反 async为异步写入, 表示数据会先暂存于内存中,而非直接写入到硬盘中,效率高,但极端情况下无法保证数据一致性。
    • all_squash: 无论访表示将客户端请求中的所有用户 ID (UIDs) 和组 ID (GIDs) 映射为 NFS 服务器上相同的 UIDs 和 GIDs。
    • no_all_squash: 客户端请求中的用户先与本机用户匹配,匹配失败后再映射为匿名用户或用户组。
    • root_squash: 将客户端root用户的请求映射到一个匿名的用户 ID (UID)/组 ID (GID)。
    • no_root_squash:表示当客户机以root身份访问时赋予本地root权限,安全风险较大。
    • no_subtree_check:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率。
    • subtree_check:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限。
  1. 导出共享给客户端

使用exportfs命令加载exports配置:

sudo exportfs -arv

或者也可以重启 NFS 服务器

# ubuntu
sudo systemctl restart nfs-kernel-server# centos
systemctl resstart nfs

查看export列表:

root@nfs:~# showmount -e localhost
Export list for localhost:
/mnt/nfs 10.210.0.0/16

3. 配置防火墙规则

# 1. ubuntu。
## 可以关闭防火墙
[root@nfs ~]# ufw disable
Firewall stopped and disabled on system startup
[root@nfs ~]# ufw status
Status: inactive## 或者ufw放行规则
sudo ufw allow nfs
sudo ufw allow portmapper
sudo ufw reload# 2. centos
## 关闭防火墙
systemctl stop firewalld;systemctl disable 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

按照端口放行:

# nfs相关端口
[root@nfs ~]# rpcinfo -p localhostprogram vers proto   port  service100000    4   tcp    111  portmapper100000    3   tcp    111  portmapper100000    2   tcp    111  portmapper100000    4   udp    111  portmapper100000    3   udp    111  portmapper100000    2   udp    111  portmapper100005    1   udp  20048  mountd100005    1   tcp  20048  mountd100005    2   udp  20048  mountd100024    1   udp  48719  status100024    1   tcp  37476  status100005    2   tcp  20048  mountd100005    3   udp  20048  mountd100005    3   tcp  20048  mountd100003    3   tcp   2049  nfs100003    4   tcp   2049  nfs100227    3   tcp   2049  nfs_acl100003    3   udp   2049  nfs100003    4   udp   2049  nfs100227    3   udp   2049  nfs_acl100021    1   udp  53709  nlockmgr100021    3   udp  53709  nlockmgr100021    4   udp  53709  nlockmgr100021    1   tcp  37318  nlockmgr100021    3   tcp  37318  nlockmgr100021    4   tcp  37318  nlockmgr

nfs除了固定的port111、2049外,还有其他服务如rpc.mounted等开启的不固定的端口,如果按照端口配置对防火墙来说就比较麻烦。为了解决这个问题,我们可以设置NFS服务的端口配置文件/etc/sysconfig/nfs,将下列内容的注释去掉,如果没有则添加:

RQUOTAD_PORT=1001
LOCKD_TCPPORT=30001
LOCKD_UDPPORT=30002
MOUNTD_PORT=1002

保存后,将端口加入防护墙放行:

# ufw
sudo ufw allow proto tcp from any to any port 111
sudo ufw allow proto udp from any to any port 111
sudo ufw allow proto tcp from any to any port 2049
sudo ufw allow proto udp from any to any port 2049
sudo ufw reload# firewalld
firewall-cmd --zone=public --add-port=111/tcp --add-port=111/udp --add-port=2049/tcp --add-port=2049/udp --add-port=1001/tcp --add-port=1001/udp --add-port=1002/tcp --add-port=1002/udp --add-port=30001/tcp --add-port=30002/udp --permanent
firewall-cmd --reload

4. 客户端挂载验证

nfs挂载需要安装nfs客户端:

# ubuntu
sudo apt-get install nfs-common# centos
yum -y install nfs-utils	# dnf安装类似

执行挂载命令:

# 查看nfs共享的目录
[root@client ~]# showmount -e 10.210.10.209
Export list for 10.210.10.209:
/mnt/nfs 10.210.0.0/16# 挂载命令如下
mount -t nfs <nfs-server-ip>:/exports-dir /local-dir[root@client:~]# mkdir -p /mnt/nfs
# -t配置fs类型,设置nfs或者nfs4
[root@client:~]# mount -t nfs 10.210.10.209:/mnt/nfs /mnt/nfs	
[root@client ~]# sudo mount | grep -i nfs
10.210.10.209:/mnt/nfs on /mnt/nfs type nfs4 (rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.210.10.220,local_lock=none,addr=10.210.10.209)# 测试读写
[root@client ~]# cd /mnt/nfs
[root@dns nfs]# echo aaa > test.txt
[root@dns nfs]# cat test.txt
aaa

配置重启系统自动挂载:

[root@client ~]# cat /etc/fstab
...
10.210.10.209:/mnt/nfs /mnt/nfs nfs defaults,_netdev 0 0
[root@client ~]# mount -a

说明:

  1. 参数_netdev表示延迟加载,等待网络服务启动后再挂载。

5. 相关命令

# 查看nfs版本及统计数据
nfsstat -s	# 服务器端使用
nfsstat -c	# 客户端使用

6. 相关资料

  1. How to configure NFS on Linux - Linux Tutorials - Learn Linux Configuration
  2. Learning NFS through server and client configuration | Enable Sysadmin (redhat.com)、
  3. 第 4 章 挂载 NFS 共享 | Red Hat Product Documentation

http://www.ppmy.cn/ops/99025.html

相关文章

鲲鹏服务器安装Kafka

由于项目需求&#xff0c;需要在鲲鹏云主机上安装Kafka&#xff0c;并且要求安装的版本为2.3.X。下面主要从以下几个步骤说明如何安装&#xff1a; 1、下载kafka的安装文件 2、上传到服务器 3、修改配置 4、启动 5、使用工具测试 服务器信息 CPU信息 [rootecs02 ~]# lscpu A…

besier打断和升阶,高阶性质

欢迎关注更多精彩 关注我&#xff0c;学习常用算法与数据结构&#xff0c;一题多解&#xff0c;降维打击。 问题描述 对besier曲线在u处打断&#xff0c;生成两条besier曲线对besier曲线升阶处理 bezier高阶性质 求导推导 P ( t ) ∑ i 0 n B i n ( t ) b i \boldsymbol …

封装el-table核心代码

问题背景 封装el-tabletable的时候有时候忘记语法了&#xff0c;然后问ai或者百度又出来一堆不太正确的回答&#xff0c;作此记录。 1.封装好的table组件完整代码&#xff08;基础版&#xff09; <template><div><el-table:data"tableData":height…

iis部署服务时,发现只能进行get请求,无法发起post、put请求

问题描述&#xff1a; iis部署服务时&#xff0c;发现只能进行get请求&#xff0c;无法发起post、put请求 问题原因&#xff1a; iis部署时&#xff0c;webDAV模块限制 解决方法&#xff1a; 1.搜索【服务器管理器】 2.点击【删除角色功能】 3.选中WebDAV&#xff0c;点…

OpenCV与AI深度学习 | 基于改进YOLOv8的景区行人检测算法

本文来源公众号“OpenCV与AI深度学习”&#xff0c;仅用于学术分享&#xff0c;侵权删&#xff0c;干货满满。 原文链接&#xff1a;基于改进YOLOv8的景区行人检测算法 作者&#xff1a;贵向泉&#xff0c;刘世清&#xff0c;李立等 来源&#xff1a;《计算机工程》期刊 编…

华为数通方向HCIP-DataCom H12-821题库(更新单选真题:21-30)

第21题 以下关于0SPF中ABR的描述,错误的是哪一项? A、ABR将连接的非骨干区域内的1类和2类1SA转换成3类LSA,发布到骨干区域中 B、ABR不能够产生4类和5类LSA C、ABR上有多个LSDB,ABR为每一个区域维护一个LSDB D、ABR将骨干区域内的1类、2类LSA和3类LSA转换成三类LSA,发布到…

kubernetes k8s Secret 概述与配置讲解

目录 1 Secret概述 1.1 Secret是什么&#xff1f; 1.2 使用Secret 1 Secret概述 1.1 Secret是什么&#xff1f; 上面我们学习的Configmap一般是用来存放明文数据的&#xff0c;如配置文件&#xff0c;对于一些敏感数据&#xff0c;如密码、私钥等数据时&#xff0c;要用sec…

期末九天从入门到精通操作数据库(mysql)

对应资源包名称&#xff1a; 期末九天从入门到精通操作数据库(mysql) 学习目标&#xff1a; 掌握数据库的基本操作&#xff0c;熟练使用navicat工具. 九天极限掌握数据库 学习内容&#xff1a; 数据查询实验视图管理实验索引管理实验用户安全性管理实验MySQL备份和还原实…