Linux's Mount Table /etc/fstab
As we saw previously in our example of creating multiple partitions on one disk that we need to add our devices and mount points into Linux's mount table. This is a file that contains information regarding partitions that should be mounted at boot time. Below is a copy of the mount table used for our previous example Creating Filesystems and Partitions.
root@centos ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Wed Jan 9 22:27:17 2013
# /dev/mapper/vg_centos-lv_root / ext4 defaults 1 1 UUID=65e88b9b-a4b7-408b-86a5-335c9f91c404 /boot ext4 defaults 1 2 /dev/mapper/vg_centos-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 # # Added for fstab demo landoflinux.com # /dev/sdb1 /test1 ext3 defaults 0 0 /dev/sdb2 /test2 ext3 defaults 0 0 /dev/sdb3 /test3 ext3 defaults 0 0 /dev/sdb5 /test4 ext3 defaults 0 0 /dev/sdb6 /test5 ext3 defaults 0 0 /dev/sdb7 /test6 ext3 defaults 0 0 /dev/sdb8 /test7 ext3 defaults 0 0
The location of the mount table is held in a file located in the following path: "/etc/fstab". This file is generally maintained by the system administrator of the server. Each line within the file describes the device/filesystems that are available to your system. Each field is separated by spaces or a tab. There are six fields for each entry:
Field One: Filesystem - This field describes the device or filesystem that is to be mounted. Ordinary mounts such as "/dev/sdb1" can be seen here. You may also come across NFS mounts. These are filesystems that are hosted by a remote server. These can be identified easily by the "nfs"
/rmtserver:/opt/test /mynfs nfs ro,bg 0 0
The above "nfs" example will mount "/opt/test" from the remote server "rmtserver" on the local mount point "/mynfs". This will be a read only mount. The "bg" specifies that it should be mounted as a background task.
On many newer systems it is quite common to see devices specified by there "UUID" Universal Unique Identifier.
Field Two: Mount Point field. For a "swap" partition this field should be specified as "none".
Field Three: This field describes the type of filesystem. Linux can support many different filesystems - "ext2, ext3, ext4, hfs, iso9600, minix, msdos, nfs, ntfs, reiserfs, smbfs, vfat, xfs plus many more." For filesystems supported by the running kernel see "/proc/filesystems".
Field Four: This field describes the mount options associated with the filesystem. This is a comma separated list. It should contain the type of mount plus any additional options.
Basic Filesystem options:
Use default options: rw, suid, dev, exec, auto, nouser and async.
Mount automatically at boot time.
Do not mount automatically at boot time.
Allow a normal user to mount.
Allow device owner to mount.
Used by fstab maintaining programs.
Do not report errors if this does not exist.
Field Five:This field is used to identify which filesystems should by dumped by the "dump" command. A value of zero "0" indicates that no dump is to take place.
Field Six:This field is used by the "fsck" program to determine the order of which filesystems checks are done at reboot time. The "/" root filesystem should have a value of "1" and other filesystems should have a value of "2". If this filed is not present or has a value of "0" then it is assumed that no check is to take place.
Mounting and Unmounting Filesystems
All files accessible on a Linux system are arranged in a tree like structure. These files can be spread across multiple devices. The mount command function is to attach a filesystem found on a device to the tree structure. The opposite of the mount command is the "umount" command. Umount detaches the filesystem.
When mounting in Linux you must tell the mount command what device or partition you want to mount and where to mount it (mount point).
Example of mount command: mount /dev/sdb1 /test1
In the above example we are mounting the device "/dev/sdb1" on the mount point "/test1".
Many Linux distributions come with pre-defined mount points such as "/mnt" and "/media". Here you can mount a "CD" or a "DVD". It is always useful to name the mount so that it reflects the device that is mounted or to be mounted.
If a device is specified in the mount table "/etc/fstab", then you may issue the mount command without specifying the mount point. This is because the device can be found in the mount table along with its associated mount point:
fstab example line:
/dev/sdb1 /test1 ext3 defaults 0 0
So based on the above entry we could issue: mount /dev/sdb1
Basic Syntax: mount -t type device dir
The above command tells the kernel to attach the filesystem found on device (of which is of type) at the directory dir.
Displays help information.
Display version information.
Displays mounted filesystems.
mount [-l] [-t type]
Displays all mounted filesystems of type. "-l" adds label listings.
A list of all mounted filesystems is also contained in a file located at "/etc/mtab". This list is updated when the mount and umount commands are issued.
Mount all devices in /etc/fstab unless they have the "noauto" option defined.
Mounting as a none root user
Normally to mount a device you will require superuser privileges. However, it is possible to specify that normal users can mount devices. This is achieved by adding the option "user" into the "fstab" file. For example:
/dev/cdrom /cd iso9660 ro,user,noauto,unhide
This will allow any user issuing the mount command for "/dev/cdrom" or "/cd" to mount the iso9660 filesystem from the "cd".