Monday, 10 May 2021

Linux Permission Overview

In Linux, every file and directory has permissions that control who can read, write, and execute the file or access the directory. These permissions are represented by a series of letters and symbols.

There are three types of permissions in Linux:

  • read (r) - Allows a user to view the contents of a file or list the files in a directory.
  • write (w) - Allows a user to modify the contents of a file or add or delete files in a directory.
  • execute (x) - Allows a user to run a file as a program or access files in a directory.

These permissions are assigned to three groups of users:

  • user (u) - The owner of the file or directory.
  • group (g) - The group that the file or directory belongs to.
  • others (o) - Any user that is not the owner or a member of the group.

You can use the ls -l command to view the permissions of files and directories in the current directory. The output will be a list of files and directories, with the permissions for each item listed in the first column. The permissions are represented by a series of letters and symbols, where the first three characters represent the permissions for the user, the next three characters represent the permissions for the group, and the last three characters represent the permissions for others.

Here is an example of the ls -l output:

drwxrwxr-x  2 user group   4096 Jan  1 12:00 myfolder
-rw-rw-r--  1 user group    123 Jan  1 12:00 myfile.txt

In this example, the first character din the first line shows that "myfolder" is a directory.

The next three characters rwxshow that the user has read, write, and execute permissions on the directory. The next three characters rwxshow that the group has read, write, and execute permissions on the directory. The last three characters r-xshow that others have read and execute permissions on the directory but do not have write permissions.

The first character - in the second line shows that "myfile.txt" is a regular file. The next three characters rw-show that the user has read and write permissions on the file.

The next three characters rw-show that the group has read and write permissions on the file. The last three characters r--show that others have only read permissions on the file but do not have write or execute permissions.

You can use the chmod command to change the permissions of a file or directory. The chmod command takes two arguments: the first is the permissions you want to set, and the second is the file or directory you want to change.

There are two ways to specify the permissions in the chmod command:

  1. Symbolic mode: This method uses symbols to specify the permissions. The symbols used are:

    • r (read)
    • w (write)
    • x (execute)

      The symbolic mode also uses the following symbols to specify which users the permissions apply to:

    • u (user/owner)

    • g (group)
    • o (others)
    • a (all)

To set permissions using the chmod command, you can use a combination of the above options and the following symbols:

    • (plus): Adds the permission.
    • (minus): Removes the permission.
  • = (equals): Sets the permission exactly as specified.

Example:

  1. To give the owner, group, and others read and write permissions on a file:
chmod a+rw file.txt

In this example, represents all users (owner, group, and others), and +rwadds read and write permissions.

b. To give the owner execute permission and remove write permission on a file:

chmod u+x,g-w file.txt

In this example, u+xadds execute permission for the owner, and g-wremoves write permission for the group.

c. To give the owner read and write permission, and remove all permissions for the group and others:

chmod u+rw,g-rwx,o-rwx file.txt

In this example, u+rwadds read and write permission for the owner, g-rwxremoves all permissions for the group, and o-rwxremoves all permissions for others.

Remember that the order of the symbolic mode options matters. The permissions are applied in the order they are specified, so make sure to specify the options in the correct order to achieve the desired result.

  1. Numeric mode: This method uses numeric values to specify the permissions. Each permission is assigned a numeric value:
  2. 4 (read)
  3. 2 (write)
  4. 1 (execute)

You can add these values to specify the permissions you want to set. For example, to give the owner read, write, and execute permissions on a file, you would use the command:

chmod 700 file.txt

In this example, the first digit (7) represents the owner's permissions (4 + 2 + 1), and the remaining digits (0) represent the group and other users' permissions.

let’s take another example:

To give the owner read and execute permissions, and remove all permissions for the group and others on a directory:

chmod 500 directory/

In this example, the first digit (5) represents the owner's permissions (4 + 1 = read and execute), and the remaining digits (00) represent the group and others' permissions.

No comments:

Post a Comment