How do I write files to a USB without being root?

10

7

I have a 4GB USB stick which I partitioned using sudo cfdisk /dev/sdb and formatted using sudo mkfs /dev/sdb1. It's currently set to ID 83 with System Linux when I run sudo fdisk -l on my Debian Squeeze.

I use pmount to mount the external USB drives. So doing a pmount /dev/sdb1 mounts it to /media/usb0 because I have the ff. line in /etc/fstab:

/dev/sdb1    /media/usb0    auto    rw,user,noauto    0    0

After issuing the pmount command, the USB is mounted to /media/usb0 -- doing an ls -l /media/usb0 gives me:

drwx------ 2 root root 16384 Mar   2 20:08 lost+found

So I go about my business and try to copy a file to /media/usb0 but I get the error below:

$ cp ~/foo.bar /media/usb0
cp: cannot create regular file `/media/usb0/foo.bar': Permission denied

But when I issue the same cp command with sudo, I am able to copy the file.

Why does this happen? I tried other USB drive I have and I am able to write to them without this error. Was there something wrong with the way I formatted or repartitioned the USB stick?

Eric

Posted 2012-03-02T12:29:10.617

Reputation: 205

Answers

7

You formatted the sdb1 partition using an ext* file system (either ext2, ext3, or ext4; I cannot recall the default). This is the default file system on Linux systems, and uses Unix file permissions to determine who can modify files or directories. Fresh filesystems always start off with the root (/media/usb0 in this case) being owned by root:root, and usually with permissions only allowing modification by the owner.

To see this for yoruself, mount the filesystem, then run ls -ld /media/usb0 to see the current ownership/perms. My guess is:

drwx------ 2 root root 16384 Mar   2 20:08 /media/usb0

A simple fix would be to a) chown the mounted file system to your own Linux account, or b) to give everyone write permission with chmod.

However, in the end, this wouldn't be a good solution, since the files created would become owned by your UID, not the username; this would quickly result in account mismatches when the USB drive is connected to a different system. Assuming you can connect it at all, that is – Windows does not support ext3 without extra trickery, so NTFS (mkfs.ntfs) or FAT32 (mkfs.vfat) would be a much better choice for the file system.

user1686

Posted 2012-03-02T12:29:10.617

Reputation: 283 655

I tried doing sudo mkfs.ntfs /dev/sdb1 and sudo mkfs.vfat /dev/sdb1 but I get the error sudo: mkfs.xxxx: command not found where xxxx is either ntfs and vfat. How do I get that to work on Debian Squeeze? – Eric – 2012-03-02T13:09:42.693

@Eric: Install ntfs-3g + ntfsprogs + dosfstools. – user1686 – 2012-03-02T13:18:12.077

Ok, I installed that. Now when I try to mount, I get this error: ntfs-3g-mount: failed to open /dev/fuse: Permission denied. What do I do about this? – Eric – 2012-03-02T14:11:51.960

4

This happens on later Debian systems (e.g. jessie) as well with certain fat32 or ext3,4 formatted USB flash drives. You need to remove (or comment out) the following line from your /etc/fstab:

/dev/sdb1    /media/usb0    auto    rw,user,noauto    0    0

After this you'll have your USB drive mounted automatically under your regular user account's permission and not root. See detailed info on Debian forums on this link.

After that your USB drive will mount smoothly with your user write permission :)

miklosq

Posted 2012-03-02T12:29:10.617

Reputation: 91

2

Use sudo and change permissions with chmod command.

See my answer in that question to see how to use chmod:

I suggest you to use chmod 777 to allow all (using ls -l it will show drwxrwxrwx).

kokbira

Posted 2012-03-02T12:29:10.617

Reputation: 4 883

1

If your filesystem is FAT16 or FAT32 you have to use this command:

sudo mount -t vfat /dev/sdb1 /media/usb0 -o uid=1000,gid=1000,utf8,dmask=027,fmask=137

For more info visit the link https://help.ubuntu.com/community/Mount/USB#Mount_the_Drive

kamy22

Posted 2012-03-02T12:29:10.617

Reputation: 11

0

Go to file system media via the GUI. Right click on the USB drive you want to copy to and select - Open as Root. Type in password. Then you can copy delete, drag and drop. All Good

Sven

Posted 2012-03-02T12:29:10.617

Reputation: 1

As alluded to above by @Sven, root:root in the ownership is the reason open as root at terminal or gui OR use chown to change either the owner or group to allow yourself to use that usb as expected. – linuxdev2013 – 2017-01-24T04:35:42.160