How to Use the Linux losetup Command to Mount Loop Devices

When working with raw disk images, ISO files, or encrypted filesystems in Linux, you cannot simply browse the contents like a normal folder. Because these files are unstructured blocks of binary data, the Linux kernel does not natively understand how to interact with them as standard file systems. To bridge this gap, you must mathematically map the raw file to a virtual block device, tricking the kernel into treating the file as if it were a physical hard drive plugged into the motherboard. To accomplish this, Linux systems administrators use the losetup command.

Why Use the losetup Command?

The losetup command is the primary utility for configuring and controlling \”loop devices.\” A loop device (typically designated as /dev/loop0, /dev/loop1, etc.) is a pseudo-device that allows a file to be accessed as a block device. This is a critical workflow in system administration. If you have downloaded an ISO installer, or if you are extracting a raw .img backup of a Raspberry Pi SD card, you must first attach it to a loop device using losetup before you can run the mount command to read the files inside.

Step 1: Find an Available Loop Device

Before you can attach a file, you need to ask the kernel which virtual slots are currently empty and available for use.

  1. Open your Linux terminal.
  2. Run the command to find the first unused loop device (requires root privileges):
sudo losetup -f

The terminal will instantly reply with the exact path to the next available virtual node, such as /dev/loop0. This is the pseudo-device you will use for your mapping.

Step 2: Attach the File to the Loop Device

Once you know the available slot, you can mathematically bind your raw file to it.

  1. Run the losetup command, specifying the loop device first, followed by the path to your raw file:
sudo losetup /dev/loop0 ~/Downloads/ubuntu-server.iso

The command executes silently. The kernel has now created a mathematical bridge. As far as the operating system is concerned, a physical CD-ROM or hard drive has just been connected to the /dev/loop0 hardware port.

Step 3: Mount the Virtual Device

Now that the file is acting as a block device, you can use standard filesystem tools to interact with it.

  1. Create a temporary directory to serve as your mount point:
sudo mkdir /mnt/iso_mount
  1. Mount the loop device to that directory:
sudo mount /dev/loop0 /mnt/iso_mount

You can now use the cd /mnt/iso_mount command to navigate into the directory and read, copy, or extract the files natively.

Step 4: Detach the Loop Device Safely

When you are finished analyzing the data, you must carefully destroy the mathematical bridge to prevent data corruption and free up kernel resources.

  1. First, unmount the directory to stop filesystem access:
sudo umount /mnt/iso_mount
  1. Finally, use the -d (detach) flag with losetup to sever the file from the pseudo-device:
sudo losetup -d /dev/loop0

By mastering the losetup command, Linux engineers can natively mount, analyze, and repair raw disk images and binary filesystems without requiring third-party extraction software.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.