Overview of automounting NFS and CIFS shares
Mounting CIFS and NFS shares using the mount command, won’t survive a reboot. Luckily there are three different ways to auto-mount CIFS and NFS shares when the machine boots up:
Announcement
You can find all my latest posts on medium.- Adding entries to /etc/fstab
- via autofs
- via systemd
By the end of this article you should be able to answer the following questions:
There are 3 ways, the are:
– Adding entries to /etc/fstab
– via autofs
– via systemd
The-Server:/nfs/Shared-Folder /nfs/The-Server/Shared-Folder nfs defaults 0 0
//The-Server/Shared-Folder /cifs/The-Server/Shared-Folder cifs username=vagrant,password=admin 0 0
# remember to use the “//” syntax again.
$ mount -a
$ mount
# or
$ df -h
$ yum install autofs
# start the service
$ systemctl start autofs
/etc/auto.master
/nfs/The-Server /etc/autofs/nfs/The-Server.conf
A-Shared-Folder -fstype=nfs,rw The-Server:/nfs/Shared-Folder
# restart the service
$ systemctl restart autofs
# cd into the mount point
$ cd /nfs/The-Server/A-Shared-Folder
$ mount
# or
$ df -h
/cifs/The-Server /etc/autofs/cifs/The-Server.conf
# note don’t forget to include the “//”, also notice that this time you also need to prefix the colon as well “:”. Therefore you now end up with “://”. It looks wrong but that’s how it is.
# restart the service
$ systemctl restart autofs
# cd into the mount point
$ cd /cifs/The-Server/Shared-Folder
$ mount
# or
$ df -h
Automounting using fstab
Quick recap, here’s how we manually mounted the shared folders from our previous article’s examples for nfs and cifs respectively:
$ mount -t nfs The-Server:/nfs/Shared-Folder /nfs/The-Server/Shared-Folder
$ mount -t cifs -o user=vagrant,password=admin //The-Server/Shared-Folder /cifs/The-Server/Shared-Folder
Automount NFS via the fstab file
Following on from the earlier example.
To automatically mount an export during boot time, you need to add the following entry into the /etc/fstab
config file.
The-Server:/nfs/Shared-Folder /nfs/The-Server/Shared-Folder nfs defaults 0 0
To check that we haven’t made a mistake, we should run the following to confirm we have done everything correctly:
$ mount -a
Automount CIFS using fstab
Now here’s the entry we need to make in the /etc/fstab
file for this example:
//The-Server/Shared-Folder /cifs/The-Server/Shared-Folder cifs username=vagrant,password=admin 0 0
However to ensure you haven’t made a mistake. Check that the CIFS share is currently mounted. And then run:
$ mount -a
Then check if the CIFS share is now mounted. If it has then your fstab cif entry is correct.
Note, in this approach, we have added credentials as plain texts in the fstab file. To make it a bit more secure, you can take this approach instead:
//The-Server/Shared-Folder /cifs/The-Server/Shared-Folder cifs credentials=~/.secret.txt 0 0
Where the credentials files contains:
$ cat /root/.secret.txt username=vagrant password=admin
Automounting using autofs
Autofs is a more intelligent alternative to using fstab. That’s because Autofs has 2 main advantages:
- mounted remote resources are automatically unmounted after a period of inactivity.
- unmounted remote resources are automatically mounted again when you try to access the mount-point.
- You don’t have to create the mount point before hand, only the mountpoint’s parent directories
To be able to do automounting, you need to first install the automounting package which is called autofs:
$ yum install autofs
The main autofs config file is auto.master:
$ 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
Now we’ll take a look at how to configure autofs to automount nfs and cifs shares, starting with nfs.
Automount NFS via autofs
The auto.master file itself doesn’t contain mounting related info. Instead it’s a file that maps each mountpoint’s parent folder along with location of a custom config file that houses actual mounting info. That’s why the entries in this file is in the form of 2 column entries. Therefore for this example, we added the following line to the /etc/auto.master file:
/nfs/The-Server /etc/autofs/nfs/The-Server.conf
Note: The “/nfs/The-Server” folder isn’t a mount point itself, but houses mount points. This folder is therefore sometime srefered to as a “automount” folder, i.e. the mount point’s parent folder.
The second entry could be anything to your liking. However it’s best practice to try to make it meaningful. Now let’s create our custom file:
$ mkdir -p /etc/autofs/nfs $ touch /etc/autofs/nfs/The-Server.conf
Now in this file custom file, we add the following:
$ cat /etc/autofs/nfs/The-Server.conf A-Shared-Folder -fstype=nfs,rw The-Server:/nfs/Shared-Folder
Here we just specify the mountpoint folder, and not the full path. In this example our mountpoint’s name is “A-Shared-Folder”
Now we restart autofs service:
$ systemctl enable autofs ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service' $ systemctl start autofs
Now let’s check our mount point.
$ pwd /nfs/The-Server $ ls -l total 0 $ cd A-Shared-Folder $ ls -l total 0 drwxr-xr-x. 2 root root 26 Oct 24 20:14 subfolder -rw-r--r--. 1 root root 0 Oct 24 20:14 testfile1.txt
Notice how we can magically cd into the mountpoint, even though it didn’t originally exist.
At first it seems a bit unusual, the structure of the auto.master file being a mapping to a mountpoint’s parent and it’s corresponding config file. This is designed for easier scalability. For example, let’s say we have 2 nfs shares available on The-Server, instead of one:
$ showmount -e The-Server Export list for The-Server: /nfs/Shared-Folder2 * # this is a new nfs share. /nfs/Shared-Folder *
Then automount this, we don’t need to edit auto.master. Instead we add another entry to our custom file:
$ cat /etc/autofs/nfs/The-Server.conf A-Shared-Folder -fstype=nfs,rw The-Server:/nfs/Shared-Folder Another-Shared-Folder -fstype=nfs,rw The-Server:/nfs/Shared-Folder2
As you can see, the mountpoint’s parent folder defined in /etc/auto.master actually becomes the home of multiple possible NFS mountpoints. In our case both NFS shares originated from the same server, we could have had different nfs servers, in which case it would instead be better for a different parent mount folder name, e.g. just “/nfs”.
Now let’s test this out:
$ pwd /nfs/The-Server $ ls -l total 0 $ cd A-Shared-Folder $ ls subfolder testfile1.txt $ pwd /nfs/The-Server/A-Shared-Folder $ cd .. $ ls A-Shared-Folder $ cd Another-Shared-Folder $ pwd /nfs/The-Server/Another-Shared-Folder $ ls -l total 4 -rw-r--r--. 1 root root 6 Oct 28 21:05 yet-more-stuff.txt $ cd .. $ pwd /nfs/The-Server $ ls -l total 0 drwxr-xr-x. 2 root root 31 Oct 28 21:05 Another-Shared-Folder drwxr-xr-x. 3 root root 42 Oct 24 20:14 A-Shared-Folder
Automount CIFS via autofs
Quick recap:
$ smbclient -L The-Server --user vagrant Enter vagrant's password: Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.12] Sharename Type Comment --------- ---- ------- Shared-Folder Disk IPC$ IPC IPC Service (Samba Server Version 4.1.12) vagrant Disk Home Directories Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.12] Server Comment --------- ------- Workgroup Master --------- -------
In a similar fashion to nfs, we first add the following line to /etc/auto.master:
/cifs/The-Server /etc/autofs/cifs/The-Server.conf
And in the custom config file we add:
Shared-Folder -fstype=cifs,rw,username=vagrant,password=admin ://The-Server/Shared-Folder
Now, let’s test if it this worked:
$ pwd /cifs/The-Server $ ls -l total 0 $ cd Shared-Folder $ pwd /cifs/The-Server/Shared-Folder $ ls -l total 0 -rwxrwxrwx. 1 root root 0 Oct 24 22:31 hello-world-cifs.txt drwxrwxrwx. 2 root root 0 Oct 24 22:31 subfolder -rw-r--r--. 1 vagrant vagrant 0 Oct 28 21:51 testfile.txt
Success!
Automounting using systemd
This is relatively new approach. With this approach you can’t have any “-” characters in the full path of your mount point. Then it’s just a case of creating the .mount file, and using systemctl to start+enable your new .mount unit.
Automounting NFS using systemd
First we create the mountpoint:
$ mkdir -p /nfs/TheServer/SharedFolder
Now we create the following unit file:
$ cat /etc/systemd/system/nfs-TheServer-SharedFolder.mount [Unit] Description=NfS share from The-Server [Mount] What=The-Server:/nfs/Shared-Folder Where=/nfs/TheServer/SharedFolder Type=nfs Options=rw [Install] WantedBy=multi-user.target
Finally we start+enable this mount unit:
$ systemctl start nfs-TheServer-SharedFolder.mount $ systemctl enable nfs-TheServer-SharedFolder.mount $ systemctl status nfs-TheServer-SharedFolder.mount nfs-TheServer-SharedFolder.mount - Nfs share form The-Server Loaded: loaded (/etc/systemd/system/nfs-TheServer-SharedFolder.mount; enabled) Active: active (mounted) since Wed 2015-10-28 23:14:23 GMT; 2min 13s ago Where: /nfs/TheServer/SharedFolder What: The-Server:/nfs/Shared-Folder Oct 28 23:14:23 puppetmaster.local systemd[1]: Mounting Nfs share form The-Server... Oct 28 23:14:23 puppetmaster.local systemd[1]: Mounted Nfs share form The-Server. Oct 28 23:16:29 puppetmaster.local systemd[1]: Mounted Nfs share form The-Server.
Now, we can go in to check it has worked:
$ cd /nfs/TheServer/SharedFolder $ ls -l total 0 drwxrwxrwx. 2 root root 26 Oct 24 20:14 subfolder -rw-r--r--. 1 nfsnobody nfsnobody 0 Oct 28 23:02 testfile -rwxrwxrwx. 1 root root 0 Oct 24 20:14 testfile1.txt
Also to confirm it has worked, we can do:
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos_puppetmaster-root 38G 4.7G 33G 13% / devtmpfs 487M 0 487M 0% /dev tmpfs 497M 80K 497M 1% /dev/shm tmpfs 497M 6.9M 490M 2% /run tmpfs 497M 16K 497M 1% /sys/fs/cgroup The-Server:/nfs/Shared-Folder 38G 3.9G 34G 11% /nfs/TheServer/SharedFolder $ mount | grep The-Server /etc/autofs/nfs/The-Server.conf on /nfs/The-Server type autofs (rw,relatime,fd=-1,pgrp=15130,timeout=300,minproto=5,maxproto=5,indirect) The-Server:/nfs/Shared-Folder on /nfs/TheServer/SharedFolder type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.50.10,local_lock=none,addr=192.168.50.11)
Automounting CIFS using systemd
First we create the mount point:
$ mkdir -p /cifs/TheServer/SharedFolder
Now we create the mount.unit:
$ cat /etc/systemd/system/cifs-TheServer-SharedFolder.mount [Unit] Description=CIFS share from The-Server [Mount] What=//The-Server/Shared-Folder Where=/cifs/TheServer/SharedFolder Type=cifs Options=rw,username=vagrant,password=admin [Install] WantedBy=multi-user.target
Once again you name this unit after the full mountpath with the “/” replaced by “-“.
Next we start+enable it:
$ systemctl start cifs-TheServer-SharedFolder.mount $ systemctl enable cifs-TheServer-SharedFolder.mount ln -s '/etc/systemd/system/cifs-TheServer-SharedFolder.mount' '/etc/systemd/system/multi-user.target.wants/cifs-TheServer-SharedFolder.mount' $ systemctl status cifs-TheServer-SharedFolder.mount cifs-TheServer-SharedFolder.mount - CIFS share from The-Server Loaded: loaded (/etc/systemd/system/cifs-TheServer-SharedFolder.mount; enabled) Active: active (mounted) since Wed 2015-10-28 23:31:07 GMT; 5min ago Where: /cifs/TheServer/SharedFolder What: //The-Server/Shared-Folder Oct 28 23:31:07 puppetmaster.local systemd[1]: Mounting CIFS share from The-Server... Oct 28 23:31:07 puppetmaster.local systemd[1]: Mounted CIFS share from The-Server. Oct 28 23:36:36 puppetmaster.local systemd[1]: Mounted CIFS share from The-Server.
Now we confirm that it is working:
$ cd /cifs/TheServer/SharedFolder $ ls -l total 0 -rwxrwxrwx. 1 root root 0 Oct 24 22:31 hello-world-cifs.txt drwxrwxrwx. 2 root root 0 Oct 24 22:31 subfolder -rw-r--r--. 1 vagrant vagrant 0 Oct 28 21:51 testfile.txt $ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos_puppetmaster-root 38G 4.7G 33G 13% / devtmpfs 487M 0 487M 0% /dev tmpfs 497M 80K 497M 1% /dev/shm tmpfs 497M 6.9M 490M 2% /run tmpfs 497M 16K 497M 1% /sys/fs/cgroup The-Server:/nfs/Shared-Folder 38G 3.9G 34G 11% /nfs/TheServer/SharedFolder //The-Server/Shared-Folder 38G 3.9G 34G 11% /cifs/TheServer/SharedFolder $ mount | grep cifs //The-Server/Shared-Folder on /cifs/TheServer/SharedFolder type cifs (rw,relatime,vers=1.0,cache=strict,username=vagrant,domain=PUPPETAGENT01,uid=0,noforceuid,gid=0,noforcegid,addr=192.168.50.11,unix,posixpaths,serverino,acl,rsize=1048576,wsize=65536,actimeo=1)