{
    "mode": "man",
    "parameter": "losetup",
    "section": "8",
    "url": "https://www.chedong.com/phpMan.php/man/losetup/8/json",
    "generated": "2026-06-02T21:25:57Z",
    "synopsis": "Get info:\nlosetup [loopdev]\nlosetup -l [-a]\nlosetup -j file [-o offset]\nDetach a loop device:\nlosetup -d loopdev ...\nDetach all associated loop devices:",
    "sections": {
        "NAME": {
            "content": "losetup - set up and control loop devices\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "Get info:\n\nlosetup [loopdev]\n\nlosetup -l [-a]\n\nlosetup -j file [-o offset]\n\nDetach a loop device:\n\nlosetup -d loopdev ...\n\nDetach all associated loop devices:\n",
            "subsections": [
                {
                    "name": "losetup -D",
                    "content": "Set up a loop device:\n\nlosetup [-o offset] [--sizelimit size] [--sector-size size] [-Pr] [--show] -f loopdev file\n\nResize a loop device:\n\nlosetup -c loopdev\n"
                }
            ]
        },
        "DESCRIPTION": {
            "content": "losetup is used to associate loop devices with regular files or block devices, to detach loop\ndevices, and to query the status of a loop device. If only the loopdev argument is given, the\nstatus of the corresponding loop device is shown. If no option is given, all loop devices are\nshown.\n\nNote that the old output format (i.e., losetup -a) with comma-delimited strings is deprecated\nin favour of the --list output format.\n\nIt’s possible to create more independent loop devices for the same backing file. This setup\nmay be dangerous, can cause data loss, corruption and overwrites. Use --nooverlap with --find\nduring setup to avoid this problem.\n\nThe loop device setup is not an atomic operation when used with --find, and losetup does not\nprotect this operation by any lock. The number of attempts is internally restricted to a\nmaximum of 16. It is recommended to use for example flock1 to avoid a collision in heavily\nparallel use cases.\n",
            "subsections": []
        },
        "OPTIONS": {
            "content": "The size and offset arguments may be followed by the multiplicative suffixes KiB (=1024), MiB\n(=1024*1024), and so on for GiB, TiB, PiB, EiB, ZiB and YiB (the \"iB\" is optional, e.g., \"K\"\nhas the same meaning as \"KiB\") or the suffixes KB (=1000), MB (=1000*1000), and so on for GB,\nTB, PB, EB, ZB and YB.\n",
            "subsections": [
                {
                    "name": "-a --all",
                    "content": "Show the status of all loop devices. Note that not all information is accessible for\nnon-root users. See also --list. The old output format (as printed without --list) is\ndeprecated.\n",
                    "flag": "-a",
                    "long": "--all"
                },
                {
                    "name": "-d --detach",
                    "content": "Detach the file or device associated with the specified loop device(s). Note that since\nLinux v3.7 kernel uses \"lazy device destruction\". The detach operation does not return\nEBUSY error anymore if device is actively used by system, but it is marked by autoclear\nflag and destroyed later.\n",
                    "flag": "-d",
                    "long": "--detach"
                },
                {
                    "name": "-D --detach-all",
                    "content": "Detach all associated loop devices.\n",
                    "flag": "-D",
                    "long": "--detach-all"
                },
                {
                    "name": "-f --find",
                    "content": "Find the first unused loop device. If a file argument is present, use the found device as\nloop device. Otherwise, just print its name.\n",
                    "flag": "-f",
                    "long": "--find"
                },
                {
                    "name": "--show",
                    "content": "Display the name of the assigned loop device if the -f option and a file argument are\npresent.\n",
                    "long": "--show"
                },
                {
                    "name": "-L --nooverlap",
                    "content": "Check for conflicts between loop devices to avoid situation when the same backing file is\nshared between more loop devices. If the file is already used by another device then\nre-use the device rather than a new one. The option makes sense only with --find.\n",
                    "flag": "-L",
                    "long": "--nooverlap"
                },
                {
                    "name": "-j --associated -o",
                    "content": "Show the status of all loop devices associated with the given file.\n",
                    "flag": "-o",
                    "long": "--associated"
                },
                {
                    "name": "-o --offset",
                    "content": "The data start is moved offset bytes into the specified file or device. The offset may be\nfollowed by the multiplicative suffixes; see above.\n\n--sizelimit size\nThe data end is set to no more than size bytes after the data start. The size may be\nfollowed by the multiplicative suffixes; see above.\n",
                    "flag": "-o",
                    "long": "--offset"
                },
                {
                    "name": "-b --sector-size",
                    "content": "Set the logical sector size of the loop device in bytes (since Linux 4.14). The option\nmay be used when create a new loop device as well as stand-alone command to modify sector\nsize of the already existing loop device.\n",
                    "flag": "-b",
                    "long": "--sector-size"
                },
                {
                    "name": "-c --set-capacity",
                    "content": "Force the loop driver to reread the size of the file associated with the specified loop\ndevice.\n",
                    "flag": "-c",
                    "long": "--set-capacity"
                },
                {
                    "name": "-P --partscan",
                    "content": "Force the kernel to scan the partition table on a newly created loop device. Note that\nthe partition table parsing depends on sector sizes. The default is sector size is 512\nbytes, otherwise you need to use the option --sector-size together with --partscan.\n",
                    "flag": "-P",
                    "long": "--partscan"
                },
                {
                    "name": "-r --read-only",
                    "content": "Set up a read-only loop device.\n\n--direct-io[=on|off]\nEnable or disable direct I/O for the backing file. The optional argument can be either on\nor off. If the argument is omitted, it defaults to off.\n",
                    "flag": "-r",
                    "long": "--read-only"
                },
                {
                    "name": "-v --verbose",
                    "content": "Verbose mode.\n",
                    "flag": "-v",
                    "long": "--verbose"
                },
                {
                    "name": "-l --list",
                    "content": "If a loop device or the -a option is specified, print the default columns for either the\nspecified loop device or all loop devices; the default is to print info about all\ndevices. See also --output, --noheadings, --raw, and --json.\n",
                    "flag": "-l",
                    "long": "--list"
                },
                {
                    "name": "-O --output",
                    "content": "Specify the columns that are to be printed for the --list output. Use --help to get a\nlist of all supported columns.\n",
                    "flag": "-O",
                    "long": "--output"
                },
                {
                    "name": "--output-all",
                    "content": "Output all available columns.\n",
                    "long": "--output-all"
                },
                {
                    "name": "-n --noheadings",
                    "content": "Don’t print headings for --list output format.\n",
                    "flag": "-n",
                    "long": "--noheadings"
                },
                {
                    "name": "--raw",
                    "content": "Use the raw --list output format.\n",
                    "long": "--raw"
                },
                {
                    "name": "-J --json",
                    "content": "Use JSON format for --list output.\n",
                    "flag": "-J",
                    "long": "--json"
                },
                {
                    "name": "-V --version",
                    "content": "Display version information and exit.\n",
                    "flag": "-V",
                    "long": "--version"
                },
                {
                    "name": "-h --help",
                    "content": "Display help text and exit.\n",
                    "flag": "-h",
                    "long": "--help"
                }
            ]
        },
        "ENCRYPTION": {
            "content": "Cryptoloop is no longer supported in favor of dm-crypt. For more details see cryptsetup(8).\n",
            "subsections": []
        },
        "EXIT STATUS": {
            "content": "losetup returns 0 on success, nonzero on failure. When losetup displays the status of a loop\ndevice, it returns 1 if the device is not configured and 2 if an error occurred which\nprevented determining the status of the device.\n",
            "subsections": []
        },
        "NOTES": {
            "content": "Since version 2.37 losetup uses LOOPCONFIGURE ioctl to setup a new loop device by one ioctl\ncall. The old versions use LOOPSETFD and LOOPSETSTATUS64 ioctls to do the same.\n",
            "subsections": []
        },
        "ENVIRONMENT": {
            "content": "LOOPDEVDEBUG=all\nenables debug output.\n",
            "subsections": []
        },
        "FILES": {
            "content": "/dev/loop[0..N]\nloop block devices\n\n/dev/loop-control\nloop control device\n",
            "subsections": []
        },
        "EXAMPLE": {
            "content": "The following commands can be used as an example of using the loop device.\n\n# dd if=/dev/zero of=~/file.img bs=1024k count=10\n# losetup --find --show ~/file.img\n/dev/loop0\n# mkfs -t ext2 /dev/loop0\n# mount /dev/loop0 /mnt\n...\n# umount /dev/loop0\n# losetup --detach /dev/loop0\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Karel Zak <kzak@redhat.com>, based on the original version from Theodore Ts’o\n<tytso@athena.mit.edu>.\n",
            "subsections": []
        },
        "REPORTING BUGS": {
            "content": "For bug reports, use the issue tracker at https://github.com/karelzak/util-linux/issues.\n",
            "subsections": []
        },
        "AVAILABILITY": {
            "content": "The losetup command is part of the util-linux package which can be downloaded from Linux\nKernel Archive <https://www.kernel.org/pub/linux/utils/util-linux/>.\n\n\n\nutil-linux 2.37.2                            2021-06-02                                   LOSETUP(8)",
            "subsections": []
        }
    },
    "summary": "losetup - set up and control loop devices",
    "flags": [
        {
            "flag": "-a",
            "long": "--all",
            "arg": null,
            "description": "Show the status of all loop devices. Note that not all information is accessible for non-root users. See also --list. The old output format (as printed without --list) is deprecated."
        },
        {
            "flag": "-d",
            "long": "--detach",
            "arg": null,
            "description": "Detach the file or device associated with the specified loop device(s). Note that since Linux v3.7 kernel uses \"lazy device destruction\". The detach operation does not return EBUSY error anymore if device is actively used by system, but it is marked by autoclear flag and destroyed later."
        },
        {
            "flag": "-D",
            "long": "--detach-all",
            "arg": null,
            "description": "Detach all associated loop devices."
        },
        {
            "flag": "-f",
            "long": "--find",
            "arg": null,
            "description": "Find the first unused loop device. If a file argument is present, use the found device as loop device. Otherwise, just print its name."
        },
        {
            "flag": "",
            "long": "--show",
            "arg": null,
            "description": "Display the name of the assigned loop device if the -f option and a file argument are present."
        },
        {
            "flag": "-L",
            "long": "--nooverlap",
            "arg": null,
            "description": "Check for conflicts between loop devices to avoid situation when the same backing file is shared between more loop devices. If the file is already used by another device then re-use the device rather than a new one. The option makes sense only with --find."
        },
        {
            "flag": "-o",
            "long": "--associated",
            "arg": null,
            "description": "Show the status of all loop devices associated with the given file."
        },
        {
            "flag": "-o",
            "long": "--offset",
            "arg": null,
            "description": "The data start is moved offset bytes into the specified file or device. The offset may be followed by the multiplicative suffixes; see above. --sizelimit size The data end is set to no more than size bytes after the data start. The size may be followed by the multiplicative suffixes; see above."
        },
        {
            "flag": "-b",
            "long": "--sector-size",
            "arg": null,
            "description": "Set the logical sector size of the loop device in bytes (since Linux 4.14). The option may be used when create a new loop device as well as stand-alone command to modify sector size of the already existing loop device."
        },
        {
            "flag": "-c",
            "long": "--set-capacity",
            "arg": null,
            "description": "Force the loop driver to reread the size of the file associated with the specified loop device."
        },
        {
            "flag": "-P",
            "long": "--partscan",
            "arg": null,
            "description": "Force the kernel to scan the partition table on a newly created loop device. Note that the partition table parsing depends on sector sizes. The default is sector size is 512 bytes, otherwise you need to use the option --sector-size together with --partscan."
        },
        {
            "flag": "-r",
            "long": "--read-only",
            "arg": null,
            "description": "Set up a read-only loop device. --direct-io[=on|off] Enable or disable direct I/O for the backing file. The optional argument can be either on or off. If the argument is omitted, it defaults to off."
        },
        {
            "flag": "-v",
            "long": "--verbose",
            "arg": null,
            "description": "Verbose mode."
        },
        {
            "flag": "-l",
            "long": "--list",
            "arg": null,
            "description": "If a loop device or the -a option is specified, print the default columns for either the specified loop device or all loop devices; the default is to print info about all devices. See also --output, --noheadings, --raw, and --json."
        },
        {
            "flag": "-O",
            "long": "--output",
            "arg": null,
            "description": "Specify the columns that are to be printed for the --list output. Use --help to get a list of all supported columns."
        },
        {
            "flag": "",
            "long": "--output-all",
            "arg": null,
            "description": "Output all available columns."
        },
        {
            "flag": "-n",
            "long": "--noheadings",
            "arg": null,
            "description": "Don’t print headings for --list output format."
        },
        {
            "flag": "",
            "long": "--raw",
            "arg": null,
            "description": "Use the raw --list output format."
        },
        {
            "flag": "-J",
            "long": "--json",
            "arg": null,
            "description": "Use JSON format for --list output."
        },
        {
            "flag": "-V",
            "long": "--version",
            "arg": null,
            "description": "Display version information and exit."
        },
        {
            "flag": "-h",
            "long": "--help",
            "arg": null,
            "description": "Display help text and exit."
        }
    ],
    "examples": [
        "The following commands can be used as an example of using the loop device.",
        "# dd if=/dev/zero of=~/file.img bs=1024k count=10",
        "# losetup --find --show ~/file.img",
        "/dev/loop0",
        "# mkfs -t ext2 /dev/loop0",
        "# mount /dev/loop0 /mnt",
        "...",
        "# umount /dev/loop0",
        "# losetup --detach /dev/loop0"
    ],
    "see_also": [],
    "tldr": {
        "source": "official",
        "description": "Set up and control loop devices.",
        "examples": [
            {
                "description": "List loop devices with detailed info",
                "command": "losetup {{-a|--all}}"
            },
            {
                "description": "Attach a file to a given loop device",
                "command": "sudo losetup {{/dev/loop}} /{{path/to/file}}"
            },
            {
                "description": "Attach a file to a new free loop device and scan the device for partitions",
                "command": "sudo losetup --show {{-P|--partscan}} {{-f|--find}} /{{path/to/file}}"
            },
            {
                "description": "Attach a file to a read-only loop device",
                "command": "sudo losetup {{-r|--read-only}} {{/dev/loop}} /{{path/to/file}}"
            },
            {
                "description": "Detach all loop devices",
                "command": "sudo losetup {{-D|--detach-all}}"
            },
            {
                "description": "Detach a given loop device",
                "command": "sudo losetup {{-d|--detach}} {{/dev/loop}}"
            }
        ]
    }
}