File Permission in Linux

File Permission in Linux

Day06

About File Permission :

File permissions in Linux are a way to control access to files and directories. They determine who can read, write, and execute a file or directory. Linux uses a three-level permission system for files and directories, which applies to three different groups: the owner, the group, and others.

Each file and directory has three types of permissions:

  1. Read (r): Allows the file to be viewed and its contents read. For directories, it allows the contents of the directory to be listed.

  2. Write (w): Allows the file to be modified, including creating, deleting, and renaming the file. For directories, it allows files to be created, deleted, or renamed within the directory.

  3. Execute (x): Allows the file to be executed as a program or script. For directories, it allows accessing and traversing the directory.

The permissions are specified for each group mentioned earlier: owner, group, and others.

Example:

In this example, the file myfile.txt has the following permissions:

  • The owner has read and write permissions (rw-).

  • The group has read-only permissions (r--).

  • Others have read-only permissions (r--).

Changing file permission using chmod

With the help of the change mode ‘chmod’ command, we can set the permissions such as read, write, and execute on a directory or file for the owner, user, and the group.

1. Numeric mode

In a numeric mode, file permissions do not denote as characters but as a three-digit octal number. The following table provides the numbers for all permission types.

for example:

chmod 764 sample

chmod 764 and checking permission. In the above command, we have changed the file permissions to 764. 764 represents the following:

  • The owner can read, write, and execute

  • The user group can read and write

  • Any user can only read

  1. Symbolic mode:

    The symbolic representation uses letters to represent the permissions and can be modified using operators like + (add), - (remove), or = (set explicitly). For example:

for example:

#chmod u+x myfile.txt

here user have permission of execute the myfile.txt

Changing ownership and group

For changing the ownership of a directory or file, use the command below:

# chown user

If you want to change the user along with the group for a directory or file, use the command below

#chown user: group filename

If you wish to change group owner only, use the command below

# chgrp group_name filename