Linux chown: Change File Ownership on a VPS
The chown command on a Linux VPS changes which user and group own a file or directory. You run it with the new owner (and optionally group) followed by the target path, and it immediately updates the ownership metadata that controls access permissions.
Why Ownership Matters on a VPS
Every file and directory on a Linux system has an owning user and an owning group. These two values determine what the owner, group members, and everyone else can do with the file, based on the permission bits (read, write, execute). On a VPS, you typically have several services running under different system users—like www-data for a web server or mysql for a database. If files have the wrong owner, a service may not be able to read or write them, which shows up as 403 errors, failed uploads, or broken applications.
For example, if you upload a PHP script as root but your web server runs as www-data, the server may not be able to read the file if permissions are restrictive. Changing ownership with chown fixes such mismatches.
Basic Syntax and Common Uses
The general form is:
chown [options] new_owner[:new_group] file_or_directory
chown alice file.txt— changes the owner to useralice; the group stays unchanged.chown alice:developers file.txt— changes owner toaliceand group todevelopers.chown :developers file.txt— changes only the group (the colon before the group name is required).chown -R alice:developers /var/www/mysite— recursively changes ownership of the directory and everything inside it.
You can also use numeric user and group IDs instead of names, e.g., chown 1001:1002 file.txt. This is useful when a user or group name does not exist on the system yet, but the ID is known.
How to Check Current Ownership
Before changing ownership, you usually want to see what it currently is. Use ls -l to list files with their owner and group:
ls -l /var/www/index.php -rw-r--r-- 1 root root 1234 Jan 10 12:00 /var/www/index.php
The third column is the owner (root), the fourth is the group (root). For directories, ls -ld shows the same information without listing contents. Another option is stat -c '%U %G' file.txt, which prints just the owner and group names.
When You Need sudo and When You Don’t
Only the root user can change the owner of a file. A regular user can change the group of a file they own, but only to a group they belong to. So on a VPS, you will almost always prefix the command with sudo:
sudo chown www-data:www-data /var/www/index.php
If you run chown without sudo and you are not root, you get a Operation not permitted error. This is a safety feature—it prevents users from giving away files to other users and bypassing disk quota or access controls.
One common mistake is using chown when you actually meant to change permissions. Ownership is about who owns the file; permissions (with chmod) are about what the owner, group, and others can do. For example, if a file is owned by www-data but has mode 600, only www-data can read and write it. Changing the owner to your user will not help if you want the web server to read it—you need to adjust permissions instead.
Recursive Changes and Symbolic Links
The -R option is powerful but must be used carefully. Running sudo chown -R www-data:www-data /var/www changes ownership of every file and subdirectory under /var/www. This is often necessary when deploying a web application, but it can break things if you accidentally target a system directory or a mount point.
By default, chown does not follow symbolic links—it changes the link itself, not the target. If you want to change the target of a symlink, use chown -h (or --no-dereference). On most modern distributions, the default behavior is to operate on the symlink, which is usually what you want. Be aware that -R combined with symlinks can cause unexpected changes if a symlink points outside the directory tree; use -R -P to avoid following any symlinks during recursion.
Common Scenarios and Examples
- Web server needs write access to an upload directory:
sudo chown -R www-data:www-data /var/www/html/uploads - Give a specific user ownership of a home directory:
sudo chown -R alice:alice /home/alice - Change only the group of a file:
sudo chown :staff /srv/shared/data.csv - Copy ownership from one file to another:
sudo chown --reference=template.txt target.txt— this applies the same owner and group astemplate.txt.
A frequent real-world task is fixing ownership after extracting a tarball as root. The extracted files may all be owned by root, but your application expects them to be owned by a service user. A single recursive chown solves that.
Safety Tips and Pitfalls
- Double-check the path before using
-R. A typo like/var /www(with a space) could change ownership on/varitself, which can break system services. - Use
--preserve-rootto protect the root directory. This option makeschown -R /fail instead of recursively changing the entire filesystem. Some distributions enable this by default. - Do not change ownership of system directories unless you know why. Files in
/etc,/usr, or/binshould generally remain owned byroot. Changing them can prevent the system from booting or services from starting. - Check the result with
ls -lorstat. After a recursive change, spot-check a few files to confirm the owner and group are what you intended.
If you accidentally change ownership of a critical system file, you can often restore it by reinstalling the package that provides it (e.g., apt reinstall package-name on Debian/Ubuntu) or by using dpkg-statoverride for special cases. But prevention is far easier—always specify the exact path and use --preserve-root if you are worried about a typo.
Alternatives and Related Commands
chown is not the only tool for managing file access. chgrp changes only the group and is essentially a shortcut for chown :group file. chmod changes permission bits, not ownership. For more fine-grained access control, you can use POSIX ACLs with setfacl and getfacl, which allow multiple users or groups to have specific permissions on a file without changing its owner. On a VPS with multiple collaborators, ACLs are sometimes a better fit than repeatedly changing ownership.
When moving files between systems, rsync with the -o and -g options can preserve owner and group if run as root. That is often the cleanest way to copy a directory tree while keeping ownership intact.
Frequently Asked Questions
What is the difference between chown and chmod?
chown changes the user and group that own a file, while chmod changes the read, write, and execute permissions for the owner, group, and others. They work together: ownership determines who the permission bits apply to, and the bits determine what that owner can do.
Can I change ownership of a file I don’t own?
No. Only root can change the owner of a file. A regular user can change the group of a file they own, but only to a group they are a member of. On a VPS, use sudo to run chown as root.
What does the colon in chown alice:staff mean?
The colon separates the owner from the group. alice:staff sets owner to alice and group to staff. If you write just :staff, only the group changes. If you write alice: (with a trailing colon), the owner changes and the group is set to the user’s login group.
How do I change ownership of all files in a directory?
Use the recursive option: sudo chown -R newowner:newgroup /path/to/directory. This changes the directory and every file and subdirectory inside it. Be careful with large directory trees—it may take a moment, but it is safe.
Why do I get “Operation not permitted� even with sudo?
This usually happens when you are trying to change ownership on a filesystem that does not support Unix ownership, such as a FAT or NTFS mount, or when the file has immutable attribute set (chattr +i). Check the filesystem type with mount and remove the immutable attribute with sudo chattr -i file if it is set.
Does chown work on symbolic links?
By default, chown changes the link itself, not the target. To change the target, use chown -h (or --dereference). Most users want the default behavior, but be aware when using -R that symlinks inside the tree are not followed unless you add -L.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.