On this page
- How do you read the current owner and group?
- What is the chown syntax?
- When should you use chgrp instead?
- How do you change ownership recursively and safely?
- How does chown treat symbolic links?
- How do you set up a web root with the right owner?
- How do you keep the group on new files in a shared folder?
- How do you copy ownership from one file to another?
- Why does chown fail or not seem to work?
- Questions
How do you read the current owner and group?
Use ls -l: the third column is the owner and the fourth is the group. stat shows the same with numeric IDs, which helps when a file belongs to a UID that no longer has a name.
ls -l /var/www/app.example.net
ls -ld /var/www/app.example.net
stat -c '%U(%u) %G(%g) %A %n' /etc/ssh/sshd_configOwnership decides who the read, write and execute bits apply to. The bits themselves are a separate topic covered in file and directory permissions; chown and chgrp only decide whose those bits are.
What is the chown syntax?
The pattern is chown OWNER[:GROUP] FILE.... Leave out parts you do not want to change.
sudo chown deploy report.csv # owner only
sudo chown deploy:webteam report.csv # owner and group
sudo chown :webteam report.csv # group only
sudo chown deploy: report.csv # owner, group = deploy's login group
sudo chown 1001:1001 report.csv # numeric IDsNumeric IDs are useful for files that will be used inside a container, where the names may not exist on the host. You can confirm names and numbers with id deploy; see users and groups for creating them.
When should you use chgrp instead?
Use chgrp when only the group changes, which reads more clearly in scripts. It also works without sudo for files you own, as long as you are a member of the target group.
chgrp webteam ~/shared-notes.md
sudo chgrp -R webteam /srv/shared
id -nG # groups you belong toIf chgrp reports "Operation not permitted" as a normal user, you are not in that group yet, or the new membership needs a fresh login.
How do you change ownership recursively and safely?
Add -R to walk a directory tree. Run it on the narrowest path possible: a recursive chown on the wrong folder, such as /usr or /, can break sudo and SSH and usually means a reinstall.
sudo chown -R deploy:deploy /home/deploy/app
sudo chown -R --preserve-root deploy /srv/data
sudo chown -R -v deploy:deploy /home/deploy/app | head
sudo find /home/deploy/app ! -user deploy -ls # anything left over--preserve-root makes chown refuse to run on /; unlike rm, chown does not turn this on by default, so put it in scripts. -v lists each change, and -c lists only files that actually changed. Our console offers reinstall from the Settings tab, but it erases the disk, so a careful path check is worth the extra second.
How does chown treat symbolic links?
By default chown changes the file a link points to, not the link. To change the link itself, add -h. In recursive mode, GNU chown does not follow links into other directories unless you ask it to with -L.
sudo chown -h deploy:deploy /var/www/current
sudo chown -R -P deploy /srv/app # never follow links (default with -R)
sudo chown -R -H deploy /srv/app # follow only links named on the command lineFollowing links during a recursive chown can hand system files to a normal user if a link points outside the tree, so stick with the default. Links are explained in symbolic and hard links.
How do you set up a web root with the right owner?
Let your deploy user own the code and let the web server group read it. The web server runs as www-data on Ubuntu and Debian and as nginx or apache on Rocky and Alma.
sudo mkdir -p /var/www/app.example.net
sudo chown -R deploy:www-data /var/www/app.example.net # Ubuntu / Debian
sudo chown -R deploy:nginx /var/www/app.example.net # Rocky / Alma with Nginx
sudo find /var/www/app.example.net -type d -exec chmod 750 {} +
sudo find /var/www/app.example.net -type f -exec chmod 640 {} +
# only the upload folder is writable by the server
sudo chown -R www-data:www-data /var/www/app.example.net/uploadsOn Rocky and Alma with SELinux enforcing, ownership is not the whole story: files also need the right label. After moving content, run sudo restorecon -Rv /var/www. See SELinux basics.
How do you keep the group on new files in a shared folder?
Set the setgid bit on the directory. New files and subfolders created inside then inherit the directory group instead of the creator's primary group.
sudo mkdir -p /srv/shared
sudo chown root:webteam /srv/shared
sudo chmod 2775 /srv/shared
touch /srv/shared/test.txt && ls -l /srv/shared/test.txtThe 2 in front of 775 is the setgid bit, shown as s in ls -ld. Combine it with a group-friendly umask (umask 002) so members can edit each other's files.
How do you copy ownership from one file to another?
Use --reference, which copies owner and group from an existing file. It is handy when restoring a config from a backup that was extracted as root.
sudo chown --reference=/etc/nginx/nginx.conf /etc/nginx/conf.d/app.conf
sudo cp -a backup/app.conf /etc/nginx/conf.d/ # -a keeps owner, mode and timesThe same idea applies to archives: tar restores owners only when it runs as root, as explained in the tar guide.
Why does chown fail or not seem to work?
- Operation not permitted: you need sudo to give a file to someone else.
- invalid user: the name does not exist on this server; check with
getent passwd name. - Change has no effect on a mount: some filesystems (FAT, many network shares) do not store Unix owners, so ownership comes from mount options.
- Files show a number instead of a name: the owning user was deleted. Hand them to a current user with
sudo find / -xdev -nouser -exec chown deploy {} +after reviewing the list.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| A1-2 | a web root and a few shared folders | 1 vCPU, 2 GB RAM, 50 GB NVMe | $7.20/mo | $43.20 for 6 months |
Questions about Ownership with chown and chgrp
Does chown change permissions too?
No, it only changes owner and group. The kernel may clear setuid and setgid bits on executables when ownership changes, so check those afterwards if you rely on them.
What does chown user: with a trailing colon do?
It sets the owner to that user and the group to the user's login group in one step.
Can I undo a recursive chown?
There is no undo. Restore from a backup, or reset known paths by hand. That is why it pays to test with -v on a small folder first.
Who should own files in a home directory?
The user and their own group, for example deploy:deploy. SSH refuses keys when ~/.ssh belongs to someone else.
Does ownership survive copying to another server?
With rsync -a run as root on the receiving side, yes, matched by name. See the rsync guide.
More in Linux command line
- Copy files and directories with cp
- Move and rename with mv
- Create directories and empty files
- List files with ls, find your place with pwd
- Read files with cat, less and tail
- Find files with find and locate
- Search text with grep
- Edit and report text with sed and awk
- echo, tee and wc
- Edit files with nano
- Edit files with Vim
- Read manual pages
- Users and groups
- Symbolic and hard links
- Pipes, redirection and xargs