Linux online course

How to Change File Ownership and Groups in Linux

Learn how to inspect and change Linux file ownership with chown and chgrp, including owner, group, recursive changes, verification, and troubleshooting.

Linux assigns every file and directory an owner user and an owning group. These identities are part of the filesystem's access-control system. You can change them from the command line with chown and chgrp.

This lesson assumes familiarity with cd, ls, shell arguments, Linux users and groups, sudo, and basic file permissions. For related Linux topics, see Linux command-line guides.

Linux File Ownership and Permissions

Ownership is the user and group identity associated with a filesystem object. The owner is the user account recorded for the object, while the group is the group account recorded for it.

Ownership is different from the permission mode. The permission mode contains read, write, and execute bits for the owner, group, and other users. Ownership determines which accounts those permission categories refer to.

  • The owner permissions apply to the recorded owner user.
  • The group permissions apply to users who belong to the recorded group.
  • The other permissions apply to users who are neither the owner nor members of the group.

Changing ownership can therefore change who receives a file's owner or group permissions. It is also an important administrative task for shared projects, services, web content, backups, and application data.

Inspect Current Ownership with ls -l

Before changing ownership, inspect the exact target:

ls -l bobs_file.txt

A typical long-format listing might look like this:

-rw-r----- 1 bob developers 1280 Aug 17 10:30 bobs_file.txt
FieldMeaningExample value
Permission stringFile type and permissions for owner, group, and others-rw-r-----
Owner userUser account that owns the objectbob
Owner groupGroup account associated with the objectdevelopers
File nameName of the directory entry being listedbobs_file.txt

In this output, the owner and group are the fields after the link count and before the size. The permission string is separate: changing ownership does not directly change the read, write, or execute bits.

Change a File Owner with chown

chown means “change owner.” Its basic owner-only form is:

sudo chown NEW_OWNER FILE

For example:

sudo chown jwilliams bobs_file.txt

This changes the user owner to jwilliams while leaving the existing group unchanged. Changing ownership commonly requires root privileges. Root is the administrative user that can generally change ownership of arbitrary files. sudo runs an authorized command with elevated privileges.

Verify the result by listing the same path:

ls -l bobs_file.txt

Check that the owner field now contains jwilliams. The command's exit status can also indicate whether it succeeded; a zero status normally indicates success.

Change Both Owner and Group with chown

Use the OWNER:GROUP form to assign both identities in one command:

sudo chown jwilliams:developers bobs_file.txt

The colon separates the valid user name from the valid group name. This command sets the owner to jwilliams and the group to developers.

Confirm both fields:

ls -l bobs_file.txt

Do not confuse the colon with part of either account name. Use account names that actually exist on the system.

Change Only the Group with chown

To keep the current owner and replace only the group, omit the owner before the colon:

sudo chown :developers bobs_file.txt

The :GROUP form changes the group to developers without changing the user owner. This differs from OWNER:GROUP, which changes both values.

Change a Group with chgrp

chgrp is the dedicated command for changing group ownership. Its syntax is:

sudo chgrp NEW_GROUP FILE

For example:

sudo chgrp developers bobs_file.txt

This changes only the group and leaves the owner unchanged. chgrp is often clearer when the task is specifically a group change. The equivalent chown :developers bobs_file.txt is useful when you are already using chown syntax.

Commands for Changing Linux Ownership

GoalCommand patternExampleWhat changes
Change owner only with chownchown NEW_OWNER FILEsudo chown jwilliams bobs_file.txtUser owner only
Change owner and group with chownchown NEW_OWNER:NEW_GROUP FILEsudo chown jwilliams:developers bobs_file.txtUser owner and group
Change group only using chown :GROUPchown :NEW_GROUP FILEsudo chown :developers bobs_file.txtGroup only
Change group only with chgrpchgrp NEW_GROUP FILEsudo chgrp developers bobs_file.txtGroup only
Apply changes recursively with -Rchown -R NEW_OWNER:NEW_GROUP DIRECTORYsudo chown -R jwilliams:developers /srv/projectSelected directory and its contents

Apply Ownership Changes Recursively

An operation is recursive when it applies to a directory and everything beneath it. The -R option makes chown or chgrp process the selected directory, its files, and nested subdirectories.

sudo chown -R jwilliams:developers /srv/project

This sets the owner and group throughout the /srv/project directory tree. To change only groups recursively, use:

sudo chgrp -R developers /srv/project

Inspect the directory itself and its immediate contents after a recursive change:

ls -ld /srv/project && ls -l /srv/project

The -d option makes ls display the directory entry rather than listing only its contents. The second command shows the immediate entries inside it.

Confirm Users and Groups Before Changing Ownership

Check that the target user exists:

id jwilliams

id displays the user's numeric identity and group memberships. It also helps distinguish a misspelled user name from a valid one.

Check that a group exists:

getent group developers

getent group checks configured name-service sources, which can include local and directory-based accounts. If either command cannot find the requested identity, correct the name or create the account or group through the appropriate administrative process.

Use Safe Paths and Shell Quoting

Use an explicit path so that the command targets the intended object. An absolute path such as /srv/project removes ambiguity about the current directory. You can inspect the current directory with ls before changing anything.

Quote file names containing spaces or shell-special characters:

sudo chown jwilliams "Bob's File.txt"

Without quoting, the shell would treat the space as an argument separator. Quoting also prevents the shell from interpreting many special characters as syntax.

Troubleshooting Ownership Changes

“Operation not permitted” or “Permission denied”

The current account lacks authority to change the target's ownership. Use sudo when you are authorized, or ask the appropriate administrator. Ordinary users generally cannot assign arbitrary files to arbitrary owners.

“Invalid user” or “invalid group”

The account name may be misspelled or may not exist. Check it with id USER or getent group GROUP, then rerun the command with valid names.

“No such file or directory”

The path may be wrong, the current directory may not be what you expect, or a name containing spaces may not have been quoted. Confirm the path with ls, use an absolute path when useful, and quote filenames such as "Bob's File.txt".

A recursive command changed too many files

The -R option may have been applied to an overly broad or incorrect directory. Before repeating the operation, inspect the target with:

ls -ld /srv/project

Use the narrowest correct directory path and avoid recursive changes when only one file needs modification.

Ownership appears unchanged

The command may have targeted a different path, failed, or the result may not have been checked. Run ls -l on the exact target and review the command's exit status. For a directory, use ls -ld DIRECTORY to inspect the directory itself.

Practical Workflow

  1. Confirm the intended path with ls or an absolute path.
  2. Inspect current ownership with ls -l FILE or ls -ld DIRECTORY.
  3. Confirm the target user with id USER and group with getent group GROUP.
  4. Choose chown for an owner change, chown :GROUP or chgrp for a group-only change, and -R only for an intentionally complete directory tree.
  5. Use sudo when authorized and required.
  6. Run ls -l again and verify the exact owner and group fields.

Exam-Relevant Notes

  • chown NEW_OWNER FILE changes the user owner.
  • chown OWNER:GROUP FILE changes both the user owner and group.
  • chown :GROUP FILE changes only the group and retains the user owner.
  • chgrp GROUP FILE changes only the group.
  • -R means recursive: the operation includes the directory and all contents beneath it.
  • ls -l displays the permission string, owner, group, and file name needed to verify a change.
  • Ownership changes and permission-mode changes are separate operations; chown does not replace chmod.