How to permanently mount a SAMBA drive under Linux

A while back, I bought a QNAP NAS drive for backing up my server and other machines which are attached to the network.  Having created a number of shared folders on the NAS, I had shortcuts to them available to me via SAMBA, but I wanted to configure my desktop machine so that the folders were permanently mounted.

So, the first step was to make sure that SMBFS was installed and up to date:

sudo apt-get install smbfs

Once the installation was complete, rather than store the connection details in plain sight, I wrote a text file which would contain the details

sudo gedit .nascredentials

The text file includes the user name and password to connect to the NAS (which are different from my Linux username and password):

username=mynasuser
password=mynaspassword

To make sure that the file was then restricted, I changed the access permissions for the credentials file so that only root user could read it

sudo chmod 600 .nascredentials

The next step was to create a mount point folder, which was accessible to any user on the system

sudo mkdir /media/nas

sudo chmod 777 /media/nas

After this, I edited the fstab file to include the mounting commands (note that the text should all be on one line, but I’ve had to include line breaks here for formatting reasons!)

sudo gedit /etc/fstab

//<NAS IP Address>/backups /media/nas cifs
  credentials=/home/myuser/.nascredentials,dir_mode=0777,
  file_mode=0777,umask=000,uid=myuser 0 0

This does rely on the NAS drive having a fixed IP address, and “backups” is the name of the samba share name.

Finally, rather than reboot, I only had to execute the following command to mount the new share

sudo mount -a

If the NAS is not switched on when the computer starts, then the drive won’t be mounted – if that happens, then I just need to run the last command to mount the folder again.

By continuing to use the site, you agree to the use of cookies. more information

In common with almost all professionally run websites, this website logs the IP address of each visitor in order to keep it running reliably. This is also essential for protecting the website and its visitors from malicious attacks, including infection with malware.

This website provides information as a service to visitors such as yourself, and to do this reliably and efficiently, it sometimes places small amounts of information on your computer or device (e.g. mobile phone). This includes small files known as cookies. The cookies stored by this website cannot be used to identify you personally.

We use cookies to understand what pages and information visitors find useful, and to detect problems such as broken links, or pages which are taking a long time to load.

We sometimes use cookies to remember a choice you make on one page, when you have moved to another page if that information can be used to make the website work better. For example:
- avoiding the need to ask for the same information several times during a session (e.g. when filling in forms), or
- remembering that you have logged in, so that you don’t have to re-enter your username and password on every page.

You can prevent the setting of cookies by adjusting the settings on your browser (see your browser Help for how to do this). Be aware that disabling cookies will affect the functionality of this and many other websites that you visit.

Close