File (directory) ownership is the foundations of every operating system. This post will teach you how to change file(s) ownership in Linux using the command chown.
There are different scenarios when you would need to change the ownership of a file using either chown or chgrp. Note: you can change group ownership with the command chown. I’ll show you how to do this later in this article.
There is another command to change permissions in Linux chmod. Note that only the current owner of the file and the root user can do this task.
Although neither of the commands are difficult to use, modifying the file ownership with chmod is beyond the scope of this post. It belongs to another article.
Change the file(s) or directory ownership to suit your needs by following roughly the instructions below.
Figure Out Who’s The Owner Of The File(s)
ls -l myfile
ls -l option uses a long listing format. With this option ls returns file ownership together with permissions. Here you would probably want to decide how to alter the file or directory ownership.
Using Chown To Change Ownership
The simplest usage of using the command and setting the ownership to the user myuser is:
chown myuser myfile
-rw-r--r-- 1 myuser root 28 june 25 2014 myfile
where the file myfile is owned now by myuser. If you want to change the group from root to mygroup, do it by slightly modifying the code:
chown myuser:mygroup myfile
In real life, we usually have to change ownership of an entire directory tree. You would do this by adding -R option which stands for recursive operation. With this option the command goes thru all files and directories within the directory in changes the ownership.
chown myuser:mygroup /home/myuser -R
Takes everything in myuser home directory back.