关于linux:Ubuntu-2004-中配置NFS服务

37次阅读

共计 3103 个字符,预计需要花费 8 分钟才能阅读完成。

NFS 是 Network File System 的首字母缩写。它是一种分布式协定,使客户端能够拜访近程服务器上的共享文件。在本文中,将在 Ubuntu 20.04 LTS 中装置 NFS 服务。而后,演示如何从客户端零碎拜访服务器上的文件。
零碎环境
NFS 服务端:Ubuntu 20.04 LTS,IP 地址:192.168.43.174
NFS 客户端:Centos8,IP 地址:192.168.43.131

一、装置 NFS 服务端
运行上面命令装置 NFS 服务端:

bob@ubuntu-20-04:~$ sudo apt install nfs-kernel-server
应用上面命令查看 nfs-server 是否曾经启动:

bob@ubuntu-20-04:~$ sudo systemctl status nfs-server
● nfs-server.service – NFS server and services

 Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
 Active: active (exited) since Wed 2021-04-21 10:20:29 CST; 1min 30s ago

Main PID: 41727 (code=exited, status=0/SUCCESS)

  Tasks: 0 (limit: 2278)
 Memory: 0B
 CGroup: /system.slice/nfs-server.service

4 月 21 10:20:28 ubuntu-20-04 systemd[1]: Starting NFS server and services…
4 月 21 10:20:29 ubuntu-20-04 systemd[1]: Finished NFS server and services.
Ubuntu 20.04 中配置 NFS 服务 Ubuntu 20.04 中配置 NFS 服务

二、创立 NFS 共享目录
下一步将创立一个 NFS 共享目录。咱们将在 /mnt 目录中创立,在这里,咱们的 NFS 共享目录称为 ShareFolder:

bob@ubuntu-20-04:~$ sudo mkdir -p /mnt/ShareFolder
咱们心愿所有客户端都能够拜访该共享文件夹外面的内容,因而调配最高权限:

bob@ubuntu-20-04:~$ sudo chown nobody:nogroup /mnt/ShareFolder
bob@ubuntu-20-04:~$ sudo chmod -R 777 /mnt/ShareFolder/
三、编辑 exports 配置文件
通过编辑 /etc/exports 配置文件,来容许哪些客户端能够拜访该共享。

上面命令关上该配置文件:

bob@ubuntu-20-04:~$ sudo vim /etc/exports
上面的条目示意容许单个客户端拜访、多个客户端拜访、容许一个网段的客户端拜访:

如果只容许一个客户端拜访,能够只写一个客户端的 IP 地址

/mnt/ShareFolder 192.168.43.131(rw,sync,no_subtree_check)

如果只容许多个客户端拜访,能够向如下一样写

/mnt/ShareFolder 192.168.43.131(rw,sync,no_subtree_check)
/mnt/ShareFolder 192.168.43.171(rw,sync,no_subtree_check)
/mnt/ShareFolder 192.168.43.137(rw,sync,no_subtree_check)

如果运行一个网段的客户端拜访,能够这样写:

/mnt/ShareFolder 192.168.43.*(rw,sync,no_subtree_check)
或者
/mnt/ShareFolder 192.168.43.0/24(rw,sync,no_subtree_check)
Ubuntu 20.04 中配置 NFS 服务 Ubuntu 20.04 中配置 NFS 服务
配置文件中的权限解释:

rw 容许读写
sync 文件同时写入硬盘和内存
no_subtree_check 即便输入目录是一个子目录,nfs 服务器也不查看其父目录的权限,这样能够提高效率
四、export 共享目录
应用上面命令将共享文件夹启用并失效:

bob@ubuntu-20-04:~$ sudo exportfs -arv
exporting 192.168.43.*:/mnt/ShareFolder
应用 showmount - e 查看是否能够看到共享目录:

bob@ubuntu-20-04:~$ showmount -e 192.168.43.174
Export list for 192.168.43.174:
/mnt/ShareFolder 192.168.43.*
Ubuntu 20.04 中配置 NFS 服务 Ubuntu 20.04 中配置 NFS 服务

五、配置客户端
装置 NFS 客户端:

在 Ubuntu20.04 中装置客户端:nfs-common

bob@ubuntu-20-04:~$ sudo apt install nfs-common

在 Centos8 中装置客户端:nfs-utils

[root@localhost ~]# yum -y install nfs-utils
而后在 /mnt 目录中创立一个 ClientFolder 目录,将从该目录中挂载服务器上的 NFS 共享目录。

[root@localhost ~]# mkdir -p /mnt/ClientFolder
最初,挂载 NFS 共享目录,如下所示:

[root@localhost ~]# mount 192.168.43.174:/mnt/ShareFolder /mnt/ClientFolder/
Ubuntu 20.04 中配置 NFS 服务 Ubuntu 20.04 中配置 NFS 服务

六、测试 NFS 共享目录
为了测试配置是否失常,咱们将在客户端 /mnt/ClientFolder 文件夹中创立文件测试一下,如下所示:

[root@localhost ClientFolder]# dd if=/dev/zero of=./test.dd bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.0540562 s, 194 MB/s
[root@localhost ClientFolder]# ll
total 10240
-rw-r–r– 1 nobody nobody 10485760 Apr 21 2021 test.dd
Ubuntu 20.04 中配置 NFS 服务 Ubuntu 20.04 中配置 NFS 服务
返回服务端,看一下 /mnt/ShareFolder 目录中是否能够看到该文件:
https://www.51cto.com/it/news…
https://server.51cto.com/arti…
https://www.donews.com/news/d…

bob@ubuntu-20-04:~$ ll /mnt/ShareFolder/
total 10248
drwxrwxrwx 2 nobody nogroup 4096 4 月 21 10:50 ./
drwxr-xr-x 4 root root 4096 4 月 21 10:24 ../
-rw-r–r– 1 nobody nogroup 10485760 4 月 21 10:50 test.dd
Ubuntu 20.04 中配置 NFS 服务 Ubuntu 20.04 中配置 NFS 服务

总结
NFS 是 Network File System 的首字母缩写。它是一种分布式协定,使客户端能够拜访近程服务器上的共享文件。

正文完
 0