chown and chgrp Basics - Changing File Ownership in Linux
What chown and chgrp Do
chown changes the owner of a file, and chgrp changes the group. The chown user:group syntax lets you change both at once.
Quick Summary
- Change owner →
sudo chown user file - Change owner and group →
sudo chown user:group file - Change group only →
sudo chgrp group file - Recursive (entire directory) →
sudo chown -R user:group dir/ - Always verify with
ls -labefore and after
Checking Current Ownership with ls -l
The 3rd and 4th columns of ls -l output show the owner and group.
$ ls -l /var/www/html/index.html
-rw-r--r-- 1 root root 1024 Jun 1 10:00 /var/www/html/index.html
| Column | Value | Meaning |
|---|---|---|
| 3rd | root |
Owning user |
| 4th | root |
Owning group |
chown Syntax
Use chown to change the owning user. You need sudo to change ownership on files you do not own.
Change owner only
$ sudo chown www-data /var/www/html/index.html $ ls -l /var/www/html/index.html
-rw-r--r-- 1 www-data root 1024 Jun 1 10:00 /var/www/html/index.html
Change owner and group at once
Use the user:group form to update both in a single command.
$ sudo chown www-data:www-data /var/www/html/index.html $ ls -l /var/www/html/index.html
-rw-r--r-- 1 www-data www-data 1024 Jun 1 10:00 /var/www/html/index.html
Change group only (chown :group syntax)
Leave the user part empty to change only the group while keeping the current owner.
$ sudo chown :developers /var/www/html/
The older dot syntax (chown user.group) still works on most systems, but colon is the current standard.
chgrp Syntax
chgrp is the dedicated command for changing the group. It is functionally equivalent to chown :group.
$ sudo chgrp www-data /var/www/html/index.html $ ls -l /var/www/html/index.html
-rw-r--r-- 1 root www-data 1024 Jun 1 10:00 /var/www/html/index.html
Use chgrp when you want to make the intent of changing only the group explicit in a script or command history.
Recursive Changes with -R
Add -R to apply the change to all files and subdirectories inside a directory.
$ sudo chown -R www-data:www-data /var/www/html/ $ ls -la /var/www/html/
drwxr-xr-x 2 www-data www-data 4096 Jun 1 10:00 . drwxr-xr-x 3 root root 4096 Jun 1 09:00 .. -rw-r--r-- 1 www-data www-data 1024 Jun 1 10:00 index.html -rw-r--r-- 1 www-data www-data 512 Jun 1 10:00 style.css
-R affects every file under the target directory. Check the contents with ls -la before running to avoid unintended changes.
Common Usage Patterns
Web server deployment (Nginx / Apache)
# Transfer ownership to the web server user after deploying files $ sudo chown -R www-data:www-data /var/www/myapp/
Handing off to an application user
$ sudo chown -R deploy:deploy /opt/myapp/
Reclaiming files created with sudo
Files created with sudo are owned by root. Reclaim them with:
$ sudo chown $USER:$USER ~/myfile.txt
$USER expands to your current login name automatically.
Common Errors and Fixes
Operation not permitted
chown: changing ownership of 'file.txt': Operation not permitted
Cause: Only root can change ownership. Run with sudo.
$ sudo chown user:group file.txt
invalid user / invalid group
chown: invalid user: 'webmaster' chgrp: invalid group: 'webteam'
Cause: The specified user or group does not exist on the system.
# List users $ getent passwd | cut -d: -f1 # List groups $ getent group | cut -d: -f1
Summary
| Command | What Changes |
|---|---|
chown user file |
Owner only |
chown :group file |
Group only |
chown user:group file |
Owner and group |
chgrp group file |
Group only |
-R flag |
Applies recursively |
Safe workflow
# Check before ls -la /path/to/target # Apply change sudo chown -R user:group /path/to/target # Verify after ls -la /path/to/target