Technical skill

Linux command: chown

Change who owns a file

Linux records a user owner and a group owner for each file and directory. These identities help determine who can read, change, or run the item. chown changes the recorded owner, group, or both; it does not move or copy the file. For example, if you have permission to use sudo, sudo chown deploy:app /srv/service/config.yaml sets the user owner to deploy and the group owner to app, provided the system can look up both names.

The common GNU form is chown OWNER:GROUP PATH. The colon joins the owner and group in one argument, with no spaces around it. To change only the user owner, use chown deploy PATH. To change only the group, use GNU chown :app PATH or the dedicated command chgrp app PATH. A user or group name is a label for a numeric ID. If you use a number because a name is unavailable, check that ID first: the same number can identify a different account on another computer.

Check permission and link behavior

Changing ownership usually requires special permission. On Linux, a process with the CAP_CHOWN capability can change owners. An ordinary file owner may change the group to one of the groups that owner belongs to. If a change fails, check the account, its permission, and the path before retrying with broader privileges.

A symbolic link is a path that points to another file. By default, GNU chown follows a symbolic link and changes the target file's ownership. The -h option changes the link itself where the filesystem supports it. Check the path with ls -l or readlink before changing ownership.

The -R option changes ownership below a directory as well as on the directory itself. That can include many files, mounted paths, and links. Inspect the exact tree first. GNU chown provides -H, -L, and -P to control how it follows links during recursive operation; its default is -P. Other systems may use a different chown, so check their manual.

Verify access after a change

If a program cannot read a configuration file, inspect the file owner, group, permissions, and the account used by the program before changing anything. It may need read access through its group, not a new owner. On GNU systems, stat -c '%U:%G %A %n' /srv/service/config.yaml prints the owner, group, permission bits, and path. Compare them with the program's configured user and group, then make the narrow change that gives the intended access.

On Linux, changing the owner of an executable can clear its set-user-ID or set-group-ID permission bits and file capabilities. These are special settings that can affect the rights a program receives when it runs. After changing an executable's owner, check its mode and capability state according to the system's policy. Verify the final owner and group with stat or ls -l, then confirm the program can access the path.

Interactive exercises

Materials

Skillsets
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful