---
title: "chown -R on Linux: recursive ownership changes done safely · CloudSat"
description: "How chown -R works, the flags that matter (-h, --from, --preserve-root), safe patterns with find, web roots, container volumes and undoing a mistake."
url: https://cloudsat.io/learn/chown-recursive-without-breaking-permissions
updated: 2026-11-18T12:00:00Z
source: CloudSat (https://cloudsat.io/). Prices in USD.
---

[Linux](https://cloudsat.io/learn/topic/linux)

# chown -R on Linux: recursive ownership changes done safely

How chown -R works, the flags that matter (-h, --from, --preserve-root), safe patterns with find, web roots, container volumes and undoing a mistake.

CloudSat team November 18, 2026 7 min read

`chown -R` rewrites the owner of every file under a directory in one command. That is exactly what you want after an upload, a restore or a container migration, and exactly what hurts when the path is wrong: there is no confirmation and no undo. This guide covers how ownership works, the flags that change what a recursive run touches, and the habits that keep it boring.

## Ownership in one minute

Every file stores a numeric user ID, a numeric group ID and a mode (the read, write and execute bits for owner, group and others). Names such as `www-data` are looked up in `/etc/passwd` and `/etc/group` only when something displays them. That is why a restored archive or a container volume can show raw numbers: the IDs exist on disk, but no local account has them.

```bash
ls -l /var/www/site            # owner and group by name
ls -n /var/www/site            # the same with numeric IDs
stat -c '%U:%G %a %n' /var/www/site/index.php
```

Only root can give a file to another owner. A regular user who owns a file can change its group, but only to a group they belong to.

## The syntax

```bash
chown alice report.txt                 # new owner, group unchanged
chown alice:developers report.txt      # owner and group
chown alice: report.txt                # owner, plus alice's login group
chown :www-data report.txt             # group only, like chgrp
chown -R www-data:www-data /var/www/site/uploads
chown --reference=index.php new.php    # copy owner and group from another file
chown -c -R alice /home/alice          # report only what actually changed
```

## Flags that change what -R touches

| Flag | Effect |
| --- | --- |
| `-R` | Recurse into directories |
| `-h` | Change a symbolic link itself, not the file it points to |
| `-P` | During `-R`, never follow symbolic links (the default) |
| `-H` | Follow a symbolic link given on the command line, but none found inside |
| `-L` | Follow every symbolic link to a directory (rarely what you want) |
| `--from=alice:alice` | Change only files whose current owner and group match |
| `--preserve-root` | Refuse to work recursively on `/` |

With GNU chown, `--no-preserve-root` is the default, so `chown -R` happily walks the whole system if `/` ends up in the argument list. An alias in root's `~/.bashrc` closes that gap:

```bash
alias chown='chown --preserve-root'
```

Symbolic links deserve a second look. In recursive mode with the default `-P`, chown changes links themselves and never follows them, which protects files outside the tree. The catch is the top-level argument: if `/var/www/current` is a symlink to a release directory, `chown -R deploy /var/www/current` changes only the link. Add a trailing slash (`/var/www/current/`) or use `-H` to go through it.

## Look before you change

A dry run costs seconds. List what does not already have the owner you want, and count it:

```bash
find /var/www/site ! -user www-data | head
find /var/www/site ! -user www-data | wc -l
```

When only some files are wrong, fix just those. `--from` skips everything that does not match the current owner, and `find` gives you full control:

```bash
chown -R --from=root:root www-data:www-data /var/www/site
find /var/www/site -user 1001 -exec chown -h www-data:www-data {} +
```

Typos do the real damage. `chown -R alice /home/alice /` with a stray space includes the root directory. Older shells expanded `.*` to include `..`, the parent directory; bash 5.2 stopped matching `.` and `..` by default, but other shells differ, so name hidden files explicitly. Type the path with tab completion, and when in doubt prefix the command with `echo` to see what the shell expands.

### Why recursive chown on system directories breaks things

The Linux kernel clears the setuid and setgid bits when an executable's owner or group changes, and it does this for root too. Run `chown -R` across `/usr/bin` and `sudo`, `passwd` and `su` lose the bit that lets them work. Never point a recursive chown at `/usr`, `/etc`, `/var/lib` or `/` as a "fix"; repair the one directory that is wrong.

## Common jobs

### A web root

Give the code to a deploy user, give the web server group read access, and allow writes only where the application needs them:

```bash
chown -R deploy:www-data /var/www/site
find /var/www/site -type d -exec chmod 2750 {} +
find /var/www/site -type f -exec chmod 0640 {} +
chown -R www-data:www-data /var/www/site/storage /var/www/site/uploads
```

The `2` in `2750` sets the setgid bit on directories, so new files inherit the `www-data` group. A web server that cannot rewrite its own code is much harder to turn against you.

### Container volumes

A bind mount keeps numeric IDs, so the host must use the ID the process runs as inside the container, not a host user name. Ask the running container, then match it:

```bash
docker exec db id postgres                 # prints the UID and GID inside the image
sudo chown -R 999:999 ./data/postgres      # 999 is an example: use the value printed above
```

Named volumes mostly avoid the problem: when a container first mounts an empty named volume, Docker copies the image's directory into it, owners included. Bind mounts get no such help.

Rootless Podman maps container IDs to a range of host IDs, so a plain chown from the host lands on the wrong numbers. Run it inside the user namespace instead: `podman unshare chown -R 999:999 ./data`.

### After changing a user's UID

`usermod -u 2001 alice` updates the files in alice's home directory, not her files elsewhere. Find the rest by the old number:

```bash
find / -xdev -user 1001 -exec chown -h 2001 {} +
```

`-xdev` keeps `find` on one filesystem, so repeat the command for every other mount point, such as a separate `/home` or `/srv`. If you also changed the group's number with `groupmod -g`, run the same search with `-group`.

## Check the result

After the change, look for leftovers and test access the way the service sees it:

```bash
find /var/www/site ! -group www-data | head              # anything the change missed
namei -l /var/www/site/uploads/2026/photo.jpg            # owner and mode of every path component
sudo -u www-data test -r /var/www/site/index.php && echo readable
```

`namei -l` is the fastest answer to a "permission denied" error. One directory on the path without the execute bit for the service's user blocks everything below it, however correct the file itself is. Testing with `sudo -u` as the service account replays the real check, which beats reasoning about mode bits in your head.

## Undo a mistake

Prevention is cheaper than archaeology. Before a large recursive change on a server, take a snapshot ([take a snapshot](https://cloudsat.io/docs/take-a-snapshot) on CloudSat), or record the current state:

```bash
getfacl -R -p /var/www/site > /root/site-perms.acl
setfacl --restore=/root/site-perms.acl     # puts owners, groups, modes and ACLs back
```

Without a backup, your options depend on the system. On the RHEL family, `rpm --setugids -a` restores the owners of packaged files and `rpm --setperms -a` their modes; run them in that order, because changing owners clears setuid bits. Debian and Ubuntu have no single command, but `apt install --reinstall <package>` rewrites a package's files with their original owners. Your own data needs a backup.

On Ubuntu 26.04, `chown` comes from rust-coreutils, the Rust rewrite of the core utilities, with the GNU build still installed under a `gnu` prefix. Everyday flags work the same; if a script depends on an unusual one, test it there before relying on it.

## FAQ

### Does chown -R change permissions?

No. It changes owner and group only. The one side effect is the cleared setuid and setgid bits on executables described above. Use `chmod` for modes.

### Why does root get "Operation not permitted"?

Common causes: an NFS export with `root_squash`, which maps root to an unprivileged user; a file with the immutable attribute (check with `lsattr`, remove with `chattr -i`); a read-only filesystem; or a path inside a container image layer rather than a volume.

### How do I change only directories or only files?

`chown` has no type filter, so use `find`: `find /path -type d -exec chown alice {} +` for directories and `-type f` for regular files. The same pattern works with `chmod`, which is where it matters most.

**Run it on CloudSat.** Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. [See plans](https://cloudsat.io/plans) or [deploy a server](https://dash.cloudsat.io/console/deploy).

## Keep reading

[All guides in Linux](https://cloudsat.io/learn/topic/linux)

- [Add users to sudo and switch accounts on Debian and Ubuntu](https://cloudsat.io/learn/linux-users-sudo-and-switching-accounts): Linux Add users to sudo and switch accounts on Debian and Ubuntu 7 min read
- [Linux commands you need on a new server](https://cloudsat.io/learn/essential-linux-commands-for-servers): Linux Linux commands you need on a new server 8 min read
- [Find what is filling your Linux disk with df, du and ncdu](https://cloudsat.io/learn/find-what-is-filling-your-linux-disk): Linux Find what is filling your Linux disk with df, du and ncdu 7 min read
- [ps aux explained: read Linux process lists and act on them](https://cloudsat.io/learn/read-ps-output-and-manage-linux-processes): Linux ps aux explained: read Linux process lists and act on them 7 min read
