Linux offers granular control over every aspect of the system, which is a key reason why most global services rely on Linux-based servers. This level of precision isn’t just limited to servers it’s also available to everyday users running Linux distributions. One powerful aspect of this control is the ability to manage user permissions, including restricting access to certain files and directories. If you didn’t know already, here’s how you can change the owner of a directory in Linux.
How to Check the Owner of a File or Directory in Linux
Before we dive into the command used to change directory ownership in Linux, it’s essential to first check who currently owns the directory. This helps you understand whether it belongs to you, another user, or someone in your group. To find out the ownership details of a file or directory, simply run the following command:
ls -l <File_name> or
ls -l <Directory_name>
This will display an output that shows who owns the file or directory. In the example above. Now that you know the current owner of the file or directory, you can move forward with transferring the ownership.
How to Change Directory Owner in Linux
Linux commands are often quite intuitive based on their names, and the chown
command is no exception. It stands for “Change Ownership” and, as the name implies, is used to change the ownership of files and directories. Here’s the basic syntax of the chown
command:
sudo chown -R $USER /directory
chown
is used for changing ownership.-R
stands for Recursive, allowing you to change the ownership of subdirectories as well.$USER
is your username, which indicates the new owner./directory
should be replaced with the path to the directory you want to change the ownership of.
Here’s an example of a valid chown
command:
sudo chown -R techymatty /example_directory
If /example_directory
is owned by the root (superuser), running the above command would transfer ownership to the user “abubakarmohammed.” Without using -R
, any internal, nested directories will remain under the previous owner.
To transfer ownership back to the superuser, you can use the following command:
sudo chown -R root: example_directory
Using -R
is essential here as it recursively applies the chown
command to all internal files. Forgetting to use it would result in the directory ownership being transferred, but its contents would remain accessible to other users and still be modifiable by them.
How to Change File Owner in Linux
As expected, chown
isn’t limited to directories and can also be used to change file ownership. The basic syntax for changing file ownership in Linux is just as straightforward.
sudo chown $USER file1 file2
For example, to change the ownership of files named 4.txt
and 5.txt
from the user “techymatty” to the superuser, you would use the following command:
sudo chown techymatty 4.txt 5.txt
Not all usernames are short, and typing them multiple times can be tedious. An alternative is to use user IDs instead of usernames. To do this, you first need to find the user ID of the person you want to transfer ownership to, and then replace the username with the user ID. To find a user’s ID, use the following command:
id -u $USER
Now that you have the user ID, you can use it in the chown
command like this:
sudo chown user_id file1
Following the above syntax, the command to transfer ownership of a file in Linux using the user ID would be:
sudo chown 1000 4.txt
And that’s how you can change the ownership of files and directories on Linux. While performing this, if you come across directories that are no longer needed, you can easily delete directories. Additionally, you can copy directory contents from one location to another. Linux offers a wide range of powerful commands that make it easy to navigate the file system. To help you get started, we’ve listed over 50 essential Linux commands.