If you have an SD memory card, pendrive or any USB storage disk you want write protect, in this article we tell you step by step and in an easy way how to do it. And there is a bit of anti-writing protection that certain drivers allow, known as «write protection bit»Depending on whether it is active at 1 or 0, it will allow writing to the unit or not. This bit should not be confused with the WP of the CPUs' flag registers, which prevent overwriting by creating a forking process on UNIX-like systems, thus protecting the main memory from data writing. Nor to the typical protection tab that we find in some SD cards or storage media. In this case we are referring to a protection that is implemented in secondary storage media.
While the controller of the storage medium USB or SD memory card, or whatever means, detect that this bit is active, the driver will prevent the kernel from ordering a write order and therefore it will only be able to read the content. It is certainly very practical when we store data that we do not want to be modified, preventing writing or deletion by mistake. But if one of these protected media has fallen into our hands and we do not know of the existence of this bit, we could waste hours thinking that our unit is damaged, or raising and lowering the tab that some pendrives or SD cards have to protect them without any result. (Lock / Unlock) ...
Table of Contents
Mount read-only or write-only media:
Although it is not exactly what we are looking for with the Write Protection Bit, we can also find that a medium has been automatically or manually mounted for read-only, so we will not have the ability to write in it. Something that in some cases can be very annoying and prevent us from modifying or storing things. Well, the solution to this is very simple.
For example, if in the / etc / fstab file the device is configured to mount automatically with the option ro (read only) or if we have mounted it with the command:
sudo mount -o ro /dev/sda /mnt
In such a case we can only read the / dev / sda device in this case it has been mounted on the / mnt mount point. So that we can write to it again:
sudo mount -o remount,rw /dev/sda /mnt
In case this method doesn't work for you, then it will be because the bit we have talked about is acting, and that is why we will have to use the tool that we present below.
What is hdparm?
The command hdparm is a low-level tool which acts as an interface between various Linux kernel drivers and SATA / PATA / SAS storage media using the libata library, as well as for old IDE media. Remember that many USB storage media controllers, including card readers such as SD cards, also use this type of controller to operate, therefore it is also compatible with them.
Su basic syntax is
hdparm [options] [device]
And presents many options with which to work, although I do not recommend that you use them if you do not know what you do well, since being a low-level tool you could seriously damage your device, for example if we manipulate some critical options such as -B. But I will tell you that among its options there are some quite interesting such as:
- Obtain the setting of the disk:
sudo hdparm /dev/sdd
- Show the identification of the disk:
sudo hdparm -i /dev/sdd
- Check buffer and cache read times:
sudo hdparm -t /dev/sdd sudo hdparm -T /dev/sdd
Always remember to use the privileges, that is, run them as root user or putting sudo in front of them or they won't work ...
Remove and set the memory protection bit with hdparm:
Once the hdparm tool is known, we can use the -r option with which we can check the status of this bit with a simple command. To do this, we only need to know the physical name of our storage device. For example, if / dev / sdd were called, in that case we could use:
sudo hdparm -r /dev/sdd
And the value of said bit will be shown on the screen. If its value is 1 that means that the mode is active readonly or read-only, or in other words, the write-protect method is active and you won't be able to write anything to memory. To deactivate it or modify the bit, you just have to do the following:
sudo hdparm -r0 /dev/sdd
And now if we execute the first command and consume the state we will see that it has returned to 0, therefore the readonly mode is off or deactivated. In case you want to return it to the active state, use the option -r1 instead of -r0 and ready. For example:
sudo hdparm -r1 /dev/sdd
That simple is enable or disable this bit. It does not have too much mystery but for those who do not know this it can be a headache when it comes to unprotecting their storage media.
I hope it has helped you and now you know how to make a write-protected pendrive, don't forget leave your comments ...
20 comments, leave yours
Hola!
First of all, thanks for the notes! They are always very useful.
I am a Debian 9 user.
In this particular case, I have a problem that I cannot solve. I have an "infected" pen with a write protection that I CANNOT remove. I'm already thinking it's physical because I've honestly tried everything (down to low-level formatting in Windows or rescue disks, modifying partitions, etc.) and nothing.
I thought I was about to solve it, when following your steps, I found that the pen drive's write protection was "on", but once I changed it to "off" I still can't delete or modify anything on it.
Which may be? Is there any other way to solve this? (I don't mind losing the info I have inside)
Thank you in advance for your help.
Hernán
Good! I do not know if they told you the solution or in the end you solved it, but I am in the same case as you.
Can you advise me?
Thank you very much.
good explanation
Hello I have a USB that the system does not recognize, I send you the information if you can help me thanks
dmesg
[83384.348839] USB 1-1: New High-Speed USB Device Number 8 Using EHCI-PCI
[83384.506219] usb 1-1: New USB device found, idVendor = 0c76, idProduct = 0005, bcdDevice = 1.00
[83384.506225] usb 1-1: New USB device strings: Mfr = 1, Product = 2, SerialNumber = 0
[83384.506228] usb 1-1: Product: USB Mass Storage
[83384.506231] usb 1-1: Manufacturer: GENERIC
[83384.506848] usb-storage 1-1: 1.0: USB Mass Storage device detected
[83384.508235] scsi host5: usb-storage 1-1: 1.0
[83385.524951] scsi 5: 0: 0: 0: Direct-Access GENERIC USB Mass Storage 1.00 PQ: 0 ANSI: 2
[83385.556757] sd 5: 0: 0: 0: Attached scsi generic sg3 type 0
[83385.561706] sd 5: 0: 0: 0: [sdc] Attached SCSI removable disk
root @ localhost: ~ # fdisk -l
Disk / dev / sda: 698.7 GiB, 750156374016 bytes, 1465149168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical / physical): 512 bytes / 4096 bytes
I / O size (minimum / optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 995F9474-C5F1-4EE9-8FD7-13EA790423DC
Device Start End Sectors Size Type
/ dev / sda1 2048 1050623 1048576 512M EFI System
/ dev / sda2 1050624 49879039 48828416 23.3G Linux filesystem
/ dev / sda3 49879040 69410815 19531776 9.3G Linux filesystem
/ dev / sda4 69410816 76107775 6696960 3.2G Linux swap
/ dev / sda5 76107776 80013311 3905536 1.9G Linux filesystem
/ dev / sda6 80013312 1465147391 1385134080 660.5G Linux filesystem
root @ localhost: ~ # fdisk -l / dev / sdc
fdisk: cannot open / dev / sdc: No medium found
root @ localhost: ~ # hdparm / dev / sdc
/ dev / sdc:
SG_IO: bad / missing sense data, sb []: f0 00 02 00 00 00 00 0b 00 00 00 00 3a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
multcount = 0 (off)
readonly = 0 (off)
readahead = 256 (on)
root @ localhost: ~ # hdparm -C / dev / sdc
/ dev / sdc:
drive state is: standby
root @ localhost: ~ # hdparm -I / dev / sdc
/ dev / sdc:
SG_IO: bad / missing sense data, sb []: f0 00 02 00 00 00 00 0b 00 00 00 00 3a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ATA device, with non-removable media
Standards:
Likely used: 1
Configuration:
Logical max current
cylinders 0 0
heads 0 0
sectors / track 0 0
—
Logical / Physical Sector size: 512 bytes
device size with M = 1024 * 1024: 0 MBytes
device size with M = 1000 * 1000: 0 MBytes
cache / buffer size = unknown
capabilities:
IORDY not likely
Cannot perform double-word IO
R / W multiple sector transfer: not supported
DMA: not supported
IOP: pio0
thanks for your help, now I was able to know if my usb was against writing and I verified that it was not against writing so this is already a low-level formatting solution, I must add that the virus made the usb disconnect in a minute or two and not It allowed to act, it did not leave time so since I already knew with what name I assigned it, which was "sdc" in the terminal I put everything as root user.
# dd if = / dev / zero | pv | dd of = / dev / sdc bs = 1M here you wait a while
# dd if = / dev / zero | pv | dd of = / dev / sdc bs = 446 count = 1 you take out and put again the usb you give
# mkfs.vfat -F 32 -n "anaconda" / dev / sdc and you're done
Parabéns! A number of voltas that I dei with this subject is to give a coa explanation. Thankful Moi.
Great. Thank you.
Finally something that really worked for me, thanks.
Hello I have a 16 GB ADATA category 10 microsd memory
I did the procedure and it seems to work, but re-inserting it and rechecking the state puts it back to read-only.
I have tried to format it but it doesn't work. What I can see is my files.
any other tool or way to solve the problem ?.
easy and concise, perfect all the explanation and examples, it has been very useful.
Hello, I am a Kali Linux user in its latest version, I have already tried a billion techniques (sorry for being exaggerated) but none of them give me neither in Linux nor in Windows
in my case, debian, the removable memory must be unmounted….
superb, thanks
I have managed to recover several promotional extracts with their instructions
Hello, it works but the user's folder varies, it would be good to clarify it, but I have an error, in the command it shows me that I modify it, but when I want to put a folder or whatever on the pendrive it tells me that it is write-protected . If you can help me solve it, that would be great. Greetings
accepts the command and shows it as unprotected it is r = 0, but when I want to format it tells me protected. Thank you
Hello good day. With this I had not tried but in Windows I have done everything and nothing. My pendrive is write protected, even with sudo hdparm -r / dev / sdd it gives value 1, but with sudo hdparm -r0 / dev / sdd it could not remove the write protection either. Thanks and greetings from Venezuela.
My Linux version is openSUSE Tumbleweed 15.3
Past information with clarity and reliability. Levei hours testing a sem number of solutions and this, presented here, is the definitive one for the problem subject to reading and blocking of gravação.
Excellent contribution, everything turned out as you mentioned ... thanks
Thank you very much, very useful, this usually happens a lot in usb, the solution is easy and fast.
Thank you very much for the contribution.
In my case, Ubuntu 23.4 accepts all the commands but does not unlock the protection bit.
Any ideas?
lorenzo@lorenzo-B85M-D3H:~$ sudo hdparm -r /dev/sdc
[sudo] password for lawrence:
/ dev / sdc:
readonly = 1 (on)
lorenzo@lorenzo-B85M-D3H:~$ sudo hdparm -r0 /dev/sdc
/ dev / sdc:
setting readonly to 0 (off)
readonly = 1 (on)
lorenzo@lorenzo-B85M-D3H:~$
Thank you