Mounting filesystem in linux

How to Mount & Unmount Filesystem/Partition in Linux/UNIX

When we install new hard disk into our Linux system, typically we use utilities such as fdisk or parted to create partitions. After creating a partitions, we use mkfs command to create ex2, ext3, or ext4 partition. Than we use mount command to mount the partition into a mount point ( directory ) to use the filesystem.

In this tutorial I am going to explain how we can use mount and unmount command in Linux with example.

Mount command syntax

mount -t type device mount_point

1. Mounting A CD-ROM

The device file for CD exist under /dev directory, you can mount it like below.

# mount -t iso9660 -o ro /dev/cdrom /mnt

-o ro : Read Only access, CD ROM mounted wtih read only access
/mnt : Mount Point

2. Show All Mounts

To show the all mounts, execute the mount command without any arguments.

# mount /dev/sda5 on / type ext4 (rw,errors=remount-ro) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) none on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620) tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755) none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880) none on /run/shm type tmpfs (rw,nosuid,nodev)

OR, You can also use df command to show all the mount points.

# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda5 195069136 128345036 56958520 70% / udev 2008336 4 2008332 1% /dev tmpfs 806244 928 805316 1% /run none 5120 0 5120 0% /run/lock none 2015604 228 2015376 1% /run/shm

3. Mount All The Filesystem Mentioned In /etc/fstab

/etc/fstab is used to mount the all filesystem during booting process. Follow the below command to mount all the filesytem mentioned in /etc/fstab.

# cat /etc/fstab # proc /proc proc nodev,noexec,nosuid 0 0 /dev/sda5 / ext4 errors=remount-ro 0 1 /dev/sda6 /mydata ext2 defaults 0 2 /dev/sda7 /backup vfat defaults 0 3

Now execute the mount command with -a option to mount all the /etc/fstab entries.

# mount -a # mount /dev/sda5 on / type ext4 (rw,errors=remount-ro) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) none on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620) tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755) none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880) none on /run/shm type tmpfs (rw,nosuid,nodev) /dev/sda6 on /mydata type ext2 (rw) /dev/sda7 on /backup type vfat (rw)

To unmount all filesystem mentioned in /etc/fstab use same -a option with umount command like below.

# umount -a umount: /run/shm: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) umount: /run: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) umount: /dev: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) umount: /: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))

Above some filesystem are unable to unmount because it is busy or currently in use.

Читайте также:  Proprietary drivers manjaro linux

4. Mount Only A Specific Filesystem From /etc/fstab

It looks the mount point entries in “/etc/fstab” when you pass the directory name to mount.

# mount | grep /mountpoint # cat /etc/fstab | grep mountpoint /dev/sda6 /mountpoint ext2 defaults 0 2

Above You can see /mountpoint is not mounted but it is mentioned in ” /etc/fstab ” file. Now execute the below command to mount it.

# mount /mountpoint # mount | grep /mountpoint /dev/sda6 on /mountpointtype ext2 (rw)

You will get below error if we run again mount command.

# mount /mountpoint mount: /dev/sda6 already mounted or /mountpointbusy mount: according to mtab, /dev/sda6 is already mounted on /mountpoint

You can also use device name instead of directory name to mount.

5. Show All Specific Type Mounted Partitions

Using -l with -t option you can list only specific type of filesystem like below.

# mount -l -t ext2 /dev/sda6 on /mountpoint type ext2 (rw) # mount -l -t ext4 /dev/sda5 on / type ext4 (rw,errors=remount-ro)

6. Floppy Disk Mounting

The device file for Floppy Disk exist under /dev directory, you can mount it like below.

7. Bind Mounts

Bind mounts are quite simple. Instead of mounting a device (with a file system) on a particular path you are mounting one path into another path.

For example: Let’s say you have a small /var but a very large /opt partition and you need additional space for your growing log files.

First, shut down the services writing to log files, then…

# mv /var/log /opt/var_log # mkdir /var/log # mount -o bind /opt/var_log /var/log

Now check the bind is done using below command.

# mount | grep var /opt/var_log on /var/log type none (rw,bind)

Now restart the previously stopped services.

If want to persist across reboots, you will need to update your /etc/fstab file like below.

# vim /etc/fstab /opt/var_log /var/log none bind 0 0

8. Access The Data From New Mount Point

Follow the below command to access the data from new mounted point.

Now you can check the old mount point moved to new mount point as shown below.

# mount | grep /mountpoint # mount | grep /mnt /dev/sda6 on /mnt type ext2 (rw)

9. Mount Filesystem With Read/Write Access

Follow the below command to mount the filesytem with Read only access.

# mount /dev/sda6 /mountpoint -r # mount | grep /mountpoint /dev/sda6 on /mountpoint type ext4 (ro)

Here -r option is synonym to -o ro.

10. Remount The Mounted Filesystem

Follow the below command to remount the mounted filesytem.

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

11. Mount ISO Image

Follow the below command to mount the ISO image.

# mount -t iso9660 -o loop centos_img.iso /mnt

12. Unmount Multiple Mounted Point

Follow the below command to unmount the more than one mount point.

# umount /mountpoint /mountpoint2 # mount | grep /mountpoint # mount | grep /mountpoint2

13. Lazy Unmount Of A Filesystem

Use lazy umount in cases where it was obviously stuck for various reasons (such as nfs server down), also when you need to see the original content of the directory that was mounted over by the mount. In both cases the mount is busy than you can do lazy unmount filesystem.

Читайте также:  Linux как посмотреть всех пользователей

14. Unmounted File System Forcefully

Follow the below command to unmount the filesystem forcefully if device is busy.

If in any case it does not work go with lazy unmount.

I hope this article will help to mount and unmount the filesystem or partition in Linux. If you have any queries and problem please comment in comment section.

Источник

How to Mount and Unmount Filesystems in Linux

The Linux operating system is basically a file system and without these files’ formats and structures, the definition of this operating system environment would be incomplete.

In other words, files are the central units of the Linux operating system architecture. The Linux filesystem stores OS-associated files and directories. Filesystems are also evident in USB and hard disk drives and a properly configured network can also permit the sharing of these filesystems among different machines.

This article seeks to expose us to the mount and umount commands primarily used in attaching and detaching file systems from various mount points.

Listing Mounted Filesystems in Linux

Linux makes it possible for a filesystem to be mounted on any directory location. A successfully mounted filesystem makes it possible for its associated/stored files and directories to be accessible from that mount directory. The directories are also referred to as the filesystem mount points.

By default, your Linux operating system already has some filesystems mounted. To get a preview of these mounted filesystems, execute the Linux mount command without any flags or arguments.

Check Linux Mounted File System

Consider the following last entry from the execution of the above mount command.

/dev/sdb5 on /media/dnyce/117137A85FFD287C type fuseblk (rw,nosuid,nodev,relatime,user_id=0)
  • /dev/sdb5 is the mounted filesystem/device name.
  • /media/dnyce/117137A85FFD287C is the mount point.
  • fuseblk is the filesystem type.
  • (rw,nosuid,nodev,relatime,user_id=0) is the mount options.

To review the mounting information associated with a specific filesystem type like fuseblk, vfat, ext4, fuseblk, etc., we can implement a mount command (with a -t flag) similar to the following:

Check Mounted File System Type

Mounting Filesystems in Linux

To successfully mount a filesystem, we have to adhere to the following two rules:

  • Make sure a mount point exists or create one using the Linux mkdir command.
  • Reference the following command syntax to successfully mount the filesystem.
$ sudo mount -t Type Device MountPoint

A Linux operating system can automatically detect the type of filesystem associated with a device hence the use of the -t option is not explicitly needed.

However, in cases where a partition is not formatted or is corrupted and/or mandatory filesystem tools like the ntfs-3g package (needed for read and write access for NTFS partitions) are not installed, the inclusion of the -t option is necessary to successfully mount that filesystem.

Читайте также:  Установка альт линукс 9 рядом с виндосом

For security reasons, Linux only allows sudoer/root users to perform mounting operations. Other users will be able to have directory access after the mounting point directories are set with the needed permissions (by root/sudoer users).

Mounting USB Drive/Stick in Linux

First, identify the USB block device with the Linux fdisk command.

Check USB Device in Linux

Create a mount point if you do not have one yet.

$ sudo mount /dev/sdb1 /mnt/usb32G

The USB drive should now be accessible from the /mnt/usb32G directory.

Mount USB Drive in Linux

Mounting ISO Files in Linux

A pseudo-device known as a loop device is effective in mounting an ISO image since it is not restricted to a physical device. This loop device makes the ISO image files accessible.

Create a mount point directory:

$ sudo mkdir /mnt/CorePlusIso

Mount the ISO image and make sure to check the absolute path to the ISO file.

$ sudo mount $HOME/Downloads/CorePlus-current.iso /mnt/CorePlusIso -o loop

Mount ISO File in Linux

You will notice that ISO images have read-only permission access. To make modifications, you will need to duplicate the ISO files from the mount directory to another directory location before making your needed changes.

$ ls -l /mnt/CorePlusIso $ sudo mount | grep CorePlusIso

Check ISO Files in Linux

Useful Mount -o Options

The -loop is for mounting loop devices.

$ sudo mount -t Type Device MountPoint -o loop

The -rw enables read-write on the mounted filesystem.

$ sudo mount -t Type Device MountPoint -o rw

The -ro enables read-only on the mounted filesystem.

$ sudo mount -t Type Device MountPoint -o ro

The -noauto disables automatic mounting of the filesystem during system reboot.

$ sudo mount -t Type Device MountPoint -o noauto

Mount File System Automatically in Linux

Use the /etc/fstab file when you want your filesystem to automatically mount after a successful system reboot.

Linux Fstab File

For instance, to make the CorePlus-current.iso file mount automatically after mounting it in the /mnt/CorePlusIso directory, first run the command:

$ sudo mount | grep CorePlusIso

List ISO Mount in Linux

The above command execution produces an output like:

/home/dnyce/Downloads/CorePlus-current.iso on /mnt/CorePlusIso type iso9660 (ro,relatime,nojoliet,check=s,map=n,blocksize=2048,iocharset=utf8)

Edit the above output to be something like:

    /home/dnyce/Downloads/CorePlus-current.iso /mnt/CorePlusIso iso9660 ro,relatime,utf8

The above line can then be added as an entry in the /etc/fstab file.

Once the Linux system reboots, mounting the above file will be as easy as pointing to its file system directory.

$ sudo mount /home/dnyce/Downloads

Unmounting a Filesystem in Linux

If it is a USB device labeled /dev/sdb1, we can unmount it with the command:

For an iso image, unmount it from its mount point directory:

$ sudo umount /mnt/CorePlusIso

To unmount multiple filesystems adhere to the command:

$ sudo umount /dev/sdb1 /mnt/CorePlusIso

When wanting to unmount a filesystem and still unsure whether it is operational, use the -l flag which will make sure the pending read and write operation completes before successfully unmounting the filesystem.

If you want to force a filesystem to unmount, implement the command:

We have successfully covered how to mount various filesystems in Linux both temporarily and permanently. We have also demonstrated to effectively unmount the same filesystems. Hope this article guide was useful. Feel free to leave a comment or feedback.

Источник

Оцените статью
Adblock
detector