How To Use chmod and chown Command in Linux

How To Use chmod and chown Command in Linux

The chmod command is used to change the permissions of a file or directory in Linux. The chown command is used to change the ownership of a file or directory.

Using `chmod`

To use `chmod`, you will need to specify the permission settings you want to apply, and the file or directory you want to modify.

There are two ways to specify the permission settings:

  1. Using octal notation: This method uses a three-digit number to represent the permissions for the owner, group, and others. Each digit is the sum of the permissions for that category. For example, 644 represents read and write permissions for the owner, and read-only permissions for the group and others.

  2. Using symbolic notation: This method uses characters to represent the permissions for the owner, group, and others. r represents read permission, w represents write permission, and x represents execute permission. You can also use + to add a permission, - to remove a permission, and = to set a permission. For example, u+x would add execute permission for the owner.

Here are some examples of using chmod:

# Change the permissions of a file to 644 using octal notation
chmod 644 file.txt

# Change the permissions of a directory to 755 using octal notation
chmod 755 directory

# Add execute permission for the owner and group, and remove write permission for others using symbolic notation
chmod ug+x,o-w file.txt
Sour‮.www:ec‬lautturi.com

Using `chown`

To use `chown`, you will need to specify the user and group that you want to own the file or directory, and the file or directory you want to modify.

Here are some examples of using chown:

# Change the owner of a file to user1
chown user1 file.txt

# Change the group ownership of a file to group1
chown :group1 file.txt

# Change the owner and group ownership of a directory to user1 and group1
chown user1:group1 directory

It's important to note that you will need to have appropriate permissions to use these commands. For example, you will need to be the owner of the file or directory, or have superuser privileges, to use chmod or chown.

Created Time:2017-10-29 22:08:34  Author:lautturi