歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Linux系統NFS網絡文件系統,nfs網絡文件系統

Linux系統NFS網絡文件系統,nfs網絡文件系統

日期:2017/3/6 8:55:42   编辑:學習Linux

Linux系統NFS網絡文件系統,nfs網絡文件系統

Linux系統NFS網絡文件系統,nfs網絡文件系統


Linux系統NFS網絡文件系統

NFS(network file system)網絡文件系統,就是通過網絡讓不同的主機系統之間可以共享文件或目錄,此種方法NFS客戶端使用掛載的方式讓共享文件或目錄到本地系統可掛載的目錄下

NFS實現是通過RPC服務來實現的

實現過程:

1、NFS RPC主要的功能是記錄每個NFS功能所對應的端口號,並將信息傳遞給請求數據的NFS客戶端,從而實現數據的傳輸

2、NFS服務啟動時會隨機取用數個端口,並主動向RPC服務注冊取用的相關端口信息,RPC服務就知道每個端口對應的NFS功能了,然後RPC會用固定的端口(111)來監聽NFS客戶端的請求,將正確的NFS端口傳給NFS的客戶端

服務端啟動順序:

事先RPC服務,後啟動NFS服務,否則NFS服務無法向RPC服務進行注冊,Centos 5.x 系統下RPC服務為portmap,Centos 6.x系統為rpcbind

NFS軟件:nfs-utils 是NFS的主程序

NFS配置文件格式如下:

NFS共享目錄 NFS客戶端地址(參數1.參數2)

1、NFS共享目錄的實際目錄必須是絕對路徑

2、NFS客戶端為服務端授權可以訪問共享目錄的NFS客戶端地址,可以是IP、域名、主機名、整個網段,或者用*來匹配所有主機

3、參數是對授權訪問NFS共享目錄的權限(客戶端的訪問權限集合)

安裝布署過程

服務器端配置如下

1、查看操作系統版本與內核

[root@Centos ~]# cat /etc/redhat-release

CentOS release 6.5 (Final)

[root@Centos ~]# uname -r

2.6.32-431.el6.x86_64

2、檢查是否安裝NFS RPC服務

[root@Centos ~]# rpm -aq nfs-utils rpcbind

rpcbind-0.2.0-12.el6.x86_64

nfs-utils-1.2.3-39.el6.x86_64

3、如果沒安裝相關服務請使用如下命令進行安裝

yum install nfs-utils -y

yum install rpcbind -y

啟動

按啟動的先後順序來啟動服務

[root@Centos ~]# /etc/init.d/rpcbind status

rpcbind is stopped

[root@Centos ~]# /etc/init.d/rpcbind start

Starting rpcbind: [ OK ]

[root@Centos ~]# /etc/init.d/nfs status

rpc.svcgssd is stopped

rpc.mountd is stopped

nfsd is stopped

rpc.rquotad is stopped

[root@Centos ~]# rpcinfo -p localhost 查看rpc的注冊信息情況

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

[root@Centos ~]# /etc/init.d/nfs start

Starting NFS services: [ OK ]

Starting NFS quotas: [ OK ]

Starting NFS mountd: [ OK ]

Starting NFS daemon: [ OK ]

Starting RPC idmapd: [ OK ]

相關服務啟動後,我們再來查看NFS向RPC的注冊情況

如果想讓NFS、RPC服務開機自啟動,實際生產環境中一般都會將配置放在/etc/rc.local下面

[root@Centos ~]# cat /etc/rc.local

####start up by 2016-08-21

/etc/init.d/rpcbind start

/etc/init.d/nfs start

檢查配置情況

[root@Centos /]# tail -2 /etc/rc.local

/etc/init.d/rpcbind start

/etc/init.d/nfs start

5、配置服務端NFS配置文件/etc/exports

[root@Centos ~]# vi /etc/exports

#####config for nfs-server 2016-8-21

/data/bbs 192.168.1.3(rw,sync)

檢查配置情況

[root@Centos /]# tail -2 /etc/exports

#####config for nfs-server 2016-8-21

/data/bbs 192.168.1.3(rw,sync)

6、配置完成後重啟NFS服務

/etc/init.d/nfs reload (/usr/bin/exprots -r) 兩者功能相同

[root@Centos /]# /etc/init.d/nfs reload

客戶端配置如下

1、查看服務器操作系統版本與內核

[root@localhost ~]# cat /etc/redhat-release

CentOS release 6.5 (Final)

[root@localhost ~]# uname -r

2.6.32-431.el6.x86_64

2、啟動RPC服務

[root@localhost ~]# /etc/init.d/rpcbind status

rpcbind is stopped

[root@localhost ~]# /etc/init.d/rpcbind start

Starting rpcbind: [ OK ]

[root@localhost ~]# /etc/init.d/rpcbind status

rpcbind (pid 25679) is running...

3、查看掛載情況

[root@localhost ~]# showmount -e 192.168.1.2

clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

[root@localhost ~]# ping 192.168.1.2

PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.

64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.745 ms

64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.532 ms

64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.470 ms

64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.522 ms

^C

--- 192.168.1.2 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 3060ms

rtt min/avg/max/mdev = 0.470/0.567/0.745/0.106 ms

[root@localhost ~]# iptables -F 關閉服務與客戶端防火牆

[root@localhost ~]# showmount -e 192.168.1.2

Export list for 192.168.1.2:

/data/bbs 192.168.1.3

4、客戶端進行NFS掛載

[root@localhost ~]# mount -t nfs 192.168.1.2:/data/bbs /mnt

[root@localhost ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root 18G 3.3G 14G 20% /

tmpfs 491M 72K 491M 1% /dev/shm

/dev/sda1 485M 35M 426M 8% /boot

192.168.1.2:/data/bbs 50G 3.5G 44G 8% /mnt

5、測試共享

服務端查看具體目錄權限

[root@Centos /]# ls -ld data/bbs/

drwxr-xr-x. 2 root root 4096 Aug 21 10:07 data/bbs/

表明其它用戶具有讀與執行權限

[root@Centos /]# cd data/bbs/

[root@Centos bbs]# touch text.txt

[root@Centos bbs]# mkdir textdir

[root@Centos bbs]# ls

textdir text.txt

客戶端測試

[root@localhost ~]# cd /mnt

[root@localhost mnt]# ls -ll

total 4

drwxr-xr-x. 2 root root 4096 Aug 21 05:39 textdir

-rw-r--r--. 1 root root 0 Aug 21 05:39 text.txt

[root@localhost mnt]# touch 123.txt

touch: cannot touch `123.txt': Permission denied ----> 權限不足

所以需要在服務端改變其它用戶的訪問權限才可以

[root@Centos /]# chmod 777 data/bbs/

[root@Centos /]# ls -ld data/bbs/

drwxrwxrwx. 3 root root 4096 Aug 21 10:39 /data/bbs/

然後在客戶端測試

[root@localhost mnt]# pwd

/mnt

[root@localhost mnt]# touch 123.txt

[root@localhost mnt]# ls -ll

total 4

-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 21 05:44 123.txt

drwxr-xr-x. 2 root root 4096 Aug 21 05:39 textdir

-rw-r--r--. 1 root root 0 Aug 21 05:39 text.txt

[root@localhost mnt]# echo "123">>123.txt

[root@localhost mnt]# cat 123.txt

123

經過測試表明,客戶端也可以正常訪問共享目錄與文件,同時也具有讀寫權限,那麼問題又來了,客戶端是通過什麼用戶名來訪問服務器端的呢,剛剛修改的權限是征對其它用戶(除了文件、目錄的所有者與所屬組外的用戶),如果需要限制訪問共享目錄,其實這個權限是具有很大安全隱患的

那麼我們來服務端看看到底是授權什麼用戶來訪問共享目錄呢???

[root@Centos /]# cat /var/lib/nfs/etab

/data/bbs 192.168.1.3(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534)

這裡我們查看這個uid/gid都為65534的用戶是哪個???

[root@Centos /]# grep 65534 /etc/passwd

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

所以服務端我們將權限重新修改如下

[root@Centos /]# chown -R nfsnobody:nfsnobody data/bbs/

[root@Centos /]# ls -ld data/bbs/

drwxrwxrwx. 3 nfsnobody nfsnobody 4096 Aug 21 10:44 data/bbs/

客戶端查看配置生效情況如下

[root@localhost mnt]# ls -ll

total 8

-rw-r--r--. 1 nfsnobody nfsnobody 4 Aug 21 05:44 123.txt

drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Aug 21 05:39 textdir

-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 21 05:39 text.txt

但是細心的朋友們肯定也發現了,這樣還是不安全,因為所有相同的版本的系統都會默認存在這一個用戶nfsnobody,那麼對限制訪問還是沒有做到萬無一失,因此這裡就出現了更改默認用戶的做法,將默認用戶更改成其它用戶

其服務端配置如下

首先得添加用戶並禁止登陸指定它的UID

[root@Centos /]# useradd -s /sbin/nologin -M -u 3000 nfsuser

[root@Centos /]# tail -1 /etc/passwd

nfsuser:x:3000:3000::/home/nfsuser:/sbin/nologin

修改NFS配置

[root@Centos /]# echo "#####config for nfs-server 2016-8-21">/etc/exports

清空配置並加上一行注釋信息

[root@Centos /]# echo "/data/bbs 192.168.1.3(rw,sync,all_squash,anonuid=3000,anongid=3000)">>/etc/exports 添加相關配置

[root@Centos /]# tail -2 /etc/exports 檢查配置情況

#####config for nfs-server 2016-8-21

/data/bbs 192.168.1.3(rw,sync,all_squash,anonuid=3000,anongid=3000)

因此客戶端同樣也需要添加用戶

[root@localhost mnt]# useradd -s /sbin/nologin -M -u 3000 nfsuser

[root@localhost mnt]# ls -ll

total 8

-rw-r--r--. 1 nfsnobody nfsnobody 4 Aug 21 05:44 123.txt

drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Aug 21 05:39 textdir

-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 21 05:39 text.txt

測試發現還是原來默認的用戶,這是因為服務端雖然改了NFS的配置,但是共享目錄與文件的相關配置仍然是默認的所以上服務端查看下目錄權限如下

[root@Centos bbs]# ls -ll

total 8

-rw-r--r--. 1 nfsnobody nfsnobody 4 Aug 21 10:44 123.txt

drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Aug 21 10:39 textdir

-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 21 10:39 text.txt

因此需要對/data/bbs/這個共享目錄進行更改

[root@Centos bbs]# chown -R nfsuser.nfsuser /data/bbs/

[root@Centos bbs]# ls -ll

total 8

-rw-r--r--. 1 nfsuser nfsuser 4 Aug 21 10:44 123.txt

drwxr-xr-x. 2 nfsuser nfsuser 4096 Aug 21 10:39 textdir

-rw-r--r--. 1 nfsuser nfsuser 0 Aug 21 10:39 text.txt

最後登陸到客戶查看配置生效情況如下

[root@localhost mnt]# ls -ll

total 8

-rw-r--r--. 1 nfsuser nfsuser 4 Aug 21 05:44 123.txt

drwxr-xr-x. 2 nfsuser nfsuser 4096 Aug 21 05:39 textdir

-rw-r--r--. 1 nfsuser nfsuser 0 Aug 21 05:39 text.txt

[root@localhost mnt]# touch 234.txt

[root@localhost mnt]# ls -ll

total 8

-rw-r--r--. 1 nfsuser nfsuser 4 Aug 21 05:44 123.txt

-rw-r--r--. 1 nfsuser nfsuser 0 Aug 21 06:27 234.txt

-rw-r--r--. 1 nfsuser nfsuser 0 Aug 21 06:17 23.txt

drwxr-xr-x. 2 nfsuser nfsuser 4096 Aug 21 05:39 textdir

-rw-r--r--. 1 nfsuser nfsuser 0 Aug 21 05:39 text.txt

表明配置正確,且達到需求,此時訪問共享目錄的權限就更改為nfsuser,其它主機如果沒有添加此用戶是無法正常訪問NFS的共享目錄與文件的,安全性比較高。至此整個安裝與布署過和結束





http://xxx/Linuxjc/1157471.html TechArticle

Copyright © Linux教程網 All Rights Reserved