Onjsdev

Share


chmod vs. chown in Linux


By onjsdev

Mar 31st, 2024

When working with Linux, there are two essential commands for managing file permissions and ownership: chmod and chown. In this article, we will take a look at differences between chmod and chown to help you understand them better.

chmod - Changing File Permissions

The chmod command stands for change mode and it helps you modify the permissions of a file or directory so that you can determines who can access, modify, or execute your system files or folders.

The Basics

  • To use chmod, you need to specify the file or directory you want to change and the permissions you want to set.
  • Permissions are represented by three digits: r for read, w for write, and x for execute.
  • The three digits correspond to three groups: owner, group, and others.

Understanding Digits

In chmod command, each number corresponds to a specific set of permissions for different categories of users. Here's what the numbers mean:

  • Read (r): Represented by the number 4. This permission allows a user to view the content of a file or list the contents of a directory.
  • Write (w): Represented by the number 2. This permission allows a user to modify or delete a file or create, delete, or rename files within a directory.
  • Execute (x): Represented by the number 1. This permission allows a user to run or execute a file (e.g., a script or binary) or access files within a directory.

Examples

  • To give the owner read and write permissions: chmod 600 file.txt
  • To allow the owner to read and write and the group to read: chmod 640 file.txt
  • To permit everyone to read and execute while keeping write permissions only for the owner: chmod 755 script.sh

chown - Changing File Ownership

The chown command stands for "change owner" and lets you modify the owner of a file or directory which determines who has control over that file, including the ability to change permissions and delete the file. The owner is the user who has control over the file.

The Basics

  • To use chown, you specify the new owner and the file or directory.
  • You can also change the group associated with the file, but this is optional.

Examples

  • To change the owner of a file to a user named "newuser": chown newuser file.txt
  • To change both the owner and group: chown newuser:newgroup file.txt
  • To change only the group: chown :newgroup file.txt

Combining chmod and chown

You often need to use both chmod and chown to manage files effectively. For instance, if you want to restrict access to a file and also change its ownership, you would use both commands.

Example

Suppose you have a file called secrets.txt and you want to make it readable only by the owner (user: alice) and change its owner to bob. You can do this with the following commands:

chmod 600 secrets.txt
chown bob secrets.txt

This will set the file permissions to allow only the owner (alice) to read and write the file and change the owner to bob.

However, note that the ownership of the file "secrets.txt" has been changed to "bob" using the chown command. Therefore, "bob" will be the new owner and have full control over the file, including the ability to change its permissions. "alice" will no longer be the owner of the file, and her control over the file has been transferred to "bob."

If you want "alice" to regain control or ownership of the file, you would need to use the chown command to change the ownership back to "alice."

Conclusion

In conclusion, chmod and chown are essential commands for managing file permissions and ownership in Linux. By using them wisely, you can ensure the security and proper functioning of your system. Understanding when and how to use these commands is fundamental for any Linux user.

Thank you for reading.