Jex’s Note

NFS (Network FileSystem)

分享目錄的 server 安裝及設定

server 安裝:

sudo apt-get update
sudo apt-get install nfs-kernel-server

設定 /etc/exports :

建議直接設定*, 在防火牆再限 ip, 不然因為 nfs port 是浮動的不好設定

/my_ugc *(rw,sync,all_squash,anonuid=1000,anongid=1000,insecure,subtree_check)
/home/me/test *(ro)
  • * : 代表任意主機
  • ro : 唯讀,是預設值
  • rw : 允許讀、寫
  • sync : 預設值。同步 I/O,也就是在資料異動時,會同步寫入記憶體與磁碟之中
  • all_squash : 所有client 端的user 都對應成 nobody(一般設定)
  • anonuid : 為 nobody 帳號設 uid
  • anongid : 為 nobody 帳號設 gid
  • insecure : 允許主機不需認證即可存取
  • subtree_check (或 no_subtree_check, 不加上會有 error) : If a subdirectory of a filesystem is exported, but the whole filesystem isn’t then whenever a NFS request arrives, the server must check not only that the accessed file is in the appropriate filesystem (which is easy) but also that it is in the exported tree (which is harder)
  • 更多官方文件參數說明


啟動 server

sudo service nfs-kernel-server start


$ showmount -e localhost
Export list for localhost:
/my_ugc/cms-me/doc *

-e or –exports : Show the NFS server’s export list.

開啟 nfs port

-A INPUT -p tcp -s   -m state --state NEW --dport 111  -j ACCEP    // nfs
-A INPUT -p tcp -s   -m state --state NEW --dport 2049  -j ACCEP   // nfs

測試 port 是否如預期

$ rpcinfo -p
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   udp    111  portmapper
    100003    2   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    2   tcp   2049
    100227    3   tcp   2049
    100021    1   udp  51575  nlockmgr
    100021    3   udp  51575  nlockmgr
    100021    4   tcp  55701  nlockmgr
    100005    1   udp  35019  mountd
    100005    1   tcp  37799  mountd

server 開機自動 mount, /etc/fstab:

/dev/xvdb /my_ugc ext4  defaults       0 0

client 掛載遠端目錄

client 安裝:

sudo apt-get install nfs-common

mount :

sudo mount devm3:/my_ugc /my_ugc

df 查看 :

devm3:/my_ugc 236058624  48626176 175434752  22% /my_ugc

client 開機自動掛載, /etc/fstab:

base:/my_ugc /my_ugc nfs rw,soft,nosuid,noexec,nodev,bg,rsize=32768,wsize=32768     0 0
  • timeo : specifies the number of seconds to pass before the error is reported.
  • intr : Allows NFS requests to be interrupted if the server goes down or cannot be reached.
  • rsize=num and wsize=num — These settings speed up NFS communication for reads (rsize) and writes (wsize) by setting a larger data block size, in bytes, to be transferred at one time. Be careful when changing these values; some older Linux kernels and network cards do not work well with larger block sizes. For NFSv2 or NFSv3, the default values for both parameters is set to 8192. For NFSv4, the default values for both parameters is set to 32768.

ref : 固定 nfs server port