Linux基礎學習-NFS網絡文件系統實時文件共享

NFS網絡文件系統

若是你們以爲Samba服務程序的配置太麻煩了,那麼你共享文件的主機都是Linux系統,那麼推薦你們在客戶端部署nfs服務來共享文件.nfs(網絡文件系統)服務能夠將遠程Linux系統上的文件共享資源掛載到本地主機的目錄上,從而使得本地主機客戶端基於TCP/IP協議,像是用本地主機上的資源那樣讀寫遠程Linux系統上的共享文件.linux

實驗環境

主機 操做系統 IP地址
NFS服務器 RHEL7.4 192.168.56.15
NFS客戶端 CentOS7.4 192.168.56.5
前期準備這裏我把防火牆以及selinux都給關閉了.

1 安裝 NFS-Server 使用 yum install nfs-utils rpcbind -y 命令.vim

[root@qdlinux ~]# yum install -y nfs-utils rpcbind
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
dvd                                                                                       | 4.1 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package nfs-utils.x86_64 1:1.3.0-0.48.el7 will be installed
--> Processing Dependency: gssproxy >= 0.7.0-3 for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> Processing Dependency: libtirpc >= 0.2.4-0.7 for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> Processing Dependency: libevent for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> Processing Dependency: libnfsidmap for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> Processing Dependency: quota for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> Processing Dependency: libevent-2.0.so.5()(64bit) for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> Processing Dependency: libnfsidmap.so.0()(64bit) for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
--> Processing Dependency: libtirpc.so.1()(64bit) for package: 1:nfs-utils-1.3.0-0.48.el7.x86_64
---> Package rpcbind.x86_64 0:0.2.0-42.el7 will be installed
--> Running transaction check
---> Package gssproxy.x86_64 0:0.7.0-4.el7 will be installed
--> Processing Dependency: libini_config.so.3(INI_CONFIG_1.1.0)(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> Processing Dependency: libini_config.so.3(INI_CONFIG_1.2.0)(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> Processing Dependency: libref_array.so.1(REF_ARRAY_0.1.1)(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> Processing Dependency: libverto-module-base for package: gssproxy-0.7.0-4.el7.x86_64
--> Processing Dependency: libbasicobjects.so.0()(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> Processing Dependency: libcollection.so.2()(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> Processing Dependency: libini_config.so.3()(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
--> Processing Dependency: libref_array.so.1()(64bit) for package: gssproxy-0.7.0-4.el7.x86_64
---> Package libevent.x86_64 0:2.0.21-4.el7 will be installed
---> Package libnfsidmap.x86_64 0:0.25-17.el7 will be installed
---> Package libtirpc.x86_64 0:0.2.4-0.10.el7 will be installed
---> Package quota.x86_64 1:4.01-14.el7 will be installed
--> Processing Dependency: quota-nls = 1:4.01-14.el7 for package: 1:quota-4.01-14.el7.x86_64
--> Processing Dependency: tcp_wrappers for package: 1:quota-4.01-14.el7.x86_64
--> Running transaction check
---> Package libbasicobjects.x86_64 0:0.1.1-27.el7 will be installed
---> Package libcollection.x86_64 0:0.6.2-27.el7 will be installed
---> Package libini_config.x86_64 0:1.3.0-27.el7 will be installed
--> Processing Dependency: libpath_utils.so.1(PATH_UTILS_0.2.1)(64bit) for package: libini_config-1.3.0-27.el7.x86_64
--> Processing Dependency: libpath_utils.so.1()(64bit) for package: libini_config-1.3.0-27.el7.x86_64
---> Package libref_array.x86_64 0:0.1.5-27.el7 will be installed
---> Package libverto-tevent.x86_64 0:0.2.5-4.el7 will be installed
---> Package quota-nls.noarch 1:4.01-14.el7 will be installed
---> Package tcp_wrappers.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package libpath_utils.x86_64 0:0.2.1-27.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================
 Package                        Arch                  Version                           Repository          Size
=================================================================================================================
Installing:
 nfs-utils                      x86_64                1:1.3.0-0.48.el7                  dvd                398 k
 rpcbind                        x86_64                0.2.0-42.el7                      dvd                 59 k
Installing for dependencies:
 gssproxy                       x86_64                0.7.0-4.el7                       dvd                105 k
 libbasicobjects                x86_64                0.1.1-27.el7                      dvd                 25 k
 libcollection                  x86_64                0.6.2-27.el7                      dvd                 41 k
 libevent                       x86_64                2.0.21-4.el7                      dvd                214 k
 libini_config                  x86_64                1.3.0-27.el7                      dvd                 63 k
 libnfsidmap                    x86_64                0.25-17.el7                       dvd                 49 k
 libpath_utils                  x86_64                0.2.1-27.el7                      dvd                 27 k
 libref_array                   x86_64                0.1.5-27.el7                      dvd                 26 k
 libtirpc                       x86_64                0.2.4-0.10.el7                    dvd                 89 k
 libverto-tevent                x86_64                0.2.5-4.el7                       dvd                9.0 k
 quota                          x86_64                1:4.01-14.el7                     dvd                179 k
 quota-nls                      noarch                1:4.01-14.el7                     dvd                 91 k
 tcp_wrappers                   x86_64                7.6-77.el7                        dvd                 79 k

Transaction Summary
=================================================================================================================
Install  2 Packages (+13 Dependent packages)

Total download size: 1.4 M
Installed size: 4.1 M
Downloading packages:
-----------------------------------------------------------------------------------------------------------------
Total                                                                            2.5 MB/s | 1.4 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libref_array-0.1.5-27.el7.x86_64                                                             1/15 
  Installing : libcollection-0.6.2-27.el7.x86_64                                                            2/15 
  Installing : libbasicobjects-0.1.1-27.el7.x86_64                                                          3/15 
  Installing : libtirpc-0.2.4-0.10.el7.x86_64                                                               4/15 
  Installing : rpcbind-0.2.0-42.el7.x86_64                                                                  5/15 
  Installing : 1:quota-nls-4.01-14.el7.noarch                                                               6/15 
  Installing : libpath_utils-0.2.1-27.el7.x86_64                                                            7/15 
  Installing : libini_config-1.3.0-27.el7.x86_64                                                            8/15 
  Installing : tcp_wrappers-7.6-77.el7.x86_64                                                               9/15 
  Installing : 1:quota-4.01-14.el7.x86_64                                                                  10/15 
  Installing : libevent-2.0.21-4.el7.x86_64                                                                11/15 
  Installing : libnfsidmap-0.25-17.el7.x86_64                                                              12/15 
  Installing : libverto-tevent-0.2.5-4.el7.x86_64                                                          13/15 
  Installing : gssproxy-0.7.0-4.el7.x86_64                                                                 14/15 
  Installing : 1:nfs-utils-1.3.0-0.48.el7.x86_64                                                           15/15 
  Verifying  : libverto-tevent-0.2.5-4.el7.x86_64                                                           1/15 
  Verifying  : rpcbind-0.2.0-42.el7.x86_64                                                                  2/15 
  Verifying  : 1:quota-4.01-14.el7.x86_64                                                                   3/15 
  Verifying  : libtirpc-0.2.4-0.10.el7.x86_64                                                               4/15 
  Verifying  : libnfsidmap-0.25-17.el7.x86_64                                                               5/15 
  Verifying  : libini_config-1.3.0-27.el7.x86_64                                                            6/15 
  Verifying  : libbasicobjects-0.1.1-27.el7.x86_64                                                          7/15 
  Verifying  : libevent-2.0.21-4.el7.x86_64                                                                 8/15 
  Verifying  : tcp_wrappers-7.6-77.el7.x86_64                                                               9/15 
  Verifying  : libpath_utils-0.2.1-27.el7.x86_64                                                           10/15 
  Verifying  : 1:quota-nls-4.01-14.el7.noarch                                                              11/15 
  Verifying  : gssproxy-0.7.0-4.el7.x86_64                                                                 12/15 
  Verifying  : libcollection-0.6.2-27.el7.x86_64                                                           13/15 
  Verifying  : libref_array-0.1.5-27.el7.x86_64                                                            14/15 
  Verifying  : 1:nfs-utils-1.3.0-0.48.el7.x86_64                                                           15/15 

Installed:
  nfs-utils.x86_64 1:1.3.0-0.48.el7                         rpcbind.x86_64 0:0.2.0-42.el7                        

Dependency Installed:
  gssproxy.x86_64 0:0.7.0-4.el7        libbasicobjects.x86_64 0:0.1.1-27.el7 libcollection.x86_64 0:0.6.2-27.el7
  libevent.x86_64 0:2.0.21-4.el7       libini_config.x86_64 0:1.3.0-27.el7   libnfsidmap.x86_64 0:0.25-17.el7   
  libpath_utils.x86_64 0:0.2.1-27.el7  libref_array.x86_64 0:0.1.5-27.el7    libtirpc.x86_64 0:0.2.4-0.10.el7   
  libverto-tevent.x86_64 0:0.2.5-4.el7 quota.x86_64 1:4.01-14.el7            quota-nls.noarch 1:4.01-14.el7     
  tcp_wrappers.x86_64 0:7.6-77.el7    

Complete!

2 啓動 NFS-Server服務器

[root@qdlinux ~]# systemctl start rpcbind
[root@qdlinux ~]# systemctl enable rpcbind
[root@qdlinux ~]# systemctl start nfs-server.service 
[root@qdlinux ~]# systemctl enable nfs-server.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

啓動和啓用NFS服務程序,因爲在使用NFS服務進行文件共享以前,須要使用RPC(Remote Procedure Call,遠程過程調用),服務將NFS服務器的IP地址和端口號等信息發送給客戶端.所以,在啓動NFS服務以前,還須要順帶重啓並啓動rpcbind服務,這裏將兩個服務一併加入到開機啓動項中.網絡

3 可使用 rpcbind -p 查看一下注冊的信息.app

[root@qdlinux ~]# rpcinfo -p
   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
    100024    1   udp  43946  status
    100024    1   tcp  38188  status
    100005    1   udp  20048  mountd
    100005    1   tcp  20048  mountd
    100005    2   udp  20048  mountd
    100005    2   tcp  20048  mountd
    100005    3   udp  20048  mountd
    100005    3   tcp  20048  mountd
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    3   tcp   2049  nfs_acl
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    3   udp   2049  nfs_acl
    100021    1   udp  35724  nlockmgr
    100021    3   udp  35724  nlockmgr
    100021    4   udp  35724  nlockmgr
    100021    1   tcp  44097  nlockmgr
    100021    3   tcp  44097  nlockmgr
    100021    4   tcp  44097  nlockmgr

4 查看系統相關進程信息async

[root@qdlinux ~]# ps -ef | egrep "rpc|nfs"
rpc        1959      1  0 16:37 ?        00:00:00 /sbin/rpcbind -w
rpcuser    2039      1  0 16:38 ?        00:00:00 /usr/sbin/rpc.statd
root       2047      2  0 16:38 ?        00:00:00 [rpciod]
root       2057      1  0 16:38 ?        00:00:00 /usr/sbin/rpc.mountd
root       2058      1  0 16:38 ?        00:00:00 /usr/sbin/rpc.idmapd
root       2068      2  0 16:38 ?        00:00:00 [nfsd4_callbacks]
root       2074      2  0 16:38 ?        00:00:00 [nfsd]
root       2075      2  0 16:38 ?        00:00:00 [nfsd]
root       2076      2  0 16:38 ?        00:00:00 [nfsd]
root       2077      2  0 16:38 ?        00:00:00 [nfsd]
root       2078      2  0 16:38 ?        00:00:00 [nfsd]
root       2079      2  0 16:38 ?        00:00:00 [nfsd]
root       2080      2  0 16:38 ?        00:00:00 [nfsd]
root       2081      2  0 16:38 ?        00:00:00 [nfsd]
root       2135   1633  0 16:52 pts/0    00:00:00 grep -E --color=auto rpc|nfs

5 NFS服務端配置共享目錄tcp

參數 做用
ro 只讀
rw 讀寫
root_squash 當NFS客戶端以root管理員訪問時,映射爲NFS服務器的匿名用戶
no_root_squash 當NFS客戶端以root管理員訪問時,映射爲NFS服務器的root管理員
all_squash 不管NFS客戶端使用什麼帳戶訪問,均映射爲NFS服務器的匿名用戶
sync 同時將數據寫入到內存與硬盤中,保證不丟失數據
async 優先將數據保存到內存,而後再寫入到硬盤,這樣效率更高,但可能會丟失數據
[root@qdlinux ~]# vim /etc/exports
/nfsfile 192.168.56.*(rw,sync,all_squash)

//建立須要共享的目錄
[root@qdlinux ~]# mkdir /nfsfile

[root@qdlinux ~]# systemctl restart nfs-server.service 
[root@qdlinux ~]# cat /var/lib/nfs/etab 
/nfsfile    192.168.56.*(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,secure,root_squash,all_squash)

//查看一下id=65534是哪個用戶
[root@qdlinux ~]# grep "65534" /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

//修改一下`/nfsfile`的權限,這裏也能夠把目錄所屬主和所屬組改成nfsnobody或者直接給成777權限
[root@qdlinux ~]# chmod -Rf nfsnobody:nfsnobody /nfsfile/

//在目錄下寫入一個文件用於測試
[root@qdlinux ~]# echo "welecome to qdlinux" > /nfsfile/readme
[root@qdlinux ~]# ll  /nfsfile/
total 4
-rw-r--rwx 1 root root 20 Aug 16 17:30 readme
[root@qdlinux ~]#

NFS客戶端的配置步驟十分簡單,先使用 showmount 命令查詢NFS服務器的遠程共享信息,其輸出格式爲"共享的目錄名稱 容許使用客戶端地址".ide

參數 做用
-e 顯示NFS服務器的共享李彪
-a 顯示本機掛載的文件資源的狀況
-v 顯示版本號
[root@qdlinux ~]# showmount -e 192.168.56.15
Export list for 192.168.56.15:
/nfsfile 192.168.56.*

6 客戶端測試測試

[root@qdlinux ~]# yum install nfs-utils rpcbind -y
[root@qdlinux ~]# systemctl start rpcbind
[root@qdlinux ~]# systemctl is-enabled rpcbind
enabled
[root@qdlinux ~]# systemctl is-active rpcbind
active

//查詢一下服務是否運行
[root@qdlinux ~]# systemctl status rpcbind
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-08-16 14:05:32 CST; 3h 48min ago
 Main PID: 568 (rpcbind)
    Tasks: 1
   CGroup: /system.slice/rpcbind.service
           └─568 /sbin/rpcbind -w

Aug 16 14:05:27 qdlinux.com systemd[1]: Starting RPC bind service...
Aug 16 14:05:32 qdlinux.com systemd[1]: Started RPC bind service.

//在客戶端使用`showmount -e`命令查詢一下
[root@qdlinux ~]# showmount -e 192.168.56.15 NFS服務器共享列表
Export list for 192.168.56.15:
/nfsfile 192.168.56.*

7 NFS客戶端掛載ui

在NFS客戶端建立一個掛在目錄,使用mount命令加-t參數,指定要掛在的文件系統的類型,並在命令後面寫上服務器的IP地址、服務器上的共享目錄以及要掛載到本地系統(客戶端)的目錄.

[root@qdlinux /]# mkdir /data
[root@qdlinux /]# mount -t nfs 192.168.56.15:/nfsfile /data

//如今已經掛載成功了,能夠看到在NFS服務器上建立的文件了.

若是但願NFS文件共享服務能一直有效,須要將其寫入到 fstab 文件中.

[root@qdlinux data]# vim /etc/fstab
192.168.56.15:/nfsfile                    /data                   nfs     defaults        0 0
1 若是NFS服務器宕機,強制卸載umount -lf /data
2 [root@qdlinux data]# umount /data
  umount.nfs4: /data: device is busy
  出現這種狀況時,退出掛載目錄而後在卸載

8 autofs自動掛載服務

autofs服務程序是一種Linux系統守護進程,當檢測到用戶試圖去訪問一個還沒有掛載的文件系統時,將自動掛載該文件系統,咱們將掛載信息寫入/etc/fstab文件後,系統在每次開機時都自動將其掛載,而autofs自動掛載服務是用戶須要使用該文件系統時纔會去動態掛載,從而節約了網絡資源和服務器的硬件資源.

[root@qdlinux /]# yum install autofs -y

直接掛載方式,本地的掛載點是絕對路徑

[root@qdlinux /]# vim /etc/auto.master.d/nfs.autofs
/-      /etc/auto.nfs_direct
[root@qdlinux /]# vim /etc/auto.nfs_direct 
/data   -rw,sync,soft   192.168.56.15:/nfsfile

//mount查看一下
/etc/auto.nfs_direct on /data type autofs (rw,relatime,fd=18,pgrp=6217,timeout=300,minproto=5,maxproto=5,direct,pipe_ino=79495)

間接掛載方式

[root@qdlinux ~]# mkdir -p /public/share
[root@qdlinux ~]# chown -Rf nfsnobody:nfsnobody /public/share
[root@qdlinux ~]# vim /etc/exports
/public/share 192.168.56.*(rw,sync,all_squash)
//從新啓動nfs-server

[root@qdlinux ~]# showmount -e 192.168.56.15
Export list for 192.168.56.15:
/public/share 192.168.56.*
/nfsfile      192.168.56.*

[root@qdlinux /]# mkdir /test
[root@qdlinux /]# vim /etc/auto.master.d/share.autofs
/test  /etc/share.nfs_share
[root@qdlinux /]# vim /etc/share.nfs_share
*       -rw,sync,soft   192.168.56.15:/public/&
//從新啓動autofs服務

如今能夠訪問了,客戶端當你切換到/test/share時能夠建立文件服務端實時同步.

相關文章
相關標籤/搜索