Linux中NFS的使用
- NFS簡介
- NFS應用
- NFS使用
- NFS配置詳解
- 搭建系統
一、NFS簡介
NFS是Network File System的縮寫及網絡文件系統。NFS主要功能是通過局域網絡讓不同的主機系統之間可以共享文件或目錄。
NFS系統和Windows網絡共享、網絡驅動器類似, 只不過windows用於局域網, NFS用於企業集群架構中, 如果是大型網站, 會用到更復雜的分布式文件系統FastDFS,glusterfs,HDFS,ceph
二、NFS應用
1.用戶訪問NFS客戶端,將請求轉化為函數
2.NFS通過TCP/IP連接服務端
3.NFS服務端接收請求,會先調用portmap進程進行端口映射
4.Rpc.nfsd進程用於判斷NFS客戶端能否連接服務端;
5.Rpc.mount進程用於判斷客戶端對服務端的操作權限
6.如果通過權限驗證,可以對服務端進行操作,修改或讀取
三、NFS的使用
1.服務端
1)安裝NFS和rpcbind [root@nfs ~]# yum install nfs-utils rpcbind -y 2)創建掛載點 [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)檢查服務端是否正常 [root@nfs ~]# showmount -e [服務端的地址,默認是本機地址] [root@nfs ~]# showmount -e Export list for nfs: /web/nfsv1 172.16.1.0/20 [root@nfs ~]# showmount -e 172.16.1.31 Export list for 172.16.1.31: /web/nfsv1 172.16.1.0/20 [root@nfs ~]# cat /var/lib/nfs/etab 7)給掛載點授權 [root@nfs ~]# chown -R nfsnobody.nfsnobody /web
2.客戶端
1)安裝NFS [root@web01 opt]# yum install -y nfs-utils 2)創建目錄 [root@web01 opt]# mkdir /opt/nfs/ 3)掛載NFS [root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/ 4)測試NFS文件同步功能
四、NFS配置詳解
nfs共享參數 | 參數作用 |
rw | 讀寫權限 (常用) |
ro | 只讀權限 (不常用) |
root_squash | 當NFS客戶端以root管理員訪問時,映射為NFS服務器的匿名用戶 (不常用) |
no_root_squash | 當NFS客戶端以root管理員訪問時,映射為NFS服務器的root管理員 (不常用) |
all_squash | 無論NFS客戶端使用什么賬戶訪問,均映射為NFS服務器的匿名用戶 (常用) |
no_all_squash | 無論NFS客戶端使用什么賬戶訪問,都不進行壓縮 (不常用) |
sync | 同時將數據寫入到內存與硬盤中,保證不丟失數據 (常用) |
async | 優先將數據保存到內存,然后再寫入硬盤;這樣效率更高,但可能會丟失數據 (不常用) |
async | 配置all_squash使用,指定NFS的用戶UID,必須存在系統 (常用) |
anongid | 配置all_squash使用,指定NFS的用戶GID,必須存在系統 (常用) |
1)控制讀寫 rw、ro 2)控制文件權限 root_squash no_root_squash all_squash no_all_squash 3)控制寫模式 sync async 4)控制用戶 anonuid anongid 統一用戶: 1)創建用戶 [root@nfs nfs1]# groupadd www -g 666 [root@nfs nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin 2)修改掛載點權限 [root@nfs nfs1]# chown -R www.www /web/ 3)使用
五、搭建考試系統
1.搭建WEB服務
1)安裝web軟件 [root@web01 opt]# yum install httpd php php-devel -y 2)將代碼放置於網站的根目錄 [root@web01 opt]# cd /var/www/html/ # 上傳代碼 3)授權 [root@web01 html]# chown -R www.www /var/www/html 4)關閉selinux和防火牆 [root@nfs ~]# setenforce 0 [root@nfs ~]# systemctl disable --now firewalld 5)修改web軟件的用戶 [root@web01 html]# vim /etc/httpd/conf/httpd.conf User www Group www 6)啟動web軟件 [root@web01 html]# systemctl start httpd 7)測試 1.上傳 2.訪問 http://172.16.1.7/upload/1_linux.jpg
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@nfs nfs1]# systemctl restart nfs-server rpcbind 4)客戶端安裝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 5)掛載 [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 6)測試 用web2上傳,web3查看