Let’s see how to change file ownership on linux system. In our example we have two files owned by cicnavi user (enter ls -l to show files with long listing).
To change the ownership of the files we can use the chown command. This command can only be run by the root user. Chown command can also change the group ownership as well. For example, let’s say that we want to change the ownership of demo1 to a root user and group, we would enter the command
chown root:root demo1
The first root determines the user, and the second determines the group. Note that we had to use sudo in order to get appropriate privileges.
So, the file is now owned by user root and group root. If we to change just the user owner, we can type just type the user name and then the file. For example, let’s change the user back to cicnavi.
chown cicnavi demo1
If we want to change only the group ownership, we can enter the command without the user, like this:
chown :cicnavi demo1
Note that for this we use : to indicate that there is no change for user (user section is blank).
To change the ownership of the file recursively throughout the directory tree, we can use the -R option:
chown -R user filename
We can also change just a group ownership using the command chgrp. For example, to change the group of our demo1 file back to root, we can enter:
chgrp root demo1
The chgrp command can be run by any user. This is actually how we would give access to files that we own to other users.
Let’s also mention umask command here. umask is what sets the default permissions on a file. Without a umask every file would be created with the permissions of 666 (read write). Also, every directory would be created with the permission set of 777 (read write execute). Our mask of 002 would set our file permissions to 664 and folder permissions to 775. If we want, we can change the umaks. For example, if we want to set it to 022, we can enter: