How is file owner and group remembered for an external drive

filesfilesystemspermissionsusers

I formatted an external hard drive with an ext4 partition and subsequently mounted it. So that I could use the new partition, I did: sudo chown me:me /mount/directory to change the file owner and group from root to me, which worked fine.

This change is persistant, so I wonder where this information is stored. If I detach the hard drive then mount it to a different directory, I'm still set as the file owner and group, so the information can't be stored in the mount directory's inode.

So does the whole partition have its own inode where this sort of information is stored, and if so, is there a way to view it? Or is there perhaps another place where the information gets stored?

Best Answer

There's no difference between an external drive and an internal drive in terms of the filesystem stored on it. The owner & group of the filesystem's root directory are stored in its root directory, the same way your root filesystem's owner & group are stored.

A corollary of this is that because UIDs and GIDs are stored only numerically, if you mount an external drive on a system with different users in /etc/passwd, you'll see that the owner & group have changed to whatever that UID & GID map to on the other system. (e.g. if on your system user me is UID 1000, and you mount the drive on a system where UID 1000 is rms, you'll see the directory owned by rms.)

On the other hand, if you use a filesystem that doesn't store UID/GID information (like FAT), then the UID/GID of every file on the filesystem is taken from the parameters you gave to the mount command (either directly or through /etc/fstab).

A second corollary is that it doesn't matter what owner or permissions /mount/directory had on your root filesystem. Once a filesystem is mounted there, that filesystem's permissions are the ones that matter. That's why I like to chmod a-rwx /mount/directory before mounting the filesystem. It prevents me from accidentally writing to /mount/directory when the filesystem is not mounted there.

Related Question