How to Mount and unmount Filesystem / Partition in Linux [Mount/Umount unix Commands]

Mounting  A partition in Linux

Once you insert new hard disks into your system, you’ll typically use utilities like fdisk or parted to create partitions. Once you create a partition, you’ll use mkfs command to create or format  ext2, ext3, or ext4 partition.


Once you create a partition, you should use mount command to mount the partition into a mount point (a directory), to start using the filesystem.

About mount and umount

The mount command mounts a storage device or filesystem, making it accessible and attaching a directory to the file system.

The umount command "unmounts" a mounted filesystem, informing the system to complete any pending read or write operations, and safely detaching it.




There are two types of mounting which will be used in LINUX or any UNIX. These are fallows.
  • Temporary Mounting
  • Permanent Mounting

Temporary Mounting

In a temporary mount point we will create a directory and mount it but this mount point will lasr only till the system is up, once it is rebooted the mounting will be lost.

Syntax:
#mount <device name> <directory name (Mount Point)>

#mount /dev/sda8 /dir1

[root@rahul ~]# mount /dev/sda8 /dir1
[root@rahul ~]#


To view all the mounted partitions

After you execute mount a partition or filesystem, execute the mount command without any arguments to view all the mounts.

#mount

[root@rahul ~]# mount
/dev/sda2 on / type ext4 (rw)
oproc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/RHEL_6.0 x86_64 Disc 1 type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
/dev/sda8 on /dir1 type ext4 (rw)
[root@rahul ~]#


Now we have successfully mounted the partition we can access it and can store the data

To add the data access the mount point
  • #cd  /dir1

Add the data and exit the directory

How to Unmounting  a Partition

# umount <mount point directory>
# umount /kernel

[root@rahul ~]# umount /dir1


Now verify it with mount command

[root@rahul ~]# mount
/dev/sda2 on / type ext4 (rw)
oproc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/RHEL_6.0 x86_64 Disc 1 type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
[root@rahul ~]#


Permanent Mounting

Permanent mounting procedure is exactly same like temp mounting, but here we will update the /etc/fstab file with the mounting details, so that it will be mounted even after the system is reboot.
Steps To Make a permanent mount point:
  • Make a directory or use an existing directory
  • Add entry in /etc/fstab file
  • Use mount –a command to check it is mounting. (mount –a will mount all the entry placed in /etc/fstab)

Here we will be using our existing /dir1 directory as mount point which is created previously.

#vim /etc/fstab


#
# /etc/fstab
# Created by anaconda on Sat Aug 13 17:36:51 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=4afa457a-d662-4e89-b4e2-09303c44c3eb /                       ext4    defaults        1 1
UUID=752c2f33-fe60-4269-9480-b27260fc3777 /boot                   ext4    defaults        1 2
UUID=496b4058-1c64-47e0-97f8-8760f41aaef8 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
oproc                    /proc                   proc    defaults        0 0
/dev/sda5               swap                    swap    defaults        0 0
/dev/sda8               /dir1                     ext4    defaults        0      0
~                                                                              
~                     
Device name      Mount point      Type of fs   Mount options    Dumping       Check sequence  


The filesystems listed in /etc/fstab gets mounted during booting process. After booting, system administrator may unmount some of the partitions for various reasons. If you want all the filesystems to be mounted as specified in /etc/fstab, use -a option with mount as shown below:

#mount –a

[root@rahul ~]# mount -a
[root@rahul ~]# mount
/dev/sda2 on / type ext4 (rw)
oproc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/RHEL_6.0 x86_64 Disc 1 type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
/dev/sda8 on /dir1 type ext4 (rw,acl)
[root@rahul ~]#


You can now access the directory and add, delete or modify the contents and can also unmount the file system at any point.

  • How we can check all mounted partitions of specific type

It is possible to list only the specific type of filesystem mounted using the option -l with -t as shown below:
# mount -l -t ext2
/dev/sda6 on /mydata type ext2 (rw)
# mount -l -t ext4
/dev/sda5 on / type ext4 (rw,errors=remount-ro)
As seen above, /dev/sda6 is the only ext2 partition and /dev/sda5 is the only ext4 partition accordingly.

  • How to Mount a Floppy Disk in linux

The device file for floppy disk would exist under /dev directory. For example, a floppy disk will be mounted as shown below.
# mount /dev/fd0 /mnt
# cd /mnt
After the successful mount, you would be able to access the contents of the floppy disk. Once you are done with it, use umount before you physically remove the floppy disk from the system.
# umount /mnt

  • How we can Mount filesystem with read or read/write access

 To mount partition as read only, use -r option which is synonym to -o ro.
# mount /dev/sda6 /mydata -r
# mount  | grep /mydata

/dev/sda6 on /mydata type ext4 (ro)
ext3 and ext4 filesystem would still allow you to do write operation when the filesystem is dirty. So, you may have to use “ro,noload” to prevent these kind of write operation.

# mount /dev/sda6 /mydata -t ext4 -o ro -o noload
# mount | grep /mydata
/dev/sda6 on /mydata type ext4 (ro,noload)
To mount a partition with read/write access, use -w option which is same as “-o rw” (i.e : default).

  • How to Remount the mounted filesystem in linux

In order to mount the already mounted filesystem, use remount option and its normally used to remount the filesystem with read/write access when its previously mounted with read access.
The /mydata mount point is going to be remounted with read/write access from read access as shown below:

# mount | grep /mydata
/dev/sda6 on /mydata type ext4 (ro,noload)
# mount -o remount,rw /mydata
# mount | grep /mydata
/dev/sda6 on /mydata type ext4 (rw)

  • How to Mount an iso image into a directory

The iso image can be mounted as shown below:

# mount -t iso9660 -o loop notes_collections.iso /mnt
# cd /mnt
# ls
sql/    java/    .net/

  • How to Unmount more than one mount points

Umount allows you to unmount more than mount point in a single execution of umount of command as follows:
# umount /mydata  /backup
# mount | grep /mydata
# mount | grep /backup

  • How can i Forcefully unmount a filesystem

umount provides the option to forcefully unmount a filesystem with option -f when the device is busy as shown below:
# umount -f /mnt
If this doesn’t work for you, then you can go for lazy unmount.

Meanwhile, you can also have a look at ps command output that which process is presently using the mountpoint as shown below:
# ps ajx | grep /mydata
 2540  3037  3037  2468 pts/2     3037 D+       0   0:00 cp -r /home/geekstuff/ProjectData/ /mydata

You can also execute fuser command to find out which process is holding the directory for operations.
# fuser -cu /mydata
/mydata:              3087(root)

It gives you the process id with username (nothing but the owner of the process). If you know what that process is, you may want to stop that process and then try the umount again.
  •  How to assign a label to the partition?

Assigning the label means giving some name to partition. For assigning label to partition e2label command is used.

Syntax:
#e2label <partition name> <label>
Ex:
#e2label /dev/sda7 mountdisk

To check the assigned label
#e2label /dev/sda7

[root@rahul ~]# e2label /dev/sda8 rahul
[root@rahul ~]# e2label /dev/sda8
rahul
[root@rahul ~]#


To list all the mounted partition along with their labels, use mount –l command

  • How to mount a partition using its label?

Mounting a /dev/sda8 partition with its label rahul, verify it with mount command

[root@rahul ~]# mount LABEL=rahul /dir1
[root@rahul ~]# mount


Make a permanent mount point using label
As we know that to make a permanent mounting, as entry has to be made in /etc/fstab file.

# vim /etc/fstab

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
oproc                    /proc                   proc    defaults        0 0
/dev/sda5               swap                    swap    defaults        0 0
LABEL=rahul               /dir1                 ext4    defaults        0 0


Now use mount –a command and verify it with mount command whether it is mounted or not.
  • How to mount a partition permanently with its block id(UUID) ?

  1. To check a uuid of a partition use blkid /dev/sda8 command.
  2. Copy the uuid.
  3. Make an entry in /etc/fstab using UUID
  4. Verify it with mount –a option


[root@rahul ~]# blkid /dev/sda8
/dev/sda8: LABEL="rahul" UUID="2a5fb5c4-9500-414b-9598-fb82a470cc90" TYPE="ext4"
[root@rahul ~]#


# vim /etc/fstab

devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
oproc                    /proc                   proc    defaults        0 0
/dev/sda5               swap                    swap    defaults        0 0
UUID="2a5fb5c4-9500-414b-9598-fb82a470cc90"          /dir1        ext4    defaults        0 0


Now mount it with mount –a command and verify it with mount command.




Post a Comment