Linux網絡文件共享服務之NFS


一、NFS服務簡介

  NFS全稱network file system 網絡文件系統,基於內核的文件系統,由sun公司開發,通過使用NFS,用戶和程序可以像訪問本地文件一樣訪問遠端系統上的文件,它基於rpc實現(rpc是remote procedure call protocol 遠程過程調用)。RPC采用C/S模式,客戶機請求程序調用進程發送一個有進程參數的調用信息到服務進程,然后等待應答信息。在服務器端,進程保存睡眠狀態直到調用信息到達為止。當一個調用信息到達,服務器獲得進程參數,計算結果,發送答復信息,然后等待下一個調用信息,最后,客戶端調用進程接收答復信息,獲得進程結果,然后調用執行繼續進行。

  NFS優勢:節省本地存儲空間,將常用的數據,如home目錄存放在NFS服務器上且可以通過網絡訪問,本地終端將可減少自身存儲空間的使用。

二、NFS工作原理

  如上圖所示,用戶需要訪問本地的資源,可通過本地文件系統訪問,本地磁盤的資源,如果用戶訪問NFS服務器上的資源,則用戶需要通過rpc服務,通過網絡去訪問NFS服務器,當用戶的訪問請求到達NFS服務器時,首先到達NFS服務器的tcp/ip協議棧,然后通過詢問NFS服務器上的rpc服務,rpc服務在NFS服務器上的作用是當NFS啟動時,NFS服務所監聽的端口有很多且是隨機,不固定,rpc的作用就是記錄這些端口,如果有人來訪問NFS服務了,它就把NFS的端口信息告訴客戶端,然后客戶端通過rpc給定端口信息,從而實現訪問NFS服務。NFS服務收到客戶端的請求,然后通過本地文件系統,訪問對應本地磁盤上的文件,從而實現了完成客戶端的訪問和響應。

三、NFS服務實現

  1、軟件包信息

[root@test ~]# yum info nfs-utils
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
可安裝的軟件包
名稱    :nfs-utils
架構    :x86_64
時期       :1
版本    :1.3.0
發布    :0.65.el7
大小    :412 k
源    :base/7/x86_64
簡介    : NFS utilities and supporting clients and daemons for the kernel NFS server
網址    :http://sourceforge.net/projects/nfs
協議    : MIT and GPLv2 and GPLv2+ and BSD
描述    : The nfs-utils package provides a daemon for the kernel NFS server and
         : related tools, which provides a much higher level of performance than the
         : traditional Linux NFS server used by most users.
         : 
         : This package also contains the showmount program.  Showmount queries the
         : mount daemon on a remote host for information about the NFS (Network File
         : System) server on the remote host.  For example, showmount can display the
         : clients which are mounted on that host.
         : 
         : This package also contains the mount.nfs and umount.nfs program.

[root@test ~]# 

  說明:NFS它是文件系統,在內核已經支持,且已經有它的驅動,我們裝包的原因是,用軟件去管理NFS文件系統,因為用戶是沒有辦法直接和內核交互的。從上面的信息也可了解到nfs-utils的介紹它是NFS實用程序和支持NFS內核服務器的客戶機和守護進程。

  2、安裝nfs-utils

[root@test ~]# yum install nfs-utils
……省略部分信息
依賴關系解決

============================================================================================================
 Package                       架構                 版本                           源                  大小
============================================================================================================
正在安裝:
 nfs-utils                     x86_64               1:1.3.0-0.65.el7               base               412 k
為依賴而安裝:
 gssproxy                      x86_64               0.7.0-26.el7                   base               110 k
 keyutils                      x86_64               1.5.8-3.el7                    base                54 k
 libbasicobjects               x86_64               0.1.1-32.el7                   base                26 k
 libcollection                 x86_64               0.7.0-32.el7                   base                42 k
 libini_config                 x86_64               1.3.1-32.el7                   base                64 k
 libnfsidmap                   x86_64               0.25-19.el7                    base                50 k
 libpath_utils                 x86_64               0.2.1-32.el7                   base                28 k
 libref_array                  x86_64               0.1.5-32.el7                   base                27 k
 libtirpc                      x86_64               0.2.4-0.16.el7                 base                89 k
 libverto-tevent               x86_64               0.2.5-4.el7                    base               9.0 k
 quota                         x86_64               1:4.01-19.el7                  base               179 k
 quota-nls                     noarch               1:4.01-19.el7                  base                90 k
 rpcbind                       x86_64               0.2.0-48.el7                   base                60 k
 tcp_wrappers                  x86_64               7.6-77.el7                     base                78 k

事務概要
============================================================================================================
安裝  1 軟件包 (+14 依賴軟件包)
……省略部分信息

  說明:可以看到nfs-utils依賴這些包,其中rpcbind就是nfs啟動時所使用的端口它會記錄起來,一旦有客戶端訪問,它就把nfs的端口信息告訴給客戶端

  3、寫配置文件,讓其客戶端允許訪問本地的資源

[root@test ~]# mkdir /nfsdir
[root@test ~]# cat >> /etc/exports << EOF
> /nfsdir 192.168.0.0/24(rw)
> EOF
[root@test ~]# cat /etc/exports
/nfsdir 192.168.0.0/24(rw)
[root@test ~]# 

  說明:nfs服務的配置文件是/etc/exports 和/etc/exports.d/*.exports,以上定義了/nfsdir目錄允許192.168.0.0/24當用戶訪問,相當與把/nfsdir給共享出來了

  4、啟動服務

[root@test ~]# systemctl status rpcbind nfs-server
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@test ~]# systemctl start nfs-server
[root@test ~]# systemctl status rpcbind nfs-server
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)
   Active: active (running) since 三 2020-01-29 23:52:08 CST; 2s ago
  Process: 4754 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 4756 (rpcbind)
    Tasks: 1
   Memory: 1.1M
   CGroup: /system.slice/rpcbind.service
           └─4756 /sbin/rpcbind -w

1月 29 23:52:08 test systemd[1]: Starting RPC bind service...
1月 29 23:52:08 test systemd[1]: Started RPC bind service.

● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: active (exited) since 三 2020-01-29 23:52:08 CST; 1s ago
  Process: 4787 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
  Process: 4771 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 4768 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 4771 (code=exited, status=0/SUCCESS)
    Tasks: 0
   Memory: 0B
   CGroup: /system.slice/nfs-server.service

1月 29 23:52:08 test systemd[1]: Starting NFS server and services...
1月 29 23:52:08 test systemd[1]: Started NFS server and services.
[root@test ~]# 

  說明:可看到在rpcbind 服務沒有啟動的情況下,直接啟動nfs-server ,它會把rpcbind給一並啟動起來,因為nfs-server依賴rpcbind服務。這個是在centos7上是這樣的,centos7上有服務依賴解決,如果啟動服務所依賴的服務沒有啟動,它會激活所依賴的服務,然后啟動服務。在centos6上就沒有這樣的功能,在centos6上如果rpcbind沒有啟動起來,則nfs服務是啟動不了的。

  5、在客戶端查看共享目錄以及掛載nfs共享出來的目錄

[root@test-node1 ~]#showmount -e 192.168.0.99
Export list for 192.168.0.99:
/nfsdir 192.168.0.0/24
[root@test-node1 ~]#mount 192.168.0.99:/nfsdir /mnt
[root@test-node1 ~]#df
Filesystem           1K-blocks     Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      51475068  4746696  44106932  10% /
tmpfs                   959580        0    959580   0% /dev/shm
/dev/sda1               487652    45302    416750  10% /boot
/dev/mapper/VolGroup-lv_home
                     251354900    69264 238510828   1% /home
192.168.0.99:/nfsdir  27560960 13935872  13625088  51% /mnt
[root@test-node1 ~]#

  說明:可看到nfs服務器共享出來的目錄在服務端是可以查看得到的,這里需要注意客戶端也需要安裝nfs-utils才可以用showmount命令來查看共享目錄。nfs同ftp服務不一樣,ftp共享出來的目錄,我們是看不出來它在服務器上的具體路徑,而nfs是可以看到的。

四、測試在客戶新建文件寫入數據,看服務端的本地目錄的變化

[root@test-node1 ~]#cd /mnt
[root@test-node1 mnt]#ls
[root@test-node1 mnt]#touch clientfile
touch: cannot touch `clientfile': Permission denied
[root@test-node1 mnt]#

  說明:默認我們配置的nfs是允許對寫,為什么在客戶端沒法創建文件呢?它告訴我們權限拒絕這到底是為什么呢?我們在服務器上用exportfs -v來查看nfs加載的配置內容

[root@test ~]# exportfs -v
/nfsdir         192.168.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
[root@test ~]# 

  說明:從上面的信息可以看到我們雖然配置了允許192.168.0.0/24這個網段的所有主機對其/nfsdir有讀寫權限(rw),但是它還有默認配置了root_squash,這個權限表示客戶端root賬號權限在NFS服務器上會壓榨,也就是說客戶端的root到NFS服務器上就不是root權限了,那客戶端的root到nfs服務器上是什么權限呢? 

[root@test ~]# chmod 777 /nfsdir
[root@test ~]# 

  說明:把NFS共享出來的目錄修改成777權限,然后在客戶端在新建文件看看新建的文件的所有者和組就可以知道客戶端的root到NFS服務器上是被映射成那個用戶了

[root@test-node1 ~]#cd /mnt
[root@test-node1 mnt]#ls
[root@test-node1 mnt]#touch rootfile
[root@test-node1 mnt]#ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 30 00:21 rootfile
[root@test-node1 mnt]#

  說明:可看到客戶端的root在共享目錄里新建的文件是nfsnobody的所有者和組,那么我們只需要在服務端把共享目錄的權限設置成nfsnobody這個用戶可讀寫權限,客戶端root就可在里面創建文件了。

[root@test ~]# chmod 755 /nfsdir
[root@test ~]# ll /nfsdir -d
drwxr-xr-x 2 root root 22 1月  30 00:21 /nfsdir
[root@test ~]# setfacl -m u:nfsnobody:rwx /nfsdir
[root@test ~]# getfacl /nfsdir
getfacl: Removing leading '/' from absolute path names
# file: nfsdir
# owner: root
# group: root
user::rwx
user:nfsnobody:rwx
group::r-x
mask::rwx
other::r-x

[root@test ~]# 

  說明:設置了/nfsdir這個共享目錄nfsnobody用戶有讀寫執行權限,那么客戶端的root用戶在共享目錄新建文件是否還有問題呢?

[root@test-node1 mnt]#pwd
/mnt
[root@test-node1 mnt]#touch rootfile2
[root@test-node1 mnt]#ll 
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 30 00:21 rootfile
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 30 00:27 rootfile2
[root@test-node1 mnt]#

  說明:可看到客戶端root用戶在共享目錄里是可以正常的創建文件的,那么客戶端普通用戶是否也可以呢?試一試

[root@test-node1 mnt]#su - qiuhom
[qiuhom@test-node1 ~]$cd /mnt
[qiuhom@test-node1 mnt]$ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 30 00:21 rootfile
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 30 00:27 rootfile2
[qiuhom@test-node1 mnt]$touch qiuhomfile
touch: cannot touch `qiuhomfile': Permission denied
[qiuhom@test-node1 mnt]$

  說明:客戶端的普通用戶為什么就不能在共享目錄里新建文件呢?我們在來看看服務端的配置

[root@test ~]# exportfs -v
/nfsdir         192.168.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
[root@test ~]# 

  說明:雖然服務端允許客戶端的root映射成nfsnobody,但是后面又設置了no_all_squash,這個權限表示所有的用戶不壓榨,也就是說root用戶壓榨,其他用戶不壓榨,也就說除root用戶,其他用戶都不映射成nfsnobody用戶。那么其他用戶在NFS服務端映射成那個用戶了呢?用上面的方法,把服務端共享的目錄修改成777權限,然后客戶端用普通用在其共享目錄新建文件,看看里面新建的文件的所有者和組就知道了

[root@test ~]# chmod 777 /nfsdir
[root@test ~]# 
==================================
[qiuhom@test-node1 mnt]$touch qiuhomfile
[qiuhom@test-node1 mnt]$ll
total 0
-rw-rw-r-- 1 qiuhom    qiuhom    0 Jan 30 00:35 qiuhomfile
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 30 00:21 rootfile
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 30 00:27 rootfile2
[qiuhom@test-node1 mnt]$

  說明:把服務端的共享目錄權限更改為777后客戶端的普通用在其里面新建文件的所有者和組都變成了客戶端的普通用名稱,但是這是在客戶端查看的信息是這樣的,我們在服務端查看是不是這樣的呢?

[root@test ~]# ll /nfsdir
總用量 0
-rw-rw-r-- 1       503       503 0 1月  30 00:35 qiuhomfile
-rw-r--r-- 1 nfsnobody nfsnobody 0 1月  30 00:21 rootfile
-rw-r--r-- 1 nfsnobody nfsnobody 0 1月  30 00:27 rootfile2
[root@test ~]# 

  說明:怎么在服務端查看剛才客戶端普通用戶新建的文件所有者和組是503呢?我們在來看看客戶端普通用戶的uid就明白了

[qiuhom@test-node1 mnt]$id qiuhom
uid=503(qiuhom) gid=503(qiuhom) groups=503(qiuhom)
[qiuhom@test-node1 mnt]$

  說明:是不是明白了,普通用在nfs服務端陪映射成以普通用戶uid和gid的用戶了,如果服務端有對應uid用戶和gid組,則服務端就會顯示器名稱,如果沒有則顯示器uid和gid,這個是服務端配置了root_squash和no_all_squash 是這樣的。

五、nfs配置文件格式說明

  1、nfs配置文件格式

  需要共享的目錄路徑    允許訪問的主機或網段1(選項)   主機或網段2(選項)……

  2、“#”井號表示注釋

  3、單個主機可以是ipv4地址,ipv6地址,FQDN(域名)

  4、網段支持兩種掩碼格式如:192.168.0.0/24  或者192.168.0.0/255.255.255.0

  5、通配符支持域名,不支持ip,如:*.test.com

  6、支持NIS域的主機組,如:@group_name

  7、anonymous:表示使用*通配所有客戶端

六、配置文件每個條目指定目錄導出到的哪些主機,及相關的權限和選項說明

  1、默認選項:(ro,sync,root_squash,no_all_squash)

  2、ro,rw 只讀和讀寫

  3、async 異步,數據變化后不立即寫磁盤,性能高

  4、sync(1.0.0后為默認)同步,數據在請求時立即寫入共享

  5、 no_all_squash (默認)保留共享文件的UID和GID

  6、 all_squash 所有遠程用戶(包括root)都變成nfsnobody

  7、 root_squash (默認)遠程root映射為nfsnobody,UID為65534,早期版本是4294967294 (nfsnobody)

  8、no_root_squash 遠程root映射成root用戶

  9、anonuid和anongid 指明匿名用戶映射為特定用戶UID和組GID,而非nfsnobody,可配合all_squash使用

七、NFS工具使用

  rpcinfo:查看rpc信息

qiuhom@test-node1 mnt]$rpcinfo -p 192.168.0.99
   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  20048  mountd
    100005    1   tcp  20048  mountd
    100024    1   udp  38042  status
    100024    1   tcp  45458  status
    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  37177  nlockmgr
    100021    3   udp  37177  nlockmgr
    100021    4   udp  37177  nlockmgr
    100021    1   tcp  40293  nlockmgr
    100021    3   tcp  40293  nlockmgr
    100021    4   tcp  40293  nlockmgr
[qiuhom@test-node1 mnt]$

  說明:-p選項指定查看指定服務器上rpcbind所注冊的端口信息,若-p后面不指定主機表示查看本機的端口注冊信息

[qiuhom@test-node1 mnt]$rpcinfo -s 192.168.0.99
   program version(s) netid(s)                         service     owner
    100000  2,3,4     local,udp,tcp,udp6,tcp6          portmapper  superuser
    100005  3,2,1     tcp6,udp6,tcp,udp                mountd      superuser
    100024  1         tcp6,udp6,tcp,udp                status      29
    100003  4,3       udp6,tcp6,udp,tcp                nfs         superuser
    100227  3         udp6,tcp6,udp,tcp                nfs_acl     superuser
    100021  4,3,1     tcp6,udp6,tcp,udp                nlockmgr    superuser
[qiuhom@test-node1 mnt]$

  說明:-s選項表示查看rpc注冊的程序,若不跟主機地址,則表示查看本機rpc注冊的程序信息

  exportfs:維護導出的NFS文件系統表

[root@test ~]# exportfs -v
/nfsdir         192.168.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
[root@test ~]

  說明:-v選項表示查看本機所有NFS共享詳情

[root@test ~]# cat /etc/exports
/nfsdir 192.168.0.0/24(rw)
[root@test ~]# cat > /etc/exports
/nfsdir *
^C
[root@test ~]# cat /etc/exports
/nfsdir *
[root@test ~]# exportfs -r
exportfs: No options for /nfsdir *: suggest *(sync) to avoid warning
[root@test ~]# exportfs -v
/nfsdir         <world>(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)
[root@test ~]# 

  說明:-r選項表示不重啟重讀配置文件,並共享目錄,如果配置文件中沒有明確寫明是同步寫磁盤還是異步寫磁盤,它重讀配置文件的時候它會警告我們建議加上sync

[root@test ~]# exportfs -v
/nfsdir         <world>(sync,wdelay,hide,no_subtree_check,sec=sys,ro,secure,root_squash,no_all_squash)
[root@test ~]# cat > /etc/exports
/nfsdir *(rw,all_squash)
^C
[root@test ~]# cat /etc/exports
/nfsdir *(rw,all_squash)
[root@test ~]# exportfs -a
[root@test ~]# exportfs -v
/nfsdir         <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,all_squash)
[root@test ~]# 

  說明:-a選項表示重讀配置並輸出本機所有共享,如果停止本機的共享,使用-a會恢復所有共享

[root@test ~]# exportfs -au
[root@test ~]# exportfs -v 
[root@test ~]# 

  說明:-au選項表示停止所有共享

  showmount -e hostname:查看指定主機上的共享目錄情況

[root@test ~]# showmount -e 192.168.0.99
Export list for 192.168.0.99:
/nfsdir *
[root@test ~]# showmount -e 
Export list for test:
/nfsdir *
[root@test ~]# 

  說明:-e后面若不跟主機地址,則表示查看本機的恭喜那個目錄情況

 八、客戶端NFS掛載說明

  基於安全考慮,建議使用nosuid,nodev,noexec掛載選項

  NFS相關的掛載選項:

    fg(默認)前台掛載,bg后台掛載

    hard(默認)持續請求,soft 非持續請求

    intr 和hard配合,請求可中斷

    rsize和wsize 一次讀和寫數據最大字節數,rsize=32768

    _netdev 無網絡不掛載

  示例:

    mount -o rw,nosuid,fg,hard,intr 172.16.0.1:/testdir /mnt/nfs/

  開機掛載:編輯/etc/fstab文件在文件最后添加,如:172.16.0.1:/public /mnt/nfs nfs defaults 0 0

九、NFS實現偽根

  1、創建目錄

[root@test ~]# mkdir /root/dir{1,2} -pv
mkdir: 已創建目錄 "/root/dir1"
mkdir: 已創建目錄 "/root/dir2"
[root@test ~]# mkdir /test/dir{1,2} -pv
mkdir: 已創建目錄 "/test"
mkdir: 已創建目錄 "/test/dir1"
mkdir: 已創建目錄 "/test/dir2"
[root@test ~]# 

  2、編寫/etc/fstab文件,在其文件中填寫如下內容

[root@test ~]# tail -n 2 /etc/fstab
/root/dir1 /test/dir1 none bind 0 0
/root/dir2 /test/dir2 none bind 0 0
[root@test ~]# 

  3、編寫/etc/exports文件,在其文件中添加如下內容

[root@test ~]# cat /etc/exports
/test *(fsid=0,rw,crossmnt)
/test/dir1 192.168.0.0/24(rw,all_squash)
/test/dir2 192.168.0.0/24(rw,all_squash)
[root@test ~]# 

  4、掛載所有目錄,並重讀NFS配置

[root@test ~]# mount -a
[root@test ~]# mount |tail -n 2
/dev/mapper/centos-root on /test/dir1 type xfs (rw,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /test/dir2 type xfs (rw,relatime,attr2,inode64,noquota)
[root@test ~]# exportfs -r
[root@test ~]# exportfs -v
/test/dir1      192.168.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,all_squash)
/test/dir2      192.168.0.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,all_squash)
/test           <world>(sync,wdelay,hide,crossmnt,no_subtree_check,fsid=0,sec=sys,rw,secure,root_squash,no_all_squash)
[root@test ~]# 

  說明:到此服務端的配置就完成了,接下來在客戶端掛載即可

  5、客戶端掛載偽根

[root@test-node1 ~]#mkdir xxx
[root@test-node1 ~]#mount 192.168.0.99:/ ./xxx/
[root@test-node1 ~]#df
df: `/mnt': Stale file handle
Filesystem           1K-blocks     Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      51475068  4746736  44106892  10% /
tmpfs                   959580        0    959580   0% /dev/shm
/dev/sda1               487652    45302    416750  10% /boot
/dev/mapper/VolGroup-lv_home
                     251354900    69268 238510824   1% /home
192.168.0.99:/        27560960 13935360  13625600  51% /root/xxx
[root@test-node1 ~]#ls xxx/
dir1  dir2
[root@test-node1 ~]#

  說明:如果客戶端需要實現開機掛載,則需要在/etc/fstab下寫明掛載關系和掛載選項即可,如:192.168.0.99:/ /root/xxx/ nfs ro 0 0

十、配置NFS使用固定端口

  編輯配置文件/etc/sysconfig/nfs在其最后添加RQUOTAD_PORT=875(端口可以根據實際情況來定)修改后的文件如下幾項內容

[root@test ~]# grep  "PORT" /etc/sysconfig/nfs
LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
STATD_PORT=662
STATD_OUTGOING_PORT=2020
RQUOTAD_PORT=875
[root@test ~]# 

  說明:除了以上端口外,防火牆配置還需要開放tcp和udp的111端口和2049端口共4個端口。配置了以上配置文件后,然后重啟nfs-server服務,nfs注冊的端口就不會變了,如下所示

[root@test ~]# systemctl restart nfs-server
[root@test ~]# 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  38042  status
    100024    1   tcp  45458  status
    100005    1   udp    892  mountd
    100005    1   tcp    892  mountd
    100005    2   udp    892  mountd
    100005    2   tcp    892  mountd
    100005    3   udp    892  mountd
    100005    3   tcp    892  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  32769  nlockmgr
    100021    3   udp  32769  nlockmgr
    100021    4   udp  32769  nlockmgr
    100021    1   tcp  32803  nlockmgr
    100021    3   tcp  32803  nlockmgr
    100021    4   tcp  32803  nlockmgr
[root@test ~]# systemctl restart nfs-server
[root@test ~]# 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  38042  status
    100024    1   tcp  45458  status
    100005    1   udp    892  mountd
    100005    1   tcp    892  mountd
    100005    2   udp    892  mountd
    100005    2   tcp    892  mountd
    100005    3   udp    892  mountd
    100005    3   tcp    892  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  32769  nlockmgr
    100021    3   udp  32769  nlockmgr
    100021    4   udp  32769  nlockmgr
    100021    1   tcp  32803  nlockmgr
    100021    3   tcp  32803  nlockmgr
    100021    4   tcp  32803  nlockmgr
[root@test ~]# 

十一、NFS實現自動掛載

  1、客戶端安裝autofs軟件包

[root@test-centos7-node1 ~]# yum info autofs
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
Name        : autofs
Arch        : x86_64
Epoch       : 1
Version     : 5.0.7
Release     : 69.el7
Size        : 808 k
Repo        : my_base/7/x86_64
Summary     : A tool for automatically mounting and unmounting filesystems
License     : GPLv2+
Description : autofs is a daemon which automatically mounts filesystems when you use
            : them, and unmounts them later when you are not using them.  This can
            : include network filesystems, CD-ROMs, floppies, and so forth.

[root@test-centos7-node1 ~]# yum install -y autofs
Loaded plugins: fastestmirror
epel                                                                                                     | 5.3 kB  00:00:00     
my_base                                                                                                  | 3.6 kB  00:00:00     
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package autofs.x86_64 1:5.0.7-69.el7 will be installed
--> Processing Dependency: libtirpc.so.1()(64bit) for package: 1:autofs-5.0.7-69.el7.x86_64
--> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-69.el7.x86_64
--> Running transaction check
---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed
---> Package libtirpc.x86_64 0:0.2.4-0.10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================
 Package                      Arch                       Version                              Repository                   Size
================================================================================================================================
Installing:
 autofs                       x86_64                     1:5.0.7-69.el7                       my_base                     808 k
Installing for dependencies:
 hesiod                       x86_64                     3.2.1-3.el7                          my_base                      30 k
 libtirpc                     x86_64                     0.2.4-0.10.el7                       my_base                      88 k

Transaction Summary
================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 927 k
Installed size: 5.3 M
Downloading packages:
(1/3): hesiod-3.2.1-3.el7.x86_64.rpm                                                                     |  30 kB  00:00:00     
(2/3): libtirpc-0.2.4-0.10.el7.x86_64.rpm                                                                |  88 kB  00:00:00     
(3/3): autofs-5.0.7-69.el7.x86_64.rpm                                                                    | 808 kB  00:00:00     
--------------------------------------------------------------------------------------------------------------------------------
Total                                                                                           1.9 MB/s | 927 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : libtirpc-0.2.4-0.10.el7.x86_64                                                                               1/3 
  Installing : hesiod-3.2.1-3.el7.x86_64                                                                                    2/3 
  Installing : 1:autofs-5.0.7-69.el7.x86_64                                                                                 3/3 
  Verifying  : 1:autofs-5.0.7-69.el7.x86_64                                                                                 1/3 
  Verifying  : hesiod-3.2.1-3.el7.x86_64                                                                                    2/3 
  Verifying  : libtirpc-0.2.4-0.10.el7.x86_64                                                                               3/3 

Installed:
  autofs.x86_64 1:5.0.7-69.el7                                                                                                  

Dependency Installed:
  hesiod.x86_64 0:3.2.1-3.el7                                  libtirpc.x86_64 0:0.2.4-0.10.el7                                 

Complete!
[root@test-centos7-node1 ~]# 

  說明:從上面的信息可以知道autofs這個包的主要功能就是自動掛載和卸載文件系統的工具

  2、查看autofs的配置文件

[root@test-centos7-node1 ~]# rpm -ql autofs|grep etc
/etc/auto.master
/etc/auto.master.d
/etc/auto.misc
/etc/auto.net
/etc/auto.smb
/etc/autofs.conf
/etc/autofs_ldap_auth.conf
/etc/sysconfig/autofs
[root@test-centos7-node1 ~]# 

  說明:可以看到autofs的組配置文件是/etc/auto.master  當然也可以把配置文件放到/etc/auto.master.d/下

  3、查看主配置文件中定義的內容

[root@test-centos7-node1 ~]# cat /etc/auto.master
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc   /etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
#       "nosuid" and "nodev" options unless the "suid" and "dev"
#       options are explicitly given.
#
/net    -hosts
#
# Include /etc/auto.master.d/*.autofs
# The included files must conform to the format of this file.
#
+dir:/etc/auto.master.d
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
[root@test-centos7-node1 ~]# 

  說明:以上主配置文件定義了/misc 目錄下的子目錄掛載策略的子配置文件是/etc/auto.misc。我們可以這樣理解,/etc/auto.master這個文件定義了掛載點的父目錄位置,以及該父目錄下面的子目錄掛載策略的配置文件存放路徑和名稱。也就是說我們要實現自動掛載,我們需要把掛載點分成兩部分,第一部分是掛載點的父目錄在哪里,這個需要在/etc/auto.master里面定,除此之外,我們還需要在/etc/master下面定義掛載點的父目錄下的子目錄掛載策略的配置文件的路徑以及名稱。當然這個路徑和名稱可以根據自己的情況來寫,通常情況下我們放在/etc/下面。了解了/etc/auto.master的配置文件規則,我們可以清楚的知道/misc 這個目錄下的子目錄掛載規則是存放在/etc/auto.misc里,接下來我們再來看看這個子配置文件

[root@test-centos7-node1 ~]# cat /etc/auto.misc 
#
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage

cd              -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

# the following entries are samples to pique your imagination
#linux          -ro,soft,intr           ftp.example.org:/pub/linux
#boot           -fstype=ext2            :/dev/hda1
#floppy         -fstype=auto            :/dev/fd0
#floppy         -fstype=ext2            :/dev/fd0
#e2floppy       -fstype=ext2            :/dev/fd0
#jaz            -fstype=ext2            :/dev/sdc1
#removable      -fstype=ext2            :/dev/hdd
[root@test-centos7-node1 ~]# 

  說明:這個之配置文件除了注釋以外就只有一行,從上面的描述,我們不難理解子配置文件中的這個配置是什么意思。結合上面說的 這個之配置文件是/misc目錄下的子目錄掛載策略的配置。以上配置表示在/misc/cd目錄掛載對應的設備上 /dev/cdrom 文件系統是iso9660 以只讀,nosuid,nodev選項掛載。看了以上的配置我們就很容易理解,centos7上的"神奇的目錄",之所以我們在不訪問/misc/cd 的時候是看不到掛載的,一旦訪問就有掛載自動掛上去了 ,它的實現原理就是通過autofs自動掛載實現的。理解了上面的配置我們就可以寫自己的掛載策略實現自動掛載

  4、NFS自動掛載實現

    4.1相對路徑法

    首先解釋下說明叫相對路徑法,相對路徑就是我們上面說的/misc/cd 這個掛載點例子,它是把掛載點分成了了basename和dirname兩部分來配置的,所以說在組配置文件中(/etc/auto.master)需要定義dirname路徑以及basename子目錄掛載策略的配置文件路徑和名稱。然后在對應的之配置文件中寫清楚掛載策略和子目錄的關系即可

    將192.168.0.99:/nfsdir  自動掛載到本機的/auto/nfs/目錄下

    4.1.1、編輯組配置文件定義父目錄 和子目錄配置文件的路徑和名稱

[root@test-centos7-node1 ~]# grep nfs /etc/auto.master
/auto   /etc/nfs_auto.txt
[root@test-centos7-node1 ~]# 

  說明:以上內容是我們自己行家掛載點父目錄和子目錄配置文件的路徑和名稱,這個子配置文件的名稱沒有什么要求,可以隨意寫。

    4.1.2、在對應子配置文件中寫子目錄掛載策略

[root@test-centos7-node1 ~]# cat /etc/nfs_auto.txt
nfs   -fstype=nfs,rw,vers=3   192.168.0.99:/nfsdir
[root@test-centos7-node1 ~]# 

  說明:配置好子目錄的掛載策略后就實現了自動掛載。這里還需要注意一點的是,我們在客戶端實現自動掛載的前提是NFS可以在客戶端掛載,如果客戶端都看不到NFS共享出來的目錄,當然也不能實現自動掛載

    4.1.3、在客戶端查看是否能夠看到NFS共享出來的目錄

[root@test-centos7-node1 ~]# showmount -e 192.168.0.99
Export list for 192.168.0.99:
/test      *
/test/dir2 192.168.0.0/24
/test/dir1 192.168.0.0/24
/nfsdir    192.168.0.0/24
[root@test-centos7-node1 ~]# 

  說明:可看到在客戶端是可以看到服務端的nfs共享的目錄,接下來我們啟動autofs服務查看掛載情況

[root@test-centos7-node1 ~]# systemctl status autofs
● autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@test-centos7-node1 ~]# systemctl start autofs
[root@test-centos7-node1 ~]# systemctl status autofs
● autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-01-28 18:13:19 CST; 1min 21s ago
  Process: 3862 ExecStart=/usr/sbin/automount $OPTIONS --pid-file /run/autofs.pid (code=exited, status=0/SUCCESS)
 Main PID: 3864 (automount)
   CGroup: /system.slice/autofs.service
           └─3864 /usr/sbin/automount --pid-file /run/autofs.pid

Jan 28 18:13:17 test-centos7-node1 automount[3864]: master_add_map_source: map source used without taking reference
Jan 28 18:13:17 test-centos7-node1 automount[3864]: master_add_map_source: map source used without taking reference
Jan 28 18:13:17 test-centos7-node1 automount[3864]: open_sss_lib:78: lookup(sss): failed to open /usr/lib64/sssd/modules...ctory
Jan 28 18:13:19 test-centos7-node1 automount[3864]: problem reading master map, maximum wait exceeded
Jan 28 18:13:19 test-centos7-node1 automount[3864]: /usr/sbin/automount: warning: could not read at least one map source...g ...
Jan 28 18:13:19 test-centos7-node1 automount[3864]: master_add_map_source: map source used without taking reference
Jan 28 18:13:19 test-centos7-node1 automount[3864]: master_add_map_source: map source used without taking reference
Jan 28 18:13:19 test-centos7-node1 automount[3864]: master_add_map_source: map source used without taking reference
Jan 28 18:13:19 test-centos7-node1 automount[3864]: open_sss_lib:78: lookup(sss): failed to open /usr/lib64/sssd/modules...ctory
Jan 28 18:13:19 test-centos7-node1 systemd[1]: Started Automounts filesystems on demand.
Hint: Some lines were ellipsized, use -l to show in full.
[root@test-centos7-node1 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       48209924 3116852  45093072   7% /
devtmpfs         1922828       0   1922828   0% /dev
tmpfs            1932652      20   1932632   1% /dev/shm
tmpfs            1932652    8800   1923852   1% /run
tmpfs            1932652       0   1932652   0% /sys/fs/cgroup
/dev/sda1        2086912  137672   1949240   7% /boot
tmpfs             386532       0    386532   0% /run/user/0
[root@test-centos7-node1 ~]# 

  說明:我們啟動autofs后,我們定義的自動掛載好像沒有自動掛載到對應的目錄。這個時候我們來直接訪問我們定義的掛載點看看有什么情況

[root@test-centos7-node1 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       48209924 3116816  45093108   7% /
devtmpfs         1922828       0   1922828   0% /dev
tmpfs            1932652      20   1932632   1% /dev/shm
tmpfs            1932652    8800   1923852   1% /run
tmpfs            1932652       0   1932652   0% /sys/fs/cgroup
/dev/sda1        2086912  137672   1949240   7% /boot
tmpfs             386532       0    386532   0% /run/user/0
[root@test-centos7-node1 ~]# ls /
auto  boot  etc   lib    media  mnt  opt    proc  run   srv  tmp  var
bin   dev   home  lib64  misc   net  patch  root  sbin  sys  usr  www
[root@test-centos7-node1 ~]# ls /auto/
[root@test-centos7-node1 ~]# cd /auto/nfs
[root@test-centos7-node1 nfs]# ll
total 0
[root@test-centos7-node1 nfs]# df
Filesystem           1K-blocks     Used Available Use% Mounted on
/dev/sda3             48209924  3116996  45092928   7% /
devtmpfs               1922828        0   1922828   0% /dev
tmpfs                  1932652       20   1932632   1% /dev/shm
tmpfs                  1932652     8804   1923848   1% /run
tmpfs                  1932652        0   1932652   0% /sys/fs/cgroup
/dev/sda1              2086912   137672   1949240   7% /boot
tmpfs                   386532        0    386532   0% /run/user/0
192.168.0.99:/nfsdir  27560960 13929472  13631488  51% /auto/nfs
[root@test-centos7-node1 nfs]# 

  說明:可看到我們去訪問我們定義的掛載點,就能夠立即將NFS給掛載到系統上,其實過上一段時間我們不訪問我們的掛載點,它還會自動卸載掉,我們一旦去訪問掛載點,它又會立刻觸發掛載,把對應的資源給掛載到系統上;這里再說一下,如果子配置文件的掛載目錄和NFS的共享目錄相同 ,我們可以用*來通配,如:NFS服務器共享的目錄是192.168.0.99:/nfsdir   客戶端掛載的目錄為/misc/nfsdir  這種掛載點的basename和nfs服務器上共享的目錄的basename相同時。我們在子配置文件中還可以寫成  *    -fstype=nfs,rw,vers=3   192.168.0.99:/&

[root@test-centos7-node1 ~]# cat /etc/nfs_auto.txt
*   -fstype=nfs,rw,vers=3   192.168.0.99:/&
[root@test-centos7-node1 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       48209924 3117020  45092904   7% /
devtmpfs         1922828       0   1922828   0% /dev
tmpfs            1932652      20   1932632   1% /dev/shm
tmpfs            1932652    8776   1923876   1% /run
tmpfs            1932652       0   1932652   0% /sys/fs/cgroup
/dev/sda1        2086912  137672   1949240   7% /boot
tmpfs             386532       0    386532   0% /run/user/0
[root@test-centos7-node1 ~]# cd /auto/nfsdir
[root@test-centos7-node1 nfsdir]# df
Filesystem           1K-blocks     Used Available Use% Mounted on
/dev/sda3             48209924  3117020  45092904   7% /
devtmpfs               1922828        0   1922828   0% /dev
tmpfs                  1932652       20   1932632   1% /dev/shm
tmpfs                  1932652     8776   1923876   1% /run
tmpfs                  1932652        0   1932652   0% /sys/fs/cgroup
/dev/sda1              2086912   137672   1949240   7% /boot
tmpfs                   386532        0    386532   0% /run/user/0
192.168.0.99:/nfsdir  27560960 13929472  13631488  51% /auto/nfsdir
[root@test-centos7-node1 nfsdir]# 

  說明:這種寫法我們在訪問其掛載點的時候,需要清楚知道服務端共享目錄的名稱才可以,這種寫法是可以匹配多個共享目錄的,只要掛載點和NFS服務器共享目錄路徑有相同的,都可以匹配上。如下所示

[root@test-centos7-node1 ~]# showmount -e 192.168.0.99
Export list for 192.168.0.99:
/test      *
/test/dir2 192.168.0.0/24
/test/dir1 192.168.0.0/24
/nfsdir    192.168.0.0/24
[root@test-centos7-node1 ~]# df
Filesystem           1K-blocks     Used Available Use% Mounted on
/dev/sda3             48209924  3116956  45092968   7% /
devtmpfs               1922828        0   1922828   0% /dev
tmpfs                  1932652       20   1932632   1% /dev/shm
tmpfs                  1932652     8776   1923876   1% /run
tmpfs                  1932652        0   1932652   0% /sys/fs/cgroup
/dev/sda1              2086912   137672   1949240   7% /boot
tmpfs                   386532        0    386532   0% /run/user/0
192.168.0.99:/nfsdir  27560960 13929728  13631232  51% /auto/nfsdir
[root@test-centos7-node1 ~]# cd /auto/test/
[root@test-centos7-node1 test]# df
Filesystem           1K-blocks     Used Available Use% Mounted on
/dev/sda3             48209924  3116956  45092968   7% /
devtmpfs               1922828        0   1922828   0% /dev
tmpfs                  1932652       20   1932632   1% /dev/shm
tmpfs                  1932652     8776   1923876   1% /run
tmpfs                  1932652        0   1932652   0% /sys/fs/cgroup
/dev/sda1              2086912   137672   1949240   7% /boot
tmpfs                   386532        0    386532   0% /run/user/0
192.168.0.99:/nfsdir  27560960 13929472  13631488  51% /auto/nfsdir
192.168.0.99:/test    27560960 13929472  13631488  51% /auto/test
[root@test-centos7-node1 test]# ls
dir1  dir2
[root@test-centos7-node1 test]# cd /auto/test/dir2
[root@test-centos7-node1 dir2]# df
Filesystem              1K-blocks     Used Available Use% Mounted on
/dev/sda3                48209924  3116956  45092968   7% /
devtmpfs                  1922828        0   1922828   0% /dev
tmpfs                     1932652       20   1932632   1% /dev/shm
tmpfs                     1932652     8776   1923876   1% /run
tmpfs                     1932652        0   1932652   0% /sys/fs/cgroup
/dev/sda1                 2086912   137672   1949240   7% /boot
tmpfs                      386532        0    386532   0% /run/user/0
192.168.0.99:/test       27560960 13929472  13631488  51% /auto/test
192.168.0.99:/test/dir2  27560960 13929472  13631488  51% /auto/test/dir2
[root@test-centos7-node1 dir2]# 

  4.2、絕對路徑法

  絕對路徑法就是在主配置文件中(/etc/auto.master)文件中用/-來表示不再主配置文件中定義父目錄名稱,然后在后面指定子配置文件的路徑和名稱即可,也就是說和相對路徑法不同的是,把絕對路徑和掛載策略都寫在子配置文件中。

  接下來我們把192.168.0.99:/ 掛載到本地的 /a/b/c/d/目錄下

    4.2.1、編輯主配置文件

[root@test-centos7-node1 ~]# grep "path" /etc/auto.master
/-      /etc/mount_all_path.txt
[root@test-centos7-node1 ~]# 

    4.2.2、在子配置文件中寫掛載策略

[root@test-centos7-node1 ~]# cat /etc/mount_all_path.txt
/a/b/c/d/       -fstype=nfs,rw,all_squash       192.168.0.99:/
[root@test-centos7-node1 ~]# 

  說明:我們寫好子配置文件后,是需要重啟autofs服務。然后通過訪問掛載點,看看它是否能夠掛載NFS共享出來的目錄呢

[root@test-centos7-node1 ~]# systemctl restart autofs
[root@test-centos7-node1 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       48209924 3116996  45092928   7% /
devtmpfs         1922828       0   1922828   0% /dev
tmpfs            1932652      20   1932632   1% /dev/shm
tmpfs            1932652    8776   1923876   1% /run
tmpfs            1932652       0   1932652   0% /sys/fs/cgroup
/dev/sda1        2086912  137672   1949240   7% /boot
tmpfs             386532       0    386532   0% /run/user/0
[root@test-centos7-node1 ~]# ls /a/b/c/d/
dir1  dir2
[root@test-centos7-node1 ~]# df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda3       48209924  3116996  45092928   7% /
devtmpfs         1922828        0   1922828   0% /dev
tmpfs            1932652       20   1932632   1% /dev/shm
tmpfs            1932652     8776   1923876   1% /run
tmpfs            1932652        0   1932652   0% /sys/fs/cgroup
/dev/sda1        2086912   137672   1949240   7% /boot
tmpfs             386532        0    386532   0% /run/user/0
192.168.0.99:/  27560960 13929472  13631488  51% /a/b/c/d
[root@test-centos7-node1 ~]# 

  說明:我們通過訪問掛載點也實現了自動掛載。其實autofs對自動掛載NFS提前有在主配置文件中定義,我們可以不用任何配置,裝好autofs軟件包后,直接cd NFS服務器的主機地址就可實現自動掛載

[root@test-centos7-node1 ~]# grep ^"\/net" /etc/auto.master
/net    -hosts
[root@test-centos7-node1 ~]# 

  說明:這個配置就是表示我們在通過網絡掛載資源的時候,可以直接訪問/net/對方ip地址即可,當然這種都是用在局域網里,NFS不建議用在互聯網。

[root@test-centos7-node1 ~]# umount /a/b/c/d/
[root@test-centos7-node1 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       48209924 3116948  45092976   7% /
devtmpfs         1922828       0   1922828   0% /dev
tmpfs            1932652      20   1932632   1% /dev/shm
tmpfs            1932652    8776   1923876   1% /run
tmpfs            1932652       0   1932652   0% /sys/fs/cgroup
/dev/sda1        2086912  137672   1949240   7% /boot
tmpfs             386532       0    386532   0% /run/user/0
[root@test-centos7-node1 ~]# cd 192.168.0.99
-bash: cd: 192.168.0.99: No such file or directory
[root@test-centos7-node1 ~]# ls /net/
[root@test-centos7-node1 ~]# ls /net/192.168.0.99
nfsdir  test
[root@test-centos7-node1 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       48209924 3116952  45092972   7% /
devtmpfs         1922828       0   1922828   0% /dev
tmpfs            1932652      20   1932632   1% /dev/shm
tmpfs            1932652    8776   1923876   1% /run
tmpfs            1932652       0   1932652   0% /sys/fs/cgroup
/dev/sda1        2086912  137672   1949240   7% /boot
tmpfs             386532       0    386532   0% /run/user/0
[root@test-centos7-node1 ~]# 

  說明:這種方式用df命令是看不到它的掛載情況的,我們需要用mount命令是可以查看到掛載詳情。如下所示

[root@test-centos7-node1 ~]# mount |tail 
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=386532k,mode=700)
/etc/auto.misc on /misc type autofs (rw,relatime,fd=7,pgrp=4091,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=28437)
/etc/nfs_auto.txt on /auto type autofs (rw,relatime,fd=13,pgrp=4091,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=29313)
/etc/mount_all_path.txt on /a/b/c/d type autofs (rw,relatime,fd=19,pgrp=4091,timeout=300,minproto=5,maxproto=5,direct,pipe_ino=26610)
-hosts on /net type autofs (rw,relatime,fd=24,pgrp=4091,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=26615)
-hosts on /net/192.168.0.99/nfsdir type autofs (rw,relatime,fd=24,pgrp=4091,timeout=300,minproto=5,maxproto=5,offset,pipe_ino=26615)
-hosts on /net/192.168.0.99/test type autofs (rw,relatime,fd=24,pgrp=4091,timeout=300,minproto=5,maxproto=5,offset,pipe_ino=26615)
[root@test-centos7-node1 ~]# 

  說明:以上的所有操作都建立在客戶端上的操作,且客戶端是能夠看到其NFS服務器上的共享目錄的前提才可以實現自動掛載。


免責聲明!

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



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