Under a Linux file system, we have files, folders, and directories. A folder only contains files and a directory contains both folders and files. Each of these files, folders, and directories are associated with an owner, a group, and permission access rights.
This article guide will walk us through determining and changing the ownership of a directory on a Linux system.
Why Change Directory Ownership in Linux?
- You might need an already created directory to only be accessible by a specific user.
- In an organizational setting, when a user quits, changing the directory ownership to folders and data prevents/revokes the initial access privileges the user had.
- When working with scripts like Python or Bash scripts, their execution might need access to directory data and therefore a directory owner must be identified for these scripts to execute successfully.
- During file transfers between different Linux systems on a network, directory ownership needs to be changed with respect to the users involved.
Problem Statement
For this article, we need a preexisting directory with already set directory ownership. Consider the following directory whose ownership is revealed via the following commands.
$ ls -l LinuxShellTips_Files $ ls -l -d LinuxShellTips_Files
The first column reveals the associated permission access rights, the second column reveals the associated owner, and the third column shows the associated group.
In the above case, we have owner dnyce and group dnyce for LinuxShellTips_Files directory, its sub-directories and files.
Changing Directory Ownership in Linux
To meet this objective, we are going to borrow the effectiveness of the Linux chown command-line utility, which is part of the GNU Coreutils package and is primarily used to change the owner and group name of a targeted file.
The chown command reference syntax is as follows:
$ chown [OPTION]... [OWNER][:[GROUP]] FILE...
As per the above chown command syntax, we first need to identify the new owner and group name that will inherit the to-be vacated directory ownership.
To list all the active owners in your Linux system run the command:
$ getent passwd
To list all the active groups in your Linux system, run the command:
$ getent group
Alternatively, you could create a new user you wish to have the new ownership of the vacated directory.
$ sudo adduser tutor $ sudo adduser tutor sudo
Changing Directory Ownership
To change the ownership of our sample directory from owner dnyce to owner tutor, we will implement the following chown command.
$ sudo chown tutor LinuxShellTips_Files
Confirm that the directory ownership has changed:
$ ls -l -d LinuxShellTips_Files
To change both the user and group ownership of the directory to tutor, execute the command:
$ sudo chown tutor:tutor LinuxShellTips_Files
Changing Directory and Sub-directories Ownership
The sub-directories in the above parent directory have different ownership and to change their ownership to be the same as the parent directory, run:
$ sudo chown -R tutor:tutor LinuxShellTips_Files
Confirm if the ownership changes took place:
$ ls -l -d LinuxShellTips_Files $ ls -l LinuxShellTips_Files
We can now change the ownership of any directory on a Linux as a sudoer/root user.