skip to content

On Linux, why can a regular user not hand one of their own files to another user with `chown`, while they can often change its group with `chgrp`?

level: seniorimportance: should knowfreq 40%

answer

  1. quotas and attribution
  2. giving away is privileged
  3. only groups you already belong to
  4. set-ID bits get cleared
  5. ownership is a number, not a name

basics

~20 s

Linux restricts changing a file's owner to privileged processes, because giving files away would let users escape disk quotas and dump content into other accounts. Changing the group is allowed to the file's owner, but only to a group they themselves belong to.

solid answer

~50 s

Linux implements the POSIX "restricted chown" behaviour: transferring ownership requires the privilege to do so, which in practice means root or a process holding the corresponding capability. Two reasons drive it. First, **quota evasion** — if you could give files away, you would dump your data onto someone else's disk allocation while still being able to read and write it through permissive modes. Second, unwanted attribution and the security consequences of a file suddenly belonging to an account that never created it. Group changes are less dangerous, so the rules are looser: the file's owner may `chgrp` to any group they are a member of. They cannot pick an arbitrary group they do not belong to — that also needs privilege. One important side effect: when an unprivileged user changes a file's owner or group, the kernel clears the set-user-ID and set-group-ID bits, so ownership changes cannot be used to smuggle a privileged executable into someone else's name.

code

bash · 8 lines
bash
# Allowed: you own the file and belong to devs
chgrp devs /srv/app/report.csv

# Denied for a regular user: Operation not permitted
chown deploy /srv/app/report.csv

# Privileged: transfer user and group together, recursively
sudo chown -R app:app /srv/app

go deeper

for a junior

Know that changing a file's owner needs root, that you can change the group only to a group you are in, and that a permission problem is often about ownership rather than about the mode bits.

for a middle

Explain the restriction and its motivation — quota accounting is per owner, so giving files away would defeat it — and state the group rule precisely, including that an arbitrary target group requires privilege.

for a senior

Diagnose ownership problems in production: wrong UIDs after a restore or a volume move, why the fix is chown rather than a wider mode, and why the kernel clears the set-ID bits when an unprivileged user changes owner or group.

for a principal

Own the identity mapping: decide how service-account UIDs and GIDs are allocated consistently across hosts, images and backups, so that ownership survives a move rather than being repaired by hand after each incident.

## The asymmetry `chown` changes the owning user (and optionally the group); `chgrp` changes only the group. On Linux the two are governed by different rules: - **Changing the owner** requires privilege — root, or a process holding the capability that permits arbitrary ownership changes. The file's own owner cannot do it, and neither can the intended recipient. - **Changing the group** is allowed to the file's owner, provided the *target* group is one the owner belongs to (their effective GID or one of their supplementary groups). Anything else requires the same privilege as changing the owner. So a normal user can move a file between groups they are already in, and can never push a file into another user's name. ``` $ chgrp devs report.csv # ok if you own it and you are in devs $ chown deploy report.csv # chown: changing ownership: Operation not permitted ``` ## Why giving a file away is privileged POSIX makes this configurable and Linux chooses the restricted behaviour. The reasons are concrete: **Disk quotas.** Quota accounting is per owning UID. If any user could reassign ownership, filling your quota would be solved by handing files to someone else — while keeping full access to them through a permissive mode, since access is decided by the mode and your relationship to it, not by the fact that you created the file. Restricted chown is what makes quota accounting meaningful at all. **Attribution and repudiation.** Ownership is used across the system as evidence of who is responsible for a file. A user who could push files into another account could plant content and have it attributed to that account. **Escalation shapes.** Combined with other mechanisms, an unrestricted ability to hand out ownership creates awkward interactions — for example moving a file into an account whose home directory has different semantics, or handing over a file that a privileged process will later trust because of who owns it. The symmetrical case matters too: you also cannot *take* a file from another user. Ownership is not something the recipient can accept; only a privileged process can move it, which keeps the operation auditable in one place. ## The set-ID clearing rule When an unprivileged process changes a file's owner or group, the kernel clears the set-user-ID bit and, for group-executable files, the set-group-ID bit. This closes a gap: without it, a user could prepare a specially crafted executable and get its ownership changed, so that it later executed with someone else's identity. The clearing is automatic and silent, so a `chgrp` on an executable can quietly change its behaviour — which is a good reason to re-check modes after any bulk ownership change on a tree containing binaries. ## Practical usage `chown` accepts a combined form and can address the group alone: ``` chown app:app /srv/app/data # user and group chown :devs report.csv # group only, same effect as chgrp chown -R app:app /srv/app # recursive chown --reference=a b # copy owner/group from another file chown -h app:app link # act on a symlink itself, not its target ``` By default `chown` follows symbolic links and changes the target; `-h` changes the link. That distinction matters when recursing over a tree you did not create. A recursive ownership change is much safer than a recursive mode change — it is not destroying a per-file structure the way `chmod -R` does — but it is still worth knowing what is under the path, particularly whether anything there is deliberately owned by a different service account. ## Where it bites in production The recurring shape: a deployment or a restored backup leaves files owned by the wrong UID, and the service cannot write its own data directory. The fix is `chown -R` as root, not opening the mode up. Reaching for `chmod 777` to work around what is actually an ownership problem is the exact anti-pattern this question is testing for. A second shape appears when files move between systems: ownership is stored as a numeric UID and GID, not as a name. Copying a filesystem image, restoring an archive, or mounting a volume on a different host can leave files owned by a UID that maps to a different account — or to no account, in which case listings show a bare number. The bits did not change; the mapping did. ## What a strong answer includes Name the restriction and its reason (quota evasion and attribution), state the group rule precisely (owner, plus membership in the target group), mention that privilege is required for anything else, and add the set-ID clearing rule as the security detail that shows you know why the kernel does more than just update two numbers.

  • A restored backup leaves an application's data directory unwritable by its service account. What is the right fix?
    Correct the ownership, not the mode. `chown -R` the tree to the service account and group, then confirm the modes still express intent — typically write for the owner, read for the group, nothing for others. Widening the mode to work around wrong ownership leaves the tree exposed and does not address why the UIDs are wrong.
  • Why do files restored onto a different host sometimes show a numeric UID instead of a username?
    Because ownership is stored as a numeric identifier in the inode, and the name is only a lookup at display time. If the account with that UID does not exist on the new host, there is nothing to render and the number is shown as-is. Nothing about the file changed — the mapping from number to name did.
  • What happens to the set-user-ID bit when an unprivileged user changes a file's group?
    The kernel clears it, along with the set-group-ID bit on group-executable files. This prevents ownership or group changes from being used to hand a privileged executable to a different identity. The clearing is silent, so bulk ownership changes over a tree containing such binaries can quietly alter behaviour and are worth re-checking afterwards.

saying these in an interview costs you the question

  • Believing any user can give their own files to another user
  • Thinking the recipient can accept ownership of a file
  • Assuming chgrp works for any group, regardless of membership
  • Fixing an ownership problem by widening the mode instead
  • Treating ownership as a stored username rather than a numeric ID

context