I have a desktop and a laptop on the network. Each one's fstab mounts NFS shares on the other, so I can access files on either. If the desktop boots up first and I turn on the laptop after, the laptop mounts the shares from the desktop. But the desktop obviously doesn't mount the shares from the laptop, since that machine was not on when the desktop booted. Or vice-versa, depending on which is started first. So everyday, I have to sudo mount -a on the one that started first, the one without the mounted shares. Is there some way for one to see the other come on the network and mount the shares? Or do I have to keep mounting one of the two manually?

  • Use a crontab entry that attempts to mount every 'x' minutes? – Charles Green Jan 29 '19 at 18:26

Deep inside a question about running a script depending on network status, is this little gem

1. Introduction

autofs is a program for automatically mounting directories on an as-needed basis. Auto-mounts are mounted only as they are accessed, and are unmounted after a period of inactivity. Because of this, automounting NFS/Samba shares conserves bandwidth and offers better overall performance compared to static mounts via fstab.

sudo apt-get install autofs

4.1. Edit /etc/auto.master

The following step creates a mount point at /nfs and configures it according to the settings specified in /etc/auto.nfs (which we will create in the next step).

Type the following into a terminal:

sudo nano /etc/auto.master

Add the following line at the end of /etc/auto.master:

/nfs /etc/auto.nfs

4.2. Create /etc/auto.nfs

Now we will create the file which contains our automounter map:

$ sudo nano /etc/auto.nfs

This file should contain a separate line for each NFS share. The format for a line is {mount point} [{mount options}] {location}. If you have previously configured static mounts in /etc/fstab, it may be helpful to refer to those. Remember, the mount points specified here will be relative to the mount point given in /etc/auto.master.

The following line is for shares using older versions of NFS (prior to version 4):

server   server:/

This creates a new mount point at /nfs/server/ and mounts the NFS root directory exported by the machine whose host-name is server.

4.2.1. NFSv4

If your NFS shares use NFSv4, you need to tell autofs about that. In such a case, the above line would appear as follows:

server   -fstype=nfs4   server:/

The client needs the same changes to /etc/default/nfs-common to connect to an NFSv4 server.

In /etc/default/nfs-common we set:

NEED_GSSD=no # no is default

4.3. Unmount static mounts and edit /etc/fstab

If you have previously configured the NFS shares as static mounts, now is the time to unmount them.

$ sudo umount /server

Next, remove (or comment out) their respective entries in /etc/fstab.

#server:/ /server/ nfs defaults 0 0

4.4. Reload /etc/init.d/autofs

After entering your changes, run the following command to reload autofs:

$ sudo reload autofs

If working on an older ubuntu version, and that does not work try:

$ sudo /etc/init.d/autofs reload

If working in Natty, and that does not work try:

$ sudo /etc/init.d/autofs restart

4.5. Make sure it works

In order to access the share and verify that it is working properly, enter the following into a shell:

$ ls /nfs/server

If you see your NFS share listed, congratulations! You have a functioning NFS mount via autofs! If you want to learn some more advanced information, keep reading.

  • Thanks @CharlesGreen! I have it working on the laptop so far. Can NFS shares, CIFS shares, and data hard drives all be mounted in the same directory, like /home/username/Shares? After autofs was set up, I lost my data hard drive mount mounted there (by fstab) and couldn't get it back until I set up a different mount directory. Do all these different kinds of mounts need to be mounted in their own respective directories, like auto.nfs mounts all being in /home/username/nfs exclusively, auto.smb mounts being in /home/username/cifs, etc.? – ticotexas Jan 30 '19 at 0:43
  • @ticotexas I have not yet had a chance to do this... I would assume there is not a real limit on the number of shares that can be mounted, as long as they have unique mount points "/home/username/Shares/Share1... /Shares/Share2... – Charles Green Jan 30 '19 at 2:43
  • I have it now working on both laptop and desktop (both 18.10) using different directories for each type of share. This page was also helpful with the Windows (another machine) shares: link – ticotexas Jan 31 '19 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.