1. 程式人生 > 實用技巧 >linux入門系列16--檔案共享之Samba和NFS

linux入門系列16--檔案共享之Samba和NFS

前一篇文章“linux入門系列15--檔案傳輸之vsftp服務”講解了檔案傳輸,本篇繼續講解檔案共享相關知識。

檔案共享在生活和工作中非常常見,比如同一團隊中不同成員需要共同維護同一個文件,在windows環境下,通常會選用第三方協作工具,如騰訊文件,石墨文件等等。

之前講解了基於ftp的檔案傳輸,為何還會單獨講解檔案共享呢?試想一下,假如我們要修改伺服器上某個檔案,如果使用ftp的話,需要先下載下來進行修改,然後在上傳到伺服器。這樣是很繁瑣的,這時候就可以使用檔案共享來解決這個問題。

檔案傳輸和檔案共享有本質的區別,基於ftp協議的檔案傳輸可以實現不同機器之間檔案的傳輸和拷貝,會產生多個副本。而檔案共享則只有一個副本,各個客戶端連線到共享伺服器操作的是同一份檔案。

Linux環境下可以通過Samba服務或NFS服務來實現檔案共享,下面分別進行介紹。

一、檔案共享服務Samba

1.1 Samba概述

為了解決區域網內的檔案和印表機等資源的共享問題,微軟和英特爾與1987年共同制定了 SMB(Server Messages Block,伺服器訊息塊)協議,這使得多個主機之間共享檔案變得簡單。

到了1991年,一個國外牛逼大學生 為了解決 Linux 系統 與 Windows 系統之間的檔案共享問題,基於SMB協議開發出了SMBServer服務程式。它是一款開源的檔案共享軟體、只需要簡單的配置就能實現檔案共享。

後來作者把它註冊商標為Samba,它現在已經成為Linux系統和Windows系統之間共享檔案的最佳選擇

1.2 Samba安裝及配置檔案

安裝之前規劃並準備實驗環境

角色 作業系統 ip地址 主機名稱
Samba伺服器 Centos7 192.168.78.101 sambaserver
Samba客戶端 Centos7 192.168.78.102 cliet
Samba客戶端 Windows10 宿主機即可

Samba服務程式的名字就是軟體包的名字,安裝後的服務名叫smb,在sambaserver主機上,通過rpm名稱檢視系統是否已經安裝,如果沒有安裝則通過Yum軟體倉庫進行安裝。

[root@sambaserver ~]# rpm -q samba
package samba is not installed
[root@sambaserver ~]# yum install samba
Loaded plugins: fastestmirror, langpacks
...省略部分內容
Installed:
  samba.x86_64 0:4.1.1-31.el7                                               
Complete!
[root@sambaserver yum.repos.d]# ^C
[root@sambaserver yum.repos.d]# rpm -q samba
samba-4.1.1-31.el7.x86_64
[root@sambaserver yum.repos.d]# 

安裝完成成後,配置檔案所在目錄為:/etc/samba/,主配置檔案為:smb.conf

[root@sambaserver yum.repos.d]# ll /etc/samba/
total 20
-rw-r--r--. 1 root root    20 Dec  3 01:48 lmhosts
-rw-r--r--. 1 root root   706 Dec  3 01:48 smb.conf
-rw-r--r--. 1 root root 11327 Dec  3 01:48 smb.conf.example
[root@sambaserver yum.repos.d]# 

接下來我們看下配置檔案裡的主要內容,還是按“linux入門系列15--檔案傳輸之vsftp服務”中的2.4.1提到的老規矩,將註釋以及空行資訊去掉,便於觀察配置項。

[root@sambaserver yum.repos.d]# mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
[root@sambaserver yum.repos.d]# cat /etc/samba/smb.conf.bak | grep -v "#" |grep  -v ";" | grep -v "^$">/etc/samba/smb.conf 

以上指令碼通過grep命令-v引數反向選擇,去掉以#和分號開頭的註釋資訊,用^$過濾掉空白行,過濾無用資訊後通過重定向覆寫到原始配置檔案中。

接下來看看主配置檔案的內容:

[root@sambaserver yum.repos.d]# cat /etc/samba/smb.conf
[global]
        workgroup = SAMBA #工作組名稱
        security = user  #安全驗證的方式,總共有4種:share、user、server、domain
        passdb backend = tdbsam #定義使用者後臺的型別,共有3種:smbpasswd、tdsam、ldapsam
        printing = cups 
        printcap name = cups
        load printers = yes #設定在 Samba 服務啟動時是否共享印表機裝置
        cups options = raw  #印表機的選項
[homes]  #共享引數
        comment = Home Directories #描述資訊
        valid users = %S, %D%w%S
        browseable = No #指定共享資訊是否在“網路上的芳鄰”中可見
        read only = No
        inherit acls = Yes
[printers] #印表機共享引數
        comment = All Printers
        path = /var/tmp
        printable = Yes
        create mask = 0600
        browseable = No
[print$]
        comment = Printer Drivers
        path = /var/lib/samba/drivers
        write list = @printadmin root
        force group = @printadmin
        create mask = 0664
        directory mask = 0775
[root@sambaserver yum.repos.d]#

我直接把註釋新增到每項配置的後邊,方便檢視。

其中[homes]引數為來訪使用者的家目錄共享資訊,[printers]引數為共享的打 印機裝置,如果不需要可以手動直接刪除即可。

1.3 Samba檔案共享實操

通過前文可以看到,Samba服務程式的主配置檔案包括全域性配置引數和區域配置引數。顧名思義,全域性配置引數用於設定整體的資源共享環境,對每一個獨立的共享資源都有效;而區域配置引數則用於設定單獨的共享資源,且只對該資源有效。

下面我們就來配置並使用Samba服務

1.3.1 檔案共享伺服器配置

(1)Samba主配置檔案配置

[root@sambaserver yum.repos.d]# cd /etc/samba/
[root@sambaserver samba]# vim smb.conf
...省略原有內容
[database]   #共享名稱設為database
        comment=請勿隨意修改資料庫喲  #提示資訊
        path=/home/database  #共享目錄/home/database
        public=no    #關閉所有人可見
        writeable=yes #允許寫入操作

新增以上內容並儲存退出。

補充一下主配置檔案中全域性配置global的2個引數,security和passdb backend 。

從1.2中講到的主配置檔案中可以看出,它們的預設值分別為user和tdbsam。

其中security有4種安裝認證方式,分別如下:

  • share:來訪主機無需驗證口令,比較方便,但安全性很差

  • user:需驗證來訪主機提供的口令後才可以訪問,提升了安全性

  • server:使用獨立的遠端主機驗證來訪主機提供的口令(集中管理賬戶)

  • domain:使用域控制器進行身份驗證

passdb backend有3種方式,定義使用者後臺的型別:

  • smbpasswd:使用 smbpasswd 命令為系統使用者設定 Samba 服務程式的密碼

  • tdbsam:建立資料庫檔案並使用 pdbedit 命令建立 Samba 服務程式的使用者

  • ldapsam:基於 LDAP 服務進行賬戶驗證

接下來我們就採用預設的user驗證模式來進行設定,其他模式的搭配使用暫時不做講解。

(2)建立訪問共享資源的賬號

從預設配置檔案中可以看出,在RHEL7中Samba採用預設的user認證模式,可以確保僅讓有密碼且受信任的使用者訪問共享資源,需要先建立賬戶資訊資料庫,並且要求賬戶必須在當前系統中已經存在。

與前文講解ftp時類似,要求使用者在當前系統中已經存在,是為了避免在建立檔案時引起檔案許可權屬性混亂而引發錯誤。

用於管理 SMB 服務程式的賬戶資訊資料庫的命令為:pdbedit。

語法格式:

​ pdbedit [引數] 賬戶

引數:

引數 作用
-a 建立Samba使用者
-x 刪除Samba使用者
-L 列出賬戶列表
-Lv 列出賬戶詳細資訊的列表

先建立使用者samba,並設定密碼為:123456

[root@sambaserver samba]# useradd -s /sbin/nologin samba
[root@sambaserver samba]# passwd samba 
Changing password for user samba.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@sambaserver samba]# id samba
uid=1001(samba) gid=1001(samba) groups=1001(samba)
[root@sambaserver samba]# 

使用功能pdbedit建立samba服務的使用者,密碼為888888(注意此處的密碼不是samba系統使用者登入的密碼,雖然也可以設定密碼給之前密碼相同,但一定要分清楚這2個密碼是單獨的,不要弄混淆

[root@sambaserver samba]# pdbedit -a -u samba
new password: #此處輸入密碼,密碼不一定要給samba使用者登入系統的密碼一致
retype new password: #重複輸入密碼
Unix username:        samba
NT username:          
Account Flags:        [U          ]
User SID:             S-1-5-21-3641161961-465911512-1567372236-1000
Primary Group SID:    S-1-5-21-3641161961-465911512-1567372236-513
Full Name:            
Home Directory:       \\sambaserver\samba
HomeDir Drive:        
Logon Script:         
Profile Path:         \\sambaserver\samba\profile
Domain:               SAMBASERVER
Account desc:         
Workstations:         
Munged dial:          
Logon time:           0
Logoff time:          Wed, 06 Feb 2036 23:06:39 CST
Kickoff time:         Wed, 06 Feb 2036 23:06:39 CST
Password last set:    Wed, 22 Jan 2020 14:45:50 CST
Password can change:  Wed, 22 Jan 2020 14:45:50 CST
Password must change: never
Last bad password   : 0
Bad password count  : 0
Logon hours         : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@sambaserver samba]# 

(3)建立共享資源的目錄

在前面的第一步中我們配置了共享目錄為/home/database。因此我們來建立此目錄,需要考慮到檔案讀寫許可權問題。

[root@sambaserver samba]# mkdir /home/database
[root@sambaserver samba]# chown -Rf samba:samba /home/database/
[root@sambaserver samba]# 

(4)SELinux上下文和策略設定

由於/home目錄是系統中普通使用者的家目錄,因此不僅要考慮檔案讀寫許可權,還要注意SELinux安全上下文以及域策略所帶來的限制(原始的主配置檔案中有關於SELinux安全上下文策略的配置說明)。

設定SELinux安全上下文

[root@sambaserver samba]# semanage fcontext -a -t samba_share_t /home/database
[root@sambaserver samba]# restorecon -Rv /home/database/
restorecon reset /home/database context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:samba_share_t:s0
[root@sambaserver samba]# 

除了設定上下文,還需要設定SELinux域相關策略,開啟相關的策略即可

[root@sambaserver samba]# getsebool -a |grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
use_samba_home_dirs --> off
virt_sandbox_use_samba --> off
virt_use_samba --> off
[root@sambaserver samba]# setsebool -P samba_enable_home_dirs on
[root@sambaserver samba]# 

(5)防火牆設定

samba服務名稱為smb,重啟並設為開機啟動,清空防火牆策略

[root@sambaserver samba]# systemctl restart smb
[root@sambaserver samba]# systemctl enable smb.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
[root@sambaserver samba]# iptables -F

至此,就可以用客戶端使用samba服務實現檔案共享了

先在共享目錄準備一個檔案用於客戶端共享

[root@sambaserver home]# cd database/
[root@sambaserver database]# ll
total 0
[root@sambaserver database]# echo "samba server">samba.txt
[root@sambaserver home]# chown -Rf samba:samba /home/database/
[root@sambaserver database]# ll
total 4
-rw-r--r--. 1 samba samba 13 Jan 22 15:44 samba.txt

這樣客戶端就可以檢視並編輯此共享檔案了。

Samba支援Windows、Linux、macOS之間檔案共享,接下來演示客戶端是Linux和Windows的情況。

1.3.2 Linux客戶端訪問檔案共享服務

在之前準備的samba客戶端主機上安裝客戶端後,用之前的samba賬號密碼就可以共享檔案了。

(1)安裝共享客戶端

[root@cliet ~]# yum install cifs-utils
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
...省略部分內容
Installed:
  cifs-utils.x86_64 0:6.2-10.el7
Complete!
[root@cliet ~]# 

(2)建立認證檔案

[root@cliet ~]# vim auth.smb
username=samba
password=888888
domain=SAMBA

儲存並退出,使用者密碼為之前1.3.1第二步中建立的samba使用者及對應的密碼,domain為主配置檔案中的域。

為了保證不被其他人隨意看到,最後把這個認證檔案的許可權修改為僅root管理才能夠讀寫。

[root@cliet ~]# ll auth.smb 
-rw-r--r--. 1 root root 44 Jan 22 15:37 auth.smb
[root@cliet ~]# chmod 600 auth.smb 
[root@cliet ~]# ll auth.smb        
-rw-------. 1 root root 44 Jan 22 15:37 auth.smb
[root@cliet ~]# 

(3)本地建立目錄掛載Samba共享目錄

[root@cliet ~]# mkdir /database
[root@cliet ~]# vim /etc/fstab 
//192.168.78.101/database /database cifs credentials=/root/auth.smb 0 0
#儲存並退出
[root@cliet ~]# mount -a

這樣就可以檢視並修改共享伺服器內的內容

[root@cliet ~]# ll /database/
total 4
-rw-r--r--. 1 root root 13 Jan 22 15:44 samba.txt
[root@cliet ~]# cat /database/samba.txt 
samba server
[root@cliet ~]# 
1.3.3 Windows訪問檔案共享服務

本演示以win10為例

在開始選單輸入共享伺服器地址

在彈出彈出登入框輸入正確的賬號密碼

進入共享目錄

進入目錄後,就可以執行檢視、寫入、更名、刪除檔案等操作,在samba伺服器和對應的其他客戶端就可以看到檔案的變化。因此就實現了檔案共享。

二、網路檔案系統NFS

如果你只是在Linux主機之間共享檔案,那NFS將更加簡單。

NFS(Network File System即網路檔案系統)服務可以將遠端 Linux 系統上的檔案共享資源掛載到本地主機的目錄上,從而使得客戶端基於TCP/IP協議,像使用本地主機上的資源一樣讀寫遠端Linux系統上的共享檔案。

虛擬機器準備,可以單獨新克隆兩臺虛擬機器,此處我就直接用原來的2臺機器,只是要注意為了避免上一實驗的干擾,將之前的Samba客戶端作為nfs的伺服器,將Samba伺服器當作nfs的客戶端。規劃如下:

角色 作業系統 ip地址 主機名稱
NFS客戶端 Centos7 192.168.78.101 sambaserver
NFS伺服器 Centos7 192.168.78.102 cliet

2.1 NFS伺服器配置

(1)安裝NFS

RHEL7系統中預設已經安裝了NFS服務,可以通過rpm命令檢視,當然也可以直接執行如下安裝命令,如果有新的包會自動更新,如果已是最新版本則提示Nothing to do。

[root@cliet ~]# rpm -q nfs-utils
nfs-utils-1.3.0-0.65.el7.x86_64
[root@cliet ~]# yum install nfs-utils
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package 1:nfs-utils-1.3.0-0.65.el7.x86_64 already installed and latest version
Nothing to do
[root@cliet ~]# 

NFS包名為nfs-utils。

(2)在NFS伺服器建立共享目錄

[root@cliet ~]# mkdir /nfs
[root@cliet ~]# chmod -Rf 777 /nfs/
[root@cliet ~]# echo "nfs server">/nfs/nfs.txt
[root@cliet ~]# chmod -Rf 777 /nfs/
[root@cliet ~]# ll /nfs/           
total 4
-rwxrwxrwx. 1 root root 11 Jan 22 23:11 nfs.txt
[root@cliet ~]# 

建立目錄,修改許可權,並建立共享檔案。

(3)NFS服務配置

NFS服務程式配置檔案為/etc/exports,預設為空。採用如下格式定義要共享的目錄和相應的許可權。

格式:共享目錄的路徑 允許訪問的 NFS 客戶端(共享許可權引數)

相關引數及作用如下

引數 作用
ro 只讀
rw 讀寫
root_squash 當NFS客戶端以root管理員訪問時,對映為NFS伺服器的匿名使用者
no_root_squash 當 NFS客戶端以root管理員訪問時,對映為NFS伺服器的root管理員
all_squash 無論NFS客戶端使用什麼賬戶訪問,均對映為NFS伺服器的匿名使用者
sync 同時將資料寫入到記憶體與硬碟中,保證不丟失資料
async 優先將資料儲存到記憶體,然後再寫入硬碟;這樣效率更高,但可能會 丟失資料

按上邊的語法格式,把第一步建立的nfs目錄共享給192.168.78.0/24網段內的所有主機,讓這些主機都有讀寫許可權。為了最大限度保證資料不丟失,在將資料寫入到NFS伺服器的硬碟中後才會結束操作,同時把來訪客戶端root管理員對映為本地的匿名使用者,配置如下:

[root@cliet ~]# vim /etc/exports
/nfs 192.168.78.*(rw,sync,root_squash)

儲存並退出,注意ip地址與許可權之間沒有空格。

(4)設定防火牆策略

直接清空iptables防火牆的預設策略,以免預設的防火牆策 略禁止正常的NFS共享服務。

[root@cliet ~]# iptables -F

(5)啟動NFS服務程式

[root@cliet ~]# systemctl restart rpcbind
[root@cliet ~]# systemctl enable rpcbind
[root@cliet ~]# systemctl start nfs-server
[root@cliet ~]# systemctl enable nfs-server

在使用NFS服務進行檔案共享之前,需要使用RPC(Remote Procedure Call,遠端過程呼叫)服務將NFS伺服器的I 地址和埠號等資訊傳送給客戶端。

因此,在啟動NFS服務之前,還需要重啟並啟用 rpcbind 服務程式,並將這兩個服務一併加入開機啟動項中。

至此NFS伺服器端配置完成,接下來配置NFS客戶端。

2.2 NFS客戶端使用

(1)檢視NFS伺服器

檢視NFS伺服器遠端共享資訊使用showmount命令。

語法:

showmount [引數] 伺服器ip

引數:

引數 作用
-e 顯示NFS伺服器的共享列表
-a 顯示本機掛載的檔案資源的情況
-v 顯示版本號

輸出格式為:“共享的目錄名稱 允許使用客戶端地址”

[root@sambaserver database]# showmount -e 192.168.78.102
Export list for 192.168.78.102:
/nfs 192.168.78.*
[root@sambaserver database]#

(2)建立目錄並掛載

[root@sambaserver database]# mkdir /nfs
[root@sambaserver database]# mount -t nfs 192.168.78.102:/nfs /nfs
[root@sambaserver database]# ll /nfs/
total 4
-rwxrwxrwx. 1 root root 11 Jan 22 23:11 nfs.txt

使用mount命令並結合-t 引數,指定要掛載的檔案系統的型別為nfs,並在命令後面寫上伺服器的 IP地址、伺服器上的共享目錄以及要掛載到本地系統的目錄/nfs,這樣就可以看到共享的目錄檔案了。

(3)掛載資訊持久化

為了保證上一步驟的掛載能一直生效,需要將其寫入到fstab檔案中。

[root@sambaserver database]# vim /etc/fstab 
...省略無關內容
192.168.78.102:/nfs /nfs nfs defaults 0 0

這樣就在本地完成了伺服器的檔案共享。

三、自動掛載服務autofs

3.1 autofs概述

無論是之前講解本地yum源的安裝還是本文講解的Samba和NFS服務,都需要將掛載資訊接入到/etc/fstab檔案中,這樣才會使共享資源自動隨伺服器開機而進行掛載。

但是如果掛載的遠端資源太多,則會給網路頻寬和硬體資源帶來很大的壓力。如果掛載後長期不使用,會造成資源的浪費。

為了不造成浪費,可以在每次需要使用才才去手動掛載,但是這樣操作又非常繁瑣。為了解決這個問題,autofs自動掛載服務應運而生。

與mount命令不同,autofs服務程式是一 種Linux系統守護程序,當檢測到使用者檢視訪問一個尚未掛載的檔案系統時,將自動掛載該檔案系統。

簡單說就是,將掛載資訊寫入/etc/fstab檔案後,系統在每次開機時都自動將其掛載,而autofs 服務程式則是在使用者需要使用該檔案系統時才去動態掛載,從而節約了網路資源和伺服器的硬體資源。

3.2 autofs安裝及配置檔案

3.2.1 autofs安裝
[root@sambaserver database]# yum install autofs
Loaded plugins: fastestmirror, langpacks
...省略部分內容
Installed:
  autofs.x86_64 1:5.0.7-106.el7
Dependency Installed:
  hesiod.x86_64 0:3.2.1-3.el7
Complete!
[root@sambaserver database]# 
3.2.2 配置檔案解釋

autofs服務程式主配置檔案為:/etc/auto.master,我們一般採用主配置和子配置的方式進行配置。原因是生產環境中,可能會同時管理很多裝置的掛載操作,如果把所有掛載資訊都寫入主配置檔案,一旦內容多了將難以管理,且不利於服務執行效率。

主配置檔案中採用“掛載目錄 子配置檔案”的格式填寫,掛載目錄是裝置掛載位置的上一級目錄。例如,光碟裝置一般掛載到/media/cdrom目錄中,那麼主配置檔案中的掛載目錄就寫成/media即可。

子配置檔案需要使用者自定義,檔名稱沒有嚴格要求,字尾建議以.misc結束。格式為:“掛載目錄 掛載檔案型別及許可權 :裝置名稱”。

下邊就以自動掛載光碟機為例,進行演示autofs的用法

3.3 以光碟機為例演示sutofs使用

3.3.1 autofs配置

主配置檔案

[root@sambaserver database]# vim /etc/auto.master
#
# 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).
#
/media /etc/iso.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
[root@sambaserver database]# 

新增/media /etc/iso.misc儲存並退出,其他內容不變。

子配置檔案

[root@sambaserver database]# vim /etc/iso.misc
iso -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

儲存並退出。

說明:把光碟裝置掛載到/media/iso 目錄中,子配置檔案可將掛載目錄寫為 iso,而-fstype 為檔案系統格式引數,iso9660 為光碟裝置格式,ro、nosuid 及 nodev 為光碟裝置具體的許可權引數,/dev/cdrom 則是定義要掛載的裝置名稱。

3.3.2 啟動autofs服務

配置完成後,啟動autofs服務並加入開機啟動

[root@sambaserver database]# systemctl start autofs
[root@sambaserver database]# systemctl enable autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.
[root@sambaserver database]# 
3.3.3 使用autofs服務

經過以上操作我們配置並開啟了autofs服務,我們先來看下是否已經給我們掛載好了光碟裝置

[root@sambaserver database]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  4.3G   14G  25% /
devtmpfs                 905M     0  905M   0% /dev
tmpfs                    914M   76K  914M   1% /dev/shm
tmpfs                    914M   29M  886M   4% /run
tmpfs                    914M     0  914M   0% /sys/fs/cgroup
/dev/sda1                497M  120M  377M  25% /boot
192.168.78.102:/nfs       18G  4.6G   13G  26% /nfs
[root@sambaserver media]# ls /media/
[root@sambaserver media]# 

可以看到游標裝置沒有掛載上,並且/media 目錄中根本就沒有 iso 子目錄

接下來,我們直接進入到掛載的/media/iso目錄,看看是否有內容

[root@sambaserver media]# cd /media/
[root@sambaserver media]# ll
total 0
[root@sambaserver media]# cd iso
[root@sambaserver iso]# ls
CentOS_BuildTag  GPL       LiveOS    RPM-GPG-KEY-CentOS-7
EFI              images    Packages  RPM-GPG-KEY-CentOS-Testing-7
EULA             isolinux  repodata  TRANS.TBL
[root@sambaserver iso]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  4.3G   14G  25% /
devtmpfs                 905M     0  905M   0% /dev
tmpfs                    914M   76K  914M   1% /dev/shm
tmpfs                    914M   29M  886M   4% /run
tmpfs                    914M     0  914M   0% /sys/fs/cgroup
/dev/sda1                497M  120M  377M  25% /boot
192.168.78.102:/nfs       18G  4.6G   13G  26% /nfs
/dev/sr0                 3.9G  3.9G     0 100% /media/iso
[root@sambaserver iso]# 

以上演示說明,當切換到iso目錄時,也就是說只有使用掛載資源時,autofs才自動進行掛載。當系統重啟後可以看到它沒有掛載上去,而再次切換到/media/iso目錄時,又會自動掛載。通過這種方式實現了按需分配,從而節約頻寬等資源。

在講解完檔案共享之後,下一篇文章將分享使用Postfix和Dovecot搭建郵件系統。