Knowledge Base

Knowledge Base Article

Understanding and Using the chown Command in Linux and Unix

Rooted in Kansas. Connected to your future.

Details

Category
Linux / UNIX Administration
Article Type
How-To Guide
Difficulty
Beginner to Intermediate
Applies To
Linux, UNIX, Ubuntu, Debian, Red Hat, Rocky Linux, and other UNIX-like operating systems

Description

Linux, UNIX, Ubuntu, Debian, Red Hat, Rocky Linux, and other UNIX-like operating systems

Procedure

1. Understanding File Ownership

Linux and UNIX files have two primary ownership attributes:

Ownership Type Description
User Owner The user account that owns the file or directory
Group Owner The group assigned to the file or directory

These attributes determine which owner and group permission bits apply.

For example:

ls -l document.txt

Example output:

-rw-r----- 1 admin staff 2048 Nov 5 10:30 document.txt

In this example:

  • admin: The user who owns the file.
  • staff: The group assigned to the file.
  • rw-: The owner can read and write.
  • r--: Members of the assigned group can read.
  • ---: Other users receive no permissions.

The chown command changes ownership, while the chmod command changes permission bits.

2. Understanding the chown Command

The basic syntax is:

chown [options] owner[:group] file

Where:

  • chown: Command used to change file ownership.
  • options: Optional parameters controlling command behavior.
  • owner: User account to assign as the owner.
  • group: Optional group to assign.
  • file: File or directory to modify.

For example:

sudo chown admin document.txt

This assigns ownership of document.txt to the user admin.

The file's group ownership remains unchanged.

Important: Changing a file's owner generally requires elevated privileges. Changing only its group may be permitted for the file owner when the requested group is one of their groups, depending on the operating system.

3. Viewing Current File Ownership

Before changing ownership, verify the existing settings.

Run:

ls -l filename

Example:

-rw-r--r-- 1 root root 4096 Nov 5 11:00 config.txt

This indicates that both the user owner and group owner are root.

To view detailed ownership information:

stat filename

Example output:

Access: (0644/-rw-r--r--)
Uid: (0/root)
Gid: (0/root)

The UID identifies the user owner, while the GID identifies the group owner.

To view the current user's identity and group membership:

id

4. Changing the User Owner

To change the user owner of a file:

sudo chown admin document.txt

This changes the file's user owner to admin.

Verify the result:

ls -l document.txt

Example:

-rw-r--r-- 1 admin staff 2048 Nov 5 10:30 document.txt

Only the user ownership changes. The group remains staff.

Changing ownership of multiple files

sudo chown admin file1.txt file2.txt file3.txt

This assigns the same owner to all three files.

5. Changing Both User and Group Ownership

To change both the user and group owner, separate them with a colon.

sudo chown admin:staff document.txt

This assigns:

  • User owner: admin
  • Group owner: staff

Verify the result:

ls -l document.txt

Example:

-rw-r--r-- 1 admin staff 2048 Nov 5 10:30 document.txt

This method is commonly used when preparing files for applications, services, and shared directories.

6. Changing Only Group Ownership

The chown command can change group ownership without changing the user owner.

sudo chown :staff document.txt

This assigns the group staff while preserving the current user owner.

Alternatively, use:

sudo chgrp staff document.txt

Both commands can accomplish the same group ownership change.

Understanding the colon

Command Result
chown admin file Changes user owner only
chown admin:staff file Changes user and group
chown :staff file Changes group only
chown admin: file Changes user and sets group to that user's login group
chown --reference=source file Copies owner and group from another file on GNU systems

7. Changing Directory Ownership

The chown command also works with directories.

Example:

sudo chown admin:staff /srv/shared

This changes ownership of the directory itself.

Files and subdirectories inside /srv/shared are not automatically modified.

To view directory ownership:

ls -ld /srv/shared

Example:

drwxr-x--- 5 admin staff 4096 Nov 5 12:00 /srv/shared

The owner and group are now assigned to admin and staff.

8. Changing Ownership Recursively

The -R option applies ownership changes to a directory and its contents.

Example:

sudo chown -R admin:staff /srv/shared

This changes ownership of:

  • The target directory.
  • Files within the directory.
  • Subdirectories.
  • Files within those subdirectories.

Common use case: Application directory

sudo chown -R appuser:appgroup /srv/application

This can be useful when an application requires ownership of its working files.

Security Warning: Recursive ownership changes can affect large numbers of files. Never run a recursive chown command on system directories without understanding the consequences.

Avoid commands such as:

sudo chown -R admin:admin /

Changing ownership throughout the entire filesystem can damage system security and prevent applications or services from functioning.

When using -R, review symbolic-link handling options and avoid untrusted directory trees. GNU chown provides -P, -H, and -L options to control recursive traversal.

9. Using chown with WordPress

Incorrect file ownership is a common cause of WordPress permission problems.

Symptoms may include:

  • Plugin installation failures.
  • Theme update errors.
  • Media upload failures.
  • WordPress requesting FTP credentials.
  • Inability to create or modify files.

Step 1: Identify the web server account

On Ubuntu or Debian, the web server commonly runs under www-data.

Check the running processes:

ps aux | grep -E 'apache2|nginx|php-fpm'

The actual file-access account depends on the web server, PHP configuration, and hosting environment.

Step 2: Check WordPress directory ownership

ls -ld /var/www/html

Step 3: Correct ownership if appropriate

For a server configured to use www-data as the application file owner:

sudo chown -R www-data:www-data /var/www/html

Important: This is an example, not a universal WordPress recommendation. Many installations intentionally separate deployment ownership from the web server account. Assigning the entire site to the web server can increase security exposure if that account is compromised.

Use the ownership model documented for your hosting environment and grant write access only where needed.

Step 4: Verify ownership

ls -ld /var/www/html
ls -l /var/www/html

Confirm that the intended account owns the required files and directories.

10. Understanding chown vs. chmod

Although both commands affect file access, they serve different purposes.

Command Purpose Example
chown Changes user and group ownership chown admin:staff file
chmod Changes file permission bits chmod 644 file
chgrp Changes group ownership chgrp staff file

Example:

sudo chown admin:staff document.txt
chmod 640 document.txt

The first command assigns the user and group owners.

The second command assigns permissions:

Owner:  Read and write
Group:  Read only
Others: No access

Ownership and permissions work together to determine access.

11. Troubleshooting chown Errors

Issue: Operation Not Permitted

Error:

chown: changing ownership of 'file': Operation not permitted

Possible Cause:

The current user lacks sufficient privileges, or the filesystem restricts ownership changes.

Solution:

Check current ownership:

ls -l file

If authorized, use:

sudo chown admin:staff file

If the error persists, investigate immutable attributes, read-only filesystems, network filesystem restrictions, and container permissions.

Issue: Invalid User

Error:

chown: invalid user: 'exampleuser'

Possible Cause:

The specified user does not exist or cannot be resolved.

Solution:

Check whether the account exists:

id exampleuser

If the account does not exist, verify the spelling or use an existing authorized account.

Issue: Invalid Group

Error:

chown: invalid group: 'examplegroup'

Possible Cause:

The specified group does not exist or cannot be resolved.

Solution:

Check the group:

getent group examplegroup

Use a valid group or create one if required and authorized.

Issue: Application Stops Working After chown

Possible Cause:

The application no longer has the ownership or access rights it requires.

Solution:

Inspect ownership:

ls -la /srv/application

Identify the service account:

ps aux

Restore ownership according to the application's documented requirements.

For example:

sudo chown -R appuser:appgroup /srv/application

Only run this command after confirming that appuser:appgroup is the correct ownership configuration.

Issue: Permission Denied After Changing Ownership

Possible Cause:

Changing ownership does not automatically grant the necessary permissions.

Solution:

Check permissions:

ls -l filename

If appropriate, adjust permissions separately:

chmod 640 filename

Also verify parent directory traversal permissions and any ACL restrictions.

12. Advanced chown Options

Common options include:

Option Description
-R Recursively change ownership
-v Display detailed processing information
-c Report only changes that were made
-f Suppress most error messages
--reference Copy ownership from another file
--from Change ownership only when current ownership matches
-h Change ownership of a symbolic link itself where supported

Some options, particularly --reference and --from, are GNU-specific and may not be available on every UNIX system.

Verbose example

sudo chown -v admin:staff document.txt

Copy ownership from another file

sudo chown --reference=source.txt destination.txt

Change ownership only if it matches an expected value

sudo chown --from=root:root admin:staff document.txt

This last command changes ownership only if the file is currently owned by user root and group root.

13. Security Best Practices

  • Follow the principle of least privilege.
  • Assign files to the appropriate service or user account.
  • Avoid unnecessary recursive ownership changes.
  • Do not change ownership of system files without verifying requirements.
  • Use groups to manage shared resources.
  • Keep sensitive files owned by trusted accounts.
  • Review ownership after migrations, restores, and deployments.
  • Verify both ownership and permissions when troubleshooting access.
  • Avoid giving web server accounts ownership of files they do not need to modify.
  • Document application-specific ownership requirements.

14. chown Quick Reference

Command Description
ls -l file View file ownership
stat file Display detailed ownership information
chown user file Change user owner
chown user:group file Change user and group
chown :group file Change group only
chgrp group file Change group ownership
chown -R user:group directory Change ownership recursively
chown -v user file Show ownership changes
chown --reference=source file Copy ownership from another file
id username View user identity and group membership
getent group groupname Verify a group exists

Addtional Information

1. Understanding File Ownership

Linux and UNIX files have two primary ownership attributes:

Ownership Type Description
User Owner The user account that owns the file or directory
Group Owner The group assigned to the file or directory

These attributes determine which owner and group permission bits apply.

For example:

ls -l document.txt

Example output:

-rw-r----- 1 admin staff 2048 Nov 5 10:30 document.txt

In this example:

  • admin: The user who owns the file.
  • staff: The group assigned to the file.
  • rw-: The owner can read and write.
  • r--: Members of the assigned group can read.
  • ---: Other users receive no permissions.

The chown command changes ownership, while the chmod command changes permission bits.

2. Understanding the chown Command

The basic syntax is:

chown [options] owner[:group] file

Where:

  • chown: Command used to change file ownership.
  • options: Optional parameters controlling command behavior.
  • owner: User account to assign as the owner.
  • group: Optional group to assign.
  • file: File or directory to modify.

For example:

sudo chown admin document.txt

This assigns ownership of document.txt to the user admin.

The file's group ownership remains unchanged.

Important: Changing a file's owner generally requires elevated privileges. Changing only its group may be permitted for the file owner when the requested group is one of their groups, depending on the operating system.

3. Viewing Current File Ownership

Before changing ownership, verify the existing settings.

Run:

ls -l filename

Example:

-rw-r--r-- 1 root root 4096 Nov 5 11:00 config.txt

This indicates that both the user owner and group owner are root.

To view detailed ownership information:

stat filename

Example output:

Access: (0644/-rw-r--r--)
Uid: (0/root)
Gid: (0/root)

The UID identifies the user owner, while the GID identifies the group owner.

To view the current user's identity and group membership:

id

4. Changing the User Owner

To change the user owner of a file:

sudo chown admin document.txt

This changes the file's user owner to admin.

Verify the result:

ls -l document.txt

Example:

-rw-r--r-- 1 admin staff 2048 Nov 5 10:30 document.txt

Only the user ownership changes. The group remains staff.

Changing ownership of multiple files

sudo chown admin file1.txt file2.txt file3.txt

This assigns the same owner to all three files.

5. Changing Both User and Group Ownership

To change both the user and group owner, separate them with a colon.

sudo chown admin:staff document.txt

This assigns:

  • User owner: admin
  • Group owner: staff

Verify the result:

ls -l document.txt

Example:

-rw-r--r-- 1 admin staff 2048 Nov 5 10:30 document.txt

This method is commonly used when preparing files for applications, services, and shared directories.

6. Changing Only Group Ownership

The chown command can change group ownership without changing the user owner.

sudo chown :staff document.txt

This assigns the group staff while preserving the current user owner.

Alternatively, use:

sudo chgrp staff document.txt

Both commands can accomplish the same group ownership change.

Understanding the colon

Command Result
chown admin file Changes user owner only
chown admin:staff file Changes user and group
chown :staff file Changes group only
chown admin: file Changes user and sets group to that user's login group
chown --reference=source file Copies owner and group from another file on GNU systems

7. Changing Directory Ownership

The chown command also works with directories.

Example:

sudo chown admin:staff /srv/shared

This changes ownership of the directory itself.

Files and subdirectories inside /srv/shared are not automatically modified.

To view directory ownership:

ls -ld /srv/shared

Example:

drwxr-x--- 5 admin staff 4096 Nov 5 12:00 /srv/shared

The owner and group are now assigned to admin and staff.

8. Changing Ownership Recursively

The -R option applies ownership changes to a directory and its contents.

Example:

sudo chown -R admin:staff /srv/shared

This changes ownership of:

  • The target directory.
  • Files within the directory.
  • Subdirectories.
  • Files within those subdirectories.

Common use case: Application directory

sudo chown -R appuser:appgroup /srv/application

This can be useful when an application requires ownership of its working files.

Security Warning: Recursive ownership changes can affect large numbers of files. Never run a recursive chown command on system directories without understanding the consequences.

Avoid commands such as:

sudo chown -R admin:admin /

Changing ownership throughout the entire filesystem can damage system security and prevent applications or services from functioning.

When using -R, review symbolic-link handling options and avoid untrusted directory trees. GNU chown provides -P, -H, and -L options to control recursive traversal.

9. Using chown with WordPress

Incorrect file ownership is a common cause of WordPress permission problems.

Symptoms may include:

  • Plugin installation failures.
  • Theme update errors.
  • Media upload failures.
  • WordPress requesting FTP credentials.
  • Inability to create or modify files.

Step 1: Identify the web server account

On Ubuntu or Debian, the web server commonly runs under www-data.

Check the running processes:

ps aux | grep -E 'apache2|nginx|php-fpm'

The actual file-access account depends on the web server, PHP configuration, and hosting environment.

Step 2: Check WordPress directory ownership

ls -ld /var/www/html

Step 3: Correct ownership if appropriate

For a server configured to use www-data as the application file owner:

sudo chown -R www-data:www-data /var/www/html

Important: This is an example, not a universal WordPress recommendation. Many installations intentionally separate deployment ownership from the web server account. Assigning the entire site to the web server can increase security exposure if that account is compromised.

Use the ownership model documented for your hosting environment and grant write access only where needed.

Step 4: Verify ownership

ls -ld /var/www/html
ls -l /var/www/html

Confirm that the intended account owns the required files and directories.

10. Understanding chown vs. chmod

Although both commands affect file access, they serve different purposes.

Command Purpose Example
chown Changes user and group ownership chown admin:staff file
chmod Changes file permission bits chmod 644 file
chgrp Changes group ownership chgrp staff file

Example:

sudo chown admin:staff document.txt
chmod 640 document.txt

The first command assigns the user and group owners.

The second command assigns permissions:

Owner:  Read and write
Group:  Read only
Others: No access

Ownership and permissions work together to determine access.

11. Troubleshooting chown Errors

Issue: Operation Not Permitted

Error:

chown: changing ownership of 'file': Operation not permitted

Possible Cause:

The current user lacks sufficient privileges, or the filesystem restricts ownership changes.

Solution:

Check current ownership:

ls -l file

If authorized, use:

sudo chown admin:staff file

If the error persists, investigate immutable attributes, read-only filesystems, network filesystem restrictions, and container permissions.

Issue: Invalid User

Error:

chown: invalid user: 'exampleuser'

Possible Cause:

The specified user does not exist or cannot be resolved.

Solution:

Check whether the account exists:

id exampleuser

If the account does not exist, verify the spelling or use an existing authorized account.

Issue: Invalid Group

Error:

chown: invalid group: 'examplegroup'

Possible Cause:

The specified group does not exist or cannot be resolved.

Solution:

Check the group:

getent group examplegroup

Use a valid group or create one if required and authorized.

Issue: Application Stops Working After chown

Possible Cause:

The application no longer has the ownership or access rights it requires.

Solution:

Inspect ownership:

ls -la /srv/application

Identify the service account:

ps aux

Restore ownership according to the application's documented requirements.

For example:

sudo chown -R appuser:appgroup /srv/application

Only run this command after confirming that appuser:appgroup is the correct ownership configuration.

Issue: Permission Denied After Changing Ownership

Possible Cause:

Changing ownership does not automatically grant the necessary permissions.

Solution:

Check permissions:

ls -l filename

If appropriate, adjust permissions separately:

chmod 640 filename

Also verify parent directory traversal permissions and any ACL restrictions.

12. Advanced chown Options

Common options include:

Option Description
-R Recursively change ownership
-v Display detailed processing information
-c Report only changes that were made
-f Suppress most error messages
--reference Copy ownership from another file
--from Change ownership only when current ownership matches
-h Change ownership of a symbolic link itself where supported

Some options, particularly --reference and --from, are GNU-specific and may not be available on every UNIX system.

Verbose example

sudo chown -v admin:staff document.txt

Copy ownership from another file

sudo chown --reference=source.txt destination.txt

Change ownership only if it matches an expected value

sudo chown --from=root:root admin:staff document.txt

This last command changes ownership only if the file is currently owned by user root and group root.

13. Security Best Practices

  • Follow the principle of least privilege.
  • Assign files to the appropriate service or user account.
  • Avoid unnecessary recursive ownership changes.
  • Do not change ownership of system files without verifying requirements.
  • Use groups to manage shared resources.
  • Keep sensitive files owned by trusted accounts.
  • Review ownership after migrations, restores, and deployments.
  • Verify both ownership and permissions when troubleshooting access.
  • Avoid giving web server accounts ownership of files they do not need to modify.
  • Document application-specific ownership requirements.

14. chown Quick Reference

Command Description
ls -l file View file ownership
stat file Display detailed ownership information
chown user file Change user owner
chown user:group file Change user and group
chown :group file Change group only
chgrp group file Change group ownership
chown -R user:group directory Change ownership recursively
chown -v user file Show ownership changes
chown --reference=source file Copy ownership from another file
id username View user identity and group membership
getent group groupname Verify a group exists