How to Change Owner of a File in Linux


Gaming Laptop Series

Linux and Unix are multi-user operating systems that means different users can access the operating system at a time and can use system resources simultaneously.

So, different file and directories can have different owners, and also have different permissions about reading, writing, or execution of that file.

If a user A accesses a file which is owned by another user B, then certain file permissions will not be accessible.

In order to give access to the user A, the owner of the file needs to be changed from B to A.

I hope things are clear till now, right?

Before knowing how to change the owner, we first need to understand about the file permissions and ownership of a file.


How to Know the File Permissions

Type ls -l or ls -lah on your terminal. It will display the files/directories and permissions of those files/directories of the current directory. For better understanding view the terminal image below:

You can see the permissions are given in the red highlighted part for corresponding files and directories.

Or you can also check the permission for and individual file or directory by typing ls -l filename in your terminal.

Details of a File Owner and Permissions

Here is the detail of the information showing about filename in the terminal

-rw-r--r-- is permission for different users of the file (We will further discuss it in later part). In the next column, 1 is the number of hard links to the file.
lalit lalit is file owner and group respectively.
And the final column contains the name of the file.

Using this information you know which permissions you as a user have for a specific file.

If you do not have the desired permissions, then either you need to change the file permissions of the file or you need to change the owner of the file to yourself to be able to access it.


Levels of file ownership

Every file or directory in Linux or Unix have the following levels of ownership

1. User or Owner File Permission

–  A person who created the file is set as the owner of that file. The system’s root user can change these attributes of the file or directory.

The underlined part of -rw-r--r-- is the permission granted for an owner regarding a filename that means an owner has read, write permissions but not executing it. The first block is reserved for d (directory) in case of a directory otherwise it is left blank. And the next three blocks show the permission for a user.

2. Group Permissions

– A group or team can have multiple users. And it can be used to grant shared access to the files or directories for group members as its a convenient way to do so. The permission granted for a group will be applicable to all the group members.

The underlined part of -rw-r--r-- shows group permission that means the group has read permission only. The second three blocks after the user block resemble the permission for a group.

3. Other User Permissions –

– Any other user who has not created the particular file or not a member of any user group. By default permission to these users is permission for all.

The last three blocks are reserved for other user permission look at the underlined part in -rw-r--r-- it shows permission for other users that means other users have only read permission here.


Use Chown Command in Linux to Change Owner of a File

To change ownership of a file or directory we will use chown command it stands for “change owner”. The basic syntax of chown is given below

chown user_name name_of_files

you can use UID that is user-id instead of user_name.

for example-

$ chown lalit filename

the above command will change the ownership of filename to lalit.


Changing group ownership

You can use chgrp command to change the group ownership of a file or directory. Following is the basic syntax of chgrp command-

chgrp group_name name_of_files

you can use GID that is group id instead of group_name.

for example-

$ chgrp developers_group filename

the above command will change the group of the filename to developers_group.

Conclusion

chown command as the name suggests, is used to Change Owner of a file in Linux.

I hope the above guide was enough to understand how a file ownership is changed.

If you still have some doubt or face any problem executing this command effectively, leave a comment so that I can help you as soon as possible.

For more guides on Linux, browse different categories inside Explore Linux.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.