Technology Encyclopedia Home >How to mount and unmount file systems in Linux?

How to mount and unmount file systems in Linux?

In Linux, mounting and unmounting file systems is a common task for managing storage devices and network shares. Here's how to do it:

Mounting a File System

Mounting attaches a file system (like a hard drive partition, USB drive, or network share) to a directory (mount point) in the Linux directory hierarchy.

  1. Create a Mount Point:
    First, create a directory where the file system will be mounted.
    Example:

    sudo mkdir /mnt/mydrive
    
  2. Mount the File System:
    Use the mount command to attach the file system.

    • For a local partition (e.g., /dev/sdb1):
      sudo mount /dev/sdb1 /mnt/mydrive
      
    • For a network share (e.g., NFS or SMB):
      Example for NFS:
      sudo mount -t nfs 192.168.1.100:/shared /mnt/mydrive
      
      Example for SMB/CIFS:
      sudo mount -t cifs //server/share /mnt/mydrive -o username=user,password=pass
      
  3. Verify the Mount:
    Check if the file system is mounted using df -h or mount | grep mydrive.

Unmounting a File System

Unmounting detaches the file system from the directory, making it inaccessible until remounted.

  1. Unmount the File System:
    Use the umount command (note the spelling: umount, not "unmount").
    Example:

    sudo umount /mnt/mydrive
    

    If the file system is busy (e.g., files are open), use lsof to find and close processes, or force unmount with sudo umount -l /mnt/mydrive (lazy unmount).

  2. Verify Unmount:
    Check with df -h or mount | grep mydrive to confirm it's no longer mounted.

Persistent Mounts (for Reboots)

To ensure a file system mounts automatically after a reboot, add an entry to /etc/fstab.
Example for a local partition:

/dev/sdb1  /mnt/mydrive  ext4  defaults  0  2

For NFS:

192.168.1.100:/shared  /mnt/mydrive  nfs  defaults  0  0

Example Use Case

Mounting a USB drive:

  1. Plug in the USB drive.
  2. Identify the device (e.g., /dev/sdc1 using lsblk or fdisk -l).
  3. Mount it:
    sudo mount /dev/sdc1 /mnt/usb
    
  4. Unmount when done:
    sudo umount /mnt/usb
    

For cloud storage solutions, Tencent Cloud offers COS (Cloud Object Storage) and CBS (Cloud Block Storage). To mount CBS in Linux, use the mount command with the appropriate device path, similar to local storage. For COS, you can use SDKs or tools like cosfs to mount it as a file system.

For network shares, Tencent Cloud's CFS (Cloud File Storage) supports NFS and SMB protocols, which can be mounted using the commands above.