32

I want to format my USB stick to ext4 and just use as I would any other typical non-linux format drive (FAT32, exFAT, NTFS).

That is to say, I want to be able to plug the usb stick into any of my linux machines, and read/write to it without needing to adjust permissions, like doing chmod or chown stuff.

I would prefer to use GUI partition software like GParted, rather than command-line commands, though I welcome any solution!

I'm sure a post like this is duplicate flag heaven for some, but after browsing 6~10 SO and forum posts from google, I didn't find a simple solution to my question. Seemed like everything was about adjusting permissions on a per-user basis. Maybe you just cannot use ext4 drives with the same brainless convenience as NTFS.

Alnitak
  • 423

1 Answers1

43

Like any unix-style filesystem, ext4 includes standard Unix file ownership and permission conventions. That is, the user is identified by an UID number, and each user will belong to one or more groups, each group identified by its GID number. Each file has an owner UID and one group owner GID. The three classic Unix file permission sets are:

  • one set of permissions for the owner, identified by the owner's UID number
  • one set of permissions for the group owner, identified by the group's GID number
  • one set of permissions for everyone else

In order to be able to access the stick without needing to adjust permissions, you must make sure any files and directories created on the stick will have non-restrictive permissions automatically. The problem is, permissions on any new files created are controlled by the umask value... and you don't really want to keep changing it to 000 for creating files on the USB stick and back to the default value (usually 002 or 022) for normal use. A single mistake could lead you creating an important configuration file with wide-open permissions, that might compromise the security of your user account or cause other more minor problems.

If you can make sure that your normal user's UID number is the same across all your Linux systems, and you only care about access for that one user (plus root of course), you can get away with just formatting the USB stick to ext4, mounting it for the first time, and assigning the ownership of its root directory to your regular user account before you begin using the filesystem.

Assuming that /dev/sdX1 is the USB stick partition you wish to create the filesystem in, and <username> is your username, you can do this when setting up the USB stick for use:

sudo mkfs.ext4 /dev/sdX1
sudo mount /dev/sdX1 /mnt
sudo chown <username>: /mnt
sudo umount /mnt

But if you cannot guarantee matching UID/GID numbers, and/or there are multiple users who might want to use the USB stick, you'll need to do something a bit more complicated, but still an one-time operation after creating the ext4 filesystem on the stick.

We need to set a default ACL on the root directory of the USB stick filesystem that assigns full access to everyone on any new file or directory. And to ensure that the stick will be mounted with ACL support enabled, we need to use tune2fs to adjust the default mount options stored in the filesystem metadata.

sudo mkfs.ext4 /dev/sdX1
sudo tune2fs -o acl /dev/sdX1
sudo mount /dev/sdX1 /mnt
sudo chown <username>: /mnt
chmod 777 /mnt
setfacl -m d:u::rwx,d:g::rwx,d:o::rwx /mnt
sudo umount /mnt

Assuming that all your systems support ACLs on ext4 filesystems, and that any removable media mounting tool you might use won't choose to ignore the acl mount option, you now should have a USB stick on which all files created on it will have permissions -rw-rw-rw- and all created sub-directories will be drwxrwxrwx+. The plus sign indicates that the sub-directory will have an ACL: the custom default permission set configured for the stick's root directory will be inherited by the sub-directories too, and they will behave the same.

The owner UID/GID will still match the UID and primary GID of the user that created the file on the filesystem, but because of relaxed file and directory permissions, that should not be much of an issue.

The only problem I might expect is that copying files to the USB stick will by default attempt to duplicate the file permissions of the original, which you don't want in this case.

For example, if you create a file on System A with permissions -rw-r--r-- and copy it to the stick, then move the stick to System B with non-matching UID numbers. You can still read the file on System B, but you cannot overwrite it on the stick without first explicitly deleting or renaming the original file. But you can do that, as long as you have write access to the directory the file's in.

This can actually be an useful feature: if you modify the same file on multiple systems, this will push you towards saving a new version of the file each time instead of overwriting the One True File... and if the file is important, that might actually be a good thing.

telcoM
  • 96,466
  • 1
    The copying issue bites me a little. I use rsync to get around that, with rsync -rlt --no-p --chmod=ugo=rwX. This will use the destination's defaults, i.e., as specified by the ACL permissions. – sircolinton Jun 30 '19 at 15:20
  • Problem: I tried your second method and stuck in its first step. Mounted my USB drive (ext4) to /media/user/myUsbDriveName then mkfs.ext4 /dev/sdb1 said: it's mounted, wanna proceed anyway? Yes: cannot create file system because it's mounted. OK, so I unmounted it (which switched off my ext harddisk) and tried again. Result: /dev/sdb1 does not exist – gloschtla Jan 03 '20 at 09:18
  • @gloschtla You're probably trying to mount/unmount using a GUI tool or something like udisksctl, which will also prepare the device for immediate unplugging on unmount. For the mkfs.ext4 step, do it after plugging in the disk but before actually mounting it, as you should never mkfs a mounted disk. – telcoM Jan 03 '20 at 09:26
  • I managed to go through your second tutorial, after having used gParted to delete the existing ext4 partition (dev/sdb1). So, then I followed your tutorial using /dev/sdb – gloschtla Jan 03 '20 at 12:15
  • OK, now you have what is known as a "superfloppy" style USB with no partitions at all. It works with Linux, but other OSs may treat it as unformatted and offer an easy way to one-click-format it. – telcoM Jan 03 '20 at 12:53
  • That's the actual solution for having symlinks in a USB stick. If you need symlinks, it's supposed to be used in a Linux system with a Linux partition that supports symlinks freely, basically ext4 with Linux all its permissions. – R. W. Prado Oct 01 '21 at 00:01