Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

CentOS 架設 NFS 並讓 MAC、FreeBSD 主機掛載使用

一、環境

伺服器端 CentOS 192.168.1.225,客戶端 CentOS 192.168.1.226。

二、安裝

CentOS 5 需要 nfs-utils 與 portmap:

yum -y install nfs-utils portmap

CentOS 6 起 portmap 已由 rpcbind 取代:

yum -y install nfs-utils rpcbind

三、伺服器端設定

建立共享目錄並編輯 /etc/exports:

[root@centos2 /]# mkdir /usr/local/test
[root@centos2 /]# vi /etc/exports
/usr/local/test/ 192.168.1.226(rw,no_root_squash,no_all_squash,sync)

讓設定生效:

[root@centos2 /]# exportfs -r

exports 的每一行由「共享目錄」與「客戶端(權限)」兩部分組成,中間不能有空格。客戶端可以是單一 IP、整個網段或支援萬用字元的網域,例如 *.youxia.com。常用權限說明:

  • rw/ro:可讀寫或唯讀。
  • sync:資料同時寫入記憶體與硬碟;async:先暫存記憶體,效能較好但斷電有風險。
  • no_root_squash:客戶端以 root 連線時,對共享目錄也擁有 root 權限,安全性上並不建議開啟。
  • root_squash:客戶端 root 被映射為匿名帳號,通常為 nobody 或 nfsnobody。
  • all_squash:不論客戶端用什麼帳號,一律映射為匿名帳號。
  • anonuid/anongid:指定匿名使用者的 UID 與 GID。

啟動服務,CentOS 6:

[root@centos2 /]# service rpcbind start
[root@centos2 /]# service nfs start
Starting NFS services: [ OK ]
Starting NFS mountd: [ OK ]
Starting RPC idmapd: [ OK ]
Starting NFS daemon: [ OK ]

CentOS 5 則是先啟動 portmap 再啟動 nfs。

四、客戶端掛載

建立掛載點並先用 showmount 確認伺服器有沒有輸出共享:

[root@localhost ~]# mkdir /usr/local/test
[root@localhost ~]# showmount -e 192.168.1.225
Export list for 192.168.1.225:
/usr/local/test 192.168.1.226

若看到 rpc mount export: RPC: Unable to receive; errno = No route to host,通常是伺服器端防火牆沒放行,需要處理。

正式掛載:

[root@localhost ~]# mount -t nfs 192.168.1.225:/usr/local/test /usr/local/test

用 mount 指令確認:

192.168.1.225:/usr/local/test on /usr/local/test type nfs
(rw,vers=4,addr=192.168.1.225,clientaddr=192.168.1.226)

看到這行就代表掛載成功。

五、驗證

客戶端建立檔案:

[root@centos2 test]# echo "hello nfs test" >> test
[root@centos2 test]# ll
-rw-r--r-- 1 root root 15 Apr 9 13:24 test

回到伺服器端查看同一目錄,檔案已經在那裡,代表雙向共用完成。

補充一點:MAC 與 FreeBSD 同樣支援 NFS 客戶端。macOS 可在「前往」選單的「連線伺服器」輸入 nfs://192.168.1.225/usr/local/test;FreeBSD 則直接用 mount_nfs 或 mount -t nfs 掛載,注意把伺服器 exports 中的客戶端範圍放進去即可。

Copyright © Linux教程網 All Rights Reserved