檔案伺服器NFS配置詳解 在Linux下部署小型檔案伺服器往往採用這麼一個架構方式:一台檔案伺服器+多台web伺服器,檔案伺服器提供目錄被web伺服器同時訪問和寫入,可以採用apache,web伺服器可以採用tomcat之類的,採用的是NFS網路檔案系統共用方式。 這裡應紅紅(^_^)要求寫出具體操作過程。環境:IP 主機名稱 用途192.168.56.151 rhel151 檔案伺服器192.168.56.152 rhel152 web伺服器 0. 首先當然是部署設定檔伺服器。檢查是否安裝相關的軟體包:[root@rhel151 ~]# rpm -qa | grep nfssystem-config-nfs-1.3.23-2.el5nfs-utils-1.0.9-66.el5nfs-utils-lib-1.0.8-7.9.el5上述3個就是NFS系統的軟體包了。接下來建立一個準備用於共用訪問的目錄/opt/nfsshare:[root@rhel151 ~]# mkdir -p /opt/nfsshare然後將共用目錄資訊以及共用開放的許可權寫入設定檔:[root@rhel151 ~]# echo "/opt/nfsshare 192.168.56.*(rw,no_root_squash)">/etc/exports[root@rhel151 ~]# cat /etc/exports/opt/nfsshare 192.168.56.*(rw,no_root_squash)上述配置表示:共用目錄為/opt/nfsshare,可以被192.168.56網段的主機訪問,許可權是讀寫(rw),對應root使用者權限設定(no_root_squash),具體的參數說明可以參考NFS手冊,這裡配置的是最常用的參數,也足夠用了。接下來重啟檔案伺服器的NFS相關服務:[root@rhel151 ~]# service portmap restart停止 portmap: [確定]啟動 portmap: [確定][root@rhel151 ~]# service nfs restart關閉 NFS mountd: [失敗]關閉 NFS 守護進程: [失敗]關閉 NFS quotas: [失敗]啟動 NFS 服務: [確定]關掉 NFS 配額: [確定]啟動 NFS 守護進程: [確定]啟動 NFS mountd: [確定]Stopping RPC idmapd: [確定]正在啟動 RPC idmapd: [確定]設定相關服務在開機啟動的時候自動啟動:[root@rhel151 ~]# chkconfig portmap on[root@rhel151 ~]# chkconfig nfs on 1. 配置用戶端訪問用戶端訪問也需要啟動相關的訪問服務:[root@rhel152 ~]# service portmap restart停止 portmap: [確定]啟動 portmap: [確定]設定訪問服務進程開機自動啟動:[root@rhel152 ~]# chkconfig portmap on建立一個擁有把檔案伺服器共用目錄掛載到本地的目錄/opt/nfsshare,你也可以不使用這個目錄,這個目錄路徑沒有必要和檔案伺服器的一致:[root@rhel152 ~]# mkdir -p /opt/nfsshare通過mount命令指定掛載nfs檔案伺服器的目錄/opt/nfsshare到本地的/opt/nfsshare目錄:[root@rhel152 ~]# mount -t nfs 192.168.56.151:/opt/nfsshare /opt/nfssharemount: 192.168.56.151:/opt/nfsshare failed, reason given by server: Permission denied哦哦~~報錯了,原因是檔案伺服器rhel151配置了主機名稱和IP地址的映射,需要把對應用戶端的映射注釋掉:[root@rhel151 ~]# cat /etc/hosts # Do not remove the following line, or various programs# that require network functionality will fail.127.0.0.1 rhel151 localhost.localdomain localhost::1 localhost6.localdomain6 localhost6192.168.56.1 m192.168.56.151 rhel151#192.168.56.152 rhel152192.168.56.153 rhel153192.168.56.154 rhel154192.168.56.161 gpm192.168.56.163 gpms192.168.56.165 gps1192.168.56.167 gps2如上,把對應用戶端rhel152的映射注釋掉:#192.168.56.152 rhel152,記住,這個操作是在rhel151檔案伺服器上操作的。返回rhel152用戶端繼續操作:[root@rhel152 ~]# mount -t nfs rhel151:/opt/nfsshare /opt/nfsshare好了,沒有任何錯誤,查看下掛載結果:[root@rhel152 ~]# mount/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)/dev/sda1 on /boot type ext3 (rw)tmpfs on /dev/shm type tmpfs (rw)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)/dev/sdb1 on /oradata type ext3 (rw)rhel151:/opt/nfsshare on /opt/nfsshare type nfs (rw,addr=192.168.56.151)很好,最後一行就是我們要的結果,通過df命令查看也可以看到結果:[root@rhel152 ~]# df -h檔案系統 容量 已用 可用 已用% 掛載點/dev/mapper/VolGroup00-LogVol00 14G 5.2G 7.8G 41% //dev/sda1 99M 13M 82M 14% /boottmpfs 1006M 0 1006M 0% /dev/shm/dev/sdb1 99G 2.0G 92G 3% /oradatarhel151:/opt/nfsshare 14G 7.6G 5.5G 59% /opt/nfsshare然後我們在rhel152寫入一個檔案看看: [root@rhel152 ~]# cd /opt/nfsshare/[root@rhel152 nfsshare]# touch 111[root@rhel152 nfsshare]# echo 'asdfasdf'>111 [root@rhel152 nfsshare]# cat 111asdfasdf已經寫入了一個檔案1111,返回rhel151服務端看看是不是已經有結果了:[root@rhel151 ~]# cat /opt/nfsshare/111 asdfasdfok,一切正常。最後,同理如果你還有rhel153、rhel153...一大堆的伺服器需要訪問,只需重複上面的rhel152的操作就可以了。檔案伺服器提供集中的訪問目錄,因為我們不太可能在每台web伺服器上都提供大容量的儲存空間,最總要的一個原因是同步資料是非常麻煩的事情,現在採用這樣服務端-用戶端的方式就可以保證資料的讀取只存放在一個地方,不存在需要同步資料的問題,而且避免了web伺服器的單點故障。-EOF-