如何在 Ubuntu 16.04 上设置和配置 NFS
在这篇文章中,我们将学习如何在 Ubuntu 16.04 上安装 NFS。网络文件系统 (NFS) 协议和文件系统允许您访问远程系统或服务器上的共享文件夹,并允许您将其作为远程目录挂载到服务器上。这允许您在不同位置的不同客户端之间共享存储空间。NFS 一直以来都是访问网络上远程存储的最简单方法。
为了完成此演示,我们需要两台安装了 Ubuntu 系统的电脑,用户拥有 sudo 权限并处于私有网络中。
在服务器上安装软件包
我们将安装“nfs-kernel-server”,这将允许我们共享服务器上的目录来共享文件和文件夹。以下是安装 nfs 软件包的命令。
$ sudo apt update $ sudo apt install nfs-kernel-server Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: keyutils libnfsidmap2 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libtirpc1 nfs-common python python-minimal python2.7 python2.7-minimal rpcbind Suggested packages: watchdog python-doc python-tk python2.7-doc binutils binfmt-support The following NEW packages will be installed: keyutils libnfsidmap2 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libtirpc1 nfs-common nfs-kernel-server python python-minimal python2.7 python2.7-minimal rpcbind 0 upgraded, 13 newly installed, 0 to remove and 98 not upgraded. Need to get 4,375 kB of archives. After this operation, 18.5 MB of additional disk s Selecting previously unselected package libnfsidmap2:amd64. (Reading database ... 56995 files and directories currently installed.) .. … … currently installed.) Processing triggers for ureadahead (0.100.0-19) ... Processing triggers for systemd (229-4ubuntu4) ... Setting up libnfsidmap2:amd64 (0.25-5) ... Setting up libpython2.7-stdlib:amd64 (2.7.12-1~16.04) ... Setting up python2.7 (2.7.12-1~16.04) ... Setting up libpython-stdlib:amd64 (2.7.11-1) ... Setting up python (2.7.11-1) ... Setting up libtirpc1:amd64 (0.2.5-1) ... Setting up keyutils (1.5.9-8ubuntu1) ... Setting up rpcbind (0.2.3-0.2) ... Setting up nfs-common (1:1.2.8-9ubuntu12) ... Creating config file /etc/idmapd.conf with new version Progress: [ 98%] [##########################################################] Adding system user `statd' (UID 110) ... Adding new user `statd' (UID 110) with group `nogroup' ... Not creating home directory `/var/lib/nfs'. nfs-utils.service is a disabled or a static unit, not starting it. Setting up nfs-kernel-server (1:1.2.8-9ubuntu12) ... Creating config file /etc/exports with new version Creating config file /etc/default/nfs-kernel-server with new version Processing triggers for libc-bin (2.23-0ubuntu3) ... Processing triggers for ureadahead (0.100.0-19) ...########################.] Processing triggers for systemd (229-4ubuntu4) ...
在客户端安装软件包
我们必须在客户端安装 nfs 软件包,通常是 nfs-common,即提供对服务器上 NFS 共享文件夹访问权限的软件包。
$ sudo apt update $ sudo apt install nfs-common apt install nfs-common Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: watchdog The following NEW packages will be installed: nfs-common 0 upgraded, 1 newly installed, 0 to remove and 98 not upgraded. Need to get 185 kB of archives. After this operation, 734 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 nfs-common amd64 1:1 .2.8-9ubuntu12 [185 kB] Fetched 185 kB in 1s (126 kB/s) Selecting previously unselected package nfs-common. (Reading database ... 57864 files and directories currently installed.) Preparing to unpack .../nfs-common_1%3a1.2.8-9ubuntu12_amd64.deb ... Unpacking nfs-common (1:1.2.8-9ubuntu12) ... Processing triggers for ureadahead (0.100.0-19) ... Processing triggers for systemd (229-4ubuntu4) ... Setting up nfs-common (1:1.2.8-9ubuntu12) ...
在服务器上启用和创建共享目录
在此演示中,我们将共享两个文件夹以区分配置设置,第一个具有超级用户权限,另一个具有客户端系统上受信任用户的权限。
导出常规挂载点
在这个例子中,我们将创建一个具有默认配置的常规 NFS 挂载点,即使客户端机器上没有任何权限的用户也很难访问,这可以用来创建一个共享空间来存储文件夹中的项目文件。
创建用于一般用途的共享文件夹
$ sudo mkdir /usr/nfs/common –p
更改文件夹权限,以便任何人都可以写入文件夹。
$ sudo chown nobody:nogroup /usr/nfs/common
现在尝试使用以下命令从客户端访问文件夹。
在我们挂载客户端上的任何共享文件夹之前,我们需要在客户端机器上创建一个挂载点。
$ sudo mkdir /mnt/nfs/common $ sudo mount 192.168.1.25:/usr/nfs/common /mnt/nfs/common
这将在客户端机器上将服务器 192.168.1.25 上的 NFS 共享挂载到 /mnt/nfs/common,它可以在客户端机器上作为本地文件夹访问。
创建具有主目录的共享文件夹
我们将共享存储在服务器上的用户主目录,以便从客户端访问,方便管理用户。
在服务器上配置 NFS 设置
由于我们看到了两种类型的 NFS 共享,让我们看看如何配置设置以满足我们的需求。
使用编辑器打开 /etc/exports 文件。
$ sudo vi /etc/exports # /etc/exports: the access control list for filesystems which may be exported # to NFS clients. See exports(5). # # Example for NFSv2 and NFSv3: # /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check) # # Example for NFSv4: # /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check) # /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
将以下几行添加到配置文件中:
/usr/nfs/common 192.168.1.100(rw,sync,no_subtree_check) /home 192.168.1.100(rw,sync,no_root_squash,no_subtree_check)
以下是我们在上面使用的命令中使用的每个选项的解释。
rw -> 这将允许客户端计算机读取和写入共享。
sync -> 这将允许在将数据应用于查询之前将其写入 NFS。它还增强了一致的环境并将更稳定。
nosubtreecheck -> 这将阻止子树检查,如果启用此选项,如果客户端已打开文件,则会导致许多问题。
norootsquash -> 这将使来自客户端 root 用户的 NFS 转换请求转换为服务器的非特权用户,这还将阻止客户端上的 root 帐户以 root 身份使用服务器的文件系统。
$ sudo systemctl restart nfs-kernel-server
在客户端挂载目录
在我们挂载客户端上的共享文件夹之前,我们需要创建一个挂载点,并将 NFS 服务器上的共享文件夹链接到本地文件夹(挂载点)。
$ mkdir /mnt/common $ mkdir /mnt/home $ sudo mount 192.168.1.100:/usr/nfs/common /mnt/common $ sudo mount 192.168.1.100:/home /mnt/home
运行命令后,我们将验证 NFS 共享文件夹是否已正确挂载。
$ df –h Filesystem Size Used Avail Use% Mounted on udev 538M 0 538M 0% /dev tmpfs 249M 628K 249M 2% /run /dev/vda1 100G 10G 90G 10% / tmpfs 445M 0 445M 0% /dev/shm tmpfs 10.0M 0 10.0M 0% /run/lock tmpfs 245M 0 245M 0% /sys/fs/cgroup tmpfs 249M 0 249M 0% /run/user/0 192.168.1.100:/home 124G 11.28G 118.8G 9% /mnt/home 192.168.1.100:/usr/nfs/common 124G 11.28G 118.8G 9% /mnt/common
我们可以看到这两个共享都已挂载,我们可以在底部看到它们,因为它们是从同一服务器挂载的,所以我们可以看到相同的磁盘使用情况。
在启动时挂载 NFS 共享
我们可以在启动时挂载 NFS 共享,以便如果我们需要连接 NFS 共享文件夹,可以直接访问挂载点上的文件夹。
打开 /etc/fstab 文件并添加以下几行。
$ sudo vi /etc/fstab
将以下行添加到文件的底部。
. . . 192.168.1.100:/usr/nfs/common /mnt/general nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0 192.168.1.100:/home /mnt/home nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
卸载 NFS 共享文件夹
如果我们不想使用这些文件夹,可以使用以下命令卸载 NFS 共享文件夹。
$ sudo umount /mnt/common $ sudo umount /mnt/home
在上面的文章中,我们设置并在 Ubuntu 16.04 上配置了 NFS 共享,其中包含两个不同的 NFS 挂载点,一个共享允许任何人读取或写入文件夹,另一个则对用户进行了限制。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP