centos nfs server和client架設實作

centos nfs server和client架設實作

環境設定
centos 6.6
server 192.168.1.216
client 192.168.1.215

nfs server 安裝
# yum install nfs-utils

# mkdir /tmp/test
# chmod 777  /tmp/test

設定nfs

# vi /etc/exports
/tmp/test         192.168.1.215(rw,sync) *(all_squash,sync)
權限設定參考如下

參數值內容說明
rw
ro
該目錄分享的權限是可讀寫 (read-write) 或唯讀 (read-only),但最終能不能讀寫,還是與檔案系統的 rwx 及身份有關。
sync
async
sync 代表資料會同步寫入到記憶體與硬碟中,async 則代表資料會先暫存於記憶體當中,而非直接寫入硬碟!
no_root_squash
root_squash
用戶端使用 NFS 檔案系統的帳號若為 root 時,系統該如何判斷這個帳號的身份?預設的情況下,用戶端 root 的身份會由 root_squash 的設定壓縮成 nfsnobody, 如此對伺服器的系統會較有保障。但如果你想要開放用戶端使用 root 身份來操作伺服器的檔案系統,那麼這裡就得要開 no_root_squash 才行!
all_squash不論登入 NFS 的使用者身份為何, 他的身份都會被壓縮成為匿名使用者,通常也就是 nobody(nfsnobody) 啦!
anonuid
anongid
anon 意指 anonymous (匿名者) 前面關於 *_squash 提到的匿名使用者的 UID 設定值,通常為 nobody(nfsnobody),但是你可以自行設定這個 UID 的值!當然,這個 UID 必需要存在於你的 /etc/passwd 當中! anonuid 指的是 UID 而 anongid 則是群組的 GID 囉。
資料來源:鳥哥的Linux 私房菜

# service rpcbind start
# service nfs start
# chkconfig rpcbind on
# chkconfig nfs on
# chkconfig nfslock on
# service iptables stop 因為只是測試所以先關閉防火牆
# showmount -e 127.0.0.1
Export list for 127.0.0.1:
/tmp/test 192.168.1.215

補上iptables設定
防火牆的設定問題與解決方案:

一般來說, NFS 的服務僅會對內部網域開放,不會對網際網路開放的。然而,如果你有特殊需求的話, 那麼也可能會跨不同網域就是了。但是,NFS 的防火牆特別難搞,為什麼呢?因為除了固定的 port 111, 2049 之外, 還有很多不固定的埠口是由 rpc.mountd, rpc.rquotad 等服務所開啟的,所以,你的 iptables 就很難設定規則! 那怎辦?難道整個防火牆機制都要取消才可以?

為了解決這個問題, CentOS 6.x 有提供一個固定特定 NFS 服務的埠口設定檔,那就是 /etc/sysconfig/nfs 啦! 你在這個檔案裡面就能夠指定特定的埠口,這樣每次啟動 nfs 時,相關服務啟動的埠口就會固定,如此一來, 我們就能夠設定正確的防火牆囉!這個設定檔內容很多,絕大部分的資料你都不要去更改,只要改跟 PORT 這個關鍵字有關的資料即可。 那麼需要更改的 rpc 服務有哪些呢?主要有 mountd, rquotad, nlockmgr 這三個,所以你應該要這樣改:

[root@www ~]# vim /etc/sysconfig/nfs
RQUOTAD_PORT=1001   <==約在 13 行左右
LOCKD_TCPPORT=30001 <==約在 21 行左右
LOCKD_UDPPORT=30001 <==約在 23 行左右
MOUNTD_PORT=1002    <==約在 41 行左右
# 記得設定值最左邊的註解服務要拿掉之外,埠口的值你也可以自行決定。

[root@www ~]# /etc/init.d/nfs restart
[root@www ~]# rpcinfo -p | grep -E '(rquota|mount|nlock)'
    100011    2   udp   1001  rquotad
    100011    2   tcp   1001  rquotad
    100021    4   udp  30001  nlockmgr
    100021    4   tcp  30001  nlockmgr
    100005    3   udp   1002  mountd
    100005    3   tcp   1002  mountd
部份資料來源參考鳥哥網站
[root@www ~]# vi /etc/sysconfig/iptables
-A INPUT -m tcp -p tcp --dport 111 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m tcp -p tcp --dport 2049 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m tcp -p tcp --dport 1001 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m tcp -p tcp --dport 1002 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m tcp -p tcp --dport 30001 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m udp -p udp --dport 111 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m udp -p udp --dport 2049 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m udp -p udp --dport 1001 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m udp -p udp --dport 1002 -s xxx.xxx.xxx.xxx -j ACCEPT
-A INPUT -m udp -p udp --dport 30001 -s xxx.xxx.xxx.xxx -j ACCEPT

nfs client設定
請先開啟兩個服務避免造成錯誤
[root@eeclass mnt]# vi /etc/exports
[root@eeclass mnt]# /etc/init.d/rpcbind start
正在啟動 rpcbind:                                         [  確定  ]
[root@eeclass mnt]# /etc/init.d/nfslock start
正在啟動 NFS statd:                                       [  確定  ]
*******************************************************************************

錯誤訊息可能會是這樣
mount.nfs: rpc.statd is not running but is required for remote locking.
mount.nfs: Either use '-o nolock' to keep locks local, or start statd.
mount.nfs: an incorrect mount option was specified
*******************************************************************************


# mkdir /tmp/test
# showmount -e 192.168.1.216
Export list for 192.168.1.216:
/tmp/test 192.168.1.215
# mount -t nfs 192.168.1.216:/tmp/test /tmp/test
*** 若要取消掛載 umount -f  192.168.1.216:/tmp/test ***
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      6.5G  4.8G  1.5G  77% /
tmpfs                 499M     0  499M   0% /dev/shm
/dev/sda1             477M   48M  405M  11% /boot
192.168.1.216:/tmp/test
                      6.5G  6.5G     0 100% /tmp/test
# vi /etc/fstab
192.168.1.216:/tmp/test /tmp/test               nfs     defaults        0 0

:wq 存檔離開
# mount -a
# reboot
# df -h 再次檢查是否正確掛載
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      6.5G  4.8G  1.5G  77% /
tmpfs                 499M     0  499M   0% /dev/shm
/dev/sda1             477M   48M  405M  11% /boot
192.168.1.216:/tmp/test
                      6.5G  6.5G     0 100% /tmp/test
完成!!

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json