💾 Using chown to change ownership
September 28, 2019
File ownership is fundamental in Linux. Every file is associated with an owner and a group, which is something we can change using the
File permissions govern who can read, write to, and delete file on a computer.
In this article, we will see how to use the Linux 🐧
chown to control file permissions. 🤩
A basic familiarity with Linux and the command line would help, but is not required.
To understand how
chown works, you just have to follow the instructions below.
First, we need to open our Linux terminal :battery:, I’m using WSL (Windows Subsystem for Linux) with Ubuntu config you can use any terminal config of your choice. 😎
For now our directory is empty, let’s create something to work on with. You can use
mkdir to create a directory and
touch to create a file.
mkdir dir1touch dir1/file2.txt
Now, let’s take a look at the directory using the
Just a single directory and a file in it. Now let’s get started learning about
First, let’s create a file named
file1.txt in the current directory.
Now, create a user named
myuser1 and check the ownership of
ls -l, use
sudo for administrative privileges and enter your password.
sudo useradd myuser1
We can see that the current owner of the file is
majhi_rockzz and it belongs to the
majhi_rockzz group. (the owner and group name can be different in different machine eg.
Let’s change the ownership of
sudo chown myuser1 file1.txt
ls -l, you can see that the owner of
file1.txt has changed to
If we want to change the group of a file, we can do that also using
sudo chown :myuser1 file1.txt
We can also change both the owner and group of a file in single command as shown here:
sudo chown myuser1:myuser1 newfile1.txt
🐠 If we wish to recursively change ownership of a directory and its content, we can do it by passing the
-R(recursive) flag to
chown. Let’s do that now with:
sudo chown -R myuser1:myuser1 dir1
chown can also be used to copy the owner/group permissions from one file to another:
sudo chown file1.txt --reference=/tmp/rootfile1.txt
If you ls -l the current directory, you’ll see that
file1.txt now belongs to
Finally, if you wish to list the changes made by the
chown command in a verbose manner, we can do so using this command:
sudo chown -v user1:user1 file1.txt
In this article we learned about how the
chown command can be used to control the ownership of a file or files. TaTa 👋