• 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

How to disable write access to USB devices using “hdparm” tool

by admin

The hdparm Command

hdparm provides a command line interface to various kernel interfaces supported by the Linux SATA/PATA/SAS ‘libata’ subsystem and the older IDE driver subsystem. It should also work with most USB drives as well since they appear as SATA devices to the kernel.

The hdparm command is standard on most Linux distributions. You must have root access to use it. The “-r” option can be used to allow or deny write access to USB devices using hdparm. From the man page of “hdparm”:

-r [n]
Get or set the flag for read-only on the device. A value of 1 marks the device as read-only.

Enabling Read-Only access to USB devices

1. Install the “hdparm” package using yum.

# yum install hdparm

2. Use the “hdparm -r1” command option on the USB device to enable read-only access.

# hdparm -r1 /dev/sdX

Here,
/dev/sdX – is the USB device.

For example, of the USB device attached is /dev/sde, you would run the command:

# hdparm -r1 /dev/sde

3. To verify that you do not have the write access to the USB device, first mount it and then try to touch a file in the mount point. For example:

# mount /dev/sde1 /mnt
# touch /mnt/file
touch: file: Permission denied

You could also use “dd” command to confirm read-only access to USB device.

$ dd if=/dev/zero of=/dev/sde bs=1k count=10
dd: writing `/dev/sde': Operation not permitted
1+0 records in
0+0 records out
0 bytes (0 B) copied, 0.0005 seconds, 0 B/s

As you can see the “dd” command threw an error too while writing.

Re-enabling write access

To re-enable read-write access to same disk, use following command:

$ hdparm -r0 /dev/sde
How to Configure UDEV rule to allow read-only permissions on USB devices

Filed Under: Linux

Some more articles you might also be interested in …

  1. Setting up an NFS server with Turnkey Linux
  2. bindkey Command Examples (Add keybindings to Z-Shell)
  3. lsattr: command not found
  4. named Command Examples in Linux
  5. How to Boot into Rescue Mode or Emergency Mode Through Systemd in CentOS/RHEL 7 and 8
  6. Understanding DNS zone files
  7. How to convert a Volume to Stripe (RAID0) Volume in LVM
  8. lsusb Command Examples in Linux
  9. pacman –query Command Examples
  10. aws Command Examples

You May Also Like

Primary Sidebar

Recent Posts

  • “glab pipeline” Command Examples
  • “glab mr” Command Examples
  • “glab mr merge” Command Examples
  • “glab mr” Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright