← 返回命令列表

Linux command

losetup 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

List

losetup -a

Attach

sudo losetup /dev/loop0 /path/to/file

Example

sudo losetup --show -P -f /path/to/file

Example

sudo losetup -r /dev/loop0 /path/to/file

Detach

sudo losetup -D

Detach

sudo losetup -d /dev/loop0

说明

losetup sets up and controls loop devices, which allow regular files to be accessed as block devices. This is commonly used for mounting disk images, ISO files, or encrypted containers.

参数

-a, --all
Show status of all attached loop devices.
-f, --find
Find the first unused loop device. With no other arguments, print its name; with a backing file, attach the file to it.
-P, --partscan
Force the kernel to scan the partition table on the newly created loop device.
-r, --read-only
Set up a read-only loop device.
-d, --detach _loopdev_
Detach the specified loop device.
-D, --detach-all
Detach all currently attached loop devices.
-j, --associated _file_
Show loop devices associated with _file_.
--show
Print the assigned device name (useful together with -f).
-o, --offset _bytes_
Start the loop device at _bytes_ offset into the backing file.
--sizelimit _bytes_
Limit the loop device to _bytes_ starting at offset.
-b, --sector-size _bytes_
Set the logical sector size (512, 1024, 2048, or 4096).
--direct-io=on|off
Enable or disable kernel direct I/O on the backing file.
-c, --set-capacity _loopdev_
Force the kernel to reread the size of the backing file.
-L, --nooverlap
Fail if any existing loop device already covers the same byte range of the backing file.
-O, --output _columns_
Specify which columns to print (use `--list --output list` to see available).
-l, --list
Use a listing format similar to `losetup -a` but with extra columns.
-J, --json
Output as JSON (use with --list).
--help
Display help information.

FAQ

What is the losetup command used for?

losetup sets up and controls loop devices, which allow regular files to be accessed as block devices. This is commonly used for mounting disk images, ISO files, or encrypted containers.

How do I run a basic losetup example?

Run `losetup -a` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -a, --all do in losetup?

Show status of all attached loop devices.