I have a couple of shared directories on my NAS server that I want to automatically mount.

Currently I have tried putting them in my .bashrc file but this means every time I open a terminal it will try mounting them even if the are already mounted.

Is there a better way of automatically mounting these? If not, is there a way I count conditionally execute the sudo mount statements only if they are not already mounted?

  • 1
    do you want them to mount at boot? – John Orion May 7 '16 at 8:45

Here is a way you can have a share mount at boot.

First open a terminal, it should open at your home directory. Type the following:

touch .smbcredentials 

then open ~/.smbcredentials in your favorite editor and add the following lines

username=your user name
password=you user password

Save the file and open terminal again and type:

sudo chmod 600 .smbcredentials

now open your /etc/fstab file... you can open it in terminal sudo vi or with your favorite terminal program or use sudo gedit or whatever your favorite GUI editor is (the file has to be edited in root or you wont be able to save it)

At the end of the /etc/fstab file you will need to add a line for each share with the format of shared directory path/name the location of the folder it will be mapped to on your system cifs a line to your credentials file with some other stuff .. I will give you an example but the paths will all be wrong .. you will need to change them to fit the shares you are using.

For instance .. if I have a share called Downloads on a network computer named MAIN (you can also use the IP address such as // I wanted it to be mapped to a folder located at /media/user/myshare and my local user name is Joe the line would look like this:

//MAIN/Downloads /media/user/myshare cifs credentials=/home/Joe/.smbcredentials,users,iocharset=utf8,sec=ntlm 0 0 


// /media/user/myshare cifs credentials=/home/Joe/.smbcredentials,users,iocharset=utf8,sec=ntlm 0 0  

with the information you supplies the correct line will be:

 // /media/NAS-movies cifs credentials=/home/mark/.smbcredentials,users,iocharset=utf8,sec=ntlm 0 0

it does look identical as to what you gave me but give it a try .. its late for me i may have missed something :D

A few things to be aware of ... Linux is case sensitive so if the share is called Downloads you need to use Downloads .. downloads is not the same folder. The folder you are mapping the share on the local machine must exist or fstab will fail.

Once you have set up this share and saved the file... unmount the share and and type in terminal:

sudo mount -a

and see if you have any errors ... if there are no errors check to see if the share is mounted .. if it is .. then you did everything right and every time the computer is booted that share will be mounted ... just follow the same steps to create any other shares on your NAS.. .. of course .. you only have to do the smbcredential file once so adding any other shares will just be changing the first two parts of the line we added.

After a chat with the OP we found out that he was missing some important files like samba and cifs once we got the all the files the system needed we were able to resolve the problem

  • Thank you! That's even better - although I'm tripping up a the last step: It's a NFS shared directory so using your examples, the line I'm entering in etc/fstab is /media/user/myshare nfs credentials=/home/ben/.smbcredentials,iocharset=utf8,sec=ntlm 0 0. However, when I try the sudo mount -a It tells me that "mount.nfs: an incorrect mount option was specified". Do you know what I am doing wrong? – Bendy May 7 '16 at 9:44
  • no what i was saying is right .. its the protocol that smb uses .. it needs to be cifs – John Orion May 7 '16 at 9:47
  • I also see a few other errors if they weren't typos .. it needs to be // if that is the address of the computer you are connecting too .. and the share location usually is /media/ben/myshare or whatever you named it. I was using "user" to specify the logged in user – John Orion May 7 '16 at 9:54
  • also when messing with the fstab file .. we need to make sure it works properly before you reboot .. you don't want to reboot with errors in fstab or you my have problems booting back in. – John Orion May 7 '16 at 9:57
  • I have changed back to cifs and //localIPAddress/ as you corrected me (thanks!) but when I submit sudo mount -a I get returned "mount: wrong fs type, bad option, bad superblock on //, missing codepage or helper program, or other error (for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program)". (Could it be a problem from following the mounting guide here: help.ubuntu.com/lts/serverguide/network-file-system.html with sudo apt install nfs-kernel-server?) – Bendy May 7 '16 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.