In Linux, directories have owners and permissions that determine who can access and modify them. The owner of a file or directory has certain privileges, such as the ability to read, write, or execute the file or the ability to modify the permissions of the file or directory.
This post will teach you how to change the owner of a directory using the “chown” command in Linux.
- User, Group, and Other Users
- Permissions of a Directory
- Change the Owner of a Directory
- Change the Owner of a Directory
In the following sections, the above points are explained in detail.
What is a User, Group, and Other User in Linux?
Before moving forward, there are a few terminologies that you need to understand, and they include,
The User: is the one who uses the system.
A Group: is a collection of users, and all permissions applied to the group are simultaneously applied to all group members.
Other User: A user accessing your system from another system (virtually).
What are the Permissions of a Directory in Linux?
In Linux, three types of permissions can be applied to a File or Directory and include the following:
Read (r): Allows only to read without permitting to do the changes.
Write (w): Allows to read and write the file or directory.
Execute (e): Makes the permitted users execute the scripts.
How to Change the Owner of a Directory in Linux?
To change the ownership of a directory, the following syntax of the “chown” command is used.
$ chown -R <User-Name>:<Group-Name> Directory
When you run the below command, you are viewing the contents of the /etc/passwd file, which is a system file that stores information about all the users on the system.
The /etc/passwd file contains one line for each user on the system, with each line containing several fields separated by a colon (:). These fields contain information such as the user’s username, user ID (UID), group ID (GID), home directory, and default shell:
$ cat /etc/passwd
You can run the below command to retrieve the list of all groups on the system from the /etc/group file.
The /etc/group file is a system file that stores information about all the groups on the system. It contains one line for each group, containing several fields separated by a colon (:). These fields contain information such as the group name, group ID (GID), and a list of members belonging to the group:
$ getent group
Now, you want to enter the below command to print the detailed information for all the directories regarding file permission, user name, and group name.
$ ls -l
The above command also shows the directory’s current owner; in our case, “itslinux” is the owner, and to change it, use this command with the syntax discussed earlier.
$ chown -R testu:G1 new
How to Change the Owner of the Directory Recursively?
The above “$ ls -l” command shows that the owner is “itslinux” as underlined, which is the same as the group. Still, after we’ve executed the “chown” command, we can see that the ownership has changed.
You can also change the ownership of all files and directories inside a directory using this command.
$ sudo chown -R testu new1
As seen in the above image, we’ve only changed the owner but not the group, so you can use the above command without changing the group, which means the current group members can access the files/directories but with specified permissions by the owner.
Conclusion
In Linux, directories have owners and permissions that determine who can access and modify them. So if you want to change the owner of the directory, then you can do that by using the chown command. All the relevant details regarding this command have been discussed in this article.