If you're sharing files between two users over the network or "sneaker net" (saving a file to a hard drive and copying it to a computer), you may encounter permission errors when you try to read or write the file. Even if you understand the concept of file permissions, you may not know exactly how to diagnose the problem or solve it. I used to perform data migration as a service, so I've run into my fair share of permission errors and ownership conflicts. Here's how I fix them fast.
1. Determine the correct user
Before you can fix a permission error, you must determine who requires permission. You might think you already know that, but what you may not realize is that the user name isn't the most definitive attribute of user identity. Your computer doesn't see you as a person so much as it sees you as a number. To learn your number, take a look at your user ID:
$ id --user 1005
2. Get the current owner
Next, determine the owner of the file you're unable to interact with. Because there's a file permission problem happening, you may need to use the
sudo command to see the information about the file:
$ sudo ls --numeric-uid-gid -rw------- 1 1000 100 23041 Aug 2 05:26 bar -rw------- 1 1000 100 54281 Aug 2 04:58 baz -rw------- 1 1000 100 822 Aug 2 08:19 foo
In this example, the user owning the files is identified as user ID 1000, and that's why user ID 1005 can't interact with them. Worse yet, the files are marked as readable and writable only by the user that owns them, so not even members of the same group can interact with the files.
3. Change permissions to match
You know the user requiring permission, so you can change the current owner to match your current user:
$ sudo chown 1005 foo
You can also grant members of your group, and possibly other users on the system, access to the files by changing the file mode. For instance, to maintain read and write permissions (7) while granting read permissions (4) to the group and any other user:
$ sudo chmod 744 foo
File permissions can seem tricky when you're not comfortable with them. For more information on how file ownership works, read Introduction to chown. For more information on how file permissions work, read Introduction to chmod.