如何在 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 掛載點,一個共享允許任何人讀取或寫入資料夾,另一個則對使用者進行了限制。

更新於:2020年1月23日

828 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.