Chown command is one of the most important commands in Linux that we should absolutely know about. Those who have some experience with Linux System administration must know that a change in permission or ownership can break your application or even your system. That’s were ‘Chown command’ in Linux comes to the rescue.
Chown command is used to change/modify the ownership of files and folders in Linux. As we know, in Linux everything is in file and folders only. And all these files and folders have an owner associated with them. So in order to modify the ownership of these files and folders, we use ‘chown command’.
Recommended Read: Useful Linux Commands that you should know
Also Read: IP Route command: Create static routes or Change the default Gateway in Linux
Let’s discuss some examples of chown command to understand it uses better. Here i am discussing some of the most commonly used examples of chown.
Chown Command examples
First of all, we should know how we can check the ownership and group of a file or a folder. We can check it with the ‘ls command’. Here is an example for the same,
$ ls -ltr /home/linuxtechlab/test.txt
-rw-r--r-- 1 linuxtechlab admin 0 2020-04-03 09:23 test.txt
Here the 3rd field i.e. ‘linuxtechlab’ is the owner of the file & ‘admin’ is the group name that owns the file. Now we can check, how we can manipulate these values with shown,
1- Change ownership of a file
To only change the owner of the file ‘test.txt’ to another user named ‘dan’, we can use the following command,
# chown dan test.txt
2- Change group of a file
With the ownership of the file changed, we might have to update the group ownership for the file as well. The command for changing the same is,
# chown :su-users test.txt
Here, su-users is the new group name.
3- Change both owner & group of file
Rather than executing both commands individually for changing user & group of a file, we can execute a single command to update both in one go,
# chown dan:su-users test.txt
4- Changing the owner & group of all files & folder inside a folder
Till now we have only discussed how to change the user & group ownership for a single file. What if we need to change the same for hundreds of files and folder, for that we can use the option ‘R’ ( recursively ). Example for that is,
# chown -R dan:su-users /home/linuxtechlab/
So this will change the ownership for all the files and folders inside ‘/home/linuxtechlab’.
5- List all changes in ownership as output
Above mentioned command changes the ownership for all the files & folders but we can’t actually see what files and folders are updated, so if need to see that output on our screen we would be required to use the option ‘v’ ( verbose ),
# chown -Rv dan:su-users /home/linuxtechlab/
6- Change ownership only when they are owned by a particular user or group
There might arise a situation where a folder might have files & folders with ownership of multiple users and groups but we might be required to change ownership for files belonging to single user. For such a scenario, we can use the ‘--from’ option.
Reference example for using ‘--from’ is mentioned below,
# chown -Rv - -from=susan dan /home/linuxtechlab
So here in folder ‘/home/linuxtechlab’ all the files with ‘susan’ as an owner will be updated to dan. Similarly to update the group in similar scenario, command is
# chown -Rv - -from=:old-group :su-users /home/linuxtechlab
7- Taking permissions reference from another file
So with chown, we can also take the permissions from one file & apply it to another file i.e. permissions for one file will be referenced for another file. Option for that is ‘--reference’, following is an example for the same,
# chown - -reference=test123.txt test.txt
here the owner & group for file ‘test123.txt’ will be applied to ‘test.txt’ file as well.
8- Taking help
Here we only discussed some of the most commonly used options that are used with chown command, but there are other options as well that can be used. To get the list of all the options, we can use the following command,
# chown - -help
With this we end this tutorial, please do send in any questions or queries using the comment box below.