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.

In this article, we will go through how to use chown and chmod commands to set permissions on files and directories.

Understanding Linux File Permissions

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

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

Each file and directory also has an owner and a group. owner is user who created file or directory, and group is a collection of users who share a common set of permissions.

Using chown Command

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

The syntax for chown command is as follows −

chown [OPTIONS] [NEW_OWNER] [FILE_OR_DIRECTORY]

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

chown john example.txt

In this example, "john" user will become new owner of "example.txt" file.

You can also use chown command to change owner of a directory and all of its contents. following example demonstrates how to change 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 owner of 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 chmod command is as follows −

chmod [OPTIONS] [PERMISSIONS] [FILE_OR_DIRECTORY]

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

Value

Meaning

0

No permission

1

Execute permission

2

Write permission

3

Write and execute permission

4

Read permission

5

Read and execute permission

6

Read and write permission

7

Read, write, and execute permission

You can set permissions for owner, group, and all other users using a combination of these values. following example demonstrates how to set read, write, and execute permissions for 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, owner of "example.txt" file will have read, write, and execute permissions, while group and all other users will have read and execute permissions.

You can also use chmod command to set permissions for a directory and all of its contents. following example demonstrates how to set read, write, and execute permissions for owner, and read and execute permissions for group and all other users for a directory named "example" −

chmod -R 755 example

The "-R" option tells chown and chmod to set permission in recursively.

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 owner of a file or directory and also set permissions for new owner, you can use chown and chmod commands in combination. following example demonstrates how to change owner of a file named "example.txt" to a user named "john" and set read, write, and execute permissions for new owner −

chown john example.txt
chmod 700 example.txt

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

You can also use chown and chmod commands in combination to change owner and set permissions for a directory and all of its contents. following example demonstrates how to change owner of a directory named "example" and all of its contents to a user named "john" and set read, write, and execute permissions for new owner −

chown -R john example
chmod -R 700 example

In this example, "john" user will become new owner of "example" directory and all of its contents, and will have read, write, and execute permissions. group and all other users will have no permissions.

Conclusion

In this article, we have explored how to use chown and chmod commands to set permissions on files and directories in Linux. By understanding different types of permissions and how to use these commands, you can effectively manage file and directory permissions on your Linux system.

Remember that setting permissions is an important part of maintaining security and integrity of your system. It is important to understand risks associated with granting too many permissions, and to be careful when changing permissions on important files and directories.

As always, be sure to consult official documentation for your specific Linux distribution and version for more detailed information on how to use these commands.

Updated on: 03-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements