Overview
autofs is a great tool to mount your network storage more efficiently on your Linux server. These are the benefits of using autofs:
- Persistence: Mounts persist across restarts
- On-demand mounting: Mounts are only established when accessed, reducing boot time and system overhead
- Reduced network traffic: Unneeded shares remain unmounted, saving bandwidth
- Automatic retry: Automatically retries connection if the NAS is temporarily unavailable
- Support for multiple protocols: Works with NFS (NFSv3, NFSv4), CIFS/SMB, FTP, and more
- Ghost directories: The
--ghostoption creates placeholder directories for easier browsing
Configuration
- Install the required
autofs. The following instruction assumes you have a Debian/Ubuntu OS.
sudo apt update && sudo apt install -y autofs cifs-utils- Create your NAS credential file:
sudo mkdir -p /etc/samba/credentials
sudo nano /etc/samba/credentials/nas.crednas.cred:
username=myuser
password=supersecretpassword
domain=MYDOMAIN # optional, omit if not neededProtect your credentials from non-root users:
sudo chmod 600 /etc/samba/credentials/nas.cred
sudo chown root:root /etc/samba/credentials/nas.cred- Configure the master
autofsmaster mapping:
sudo nano /etc/auto.master/etc/auto.master:
# Assuming you want to mount to `/mnt/smb` locally to your NAS:
/mnt/smb /etc/auto.cifs --timeout=300 --ghost- Create a new map file:
sudo nano /etc/auto.cifs/etc/auto.cifs:
# Assuming your NAS is located at 192.168.2.223 with shares: vm, iso
vm -fstype=cifs,_netdev,credentials=/etc/samba/credentials/nas.cred,vers=3.1.1,serverino ://192.168.2.223/vm
iso -fstype=cifs,_netdev,credentials=/etc/samba/credentials/nas.cred,vers=3.1.1,serverino ://192.168.2.223/iso- Restart
autofsto pick up the updated configuration:
sudo systemctl restart autofs- Check your files:
# should show files from your NAS `vm` path!
cd /mnt/smb/vmTroubleshooting
Check autofs status
sudo systemctl status autofsRun in debug mode
sudo automount -f -v -vForce mount manually
sudo mount -aView mount logs
journalctl -u autofs -fCommon issues
| Issue | Solution |
|---|---|
| Mount fails with “No such file or directory” | Check NAS IP and share name in map file |
| Mount fails with “Permission denied” | Verify credentials file permissions and contents |
| Mount hangs | Add soft,intr options or check network connectivity |
| Files show as wrong owner | Add uid and gid options to map |