119

I have a USB which is write protected:

dmesg | tail

[10098.126089] sd 7:0:0:0: [sdb] Write Protect is on
[10098.126098] sd 7:0:0:0: [sdb] Mode Sense: 23 00 80 00
[10098.126779] sd 7:0:0:0: [sdb] No Caching mode page present
[10098.126788] sd 7:0:0:0: [sdb] Assuming drive cache: write through
[10098.131418] sd 7:0:0:0: [sdb] No Caching mode page present
[10098.131425] sd 7:0:0:0: [sdb] Assuming drive cache: write through
[10098.133335]  sdb: sdb1
[10098.135509] sd 7:0:0:0: [sdb] No Caching mode page present
[10098.135515] sd 7:0:0:0: [sdb] Assuming drive cache: write through
[10098.135521] sd 7:0:0:0: [sdb] Attached SCSI removable disk

How can I turn the write protection off?

What I've tried

  1. Checked if it has a hardware switch - no
  2. Tried to format it on windows and on Linux (via terminal too)
  3. Tried fdisk | chmod
  4. Tried to fix this with several tools from Ubuntu software center
  5. Used Google and have seen about 10,000 discussions about this problem but they were never solved

Additional information

fsck -n /dev/sdb1

fsck from util-linux 2.19.1
dosfsck 3.0.9, 31 Jan 2010, FAT32, LFN
There are differences between boot sector and its backup.
Differences: (offset:original/backup)
  65:01/00
  Not automatically fixing this.
Free cluster summary wrong (968250 vs. really 911911)
  Auto-correcting.
Leaving file system unchanged.
/dev/sdb1: 50 files, 93653/1005564 clusters

fdisk -l

   Device  boot.   Start        End      Blocks   Id  System
/dev/sdb1            32     8060927     4030448    b  W95 FAT32

umount /dev/sdb1

mkfs -t vfat /dev/sdb1

mkfs.vfat 3.0.9 (31 Jan 2010)
mkfs.vfat: unable to open /dev/sdb1
Zanna
  • 70,465
noob
  • 1,236
  • Backup the data and format the the drive if possible

    use diskutility

    – Tachyons Feb 17 '12 at 17:13
  • 8
    I can't format it because of write protection. – noob Feb 17 '12 at 17:21
  • 2
    @micha - what kind of USB device is it - obviously you've checked if it has a hardware switch? any errors if you attempt to mount it? sudo mount /dev/sdb1 -v ? – fossfreedom Feb 17 '12 at 18:00
  • Sure I've already checked if it has a hardware switch (was told me on google too). sudo mount /dev/sdb1 -v Don't give me a real error it just tells me that it is already mounted. – noob Feb 17 '12 at 18:48
  • What brand make / model is it? – balloons Feb 17 '12 at 22:14
  • I just know that it's from Verbatim. – noob Feb 18 '12 at 07:51
  • Greetings micha - Some usb drives are secured with some sort of encryption, *Verbatim* have a good few products like this http://www.verbatim-europe.co.uk/en_1/product_secure-data-usb-drive-4gb_16_0_16429__69.html this would prevent a user from formating, reading, writing to the flash disk without providing a password to get past the encryption. Also to note, most of these kinds of usb flash drives only work with Windows OS, as the encryption software is written specifically for Windows. When plugging the usb into a Windows machine does it install it's own software? – kingmilo Feb 18 '12 at 08:24
  • @kingmilo No it wasn't write protected from the beginning and I did nothing to protect it. – noob Feb 18 '12 at 08:30
  • 1
    micha, fair enough. Have you tried running efsck or used Ubuntu's Disk Utility to check the health status of the flash disk? If it was working fine & then stopped working all of a sudden there could be a hardware malfunction. – kingmilo Feb 18 '12 at 08:53
  • Is it a new USB drive recently purchased and never used? If so I would take it back to the store where purchased and get a replacement. If it is a drive you have had and used for some time and this has only just occurred you need to provide information on what you have recently done to get it to this state, there is too little information in your question to get an idea on what to do to solve your problem. – Mark Rooney Feb 18 '12 at 11:19
  • try in windows as most help to remove write protection from usb , i have googled , check the one which have some setting in registry – One Zero Feb 18 '12 at 11:46
  • @OneZero yes I know the windows regedit stuff about that but it don't work. – noob Feb 18 '12 at 16:29
  • @MarkRooney no it's an old usb. What information do you need to solve the problem? – noob Feb 18 '12 at 16:30
  • 2
    @kingmilo e2fsck : Bad magic number in super-block while trying to open /dev/sdb1 SuperBlock is not readable. – noob Feb 18 '12 at 16:42
  • @micha - that points to hardware failure unfortunately, but at least the mystery is solved ;) – kingmilo Feb 18 '12 at 17:20
  • @kingmilo note that I can access and copy all files which are on the usb right now without problem. – noob Feb 18 '12 at 17:25
  • check if it works > Low Level Format Tool: Write-Protected USB – One Zero Feb 18 '12 at 17:27
  • 1
    @micha - very well, still doesn't mean it's not faulty though unfortunately. It's common for storage devices to appear to work normally when in fact they are faulty, just at a different stage of faulty. I think with all the activity on this question you should ask for small donations to replace the drive, it would be easier – kingmilo Feb 18 '12 at 17:34
  • :S I've a question... People created the usb so why can't they repair it? – noob Feb 18 '12 at 17:43
  • They could but it would cost much more to repair it than it would to just copy everything to a new device and send you that. If you want to research eeprom's and rework stations, perhaps you could everntually replace the flash memory chip on the device, or solve what else might be wrong with it, bad capacitors etc. – l300lvl Feb 19 '12 at 22:46
  • Can you write to it when you run your file manager as root? – RobinJ Feb 23 '12 at 13:31
  • no (for your information the terminal commands where all executed with sudo) – noob Feb 23 '12 at 15:07
  • Does the Usb drive work on Windows ? – Vibhav Feb 24 '12 at 08:21
  • I created this same problem for myself when I did dd if=/dev/zero of=/dev/sdg . The accepted answer by Angel Genchev fixed it for me. – 6ft Dan Jul 10 '17 at 22:58
  • This worked for me https://superuser.com/questions/860404/cannot-reformat-bootable-usb-drive-on-windows – Adam Oct 21 '23 at 13:39

9 Answers9

106

To turn off disk device`s write protect, we use the low level system utility hdparm like this:

sudo apt-get install hdparm    
sudo hdparm -r0 /dev/sdb

where we asume that /dev/sdb is the Physical disk device we're working on. If the device has partitions that are mounted as read-only, you should re-mount 'em as read-write in order to write data to them.

Hope that helps.

Owl
  • 546
  • You are my hero. Where can I learn what happened after this magic magic magic command? – Marcos Modenesi Jul 01 '15 at 23:55
  • 4
    You can find the explanation by typing that command here > http://explainshell.com/ – sugab Jul 19 '15 at 05:31
  • 121
    it shows readonly = 0 (off) but still i'm not able to write or format. – shyammakwana.me Apr 11 '16 at 06:26
  • 2
    @OP I had the same as you, which is: sdxc card mounted read-only whatever the computer; hdparm -r shows "1 - on" then after unlocking with r0: "0 - off" but mount read-only anyway. But mounting my SanDisk sd card in another adapter and I couuld mount it rw. It turns out two of my microSD-to-SD adapters have a broken pin between two connectors. And these two will mount read-only whatever I do. Kuddos to @Angel_Genchev he answer has the most command in it. – tuk0z Aug 25 '16 at 16:02
  • I still see Write protection On on my drive. – Jaffer Wilson Nov 29 '16 at 09:55
  • @lliseil the micro-sd -> sdcard adapters will not function at all if any one of the 8 pins are broken.

    If either or both of pins 1 and 8 are missing/damaged, the possibility of using SPI mode still exists but requires hardware and driver support, which is not as common.

    – Hydranix Dec 06 '16 at 00:44
  • 3
    @lliseil It's more than likely the reader's write protect switch is being pressed upon by the card inserted into it, normally by the tab on the card but the switch (not the tab) can become bent and stuck on. you can fix the sdcard reader by cutting off the write-protect switch pins, the ones that make physical contact with the lock tab on the card. When the switch in the reader is open, write-protect is off, when it is closed (by being pressed upon the the tab on the card) write protect is on. Removing one or both of the switches metal arms will permanently disable write-protect on the reader. – Hydranix Dec 06 '16 at 00:49
  • @AngelGenchev Not working for me either. hdparm -r0 /dev/sdc says /dev/sdc: setting readonly to 0 (off) readonly = 0 (off) But when I try to mount /dev/sdc /media/usb/ it says the same error wrong fs type, bad option, bad superblock on /dev/sdc, ... – abhisek Jul 31 '18 at 06:36
  • I have the same problem too, micro-sd-card in sd-card-adapter in sd-card slot. Different sizes (GB) of cards, different vendors and models, different adaptors. All cards/adaptors work in my digicam and all of them work sometimes, if I join them via an USB-adapter. And it worked before, with Ubuntu-14.04 and the slot works fine - hate to have to say it - on Windows-7, which I only use, to tackle such issues. – user unknown Aug 03 '19 at 10:46
  • 1
    This does not help if the drive is out of write cycles and turns write protect on by itself. – xdevs23 Jun 17 '20 at 21:00
  • How can we check the write cycles? @xdevs23 – Ali Asgari Dec 23 '20 at 11:06
  • @AliAsgari It's rarely possible to check this on USB drives, you can check that if the drive that is connected has S.M.A.R.T. support, in which case you can use smartctl -a /dev/<device> and look for something similar to "Wear Leveling" or "Remaining Lifetime" where you will be able to find how much, usually percentage-wise, is left. – xdevs23 Dec 23 '20 at 16:50
  • This said that was changed to write mode but believe it or not still unable to write or format so i did the following. I have a Samsung Adapter with the LOCK switch. I removed the sd card from the adapter and put the adapter in lock mode, then inserted the card and changed the switch of the adapter to unlocked, then removed the sd card from the adapter, again put the adapter slowly in lock mode and repeat the action of put the card in the adapter and change from lock to unlocked, finally put in the computer and works ... miracle miracle ! – MadMad666 Oct 06 '21 at 14:16
45

After researching your question it appears that this is a not-too-uncommon problem with certain brands of USB flash drives (some older Samsung, a Kingston model) that would essentially just "crap out" for no known reason. People had tried opening them and jumping two leads (maybe from a flaky switch?) to no avail. If you still have this drive and it's still in warranty I'd return it and get a replacement.

I hate to break the bad news to you =\ but it appears you're out of luck in this situation as everything I've read points to hardware failure.

Edit 05/27/2016: I experienced an issue personally with a flash drive flaking out on me recently. In my case, this was a Corsair Flash Voyager 128GB that started slowing down pretty drastically on me. While it didn't show the symptoms noted here, it occasionally would not mount and showed up as a "Silicon Power" device. This was a result of the drive having accrued a large amount of bad sectors and dropping into diagnostic/programming mode. Since this is one of my more popular answers and this also falls into the category of "failing flash drives," I figured I'd include it here for reference.

Update 2: Regarding that Corsair Flash Voyager, I sent mine in for an RMA, only to have my second one fail on me in the same fashion. The problem actually turned out to be mechanical. The sliding mechanism seems to put a small amount of pressure on the PCB. Ordinarily, this wouldn't have caused an issue in the normal life span of the device. But for this particular model, it seems to have had weak solder joints that the pressure from the sliding action exacerbated -- leading to oxidation in the cracked joint and eventual failure. Rather than doing yet another RMA, I took matters into my own hands. I opened the case, shaved some of the plastic casing away to give the PCB some wiggle room and then reflowed the NAND chip to repair the broken solder joints. It's working great to this day!

Chuck R
  • 4,918
  • 6
    @admins:i wanna know whether this type of answers are allowed or not :) – Tachyons Feb 18 '12 at 15:57
  • 10
    @AboobackerMk If it is the answer, then it is the answer. :) – jrg Feb 18 '12 at 15:59
  • 9
    @Tachyons "whether this type of answers are allowed or not?" what is with people on stackexchange consistently trying to discredit other people's questions or answers? This answer is obviously a valid answer; for the reason jrg said. – GoProCameraByGoPro Apr 16 '15 at 03:05
  • @GoProCameraByGoPro that is 3 year old comment , my concern was not about quality of the answer . and jrg already replied to my question :-) – Tachyons Apr 16 '15 at 09:26
  • 1
    @Tachyons I'm doing my part to call the pretension here on stackexchange into question. If I had the data I would look at the trend of the demographic of people who do that and I could probably just ignore it, but for now I'll have to ask them questions as I see them. – GoProCameraByGoPro Apr 16 '15 at 17:26
  • This can be a hardware issue. It can be caused by the adapters used to connect the SD or mini SD card to your system. If you don't have a native SD card reader and are using some form of SDHC card reader attached to a USB port, the problem can be in this card reader. When I used this card reader: Bus 002 Device 024: ID 05e3:0723 Genesys Logic, Inc. GL827L SD/MMC/MS Flash Card Reader it mounts with Write Protect is on, but with this card reader: Bus 002 Device 023: ID 058f:6331 Alcor Micro Corp. SD/MMC/MS Card Reader it mounts with Write Protect is off. – oenpelli Feb 03 '20 at 22:43
  • I have a Corsair Flash Voyager 128GB that has developed this problem too. For some reason my other USB flash drives appeared to be affected on my system too. It's possible I was mistaken about that, though, or perhaps the Corsair put the system into a bad state. I would suggest rebooting and trying a known good USB flash drive if nothing is working. – dan-gph May 15 '20 at 08:30
30

None of these answers provided so far are correct.

To actually make it work, you can override the detection of the USB disk announcing its read-only with a USB quirks setting. Here's how it works.

  1. Plug in the USB device and do an lsusb, example:

    $ lsusb
    Bus 002 Device 012: ID 0781:5583 SanDisk Corp. 
    
  2. Take note of those two codes after ID and between the colon (they're called the idVendor and idProduct). Unplug the USB device.

  3. Remove the usb_storage kernel module (assuming it's compiled as a module)

    $ sudo modprobe -r $(lsmod | sed -n 's:,: :g ; s,^usb_storage[ 0-9]*,,p') usb_storage
    
  4. Now we will put the module back in using a quirks mode setting to override the detection of the device's write-only flag.

    From source/drivers/usb/storage/usb.c#L572 taken from v4.19 you can see that the quirks mode setting we're looking for is w. Here's how we'll reload the kernel module:

    $ sudo modprobe usb_storage quirks=0781:5583:w
    

    Replace the numbers between the colons with the ones your saw in step (1) from above.

  5. Plug the USB storage device back in. We can now confirm with dmesg that this worked:

    Before: broken write only

    And after: working read/write

    Also after issuing a mount command you'll see:

    /dev/sdb1 on /usb type ext4 (rw,relatime)
    

Now go get your stuff off that disk immediately, it's failing.

zx485
  • 2,426
  • Thank you so much for this answer. I had a problem with a Compact Flash card in a USB reader that appeared to be write-protected and this is the only solution I have found that fixes it. What is weird is that other CF cards are working fine. – njh Jan 18 '19 at 13:21
  • 1
    Thank you. This answer is the only one out there working on my failing usb drive. – Ajay Singh Dec 06 '19 at 20:01
  • 1
    I have this issue currently when using the Startup disk creator to create an Ubuntu 20 bootable USB stick. I verified it by unmounting it, repartitioning it in Gparted and formatting it FAT32, after which I can write to it. When I remake the pendrive, it's read only again.

    It remains read-only after using hdparm, remount, etc, there seems to be no way to work around it.

    – Vincent Gerris Mar 05 '20 at 09:16
  • 1
    It appears that my usb_storage is compiled as builtin, is there a way to specify this as a boot parameter or something or would I need to recompile my kernel? – Joe Baker May 30 '20 at 05:29
  • It's not failing, it's a brand new drive, and it works fine in other partitions other than mint iso images. It works before and after. But it doesn't work when an os image is catted to it. – Owl Jul 09 '22 at 17:47
  • This seems to works, but does not. I can write to the usb stick, run sync, and it seems to be OK. Then I unplug and re-plug, and the old data is still there. – Jori Mäntysalo Oct 24 '22 at 12:26
26

using fdisk -l locate the drive, ie: /dev/sdc1

now

umount /dev/sdc1

Finally, reformat the flash-drive

sudo mkfs -t vfat /dev/sdc1

I found this quick and easy. Be sure to UNMOUNT the drive before trying to format.

Ringtail
  • 16,127
  • 2
    sudo mkfs -t vfat /dev/sdc1 > most blog say this , but people say its not working , let see if it works . – One Zero Feb 18 '12 at 11:38
  • 10
    unmounted... but `mkfs.vfat 3.0.9 (31 Jan 2010)

    mkfs.vfat: unable to open /dev/sdb1`

    – noob Feb 18 '12 at 16:20
  • it worked for me, just had to use -I to make it one big partition and instead of /dev/sdc1, /dev/sdc instead. – mchid Feb 03 '15 at 14:21
  • 1
    I tried all other ways (hdparm -r0, blockdev --setrw, remount,rw) but nothing solved my problem except this! – Iman Mirzadeh Oct 15 '16 at 00:44
  • 5
    It says mkfs.vfat: unable to open /dev/sdc: Read-only file system – Shayan Jul 28 '17 at 04:48
  • mkfs.vfat: unable to synchronize /dev/sda1:Input/output error – shaderone Oct 13 '22 at 11:24
  • This answer is completely unrelated to OP's problem of having an invalid/incorrect write protection flag, and does not solve that problem. The only case where this answer will solve anything is if you can't modify any partition ebcase a partition is still mounted regularly. – Wolfram Aug 29 '23 at 11:33
25

Angel's answer is good, but the actual commands weren't so easy for me. This is what worked : Plug in the card (mine is an SD card with a manual write-protect switch on it, but the switch is off and it is writable on a Windows machine). Ubuntu mounted it automatically on /media/andrew/6AB0-1FD91, and dmesg showed the partition to be /dev/sdb1.

Unmount it, and make it writeable

sudo umount /dev/sdb1
sudo hdparm -r0 /dev/sdb

Create a new mount point and mount it there (my userID from /etc/passwd is 1000)

sudo mkdir /media/andrew/temp
sudo mount -o uid=1000 /dev/sdb1 /media/andrew/temp

it'll still complain that it's read-only. I don't know why I had to change this flag before AND after mounting, but that's the only way it worked for me. Set it to writeable again, and remount it at the same place

sudo hdparm -r0 /dev/sdb1
sudo mount -o remount,rw /dev/sdb1

Now I can write to the disk as my normal user. I'm being very careful with it in case it is actually failing, but those commands allowed me to finish what I was doing.

andrew lorien
  • 1,353
  • 12
  • 14
  • 1
    NOTE : my card was actually failing (Ubuntu mounts it read-only, but my camera happily writes to it). A couple of months later, it's corrupting files - so while this will work, be careful! – andrew lorien Jan 18 '17 at 06:21
  • I tried your stpes getting .... # sudo mount -o uid=1000 /dev/sdc1 /mnt/ Can only open '/dev/sdc1' as read-only The disk contains an unclean file system (0, 0). The file system wasn't safely closed on Windows. Fixing. – Ashish Karpe Jan 22 '18 at 06:57
  • 3
    that worked for me, and none of the other above... – Pierre Mar 12 '18 at 15:46
  • 1
    This worked after trying many solutions. Thanks @andrewlorien – Abk Aug 05 '19 at 14:35
  • 1
    This actually doesn't work. It just mirrors the device. Unplug and replug it and same issue persists. – Ashwani Shukla Feb 21 '20 at 16:45
  • @AshwaniShukla you're right, it's not a permenant solution. but i still haven't found one of those. – andrew lorien Feb 25 '20 at 04:01
  • After trying thisn solution it forcefully prents to work and no! On refsheing nautilus it goes back to what it was. – Justech Feb 11 '21 at 06:23
  • Are your instructions missing a umount, after mounting on media/amdrew/tmo? – Greenonline Feb 20 '21 at 13:31
4

This happens when the ISO file was burned to usb using the DD command or any of the "standard" tools.

If it's created like this, there's no way to make it writable.

The way to do it is to burn the ISO using some tool such as unetbootin (linux) or Roofus (Windows).

  • 1
    This seems to be the problem in my case. I burned an Ubuntu image into an SD card and nothing I tried would make it writable. I finally was able to write to it by removing it from the SD slot and using an SD-to-USB adapter to plug it into a USB port. – David Powell Mar 24 '22 at 14:38
  • Same problem with me, i was trying to make a chromeos image by slipstreaming it into a linux mint usb flash drive. – Owl Jul 09 '22 at 18:46
4

Well, this is a bit of a bummer.

I tried everything here & elsewhere & nothing worked.

Formatted it on a friend's Windows laptop-- works fine now. FFS!

Sigh....(wondering now if I might've been able to fix it in my Win 7 VM on my Mint desktop instead?).

  • May you show show your formatting procedure please? – Justech Feb 08 '21 at 06:53
  • That was four years ago, so not certain. I would likely have plugged the USB into the Windows machine, opened file explorer> right-click the USB> format. If it doesn't show in file explorer, it may in disk management, where you can also similarly format. Lastly, command prompt to quick format is:

    format E: /v:Flashname /fs:NTFS /q

    (your drive letter may not be E:, Flashname is whatever you want to name it, NTFS can be changed to the file system you want).

    – Tracy LF Feb 09 '21 at 13:16
  • 1
    The topic here is beyond your solution. – Justech Jan 24 '22 at 09:38
0

I tried the procedure from @kristopolous above but it didn't help me.

What did help me was using ddrescue (the package is called gddrescue) to make an image of the flash drive. Then I could mount the image read-write, do an fsck on the partition, unmount it, and use gparted to copy that partition to another flash drive.

-2

Insert memory stick and start gparted. Select it via the button at top right. It should be obvious if you are inspecting your memory stick (Size is a good clue). Select Partition--> unmount.

Select 'Device' at top, then 'Create Partition Table' and take the default, which is msdos.

Now you should be able to create a new partition and format it f32. If you can't, it's probably bust.

HTH

Vic
  • 418