Setting Permissions with chown and chmod

When working with files and directories in Linux, it's important to understand how to set permissions. Permissions define who can access and modify files and directories on a system. The chown and chmod commands are essential tools for managing ownership and access rights.

Understanding Linux File Permissions

In Linux, each file and directory has three types of permissions: read (r), write (w), and execute (x). These permissions can be set for three different categories of users owner of the file or directory, group to which the file or directory belongs, and all other users.

Linux File Permission Structure - rwx rwx rwx File Type Owner (rwx) Group (rwx) Others (rwx) - = regular file d = directory r = read (4) w = write (2) x = execute (1)

The read permission allows users to view contents of a file or directory. The write permission allows users to modify contents of a file or directory. The execute permission allows users to run a file or access a directory.

Using chown Command

The chown command is used to change the owner of a file or directory. To change the owner of a file or directory, you must have root privileges or be the current owner of the file or directory.

The syntax for the chown command is as follows

chown [OPTIONS] [NEW_OWNER][:NEW_GROUP] [FILE_OR_DIRECTORY]

Examples of chown Usage

The following example demonstrates how to change the owner of a file named "example.txt" to a user named "john"

chown john example.txt

To change both owner and group simultaneously

chown john:developers example.txt

You can also use the chown command to change the owner of a directory and all of its contents recursively. The following example demonstrates how to change the owner of a directory named "example" and all of its contents to a user named "john"

chown -R john example

The -R option tells chown to change the owner of the directory and all of its contents recursively.

Using chmod Command

The chmod command is used to change permissions of a file or directory. To change permissions of a file or directory, you must have appropriate permissions to do so.

The syntax for the chmod command is as follows

chmod [OPTIONS] [PERMISSIONS] [FILE_OR_DIRECTORY]

Numeric Permission Values

The following table shows different values that can be used with the chmod command to set permissions

Value Binary Permissions Description
0 000 --- No permission
1 001 --x Execute permission
2 010 -w- Write permission
3 011 -wx Write and execute permission
4 100 r-- Read permission
5 101 r-x Read and execute permission
6 110 rw- Read and write permission
7 111 rwx Read, write, and execute permission

Examples of chmod Usage

You can set permissions for owner, group, and all other users using a combination of these values. The following example demonstrates how to set read, write, and execute permissions for the owner, and read and execute permissions for group and all other users for a file named "example.txt"

chmod 755 example.txt

In this example, the owner of "example.txt" file will have read, write, and execute permissions (7), while group and all other users will have read and execute permissions (5).

You can also use the chmod command to set permissions for a directory and all of its contents recursively

chmod -R 755 example

Combining chown and chmod Commands

To effectively manage file and directory permissions, it is often necessary to use both chown and chmod commands in combination. For example, if you want to change the owner of a file or directory and also set permissions for the new owner, you can use chown and chmod commands together.

The following example demonstrates how to change the owner of a file named "example.txt" to a user named "john" and set read, write, and execute permissions for the new owner only

chown john example.txt
chmod 700 example.txt

In this example, "john" user will become the new owner of "example.txt" file, and will have read, write, and execute permissions. The group and all other users will have no permissions.

For directories and their contents recursively

chown -R john example
chmod -R 700 example

Common Permission Patterns

Numeric Symbolic Use Case
644 rw-r--r-- Regular files (owner can read/write, others can read)
755 rwxr-xr-x Directories and executable files
700 rwx------ Private files (owner access only)
666 rw-rw-rw- Files that everyone can read and write

Conclusion

The chown and chmod commands are fundamental tools for managing file and directory permissions in Linux. Understanding how to properly set ownership and permissions is crucial for maintaining system security and controlling access to files. Always be cautious when changing permissions on system files, and follow the principle of least privilege to maintain a secure environment.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements