Mount a disk permanently with ‘fstab’.

Having a hard drive in your Linux system is no fun unless you mount it! So why would you want to have to manually mount the drives after every reboot? In today’s article I will go over the steps of writing your disk information to the Linux ‘fstab’ file to allow you to automatically mount a drive’s partition every time you reboot your system.

Linux uses a file named ‘fstab’ to keep track of what partitions should be mounted when the system starts-up and the location to mount said partitions. The ‘fstab’ file is located in ‘/etc/’ and can be edited directly. To edit the file you can use any text editor; in this article I will be using the ‘Vim’ editor which can be obtained in Ubuntu by using the following command:

sudo apt-get install vim 

In order to put your drives information into the ‘fstab’ file you will need the following information:

1) The file system that the partition has been formatted with. (e.g. ext4)

2) The UUID of the partition. This can be obtained by issuing the following command:

blkid -p /dev/<drive>

3) The mount point (aka, the place where you would like to mount the disk.) This can be any folder that you choose as long as it has the correct permissions and is empty. It is best that you make a new folder in ‘/mnt/’ using the mkdir command.

Once you have compiled a list of the above info, you can open the file and take a look at the contents. Use the following command to do so:

vim /etc/fstab

Your file should look similar to the following:

fstab

Note: Your file may be opened as “read-only”. Depending on the distribution you are using, you may have to enter the ‘sudo’ command before opening the file in Vim.

As you can see there are three entries in my ‘fstab’ file; proc, / (root) and swap (which has no mount point.). The entries are written in the following format:

<file system> <mount point> <type> <options> <dump> <pass>

The file system is either the drive designation in the format /dev/<drive> (e.g. /dev/sdb1), the “Label” of the drive or the UUID of the drive.

The mount point is the place where you would like the partition to be mounted and therefor accessible to the system and the user.

The type is the file system that the partition is formatted with ( e.g. ext4)

The options, dump and pass tell the system what options the partition should be mounted with, whether or not to copy the drive to a backup (this is hardly used in modern times) and what order to mount the disks. The pass command can have three values with the following designations:

0 = no check (basically the entry is ignored)

1 = root file system (this drive should be mounted before other drives)

2 = other file system (this drive is not a system drive and should be mounted after the system drives)

Using the example drive we installed in my previous posts, we should create and entry for our new drive. To do so from within Vim, press ‘i’ to enter ‘insert’ mode so you can edit the file. Once you are in ‘insert’ mode enter the following information at the bottom of the ‘fstab’ file:

Note: You will be using the UUID of the drive. You should obtain the UUID with the command shown previously in this post.

 UUID=<UUID> /mnt/mountpoint ext4 defaults 0 0

Where <UUID> is the UUID of the partition you would like to mount.

After you have entered the above information exit out of ‘insert’ mode by pressing the ‘Esc’ key and then save and exit the file with by typing ‘:wq’.

In order to test the entry you should reboot your system and then use the ‘df’ command to view the mounted drives. You disk should show up as mounted to the location you specified as the mount point. If the drive is not shown in the output of the ‘df ‘ command then you should double check to make sure that you entered the correct information in ‘fstab’.

This article completes the series of posts that shows you, step-by-step, how to get a drive up and running from scratch on your Linux system. If you have any questions or comments, or you find any mistakes, please let me know!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s