Share


Chown vs Chmod


By gobrain

Jul 3rd, 2024

chmod and chown are both commands used in Linux to manage file access, but they control different aspects. Let's take a look at differences between them to understand their specific uses.

chown (change owner)

chown is used to modify the ownership of a file or directory. This specifies who has the ultimate control over the file's permissions.

Here's a breakdown of what chown does:

  • Changes Ownership: Assigns a new user (and optionally a group) to be the owner of a file or directory.
  • Controls Permissions: By changing ownership, you indirectly control who can set permissions for the file using the chmod command. The owner has the most control over permissions.

Here is an anology:

The owner of the house controls who gets to enter and what they can do inside. chown is like changing who owns the house, giving them the authority to decide access.

chmod (change mode)

chmod, on the other hand, defines the access permissions for a file or directory. These permissions dictate what actions (read, write, execute) different users (owner, group, others) can perform on the file.

There are three basic permission types:

  • Read (r): Allows users to view the contents of a file or list the contents of a directory.
  • Write (w): Allows users to modify the contents of a file or create/delete files within a directory.
  • Execute (x): Allows users to run a script or program (for files) or enter a directory.

Permissions are assigned to three categories of users:

  • Owner: The user who created the file or directory.
  • Group: A group of users associated with the file/directory.
  • Others: Everyone else on the system.

Here is how chmod works:

chmod uses a specific syntax to define the permissions for each user category. There are two main ways to specify permissions:

  • Symbolic mode: Uses letters (r, w, x) and operators (+, -) to represent permissions (easier to remember).
  • Octal mode: Uses a three-digit number (combination of permission values) for a more compact representation (often used by experienced users).

For example,

The following command grants read and write permissions to the owner, and no permissions to the group and others (common for sensitive files):

chmod 600 mysecretfile.txt

Here is the explanation:

  • 6: Octal representation of permissions (read: 4, write: 2 -> 4+2 = 6)
  • 0: No permissions for others (group and everyone else)

chmod vs. chown

In simpler terms, chown assigns ownership (who controls access), chmod sets permissions (what kind of access is allowed). To back the house analogy above,

  • chown decides directly who owns the house and indirectly what others can do in house
  • chmod determines who can enter the house and what they can do inside (read furniture, write on walls, etc.).

When to use each:

  • Use chown when you need to change who has control over a file, granting them the ability to set permissions with chmod.
  • Use chmod to adjust the read, write, and execute permissions for the owner, group, and others assigned by chown.

Conclusion

In conclusion, chmod and chown are two important commands in Linux used for managing file permissions and ownership. In this article we have covered differences between them.

Thank you for reading.