1. 程式人生 > >nfs網絡共享服務基礎

nfs網絡共享服務基礎

star bind 就會 showmount oca -c tar nfs設置 連通性

nfs原理

技術分享

1.開啟RPC服務

2.NFS向RPC服務註冊啟動的端口

3.用戶向RPC詢問NFS服務的端口

4.RPC返回端口給客戶端

5.客戶端通過獲得的端口與NFS服務器進行數據傳輸

實驗步驟

一、準備

1.檢查系統版本信息

2.檢查是否安裝nfs和rpc服務

3.安裝服務

二、配置

1.啟動rpc服務和nfs服務

2.查看nfs是否有向rpc註冊端口

3.設置開機自啟動

4.編輯nfs配置文件,並重啟服務

5.在客戶端進行掛載

三、測試

1.測試客戶端與服務端的連通性

2.在分享目錄內創建文件並在客戶機上測試

詳細配置:

註意:關閉selinux和iptables

服務端:

檢查系統版本

[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[[email protected] ~]# uname -r
2.6.32-642.el6.x86_64
[[email protected] ~]# uname -m
x86_64

檢查是否安裝服務

[[email protected] ~]#  rpm -aq nfs-utils rpcbind

安裝服務

#這裏可以有兩種方法,一種是正常yum安裝,另一種是yum組安裝
#分別在服務端和客戶端使用這兩種方法
[[email protected] ~]# yum install nfs-utils rpcbind -y

啟動rpc服務

#必須先開啟rpc服務,在開啟nfs服務,否則nfs服務會啟動不了,並且可能會發生錯誤

[[email protected] ~]# /etc/init.d/rpcbind restart
Stopping rpcbind:                                          [  OK  ]
Starting rpcbind:                                          [  OK  ]
[[email protected] ~]# ps -ef |grep rpc
rpcuser   1258     1  0 18:36 ?        00:00:00 rpc.statd
rpc       1630     1  0 18:53 ?        00:00:00 rpcbind
root      1634  1546  0 18:54 pts/0    00:00:00 grep --color=auto rpc

#檢查nfs是否有向rpc註冊端口
[[email protected] ~]# 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
#此處結果顯示表示rpc自己使用的端口

[[email protected] ~]# rpcinfo -p localhost
rpcinfo: can‘t contact portmapper: RPC: Remote system error - Connection refused    
#此處即為rpc沒有開啟而產生的報錯

開啟nfs服務

[[email protected] ~]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
[[email protected] ~]# 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
    100005    1   udp  44683  mountd
    100005    1   tcp  53742  mountd
    100005    2   udp  45815  mountd
    100005    2   tcp  37259  mountd
    100005    3   udp  43988  mountd
    100005    3   tcp  48220  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  43032  nlockmgr
    100021    3   udp  43032  nlockmgr
    100021    4   udp  43032  nlockmgr
    100021    1   tcp  52980  nlockmgr
    100021    3   tcp  52980  nlockmgr
    100021    4   tcp  52980  nlockmgr
#這種輸出才是nfs向rpc註冊端口後的結果,由於nfs提供多種服務,所以會申請多個端口
#還可以在/etc/sysconfig/nfs中設定監聽端口,便於iptbales的書寫
echo"LOCKD_TCPPORT=32803" >>/etc/sysconfig/nfs

將rpc和nfs設置為開機自啟動

[[email protected] ~]# chkconfig nfs on
[[email protected] ~]# chkconfig rpcbind on
[[email protected] ~]# chkconfig --list nfs
nfs             0:off   1:off   2:on    3:on    4:on    5:on    6:off
[[email protected] ~]# chkconfig --list rpcbind
rpcbind         0:off   1:off   2:on    3:on    4:on    5:on    6:off
#其實查看/etc/init.d/nfs | rpcbind即可得知,rpc於nfs先啟動,後關閉

配置nfs服務並重啟

[[email protected] ~]# mkdir /data
[[email protected] data]# chown -R nfsnobody.nfsnobody /data/
#如果沒有修改共享目錄的屬主,則可能會出現如下報錯
#touch: cannot touch `hehe‘: Permission denied
#實際上nfs在開啟服務後會自動創建nfsnobody這個用戶,可在/var/lib/nfs/etab 查看默認設置

[[email protected]erver ~]# cat /etc/exports 
##share my miss for glx at 2017/09/13
/data 192.168.1.0/24(rw,sync)
[[email protected] ~]# /etc/init.d/nfs reload    
#ps:reload平滑重啟不會影響正在訪問客戶

#重啟後檢查
[[email protected] ~]# showmount -e localhost   
Export list for localhost:
/data 192.168.1.0/24
#ps:如果出現clnt_create: RPC: Program not registered錯誤,則是rpc和nfs的啟動順序出錯,將兩者都停止並按fpc,nfs順序重新啟動即可

 

客戶端:

安裝服務

[[email protected] ~]# yum groupinstall "NFS file server" -y
#其實這個安裝包組中包含了nfs及rpc,實際上客戶端只需要rpc即可

啟動rpc服務,並設置成開機自啟動

[[email protected] ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[[email protected] ~]# chkconfig rpcbind on
[[email protected] ~]# chkconfig --list rpcbind
rpcbind         0:off   1:off   2:on    3:on    4:on    5:on    6:off

測試與服務端的連通性

[[email protected] ~]# ping 192.168.1.111
[[email protected] ~]# telnet 192.168.1.111 111

掛載共享目錄到本地

[[email protected] ~]# mount -t nfs 192.168.1.111:/data /mnt
[[email protected] ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2            7.1G  907M  5.9G  14% /
tmpfs                495M     0  495M   0% /dev/shm
/dev/sda1            190M   30M  150M  17% /boot
/dev/sr0             3.7G  3.7G     0 100% /media/cd
192.168.1.111:/data   12G  2.0G  8.9G  19% /mnt

#這樣掛載重啟就會失效,所以如果對安全沒特別要求可寫入/etc/fstab中
#或者echo "mount -t nfs 192.168.1.111:/data /mnt" >> /etc/rc.local

  

nfs網絡共享服務基礎