KnowHow

技術的なメモを中心にまとめます。
検索にて調べることができます。

autofsを使ってNFSをマウントする方法

登録日 :2024/03/02 10:56
カテゴリ :Linux

前回、NFSを使ってのファイル共有とマウント方法(起動時の自動マウントまで)をまとめました。
NFSサーバとNFSクライアントの設定まで終わっていることを前提に、autofsの設定方法をまとめます。

1 NFSサーバ側の設定

NFSサーバ側では、autofsで共有したいディレクトリを/etc/exportsに設定する。

[root@manage /]# vi /etc/exports
/manage_share 192.168.56.0/255.255.255.0(rw,sync,no_root_squash)

nfs-serverを再起動する

[root@manage /]# systemctl restart nfs-server.service

2 NFSクライアント側の設定

2-1 まずatufsサービスをインストールする。

sudo dnf install autofs

2-2 /etc/auto.master ファイルにautofs設定ファイルのマッピングを記述
最終行に以下のように追記する。
/mnt以下に、autofsでマウントされたマウントポイントが来る。

[root@newhead /]# vi /etc/auto.master
/mnt    /etc/auto.nfs

2-3 /etc/auto.nfsファイルに、マウント情報を記述
/etc/auto.nfsは新規作成にて、マウント情報を以下のように記述する。

これは、/mnt/shareにマウントポイントを設定する例(/mntは、/etc/auto.masterに記述済み)。
NFSサーバの/manage_shareを/mnt/shareとしてマウントする。

[root@newhead /]# vi /etc/auto.nfs
share   -rw,nfsvers=3 192.168.1.1:/manage_share

2-4 autofsのサービスの起動とenable設定

[root@newhead ~]# systemctl enable --now autofs

2-5 autofsのサービスを再起動

[root@newhead /]# systemctl status autofs
 autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor pres>
   Active: active (running) since Wed 2024-02-28 03:47:59 JST; 7s ago
 Main PID: 83379 (automount)
    Tasks: 7 (limit: 4552)
   Memory: 4.1M
   CGroup: /system.slice/autofs.service
           mq83379 /usr/sbin/automount --systemd-service --dont-check-daemon

 2 28 03:47:59 newhead systemd[1]: Starting Automounts filesystems on demand.>
 2 28 03:47:59 newhead systemd[1]: Started Automounts filesystems on demand.
[root@newhead /]#

2-6 /mnt/shareにアクセスできるかを確認する

[root@newhead /]# df -h
ファイルシス        サイズ  使用  残り 使用% マウント位置
devtmpfs              356M     0  356M    0% /dev
tmpfs                 386M     0  386M    0% /dev/shm
tmpfs                 386M   40M  346M   11% /run
tmpfs                 386M     0  386M    0% /sys/fs/cgroup
/dev/mapper/rl-root    14G  7.0G  6.5G   52% /
/dev/sda1            1014M  265M  750M   27% /boot
tmpfs                  78M   24K   78M    1% /run/user/0
[root@newhead /]# cd /mnt/share
[root@newhead share]# ls
test  test2
[root@newhead share]#

無事アクセスできた。一度アクセスすると、dfコマンドでマウントポイントが表示される。

[root@newhead share]# df -h
ファイルシス                 サイズ  使用  残り 使用% マウント位置
devtmpfs                       356M     0  356M    0% /dev
tmpfs                          386M     0  386M    0% /dev/shm
tmpfs                          386M   40M  346M   11% /run
tmpfs                          386M     0  386M    0% /sys/fs/cgroup
/dev/mapper/rl-root             14G  7.0G  6.5G   52% /
/dev/sda1                     1014M  265M  750M   27% /boot
tmpfs                           78M   24K   78M    1% /run/user/0
192.168.1.1:/manage_share    14G  8.6G  4.8G   65% /mnt/share
[root@newhead share]#

このマウントポイントは、一定時間アクセスがないと消えるが、再度マウントポイントにアクセスすると、dfでも表示されるようになる。

マウントポイントのTimeout時間は以下の通り。
デフォルトでは、300秒(5分)となっているようである。

[root@newhead share]# cat /etc/autofs.conf | grep dismount
# dismount_interval - is equivalent to the autofs timeout option. It
dismount_interval = 300
# browsable_dirs, dismount_interval and selectors_in_defaults
#dismount_interval = 60
[root@newhead share]#