0x00 NFS概述
網絡文件系統(英語:Network File System,縮寫為NFS)是一種分布式文件系統協議,最初由Sun Microsystems公司開發,并于1984[1]年發布。其功能旨在允許客戶端主機可以像訪問本地存儲一樣通過網絡訪問服務器端文件。 NFS和其他許多協議一樣,是基于開放網絡運算遠程過程調用(ONC RPC) 協議之上的。
NFS是一個開放、標準的RFC協議,任何人或組織都可以依據標準實現它。
0x01 NFS實現原理
當一臺計算機(客戶端)需要訪問存儲在其他機器上的數據(NFS 服務器):
- 服務端實現 NFS 守護進程, 默認運行 nfsd, 用來使得數據可以被客戶端訪問.
- 服務端系統管理員可以決定哪些資源可以被訪問, 導出目錄的名字和參數, 通常使用 /etc/exports 配置文件 和exportfs命令。
- 服務端 安全-管理員 保證它可以組織和認證合法的客戶端.
- 服務端網絡配置保證可以跟客戶端透過 防火墻 進行協商.
- 客戶端請求導出的數據, 通常調用一個 mount 命令.
- 如果一切順利, 客戶端的用戶就可以通過已經掛載的 文件系統 查看和訪問服務端的文件了.
0x02 NFS 相關配置及命令
- NFS 軟件包 : nfs-utils-1.3.0-0.33.el7.x86_64
-
/etc/exports或/etc/exports.d/*
/PATH/TO/SOME_DIR clients1(export_options, …) clients2(export_options, …)
注意: /etc/exports.d/ 下的文件必須以 .exports 結尾
clients: single host:ipv4, ipv6, FQDN; network:address/netmask, 同時長短格式的掩碼; wildcards:主機名通配,例如:*.magedu.com; netgroups:NIS域內的主機組;@group_name; anonymous:使用*通配所有主機; export_options: General Options: ro:只讀 rw:讀寫; sync:同步; async:異步; User ID Mapping: root_squash:壓縮root用戶,一般指將其映射為nfsnobody; no_root_squash:不壓縮root用戶; all_squash:壓縮所有用戶; anonuid and anongid:將壓縮的用戶映射為此處指定的用戶;
-
兩個命令:exportfs和showmount
-
exportfs- maintain table of exported NFS file systems
exportfs [-av] -u [client:/path ..]
eg: exportfs -av -u 172.18.9.9:/data-a Export or unexport all directories. -r Reexport all directories -u Unexport one or more directories.
-
showmount- show mount information for an NFS server
-a or --all -d or --directories List only the directories mounted by some client. -e or --exports Show the NFS server’s export list.
-
0x03 NFS實踐作業(一)
實驗要求:
(1) nfs server導出/data/目錄;
(2) nfs client掛載/data/至本地的/mydata目錄;本地的mysqld或mariadb服務的數據目錄設置為/mydata, 要求服務能正常啟動,且可正常 存儲數據;
實驗環境:
- NFS_server : centos7.3 ,IP ( 172.18.9.119 )
- NFS_client : centos6.8 , IP ( 172.18.9.9 )
實驗步驟:
-
NFS_server:
1. ! rpm -q nfs-utils >/dev/null && yum install -y nfs-utils #安裝nfs 2. systemctl start nfs.service 3. mkdir /data #創建共享目錄 4. vim /etc/exports.d/mysql_data.exports /data/ 172.18.9.9(rw,anonuid=27,anongid=27,async) 5. exportfs -ar 6. setfacl -m o:rwx /data
-
NFS_client
1. yum install -y nfs-utils 2. yum install -y mysql-server 3. mkdir /mydata 4. mount -t nfs 172.18.9.119:/data /mydata 5. vim /etc/my.cnf datadir=/mydata 6. service mysqld start
注意: 上述步驟有先后順序,請嚴格執行
0x04 NFS實踐作業(二)
實驗要求:
(1) nfs server導出/data/application/web,在目錄中提供wordpress;
(2) nfs client掛載nfs server導出的文件系統至/var/www/html;
(3) 客戶端(lamp)部署wordpress,并讓其正常訪問;要確保能正常發文章,上傳圖片;
(4) 客戶端2(lamp),掛載nfs server導出的文件系統至/var/www/html;驗正其wordpress是否可被訪問; 要確保能正常發文章,上傳圖片;
實驗環境:
NFS_server : centos7.3 , IP : 172.18.9.119
NFS_client_1: centos6.8 , IP : 172.18.9.9
NFS_client_2: centos6.8, IP : 172.18.9.10
實驗步驟:
-
NFS_server:
1. yum install -y nfs-utils #安裝nfs 2. systemctl start nfs.service 3. mkdir -p /data/application/web #創建共享目錄 4. vim /etc/exports.d/data_app_web.exports /data/ 172.18.9.9(rw,async) 172.18.9.10(rw,async) 5. exportfs -ar 6. setfacl -R -m o:rwx /data/application/web #注意:此權限一定要設定,不然client會無法掛載訪問
-
NFS_client_1: 部署 wordpress
1. yum install -y httpd mysql-server php php-mysql 2. wget https://wordpress.org/latest.zip && unzip latest.zip && mv wordpress /var/www/html/ #下載wordpress 并解壓至 /var/www/html ### 設置wordpress 數據庫 ### 3. mysql mysql> create database wpdb; # 出現 Query OK, 0 rows affected (0.00 sec) 即為成功 mysql> grant all privileges on wpdb.* to wpuser@'%' identified by "mima"; mysql> exit; 4. service mysqld start # 啟動mysql 服務 5. mount 172.18.9.119:/data/application/web /var/www/html/ #掛載nfs 共享目錄至 /var/www/html 6. service httpd #啟動httpd 服務 7. # 安裝wordpress
-
安裝wordpress
在瀏覽器中輸入: http://172.18.9.9/wordpress/
-
安裝wordpress
….. 剩下的自己看情況設置……
到此wordpress 以經建好,可以發布文章了。(在瀏覽器中輸入: http://172.18.9.9/wordpress/ )
-
NFS_client_2:
1. yum install -y httpd mysql-server php php-mysql 2. mount 172.18.9.119:/data/application/web /var/www/html/ #掛載nfs 共享目錄至 /var/www/html 3. service httpd start 4. service mysqld start
在瀏覽器中輸入: http://172.18.9.10/wordpress/
登錄,即可發表文章,并能查看client_1 發表的文章。
0x05 常見問題
-
mount.nfs: access denied by server while mounting 172.18.9.119:/data/application/web
這是因為nfs_server端的共享目錄沒有執行權限解決方案:
在nfs_server執行: setfacl -m o:rwx /data/application/web -
mount nfs后, 如果遇到服務器修改 /etc/exports 等原因時,經常會遇到
Text代碼
umount2: Stale NFS file handle umount: htdocs: Stale NFS file handle
或者
Text代碼/var/www/html was not found in /proc/mounts
等問題
解決方案:
找到使用目錄的進程, kill掉 sudo umount -i -f /directory sudo umount -i -d -r -n -v -f /directory
原創文章,作者:Yanglibin,如若轉載,請注明出處:http://www.www58058.com/74462