In this lesson we’re going to discuss file and directory ownership (chown command) within the Linux file system.
Now to effectively control who is allowed to do what with the files and directories in the file system you first have to consider who owns each file and directory.
So, let’s begin this lesson by discussing how ownership works.
How Ownership Works
Understand that any time a user creates a new file or a new directory in the Linux file system that users account is assigned to be that file or directories owner by default.
Now by default the owner of a directory in the Linux file system will automatically receive read, write and execute permissions to that directory.
Which basically allows them to do whatever they want to do in that directory as that directory’s owner.
Likewise, the owner of a file in the Linux file system will receive read and write permissions to that file by default.
How to check file and directory ownership
For example, suppose I log in to my Linux system and then I open up my libera office word processing application and I create a file named mydata.doc in my home directory.
Now because I created this file the owner of this file is me as it shows right here which is actually the helpdesk user.
Because the helpdesk user account created this file, the helpdesk user is automatically assigned ownership of mydata.doc.
In this screen what we did was, right click on mydata.doc file in the Graphical User Interface (GUI) of my Ubuntu system and then I selected properties and then I selected the permissions tab and here I can see who the files owner is.
Now be aware that there are actually two different owners for this file the first one is the name of the user who owns the file again that’s me helpdesk but in addition there’s also a group that owns the file as well.
And by default, it will be the primary group of whatever user created the file in the first place.
In this case my default group on this system is the helpdesk group because that’s the primary group that the heldpesk user belongs to.
Therefore, the owner of the file is the helpdesk user and the owning group of the file is the helpdesk group.
You can also view file and directory ownership from the command line using the
ls -l command.
$ ls -l mydata.doc -rw-r--r-- 1 helpdesk helpdesk 0 Mar 20 02:33 mydata.doc
In this example, I ran the command in my user’s home directory.
Notice that the third column in the output displays the name of the file or directory owner, in this case the file is owned by helpdesk.
The fourth column over here displays the name of the group that owns the file.
How to modify ownership of a file or directory
Now understand that file and directory ownership isn’t fixed even the ownership is automatically assigned whenever a file of directory is created.
You can modify it if you want to.
You can specify a different user or a different group or both as the owner of a particular file or directory in the file system.
But to do so you need to keep this in mind in order to change the user who owns a file you have to be logged in as root, only root is allowed to do this.
But to change the group that owns a file you have to be logged in as either root or as the user who currently owns the file.
Now there are a couple of different utilities that you can use to do this.
The first one is the
chown utility which stands for a Change Owner.
It can be used to change the user or the group that owns a particular file or directory and the syntax is shown here.
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown followed by the name of the user or the group that you want to change ownership to followed by the file or directory in the file system whose ownership you want to change.
How to Change the Owner of a File
For example, suppose I have a file named
mydata.txt, It’s located in the
/tmp directory and currently that file is owned by the root user.
$ ls -l /tmp/mydata.txt -rw-r--r-- 1 root root 0 Mar 20 03:57 /tmp/mydata.txt
Well I want to change the ownership of that file from root to the helpdesk user.
To do this I would enter
chown and then the name of the user that I want to change ownership to, in this case helpdesk and then the name of the file.
$ sudo chown helpdesk /tmp/mydata.txt $ ls -l /tmp/mydata.txt -rw-r--r-- 1 helpdesk root 0 Mar 20 03:57 /tmp/mydata.txt
If we do a
ls -l down here, we see that helpdesk is now the user that owns the
But notice that the name of the group that owns the file was not changed.
The group named root which is the primary group associated with the root user still owns this file and this was assigned when the file was originally created by the root user.
How to Change the Group of a File
Well suppose I want to change the group that owns the file to my primary group from the root user’s primary group.
Now my primary group is the helpdesk group.
Therefore I would enter
chown and then I would enter
.helpdesk and then the name of the file that I want to modify.
Now this dot(
.) right here is very important because essentially what it does is tell that
chown command that the entity that follows is not a username but is a group name.
$ sudo chown .helpdesk /tmp/mydata.txt $ ls -l /tmp/mydata.txt -rw-r--r-- 1 helpdesk helpdesk 0 Mar 20 03:57 /tmp/mydata.txt
When we run the command, we see that the name of the owning group has now changed from root to helpdesk.
How to Change the Owner and Group of a File
Now be aware that I could have actually accomplished both tasks with one single command.
You can change the owning user and group at the same time by simply specifying the name of the user first then the period(
.) and then the name of the group that you want to change ownership to and then the name of the file.
Does it all at once.
$ sudo chown helpdesk.helpdesk /tmp/mydata.txt $ ls -l /tmp/mydata.txt -rw-r--r-- 1 helpdesk helpdesk 0 Mar 20 03:57 /tmp/mydata.txt
Change the ownership of a Directory
To change the ownership of a directory, you can run the following command.
Here I am changing the ownership of a directory named data1.
$ sudo chown helpdesk.helpdesk data1/
Change the ownership of multiple File and Directory
You can change the ownership of multiple files or directories simultaneously.
Here’s an example.
I have two files named
file2.txt and two directories named
dir2 which I am going to change the ownership of.
$ sudo chown helpdesk.helpdesk file1.txt file2.txt dir1/ dir2/
Modify ownership of Files and Directories Recursively
And also, be aware that you can use the Uppercase
-R option with the
In the examples, that we’ve shown right here where we’re modifying just one single file at a time and that’s fine if you just have one single file to modify.
But if you have a whole bunch of files that you need to modify and you have may be even sub-directories containing files whose ownership you need to modify,
-R saves a ton of time.
$ sudo chown -R helpdesk.helpdesk data/ $ ls -l data/ -rw-r--r-- 1 helpdesk helpdesk 0 Mar 20 06:14 file1.txt -rw-r--r-- 1 helpdesk helpdesk 0 Mar 20 06:14 file2.txt -rw-r--r-- 1 helpdesk helpdesk 0 Mar 20 06:14 file3.txt
Basically, this tell
chown to change ownership recursively.
In which case it’ll burrow down through all of the sub-directories of whatever path you specify and apply the ownership change that you specify to all of those files all at once.
Change the ownership of a Symlink
To change the ownership of a Symlink, you must use the
-h option with the chown command.
$ sudo chown -h helpdesk.helpdesk file_sym.txt
-h option is not used, chown command will change the ownership of the files to which the Symlinks points, not the Symlinks themselves.
For example, if you try to change the owner and the group of the symlink named
file_sym.txt that points to
chown will change the ownership of the file or directory the Symlink points to.
Here I have a file named
file.txt which has a symlink named
file_sym.txt and current owner of
file.txt is root.
$ ls -l lrwxrwxrwx 1 root root 8 Mar 20 06:47 file_sym.txt -> file.txt -rw-r--r-- 1 root root 0 Mar 20 06:46 file.txt
Now lets run the chown command without
$ sudo chown helpdesk.helpdesk file_sym.txt
And as you can see
chown has changed the ownership of the source file instead of Symlink.
$ ls -l lrwxrwxrwx 1 helpdesk helpdesk 8 Mar 20 06:47 file_sym.txt -> file.txt -rw-r--r-- 1 helpdesk helpdesk 0 Mar 20 06:46 file.txt
Change the ownership Using a Reference File
You can change the ownership of another file by referring to an existing file.
To do this, you have to use the
--reference option with the chown command.
For example, I have a file named
file1.txt whose owner is a user named helpdesk and whose owning group is also helpdesk.
Now Referring to this file, I will change the ownership of the
file2.txt, whose current owner is the root user.
$ chown --reference=file1.txt file2.txt
You can visit at following websites to get more information on
I hope that now you have a good understanding of How ownership works and you have some ideas for how you can use chown command within your workflow.
If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.