To understand the use of chown, let's follow these steps:
- To understand the use of the chown command, let's create a file named file1.txt and a user named user1:
The previous command for changing password information is optional. You can ignore it if you want to.
- Now, let's check the current owner of file1.txt.
We can see that the current owner for both the files is root and it belongs to the root group.
- Let's change the ownership of file1.txt to user1:
As seen here, the owner of file1.txt has now changed to user1.
- If we want to change the group of a file, we can do that also using chown:
- We can change both the owner and group of a file in single command as shown here:
We can see that the owner and group of file2.txt have changed.
- If we wish to recursively change ownership of a directory and its contents, we can do it as shown:
- Chown can also be used to copy the owner/group permissions from one file to another: