LFCS: Automounting Local & Network Drives with Autofs

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
454
Reaction score
519
Credits
19,511
There may be a need for your system to have multiple connections to storage devices. You may not want to have these connections always enabled and so using automount can help resolve this issue.

We can automount to encrypted, Network File System (NFS) or even regular devices.

Installation

CentOS may usually have ‘autofs installed, but Ubuntu does not automatically install ‘autofs’. So, we need to get the program installed before we can use it.

CentOS:
Code:
sudo yum install autofs -y

Ubuntu:
Code:
sudo apt install autofs -y

If you have previously performed an install of the program, running the command will just let you know it exists.

Once installed, the installation should create several files:
  • /etc/autofs.conf - most of the default settings for the automounter
  • /etc/auto.misc - map file
  • /etc/auto.smb - executable script for samba share mounts
  • /etc/auto.master - primary list of mounted storage, mainly to local folders
  • /etc/auto.net - executable script for NFS mounts
These have the configuration of the ‘autofs’ service.

If you look at the ‘autofs.conf’ file, you can see the timeout option is for 300 seconds (5 minutes). If you connect to a share, it disconnects after 5 minutes of inactivity. The system will not make a connection until the user connects to it.

On Ubuntu, you need to open /etc/auto.master’ and uncomment the lines starting with ‘/misc’ and ‘/net’. Also, in ‘/etc/autofs.conf’ you need to add the line ‘mount_nfs_default_protocol = 4’ after the line ‘browse_mode = no’. This is very important!

If you look at ‘auto.misc’, it shows a connection locally to ‘/misc’ and also shows a default mounting to ‘cd’. If you are using VirtualBox, you need to load an ISO file to your CD-ROM for this to work.

Starting ‘autofs’

Now that we have the program installed, we can start it. Use the following two commands to enable it start at boot and to start it now:

Code:
sudo systemctl enable autofs
sudo systemctl start autofs

Once you start the service, we can move to the ‘/misc’ folder and also to ‘cd’:

Code:
cd /misc/cd

Performing ‘ls’ can show the contents of the CD-ROM if you have one loaded. The automount ‘cd’ is automatically in the ‘auto.misc’ file. You can comment the line out, it is:

Code:
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

The last section shows the local device. It is preceded by a colon(:) which means local device.

Keep in mind that any device you automount, you will want to remove from ‘/etc/fstab’ so you do not mount it twice.

Keep in mind that anything mount in ‘fstab’ is a continuous connection, whereas with ‘autofs’ it is only connected to while using and for 5 minutes after, unless you change the default timeout.

Another point to remember is that when you go into ‘/misc’ it may be empty since connections have timed out. If a connection is still active, then it will appear when you run ‘ls’. If the folder is not present, then just change into the configured folder and it should appear.

NOTE: You can sort of look at the folder disappearing after their timeout as a sort of security method. This can help reduce access to these shares.

Encrypted Storage

If we want to automount encrypted storage, we can handle that with automount.

NOTE: Make sure you open the encrypted volume or partition with the full command for ‘cryptsetup luksOpen’. You should not mount it, just open it.

Use your favorite editor and open ‘/etc/auto.misc’.

Find the line for the ‘cd’ and insert a line below it to add:

Code:
data -fstype=xfs :/dev/mapper/data

Change the format type, if needed, as well as the share name, instead of ‘/dev/mapper/data’. On my Ubuntu partition I used ‘ext4’.

Once you have saved and exited the editor, you have made the changes, but they are not active yet. The service needs restarted:

Code:
sudo systemctl restart autofs

Now, you can go to ‘/misc/data’ and have access to the encrypted storage.

Network File System (NFS)

Now, we need to look at NFS, so we need to set up Network File Sharing on Server2.

On CentOS, you need to add the program, if needed:

Code:
sudo yum install nfs-utils -y

On Ubuntu:

Code:
sudo apt install nfs-kernel-server -y

We need to enable the firewall to allow NFS traffic through.

Code:
sudo firewall-cmd --add-service=nfs --permanent
sudo firewall-cmd --reload[/code]

You then need to start and enable the services on CentOS:

Code:
sudo systemctl enable rpcbind nfs-server
sudo systemctl start rpcbind nfs-server

For Ubuntu:

Code:
sudo systemctl enable nfs-kernel-server
sudo systemctl start nfs-kernel-server

Next, you need to create the folder that will be shared. Use the following:

Code:
sudo mkdir /share

Now copy some files into it so you can see the remote system connects to the shared folder.

Use the following commands to reload the shared file system and then display the share to verify it is active:

Code:
exportfs -r
exportfs -s

When using the ‘exportfs -s’ command, it should show the ‘/share’ folder.

Server2 should now be ready to share the ‘/share’ folder over the network.

Mounting a Remote File System

Now that we have created a share on another server, we can automount it when needed.

So, now on Server1 for CentOS, we can edit the file ‘/etc/auto.misc’ and add the following line:

Code:
share -ro server2.centos.linux.org:/share

For Ubuntu:

Code:
share -ro server2.ubuntu.linux.org:/share

You can change your share names and server names as needed. Be sure that the server's name is in the Hosts file and the two systems can ping one another. It must be able to resolve the hostname.

Conclusion

This may have been a little short, but it worked great for CentOS. I had to find the line in /etc/autofs.conf’ for Ubuntu that wasn’t letting it connect.

But we got through it, so make sure you try this. It works very well.
 


Follow Linux.org

Members online


Top