All files and directories in Linux systems belong to someone. You can change their ownership with the command
chown . We show you how.
Each file belongs to a user and a group.
Linux is a multi-user system. The operating system allows you to define multiple user accounts and to log in a valid user on the computer. In addition, multiple users can simultaneously use a computer.
Linux uses the concept of ownership to log which files belong to which user and to increase security. Each file belongs to one owner ̵
When a file is created, its owner is the user who created it. The group to which the file belongs – the "owning" group – is the user's current group. Users and groups have names as well as numeric identities called user (or unique) ID (UID) and group ID (GID).
When you create a file, it belongs to you and belongs to your current group. Usually this is the group you signed up with. By default, this is a group that has the same name as your username and was created when you were created as a user in the system.
With the command
chown you can change the owner values to something else. You can specify a new owner, a new group or a new owner and a new group at the same time. The owner of a file can change group ownership, but only root can change user ownership because it affects another user. Without root privileges, you can not make another user on the system unintentionally "take over" a file.
Why should you change the ownership?
Here are some examples of situations in which you might want to do this:
- When transferring files between different Linux or Unix-like operating systems, you must transfer the user and group owners to the new user and group owners of the account change under which you want to use the files on the new Linux computer.
- A user can leave your organization and all his files are the responsibility of another employee. You must change the owner and the group owner to the employee who is now responsible for these files.
- You can create a script that is used by a specific user.
- You can create a file or a logged directory You must be logged in as root, but you want a specific user to have access to it.
Viewing Your Groups, UID, and GID
You can use the
groups command to list the groups in which you are located.
For a list of groups, their numeric IDs, and their UID and GID, use the command
id :  ID
You can use some options with ID to refine the output.
- -u : Enter your UID.
- -g : Enter your effective (current) GID.
- -nu : Enter your username.
- -ng : Enter your current group name.  id -u
Viewing the User and Group Ownership of a File  To view the owner of a file or directory, use the option
-l(long listing) with
We can see that name
daveappears twice in the collection. The leftmost appearance indicates that the file owner is a user named
dave. The rightmost
Davetells us that the file belongs to a group also known as
By default, they are added to a private user when a Linux user is created. Group named after their user name. You are the only member of this group.
This executable belongs to the user
maryand the group to which the file belongs is the private group of
ls - l
This file belongs to user
oscarbut the group to which the file belongs is called
researchlab. This means that other members of the
researchlabgroup can access this file according to the file permissions set for members of this group.
<img class = "alignnone size-full wp-image-438644" src = "https://www.howtogeek.com/wp-content/uploads/2019/08/x6-3.png.pagespeed.gp + jp + jw + pj + ws + js + rj + rp + rw + ri + cp + md.ic.PjqginMs8C.png "alt =" ls -l in a terminal window  Changing user ownership
Let us Here are a few examples: This command changes the user ownership of the while.c file to the user
sudo chown mary while.c
Use chownto change ownership of multiple files simultaneously.
sudo chown mary getval.c global.c goto.c
This changes the user ownership of all three files.
ls -l getval.c global.c goto.c
You can use wildcards to select file groups. This command changes the user ownership of all files beginning with the letter "c".
sudo chown mary c *. *
All of the files will now have
Maryas the owner. Note that none of the group owners have changed.
ls -l mary c *. *
Let's change the owner of a directory. We simply pass the directory name to
chowninstead of a filename.
sudo chown mary ./archive/[19659065<sudochownmary/archive/initeminalwindow" width="646" height="57"/>
To check the owner properties of the directory we used
lsbut also use the option
-d(directory). It lists the properties of the directory, not the files it contains.
ls -l -d ./archive/[19659068)ls-ld/archive/include_terminalwindow" width="646" height="122"/>
To change To own all files in a directory, you can use the option
-R(recursive) use. This option changes the user ownership of all files in the folder
sudo chown -R mary ./archive/[19659071<sudochown-rmary/archive/initeminalwindow" width="646" height="57"/>
Now let's look at the files in the archive directory.
ls -l ./archive/[19659074)ls-l/archive/inclusive_interface" width="646" height="292"/>
As expected, all of the files now belong to
Changing Group Ownership
There are several ways to change group ownership.
To change the group ownership concurrently with the user Pass the new owner name and the new group name with a colon ":". The group must already exist.
sudo chown mary: researchlab charm.c
The user owner and the group to which the file belongs have both been changed.
ls -l charm.c
A short way to change the group owner into the current owner's group, just type in the colon and omit the group name.
sudo chown mary: caps.c
ls -l caps.c
Both user and group ownership rights have been changed to
To change only the group ownership, you must precede a colon and omit the user name. The owner of the user is not changed.
sudo chown: researchlab at.c
ls -l at.c
Group ownership has been changed, but user ownership remains unchanged.
Using Chown with UID and GID Values
You can use the numeric UID and GID values with the command
chown. This command sets the user and the group ownership to
sudo chown 1001: 1001 at.c
 ls -l at.c
 Possession is nine-tenths of the law
Or so it is said. However, under Linux, ownership is an integral part of file security, with the rest covered by file permissions. Use the commands
chmodto secure file access to your system.