• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. whiptail: command not found
  2. ltrace Command Examples in Linux
  3. rpcinfo: can’t contact rpcbind: : RPC: Authentication error; why = Client credential too weak
  4. locale Command Examples in Linux
  5. larasail Command Examples in Linux
  6. amass track – Track differences between enumerations of the same domain (Command Examples)
  7. lnav Command Examples in Linux
  8. flatpak Command Examples in Linux
  9. How To Generate An CentOS/RHEL 6 UEFI Bootable ISO Image
  10. pamac Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • aws ec2: CLI for AWS EC2 (Command Examples)
  • aws cur – Create, query, and delete AWS usage report definitions (Command Examples)
  • aws configure – Manage configuration for the AWS CLI (Command Examples)
  • aws cognito-idp: Manage Amazon Cognito user pool and its users and groups using the CLI

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright