Linux系統NFS網路文件系統

来源:http://www.cnblogs.com/youkanyouxiao/archive/2016/09/11/5861773.html
-Advertisement-
Play Games

Linux系統NFS網路文件系統 NFS(network file system)網路文件系統,就是通過網路讓不同的主機系統之間可以共用文件或目錄,此種方法NFS客戶端使用掛載的方式讓共用文件或目錄到本地系統可掛載的目錄下 NFS實現是通過RPC服務來實現的 實現過程: 1、NFS RPC主要的功能 ...


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的共用目錄與文件的,安全性比較高。至此整個安裝與佈署過和結束

 





 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1. 使用命令行打開文件(如 sublime) open -a /Applications/Sublime\ Text\ 2.app/ yourFile.log 如果使用頻繁,還可以添加軟連接 ...
  • 下麵是我學習linux的一些筆記,整理總結如下 首先先普及一下常識:DNS是用來解析功能變數名稱的;url埠預設是80,可以不寫;localhost 就是127.0.0.1,如果是root時,提示符為:# ,普通用戶則為:$; Linux下常用命令: 1.查看當前系統下登錄的用戶時,可以輸入whoami命 ...
  • 網上看了好多博客,截取的精華 ...
  • shell script簡介 shell是解釋型語言,就是解釋器會一條一條的翻譯每一條語句並執行,對比之下,C語言是編譯型語言,編譯器把整個工程編譯成可執行文件才能執行 在沒有續行符( )的情況下,shell腳本的一條語句以"回車"為結束 任何一個shell腳本程式都必須在開頭用 標識使用的shel ...
  • echo 顯示後面的內容,預設選項表示將後面的內容原模原樣的顯示出來,可以配合Shell的管道與重定向使用實現對寫文件操作 $echo "this is echo" echo_content.txt $echo [ e] [內容字元串] 將內容中的轉義字元按照其含義顯示,支持的轉義字元如下: \a ...
  • 在安裝完ContextCapture軟體之後,大家懷著迫不及待的心情雙擊了運行快捷鍵。但是很遺憾的是,會產生下麵的提示視窗: 也許大家並不在意,就覺得關掉這個視窗不就行了。然而,頭疼的問題來了。這個視窗關了之後,軟體反而也關閉了!!!! 好了,既然問題出現了,那麼就需要有解決的方法! ——————— ...
  • 如果你初次使用Ubuntu,打開軟體中心看到那少的可憐的軟體一定十分失望,難道大名鼎鼎的Linux就這麼幾款軟體可用?當然不是,軟體中心只是Ubuntu提供的一個軟體庫,浩如煙海的Linux軟體其實都存儲在各個鏡像站里,掌握軟體管理命令才能充分使用Linux的便利功能。 apt cache和apt ...
  • 本地服務管理器視窗快捷鍵: services.msc ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...