• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

losetup Command Examples in Linux

by Deepika

Loopback filesystems are very interesting components of Linux-like systems. We usually create filesystems on devices (for example, disk drive partitions). These storage devices are available as device files such as /dev/device_name. In order to use the storage device filesystem, we mount it at a directory called a mount point . On the other hand, loopback filesystems are those that we create in files rather than a physical device. We can then mount those files as filesystems at a mount point. This essentially lets you create logical “disks” inside a file on your physical disk!

losetup is a system administration command. It is used to set up and control loop devices. It can attach a loop device to a regular file or block device, detach a loop device, or query a loop device. A loop device can be used to mount an image file as if it were a normal device.

Command line Options

Option Description
-d Detach specified loopdevice.
-e encryption, -Enumber Use specified kernel encryption module when performing writes and reads. (Usually NONE, DES, and XOR.)
-o offset Start reading data at offset bytes from the beginning of file.
-p fd Read the passphrase from file descriptor fd.

An ISO file is an archive of an optical media. We can mount ISO files in the same way that we mount physical disks by using loopback mounting. We can even use a nonempty directory as the mount path. Then, the mount path will contain data from the devices rather than the original contents until the device is unmounted. For example:

# mkdir /mnt/iso
# mount -o loop linux.iso /mnt/iso

Now perform operations using files from /mnt/iso. ISO is a read-only filesystem.

losetup Command Examples

1. List loop devices with detailed info:

# losetup -a

2. Attach a file to a given loop device:

# sudo losetup /dev/loop /path/to/file

3. Attach a file to a new free loop device and scan the device for partitions:

# sudo losetup --show --partscan -f /path/to/file

4. Attach a file to a read-only loop device:

# sudo losetup --read-only /dev/loop /path/to/file

5. Detach all loop devices:

# sudo losetup -D

6. Detach a given loop device:

# sudo losetup -d /dev/loop

Filed Under: Linux

Some more articles you might also be interested in …

  1. nmap Command Examples in Linux
  2. Linux OS Service ‘anacron’
  3. cradle elastic: Manage the Elasticsearch instances for a Cradle instance
  4. Linux “seq” Command Examples
  5. gh issue: Manage GitHub issues from the command-line
  6. clang-format: Tool to auto-format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code
  7. “git extras” Command Examples
  8. setfacl: command not found
  9. ansible-vault – Encrypts & decrypts values, data structures and files within Ansible projects
  10. macof: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright