# NFS NFS是一個設定不當就很容易出毛病的東西,一但出毛病就會在關機時卡住或程式卡住,因此不可不慎 https://linux.vbird.org/linux_server/centos6/0330nfs.php ## NFS簡介 NFS為 Network FileSystem 的簡稱,它的目的就是想讓不同的機器、不同的作業系統可以彼此分享個別的檔案 > https://linux.vbird.org/linux_server/centos6/0330nfs.php ## 安裝NFS Server ```bash= sudo apt update sudo apt install nfs-kernel-server nfs-common ``` ## /etc/exports 介紹 `/etc/exports`的檔案格式大概會長這樣: ```bash= /foo 192.0.2.0/24(rw) {檔案路徑} 主機,網段..(各類參數設定) ``` 以下是官網提供的範例: ```bash= # sample /etc/exports file / master(rw) trusty(rw,no_root_squash) /projects proj*.local.domain(rw) /usr *.local.domain(ro) @trusted(rw) /home/joe pc001(rw,all_squash,anonuid=150,anongid=100) /pub *(ro,insecure,all_squash) /srv/www -sync,rw server @trusted @external(ro) /foo 2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw) /build buildhost[0-9].local.domain(rw) ``` ### Machine Name Formats * single host: 單一的host(ipv4和ipv6皆可) * Ip networks: 一個網段,例如: `/255.255.252.0`, `/22`,`192.0.2.0/24` * wildcards: 可以使用`*`,`*.cs.foo.edu` 代表`cs.foo.edu`domain的所有host可以使用 * netgroups: NIS netgroups may be given as @group * anonymous: `*`代表任何人都可以連線 ### exports參數介紹 **General Options:** * ro:目錄唯讀 * rw:這個選項允許NFS客戶機進行讀/寫訪問 * secure:使用1024以下的TCP/IP連接埠實現NFS的連接。指定insecure可以停用這個選型。 * async:此選項允許 NFS Server 違反 NFS protocol,允許檔案尚未存回磁碟之前回覆請求。這個選項可以提高性能,但是有可能會讓 server 崩潰,可能會需要重新啟動 server,並有機會造成資料遺失。 * sync:只會儲存檔案會磁碟之後才會回覆請求。 * no_wdelay:這個選項關閉寫入延遲。如果設定了async,那麼NFS就會忽略這個選項。 * no_subtree_check:禁用子樹檢查,會有些微的不安全,但在某些情況下可以提高可靠性。 * nohide:如果將一個目錄掛載到另一個目錄上,那麼原來的目錄通常會被隱藏起來或看起來像空的一樣。要停用這種行為,需要啟動hide選項。 * root_squash:這個選項不允許root使用者存取掛載上來的NFS磁碟區。 * no_root_squash:這個選項允許root使用者存取掛載上來的NFS磁碟區. **User ID Mapping 參數:** * root_squash:這個選項不允許root使用者存取掛載上來的NFS磁碟區(將uid0的使用者映射到nobody匿名使用者) * no_root_squash:這個選項可以使用root 身份來控制 NFS Server 的檔案。 * all_squash:所有登入 NFS 的使用者身份都會被壓縮成為 nobody。 * anonuid and anongid:調整匿名使用者的權限,可以讓所有透過 NFS 修改文件,都看起來是同一個使用者。 > [官方手冊](https://manpages.ubuntu.com/manpages/xenial/man5/exports.5.html?ref=blog.devcloud.com.tw) ## /etc/exports 設定 編輯`etc/exports`檔案 ```c= vim etc/exports ``` 檔案內容大致如下: ```bash= /opt 192.168.85.0/24(rw,no_wdelay,no_subtree_check,nohide,no_root_squash,all_squash,anonuid=777,anongid=777) ``` * `/opt` 為要被掛載的目錄 * `192.168.85.0/24` 允許 192.168.85.[0~255] 的網段中的使用該目錄進行掛載 (NFS Client) * (rw,no_wdelay,no_subtree_check,nohide,no_root_squash,all_squash,anonuid=777,anongid=777) 用了上述的參數後,可以盡可能的提高效率,而且會賦予每個使用者777的權限 設定完`/etc/exports`後,重啟nfs服務 ```c= sudo systemctl restart nfs-kernel-server.service ``` ## NFS Server設定 確認nfs server是否正常啟動: ```bash= sudo systemctl status nfs-kernel-server.service ``` ![image.png](https://hackmd.io/_uploads/Syokq_IQa.png) 可以使用`showmount`檢查nfs server開放的目錄: ```bash= showmount -e localhost ``` ![image.png](https://hackmd.io/_uploads/Sk3L9_Lma.png) ## NFS Client 安裝nfs client ```bash= sudo apt install nfs-common ``` 使用`showmount`檢查NFS Server可連線的目錄 ```bash= showmount -e {NFS Server IP} ``` ![image.png](https://hackmd.io/_uploads/S1NWju8Qa.png) 建立掛載用目錄,並將NFS Server的目錄掛載至本機目錄 ```bash= sudo mkdir opt sudo mount 192.168.85.129:/opt /opt ``` 確認是否有掛載成功 ```bash= df -h #or mount ``` ![image.png](https://hackmd.io/_uploads/rkKFQKImT.png) 在客戶端創建一個檔案: ![image.png](https://hackmd.io/_uploads/ByYSz5L7a.png) 在server端這裡也可以看到 ![image.png](https://hackmd.io/_uploads/HysUM5UX6.png) 也可以在server端使用showmount查看連線的使用者 ```c= showmount ``` > 參考資料: > https://blog.csdn.net/m0_49962289/article/details/108433804 > https://blog.devcloud.com.tw/ubuntu-nfs-install/ > https://officeguide.cc/linux-nfs-server-client-installation-configuration-tutorial-examples/ ## Debug ### NFS server 讓 NFS 伺服器重新載入設定: ```bash= sudo exportfs -ar ``` 顯示目前 NFS 伺服器的 export 設定 ```bash= sudo exportfs -v ``` ### NFS Client 如果nfs-common的狀態為masked ```bash= rm /lib/systemd/system/nfs-common.service # 刪除原本的 unit-file systemctl daemon-reload # 讓 Systemd 重新載入背景服務 (產生新的 unit-file) ``` > https://stackoverflow.com/questions/66473861/unit-nfs-common-service-is-masked