NFS服務搭建與配置

NFS介紹

NFS是Network File System的縮寫

NFS最早由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發,最新爲4.1版本

NFS數據傳輸基於RPC協議,RPC爲Remote Procedure Call的簡寫。

NFS應用場景是:A,B,C三臺機器上需要保證被訪問到的文件是一樣的,A共享數據出來,B和C分別去掛載A共享的數據目錄,從而B和C訪問到的數據和A上的一致

總結:NFC服務需要藉助RPC協議實現通信。

NFS服務端安裝配置

實驗需要2臺機器,一臺作爲服務端,一臺作爲客戶端。

服務端,安裝2個包nfs-utils和rpcbind

  [root@zyshanlinux-001 ~]# yum install -y nfs-utils rpcbind
  
  Installed:
    nfs-utils.x86_64 1:1.3.0-0.54.el7                               rpcbind.x86_64 0:0.2.0-44.el7 

客戶端,安裝包nfs-utils

  [root@zyshanlinux-02 ~]# yum install -y nfs-utils
  
  Installed:
    nfs-utils.x86_64 1:1.3.0-0.54.el7 

配置文件,允許共享主機IP

  [root@zyshanlinux-001 ~]# vim /etc/exports

配置內容,就一行

  /home/nfstestdir 192.168.106.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

保存配置文件後,執行如下準備操作

首先要創建分享的目錄,給創建的目錄賦予777的權限。

  [root@zyshanlinux-001 ~]# mkdir /home/nfstestdir
  [root@zyshanlinux-001 ~]# chmod 777 /home/nfstestdir

服務端啓動rpcbind前後監聽端口情況

  [root@zyshanlinux-001 ~]# netstat -lnpt
  Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1254/nginx: master  
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1086/sshd           
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1325/master         
  tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1254/nginx: master  
  tcp6       0      0 :::22                   :::*                    LISTEN      1086/sshd           
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1325/master         
  tcp6       0      0 :::3306                 :::*                    LISTEN      1447/mysqld         
  [root@zyshanlinux-001 ~]# systemctl start rpcbind
  [root@zyshanlinux-001 ~]# netstat -lnpt
  Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
  tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      46926/rpcbind       
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1254/nginx: master  
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1086/sshd           
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1325/master         
  tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1254/nginx: master  
  tcp6       0      0 :::111                  :::*                    LISTEN      46926/rpcbind       
  tcp6       0      0 :::22                   :::*                    LISTEN      1086/sshd           
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1325/master         
  tcp6       0      0 :::3306                 :::*                    LISTEN      1447/mysqld 

客戶端啓動rpcbind前後監聽端口情況

  [root@zyshanlinux-02 ~]# netstat -lntp
  Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      878/sshd            
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1113/master         
  tcp6       0      0 :::22                   :::*                    LISTEN      878/sshd            
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1113/master         
  [root@zyshanlinux-02 ~]# ps aux |grep rpc
  root     21597  0.0  0.0 112660   964 pts/0    R+   20:38   0:00 grep --color=auto rpc
  [root@zyshanlinux-02 ~]# systemctl start rpcbind
  [root@zyshanlinux-02 ~]# netstat -lntp
  Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
  tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      22898/rpcbind       
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      878/sshd            
  tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1113/master         
  tcp6       0      0 :::111                  :::*                    LISTEN      22898/rpcbind       
  tcp6       0      0 :::22                   :::*                    LISTEN      878/sshd            
  tcp6       0      0 ::1:25                  :::*                    LISTEN      1113/master         
  [root@zyshanlinux-02 ~]# ps aux |grep rpc
  rpc      22898  0.0  0.0  64956  1048 ?        Ss   20:39   0:00 /sbin/rpcbind -w
  root     23120  0.0  0.0 112660   968 pts/0    R+   20:39   0:00 grep --color=auto rpc
  

啓動NFS

  [root@zyshanlinux-001 ~]# systemctl start nfs
  [root@zyshanlinux-001 ~]# ps aux |grep nfs
  root     51317  0.0  0.0      0     0 ?        S<   20:42   0:00 [nfsd4_callbacks]
  root     51323  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51324  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51325  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51326  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51327  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51328  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51329  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51330  0.0  0.0      0     0 ?        S    20:42   0:00 [nfsd]
  root     51574  0.0  0.0 112704   960 pts/0    R+   20:42   0:00 grep --color=auto nfs
  [root@zyshanlinux-001 ~]# ps aux |grep rpc
  rpc      46926  0.0  0.0  69220  1428 ?        Ss   20:38   0:00 /sbin/rpcbind -w
  root     51289  0.0  0.0      0     0 ?        S<   20:42   0:00 [rpciod]
  rpcuser  51290  0.0  0.0  42420  1752 ?        Ss   20:42   0:00 /usr/sbin/rpc.statd
  root     51307  0.0  0.0  42608   940 ?        Ss   20:42   0:00 /usr/sbin/rpc.mountd
  root     51308  0.0  0.0  45924   540 ?        Ss   20:42   0:00 /usr/sbin/rpc.idmapd
  root     52778  0.0  0.0 112704   956 pts/0    R+   20:43   0:00 grep --color=auto rpc
  

服務端開機啓動NFS

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

NFS配置選項

  [root@zyshanlinux-001 ~]# cat /etc/exports
  /home/nfstestdir 192.168.106.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

rw 讀寫 ro 只讀 sync 同步模式,內存數據實時寫入磁盤 async 非同步模式 no_root_squash 客戶端掛載NFS共享目錄後,root用戶不受約束,權限很大 root_squash 與上面選項相對,客戶端上的root用戶收到約束,被限定成某個普通用戶 all_squash 客戶端上所有用戶在使用NFS共享目錄時都被限定爲一個普通用戶 anonuid/anongid 和上面幾個選項搭配使用,定義被限定用戶的uid和gid

##客戶端連接服務端的IP

  [root@zyshanlinux-02 ~]# showmount -e 192.168.106.128
  clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

報錯,排除是網絡不通的問題,那就需要關閉防火牆,服務端客戶端都關閉。

服務端:

1、關閉防火牆 2、關閉SELinux,但服務端已經關閉。

  [root@zyshanlinux-001 ~]# systemctl stop firewalld
  [root@zyshanlinux-001 ~]# setenforce 0
  setenforce: SELinux is disabled

客戶端:

1、關閉防火牆 2、關閉SELinux

  [root@zyshanlinux-02 ~]# systemctl stop firewalld
  [root@zyshanlinux-02 ~]# getenforce
  Enforcing
  [root@zyshanlinux-02 ~]# setenforce 0

連接服務端IP成功

  [root@zyshanlinux-02 ~]# showmount -e 192.168.106.128
  Export list for 192.168.106.128:
  /home/nfstestdir 192.168.106.0/24

掛載服務端共享的目錄,用df -h測試,掛載成功

  [root@zyshanlinux-02 ~]# mount -t nfs 192.168.106.128:/home/nfstestdir /mnt
  [root@zyshanlinux-02 ~]# df -h
  Filesystem                        Size  Used Avail Use% Mounted on
  /dev/sda3                          28G 1011M   27G   4% /
  devtmpfs                          907M     0  907M   0% /dev
  tmpfs                             916M     0  916M   0% /dev/shm
  tmpfs                             916M  8.8M  908M   1% /run
  tmpfs                             916M     0  916M   0% /sys/fs/cgroup
  /dev/sda1                         197M  113M   85M  58% /boot
  tmpfs                             184M     0  184M   0% /run/user/0
  192.168.106.128:/home/nfstestdir   28G  7.3G   21G  27% /mnt

由於配置文件上設置了屬主和屬組,可以看到文件的屬主和屬組都爲1000

客戶端:創建文件,創建的文件的屬主和屬組都爲1000,由於沒有該用戶,都用1000代替

  [root@zyshanlinux-02 mnt]# touch zyshanlinux.111
  [root@zyshanlinux-02 mnt]# ls -l
  total 0
  -rw-r--r--. 1 1000 1000 0 Jul 15 21:20 zyshanlinux.111
  [root@zyshanlinux-02 mnt]# id 1000
  id: 1000: no such user

服務端:客戶端創建的文件在服務端查看,屬主是user1,屬組是1000

  [root@zyshanlinux-001 ~]# ls -l /home/nfstestdir
  total 0
  -rw-r--r-- 1 user1 1000 0 Jul 15 21:20 zyshanlinux.111
  [root@zyshanlinux-001 ~]# id user1
  uid=1000(user1) gid=1001(user1) groups=1001(user1)

exportfs命令

如果要服務端要關閉或重啓NFS,需要先把客戶端掛載服務端的目錄先卸載

  [root@zyshanlinux-02 mnt]# umount /mnt
  umount.nfs4: /mnt: device is busy
  [root@zyshanlinux-02 mnt]# cd 
  [root@zyshanlinux-02 ~]# umount /mnt
  [root@zyshanlinux-02 ~]# 

由於服務端不能隨意關閉或重啓nfs,會導致客戶端正在掛載的目錄讀寫會出現問題。引入了exportfs命令,不許重啓NFS服務,配置文件也會生效。

修改配置文件

  [root@zyshanlinux-001 ~]# !vi
  vim /etc/exports

新增配置內容,記得IP是允許的客戶端IP

  /home/nfstestdir 192.168.106.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
  /tmp 192.168.106.130(rw,sync,no_root_squash)

服務端,全部共享目錄重新掛載並顯示,不用重啓nfs服務,配置文件就會生效

  [root@zyshanlinux-001 ~]# exportfs -arv
  exporting 192.168.106.130:/tmp
  exporting 192.168.106.0/24:/home/nfstestdir

客戶端也生效了。

  [root@zyshanlinux-02 ~]# !showm
  showmount -e 192.168.106.128
  Export list for 192.168.106.128:
  /home/nfstestdir 192.168.106.0/24
  /tmp             192.168.106.130

客戶端掛載

  [root@zyshanlinux-02 ~]# mount -t nfs 192.168.106.128:/tmp/ /mnt/
  [root@zyshanlinux-02 ~]# cd /mnt
  [root@zyshanlinux-02 mnt]# ls
  mysql2.sql               systemd-private-d14aa6709ba64c9ca559b305bd7b0b86-chronyd.service-Tpa4NU
  mysql_all.sql            systemd-private-d14aa6709ba64c9ca559b305bd7b0b86-vgauthd.service-iHfxUd
  mysql.sock               systemd-private-d14aa6709ba64c9ca559b305bd7b0b86-vmtoolsd.service-mSuqtC
  mysql.sql                test.com.log
  pear                     test.com.log-20180704
  php_errors.log-20180704  user.sql
  php-fcgi.sock

由於配置文件中寫了no_root_squash,所以客戶端和服務端屬主屬組的差異

  [root@zyshanlinux-02 mnt]# vi 1212.txt
  [root@zyshanlinux-02 mnt]# ls -l 1212.txt
  -rw-r--r--. 1 root root 28 Jul 15 22:21 1212.txt
  
  [root@zyshanlinux-001 tmp]# ls -l 1212.txt
  -rw-r--r-- 1 root root 28 Jul 15 22:21 1212.txt

NFS客戶端問題

NFS 4版本會有該問題 客戶端掛載共享目錄後,不管是root用戶還是普通用戶,創建新文件時屬主、屬組爲nobody 客戶端掛載時加上 -o nfsvers=3 客戶端和服務端都需要 vim /etc/idmapd.conf // 把“#Domain = local.domain.edu” 改爲 “Domain = xxx.com” (這裏的xxx.com,隨意定義吧),然後再重啓rpcidmapd服務

先在掛載前

mount -t nfs 192.168.106.128:/tmp/ /mnt/

再掛載

mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/

  [root@zyshanlinux-02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/
  mount.nfs: an incorrect mount option was specified
  [root@zyshanlinux-02 ~]# cd
  [root@zyshanlinux-02 ~]# umount /mnt/
  umount: /mnt/: not mounted
  [root@zyshanlinux-02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/
  mount.nfs: an incorrect mount option was specified
  [root@zyshanlinux-02 ~]# mount -t nfs -o nfsvers=3 192.168.106.128:/tmp/ /mnt/
  [root@zyshanlinux-02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/
  [root@zyshanlinux-02 ~]# df -h
  Filesystem             Size  Used Avail Use% Mounted on
  /dev/sda3               28G  1.1G   27G   4% /
  devtmpfs               907M     0  907M   0% /dev
  tmpfs                  916M     0  916M   0% /dev/shm
  tmpfs                  916M  8.7M  908M   1% /run
  tmpfs                  916M     0  916M   0% /sys/fs/cgroup
  /dev/sda1              197M  113M   85M  58% /boot
  tmpfs                  184M     0  184M   0% /run/user/0
  192.168.106.128:/tmp/   28G  7.3G   21G  27% /mnt
  [root@zyshanlinux-02 ~]# mount -t nfs -oremount,nfsvers=3 192.168.106.128:/tmp/ /mnt/

發佈了57 篇原創文章 · 獲贊 22 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章