0

How to Automatically Mount Network Shares on Linux

Posted by Arch on August 20, 2019
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:

  1. 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:
    mkdir ~/system-backups
  2. Use sudo –s to get a root shell, and go to the /etc/ folder.
    cd /etc/
  3. Backup your file and store it in the folder you created. Use the following command.
    Cp fstab /home/username/system-backups
  4. Make sure to rename the backup file and give it a .bak extension.
    mv fstab fstab.bak

Restore the Fstab Backup File

how to automatically mount network shares

Here are the steps for restoring the backup:

  1. First, you need to use sudo –s to get a root shell.
  2. Next, you have to go to the /etc/ folder and delete the fstab file which is broken.
    cd /etc/rm fstab
  3. Now you need to copy your backup file back to the /etc/ directory.
    cp /home/username/system-backups/fstab /etc/
  4. 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:

  1. Locate the share you would like to mount.
    showmount –e 192.168.1.150
  2. Create a directory where the share would mount.
    mkdir ~/Network-files
  3. Access the fstab file using nano.
    sudo –snano /etc/fstab
  4. 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:

Ubuntu

sudo apt install cifs-utils

Debian

sudo apt-get install cifs-utils

Fedora

sudo dnf install cifs-utils

Arch Linux

sudo pacman –S cifs-utils

OpenSUSE

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:

sudo –s

nano /etc/fstab

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.

linux

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.

Leave a Reply

Your email address will not be published. Required fields are marked *


Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.