sodcalculator

10 Tech How-To

How to Change the Owner of a Directory in Linux

Guide 10-39803 · Updated · Step by step

Changing a directory’s owner in Linux transfers ownership to another user, and optionally to another group. Use the chown command from a terminal; you’ll need the target account to exist and usually administrator privileges to assign ownership.

What You’ll Need

  • Access to a Linux terminal, either locally or through SSH.
  • The directory path and the username you want to make its owner. If assigning a group too, know the group name.
  • sudo access, or a root shell, if you do not own the directory or need to assign it to another user.

Step-by-Step

  1. Open a terminal and identify the directory. If you are unsure of its exact path, move to its parent directory and run pwd to print the current location, then ls -ld -- directory-name to inspect it. The -d option displays the directory itself rather than its contents. For a path with spaces, put it in quotes, such as '/home/alex/Project Files'.
  2. Check the current owner and group. Run ls -ld -- '/path/to/directory'. The output begins with permissions and link information, followed by the owner and group. For a more explicit view, use stat -c '%U %G %n' -- '/path/to/directory' on systems with GNU stat. These checks help confirm you have the right path before changing anything.
  3. Confirm the new user exists. Run id username, replacing username with the intended account. A successful result shows its user ID and groups. If the account is missing, stop and ask an administrator to create or confirm the correct account; do not substitute a display name or assume that a similarly named account is the same user.
  4. Change the owner of the directory itself. For a directory you are allowed to manage, run sudo chown username -- '/path/to/directory'. Replace both placeholders with the actual username and path. The -- marks the end of command options, which is helpful if a path begins with a hyphen. Enter your password if prompted. To change ownership without sudo, omit it only when your account has permission to make that change.
  5. Optionally set a group as well. To change both owner and group, use sudo chown username:group -- '/path/to/directory'. Check the group first with getent group groupname or confirm it with your administrator. To change only the group while leaving the owner unchanged, use sudo chown :group -- '/path/to/directory'. A colon separates the user and group fields.
  6. Choose whether to include files inside it. The command so far changes only the directory entry, not its contents. To change the directory and everything beneath it, add -R: sudo chown -R username:group -- '/path/to/directory'. Recursive changes can affect many files, so first inspect the path and consider listing its contents. Do not use a broad path such as / or your whole home directory unless that scope is intentional and approved.
  7. Verify the result. Run ls -ld -- '/path/to/directory' to confirm the directory’s owner and group. If you used -R, inspect selected files with ls -l or check a particular file with stat. Verify that expected applications and users can still access the files.

Common Mistakes

  • Forgetting recursion: Without -R, only the named directory changes. Its existing files and subdirectories keep their owners.
  • Using recursion when it is not needed: chown -R may change private files, application data, or mounted content. Check the target carefully before running it.
  • Mixing up user and group: In username:group, the name before the colon is the user and the name after it is the group. Use account tools to verify both.
  • Entering the wrong path: Relative paths depend on your current directory. Use an absolute path, quote paths with spaces, and inspect the target before making changes.
  • Assuming ownership grants every kind of access: Permissions, ACLs, read-only mounts, and security policies can still limit access. Changing the owner does not automatically make every file readable or writable.

Tips

  • Use sudo for the single command instead of staying in a root shell. This reduces the chance of accidentally running an unrelated command with elevated privileges.
  • For an unusual path beginning with a hyphen, use an absolute path or prefix a relative path with ./; the -- separator also prevents it being treated as an option.
  • For a large recursive change, preview the directory with find '/path/to/directory' -maxdepth 2 -print. This shows a limited portion of the tree before you act.
  • Ownership changes do not normally alter permission bits. If access remains wrong, inspect permissions with ls -l and ACLs with getfacl if installed; avoid changing permissions blindly.
  • On shared or managed systems, confirm the intended owner with the system administrator, especially for service directories and files used by system programs.

Troubleshooting

“Operation not permitted” or “Permission denied”: You may not have the rights required to change ownership. Retry with sudo if your account is authorized. If it still fails, check whether the filesystem is mounted read-only, whether the directory is on a network or special filesystem, and whether system policy restricts the operation. Contact the administrator rather than trying to bypass those controls.

“Invalid user” or “invalid group”: Check spelling and confirm the account or group exists with id or getent. User and group names are case-sensitive on many Linux systems. Use the exact local account name, not a person’s full name.

The command reports success but the owner looks unchanged: Confirm you inspected the exact same path, including any symbolic links, and check the result with stat. By default, chown follows a symbolic link and changes the referenced target’s ownership; this can be surprising. Do not use link-handling options unless you understand which object they affect. Also check whether a container, network mount, or mapped user ID affects how ownership is displayed.

Some files changed, but others did not: A recursive command can encounter files you cannot access, mounted filesystems, or special filesystem restrictions. Read all error messages and inspect remaining files individually. Avoid repeatedly broadening the command; determine why those entries differ first.

Frequently Asked Questions

What is the basic command to change a directory owner?
Use sudo chown username -- '/path/to/directory', replacing the username and path with the correct values.

Does chown change the files inside the directory?
Not by default. Add -R to change the directory and its contents recursively, after checking the scope.

How do I change the owner and group together?
Use sudo chown username:group -- '/path/to/directory'. Verify that both accounts exist before running it.

Can I change ownership without sudo?
Sometimes. You can change ownership when you have sufficient permissions, but regular users generally cannot assign files to another user. System rules and filesystem type also matter.

Will changing ownership change file permissions?
No. Ownership and permissions are separate. Use ls -l to inspect permissions, and adjust them only if you understand the access you intend to grant.

What happens if the directory is a symbolic link?
By default, chown follows the link and changes the target’s ownership. Check the link and target before running the command, especially with recursive operations.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →