How to configure a proper fstab

I have been trying to work out how to do this properly for quite a while so after trawling the internet for solutions and a lot of trial and error I managed to get it working so I thought I would document and share that I had done.

Fstab stands for file system table and it is a Unix or Unix like (Linux) configuration file which is commonly found at /etc/fstab. This file lists all available partitions, folder shares and data sources that are not disk based.  This file is read by the mount command which happens when your computer boots and determines the file system structure i.e. where you can, and more importantly, where applications can access folder shares.

My Use Case

I have a large music library sitting on my NAS and I wanted to be able to access this library using the Strawberry Audio Player. In my systems current setup, I am unable to connect to network shares using nautilus, therefore I have to create an fstab to allow the Strawberry media player to access these files

Pre-requisites

It is assumed that you have a NAS with folder shares already setup and accessible via samba etc.

Before we do anything we will need to install cifs-utils and smbfs (These need to be installed regardless of having Samba and related packages installed). This can be done with:

sudo apt-get install cifs-utils && apt-get install smbfs

It may be that you already have these packages installed already and if that is the case go to the next step.

Create the mount point

Now is a good time to create a mount point on your computer, to do this navigate to:

cd /mnt

for the purposes of this guide, I will call this folder audio. To create this folder use the following command:

mkdir audio

Once you have created the audio folder we will now create a credentials file so that we can limit access to the folder to a user(s).

Create a credentials file

Before you go ahead and create this file, you will need to identify the individual user id (uid) and the group (id).  To do this enter the following:

sudo cat /etc/passwd

You should see something like this. The line will always start with your username in this instance smithj.

One point to note is that as a general rule, Linux systems will automatically assign UIDs and GIDs to new user accounts in numerical order starting at 1000. In this instance John Smith is the only person with an account set-up on this machine therefore, the UID = 1000 and GID = 1000 as illustrated below.

smithj:x:1000:1000:John Smith:/home/smithj:/bin/bash

Now that we have identified your UID and GID you can now go ahead and create your credentials file.  To do this we need to create a .credentials file in your home directory. You can use any text editor for this. The .credentials file will need the following information:

username=smithj
password=password
domain=domain_or_workgroupname

Remember to change the username, password and domain to match your network share credentials not the pc credentials. Remember, you are trying to access a network share that should if set-up correctly, require a username, password and domain. It should look something like this

Save the text file then rename it .credentials as illustrated below

and click in the Rename button. The file will disapear, this is normal as any file that starts with a full stop is flagged as a hidden file. If you click on the 3 vertical dots on the top right of the file explorer you can toggle show hidden files.

Now secure the file with the following:

chmod 0600 ~/.credentials

You can now go ahead and create the fstab entry.

Create the fstab entry

To create the fstab entry we need to edit the fstab file. To do this enter:

sudo nano /etc/fstab

You will now see the fstab file as illustrated below.

Now move the cursor down to below the last line and enter the following:

# /network shares

You don’t have to do this but it will help you identify your network shares better. now enter the following:

//192.168.68.200/audio_share /mnt/audio cifs credentials=/home/smithj/.credentials,uid=1000,gid=1000 0 0

Your fstab file should now look like this:

Now save the file with CTRL X and Y

Now would be a good time to explain what that whole line is.

  • //192.168.68.200/audio_share – This is the IP address of the network share and the share name. In other words the source, remember to change the IP address and the share name.
  • /mnt/audio – This is mount point directory with the folder that you created.
  • cifs – Common Internet File System. This is the protocol that is used for providing shared access to files and printers between computers on the network.
  • credentials=/home/smithj/.credentials,uid=1000,gid=1000 – This is the location of the .credentials file which you created in the home directory. The UID and GID along with the information given in the .credentials file will be used for authentication to access the network share. As mentioned earlier this is the only user account to both the UID and GID=1000.
  • 0 – Dump, this is a number indicating whether and how often the file system should be backed up by the dump program. 0 = the file system will not be backed up.
  • 0 – Pass this the number indicating the order in which the file system consistency (fsck) will check the devices for errors at boot:
    • 0 = Do not Check
    • 1 = Check immediately during boot
    • 2 = Check after boot

Test the mount point

You should now be in a position to test the mount point and this can be done by entering:

sudo mount -a

If you don’t get any error messages then you are good to go. Reboot your computer and navigate to /mnt/audio you should now be able to see your audio files.

If for any reason you did not see any files, then the share failed to mount. Verify that the folder did not mount with:

sudo mount -a

Go back and check the folder now. If the folder mounted then it is likely that the fstab file is being read before the network stack is running during the boot process. One way to fix this is to create an fstab file in the if-up.d folder. To do this enter the following:

cd /etc/network/if-up.d

Now create the fstab file with:

sudo nano fstab

and enter the following:

#! /bin/sh
 mount -a

Save the file with CTRL X and Y. You will also need to make the file executable with:

sudo chmod +x /etc/network/if-up.d/fstab

Reboot your PC again and once logged-in you should now see the contents of the network share. Placing the script into the if-up.d folder will ensure that the script will only run after the network settings are up and operational. This should fix the timing issue by preventing the fstab  from being read too early in the boot process.