How To

chown Command in Linux: Change File and Directory Ownership

Every file and directory in Linux has an owner (a user) and a group. Together with permission bits, these two attributes control who can read, write, or execute a file.

The chown command in Linux changes the user and group ownership of files, directories, and symbolic links. This guide covers all syntax patterns, common options, and practical use cases.

How to Use the chown Command in Linux

The syntax is:

bashchown [OPTIONS] USER[:GROUP] FILE(s)

Key syntax patterns at a glance:

PatternEffect
chown USER FILEChange owner only; group unchanged
chown USER: FILEChange owner; set group to user’s login group
chown USER:GROUP FILEChange both owner and group
chown :GROUP FILEChange group only (equivalent to chgrp)

No output is produced on success. Add -v for verbose output that confirms each change.

Who can use chown. Regular users can change only the group of files they own, and only to a group they belong to. Changing the owner always requires sudo or root.

Numeric UIDs. You can use a numeric UID instead of a username:

bashchown +1000 file2

The + prefix is important. Without it, chown 1000 file2 could match a username literally named 1000 if such a user exists on the system. The + forces chown to treat the value as a numeric ID.

Change File Owner, Group, or Both

Change the owner only:

bashchown linuxize file1

The group stays exactly as it was.

Change both the owner and the group:

bashchown linuxize:devs file1

Change the owner and set the group to the user’s default login group:

bashchown linuxize: file1

The trailing colon with no group name tells chown to look up the user’s primary group and apply it automatically. This is different from chown linuxize file1, which leaves the group completely untouched.

Change only the group:

bashchown :www-data file1

This produces the same result as chgrp www-data file1. Both commands exist; chown :GROUP is useful when you are already using chown and want to handle everything in one command.

To change ownership of multiple files, list them separated by spaces:

bashchown linuxize:devs file1 file2 dir1

Symbolic Links, Recursive Changes, and Reference Files

Symbolic links. By default, chown changes the ownership of the file a symlink points to, not the symlink itself. On most systems, /proc/sys/fs/protected_symlinks is set to 1, which blocks this dereferencing through symlinks and produces a “cannot dereference” error.

To change the ownership of the symlink itself:

bashchown -h www-data symlink1

Recursive changes with -R apply to a directory and everything inside it:

bashchown -R www-data: /var/www

When the directory contains symbolic links, add -h to also change their ownership:

bashchown -hR www-data: /var/www

Two additional flags, -H and -L, control how chown follows symlinks during recursive traversal. -H follows symlinks given as command-line arguments. -L follows every symlink to a directory encountered during the walk. Avoid both in most cases — they can cause unintended ownership changes well outside the target directory.

Copy ownership from a reference file with --reference:

bashchown --reference=file1 file2

This sets file2 to the same owner and group as file1 in a single operation. If file1 is itself a symlink, chown uses the ownership of its target, not the symlink.

Common Use Cases and Taking Ownership as the Current User

Web server directories. Assign /var/www to the web server user:

bashsudo chown -R www-data:www-data /var/www

After archive extraction or sudo file copies. Files extracted as root or copied with sudo retain root ownership. Use the $USER environment variable to reassign them to the current user without hardcoding a username:

bashsudo chown $USER:$USER file1sudo chown -R $USER:$USER mydir

Application deployments. Assign application files to a dedicated service user so the application runs with minimal privileges. If the service is compromised, it cannot access files owned by other users.

Always run ls -la /path/to/dir before using -R to confirm the directory tree and spot unexpected symlinks. Avoid recursive operations on high-level directories like / or /var.

Use chown USER:GROUP FILE for a single file, add -R for directories, and use -h when working with symlinks. For changing permission bits rather than ownership, use chmod. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

type Command in Linux: Show How the Shell Resolves a Name

The type command in Linux shows how the current shell would interpret a name typed on the…

46 minutes ago

How to Check Memory Usage in Linux: free, top, and /proc/meminfo

When a Linux system is slow or behaving unexpectedly, memory is one of the first…

51 minutes ago

ip Command in Linux: Configure Interfaces, Routes, and ARP

The ip command is the standard network configuration tool on modern Linux systems. It is part of…

56 minutes ago

How to Remove Files and Directories in Linux: rm, shred, rmdir

The command line does not have a Trash folder. When you delete a file from…

1 day ago

How to Create a systemd Service File in Linux: Step-by-Step Guide

Systemd is the init system on most modern Linux distributions. A service file (also called…

1 day ago

How to Create Users in Linux with useradd: A Complete Guide

Linux is a multi-user system. Each person or service that needs access should have its…

1 day ago