NFS即網絡文件系統,允許與他人共享目錄和文件。
(1).實驗環境
NFS服務器youxi1 192.168.1.6
測試主機youxi2 192.168.1.7
NFS是C/S模式,監聽2049端口。
(2).實驗
1)在NFS服務器youxi1上安裝NFS
[root@youxi1 ~]# yum -y install rpcbind nfs-utils
2)啟動NFS,並開機自啟
[root@youxi1 ~]# systemctl start rpcbind [root@youxi1 ~]# systemctl enable rpcbind [root@youxi1 ~]# systemctl start nfs-server //NFS依賴rpcbind進行通訊,所以要先啟動rpcbind [root@youxi1 ~]# systemctl enable nfs-server [root@youxi1 ~]# netstat -antup | grep 2049 tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN - tcp6 0 0 :::2049 :::* LISTEN - udp 0 0 0.0.0.0:2049 0.0.0.0:* - udp6 0 0 :::2049 :::* -
3)測試主機youxi2安裝nfs-utils
[root@youxi2 ~]# yum -y install nfs-utils
4)測試主機youxi2第一次測試
[root@youxi2 ~]# showmount -e 192.168.1.6 Export list for 192.168.1.6: //提示是空的
5)修改NFS服務器的配置文件/etc/exports,將已有的/www/html/文件夾共享出去
[root@youxi1 ~]# ll -d /www/html/ drwxr-xrwx 2 root root 20 5月 19 19:37 /www/html/ [root@youxi1 ~]# vim /etc/exports /www/html/ 192.168.1.7(rw,no_root_squash) //可以共享給指定IP(192.168.1.7),也可以共享給指定網段(192.168.1.0),還可以共享給所有IP(*).rw代表讀寫權限。 [root@youxi1 ~]# exportfs -rv //重新讀取配置文件,而不中斷服務 exporting 192.168.1.7:/www/html
/etc/exports配置文件中權限參數常用的有如下五個:
ro只讀權限
rw讀寫權限
sync同步寫入內存與磁盤當中
no_all_squash保留共享文件的UID和GID(默認)
no_root_squash使得root用戶具有根目錄的完全訪問權限
6)測試主機youxi2第二次測試
[root@youxi2 ~]# showmount -e 192.168.1.6 Export list for 192.168.1.6: /www/html 192.168.1.7
7)在測試主機youxi2上掛載NFS
[root@youxi2 ~]# mkdir /html [root@youxi2 ~]# mount -t nfs 192.168.1.6:/www/html/ /html //這時候共享才開始正常使用 [root@youxi2 ~]# ls /html passwd
還可以做開機掛載
[root@youxi2 ~]# vim /etc/fstab 192.168.1.6:/www/html /html nfs defaults 0 0 //添加一行
新建一個文件
[root@youxi2 ~]# touch /html/1.txt [root@youxi2 ~]# ls /html/ 1.txt passwd
8)NFS高並發下掛載優化常用參數(mount -o選項)
async:異步同步,此參數會提高I/O性能,但會降低數據安全(除非對性能要求很高,對數據可靠性不要求的場合。一般生產環境,不推薦使用)。
noatime:取消更新文件系統上的inode訪問時間,提升I/O性能,優化I/O目的,推薦使用。
nodiratime:取消更新文件系統上的directory inode訪問時間,高並發環境,推薦顯式應用該選項,提高系統性能,推薦使用。
當然也可以寫入/etc/fstab配置文件
192.168.1.6:/www/html /html nfs noatime,nodiratime 0 0
