Linux unmount if mounted

How To Unmount Disk in Linux, Ubuntu, CentOS with umount Command

Linux distributions like Ubuntu, Debian, CentOS, RHEL, and others use disks by mounting them to the file system. We have already examined mount command in the following tutorial. In this tutorial we will learn how to umount the disk in a Linux system.

Before Unmount

Before unmount we should be sure that all changes are written to the file system and disk. So we need to close open files that reside in the file system we want to unmount.

umount command help information can be listed with —help like below. We can see some different help options in a brief way like unmount all filesystems, verbose etc.

List Mounted File Systems

Before unmounting filesystems and partitions we may need to list currently mounted filesystems and partitions.We can use the command mount in order to list currently mounted file systems and partitions with some information.

Alternatively lsblk command can be used already mounted file systems which provides more hierarchical list and eliminate unnecassary information.

Umount Specified Partition

We will start by unmounting specified partition. We can unmount just providing the partition path. In this example, we will unmount /dev/hda1 . We need root privileges in order to complete this job which is gained with the sudo command.

When the unmount is completed succesfully there will be no message about the process which simply sign the succesfull unmount. If there are some messages which are generally related with the error this means some error which prevents the unmount operation.

Unmount All Partitions

If we need to unmount all partitions of file systems currently mounted to the Linux system. We will use -a option which means all.

Force To Unmount

In some cases write operations can be resume for a long time and we need to unmount the file system. So we can force umount command with -f option like below. The following command will unmount the file system located in /dev/hda1 in a forceful way which may create some minor file system related errors in the next mount. Alternatively, the —force option can be used to force unmount. We can see the error like ” umount:/mnt devices is busy” error like below.

Alternatively we can list already opened file descriptor with the lsof command like below. We will just provide the mount path where the opened files will be list.

Verbose

If umount failed we can find detailed information about the reason. We can use -v option which will list detailed information while trying to unmount.

Источник

How do I forcefully unmount a Linux disk partition?

S ometimes you try to unmount a disk partition or mounted CD/DVD disk or device, which is accessed by other users, then you will get an error umount: /xxx: device is busy. However, Linux or FreeBSD comes with the fuser command to kill forcefully mounted partition. For example, you can kill all processes accessing the file system mounted at /nas01 using the fuser command.

Understanding device error busy error

Linux / UNIX will not allow you to unmount a device that is busy. There are many reasons for this (such as program accessing partition or open file) , but the most important one is to prevent the data loss. Try the following command to find out what processes have activities on the device/partition. If your device name is /dev/sdb1, enter the following command as root user:
# lsof | grep ‘/dev/sda1’
Output:

Above output tells that user vivek has a vi process running that is using /dev/sda1. All you have to do is stop vi process and run umount again. As soon as that program terminates its task, the device will no longer be busy and you can unmount it with the following command:
# umount /dev/sda1

Читайте также:  Windows 10 домен 2003 smb1

How do I list the users on the file-system /nas01/?

Type the following command:
# fuser -u /nas01/
# fuser -u /var/www/
Sample outputs:

The following discussion allows you to unmout device and partition forcefully using mount or fuser Linux commands.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Linux fuser command to forcefully unmount a disk partition

Suppose you have /dev/sda1 mounted on /mnt directory then you can use fuser command as follows:

Type the command to unmount /mnt forcefully:
# fuser -km /mnt
Where,

  • -k : Kill processes accessing the file.
  • -m : Name specifies a file on a mounted file system or a block device that is mounted. In above example you are using /mnt

Linux umount command to unmount a disk partition.

You can also try the umount command with –l option on a Linux based system:
# umount -l /mnt
Where,

  • -l : Also known as Lazy unmount. Detach the filesystem from the filesystem hierarchy now, and cleanup all references to the filesystem as soon as it is not busy anymore. This option works with kernel version 2.4.11+ and above only.

If you would like to unmount a NFS mount point then try following command:
# umount -f /mnt
Where,

  • -f: Force unmount in case of an unreachable NFS system

Please note that using these commands or options can cause data loss for open files; programs which access files after the file system has been unmounted will get an error.

Источник

How to Mount and Unmount Filesystem in Linux

1. Introduction

Mount is to access a filesystem in Linux. You can mount a filesystem on any directory and access content by entering to that directory. In Linux terms, these directories are called mount points. This tutorial will help you to mount and unmount filesystem in Linux system.

2. Use mount Command

Mostly, each Linux/Unix operating systems provides mount command. This command is used to mounting any file system on any directory. After that you can access the filesystem content.

For example, you have added a disk /dev/sdb on on your system. Now you want to mount this on /data directory. Use following command to mount it.

Mount command automatically detects the file system on disk. But in some cases, you need specify the file system type with command.

3. Unmount Filesystem

Use umount command to unmount any mounted filesystem on your system. Run umount command with disk name or mount point name to unmount currently mounted disk.

4. Mount Disk on System Boot

You also required to mount disk on system boot. So that partitions will be available on system boot. /etc/fstab file is used to mount disks. You need to edit /etc/fstab and make new entry to mount the partitions automatically.

Edit /etc/fstab and append below line at end of file. Change /dev/sdb with your disk name.

Now run mount -a command to immediate mount all disk defined in /etc/fstab file.

Источник

How to Mount and Unmount Filesystem / Partition in Linux (Mount/Umount Command Examples)

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 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.

This tutorial explains everything you need to know about both mount and umount command with 15 practical examples.

The general mount command syntax to mount a device:

1. Mount a CD-ROM

The device file for CD would exist under /dev directory. For example, a CD-ROM device will be mounted as shown below.

In the above example, the option “-o ro” indicates that the cdrom should be mounted with read-only access. Also, make sure that the destination directory (in the above example, /mnt) exist before you execute the mount command.

2. View All Mounts

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

In the example below, after mounting the USB drive on a system, the output of mount looks like the below. As seen below, the USB device (i.e:/dev/sdb) is mounted on /media/myusb, which is displayed as the last line in the mount command.

You can also use df command to view all the mount points.

3. Mount all the filesystem mentioned in /etc/fstab

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:

Читайте также:  Не монтирует linux mint

Example /etc/fstab file entries:

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

The same -a option can be used with umount to unmount all the filesystems mentioned in /etc/mtab

Some filesystem are not unmounted as its busy or currently in use. Note that the files /etc/mtab and /proc/mounts contents would be similar.

4. Mount only a specific filesystem from /etc/fstab

When you pass only the directory name to mount, it looks for mount point entries, if not found, then search continuous for a device in /etc/fstab and gets mounted.

As seen above, /mydata directory is not a mountpoint, but it is present in /etc/fstab.

If you execute the same again, you would get the error message as follows:

Here you may also pass the device name instead of directory name (to be picked up from /etc/fstab file).

Note that the files /etc/mtab and /proc/mounts contents would be similar.

5. View 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:

As seen above, /dev/sda6 is the only ext2 partition and /dev/sda5 is the only ext4 partition accordingly.

6. Mount a Floppy Disk

The device file for floppy disk would exist under /dev directory. For example, a floppy disk will be mounted as shown below.

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.

7. Bind mount points to a new directory

The mountpoint can be binded to a new directory. So that you would be able to access the contents of a filesystem via more than one mountpoints at the same time.

Use -B option with olddir and newdir to be binded as follows,

Now the bind is done and you might verify it as follows,

As seen above the bind is done properly. So when you do modification in filesystem in one place, you can see those reflection of it in other mount point as shown below:

8. Access contents from new mount point

Mount allows you to access the contents of a mount point from a new mount point. Its nothing but move a mounted tree to another place.

In the example below, the mount point /mydata will be accessed from /mnt using the option -M as shown below:

Once its done, you cant use the old mount point as its moved to a new mount point and this can be verified as shown below:

9. Mount without writing entry into /etc/mtab

During read only mount of /etc/, the /etc/mtab file entries cannot be modified by mount command. However, mount can be done without writing into /etc/mtab by using the option -n as follows,

You cannot see any entry for this /mydata in mount command output and as well from /etc/mtab file as follows:

Access the contents of a mounted directory /mydata:

10. Mount filesystem with read or read/write access

To mount partition as read only, use -r option which is synonym to -o 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.

To mount a partition with read/write access, use -w option which is same as “-o rw” (i.e : default).

11. Remount the mounted filesystem

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:

12. Mount an iso image into a directory

The iso image can be mounted as shown below:

13. 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:

14. Lazy unmount of a filesystem

This is a special option in umount, in case you want to unmount a partition after disk operations are done. You can issue command umount -l with that partition and the unmount will be done after the disk operations gets finished.

For instance, consider a scenario that a task (i.e: script or any other command) is doing a copy operation on a disk and at the same time you are allowed to issue a unmount with -l, so that unmount would be done once the copy is over (i.e: the disk operation).

15. Forcefully unmount a filesystem

umount provides the option to forcefully unmount a filesystem with option -f when the device is busy as shown below:

If this doesn’t work for you, then you can go for lazy unmount.

Читайте также:  Windows cmd список файлов

Meanwhile, you can also have a look at ps command output that which process is presently using the mountpoint as shown below:

You can also execute fuser command to find out which process is holding the directory for operations.

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.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook
  • Awk Introduction – 7 Awk Print Examples
  • Advanced Sed Substitution Examples
  • 8 Essential Vim Editor Navigation Fundamentals
  • 25 Most Frequently Used Linux IPTables Rules Examples
  • Turbocharge PuTTY with 12 Powerful Add-Ons

Thank you for this stuff….. But i have an another doubt is it possible to mount the NTFS filesystem in the system……………….?

Quote”after mounting the USB drive on a system”…….

Maybe you should tell us about HOW to mount an USB 🙂

At one time, the way I did it involved figuring-out which device in the hub it was attached to. I would plug the device in, wait 5 seconds then issue a dmsg command and filter it on SCSI devices (#dmsg | grep sd). Now, it is automatically mounted under /media/usb.

Thanks, very useful article

you can mount NTFS disks,
Use ntfs-3g for this
or mount -t ntfs /dev/foo /mnt/point

Usage: ntfs-3g [-o option[,…]]

Options: ro (read-only mount), remove_hiberfile, uid=, gid=,
umask=, fmask=, dmask=, streams_interface=, syncio.
Please see the details in the manual (type: man ntfs-3g).

Example: ntfs-3g /dev/sda1 /mnt/windows
Further Info: Ntfs-3g news, support and information: http://ntfs-3g.org

helpfull suggestion!
much thanks.

Hi, It was very good. Thanks a lot.

I am adding one thing which is missing is SAN share nfs & cifs volumes mounting.

# mount IPAddress:/vol/vol_nfs_01_10/qtree_01 /opt/myshare

SAN volume share given by SAN Team.

Install ntfs-3g pkg to mount NTFS FS.

Hope this thread is still active! I’ve setup Cygwin on a windows box for a subversion solution and trying to map SVN repository which is on a network share. I am able to do it by net use command but I need a permanent mount which works regardless of whether the server is being RDPed or otherwise.
I’ve update FSTAB with the following:

//NTSHARE/SVNREP /home/mnt user,rw,auto,errors=remount-ro 0 0

But it doesn’t seem to mount the share as I’d hoped. What am I missing? Any help on this would be greatly appreciated. Thanks.

Sir, I have a question regarding the mounting of the nfs ??
I had given the command
“wolf00:/mnt/wolf /mnt/wolf nfs rw,hard,intr 0 0”
will it work fine?becoz its not working in my system . I am currently trying to work on RHEL-4

Very useful..
Thanks.

Your all suggetions are really helpful ..

Keep This Good Work On…

Hello, I have a question. What command I need to mount the “var” folder on a magento install. Server is running on centOS.

I have a simple question that I am just missing. How can I tell what type a drive is to mount it. You use several examples above but there is not an explanation to fine the type. Any help would be greatly appreciated.

Thanks your article!

Your example is very useful to me!
Write the useful article like this for linux beginner!

Wish your help!
In Cent Os7, on booting, which script file is the first executed?

I want install C++ software in ubuntu 14.04, so can anyone help me?

Should this be an issue. Linux is still work in progress. I just need to access my drives and that’s all. why the hell should i go through all these troubles just to access my files on another drive?

I am running Trisquel GNU/Linux 7.0 Mini, booting from a USB flash drive. The files I want to access are on the hard drive of the laptop.

I think I am clear on this… I need to know the name of the file system on the hard drive and the type of extension in order to mount it and get at my files? I think this is right. It has to be, because Trisquel does not mount the file systems on the hard drive at bootup. It simply shows they exist by labeling them, “500 GB Volume”, and so on down the line. And when I try to mount them by clicking on them, Trisquel tells me that I do not have permission to mount these file systems, nor does it tell me the names or type of the file systems. I am sure I need to know what they are already. I do know what they are, but I do not want to try these mounting commands until I am clear on what I am doing. I don’t want to screw up any of my drives or files, don’t want to rename them, etc. I only want to leave them as they are, access them from Trisquel, and get to work on them.

Источник

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