安裝 rpcbind 和 nfs-utils
1 yum install rpcbind 2 yum install nfs-utils
設置共享目錄
[root@bogon ~]# vim /etc/exports #修改/etc/exports文件,輸出共享目錄 添加一行, 例如 /mnt/share *(rw,sync,no_root_squash)
或
/mnt/share 192.168.1.0/24(ro,async) 192.168.0.0/24(rw,sync)
[root@localhost ~]# more /etc/exports
/home/tomcat/archive/ 192.168.1.0/24(ro,async)
/etc/exports文件內容格式:
<輸出目錄> [客戶端1 選項(訪問權限,用戶映射,其他)] [客戶端2 選項(訪問權限,用戶映射,其他)] a. 輸出目錄: 輸出目錄是指NFS系統中需要共享給客戶機使用的目錄; b. 客戶端: 客戶端是指網絡中可以訪問這個NFS輸出目錄的計算機 客戶端常用的指定方式 指定ip地址的主機:192.168.0.200 指定子網中的所有主機:192.168.0.0/24 192.168.0.0/255.255.255.0 指定域名的主機:david.bsmart.cn 指定域中的所有主機:*.bsmart.cn 所有主機:* c. 選項: 選項用來設置輸出目錄的訪問權限、用戶映射等。NFS主要有3類選項: 訪問權限 設置輸出目錄只讀:ro 設置輸出目錄讀寫:rw 用戶映射 all_squash:將遠程訪問的所有普通用戶及所屬組都映射為匿名用戶或用戶組(nfsnobody); no_all_squash:與all_squash取反(默認設置); root_squash:將root用戶及所屬組都映射為匿名用戶或用戶組(默認設置); no_root_squash:與rootsquash取反; anonuid=xxx:將遠程訪問的所有用戶都映射為匿名用戶,並指定該用戶為本地用戶(UID=xxx); anongid=xxx:將遠程訪問的所有用戶組都映射為匿名用戶組賬戶,並指定該匿名用戶組賬戶為本地用戶組賬戶(GID=xxx); 其它選項 secure:限制客戶端只能從小於1024的tcp/ip端口連接nfs服務器(默認設置); insecure:允許客戶端從大於1024的tcp/ip端口連接服務器; sync:將數據同步寫入內存緩沖區與磁盤中,效率低,但可以保證數據的一致性; async:將數據先保存在內存緩沖區中,必要時才寫入磁盤; wdelay:檢查是否有相關的寫操作,如果有則將這些寫操作一起執行,這樣可以提高效率(默認設置); no_wdelay:若有寫操作則立即執行,應與sync配合使用; subtree:若輸出目錄是一個子目錄,則nfs服務器將檢查其父目錄的權限(默認設置); no_subtree:即使輸出目錄是一個子目錄,nfs服務器也不檢查其父目錄的權限,這樣可以提高效率;
啟動NFS服務器
為了使NFS服務器能正常工作,需要啟動rpcbind和nfs兩個服務,並且rpcbind一定要先於nfs啟動。
# service rpcbind start
# service nfs start
查詢NFS服務器狀態
# service rpcbind status
# service nfs status
停止NFS服務器
要停止NFS運行時,需要先停止nfs服務再停止rpcbind服務,對於系統中有其他服務(如NIS)需要使用時,不需要停止portmap服務
# service nfs stop
# service rpcbind stop
客戶端
客戶端也需要安裝rpcbind 和 nfs-utils, 也需要啟動這兩個服務
查看可用的nfs服務:
[root@bogon ~]# showmount -e 10.10.14.52 Export list for 10.10.14.52: /mnt/share *
掛載對應的nfs目錄到本地, 掛載前要檢查此目錄是否存在
mount -t nfs 10.10.14.52:/mnt/share /mnt
取消掛載(對應的本地路徑)
umount /mnt
固定nfs服務端口以便設置防火牆
vi /etc/sysconfig/nfs # uncomment the following options
RQUOTAD_PORT=875 LOCKD_TCPPORT=32803 LOCKD_UDPPORT=32769
MOUNTD_PORT=892 STATD_PORT=662
# then stop nfs, stop prcbind, start rpcbind, start nfs # use "lsof -nPi" to check the listening ports
iptables中需要開放的端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 111 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 111 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 662 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 662 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 875 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 875 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 892 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 892 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 2049 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 2049 -j ACCEPT
問題解決
1. Centos5.x 客戶端
安裝的組件和Centos6.x不一樣
~]#yum install rpcbind ~]#yum install portmap ~]#yum install nfs-utils ~]#service portmap start ~]#service nfs start ~]#showmount -e 192.168.1.20 ~]#cd /mnt/ ~]#mkdir nfshare ~]#mount -t nfs 192.168.1.20:/home/tomcat/archive /mnt/nfshare ~]#ll /mnt/nfshare/
2. 出現 access denied by server 錯誤
在Centos6.5上安裝的nfs服務, 在Centos5.8上掛載正常, 但是在另一台Centos6.5上掛載出現 access denied by server.
nfs客戶端:
[root@localhost ~]# showmount -e 192.168.1.20 Export list for 192.168.1.20: /home/tomcat/archive 192.168.1.0/24 [root@localhost ~]# mount -t nfs 192.168.1.20:/home/tomcat/archive /mnt/nfshare mount.nfs: access denied by server while mounting 192.168.1.20:/home/tomcat/archive
nfs服務端:
[root@localhost ~]# more /etc/exports /home/tomcat/archive/ 192.168.1.0/24(ro,async)
經過測試, 如果在后面加入 no_root_squash 參數, (ro,async,no_root_squash) 后, 就不會再出現 access denied by server 錯誤, 但是這個選項是不推薦使用的.
后來發現在nfs客戶端使用這個命令能正常掛載
mount -o v3 192.168.1.20:/home/tomcat/archive /mnt/nfshare
這個 -o v3 表明使用的是 nfs version 3, 使用以下命令可以查看nfs version
[root@localhost ~]# mount -v /dev/mapper/VolGroup-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) nfsd on /proc/fs/nfsd type nfsd (rw) 192.168.1.20:/home/tomcat/archive on /mnt/nfshare type nfs (rw,v3,addr=192.168.1.20)
[root@localhost ~]# nfsstat -m /mnt/nfshare from 192.168.1.20:/home/tomcat/archive Flags: rw,relatime,vers=3,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.20,mountvers=3,mountport=892,mountproto=udp,local_lock=none,addr=192.168.1.20
設置啟動自動加載, 編輯 /etc/fstab, 例子
[root@iZ2578hac3vZ ~]# more /etc/fstab # # /etc/fstab # Created by anaconda on Thu Aug 14 21:16:42 2014 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=94e4e384-0ace-437f-bc96-057dd64f42ee / ext4 defaults,barrier=0 1 1 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 192.168.1.2:/archive/nfshare /mnt/nfshare nfs ro 0 0
同時要確保 rpcbind, nfs, netfs 這3個服務是開機自啟動的
chkconfig nfs on
chkconfig netfs on
chkconfig --list