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.
chown root notes.txtAs 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.
id -G
chgrp "$(id -g)" notes.txtThe 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.
touch owned.txt
ls -l owned.txt
ls -ln owned.txt
stat -c "%U %u %G %g %n" owned.txtThe 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.
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.