$_ CLI In Depth
Learning progress 0%
lesson permissions users sudo

Ownership: chown & chgrp

5 min read Module 5: Users, Permissions & sudo Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:07

Owner and group are separate fields

The mode tells the kernel what the owner, the group, and everyone else may do. The owner and group tell the kernel which triplet applies to you. The owner is a numeric UID, and the group is a numeric GID. Changing either one changes which class of bits applies to a given process, even when the mode itself stays the same.

This is why a file that you can read today might become unreadable after someone changes its group. The mode did not change, but the group the kernel compares against did. When a permission problem appears after a change of ownership, check the owner and group before you change the mode.

Why chown needs root

Changing the owner of a file to another user is a privileged operation. On Linux, only root can give a file to a different UID. The reason is practical. If an ordinary user could hand a file to someone else, the recipient could be filled with large files that the sender could no longer remove, and quotas and accounting would stop meaning anything. The kernel therefore refuses the change for normal accounts.

BASH1 lines
chown root notes.txt

As an ordinary user, that command fails with an error such as Operation not permitted, and the exit status is non-zero. The file keeps its owner. The error is not a bug in your command. It is the kernel enforcing the rule, and the answer is never to try a different program with the same effect.

chgrp to a group you belong to

The group is handled differently. The owner of a file may change its group to any group that the owner belongs to. The kernel checks the list of supplementary groups of the process, so you can move a file to a group you are a member of, but not to a group you are not in. This rule lets you share a file with a team without asking root for each change.

BASH2 lines
id -G
chgrp "$(id -g)" notes.txt

The first command lists your group numbers. The second changes the group of a file to your primary group, which is always allowed. Trying to set a group you do not belong to fails in the same way as the owner change above. The chgrp command is the one to use when you want to share a file, because it leaves the owner in place.

Reading ownership with ls -ln and stat

The ls -l command shows owner and group names, which are looked up from the password and group files. The -n option shows the numbers instead, which is the form the kernel actually stores. The two forms can disagree when a name is missing from the database, so the numeric form is the safe one to check when something looks wrong.

BASH4 lines
touch owned.txt
ls -l owned.txt
ls -ln owned.txt
stat -c "%U %u %G %g %n" owned.txt

The stat format above prints the owner name and number, and the group name and number, followed by the file name. Use it when you need the exact values for a script. A file you just created shows your own UID and your primary GID, because the kernel copied them from the process that created it.

Note

Commands such as chown -R and chgrp -R apply the change to a whole tree, and they fail for most of the files you do not own. Read the error messages carefully, because a partial change can leave a tree in a mixed state.

Group changes and the primary group

A process has one effective group and a list of supplementary groups. The effective group is the one that new files inherit, and it is normally the primary group named in the password file. When you change the group of a file you own, you choose one of the groups in your list, and the chgrp command checks that list before it makes the change.

The primary group is a convenient target because it is always in your list. A command such as chgrp "$(id -g)" file gives the file your primary group without typing a name. The same command fails for a group that you are not in, which is the behavior that stops a user from moving a file into a group that the user does not belong to.

Ownership problems in shared places

Ownership explains a family of failures on shared systems. A file copied from another account, or extracted from an archive made by someone else, keeps the owner number recorded in the source. If that number is not yours, you may be unable to change the mode, even when the group bits look generous, because the owner check comes first. The practical fix is to copy the file again so that your account becomes the owner, rather than to fight the owner number with more chmod calls.

The setgid bit on a shared directory removes much of this friction. A new file created in that directory takes the group of the directory, so the group is correct from the start, and the chgrp step is unnecessary. The setuid and setgid unit describes the directory form of the bit in more detail.

Hands-on: see the numeric owner

Create a file, then print its numeric owner with stat -c %u. Compare that number with id -u. Then try chown root on the file, and check the exit status with echo $?. The lab for this unit asks you to save the owner UID in a file, and to record the exit status of a failed attempt to give the file to root. Both checks read the real state of the file, so you cannot pass them by typing values from memory.

Terminal lab · 2 steps · real Linux in your browser

Ownership: chown & chgrp

Read the numeric owner of a file you own, and confirm that an ordinary account cannot hand the file to root.

01
Record the numeric owner of a file
The file /workspace/own/owned.txt already exists and belongs to you. Save its numeric owner UID to /workspace/own/uid.txt using stat -c %u.
Show hint for step 1
The number in the file should match what id -u prints.
02
Show that you cannot give a file to root
Try chown root on /workspace/own/owned.txt. Expect it to fail. Save the exit status of that command in /workspace/own/status.txt. The file must still belong to you.
Show hint for step 2
Redirect the error message away with 2>/dev/null, then read the exit status with $? on the next line.
Check yourself Question 1 of 4
Concept Check Two fields that change recall

Which two properties does chown change?

Recap & Key Takeaways

The owner and group are numeric fields, and they decide which mode class applies to a process.
Only root can give a file to another user, so chown root fails for an ordinary account with Operation not permitted.
The owner can change a file's group to any group the owner belongs to, which is how files are shared with a team.
Use stat -c with %u and %g, or ls -ln, to read the numeric owner and group when names are ambiguous.

Primary Sources & Further Reading

Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue