Ubuntu16.04(及後續版本)上配置NFS分區
我們實驗室的一個常見的場景就是把用於機器學習的機器的HOME掛載到VNC伺服器的HOME上,好處自然不必多說了。下面Gemfield就來描述下如何配置NFS的服務端和客戶端。
1,在NFS Server端安裝nfs-kernel-server
gemfield@ai:~$ sudo apt install nfs-kernel-server[sudo] password for gemfield: Reading package lists... DoneBuilding dependency tree Reading state information... DoneThe following packages were automatically installed and are no longer required: libcapture-tiny-perl libconfig-simple-perl libdbd-pg-perl libdbd-sqlite3-perl libdbi-perl libfile-which-perl libnx-x11-6 libnx-xcomposite1 libnx-xdamage1 libnx-xdmcp6 libnx-xext6 libnx-xfixes3 libnx-xinerama1 libnx-xpm4 libnx-xrandr2 libnx-xrender1 libnx-xtst6 libpq5 libxcomp3 libxcompext3 libxcompshad3 linux-headers-4.10.0-19 linux-headers-4.10.0-19-generic linux-image-4.10.0-19-generic linux-image-extra-4.10.0-19-generic nx-x11-common nxagent pwgenUse sudo apt autoremove to remove them.The following additional packages will be installed: keyutils libnfsidmap2 libtirpc1 nfs-common rpcbindSuggested packages: open-iscsi watchdogThe following NEW packages will be installed: keyutils libnfsidmap2 libtirpc1 nfs-common nfs-kernel-server rpcbind0 upgraded, 6 newly installed, 0 to remove and 21 not upgraded.Need to get 466 kB of archives.After this operation, 1,820 kB of additional disk space will be used.Do you want to continue? [Y/n] yGet:1 http://cn.archive.ubuntu.com/ubuntu zesty/main amd64 keyutils amd64 1.5.9-9ubuntu1 [47.3 kB] Get:2 http://cn.archive.ubuntu.com/ubuntu zesty/main amd64 libnfsidmap2 amd64 0.25-5.1 [27.2 kB] Get:3 http://cn.archive.ubuntu.com/ubuntu zesty/main amd64 libtirpc1 amd64 0.2.5-1.1 [75.4 kB] Get:4 http://cn.archive.ubuntu.com/ubuntu zesty/main amd64 rpcbind amd64 0.2.3-0.5 [40.7 kB] Get:5 http://cn.archive.ubuntu.com/ubuntu zesty/main amd64 nfs-common amd64 1:1.2.8-9.2ubuntu2 [187 kB] Get:6 http://cn.archive.ubuntu.com/ubuntu zesty/main amd64 nfs-kernel-server amd64 1:1.2.8-9.2ubuntu2 [88.2 kB]Fetched 466 kB in 0s (1,040 kB/s) Selecting previously unselected package keyutils.(Reading database ... 250704 files and directories currently installed.)Preparing to unpack .../0-keyutils_1.5.9-9ubuntu1_amd64.deb ...Unpacking keyutils (1.5.9-9ubuntu1) ...Selecting previously unselected package libnfsidmap2:amd64.Preparing to unpack .../1-libnfsidmap2_0.25-5.1_amd64.deb ...Unpacking libnfsidmap2:amd64 (0.25-5.1) ...Selecting previously unselected package libtirpc1:amd64.Preparing to unpack .../2-libtirpc1_0.2.5-1.1_amd64.deb ...Unpacking libtirpc1:amd64 (0.2.5-1.1) ...Selecting previously unselected package rpcbind.Preparing to unpack .../3-rpcbind_0.2.3-0.5_amd64.deb ...Unpacking rpcbind (0.2.3-0.5) ...Selecting previously unselected package nfs-common.Preparing to unpack .../4-nfs-common_1%3a1.2.8-9.2ubuntu2_amd64.deb ...Unpacking nfs-common (1:1.2.8-9.2ubuntu2) ...Selecting previously unselected package nfs-kernel-server.Preparing to unpack .../5-nfs-kernel-server_1%3a1.2.8-9.2ubuntu2_amd64.deb ...Unpacking nfs-kernel-server (1:1.2.8-9.2ubuntu2) ...Setting up libnfsidmap2:amd64 (0.25-5.1) ...Processing triggers for ureadahead (0.100.0-19) ...Setting up keyutils (1.5.9-9ubuntu1) ...Processing triggers for libc-bin (2.24-9ubuntu2.2) ...Setting up libtirpc1:amd64 (0.2.5-1.1) ...Processing triggers for systemd (232-21ubuntu7.1) ...Processing triggers for man-db (2.7.6.1-2) ...Setting up rpcbind (0.2.3-0.5) ...Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /lib/systemd/system/rpcbind.service.Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /lib/systemd/system/rpcbind.socket.Setting up nfs-common (1:1.2.8-9.2ubuntu2) ...Creating config file /etc/idmapd.conf with new versionAdding system user `statd (UID 121) ...Adding new user `statd (UID 121) with group `nogroup ...Not creating home directory `/var/lib/nfs.Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.nfs-utils.service is a disabled or a static unit, not starting it.Setting up nfs-kernel-server (1:1.2.8-9.2ubuntu2) ...Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /lib/systemd/system/nfs-server.service.Creating config file /etc/exports with new versionCreating config file /etc/default/nfs-kernel-server with new versionProcessing triggers for libc-bin (2.24-9ubuntu2.2) ...Processing triggers for systemd (232-21ubuntu7.1) ...Processing triggers for ureadahead (0.100.0-19) ...
2, 在NFS客戶端安裝nfs-common
gemfield@gemfield-ThinkPad-Edge:~$ sudo apt install nfs-common[sudo] password for gemfield: 正在讀取軟體包列表... 完成正在分析軟體包的依賴關係樹 正在讀取狀態信息... 完成 將會同時安裝下列軟體: keyutils libevent-2.0-5 libnfsidmap2 libtirpc1 rpcbind建議安裝: open-iscsi watchdog下列【新】軟體包將被安裝: keyutils libevent-2.0-5 libnfsidmap2 libtirpc1 nfs-common rpcbind升級了 0 個軟體包,新安裝了 6 個軟體包,要卸載 0 個軟體包,有 0 個軟體包未被升級。需要下載 493 kB 的歸檔。解壓縮後會消耗 1,693 kB 的額外空間。您希望繼續執行嗎? [Y/n] y獲取:1 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 libnfsidmap2 amd64 0.25-5 [32.2 kB]獲取:2 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 libtirpc1 amd64 0.2.5-1 [75.6 kB]獲取:3 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 keyutils amd64 1.5.9-8ubuntu1 [47.1 kB]獲取:4 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libevent-2.0-5 amd64 2.0.21-stable-2ubuntu0.16.04.1 [114 kB]獲取:5 http://cn.archive.ubuntu.com/ubuntu xenial/main amd64 rpcbind amd64 0.2.3-0.2 [40.3 kB]獲取:6 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nfs-common amd64 1:1.2.8-9ubuntu12.1 [184 kB]已下載 493 kB,耗時 0秒 (1,218 kB/s)正在選中未選擇的軟體包 libnfsidmap2:amd64。(正在讀取資料庫 ... 系統當前共安裝有 205322 個文件和目錄。)正準備解包 .../libnfsidmap2_0.25-5_amd64.deb ...正在解包 libnfsidmap2:amd64 (0.25-5) ...正在選中未選擇的軟體包 libtirpc1:amd64。正準備解包 .../libtirpc1_0.2.5-1_amd64.deb ...正在解包 libtirpc1:amd64 (0.2.5-1) ...正在選中未選擇的軟體包 keyutils。正準備解包 .../keyutils_1.5.9-8ubuntu1_amd64.deb ...正在解包 keyutils (1.5.9-8ubuntu1) ...正在選中未選擇的軟體包 libevent-2.0-5:amd64。正準備解包 .../libevent-2.0-5_2.0.21-stable-2ubuntu0.16.04.1_amd64.deb ...正在解包 libevent-2.0-5:amd64 (2.0.21-stable-2ubuntu0.16.04.1) ...正在選中未選擇的軟體包 rpcbind。正準備解包 .../rpcbind_0.2.3-0.2_amd64.deb ...正在解包 rpcbind (0.2.3-0.2) ...正在選中未選擇的軟體包 nfs-common。正準備解包 .../nfs-common_1%3a1.2.8-9ubuntu12.1_amd64.deb ...正在解包 nfs-common (1:1.2.8-9ubuntu12.1) ...正在處理用於 man-db (2.7.5-1) 的觸發器 ...正在處理用於 libc-bin (2.23-0ubuntu9) 的觸發器 ...正在處理用於 systemd (229-4ubuntu21) 的觸發器 ...正在處理用於 ureadahead (0.100.0-19) 的觸發器 ...正在設置 libnfsidmap2:amd64 (0.25-5) ...正在設置 libtirpc1:amd64 (0.2.5-1) ...正在設置 keyutils (1.5.9-8ubuntu1) ...正在設置 libevent-2.0-5:amd64 (2.0.21-stable-2ubuntu0.16.04.1) ...正在設置 rpcbind (0.2.3-0.2) ...正在設置 nfs-common (1:1.2.8-9ubuntu12.1) ...Creating config file /etc/idmapd.conf with new versionCreating config file /etc/default/nfs-common with new versionAdding system user `statd (UID 122) ...Adding new user `statd (UID 122) with group `nogroup ...Not creating home directory `/var/lib/nfs.nfs-utils.service is a disabled or a static unit, not starting it.正在處理用於 libc-bin (2.23-0ubuntu9) 的觸發器 ...正在處理用於 systemd (229-4ubuntu21) 的觸發器 ...正在處理用於 ureadahead (0.100.0-19) 的觸發器 ...gemfield@gemfield-ThinkPad-Edge:~$
3, 在NFS Server上配置要export的目錄
格式要求為:共享目錄 client(選項,...,選項)
Gemfield按照這個格式添加了如下一行(可以看到,客戶端用*表示,可以允許任意客戶端掛載。而no_root_squash表示登入 NFS 主機使用分享目錄的使用者,如果是 root 的話,那麼對於這個分享的目錄來說,他就具有 root 的許可權!這樣有安全隱患,可以使用root_squash代替。Gemfield這裡為了某個目的就先不安全了......):
/home *(rw,sync,no_root_squash,no_subtree_check)
gemfield@ai:~$ cat /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)#/home *(rw,sync,no_root_squash,no_subtree_check)gemfield@ai:~$
4,重啟NFS Server服務
gemfield@ai:~$ sudo systemctl restart nfs-kernel-server
5,在NFS Client機器上掛載
gemfield@gemfield-ThinkPad-Edge:/home$ sudo mount ai.gemfield.org:/home /home [sudo] password for gemfield: gemfield@gemfield-ThinkPad-Edge:/home$
6,設置開機自動掛載
在/etc/fstab文件中添加如下一行掛載記錄:
ai.gemfield.org:/home /home nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
gemfield@gemfield-ThinkPad-Edge:~$ cat /etc/fstab # /etc/fstab: static file system information.## Use blkid to print the universally unique identifier for a# device; this may be used with UUID= as a more robust way to name devices# that works even if disks are added and removed. See fstab(5).## <file system> <mount point> <type> <options> <dump> <pass># / was on /dev/sda1 during installationUUID=0fb0bb42-b7d0-4d7b-a200-37d18da2f172 / ext4 errors=remount-ro 0 1# swap was on /dev/sda5 during installationUUID=e14f4600-7216-429b-8583-b63ead400b3a none swap sw 0 0ai.gemfield.org:/home /home nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0gemfield@gemfield-ThinkPad-Edge:~$
7,遺漏了什麼?
看起來將NFS Server的Home目錄掛載到客戶端機器上就這麼簡單就完成了,不過,你必須要確保兩邊機器上相同的用戶必須擁有相同的uid和gid,否則許可權問題會導致前面想要表達的東西化為泡影。
8,錦上添花
在/etc/bash.bashrc文件的最後追加下面兩行配置,這樣就可以使用F1快捷鍵登陸ai機器了。
echo "Press F1 to login ai.gemfield.org..."bind "eOP":"ssh -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null ai.gemfield.org
"
推薦閱讀: