How to Use the chmod Command on Linux

The chmod command is used in Linux and other Unix-like operating systems to change the permissions of files and directories. Here’s how to use it:

  1. Open the Terminal: Open a terminal window on your Linux system.
  2. Identify the file or directory you want to change permissions for: Use the ls command to list the contents of the current directory and identify the file or directory you want to modify the permissions for.
  3. Determine the permissions you want to set: Permissions are set using a three-digit number. The first digit specifies the permissions for the owner of the file or directory, the second digit specifies the permissions for the group that the file or directory belongs to, and the third digit specifies the permissions for everyone else. Each digit is a sum of three numbers, where 4 represents read access, 2 represents write access, and 1 represents execute access. For example, the number 755 would give the owner full permissions, the group read and execute permissions, and everyone else read and execute permissions.
  4. Use the chmod command to set the permissions: The basic syntax of the chmod command is as follows:
bash
chmod [options] permissions file/directory

For example, to give the owner full permissions, the group read and execute permissions, and everyone else read and execute permissions to a file named example.txt, you would run the following command:

bash
chmod 755 example.txt

You can also use the letter notation to set the permissions. For example, to give the owner full permissions, the group read and execute permissions, and everyone else read and execute permissions to a file named example.txt, you would run the following command:

bash
chmod u=rwx,g=rx,o=rx example.txt

In this example, u stands for user, g stands for group, and o stands for others. The = symbol means to set the permissions, and rwx means read, write, and execute.

  1. Verify the permissions: Use the ls -l command to view the file or directory and verify that the permissions have been set correctly.

That’s it! You’ve now successfully used the chmod command to change the permissions of a file or directory on your Linux system.