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