Understanding Linux /etc/fstab - gHacks Tech News

Understanding Linux /etc/fstab

The fstab file has a very key mission for your Linux system. What it does is map devices to directories so those devices can be used. If you plug in an external drive or a device like such as an iPod you are going to have to take advantage of fstab. In this article you will learn how to create a proper fstab entry to make mounting devices simple.

What fstab does

As said earlier, the /etc/fstab file is a means to map devices to locations so the devices can be used. Typically when you plug in an external device that device will show up as a device in the special directory /dev. Most externally connected usb devices will show up as a variation of /dev/sda. But if you try to access that device through the /dev directory you'll have no luck. Instead you have to map the device to a regular, mountable directory so the device can be used. Without the fstab file only the root user would be able to do the mounting and the mount command would always be something like "mount /dev/sda2 /media/mp3". When the root user mounts a device in this way only the root user will have write access to the device. In the case of an mp3 player that means only the root user will be able to add music to the device. That's where fstab helps out. You can create an fstab entry that will allow standard users to mount and unmount devices as well as write to those mounted devices.

Typical fstab entry

The structure of an fstab entry is:

device   mounting_directory      filesystem_type     options    0 0

The device will always be assigned once you have plugged in what it is you are going to mount. The easiest way to find out what has been assigned is to open up a command terminal and enter the command dmesg. Using dmesg will require you to keep issuing the command until the kernel has recognized the device. Or you could enter the command tail -f /var/log/messages (must be run as root). Using the tail command will keep follow the output of the messages log file which will give you all the information you need.

With the understanding of where you get the device location in hand let's move on to the mounting_directory entry. This is quite simple: Create a directory, as a sub directory under /media named after the device you want to mount. For instance, if you are going to mount a USB thumbdrive you can create a directory called /media/thumb or if you need to mount your iPod you can create a directory called /media/ipod. This directory is where you will be mounting your device. When the device is mounted there you will go to that directory to manage the devices' data.

The next section, filesystem_type, describes the type of file system you wanting to mount. Linux supports quite a large selection of file systems such as: cramfs, efs, ext2, ext3, vfat, fat, nfs, udf, sysv, smbfs, minix, msdos, reiserfs, hpfs, hfs, iso9660, and many more.  There is also the auto file system type which means the kernel will discover the type.

The options section of fstab is where things start to grow a bit more complex. I will explain the more common options:

  • auto/noauto: The auto option is default and means the device will be mounted automatically. The noauto options means the device will not be mounted automatically. By "automatically" I mean either at boot or when the command mount -a is issued.
  • user/nouser: The user option allows all standard (and root) users to mount the device. The nouser options only allows the root user to mount the device.
  • ro: Mount the device in read only mode.
  • rw: Mount the device in read/write mode.
  • sync/async: The sync options writes data to the device on the fly (as soon as a command is issued) whereas the async option writes data later.
  • suid: This allos suid and sgid bits to be effective on the mounted file system.
  • defaults: Use all default options (rw, suid, dev, exec, auto, nouser, and async)

The final section is actually the dump/fsck section. Basically if you set these bits to 0 (off) the mounted devices will not be checked by either dump or fsck. You will rarely, if ever, need anything but zeros here.

Final Thoughts

So there it is. The basics of the oft-confusing fstab file. Later we will get into some sticker fstab issues, but for now you should have a pretty sound understanding of how fstab works.

  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:


    1. What is Linux? said on January 3, 2009 at 8:47 pm

      This is the page where I go for fstab issues, in fact, I had referred someone to there just today:

      I look forward to the next post, in which you touch upon common fstab problems. There are a few common issues with fstab, such as automounting and encoding errors, yet no page seems to tackle them all. Thanks!

    2. jack said on January 3, 2009 at 11:09 pm

      that’s a good reference site.

      can you give me an example of the encoding errors you are getting?

    3. What is KDE? said on January 4, 2009 at 1:45 pm

      It has been years since I personally have had the encoding problems, but these are the specifics as I remember them:
      CD-ROMs do not support utf-8 file names without hacks.
      Windows does not use UTF-8 for file names.
      Therefore, moving non-ASCII named files from a Windows drive to Linux, or from a CD, is problematic unless fstab is properly configured for that specific charset/encoding.

      You could probably troll linuxquestions.org for lots of examples.

    4. Trephined said on January 14, 2009 at 10:35 pm

      HAL FTW

    5. me said on May 30, 2013 at 1:50 am

      OMG I jest want to add a drive to Ubuntu server, all this bs is bs

    Leave a Reply