10+ losetup command examples in Linux [Cheat Sheet]


Reviewer: Deepak Prasad

The loop device is a block device that maps its data to the blocks of a regular file or another block device. Loop devices are mounted as file systems on Unix-like operating systems.

losetup is a handy command-line utility to set up and control loop devices in Linux. It also allows you to detach loop devices and display the information of used loop devices.


How to use losetup command

The syntax to use losetup command is as follows:

losetup [options] [loopdev] [file]

Common Options:

  • -a, --all: Show the status of all loop devices.
  • -d, --detach loopdev: Detach the specified loop device.
  • -D, --detach-all: Detach all associated loop devices.
  • -f, --find: Find the first unused loop device.
  • -j file, --associated file: Show status of all loop devices associated with a given file.
  • -P, --partscan: Force kernel to scan partition table on the loop device.
  • -r, --read-only: Set up a read-only loop device.
  • -v, --verbose: Verbose mode.
  • -o offset, --offset offset: The data start is moved offset bytes into the specified file or device.
  • -s size, --sizelimit size: The data end is limited to size bytes after the data start.

Here is a full example of creating, setting up, and using a loop device:

# Create a 100MB disk image file
dd if=/dev/zero of=disk.img bs=1M count=100

# Set up a loop device
losetup /dev/loop0 disk.img

# Create an ext4 filesystem on the loop device
mkfs.ext4 /dev/loop0

# Mount the loop device
mount /dev/loop0 /mnt

# Use the filesystem (e.g., create a file)
echo "Hello, World!" > /mnt/hello.txt

# Unmount the filesystem
umount /mnt

# Detach the loop device
losetup -d /dev/loop0

Let's explore this with individual examples.


1. Create a loop device with losetup

You can use the losetup command to create a new loop device in Linux.

First, you need to create a file that will be used as a block device. The following command creates a file named loopfile which is 1GB in size.

$ sudo dd if=/dev/zero of=loopfile bs=100M count=10

Sample Output:

golinux@ubuntu-PC:~$ sudo dd if=/dev/zero of=loopfile bs=100M count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 1.40495 s, 746 MB/s

Next, run the losetup command specifying the loop device and file you want to map it to.

$ sudo losetup /dev/loop13 loopfile

If the loop device is already in use, you will get the following error.

losetup: loopfile: failed to set up loop device: Device or resource busy

You can use the -f option to avoid errors. The available loop device will be automatically used.

$ sudo losetup -f loopfile

To verify whether the loop device is created with a file, run this command.

$ losetup -j loopfile

Sample Output:

golinux@ubuntu-PC:~$ losetup -j loopfile
/dev/loop13: []: (/home/golinux/loopfile)


2. Display information about all loop devices

The losetup command can be executed with -a argument to list all loop devices.

$ losetup -a 

Sample Output:

losetup command to show status of all loop devices

Output columns:

  • NAME: loop device name
  • SIZELIMIT: size limit of the file in bytes
  • OFFSET: offset from the beginning
  • AUTOCLEAR: autoclear flag set
  • RO: read-only device
  • BACK-FILE: device backing file
  • DIO: access backing file with direct-io
  • LOG-SEC: logical sector size in bytes


3. Detach loop devices

The -d or --detach option is used to detach the file or device associated with the loop device. You can specify one or more loop devices.

$ losetup -d loopdev


$ losetup --detach loopdev

These are the available loop devices on my server:

server:~ # losetup -a
/dev/loop0: [64772]:46 (/Images/CentOS-8.2.2004-x86_64-dvd1.iso)
/dev/loop1: [64772]:38 (/Images/OneNDS-XX-IS-
/dev/loop2: [64773]:131302 (//opt/secure/loopback.img)

I will detach /dev/loop2 as the other 2 devices are basically image mounted on the system so we cannot detach them using losetup command:

server:~ # losetup -d /dev/loop2 

server:~ # losetup -a
/dev/loop0: [64772]:46 (/Images/CentOS-8.2.2004-x86_64-dvd1.iso)
/dev/loop1: [64772]:38 (/Images/Dummy-XX-IS-


4. Detach all used devices

If you want to detach all loop devices, you can run the losetup command with -D or --detach-all option.

$ losetup -D


$ losetup --detach-all


5. Find first unused loop device

The -f or --find option helps to find the first unused loop device.

$ losetup -f


$ losetup --find

Sample Output:

golinux@ubuntu-PC:~$ losetup -f


6. List all devices associated with a file

The -j or --associated option instructs losetup to display the list of all loop devices associated with a given file.

$ losetup -j file


$ losetup --associated file

Sample Output:

The following command prints the loop device associated with a file named blockfile in the current directory

golinux@ubuntu-PC:~$ losetup -j blockfile
/dev/loop14: []: (/home/golinux/blockfile)


7. Print the list in JSON format

The -J or --json option uses the JSON output format to display all devices.

$ losetup -J


$ losetup --json

Sample Output:

print the list of loop devices in json format


8. Set up a read-only loop device

To set up a read-only loop device, you have to use the losetup command with -r or --read-only option.

The following command creates a read-only loop device using the file named loopfile.

$ sudo losetup -r /dev/loop13 loopfile

Sample Output:

set up a read only loop device with losetup command


9. Create a loop device with the size limit

You can set the size limit in the loop device with --sizelimit option. The following command creates a new loop device with a size limit of 200M.

$ sudo losetup --sizelimit <num> loopdev loopfile

Sample Output:

create a loop device with size limit in linux


10. Set offset value in loop device

The -o or --offset option helps to create a loop device with a specified offset value.

$ sudo losetup -o <num> loopdev loopfile


$ sudo losetup --offset <num> loopdev loopfile

Sample Output:

create a loop device with offset value in linux


11. Create a partitioned loop device

The -P or --partscan can be used to create a partitioned loop device.

$ sudo losetup -Pf loopfile


$ sudo losetup --partscan -f loopfile



Now you know how to set up and manage loop devices in Linux. You have also learned to delete loop devices and get detailed information about used loop devices in the system.

We hope you find this article helpful. If you have any confusion, please let us know in the comment section.



man page for losetup


Views: 399
Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment