How to Automatically Mount Network Shares on Linux
A lot of tasks on Linux are complicated, especially when you have to do them manually. Mounting network shares is no different as you need to configure logins, input IP addresses, and more.
Everything goes much smoother when you automatically mount network shares. You can do that inside the fstab file. Read on and you will find out the best ways to mount Samba/CIFS and NFS shares automatically.
Make a Backup of the Fstab File
Before you begin, it is recommended to back up the fstab file because it is potentially very dangerous. You can mess up your entire system if you do something wrong while changing this file. Follow these steps to make backup the fstab file:
- Create a folder for the fstab backup. Also, maybe save the backup to a flash drive or an online cloud too, just to be sure. Use the following command:
- Use sudo –s to get a root shell, and go to the /etc/ folder.
- Backup your file and store it in the folder you created. Use the following command.
Cp fstab /home/username/system-backups
- Make sure to rename the backup file and give it a .bak extension.
mv fstab fstab.bak
Restore the Fstab Backup File
Here are the steps for restoring the backup:
- First, you need to use sudo –s to get a root shell.
- Next, you have to go to the /etc/ folder and delete the fstab file which is broken.
cd /etc/rm fstab
- Now you need to copy your backup file back to the /etc/ directory.
cp /home/username/system-backups/fstab /etc/
- Finally, change the name of the backup file.
mv fstab.bak fstab
Automatically Mount NFS Share
Most novice users will find manual NFS mounting very difficult. Luckily, you can get automatic access to shares if you add a line to the fstab file. Follow these steps to do it:
- Locate the share you would like to mount.
showmount –e 192.168.1.150
- Create a directory where the share would mount.
- Access the fstab file using nano.
sudo –snano /etc/fstab
- Type in the command for the mount. Here’s how it needs to look like:
servername: /data /home/username/Network-Files nfs rsize=8192, timeo=14, _netdev 0 0
Make sure to replace the “data” section with the title of your NFS share. To make sure the alterations to /etc/fstab are saved, press together CTRL and O keys on your keyboard. Finally, reboot the computer. When you log in, the NFS share should mount automatically.
Automatically Mount Samba Share
SAMBA is also very hard and tedious to use. However, it is very useful because it can serve shared files to the Windows, Mac, and Linux, as well as iOS and Android operating systems.
First things first, you need to install the CIFS utilities. CIFS is a toolkit that makes sure the automatic mounting of the Samba shares goes smoothly.
Here are the commands you can use to install CIFS on different Linux managers:
sudo apt install cifs-utils
sudo apt-get install cifs-utils
sudo dnf install cifs-utils
sudo pacman –S cifs-utils
sudo zypper install cifs-utils
Next, find and install CIFS utils. Now you can make a mount folder for SMB shares.
sudo mkdir /mnt/samba
Once you have downloaded the tools, you can finally set up your SMB mount with these commands:
Type the mount line.
//SERVER/share /mnt/samba cifs username=user,password=password 0 0
You should replace the “share” part with the actual name of the network share, and the “SERVER” part with your server’s name or its IP address. Also, instead of ”user” write your SAMBA username and instead of ”password” your actual SAMBA password.
When you type out the mount line, press together CTRL and O keys simultaneously to save all the changes. Finally, reboot the system and you will see SAMBA share mounted automatically when the system boots up.
Automation Execution Completion
That is how you automatically mount network shares on Linux using SAMBA and NFS. If you follow the steps correctly and make this process automatic, it should save you a lot of time and effort.
Was this tutorial difficult to follow or you managed to do everything easily? Post in the comments if there is something you would like to add.