Kahibaro
Discord Login Register

Mounting and unmounting

Understanding Mount Points

Linux does not automatically “show” every filesystem; you must attach (mount) a filesystem into the single directory tree.

Key ideas specific to mounting:

Basic `mount` Command Usage

The mount command attaches a filesystem to a mount point.

General syntax:

bash
mount [options] <source> <mountpoint>

Where:

Viewing Currently Mounted Filesystems

To see what’s currently mounted:

bash
mount

or more readable:

bash
mount | column -t

Common alternatives:

bash
findmnt        # Structured overview (recommended)
df -h          # Shows mounted filesystems and usage

Examples:

bash
findmnt
findmnt /home          # Show only the mount that provides /home
findmnt /dev/sda1      # Show where this device is mounted

findmnt is especially useful to see the hierarchy of mounts and which device backs each directory.

Mounting by Device

Suppose you have a partition /dev/sdb1 you want to mount at /mnt/data:

  1. Create the mountpoint directory if it doesn’t exist:
bash
sudo mkdir -p /mnt/data
  1. Mount:
bash
sudo mount /dev/sdb1 /mnt/data
  1. Verify:
bash
findmnt /mnt/data
ls /mnt/data

If the filesystem type cannot be auto-detected, specify it:

bash
sudo mount -t ext4 /dev/sdb1 /mnt/data

Common filesystem types include ext4, xfs, btrfs, vfat, ntfs, nfs, cifs, etc.

Mounting by UUID or Label (Temporarily)

While /etc/fstab is the canonical place to use UUIDs/labels, you can also mount explicitly by them.

Find identifiers:

bash
lsblk -f
# or
blkid

You will see something like:

text
/dev/sdb1: UUID="1234-ABCD" TYPE="ext4" PARTLABEL="data"

Mount via UUID:

bash
sudo mount UUID=1234-ABCD /mnt/data

Mount via label:

bash
sudo mount LABEL=data /mnt/data

Mounting by UUID or label is more robust than using /dev/sdX names, which can change between boots, but persistent configuration typically belongs in /etc/fstab (covered elsewhere).

Temporary vs Persistent Mounts

Mount all filesystems defined in /etc/fstab (except those marked noauto):

bash
sudo mount -a

Common Mount Options

Mount options alter how the filesystem behaves. These are passed with -o:

bash
sudo mount -o <option1,option2,...> <source> <mountpoint>

Some commonly-used options:

bash
  sudo mount -o ro /dev/sdb1 /mnt/data
bash
  sudo mount -t vfat -o uid=1000,gid=1000 /dev/sdc1 /mnt/usb
bash
  sudo mount -t vfat -o uid=1000,gid=1000,umask=022 /dev/sdc1 /mnt/usb

Note that not all options apply to all filesystem types; consult man mount and the specific filesystem’s documentation.

Mounting Removable Media (USB Drives)

Most desktop environments auto-mount USB devices. On servers or minimal systems, you often mount them manually.

Typical workflow:

  1. Identify the device:
bash
lsblk
# or
sudo fdisk -l

Look for a new device (/dev/sdc1, /dev/sdd1, etc.) after plugging in the USB drive.

  1. Create a mountpoint (once):
bash
sudo mkdir -p /mnt/usb
  1. Mount (filesystem type usually vfat or exfat for USB sticks):
bash
sudo mount /dev/sdc1 /mnt/usb
# or, if needed:
sudo mount -t vfat /dev/sdc1 /mnt/usb
  1. Use the data:
bash
ls /mnt/usb
cp /mnt/usb/file .
  1. Unmount before unplugging (covered in more detail below):
bash
sudo umount /mnt/usb

Never unplug while it’s still mounted, especially when writing data.

Unmounting (`umount`)

Unmounting detaches a filesystem from its mount point and flushes writes.

Command:

bash
sudo umount <mountpoint-or-source>

Examples:

bash
sudo umount /mnt/data
sudo umount /dev/sdc1

When Unmounting Fails (“Target is busy”)

A common error:

text
umount: /mnt/data: target is busy.

This means some process is using the filesystem (open files, working directory, etc.).

Ways to resolve:

  1. Leave that directory:

If your shell is inside the mountpoint, unmount will fail. Move out:

bash
cd
sudo umount /mnt/data
  1. Close files using it:

Close any editors, file managers, terminals, or programs accessing files under that mount.

  1. Find which processes are using it:
bash
sudo lsof +f -- /mnt/data
bash
sudo fuser -vm /mnt/data

Then stop or kill those processes (carefully) and try unmounting again.

  1. Force or lazy unmount (with caution):
bash
  sudo umount -f /mnt/data
bash
  sudo umount -l /mnt/data

Use these only when normal unmounting doesn’t work and you understand the consequences (especially for writes).

Mounting and Unmounting with `systemd` (`systemd-mount`)

On systemd-based systems, you can also use systemd-mount for temporary, service-like mounts.

Example:

bash
sudo systemd-mount /dev/sdb1 /mnt/data

To list systemd-managed mounts:

bash
systemctl --type=mount

To unmount via systemd:

bash
sudo systemd-umount /mnt/data
# or through systemctl if you know the unit name:
sudo systemctl stop mnt-data.mount

This approach integrates mounts with systemd units, which can be useful in more advanced setups (timers, dependencies, etc.).

Bind Mounts

A bind mount makes an existing directory tree visible at another location in the filesystem. It does not create a separate filesystem; it’s just another view of the same data.

Example: make /var/www/html accessible under /srv/www:

  1. Ensure the target directory exists:
bash
sudo mkdir -p /srv/www
  1. Bind mount:
bash
sudo mount --bind /var/www/html /srv/www

Now /srv/www and /var/www/html refer to the same underlying files.

To unmount:

bash
sudo umount /srv/www

Bind mounts are especially handy for:

Persistent bind mounts (via /etc/fstab) are part of broader storage configuration and are not detailed here.

Special Mount Types: `tmpfs` and `proc` (Operational View)

These are usually configured by the system, but you may occasionally interact with them manually.

`tmpfs`

tmpfs is a memory-backed filesystem; common for /run, /tmp (on some systems).

Manual mount example:

bash
sudo mount -t tmpfs -o size=512M tmpfs /mnt/ramdisk

Unmount:

bash
sudo umount /mnt/ramdisk

Use cases include fast, temporary storage where persistence across reboots is not required.

`proc`, `sysfs`, `devpts`, etc.

These pseudo-filesystems provide interfaces to the kernel; they are typically mounted automatically during boot.

Example (only for specific troubleshooting or chroot environments):

bash
sudo mount -t proc proc /mnt/chroot/proc
sudo umount /mnt/chroot/proc

Manipulating these is an advanced use-case; normally you just need to know they are mounts and may show up in mount/findmnt output.

Safe Workflow for Temporary Mounts

A practical pattern for manual mounts:

  1. Create mountpoint (once):
bash
sudo mkdir -p /mnt/backup
  1. Mount when needed:
bash
sudo mount /dev/sdb1 /mnt/backup
  1. Verify:
bash
findmnt /mnt/backup
  1. Use:
bash
rsync -av /home/ /mnt/backup/home-backup/
  1. Flush and unmount:
bash
sync           # optional, forces pending writes to disk
sudo umount /mnt/backup
  1. Check it’s gone:
bash
findmnt /mnt/backup   # should show nothing

Building this habit (especially the unmount step) greatly reduces the risk of data corruption, particularly on removable media.

Views: 17

Comments

Please login to add a comment.

Don't have an account? Register now!