Change File Owner and Group

How to change files and folders owner and group on Linux and Unix
August 7, 2024 by
Change File Owner and Group
Hamed Mohammadi
| No comments yet

The chown command is a powerful tool used to modify ownership and group ownership of files and directories within a Unix-like operating system. To execute this command, a user must have superuser privileges, typically associated with the root account.

The basic syntax for the chown command is as follows:

# chown [new_owner][:new_group] file_or_directory

  • new_owner: Specifies the new owner of the file or directory. This can be a username or a user ID number.

  • new_group: (Optional) Specifies the new group owner of the file or directory. This can be a group name or a group ID number.

  • file_or_directory: Represents the target file or directory whose ownership you want to change.

It's important to note that using chown incorrectly can have significant security implications. Exercise caution when modifying file ownership.

chown can change the file owner and/or the file group owner depending on the first argument of the command. For example, some chown Argument Examples:

  • hamed, Changes the ownership of the file from its current owner to user hamed.

  • hamed:hamed, Changes the ownership of the file from its current owner to user hamed and changes the file group owner to group hamed.

  • :admins, Changes the group owner to the group admins. The file owner is unchanged.

  • user: , Changes the file owner from the current owner to user user and changes the group owner to the login group of user user.

Let’s say we have two users: hamed, who has access to superuser privileges, and fati, who does not. User hamed wants to copy a file from his home directory to the home directory of fati. Because hamed wants fati to be able to edit the file, hamed changes the ownership of the copied file from hamed to fati.

[hamed@server1 ~]$ sudo cp myfile.txt ~fati 
Password: 
[hamed@server1 ~]$ sudo ls -l ~fati/myfile.txt 
-rw-r--r-- 1 root root root 2024-06-25 08:30 /home/fati/myfile.txt
[hamed@server1 ~]$ sudo chown fati: ~fati/myfile.txt 
[hamed@server1 ~]$ sudo ls -l ~fati/myfile.txt 
-rw-r--r-- 1 fati fati fati 2024-06-25 14:31 /home/fati/myfile.txt

Here we see user hamed copy the file from his directory to the home directory of user fati. Next, hamed changes the ownership of the file from root (a result of using sudo) to fati. Using the trailing colon in the first argument, hamed also changed the group ownership of the file to the login group of fati, which happens to be group tony.

Notice that after the first use of sudo, hamed was not prompted for hhis password. This is because sudo, in most configurations, “trusts” you for several minutes until its timer runs out.

To illustrate a practical use case for chown, consider a production environment where a Django project is deployed using the Gunicorn service. Let's say the Gunicorn service runs as the user hamed and the group www-data to allow the web server (like Nginx) to access static and media files.

In this scenario, it's crucial that the www-data group has read and write permissions to the relevant directories. To achieve this, we can add the www-data group to the hamed user's group list. This can be accomplished using the following command:

# usermod -aG www-data hamed

This command appends the www-data group to the existing groups of the user hamed.

Note: Always exercise caution when modifying group memberships, as incorrect changes can impact system security.


Change File Owner and Group
Hamed Mohammadi August 7, 2024
Share this post
Archive

Please visit our blog at:

https://zehabsd.com/blog

A platform for Flash Stories:

https://readflashy.com

A platform for Persian Literature Lovers:

https://sarayesokhan.com

Sign in to leave a comment