blockdev

disk managementlinux
The blockdev command is one of the most frequently used commands in Linux/Unix-like operating systems. blockdev The blockdev command is used to call block device ioctls from the command line. It allows administrators to get or set various attributes of block devices such as hard drives, SSDs, or partitions.

Quick Reference

Command Name:

blockdev

Category:

disk management

Platform:

linux

Basic Usage:

blockdev [options] [arguments]

Common Use Cases

  • 1

    Device configuration

    Configure block device parameters

  • 2

    Performance tuning

    Optimize block device performance settings

  • 3

    System administration

    Manage block devices for optimal performance

  • 4

    Storage optimization

    Optimize storage device settings

Syntax

blockdev [options] [commands] device

Options

Option Description
--report Print a report for specified (or all) devices
--getsz Get size in 512-byte sectors
--getsize Get size in 512-byte sectors (same as --getsz)
--getsize64 Get size in bytes
--getro Get read-only status (1 if read-only, 0 if read-write)
--setro Set read-only
--setrw Set read-write
--getbsz Get block size in bytes
--setbsz BYTES Set block size in bytes
--getss Get logical sector size in bytes
--getpbsz Get physical block (sector) size in bytes
--getioopt Get optimal I/O size in bytes
--getra Get readahead in 512-byte sectors
--setra SECTORS Set readahead
--flushbufs Flush buffers
--rereadpt Reread partition table
--getdiscardzeroes Get discard zeroes support status
-V, --version Display version information and exit
-h, --help Display help information and exit

Examples

How to Use These Examples

The examples below show common ways to use the blockdev command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Get the size of a block device in bytes
blockdev --getsize64 /dev/sda
Get the size of a block device in 512-byte sectors
blockdev --getsize /dev/sda
Get the read-only status of a device
blockdev --getro /dev/sda
Set a device to read-only mode
blockdev --setro /dev/sda
Set a device to read-write mode
blockdev --setrw /dev/sda
Get the current I/O block size
blockdev --getbsz /dev/sda

Advanced Examples:

Reread the partition table without rebooting
blockdev --rereadpt /dev/sda
Flush device buffers
blockdev --flushbufs /dev/sda
Get physical sector size
blockdev --getpbsz /dev/sda
Get optimal I/O size
blockdev --getioopt /dev/sda
Batch mode: Apply the same command to multiple devices
blockdev --setra 4096 /dev/sda /dev/sdb /dev/sdc
Check if a device supports discard operations (TRIM/UNMAP)
blockdev --getdiscardzeroes /dev/sda
Set readahead value to optimize performance
blockdev --setra 8192 /dev/sda
Script to set readahead for all disk devices
for disk in $(lsblk -d -o NAME | tail -n +2); do
blockdev --setra 4096 /dev/$disk done
Display block device attributes in a script
echo "Device: /dev/sda"
echo "Size (bytes): $(blockdev --getsize64 /dev/sda)" echo "Size (sectors): $(blockdev --getsize /dev/sda)" echo "Read-only: $(blockdev --getro /dev/sda)" echo "Block size: $(blockdev --getbsz /dev/sda)"

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Features:

The blockdev command provides direct access to block device attributes:

  • Query information about block devices (size, sector size, read-only status)
  • Modify device settings (readahead, read-only/read-write status)
  • Perform maintenance operations (flush buffers, reread partition tables)
  • Works with any block device (hard drives, SSDs, partitions, logical volumes)
  • Low-level access to device attributes without mounting the filesystem

Use Cases:

  • Performance tuning: Adjust readahead settings for optimized I/O
  • System administration: Set devices to read-only mode for diagnostic purposes
  • Scripting: Gather device information for automation and monitoring
  • Maintenance: Force the kernel to reread partition tables after changes
  • Forensics: Ensure devices are mounted read-only to preserve evidence

Readahead Optimization:

One common use of blockdev is adjusting the readahead value:

  • Readahead is the amount of data the kernel reads ahead during sequential reads
  • Higher values can improve sequential read performance but consume more memory
  • Typical values range from 256 (128KB) to 8192 (4MB)
  • SSDs often benefit from lower readahead values than HDDs
  • Database servers may benefit from custom readahead settings for their workloads

Security Considerations:

  • blockdev requires root privileges for most operations
  • Setting a device to read-only can prevent accidental modifications
  • Improper use can impact system stability and data availability
  • Always be cautious when using blockdev on production systems

Compatibility Notes:

  • Available on most Linux systems as part of the util-linux package
  • Some operations may not be supported by all device types or drivers
  • Use with caution on virtual devices or network block devices
  • Always test on non-production systems before making changes

Alternatives and Related Commands:

  • hdparm: More comprehensive tool for disk parameters, especially for ATA/SATA drives
  • sdparm: Similar functionality for SCSI devices
  • lsblk: List information about block devices in a more user-friendly format
  • sysfs: Access similar information through /sys/block/ filesystem

Common Use Cases

Device configuration

Configure block device parameters

Performance tuning

Optimize block device performance settings

System administration

Manage block devices for optimal performance

Storage optimization

Optimize storage device settings

Device management

Configure and manage storage devices

Related Commands

These commands are frequently used alongside blockdev or serve similar purposes:

Use Cases

1

Device configuration

Configure block device parameters

2

Performance tuning

Optimize block device performance settings

3

System administration

Manage block devices for optimal performance

4

Storage optimization

Optimize storage device settings

5

Device management

Configure and manage storage devices

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the blockdev command works in different scenarios.

$ blockdev
View All Commands