Using chown command in Linux
The chown command in Ubuntu is used to change the ownership of files and directories. It stands for “change owner.” This command allows you to modify the user and group ownership of a specified file or directory.
Examples and Usage
- Change the owner of a file:
chown newowner filename
- Change the owner and group of a file:
chown newowner:newgroup filename
- Change the group of a file:
chown :newgroup filename
- Recursively change ownership of a directory and its contents:
chown -R newowner:newgroup directory
Webserver example
In case you need to adapt a file to be accesses by web server, you need to have the following permission:
chown -R www-data:www-data *
Troubleshooting
To check the ownership you can type:
ls -ltrh
Output:
root@webserver:/srv/www/wordpress# ls -ltrh
total 248K
-rw-r--r-- 1 www-data www-data 351 Feb 6 2020 wp-blog-header.php
-rw-r--r-- 1 www-data www-data 405 Feb 6 2020 index.php
-rw-r--r-- 1 www-data www-data 2.5K Nov 26 2022 wp-links-opml.php
-rw-r--r-- 1 www-data www-data 5.6K May 30 2023 wp-cron.php
-rw-r--r-- 1 www-data www-data 2.3K Jun 14 2023 wp-comments-post.php
-rw-r--r-- 1 www-data www-data 34K Jun 19 2023 wp-signup.php
-rw-r--r-- 1 www-data www-data 4.8K Jun 22 2023 wp-trackback.php
-rw-r--r-- 1 www-data www-data 3.9K Jul 16 2023 wp-load.php
-rw-r--r-- 1 www-data www-data 8.4K Sep 16 2023 wp-mail.php
drwxr-xr-x 9 www-data www-data 4.0K Nov 9 2023 wp-admin
-rw-r--r-- 1 www-data www-data 325 Jan 12 07:27 wordfence-waf.php
-rw-r--r-- 1 root root 325 Jan 12 07:40 wordfence-waf.php.bkp
-rw-r--r-- 1 www-data www-data 3.2K Apr 2 20:01 xmlrpc.php
-rw-r--r-- 1 www-data www-data 20K Apr 2 20:01 license.txt
drwxr-xr-x 30 www-data www-data 16K Apr 2 20:01 wp-includes
-rw-r--r-- 1 www-data www-data 28K Apr 2 20:01 wp-settings.php
-rw-r--r-- 1 www-data www-data 3.0K Apr 2 20:01 wp-config-sample.php
-rw-r--r-- 1 www-data www-data 7.3K Apr 2 20:01 wp-activate.php
-rw-r--r-- 1 www-data www-data 50K Apr 2 20:01 wp-login.php
-rw-r--r-- 1 www-data www-data 7.3K Jun 6 08:18 readme.html
-rw-r--r-- 1 www-data www-data 3.6K Jun 19 11:34 wp-config.php
drwxr-xr-x 11 www-data www-data 4.0K Jun 21 19:55 wp-content