13

I had an old laptop. When it got complaint I removed its hard drive and started using it as external hard disk. It has an 80 GB of memory allocation for the windows C drive partition. But now i don't need that partition anymore. But the problem is I am not able to access that drive to paste files into it. How can I make read and write permission to this drive using terminal?

Anandu M Das
  • 2,233

3 Answers3

23

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

sudo chown -R $USER:$USER /media/$USER/nameofdrive

This gives the user ownership over the drive to the current user 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.

To assign ownership permission to a different user and or group, just replace $USER:$USER with whatever username:groupname or username:username you wish to assign ownership to.

Martin
  • 103
mchid
  • 43,546
  • 8
  • 97
  • 150
5

1. This is an ok technique for files ON an external drive, but is 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:

Note: be patient: for large amounts of data this can take many minutes. I just performed the following command on a directory on a high-speed internal SSD containing 966 GB of data (as shown by du -sh) and 10.6 million files (as shown by find . | wc -l) and it took 4 minutes.

# format: 
# sudo chown -R username_of_new_owner:groupname_of_new_owner path/to/dir
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
1

The drives will be located in /media/username folder. Goto that directory using cd command. And then on terminal

sudo chmod 777 nameofdrive

777 assigns read,write and execute permissions to all users.

Anandu M Das
  • 2,233