目錄
一.NFS服務器簡介
1.含義簡介:
2.工作原理簡介:
3.RPC服務與NFS服務配合使用
二.NFS配置文件參數命令介紹
1.主配置文件/etc/exports
2.日志文件/var/lib/nfs/
3.showmount命令
三.主配置文件/etc/exports掛載寫法
1.配置nfs服務端和客戶端
2.windows客戶端掛載測試
3.權限介紹
(1)rw/ro,服務器目錄的共享權限是可讀寫/只讀,但起決定作用的還是該目錄的rwx權限
(2)sync/async,sync指數據會同步寫入到內存與硬盤中,async則指數據會先暫存于內存當中,不直接寫入硬盤
(3)no_root_squash/root_squash,若客戶端在共享目錄里創(chuàng)建的文件的所屬者和所屬組是root用戶和root組,那么查看顯示文件的屬主和屬組時有兩種情況:
(4)all_squash/no_all_squash
(5)anonuid=,anongid=
四.在客戶端配置auto自動掛載和卸載
1.安裝autofs
2.auto建立客戶端數據文件/etc/auto.master時配置參數(可以省略)
3.配置自動掛載/卸載過程
?
一.NFS服務器簡介
1.含義簡介:
NFS,網絡文件系統(tǒng),是FreeBSD支持的文件系統(tǒng)之一,允許網絡中的不同的計算機、不同的操作系統(tǒng)之間通過TCP/IP網絡共享資源。
2.工作原理簡介:
本地NFS客戶端應用可以讀寫位于遠端NFS服務器(網絡)上的文件,NFS服務器可以讓計算機將網絡中的NFS服務器共享的目錄掛載到本地端的文件系統(tǒng)中,而在本地端的系統(tǒng)中看來,那個遠程主機的目錄類似于自己本地文件系統(tǒng)的一個磁盤分區(qū)。
3.RPC服務與NFS服務配合使用
?NFS的功能很多,每啟動一個功能就會啟用一些端口來傳輸數據,所以NFS的功能對應的端口并不固定,而是隨機取用一些未被使用的小于1024的端口用于傳輸。需要借助RPC(遠程過程調用)解決客戶端連接服務器(端口)的問題。當啟動NFS時會隨機選取幾個端口號,并主動向RPC注冊,此時RPC知道每個NFS功能所對應的端口號,RPC將端口號下發(fā)給客戶端,使得客戶端可以連接到正確的端口上去。RPC采用固定端口號Port?111來監(jiān)聽客戶端需求并向客戶端下發(fā)正確的端口號。
?注意:RPC要在啟動NFS之前啟動才能實現NFS向RPC注冊,如果重啟RPC服務,NFS原來注冊的數據會清楚,那么它管理的所有服務都需要重新啟動重新注冊。
二.NFS配置文件參數命令介紹
1.主配置文件/etc/exports
主配置文件如果不存在需要手動創(chuàng)建
編輯了主配置文件不需要重新啟動服務,使用exportfs (-r)命令進行(全部)加載配置文件即可(同步更新/etc/exports,/var/lib/nfs/xtab)
exportfs其他選項:
-a? ? 全部掛載或卸載/etc/exports中的內容
-u? ? 卸載單一目錄,可以和-a一起使用為卸載所有/etc/exports文件中的目錄
-v? ? 將詳細的信息輸出到屏幕上
2.日志文件/var/lib/nfs/
etab和xtab比較重要,etab主要記錄NFS共享出來的目錄的完整權限設置值,xtab主要記錄以往鏈接到這個NFS服務器的相關客戶端數據。
[root@sulibao ~]# ll /var/lib/nfs/
total 0
-rw-r--r-- 1 root root 0 Oct 14 2021 etab
-rw-r--r-- 1 root root 0 Jan 20 11:17 export-lock
drwx------ 2 root root 25 Jan 20 11:17 nfsdcltrack
-rw-r--r-- 1 root root 0 Oct 14 2021 rmtab
dr-xr-xr-x 11 root root 0 Dec 31 17:46 rpc_pipefs
drwx------. 4 rpcuser rpcuser 43 Jan 20 11:17 statd
-rw-r--r-- 1 rpcuser rpcuser 0 Oct 14 2021 state
drwxr-xr-x. 2 root root 6 Oct 14 2021 v4recovery
-rw-r--r-- 1 root root 0 Oct 14 2021 xtab
3.showmount命令
查看NFS共享出來的目錄資源
showmount -e 主機名/ip表示顯示NFS服務器的導出列表
三.主配置文件/etc/exports掛載寫法
1.配置nfs服務端和客戶端
如下是在Linux上創(chuàng)建服務端,并以Linux作為客戶端進行測試
(1)準備兩臺虛擬機,關閉SElinux和防火墻,保證網絡可用,有yum源,虛擬機1作為服務器端,虛擬機2作為客戶端。
兩臺虛擬機都下載rpcbind和nfs-utils
[root@sulibao ~]# yum install -y rpcbind
[root@sulibao ~]# yum install -y nfs-utils
(2)通過“共享目錄? 主機名/IP/網段(權限)”編寫配置進行掛載,權限可以為多個,寫在括號里面以逗號隔開
服務器端寫主配置文件
[root@sulibao ~]# vim /etc/exports
/nfsdir 192.168.2.0/24(rw)
注意:這里經常出現問題, (權限)與前面的字段之間多大了空格,此時權限不會生效!
(3)服務器端創(chuàng)建共享目錄和文件并寫入內容
[root@sulibao ~]# mkdir /nfsdir
[root@sulibao ~]# cd /nfsdir/
[root@sulibao nfsdir]# touch aaa.txt
[root@sulibao nfsdir]# vim aaa.txt
nfs-server
(4)先開啟rpcbind服務再開啟nfs-server服務
[root@sulibao nfsdir]# systemctl restart rpcbind
[root@sulibao nfsdir]# systemctl restart nfs-server
(5)在服務器端和客戶端分別進行測試是否能夠連接到共享目錄
服務器端
[root@sulibao nfsdir]# showmount -e 192.168.2.160
Export list for 192.168.2.160
/nfsdir 192.168.2.0/24
[root@sulibao nfsdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 11:44 aaa.txt
客戶端
[root@sulibao ~]# showmount -e 192.168.2.160
Export list for 192.168.2.160:
/nfsdir 192.168.2.0/24
(6)在客戶端進行測試是否能夠實現掛載
在客戶端創(chuàng)建一個掛載目錄,使用“mount 服務器IP:共享目錄? 掛載目錄”進行掛載
[root@sulibao ~]# mkdir /mountdir
[root@sulibao ~]# mount 192.168.2.160:/nfsdir /mountdir/
[root@sulibao ~]# mount //通過mount可以查看有沒有掛載記錄
192.168.2.160:/nfsdir on /mountdir type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.2.170,local_lock=none,addr=192.168.2.160)
[root@sulibao ~]# df -h 也可以通過df -h查看目錄掛載情況
Filesystem Size Used Avail Use% Mounted on
devtmpfs 5.1G 0 5.1G 0% /dev
tmpfs 5.1G 0 5.1G 0% /dev/shm
tmpfs 5.1G 9.8M 5.1G 1% /run
tmpfs 5.1G 0 5.1G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 35G 5.2G 30G 15% /
/dev/nvme0n1p1 1014M 257M 758M 26% /boot
tmpfs 1.1G 36K 1.1G 1% /run/user/0
/dev/sr0 11G 11G 0 100% /media/cdrom
192.168.2.160:/nfsdir 35G 5.1G 30G 15% /mountdir
在客戶端也能查看到在服務器端上寫的內容
[root@sulibao ~]# cd /mountdir/
[root@sulibao mountdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 11:44 aaa.txt
[root@sulibao mountdir]# cat aaa.txt
nfs-server
(7)注意:
這里主配置文件里設置的權限是可讀可寫但實際上其他用戶共享這個目錄時并不能新建或編輯文件
[root@sulibao mountdir]# touch bbb.txt
touch: cannot touch 'bbb.txt': Read-only file system
?需要在服務器端更改共享目錄權限,如若沒生效可以選擇試試重啟nfs-server
[root@sulibao nfsdir]# chmod 777 /nfsdir/
[root@sulibao nfsdir]# systemctl restart nfs-server.service
在客戶端就可以修改共享目錄內文件或者新建文件了
[root@sulibao mountdir]# touch bbb.txt
[root@sulibao mountdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
[root@sulibao mountdir]# vim aaa.txt?
2.windows客戶端掛載測試
(1)這里以一個新例子做介紹
[root@localhost ~]# showmount -e #160主機上搭建了nfs服務端
Export list for localhost.localdomain:
/root/data *
(2)到windows上進行掛載,方法1
到控制面板開啟NFS客戶端和管理工具功能
?右鍵此電腦,點擊映射網絡驅動器
如下書寫
?等待連接完成即可
?
(3)方法2
開啟NFS客戶端和管理工具后直接在cmd內進行掛載
C:\Users\24107>mount 192.168.2.160:/root/data Z:
Z: 現已成功連接到 192.168.2.160:/root/data
命令已成功完成。
?
?(4)如上通過驅動器映射和命令行的掛載若需要卸載,建議都使用命令行進行卸載
C:\Users\24107>umount Z:
正在斷開連接 Z: \\192.168.2.160\root\data
命令已成功完成。
3.權限介紹
(1)rw/ro,服務器目錄的共享權限是可讀寫/只讀,但起決定作用的還是該目錄的rwx權限
(2)sync/async,sync指數據會同步寫入到內存與硬盤中,async則指數據會先暫存于內存當中,不直接寫入硬盤
(3)no_root_squash/root_squash,若客戶端在共享目錄里創(chuàng)建的文件的所屬者和所屬組是root用戶和root組,那么查看顯示文件的屬主和屬組時有兩種情況:
- no_root_squash指將root屬主和組映射為root屬主和組(默認的)
服務器端修改主配置文件/etc/exports,重新加載配置文件并測試
[root@sulibao nfsdir]# vim /etc/exports
/nfsdir 192.168.2.0/24(rw,no_root_squash)
[root@sulibao nfsdir]# exportfs -r
[root@sulibao nfsdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
客戶端測試
[root@sulibao mountdir]# touch ccc.txt
[root@sulibao mountdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
- root_squash指將root屬主和組映射為匿名用戶和組(默認的)
?服務器端修改主配置文件/etc/exports,重新加載主配置文件并測試
[root@sulibao nfsdir]# vim /etc/exports
/nfsdir 192.168.2.0/24(rw,root_squash)
[root@sulibao nfsdir]# exportfs -r
[root@sulibao nfsdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt
?客戶端測試
[root@sulibao mountdir]# touch ddd.txt
[root@sulibao mountdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt
(4)all_squash/no_all_squash
(1)all_squash指客戶端普通用戶創(chuàng)建文件時,會將文件的用戶和組映射為匿名用戶和匿名組
服務器端修改朱培志文件,重新加載配置文件并測試
[root@sulibao nfsdir]# vim /etc/exports
/nfsdir 192.168.2.0/24(rw,no_root_squash,all_squash)
[root@sulibao nfsdir]# exportfs -r
[root@sulibao nfsdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt
客戶端普通用戶下測試
[root@sulibao mountdir]# su - xiaosu
[xiaosu@sulibao ~]$ cd /mountdir/
[xiaosu@sulibao mountdir]$ ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt
[xiaosu@sulibao mountdir]$ touch ggg.txt
[xiaosu@sulibao mountdir]$ ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt
(2)no_all_squash指客戶端普通用戶創(chuàng)建的文件的UID和GID是多少服務器端就映射為UID和GID對應的用戶
服務器端修改主配置,重新加載配置文件
[root@sulibao nfsdir]# vim /etc/exports
/nfsdir 192.168.2.0/24(rw,no_root_squash,no_all_squash)
[root@sulibao nfsdir]# exportfs -r
[root@sulibao nfsdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt
客戶端普通用戶下對應服務器端進行測試,這里普通用戶的UID和GID是1000,那么服務器上就會映射出服務器上UID和GID為1000的用戶或者UID和GID值
[root@sulibao mountdir]# su - xiaosu
[xiaosu@sulibao ~]$ cd /mountdir/
[xiaosu@sulibao mountdir]$ ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt
-rw-r--r-- 1 root root 0 Jan 20 15:41 hhh.txt
[xiaosu@sulibao mountdir]$ touch iii.txt
[xiaosu@sulibao mountdir]$ ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt
-rw-r--r-- 1 root root 0 Jan 20 15:41 hhh.txt
-rw-rw-r-- 1 xiaosu xiaosu 0 Jan 20 15:42 iii.txt
[xiaosu@sulibao mountdir]$ cat /etc/passwd |grep xiaosu
xiaosu:x:1000:1000:xiaosu:/home/xiaosu:/bin/bash
//客戶端普通用戶xiaosu的UID和GID為1000
[root@sulibao nfsdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt
-rw-r--r-- 1 root root 0 Jan 20 15:41 hhh.txt
-rw-rw-r-- 1 sulibao sulibao 0 Jan 20 15:42 iii.txt
[root@sulibao nfsdir]# cat /etc/passwd | grep 1000
sulibao:x:1000:1000:sulibao:/home/sulibao:/bin/bash
//服務器端上UID=GID=1000匹配的是普通用戶sulibao,就映射sulibao
[root@sulibao mountdir]# useradd -u 6666 susu
[root@sulibao mountdir]# cat /etc/passwd | grep 6666
susu:x:6666:6666::/home/susu:/bin/bash
[root@sulibao mountdir]# chown susu:susu hhh.txt
[root@sulibao mountdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt
-rw-r--r-- 1 susu susu 0 Jan 20 15:41 hhh.txt
-rw-rw-r-- 1 xiaosu xiaosu 0 Jan 20 15:42 iii.txt
//客戶端普通用戶susu的UID和GID為6666,將hhh.txt屬主和組改為susu
[root@sulibao nfsdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt
-rw-r--r-- 1 6666 6666 0 Jan 20 15:41 hhh.txt
-rw-rw-r-- 1 sulibao sulibao 0 Jan 20 15:42 iii.txt
//服務器端沒有與UID=GID=6666的用戶和組,就直接映射UID和GID
(5)anonuid=,anongid=
將文件的用戶和組映射為指定的UID和GID或相匹配的用戶,若不指定默認為65534(nfsnobody)
服務器端修改主修改配置文件并重新加載配置文件
[root@sulibao nfsdir]# vim /etc/exports
/nfsdir 192.168.2.0/24(rw,no_root_squash,all_squash,anonuid=5555,anongid=5555)
[root@sulibao nfsdir]# exportfs -r
[root@sulibao nfsdir]# useradd -u 5555 slb
客戶端測試,客戶端沒有此UID和GID的用戶就會顯示UID和GID值,有就顯示值相匹配的用戶
[root@sulibao mountdir]# touch qqq.txt
[root@sulibao mountdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt
-rw-r--r-- 1 susu susu 0 Jan 20 15:41 hhh.txt
-rw-rw-r-- 1 xiaosu xiaosu 0 Jan 20 15:42 iii.txt
-rw-r--r-- 1 5555 5555 0 Jan 20 16:06 qqq.txt
//客戶端上沒有
[root@sulibao nfsdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jan 20 15:40 ggg.txt
-rw-r--r-- 1 6666 6666 0 Jan 20 15:41 hhh.txt
-rw-rw-r-- 1 sulibao sulibao 0 Jan 20 15:42 iii.txt
-rw-r--r-- 1 slb slb 0 Jan 20 16:06 qqq.txt
//服務器端上剛創(chuàng)建了slb用戶,UID和GID相匹配,映射slb用戶
四.在客戶端配置auto自動掛載和卸載
auto自動掛載主要是用來實現當客戶端有掛載需求時才進行掛載,基于上文的服務器虛擬機和客戶端虛擬機進行演示
1.安裝autofs
[root@sulibao nfsdir]# yum install -y autofs
2.auto的配置文件介紹
(1)/etc/auto.master
用于auto掛載的設置,auto掛載信息的映射關系
格式:客戶端掛載目錄? ?(參數)? 掛載auto配置文件的目錄
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc /etc/auto.misc //表示/misc的掛載信息在/etc/auto.misc中
#
# NOTE: mounts done from a hosts map will be mounted with the
# "nosuid" and "nodev" options unless the "suid" and "dev"
# options are explicitly given.
#
/net -hosts
#
# Include /etc/auto.master.d/*.autofs
# The included files must conform to the format of this file.
#
+dir:/etc/auto.master.d
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
(2)/etc/autofs.conf
autofs服務的配置,主要關注timeout,表示切除目錄后多少時間自動解除掛載
timeout = 300 //默認是300秒
(3)/etc/auto.misc
auto掛載的信息
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
掛載目錄 掛載選項 :掛載設備
# the following entries are samples to pique your imagination
#linux -ro,soft,intr ftp.example.org:/pub/linux
#boot -fstype=ext2 :/dev/hda1
#floppy -fstype=auto :/dev/fd0
#floppy -fstype=ext2 :/dev/fd0
#e2floppy -fstype=ext2 :/dev/fd0
#jaz -fstype=ext2 :/dev/sdc1
#removable -fstype=ext2 :/dev/hdd
2.auto建立客戶端數據文件/etc/auto.master時配置參數(可以省略)
參數 | 實現功能 |
fg/bg | fg表示掛載行為在前臺執(zhí)行,bg表示掛載行為在后臺執(zhí)行。前臺執(zhí)行,則mount會持續(xù)嘗試掛載,直到成功或超時為止。后臺執(zhí)行,mount會在后臺持續(xù)多次進行mount,不會影響到前臺的程序運行。 |
soft/hard | hard表示當兩者之間的任何一臺主機脫機,則RPC會持續(xù)地呼叫,直到對方恢復連接為止。如果是soft的話,那RPC會在超時后重復呼叫,非持續(xù)呼叫 |
intr | 當使用hard方式掛載時,若加上intr這個參數,則當RPC持續(xù)呼叫時,該次的呼叫是可以被中斷的 |
rsize/wsize | 讀出(rsize)與寫入(wsize)的區(qū)塊大小,設置值可以影響客戶端與服務器端傳輸數據的緩沖記憶容量 |
3.配置自動掛載/卸載過程
服務器端不動,autofs主要配置在客戶端
(1)安裝好autofs后編輯配置文件/etc/auto.master
找到/misc ? /etc/auto.misc這一行,在其后或前寫一行,指定/etc/auto.nfs的自動掛載信息在/nfscli下
[root@sulibao ~]# vim /etc/auto.master
/nfscli ?/etc/auto.nfs
/misc ? /etc/auto.misc
//nfscli不需要手動創(chuàng)建,稍后重啟autofs服務會自動創(chuàng)建
(2)編輯新建剛才指定的文件/etc/auto.nfs
指定客戶端掛載地址和服務器端共享目錄地址
[root@sulibao ~]# vim /etc/auto.nfs
mountdir 192.168.2.160:/nfsdir
(3)重啟autofs服務,查看/nfscli目錄是否自動創(chuàng)建
[root@sulibao ~]# systemctl restart autofs.service
[root@sulibao ~]# ls -ld /nfscli
drwxr-xr-x 2 root root 0 Jan 20 17:49 /nfscli
(4)觸發(fā)自動掛載(切入客戶端掛載目錄mountdir即觸發(fā))
切入前先使用mount命令查看掛載記錄方便查看是否生效(如果上文配置的臨時掛載仍處于掛載狀態(tài),可以使用“umount -lf 目錄”取消掛載)
[root@sulibao ~]# mount | tail -5
/dev/sr0 on /media/cdrom type iso9660 (ro,relatime,nojoliet,check=s,map=n,blocksize=2048,uid=0,gid=0,dmode=500,fmode=400)
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
/etc/auto.nfs on /nfscli type autofs (rw,relatime,fd=5,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=243315)
/etc/auto.misc on /misc type autofs (rw,relatime,fd=11,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=241423)
-hosts on /net type autofs (rw,relatime,fd=17,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=243320)
切入目錄/nfscli,此時查看該目錄內并沒有內容,但是仍然可以進入客戶端掛載目錄mountdir,進入可以查看到服務器端共享目錄內容,通過mount查看,自動掛載成功
[root@sulibao ~]# cd /nfscli
[root@sulibao nfscli]# ll
total 0
[root@sulibao nfscli]# cd mountdir
[root@sulibao mountdir]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 20 14:56 aaa.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 14:56 bbb.txt
-rw-r--r-- 1 root root 0 Jan 20 14:58 ccc.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:05 ddd.txt
-rw-r--r-- 1 root root 0 Jan 20 15:09 eee.txt
-rw-r--r-- 1 nobody nobody 0 Jan 20 15:37 fff.txt
-rw-rw-r-- 1 nobody nobody 0 Jan 20 15:40 ggg.txt
-rw-r--r-- 1 6666 6666 0 Jan 20 15:41 hhh.txt
-rw-rw-r-- 1 xiaosu xiaosu 0 Jan 20 15:42 iii.txt
-rw-r--r-- 1 5555 5555 0 Jan 20 16:06 qqq.txt
[root@sulibao mountdir]# mount | tail -5
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
/etc/auto.nfs on /nfscli type autofs (rw,relatime,fd=5,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=243315)
/etc/auto.misc on /misc type autofs (rw,relatime,fd=11,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=241423)
-hosts on /net type autofs (rw,relatime,fd=17,pgrp=48620,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=243320)
192.168.2.160:/nfsdir on /nfscli/mountdir type nfs4 (rw,relatime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.2.170,local_lock=none,addr=192.168.2.160)
(5)設置自動取消掛載
編輯/etc/autofs.conf,方便測試將timeout時間改短一點
[root@sulibao mountdir]# vim /etc/autofs.conf
# Define default options for autofs.
#
[ autofs ]
#
# master_map_name - default map name for the master map.
#
#master_map_name = auto.master
#
# timeout - set the default mount timeout in secons. The internal
# program default is 10 minutes, but the default installed
# configuration overrides this and sets the timeout to 5
# minutes to be consistent with earlier autofs releases.
#
timeout = 10
[root@sulibao mountdir]# systemctl restart autofs.service
[root@sulibao mountdir]# cd /nfscli/mountdir/ //重啟測試能夠自動掛載
[root@sulibao mountdir]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 5.1G 0 5.1G 0% /dev
tmpfs 5.1G 0 5.1G 0% /dev/shm
tmpfs 5.1G 9.8M 5.1G 1% /run
tmpfs 5.1G 0 5.1G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 35G 5.2G 30G 15% /
/dev/nvme0n1p1 1014M 257M 758M 26% /boot
tmpfs 1.1G 36K 1.1G 1% /run/user/0
/dev/sr0 11G 11G 0 100% /media/cdrom
192.168.2.160:/nfsdir 35G 5.1G 30G 15% /nfscli/mountdir
切出掛載目錄觸發(fā)取消掛載再等10秒再查看掛載情況文章來源:http://www.zghlxwxcb.cn/news/detail-761471.html
[root@sulibao mountdir]# cd
[root@sulibao ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 5.1G 0 5.1G 0% /dev
tmpfs 5.1G 0 5.1G 0% /dev/shm
tmpfs 5.1G 9.8M 5.1G 1% /run
tmpfs 5.1G 0 5.1G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 35G 5.2G 30G 15% /
/dev/nvme0n1p1 1014M 257M 758M 26% /boot
tmpfs 1.1G 36K 1.1G 1% /run/user/0
/dev/sr0 11G 11G 0 100% /media/cdrom
//取消掛載成功
?文章來源地址http://www.zghlxwxcb.cn/news/detail-761471.html
到了這里,關于NFS服務器簡介、在Linux上搭建NFS服務器和客戶端,使用autofs進行NFS客戶端自動掛載和卸載詳解的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!