NFS服務及自動掛載
文件共享服務器
概念,網絡文件系統,通過網絡,讓不同的系統、機器實現文件共享
工作原理:由多個模塊共同來完成,端口隨機,通過RPC代理程序來幫助連接NFS服務
一、環境
服務端:192.168.1.111
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
客戶端:192.168.1.107
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
二、nfs安裝配置-服務端
安裝環境支持
[root@localhost ~]# yum install nfs-utils -y
創建NFS共享目錄
[root@localhost ~]# mkdir -p /data/nfsshare
隨便複製一個文件至共享目錄
[root@localhost ~]# cp /etc/fstab /data/nfsshare
配置文件,exports此文件名固定
[root@localhost ~]# vim /etc/exports
/data/nfsshare 192.168.1.107(ro,sync) 對客戶端定義,ro爲只讀,rw爲可寫
注:也可以寫成網段的形式,要加/24,不可寫0.0.0.0,可以多行,如下:
/data/nfsshare 192.168.1.0/24(ro,sync)
數據導出
[root@localhost ~]# exportfs -r
重啓服務
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl restart nfs
注:#chmod o+w /data/nfsshare 如果客戶端要求可寫比如共享目錄,那麼需要加上這一句
關閉selinux及防火牆(這裏就不做防火牆配置了,直接關閉)
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# systemctl stop firewalld
三、nfs安裝配置-客戶端
安裝環境支持
[root@localhost ~]#yum install nfs-utils -y
查看服務端掛載信息
[root@localhost ~]# showmount -e 192.168.1.111
Export list for 192.168.1.111:
/data/nfsshare 192.168.1.0/24
創建本機掛載目錄
[root@localhost ~]# mkdir -p /data/nfs
臨時掛載
[root@localhost ~]# mount 192.168.1.111:/data/nfsshare /data/nfs
mount -t nfs 192.168.1.111:/data/nfsshare /data/nfs 也可以這樣寫,加-t nfs
測試
[root@localhost nfs]# ll /data/nfs
total 4
-rw-r--r--. 1 root root 541 May 17 11:01 fstab
-rw-r--r--. 1 root root 0 May 17 11:29 test
四、客戶端開機自動掛載(fstab配置文件實現開機自動掛載)
開機自動掛載分**"fstab配置文件"手動開機自動掛載與"autofs"工具自動掛載**
fstab配置文件手動開機自動掛載**
[root@localhost ~]# vim /etc/fstab 最後加入一行
192.168.1.111:/data/nfsshare /data/nfs nfs defaults 0 0
五、客戶端開機自動掛載(autofs工具實現自動掛載)
autofs爲守護進程,自動掛載,客戶端通過autofo工具,訪問時自動掛載,不使用後,5分鐘自行斷開,卸載。好處是可以隱藏掛載目錄
安裝autofs
[root@localhost ~]# yum install autofs -y
啓動autofs
[root@localhost ~]# systemctl start autofs
#rpm -qc autofs 查看autofs相關文件
[root@localhost ~]# rpm -qc autofs 查看autofs配置文件
/etc/auto.master 定義掛載點與掛載點配置文件
/etc/auto.misc 定義虛擬目錄、掛載源及權限等
/etc/auto.net
/etc/auto.smb
/etc/autofs.conf autofs配置文件,可定義超時卸載時間等,默認300秒,5分鐘
/etc/autofs_ldap_auth.conf
/etc/sysconfig/autofs
/usr/lib/systemd/system/autofs.service
開始配置,按照順序
1、vim /etc/autofs.conf 不做配置
2、vim /etc/auto.master 定義掛載點及掛載點的配置文件
[root@localhost ~]# vim /etc/auto.master
/data/nfs /etc/auto.nfs
3、vim /etc/auto.nfs 定義掛載點下的虛擬目錄
[root@localhost ~]# vim /etc/auto.nfs 定義掛載點下的虛擬目錄,可定義多行,一個虛擬目錄爲一行,定義不同的權限及目錄等
cd -fstype=nfs,rw,sync 192.168.1.111:/data/nfsshare
cd1 -fstype=nfs,ro,sync 192.168.1.111:/data/nfsshare
如此,當cd /data/nfs下時,裏面無文件,使用cd cd可進入虛擬目錄cd。當對cd進行訪問時,如ls,會發生自動掛載,裏面的內容爲掛載後的內容
重啓服務
[root@localhost ~]# systemctl restart autofs
設置自動掛載開機自啓動
[root@localhost ~]# systemctl enable autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.
測試
[root@localhost ~]#
[root@localhost ~]# cd /data/nfs
[root@localhost nfs]# ls 發現無文件
[root@localhost nfs]# ll cd 對虛擬目錄cd訪問時,會迅速發生掛載,可以看到裏面的文件了
total 4
-rw-r--r--. 1 root root 541 May 17 11:01 fstab
-rw-r--r--. 1 root root 0 May 17 11:29 test
-------------------------end