If you are the owner of a file, you can change permissions, and even re-assign ownership. Needless to say, only the file owner (the user account) is allowed to do this, otherwise, the whole permission/ownership business would not make an awful lot of sense, would it? At least from a security perspective
That's all fine and dandy, unless you are root, or acquire root privileges. Root is a special user, which can do basically anything, even modifying file ownership and permissions for other users. More on root will follow.
Only owner (user), or root can do that. Otherwise one would be able to "steal" others' files, and the whole thing would make no sense whatsoever.
To modify a file's ownership, use the
chown command, like so:
chown <newuser>:<newgroup> <filename>
e.g if you wanted to assign the ownership of the file
some_text_file.txt to a user called
hrotgar and the group
heorot you would
chown hrotgar:heorot some_text_file.txt
(Needless to say, the file's contents would be the whole Beowulf story in Old English.)
You can also do this recursively. I.e. if you are changing ownership of a directory, the ownership of its contents will remain unchanged, unless you explicitly say otherwise. To achieve this (i.e. change ownership of a directory, and everything it contains), you would use the
-R parameter (case sensitive!).
So if you have a directory called
geatland, and want to change ownership of this directory and all of its contents to the user
hrotgar and the group
heorot, you would basically
chown -R hrotgar:heorot geatland
You do not need to specify whether it's a file or directory you are changing ownership for, remember Linux sees everything as a file.
Also, only the owner (user) or root can do this. Changing permissions can be done with the
There are two ways to do this, the easy way, and the also easy, only slightly more complicated way. the difference is, you'd either use the numerical representation of permissions or the letters themselves.
To change file permissions using the numerical permissions is very easy. First, decide what permissions you'd like the file to have. let's say you want to change permissions on the file
some_text_file.txt to 666 (meaning everybody can read and write, nobody can execute, not even the The Dark Lord 😈 (™)). To achieve this, you would use the command
chmod 666 some_text_file.txt
and you are done. The problem here might be that you'd have to calculate the effective permissions first, but with a little practice that becomes quite easy and straightforward. There is another way, in which you do not need to worry about all permissions, only the specific permission and permission group you want to alter:
Permissions are security features, so being too liberal about them can compromise your system's integrity. Always be mindful when assigning permissions to only give as many as are absolutely necessary.