Thứ Năm, 23 tháng 12, 2021

Fstab File ( /etc/fstab ) Entry Options in Linux

 The ‘/etc/fstab’ file is one of the important configuration file used by Linux machines which specify the devices and partitions available and where/how to use these partitions. This file will be created/updated during the system installation. You need to modify or maintain it in the way you need to use the devices/partitions.

In Linux each device is available as a directory in ‘/dev’ folder. That is, when you connect a floppy drive or plug in any external device to a Linux machine, it will be showing up in /dev folder. But, you won’t be able to use the device or access contents from it using that device file. You need to mount the device in order to make it available for use. The fstab file allows you to specify how and what options need to be used for mounting a particular device or partition, so that it will be using that options every time you mount it. This file is read each time when the system is booted and the specified filesystem is mounted accordingly. You could also comment out the specified lines and can manually mount filesystem after reboot.

As an example, if your fstab file (/etc/fstab) contains the following entry,

/dev/hdc /cdrom iso9660 rw,noauto,user 0 0

You just need to issue the following command to mount cdrom after inserting a CD into the CD drive.

mount /cdrom

This will mount the CDROM to the folder /cdrom with the options ‘rw, noauto and user’ (we will go through the various options in detail later) and you will be browse the contents of the CD through the directory /cdrom.

fstab Options

Each line in fstab corresponds to a particular device or partitions. A sample entry on fstab file is as follows.

/etc/fstab
# device mounting_directory filesystem_type options dump fsck
/dev/hdc /cdrom iso9660 rw,noauto,user 0 0

• The first field (device)

corresponds to the device name. If you have plugged in an external device and confused about the device name, you need to use ‘dmesg’ or ‘tail –f /var/log/messages’ to find the device name. For SCSI hard disks, devices will be names like /dev/sda (first drive), /dev/sdb (second drive).

• The second field (mounting_directory)

mentions the mount point on which the device needs to be mounted. This directory should exist. That is, you need to create th directory before using mount command.

• Third field (filesystem_type)

is the filesystem type. The various important file system types are,

ext2 and ext3: Commonly all latest Linux partitions are Ext3. Ext3 is a newer filesystem type that differs from Ext2 in that it's journaled, meaning that if you turn the computer off without properly shutting down, you shouldn't lose any data and your system shouldn't spend ages doing filesystem checks the next time you boot up.

reiserfs - ReiserFS is a journaled filesystem, but it's much more advanced than Ext3. Many Linux distros (including SuSE) have started using ReiserFS as their default filesystem for Linux partitions.

Swap- The filesystem type "swap" is used in your swap partitions.

vfat and ntfs : The USB stick is most likely formatted as Vfat (more widely known as FAT32). The Windows partitions are probably either Vfat or NTFS.

ISO 9660 – This is a common format that target Compact Discs, DVDs and Blu-ray discs.

Auto- The option "auto" simply means that the filesystem type is detected automatically. Normally floppy or CDROMs will be given this option as their filesystem type may vary.

• The fourth field (options)

Describes the mount options. There are few number of mount options available. We will go through the important options that a server admin should be aware of.

auto and noauto : auto specifies that the device/partition should be automatically mounted on boot time and ‘noauto’ specifies that the device should be explicitly mounted. When you execute ‘mount –a’ all partition that has ‘auto’ value set will get mounted automatically. The root partitions should have the ‘auto’ option set so that the partition will get mounted automatically.

exec and noexec : The option ‘exec’ specifies that the files residing in that device will be able to execute and ‘noexec’ remove the execute feature. The partitions which are intended to keep non executable files like /var or /tmp can have noexec feature enabled for better security.

user and nouser : The ‘user’ option specifies that the users will be able to mount the partitions and ‘nouser’ specifies that only root user can mount any partitions. The ‘user’ option should be set for devices like ‘floppy or cdrom’ so that the users will be able to mount the device rather than being root.

Example

This will allow user to mount to directory other than root. Make sure you enabled permission on mountpoint directory for the required user before mounting. On a mounted directory even if you set permission it normally don't work.

/source/location /destination/mountpoint ext3 user,defaults 0 2

Other options, here you can explicitly set owner and group name

/source/location /destination/mountpoint ext3 -o uid=linux -o gid=opensource

or

/source/location /destination/mountpoint ext3 uid=linux,gid=opensource

ro and rw : The option ‘ro’ specifies that the filesystem should be mounted as read-only and the option ‘rw’ enables read-write.

sync and async – This specifies how the input and output to the filesystem should be done. sync means it should be done synchronously. That is, when you copy a file to the floppy, the changes are physically written to the floppy at the same time you issue the copy command. For ‘async’, the changes will be written only at the time of unmounting the floppy.

suid / nosuid – The option ‘suid’ permit the operation of suid, and sgid bits and the option ‘nosuid’ block the operation of suid and sgid bits.

Defaults - The normal default for Ext3 file systems is equivalent to rw,suid,dev,exec,auto,nouser,async(no acl support).

• The fifth field (dump)

Specifies the option that need to be used by the dump (backup utility) program. If the value is set to 0, then the partition is excluded from taking backup and if the option is a nonzero value, the filesystem will be backed up.

• The sixth field (fsck)

Mentions the fsck option. That is if the value is set to zero, the device or partition will be excluded from fsck check and if it is nonzero the fsck check will be run in the order in which the value is set.

The root partition will have this value set to one (1) so that it will be checked first by fsck. If you set to two (2) that means the system to do fsck on all rest partition marked '2' in sequence on system reboot.

If you face any troubles on mounting a partition, the file /etc/fstab should be checked for any misconfiguration.

Read More

How to Mount an NFS Share in Linux

 Network File System (NFS) is a distributed file system protocol that allows you to share remote directories over a network. With NFS, you can mount remote directories on your system and work with the remote files as if they were local files.

On Linux and UNIX operating systems, you can use the mount command to mount a shared NFS directory on a particular mount point in the local directory tree.

In this tutorial, we will show you how to manually and automatically mount an NFS share on Linux machines.

Installing NFS Client Packages

To mount an NFS share on a Linux system first you’ll need to install the NFS client package. The package name differs between Linux distributions.

  • Installing NFS client on Ubuntu and Debian:

    sudo apt updatesudo apt install nfs-common
  • Installing NFS client on CentOS and Fedora:

    sudo yum install nfs-utils

Manually Mounting an NFS File Systems

Mounting a remote NFS share is the same as mounting regular file systems.

To mount an NFS file system on a given mount point, use the mount command in the following form:

mount [OPTION...] NFS_SERVER:EXPORTED_DIRECTORY MOUNT_POINT

Use the steps below to manually mount a remote NFS share on your Linux system:

  1. First, create a directory to serve as the mount point for the remote NFS share:

    sudo mkdir /var/backups

    Mount point is a directory on the local machine where the NFS share is to be mounted.

  2. Mount the NFS share by running the following command as root or user with sudo privileges:

    sudo mount -t nfs 10.10.0.10:/backups /var/backups

    Where 10.10.0.10 is the IP address of the NFS server, /backup is the directory that the server is exporting and /var/backups is the local mount point.

    On success, no output is produced.

    If you want to specify additional mount options , use the -o option. Multiple options can be provided as a comma-separated list. To get a list of all mount options type man mount in your terminal.

  3. To verify that the remote NFS volume is successfully mounted use either the mount or df -h command.

Once the share is mounted, the mount point becomes the root directory of the mounted file system.

When you are manually mounting the share, the NFS share mount does not persist after a reboot.

Automatically Mounting NFS File Systems with /etc/fstab

Generally, you will want to mount the remote NFS directory automatically when the system boots.

The /etc/fstab file contains a list of entries that define where how and what filesystem will be mounted on system startup.

To automatically mount an NFS share when your Linux system starts up add a line to the /etc/fstab file. The line must include the hostname or the IP address of the NFS server, the exported directory, and the mount point on the local machine.

Use the following procedure to automatically mount an NFS share on Linux systems:

  1. Set up a mount point for the remote NFS share:

    sudo mkdir /var/backups
  2. Open the /etc/fstab file with your text editor :

    sudo nano /etc/fstab

    Add the following line to the file:

    /etc/fstab
    # <file system>     <dir>       <type>   <options>   <dump>	<pass>
    10.10.0.10:/backups /var/backups  nfs      defaults    0       0

    Where 10.10.0.10 the NFS server IP address, /backup is the exported directory and /var/backups is the local mount point.

  3. Run the mount command in one of the following forms to mount the NFS share:

    mount /var/backups
    mount 10.10.0.10:/backups

    The mount command, will read the content of the /etc/fstab and mount the share.

    Next time you reboot the system the NFS share will be mounted automatically.

Unmounting NFS File Systems

The umount command detaches (unmounts) the mounted file system from the directory tree.

To detach a mounted NFS share, use the umount command followed by either the directory where it has been mounted or remote share:

umount 10.10.0.10:/backups umount /var/backups

If the NFS mount have an entry in the fstab file, remove it.

The umount command will fail to detach the share when the mounted volume is in use. To find out which processes are accessing the NFS share, use the fuser command:

fuser -m MOUNT_POINT

Once you find the processes you can stop them with the kill command and unmount the NFS share.

If you still have problems unmounting the share use the -l (--lazy) option which allows you to unmount a busy file system as soon as it is not busy anymore.

umount -l MOUNT_POINT

If the remote NFS system is unreachable, use the -f (--force) option to force an unmount.

umount -f MOUNT_POINT

Generally not a good idea to use the force option as it may corrupt the data on the file system

Read More