1. 程式人生 > 其它 >linux基礎之網路檔案系統NFS詳述

linux基礎之網路檔案系統NFS詳述

目錄

NFS

1、NFS簡介

NFS是Network File System的縮寫及網路檔案系統。NFS主要功能是通過區域網絡讓不同的主機系統之間可以共享檔案或目錄。

NFS系統和Windows網路共享、網路驅動器類似, 但是windows用於區域網, NFS用於企業叢集架構中, 如果是大型網站, 會用到更復雜的分散式檔案系統比如FastDFS,glusterfs,HDFS,ceph。

2、NFS應用

1.使用者訪問NFS客戶端,將請求轉化為函式
2.NFS通過TCP/IP連線服務端
3.NFS服務端接收請求,會先呼叫portmap程序進行埠對映
4.Rpc.nfsd程序用於判斷NFS客戶端能否連線服務端;
5.Rpc.mount程序用於判斷客戶端對服務端的操作許可權
6.如果通過許可權驗證,可以對服務端進行操作,修改或讀取

3、NFS實踐

3.1 服務端

1、安裝NFS和rpcbind
[root@nfs ~]# yum install nfs-utils rpcbind -y

2、建立掛載點
[root@nfs ~]# mkdir /web
[root@nfs ~]# mkdir /web/nfs{1..9}
 
3、配置掛載點
[root@nfs ~]# vim /etc/exports
格式:[掛載點] [可以訪問的IP]([許可權])
	/web/nfs1  172.16.1.0/20(rw,sync,all_squash)

4、關閉selinux和防火牆
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld

5、啟動Nfs和rpcbind服務
[root@nfs ~]# systemctl start nfs-server 
[root@nfs ~]# systemctl start rpcbind

6、檢查服務端是否正常( showmount -e [服務端的地址,預設是本機地址]   # 結果沒有報錯就是正常)
[root@nfs ~]# showmount -e  # 和 showmount -e 172.16.1.31結果是一樣的

7、給掛載點授權
[root@nfs ~]# chown -R nfsnobody.nfsnobody /web

3.2 客戶端

1、安裝NFS
[root@web01 ~]# cd /opt/
[root@web01 opt]# yum install -y nfs-utils

2、建立目錄
[root@web01 opt]# mkdir /opt/nfs/

3、掛載NFS(命令:mount -t nfs 掛載目錄 掛載物件)
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/

4、測試NFS檔案同步功能(建立檔案看看能否同步到服務端)
[root@web01 opt]# touch nfs/2.txt
[root@web02 opt]# touch nfs/3.txt
[root@web03 opt]# touch nfs/1.txt
[root@nfs web]# ll /web/nfs1
[root@web03 opt]# rm -rf nfs/*  # 隨便在客戶端執行都能同步到服務端
[root@nfs web]# ll /web/nfs1

4、NFS配置詳解

1、控制讀寫
rw、ro

2、控制檔案許可權
root_squash
no_root_squash
all_squash
no_all_squash

3、控制寫模式
sync : 同步寫,重要資料建議用
async :非同步寫,常用於不重要資料

4、控制使用者
anonuid
anongid
	
5.統一使用者(所有的平臺都使用www使用者,形成統一)
5.1 建立使用者
[root@nfs nfs1]# groupadd www -g 666
[root@nfs nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
[root@web01 nfs1]# groupadd www -g 666
[root@web01 nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
[root@web02 nfs1]# groupadd www -g 666
[root@web02 nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
[root@web03 nfs1]# groupadd www -g 666
[root@web03 nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin


5.2 修改配置檔案和掛載點許可權
[root@nfs web]# vim /etc/exports
/web/nfs1  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
[root@nfs web]# systemctl restart nfs-server rpcbind
[root@nfs nfs1]# chown -R www.www /web/

5.3 使用:每個客戶端都先解除安裝掛載再重新掛載,接著建立檔案看看能否同步到服務端
[root@web01 opt]# umount /opt/nfs
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/
[root@web02 opt]# umount /opt/nfs
[root@web02opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/
[root@web03 opt]# umount /opt/nfs
[root@web03 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/
[root@web01 opt]# touch nfs/10.txt
[root@web02 opt]# touch nfs/11.txt
[root@web03 opt]# touch nfs/12.txt
[root@nfs web]# ll /web/nfs1  # 檢視服務端是否有客戶端建立的檔案,建立檔案的屬組屬組是否www.www

5、搭建考試系統

5.1 搭建web服務

1、安裝web軟體(每個平臺)
[root@web01 opt]# yum install httpd php php-devel -y
[root@web02 opt]# yum install httpd php php-devel -y
[root@web03 opt]# yum install httpd php php-devel -y

2、將程式碼放置於網站的根目錄:切換到網站的根目錄
[root@web01 opt]# cd /var/www/html/
[root@web02 opt]# cd /var/www/html/
[root@web03 opt]# cd /var/www/html/

# 上傳程式碼壓縮包並解壓
[root@web01 html]# unzip kaoshi.zip
[root@web02 html]# unzip kaoshi.zip
[root@web03 html]# unzip kaoshi.zip

3、授權
[root@web01 html]# chown -R www.www /var/www/html
[root@web02 html]# chown -R www.www /var/www/html
[root@web03 html]# chown -R www.www /var/www/html

4、關閉selinux和防火牆
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld
[root@web01 ~]# setenforce 0
[root@web01 ~]# systemctl disable --now firewalld
[root@web02 ~]# setenforce 0
[root@web02 ~]# systemctl disable --now firewalld
[root@web03 ~]# setenforce 0
[root@web03 ~]# systemctl disable --now firewalld

5、修改web軟體的使用者:
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
[root@web02 html]# vim /etc/httpd/conf/httpd.conf
[root@web03 html]# vim /etc/httpd/conf/httpd.conf
    User apache  :改成 User www
    Group apache  :改成 Group www

6、啟動web軟體
[root@web01 html]# systemctl restart httpd
[root@web02 html]# systemctl restart httpd
[root@web03 html]# systemctl restart httpd

7、測試:訪問 http://172.16.1.7/;訪問 http://172.16.1.8/;訪問 http://172.16.1.8/
	7.1 建立目錄並授權
	[root@web01 html]# mkdir upload
	[root@web01 html]# chown www.www upload
	[root@web02 html]# mkdir upload
	[root@web02 html]# chown www.www upload
	[root@web03 html]# mkdir upload
	[root@web03 html]# chown www.www upload
	
	7.2 上傳圖片
	
	7.3 檢視是否上傳成功
    瀏覽器訪問:http://172.16.1.7/upload/1_linux.jpg;http://172.16.1.8/upload/2_linux.jpg;http://172.16.1.9/upload/3_linux.jpg
    [root@web01 html]# ll /var/www/html/upload

5.2 配合NFS實現檔案共享

1、修改NFS配置檔案(服務端)
[root@nfs nfs1]# vim /etc/exports
# 在原基礎上增加如下一行
	/web/upload  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)

2、建立掛載點並授權(服務端)
[root@nfs nfs1]# mkdir /web/upload
[root@nfs nfs1]# chown www.www /web/upload

3、客戶端安裝NFS軟體
[root@web01 html]# yum install nfs-utils -y
[root@web02 html]# yum install nfs-utils -y
[root@web03 html]# yum install nfs-utils -y

4、掛載(客戶端)
[root@web01 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web02 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web03 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload

5、重啟NFS(服務端)
[root@nfs nfs1]# systemctl restart nfs-server rpcbind

6、測試:
在瀏覽器用不同伺服器上傳圖片,再用不同的伺服器檢視別的伺服器上傳的圖片,能正常檢視則實現共享了。