快轉到主要內容

我的Linux生活日記 26.分享資料夾-autofs

·638 字·2 分鐘·
PolloChang
作者
PolloChang
我是一隻雞

我的Linux生活日記 26.分享資料夾-autofs
#

昨天已經安裝完 nfs server 接下來就是其他主機安裝端點工具啦!有兩個處理方式:mount 以及 autofs。

mount
#

mount 指令如下:

1
2
madir -p /mnt/nfs/centos7
mount 192.168.56.173:/share /mnt/nfs/centos7

但是不建議這樣做,主要是網路不通無法掛載遠端目錄時會在成PRC服務逾時,間接拖垮系統效能。接下來是建議使用 autofs 掛載目錄。

安裝
#

  • Debian
1
sudo apt install autofs
  • CentOS7
1
sudo yum install autofs

編輯設定文件
#

這邊我直接新增文件,如果沒有 /etc/auto.master.d 這個目錄可以進入 /etc/auto.master 這份文件直接編輯

  • /etc/auto.master.d/jc-share.autofs
1
2
# [掛載目錄] [遠端配置文件]       [參數]
/mnt/nfs    /etc/jc-share.nfs --timeout=10

上述文件中 /mnt/nfs 不需要特別新增,autofs會在掛載時自動處理。

  • /etc/jc-share.nfs
1
2
# [資料夾名稱] [掛載參數]             [遠端主機分享目錄]
centos7       -fstype=nfs,vers=4,rw 192.168.56.173:/share

測試
#

1
sudo automount -f -v
  • 失敗畫面
1
2
3
4
5
6
7
8
Starting automounter version 5.1.2, master map /etc/auto.master
using kernel protocol version 5.03
lookup(file): failed to read included master map auto.master
mounted indirect on /mnt/nfs with timeout 10, freq 3 seconds
attempting to mount entry /mnt/nfs/centos7  # >> 當有呼叫 /mnt/nfs/centos7 才會去掛載
mount(nfs): no hosts available
failed to mount /mnt/nfs/centos7 # 掛載失敗訊息
re-reading map for /mnt/nfs
  • 成功畫面
1
2
3
4
5
6
7
8
Starting automounter version 5.1.2, master map /etc/auto.master
using kernel protocol version 5.03
lookup(file): failed to read included master map auto.master
mounted indirect on /mnt/nfs with timeout 10, freq 3 seconds
attempting to mount entry /mnt/nfs/centos7  # >> 當有呼叫 /mnt/nfs/centos7 才會去掛載
re-reading map for /mnt/nfs
mounted /mnt/nfs/centos7
1 remaining in /mnt/nfs  # 掛載成功訊息

啟動 autofs 服務
#

測試麼有問題後就可以開啟服務啦!

  • Debian 10, CentOS 7
1
2
3
sudo systemctl restart autofs
sudo systemctl start autofs
sudo systemctl enable autofs
  • centOS 6
1
2
3
/etc/init.d/autofs stop && /etc/init.d/autofs start
/etc/init.d/autofs status
chkconfig aufofs on

掛載 Cifs
#

  • /etc/auto.master.d/bakerst.autofs
1
/bakerst  /etc/auto.bakerst
  • /etc/auto.bakerst
1
cases   -fstype=cifs,credentials=/secure/sherlock   ://serverX/cases
  • 新增檔案 /secure/sherlock,內容如下:(only root access, permission 600)
1
2
3
username=[UserName]
password=[Password]
domain=[DomainName]

Accessing Network Storage with SMB

錯誤訊息
#

  • automount: program is already running.

代表 autofs 已經啟動

  • CentOS6.5 掛載 cifs 會失敗

cifs mount works on 6.8, but not on 6.5

參考資料
#

使用 Autofs 按需掛接

鳥哥的 Linux 私房菜 - 第十三章、檔案伺服器之一:NFS 伺服器