Files Operation: Change Ownership of The Files
Linux Files Operation: chown (Change Ownership of The Files) plays a key role in security and system management. Each file is associated with a specific owner and group, dictating who has access and what permissions they have. At times, you may need to transfer ownership of files, whether for administrative tasks, collaboration, or system maintenance. This is where the chown
command comes into play.
In this post, we’ll explore how to use chown
to change the ownership of files and directories on a Linux system. We’ll cover basic and advanced usage, from changing ownership to managing user and group associations effectively. By the end, you’ll have a solid understanding of how to control file permissions and ownership, ensuring proper access management in your Linux environment.
Let’s dive in and master file ownership with chown
!
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 *
chown
: Changes the ownership of files or directories.-R
: Recursively applies ownership changes to all files and subdirectories within the current directory.www-data:www-data
: Specifies the new owner (www-data
) and group (www-data
). This is often used for web server files to grant the server process the necessary access to manage those files.*
: The wildcard character selects all files and directories in the current directory to apply the ownership change.
Troubleshooting
To check the oownership,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
Linux Files Operation: chwon (Change Ownership of The Files)