permissions - How do I make a USB flash drive writable on Linux?
2014-04
I want to reformat a SanDisk Cruzer Edge USB flash drive but it appears read-only:
$ sudo dd if=/dev/zero of=/dev/sdg
dd: failed to open ‘/dev/sdg’: Read-only file system
I checked the readonly status with hdparm
:
$ sudo /usr/sbin/hdparm /dev/sdg
SG_IO: bad/missing sense data, sb[]: 70 00 05 00 00 00 00 14 00 00 00 00 20 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)
geometry = 1017/124/62, sectors = 7821312, start = 0
The readonly
flag displays off
but that I still cannot write to the drive. How do I solve this?
After running dosfsck
on the device I get this output:
$ sudo /usr/sbin/dosfsck /dev/sdg
fsck.fat 3.0.22 (2013-07-19)
Logical sector size (1766 bytes) is not a multiple of the physical sector size.
Try to repartition it with gparted or similar tool. I assume you only need one partition, so delete everything and create one with FS of your choice.
Oh, and by the way, didn't you mean /dev/sdg1 or smth. As far as I know there has to be a number at the end, not just sdg
ALSO: consider how long have you been using this flashdrive and how intensively have you been writing data to it. Flash memory has a total limit of write operations per cell (read here). When the limit is reached the drive should normally become read-only. I say normally, because there are some cases when the drive fails completely - this can happen if you are using cheap low-quality device.
When I mount an external usb drive on linux (CentOs4), the permissions are by default set to read-only. Since there are multiple users on the computer who need to use the external drive, I want everybody to have rw
permission for the entire drive. I also want them to be able to mount the drive if the computer has accidentially been shut down. They can use sudo mount
to mount the drive, but this will only give them read permission, and I obviously don't want to allow sudo chmod
.
Is there a default setting that I can change so that every new external usb disk automatically gets rw
permissions?
To enable everyone rw access, the key is umask=0 option to mount command.
sudo mount -o umask=0,uid=nobody,gid=nobody /dev/something /mnt/somewhere
umask=0 is enough, uid and gid just for sake of clarity, so you don't see more 'root' owners than necessarily.
@Tom's answer (writing /etc/fstab entry) will allow you to skip sudo
and if you write umask=0 as additional option there, you'll get best of both worlds:
Having this in /etc/fstab:
/dev/something /mnt/somewhere auto users,noatime,umask=0 0 0
allows you to just run
mount /dev/something
and everyone has access to all files.
Here's technical note, if you wish to know details:
As man mount
says, 'umask=0' will ensure that no additional rules apply to files access mode. For FAT filesystems (which are most widely used on USB disks), there's no access mode stored. But your current process has some umask value set, you can see it if you run just umask
in terminal. mount
uses that as default and removes access mode of your umask value from all files on mounted disk. Most widely used umask values are (octal) 022 - no group and other write, and 027 - no group write, no any other access.
Add an entry to /etc/fstab. Here is an entry that I added just a few hours ago for my Seagate USB drive:
UUID=4ACC734ECC733375 /media/Linux ext3 errors=remount-ro,defaults,users,noatime,nodiratime 0 0
The key here is the "users" entry that allows users to mount and unmount the drive.
Edit: this works for specific drives - I don't know if it can be enabled for all drives with one entry.