CentOS-6.5-NFS部署


 
                   nfs-server與nfs-client端配置一樣
 
NFS(network file system)網絡文件系統;用於在網絡上共享存儲。
服務端-192.168.161.133
需要安裝的包nfs-utils      
rpcbind
客戶端-192.168.161.132
nfs-utils
rpcbind
1、服務端配置nfs
需要安裝兩個包nfs-utils和rpcbind(用於服務端和客戶端的通信,CentOS6以前的版本都是安裝portmap包、centos6版本安裝rpcbind)
[root@nfsserver /]# uname -r    打印系統版本
2.6.32-431.el6.x86_64
[root@nfsclient ~]# rpm -aq nfs-utils portmap rpcbind        #檢查nfs相關的包 如果有這兩個包的話使用rpm卸載  重新安裝
nfs-utils-1.2.3-39.el6.x86_64
rpcbind-0.2.0-11.el6.x86_64
[root@nfsclient /]# tail -6 /etc/yum.repos.d/CentOS-Media.repo    #查看本地yum配置文件
baseurl=file:///media/CentOS/
        file:///media/cdrom/
        file:///media/cdrecorder/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
[root@nfsclient /]# sed -i 's#enabled=0#enabled=1#gp' /etc/yum.repos.d/CentOS-Media.repo  #修改配置文件
[root@nfsclient /]# tail -6 /etc/yum.repos.d/CentOS-Media.repo     # 檢查是否修改成功
        file:///media/cdrom/
        file:///media/cdrecorder/
gpgcheck=1
enabled=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
[root@nfsclient /]# yum -y remove nfs-utils rpcbind    #卸載nfs相關的包  不建議使用yum  remove卸載包、因為他會把依拉包卸載掉、有可能會影響服務器上的其他服務
Removed:
  nfs-utils.x86_64 1:1.2.3-39.el6       rpcbind.x86_64 0:0.2.0-11.el6      
 
Dependency Removed:
  ipa-client.x86_64 0:3.0.0-37.el6    nfs-utils-lib.x86_64 0:1.1.5-6.el6   
  yp-tools.x86_64 0:2.9-12.el6        ypbind.x86_64 3:1.20.4-30.el6        
 
Complete!
[root@nfsclient /]# rpm -aq nfs-utils portmap rpcbind  #確認是否卸載成功
發一:[root@nfsserver /]# yum -y install nfs-utils rpcbind  #安裝nfs相關的包
Installed:
  nfs-utils.x86_64 1:1.2.3-70.el6_8.1     rpcbind.x86_64 0:0.2.0-12.el6    
 
Dependency Installed:
  nfs-utils-lib.x86_64 0:1.1.5-11.el6                                       
  python-argparse.noarch 0:1.2.1-2.1.el6                                    
 
Dependency Updated:
  libtirpc.x86_64 0:0.2.1-11.el6                                            
 
Complete!
[root@nfsserver /]# rpm -qa nfs-utils portmap rpcbind 
rpcbind-0.2.0-12.el6.x86_64
nfs-utils-1.2.3-70.el6_8.1.x86_64
                                    nfs-client端配置(安裝時兩邊都用同一種方法,保持一致不容易出錯)
法二:[root@nfsclient /]# yum grouplist | grep -i 'nfs'  #查找nfs包組
Failed to set locale, defaulting to C
   NFS file server
[root@nfsclient /]# yum -y groupinstall "NFS file server"
Installed:
  nfs-utils.x86_64 1:1.2.3-70.el6_8.1                                       
 
Dependency Installed:
  nfs-utils-lib.x86_64 0:1.1.5-11.el6                                       
  python-argparse.noarch 0:1.2.1-2.1.el6                                    
  rpcbind.x86_64 0:0.2.0-12.el6                                             
 
Updated:
  nfs4-acl-tools.x86_64 0:0.3.3-8.el6                                       
 
Dependency Updated:
  libtirpc.x86_64 0:0.2.1-11.el6                                            
 
Complete!
 
root@nfsclient /]# rpm -aq nfs-utils portmap rpcbind
rpcbind-0.2.0-12.el6.x86_64
nfs-utils-1.2.3-70.el6_8.1.x86_64
                                                    nfs-server端配置
[root@nfsserver /]# /etc/init.d/rpcbind start     #啟動rpcbind服務
Starting rpcbind:                                          [  OK  ]
[root@nfsserver /]# ps -ef | grep rpcbind     #查看rpcbind服務狀態
rpc       5212     1  0 15:04 ?        00:00:00 rpcbind
root      5221  4567  0 15:05 pts/0    00:00:00 grep rpcbind
[root@nfsserver /]# /etc/init.d/rpcbind status   #查看rpcbind服務狀態
rpcbind (pid  5212) is running...
[root@nfsserver /]# /etc/init.d/rpcbind stop
Stopping rpcbind:                                          [  OK  ]
[root@nfsserver /]# rpcinfo -p localhost
rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused
報錯原因rpcbind服務未啟動
[root@nfsserver /]# rpcinfo -p localhost
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
 
[root@nfsserver /]# /etc/init.d/nfs start   #啟動nfs服務
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
正在啟動 RPC idmapd:                                      [確定]
[root@nfsserver /]# rpcinfo -p localhost
   program vers proto   port  service
    100000    4   tcp    111  portmapper
    100000    3   tcp    111  portmapper
    100000    2   tcp    111  portmapper
    100000    4   udp    111  portmapper
    100000    3   udp    111  portmapper
    100000    2   udp    111  portmapper
    100011    1   udp    875  rquotad
    100011    2   udp    875  rquotad
    100011    1   tcp    875  rquotad
    100011    2   tcp    875  rquotad
    100005    1   udp  37917  mountd
    100005    1   tcp  38242  mountd
    100005    2   udp  40271  mountd
    100005    2   tcp  50999  mountd
    100005    3   udp  48150  mountd
    100005    3   tcp  47540  mountd
    100003    2   tcp   2049  nfs
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    2   tcp   2049  nfs_acl
    100227    3   tcp   2049  nfs_acl
    100003    2   udp   2049  nfs
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    2   udp   2049  nfs_acl
    100227    3   udp   2049  nfs_acl
    100021    1   udp  38739  nlockmgr
    100021    3   udp  38739  nlockmgr
    100021    4   udp  38739  nlockmgr
    100021    1   tcp  44041  nlockmgr
    100021    3   tcp  44041  nlockmgr
    100021    4   tcp  44041  nlockmgr
[root@nfsserver /]# chkconfig nfs on      #添加開機自啟
[root@nfsserver /]# chkconfig rpcbind on
[root@nfsserver /]# chkconfig --list nfs    #檢查服務是否成功添加開機自啟
nfs                0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@nfsserver /]# chkconfig --list rpcbind
rpcbind            0:off    1:off    2:on    3:on    4:on    5:on    6:off
 
nfs-client配置
root@nfsclient /]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@nfsclient /]# /etc/init.d/rpcbind status
rpcbind (pid  25927) is running...
[root@nfsclient /]# chkconfig rpcbind on
[root@nfsclient /]# chkconfig --list rpcbind
rpcbind            0:off    1:off    2:on    3:on    4:on    5:on    6:off
 

 

 
                                                     nfs-server端配置
[root@nfsserver /]# mkdir /data            #創建共享目錄
[root@nfsserver /]# vim /etc/exports    #nfs配置文件 默認為空
[root@nfsserver /]# cat /etc/exports 
#shared data for bbs by oldboy at 20160810
/data 192.168.161.0/24(rw,sync)
注:這里的共享目錄是/data目錄;指定一個網段192.168.161.0/24(也可以指定單個IP);rw為可讀寫(ro只讀);sync為同步模式(sync寫入數據是直接寫入磁盤,async非同步,寫入數據寫書內存、如果nfs服務端宕機容易造成數據丟失)詳細注解可使用man exports命令;具體參數可參考下面nfs默認的配置、
[root@nfsserver /]# cat /var/lib/nfs/etab      #nfs默認的配置參數
/data    192.168.161.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash)
[root@nfsserver /]# /etc/init.d/nfs reload
[root@nfsserver /]# showmount -e localhost        #現在本機檢查共享是否成功
Export list for localhost:
/data 192.168.161.0/24

 

 
                                                             nfs-client端配置
 
 
 
[root@nfsclient /]# showmount -e 192.168.161.133     //此為服務端IP
注:要關閉防火牆、不然客戶端查不到共享,匯報如下錯誤:
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
clnt_create:RPC:端口映射器失敗——不能接受:errno 113(沒有通往主機)
[root@nfsclient /]# service iptables stop    #關閉client端防火牆 或開放相應的端口
iptables:將鏈設置為政策 ACCEPT:filter                    [確定]
iptables:清除防火牆規則:                                 [確定]
iptables:正在卸載模塊:                                   [確定]
[root@nfsserver /]# service iptables stop                #關閉client端防火牆 或開放相應的端口
iptables:將鏈設置為政策 ACCEPT:filter                    [確定]
iptables:清除防火牆規則:                                 [確定]
iptables:正在卸載模塊:                                   [確定]
故障排查:[root@nfsclient /]# ping 192.168.161.133
[root@nfsclient /]# telnet 192.168.161.133 111
[root@nfsclient /]# showmount -e 192.168.161.133    #成功檢測到共享
Export list for 192.168.161.133:
/data 192.168.161.0/24
[root@nfsclient /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        16G  4.0G   12G  27% /
tmpfs           495M   80K  495M   1% /dev/shm
/dev/sda1       2.0G   59M  1.8G   4% /boot
/dev/sr0        4.2G  4.2G     0 100% /media/CentOS_6.5_Final
/dev/sr0        4.2G  4.2G     0 100% /media/cdrom
[root@nfsclient /]# mount -t nfs 192.168.161.133:/data /mnt   #掛載到/mnt下
[root@nfsclient /]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               16G  4.0G   12G  27% /
tmpfs                  495M   80K  495M   1% /dev/shm
/dev/sda1              2.0G   59M  1.8G   4% /boot
/dev/sr0               4.2G  4.2G     0 100% /media/CentOS_6.5_Final
/dev/sr0               4.2G  4.2G     0 100% /media/cdrom
192.168.161.133:/data   16G  3.9G   12G  26% /mnt   #掛載成功
[root@nfsclient /]# cd /mnt
[root@nfsclient mnt]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 10 15:49 11
-rw-r--r--. 1 root root 0 Aug 10 15:49 22
-rw-r--r--. 1 root root 0 Aug 10 15:49 33
[root@nfsclient mnt]# touch test.log      #測試下是否有寫入權限
touch: cannot touch `test.log': Permission denied    拒絕訪問
到nfs-server端檢查
[root@nfsserver /]# ll /data -ld
drwxr-xr-x 2 root root 4096 Aug 10 15:49 /data
[root@nfsserver /]# cat /var/lib/nfs/etab    #此目錄是nfs默認的用戶的信息
/data    192.168.161.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash)
[root@nfsserver /]# grep 65534 /etc/passwd     #根據用戶id查出用戶
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[root@nfsserver /]# chown -R nfsnobody.nfsnobody /data     #給nsfnobody賦予權限
[root@nfsclient mnt]# touch test.log     #創建測試目錄
[root@nfsclient mnt]# ll
total 0
-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 10 15:49 11
-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 10 15:49 22
-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 10 15:49 33
-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 10 16:02 test.log   #創建成功

 

開機自動掛載  修改/etc/fstab文件
 
 
nfs客戶端自動掛載autofs
為什么需要autofs
autofs可以實現當前用戶訪問的時候在掛載,如果沒有用戶訪問,指定之間、內,就自動卸載。
可以解決NFS服務器和客戶端緊密耦合的問題。缺點:是用戶請求才會掛載,所有請求的瞬間效率較差。一般的企業不用它。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM