Automatically mounting network shares via fstab and systemd

As handy as mounting network shares via the mount command can be, you may not want to manually mount a share each time you wish to use it. In a network with a central file server, it makes sense to configure workstations to mount network shares automatically so that every time you boot your system, the share will automatically be mounted and ready to go.

The tried and tested approach to mounting resources automatically is the /etc/fstab file. Every Linux system has a /etc/fstab file, so go ahead and look at yours. By default, this file only contains configuration for mounting your local resources, such as partitions on your hard disk. It's standard practice to add additional lines of configuration to this file to mount anything from additional hard drives to network shares.

Note

Be careful while editing your /etc/fstab file. If you accidentally alter the configuration for your local hard disk, your system won't boot the next time you go to start it. Always use caution while editing this file.

Here's an example /etc/fstab file:

# root filesystem
UUID=4f60d247-2a46-4e72-a28a-52e3a044cebf       /                   ext4            errors=remount-ro           0 1
# swap
UUID=c9149e0a-26b0-4171-a86e-a5d0ee4f87a7       none                swap            sw                          0 0

In my file, the Universally Unique Identifier (UUID) reference my local hard disk partitions. These will be different on each system. Next, a mount point is listed for each. The / sign represents the root of the filesystem, and the swap partition doesn't need a mount point so it is set to none.

At the end of the /etc/fstab file, we can add additional mounts that we would like to be available each time we start the system. If we wish to add an NFS share, we could do the following:

10.10.10.101:/share/music/mnt/music  nfs  users,rw,auto,nolock,x-systemd.automount,x-systemd.device-timeout=10 0 0

In the first section, we declare the IP address of the server followed by a colon and the path to the exported directory. In this case, I'm accessing /share/music on 10.10.10.101. The next section is the mount point, so I'm attaching this export to /home/jay/music on my local system. Next, we designate that the share we're accessing is nfs. No surprises there. Finally, we end the configuration with some options for how we would like to mount this share. An easy mount option is rw, which stands for read-write. We could've used ro here if we wanted to prevent the files contained within from being changed.

Among the options in the previous example is x-systemd.automount. Basically, this tells systemd (the default init system on Debian and CentOS since version 8 and 7 respectively) that we would like to keep this mounted if possible. With this option, systemd will try its best to remount this share if for some reason it gets disconnected. Also, x-systemd.device-timeout=10 can be added which tells the system to wait no longer than 10 seconds if the share isn't available on the network. We end the line with 0 0 because this isn't a local filesystem and doesn't need consistency check while booting.

Note

If you're not using a distribution with systemd (such as CentOS 7 and Debian 8), do not include the x-systemd options because they won't be understood by distributions that use different init systems.

Similarly, Samba shares can also be added to your /etc/fstab file. Here's an example:

//10.10.10.9/Videos  /samba  cifs  username=jay  0  0

One final note regarding the /etc/fstab file before we move on. The examples in this section have all assumed that you want a network share to be available automatically. However, this may not always be the case. If you add the noauto mount option to a configuration line in your fstab, the share will not automatically be mounted at boot time. With noauto added to our Samba example, the fstab line would be changed as follows:

//10.10.10.101/Videos  /samba  cifs  noauto,username=jay  0  0 

An NFS example would look like this:

10.10.10.101:/share/music
/mnt/music    nfs    users,rw,noauto,nolock,x-systemd.device-timeout=10 0 0

There are several situations where this might be useful. One example might be using a laptop, where you wouldn't always be connected to the same network. If that is the case, you wouldn't want your machine to try and automatically mount something unless you're actually connected to that network. With noauto added as a mount option, you can manually mount the resource any time you need it, without needing to memorize a long mount command to do so. For example, to mount an NFS export that's contained in your fstab file, you would execute the following:

# mount /mnt/music

By comparison, that's a lot easier than typing the following each time you wish to mount that export:

# mount -t nfs 10.10.10.101:/exports/music/ mnt/music

Since we added the export to the fstab file, the mount command looks for a relevant line when we type a simplified mount command as we have just done. If it finds a configuration for the mount point you're trying to access, it will let you access it without needing to type out the entire command. Even if you don't want to access remote shares automatically, it can still be quite handy to add them to your fstab file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset