天天看點

NFS網絡檔案系統配置

NFS網絡檔案系統配置 

NFS簡介

NFS(Network File System)即網絡檔案系統,是FreeBSD支援的檔案系統中的一種,它允許網絡中的計算機之間通過TCP/IP網絡共享資源。在NFS的應用中,本地NFS的用戶端應用可以透明地讀寫位于遠端NFS伺服器上的檔案,就像通路本地檔案一樣。

nfs安裝包:nfs-utils

nfs配置檔案:/etc/exports

文法:   檔案系統  用戶端(導出選項)  用戶端(選項)

用戶端處可填寫:IP、FQDN或DOMAIN、NETWORK、*

NFS主要有3類選項:

通路權限選項

    設定輸出目錄隻讀:ro

    設定輸出目錄讀寫:rw

使用者映射選項

    all_squash:将遠端通路的所有普通使用者及所屬組都映射為匿名使用者或使用者組(nfsnobody);

    no_all_squash:與all_squash取反(預設設定);

    root_squash:将root使用者及所屬組都映射為匿名使用者或使用者組(預設設定);

    no_root_squash:與root_squash取反;以root身份建立目錄和檔案,此時共享目錄權限(755);針對于其它使用者向共享目錄寫入目錄或檔案,需要給共享目錄權限(757)此時普通使用者将普通使用者自己身份寫入目錄和檔案。

    anonuid=xxx:将遠端通路的所有使用者都映射為匿名使用者,并指定該使用者為本地使用者(UID=xxx);

    anongid=xxx:将遠端通路的所有使用者組都映射為匿名使用者組賬戶,并指定該匿名使用者組賬戶為本地使用者組賬戶(GID=xxx);

其它選項

    secure:限制用戶端隻能從小于1024的tcp/ip端口連接配接nfs伺服器(預設設定);

    insecure:允許用戶端從大于1024的tcp/ip端口連接配接伺服器;

    sync:将資料同步寫入記憶體緩沖區與磁盤中,效率低,但可以保證資料的一緻性;

    async:将資料先儲存在記憶體緩沖區中,必要時才寫入磁盤;

    wdelay:檢查是否有相關的寫操作,如果有則将這些寫操作一起執行,這樣可以提高效率(預設設定);

    no_wdelay:若有寫操作則立即執行,應與sync配合使用;

    subtree:若輸出目錄是一個子目錄,則nfs伺服器将檢查其父目錄的權限(預設設定);

    no_subtree:即使輸出目錄是一個子目錄,nfs伺服器也不檢查其父目錄的權限,這樣可以提高效率;

檢視NFS伺服器端共享的檔案系統

    showmount –e SERVER_IP

用戶端使用NFS檔案系統

    mount –t nfs SERVER:/path/to/sharedfs /path/to

  # exportfs [-aruv]

  -a 全部挂載或解除安裝 /etc/exports中的内容

  -r 重新讀取/etc/exports 中的資訊 ,并同步更新/etc/exports、/var/lib/nfs/xtab

  -u 解除安裝單一目錄(和-a一起使用為解除安裝所有/etc/exports檔案中的目錄)

  -v 在export的時候,将詳細的資訊輸出到螢幕上。 

    具體例子: 

         exportfs:維護exports檔案導出的檔案系統表的專用工具

    exportfs –rv:重新導出所有的檔案系統

    exportfs –au:關閉導出的所有檔案系統(解除安裝所有共享目錄)

開機自動挂載nfs:

SERVER:/PATH/TO/EXPORTED_FS /mount_point   nfs     defaults,_netdev   0 0

 _netdev:說明此共享是一個網絡檔案系統,如果聯系不上,就會跳過此檔案系統

示例:

172.16.4.100:/share/nfs   /mnt       nfs     defaults,_netdev 0 0

NFS使用示範

示例:将本機的/share/nfs使用nfs共享出去,讓其他用戶端可以通路

[root@nfs ~]# mkdir -p /share/nfs

[root@nfs ~]# vim /etc/exports

/share/nfs 172.16.4.136(rw)

[root@nfs ~]# exportfs -v

/share/nfs      172.16.4.136(rw)

/export  10.1.1.0/255.255.255.0(rw,fsid=0,no_root_squash,async)

驗證nfs是否成功共享

[root@client ~]# showmount -e 172.16.4.100

Export list for 172.16.4.100:

/share/nfs 172.16.4.136

用戶端挂載使用

[root@client ~]# mount -t nfs 172.16.4.100:/share/nfs /mnt/

[root@client ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg0-root   20G 299M   19G   2% /

tmpfs                 491M     0 491M   0% /dev/shm

/dev/sda1             190M   28M 153M  16% /boot

/dev/mapper/vg0-usr   9.8G 2.0G  7.4G  21% /usr

/dev/mapper/vg0-var    20G 267M   19G   2% /var

172.16.4.100:/share/nfs

                       20G  312M  19G   2% /mnt

這個時候nfs就可以正常使用了

由于伺服器預設将root使用者映射為了來賓使用者,如果以root使用者通路nfs伺服器是沒有寫權限的

[root@client ~]# cp /etc/fstab /mnt/

cp: cannot create regular file `/mnt/fstab':Read-only file system

伺服器端設定允許root使用者以root權限通路這個共享伺服器

/share/nfs 172.16.4.136(rw,no_root_squash)

設定完成之後重新開機伺服器,在使用root使用者複制檔案就有權限了

[root@client ~]# ll /mnt/

total 4

-rw-r--r-- 1 nobody nobody 921 Apr 23 18:51 fstab

驗證:nfs是通過使用者id來認證使用者,而不是使用者名

建立使用者gentoo使使用者對/share/nfs有讀寫執行權限,然後在裡面建立一個檔案

[root@Server ~]# useradd -u 600 gentoo

[root@Server ~]# setfacl -m u:gentoo:rwx /share/nfs/

[root@Server ~]# su - gentoo

[gentoo@Server ~]$ cd /share/nfs/

[gentoo@Server nfs]$ touch a.gentoo

[gentoo@Server nfs]$ ll

-rw-rw-r-- 1 gentoo gentoo   0 Apr 23 18:55 a.gentoo

用戶端操作:

建立相同id,然後不同的使用者名,進行寫入

[root@client ~]# useradd -u 600 fedora

[root@client ~]# su - fedora

[fedora@client ~]$ cd /mnt/

[fedora@client mnt]$ ll

-rw-rw-r-- 1 nobody nobody   0 Apr23 18:55 a.gentoo

[fedora@client mnt]$ touch a.fedora

-rw-rw-r-- 1 nobody nobody   0 Apr 23 18:59 a.fedora

-rw-rw-r-- 1 nobody nobody   0 Apr 23 18:55 a.gentoo

建立不同id,相同使用者名,進行寫入

[root@client ~]# useradd gentoo

[root@client ~]# id gentoo

uid=602(gentoo) gid=602(gentoo) groups=602(gentoo)

[root@client ~]# su - gentoo

[gentoo@client ~]$ cd /mnt/

[gentoo@client mnt]$ ll

-rw-rw-r-- 1 nobody nobody   0 Apr 24 2015 a.fedora

-rw-rw-r-- 1 nobody nobody   0 Apr 24 2015 a.gentoo

[gentoo@client mnt]$ touch b.gentoo

touch: cannot touch `b.gentoo': Permission denied

結論:相同使用者名,無法成功寫入,相同id可以成功寫入。如果相同的使用者名,相同id也是可以成功寫入的。

關于權限的分析

  1. 用戶端連接配接時候,對普通使用者的檢查

    a. 如果明确設定了普通使用者被壓縮的身份,那麼此時用戶端使用者的身份轉換為指定使用者;

    b. 如果NFS server上面有同ID使用者,那麼此時用戶端登入賬戶的身份轉換為NFS server上面的同ID使用者;

    c. 如果沒有明确指定,也沒有同ID使用者,那麼此時 使用者身份被壓縮成nobody;

  2. 用戶端連接配接的時候,對root的檢查

    a. 如果設定no_root_squash,那麼此時root使用者的身份被壓縮為NFS server上面的root;

    b. 如果設定了all_squash、anonuid、anongid,此時root 身份被壓縮為指定使用者;

    c. 如果沒有明确指定,此時root使用者被壓縮為nfsnobody;其它使用者對照上述1中的b、c來顯示所有者和所屬組;

    d. 如果同時指定no_root_squash與all_squash 使用者将被壓縮為nfsnobody,如果設定了anonuid、anongid将被壓縮到所指定的使用者與組;

示例一:

NFS Server:

[root@NFS ~]# mkdir /Data

[root@NFS ~]# chmod o+w /Data/

[root@NFS ~]# cat /etc/exports 

/Data  10.10.172.0/255.255.255.0(rw,no_root_squash,async)

[root@NFS ~]# service nfs restart

Shutting down NFS daemon: [  OK  ]

Shutting down NFS mountd: [  OK  ]

Shutting down NFS quotas: [  OK  ]

Shutting down NFS services:  [  OK  ]

Shutting down RPC idmapd: [  OK  ]

Starting NFS services:  [  OK  ]

Starting NFS quotas: [  OK  ]

Starting NFS mountd: [  OK  ]

Starting NFS daemon: [  OK  ]

Starting RPC idmapd: [  OK  ]

[root@NFS ~]# tail /etc/passwd

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

apache:x:48:48:Apache:/var/www:/sbin/nologin

saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

centos:x:500:500:centos:/home/centos:/bin/bash

test:x:501:501::/home/test:/bin/bash

[root@NFS ~]# 

NFS Client:

[root@Client ~]# tail /etc/passwd

gentoo:x:501:501::/home/gentoo:/bin/bash

test:x:502:502::/home/test:/bin/bash

[root@Client ~]# mount -t nfs 10.10.172.82:/Data /mnt

[root@Client ~]# cd /mnt/

[root@Client mnt]# touch root.txt

[root@Client mnt]# ll

total 0

-rw-r--r--. 1 root   root   0 Sep 18  2016 root.txt

[root@Client ~]# su centos

[centos@Client mnt]$ touch centos.txt

[centos@Client mnt]$ ll

-rw-rw-r--. 1 centos centos 0 Sep 18  2016 centos.txt

[centos@Client mnt]$ su gentoo

Password: 

[gentoo@Client mnt]$ touch gentoo.txt

[gentoo@Client mnt]$ ll

-rw-rw-r--. 1 centos centos 0 Sep 18 16:55 centos.txt

-rw-rw-r--. 1 test   test   0 Sep 18  2016 gentoo.txt

[gentoo@Client mnt]$ su test

[test@Client mnt]$ touch test.txt

[test@Client mnt]$ ll

-rw-rw-r--. 1 test   test   0 Sep 18 16:55 gentoo.txt

-rw-rw-r--. 1 nobody nobody 0 Sep 18 16:56 test.txt

[test@Client mnt]$ 

示例二:

/Data  10.10.172.0/255.255.255.0(rw)

-rw-r--r--. 1 nfsnobody nfsnobody 0 Sep 18  2016 root.txt

[root@Client mnt]# su centos

-rw-rw-r--. 1 centos    centos    0 Sep 18  2016 centos.txt

[centos@Client mnt]$ su test

-rw-rw-r--. 1 centos    centos    0 Sep 18 17:16 centos.txt

-rw-r--r--. 1 nfsnobody nfsnobody 0 Sep 18 17:16 root.txt

-rw-rw-r--. 1 nobody    nobody    0 Sep 18  2016 test.txt

[test@Client mnt]$ su gentoo

-rw-rw-r--. 1 test      test      0 Sep 18  2016 gentoo.txt

-rw-rw-r--. 1 nobody    nobody    0 Sep 18 17:17 test.txt

[gentoo@Client mnt]$ 

本文轉自 dengaosky 51CTO部落格,原文連結:http://blog.51cto.com/dengaosky/1853686,如需轉載請自行聯系原作者