How to change permission of a drive in an external hard disk?

If you want to gain ownership of a drive you can use the chown command:

sudo chown -R username:username /media/username/nameofdrive

This gives the user ownership over the drive without allowing permission to unauthorized users and -R makes this command recursive so that ownership also applies to all of the existing individual files on the drive as well and not just the drive itself.

You can also use -R to make chmod recursive as well.


1. Ok technique for files ON an external drive, but the correct technique for files you just copied onto your computer FROM an external drive

This works: recursively (-R) change the username (the part before the colon :) and groupname (the part after the colon :) to your username:

sudo chown -R username:username /media/username/nameofdrive

BUT, the problem with this is that it makes the external disk difficult to share between multiple usernames and/or on multiple computers, because each person on each system who needs access to these files must either have the same username, or must be part of the same groupname. Having the same username only works if it is the same person, so that doesn't always make sense. Having the same groupname is okay if it is consistently the same people using the drive, and they all add themselves to the same group with:

usermod -a -G groupname

BUT, if the groupname doesn't exist on one system, and just shows up as a number, such as 5000, when you look at it with ll (ls -alF), then you can't easily add yourself to the same group because usermod -a -G 5000 produces this error:

$ usermod -a -G 5000
usermod: group '5000' does not exist

2. Sometimes better for files ON an external drive

So, that leads us to this solution: just give full permissions to these files to everyone:

sudo chmod -R 777 /media/username/nameofdrive

BUT, that also marks ALL files as executable, which doesn't really make sense. You don't want to arbitrarily mark every single file as executable, as that can pose a security risk, and also be annoying when every time you double-click a text file to open and edit it, it asks you if you'd like to run/execute it.

3. Better still #1 (or best, if you DO need write permissions for everyone) for files ON an external drive

So, that leads me to my recommendation: give full permissions to everyone (-a, or 'a'll), but only set the executable bit (ie: use +X, NOT +x) if it is either a directory, OR already set for one or more of "user", "group", or "other", like this:

sudo chmod -R a+rwX /media/username/nameofdrive

man chmod says about the capital X versus lower-case x option:

...execute/search only if the file is a directory or already has execute permission for some user (X),...

4. Better still #2 (or best, if you do NOT need write permissions for everyone, ie: if read is enough) for files ON an external drive

BUT, even better still, if you can get away with it: if you don't really need guaranteed write permissions for everyone, just ensure you have at least read permissions for everyone on the drive, like this:

sudo chmod -R a+rX /media/username/nameofdrive

Then, apply permissions -R a+rwX only AFTER copying the files to your local machine from the external drive:

cp -r /media/username/nameofdrive/some_dir ~/some_local_path
# Now add `w`rite for everyone
sudo chmod -R a+rwX ~/some_local_path
# OR (even better!): just make yourself the owner now that it's on your local drive:
sudo chown -R username:username ~/some_local_path

References:

  1. https://superuser.com/questions/454795/how-can-i-do-a-recursive-chmod-only-on-directories/454807#454807
  2. [my repo/file: search this file for chmod to find a bunch of my personal chmod examples here] eRCaGuy_dotfiles/git & Linux cmds, help, tips & tricks - Gabriel.txt

Related

  1. [my answer] https://superuser.com/questions/1271882/convert-ntfs-partition-to-ext4-how-to-copy-the-data/1464264#1464264