How to automatically mount directory shared over local network using NFS

Use kernel automonter to automatically mount directory shared over network using NFS.

List the NFS server's export list.

$ sudo showmount -e 192.168.1.131
Export list for 192.168.1.131:
/srv/exports/www 192.168.1.130,192.168.1.108

Create mount directory.

$ sudo mkdir /var/www

Install kernel-based automonter for Linux.

$ sudo apt-get install autofs

Inspect default configuration.

$ cat /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).
#
#/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

Create /etc/auto.master.d directory.

$ sudo mkdir /etc/auto.master.d

Define location of the direct autofs map.

$ cat << EOF | sudo tee /etc/auto.master.d/direct.autofs
/- /etc/auto.direct
EOF

Define direct autofs map.

$ cat << EOF | sudo tee /etc/auto.direct
/var/www -rw 192.168.1.131:/srv/exports/www
EOF

Restart autofs service.

$ sudo systemctl restart autofs

Verify that it works.

$ ls -l /var/www/
total 4
-rw-r--r-- 1 www-data www-data 17 Jan 23 14:49 index.html

Additional information

Execute automount program by hand to use debug mode.

$ sudo systemctl stop autofs
$ sudo automount --debug --foreground
Starting automounter version 5.1.2, master map /etc/auto.master
using kernel protocol version 5.02
lookup_nss_read_master: reading master file /etc/auto.master
do_init: parse(sun): init gathered global options: (null)
lookup_read_master: lookup(file): read entry +dir:/etc/auto.master.d
lookup_nss_read_master: reading master dir /etc/auto.master.d
lookup_read_master: lookup(dir): scandir: /etc/auto.master.d
include_file: lookup(dir): include: /etc/auto.master.d/direct.autofs
lookup_nss_read_master: reading master file /etc/auto.master.d/direct.autofs
do_init: parse(sun): init gathered global options: (null)
lookup_read_master: lookup(file): read entry /-
lookup_read_master: lookup(file): read entry +auto.master
lookup_nss_read_master: reading master files auto.master
do_init: parse(sun): init gathered global options: (null)
lookup(file): failed to read included master map auto.master
master_do_mount: mounting /-
automount_path_to_fifo: fifo name /var/run/autofs.fifo--
lookup_nss_read_map: reading map file /etc/auto.direct
do_init: parse(sun): init gathered global options: (null)
mounted direct on /var/www with timeout 300, freq 75 seconds
do_mount_autofs_direct: mounted trigger /var/www
st_ready: st_ready(): state = 0 path /-
handle_packet: type = 5
handle_packet_missing_direct: token 3, name /var/www, request pid 2271
attempting to mount entry /var/www
lookup_mount: lookup(file): looking up /var/www
lookup_mount: lookup(file): /var/www -> -rw 192.168.1.131:/srv/exports/www
parse_mount: parse(sun): expanded entry: -rw 192.168.1.131:/srv/exports/www
parse_mount: parse(sun): gathered options: rw
parse_mount: parse(sun): dequote("192.168.1.131:/srv/exports/www") -> 192.168.1.131:/srv/exports/www
parse_mount: parse(sun): core of entry: options=rw, loc=192.168.1.131:/srv/exports/www
sun_mount: parse(sun): mounting root /var/www, mountpoint /var/www, what 192.168.1.131:/srv/exports/www, fstype nfs, options rw
mount_mount: mount(nfs): root=/var/www name=/var/www what=192.168.1.131:/srv/exports/www, fstype=nfs, options=rw
mount_mount: mount(nfs): nfs options="rw", nobind=0, nosymlink=0, ro=0
get_nfs_info: called with host 192.168.1.131(192.168.1.131) proto 6 version 0x30
get_nfs_info: nfs v3 rpc ping time: 0.000000
get_nfs_info: host 192.168.1.131 cost 0 weight 0
get_nfs_info: called with host 192.168.1.131(192.168.1.131) proto 17 version 0x30
get_nfs_info: nfs v3 rpc ping time: 0.000000
get_nfs_info: host 192.168.1.131 cost 0 weight 0
prune_host_list: selected subset of hosts that support NFS3 over TCP
mount_mount: mount(nfs): calling mkdir_path /var/www
mount_mount: mount(nfs): calling mount -t nfs -s -o rw 192.168.1.131:/srv/exports/www /var/www
mount_mount: mount(nfs): mounted 192.168.1.131:/srv/exports/www on /var/www
dev_ioctl_send_ready: token = 3
mounted /var/www
[...]

Read automount, auto.master and autofs manual pages for other usage scenarios.

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.