top of page

File and Directory Permissions in Linux

Writer: compnomicscompnomics

Understanding file and directory permissions is crucial for maintaining security and control in Linux. These permissions dictate who can read, write, and execute files and directories.


Understanding Permissions:


Linux uses three permission types:

  • Read (r): Allows viewing file contents or listing directory contents.

  • Write (w): Allows modifying file contents or creating/deleting files within a directory.

  • Execute (x): Allows executing a file (if it's a program) or entering a directory.


These permissions are assigned to three categories:

  • User (u): The file/directory owner.

  • Group (g): The group associated with the file/directory.

  • Others (o): All other users on the system.


Permission Representation:

Permissions are represented in two ways:

  • Symbolic: rwx (read, write, execute)

  • Octal: A three-digit number (e.g., 755)

Symbolic Method:

  • + Adds a permission.

  • - Removes a permission.

  • = Sets permissions exactly.

  • Example: chmod u+x file.sh (adds execute permission for the user)

  • Example: chmod g-w directory (removes write permission for the group)

  • Example: chmod o=r file.txt (sets read-only permission for others)

Octal Method:

  • Each permission type (r, w, x) has a corresponding octal value:

    • r = 4

    • w = 2

    • x = 1

  • To calculate the octal value for a category, add the values of the desired permissions.

  • Example: rwx = 4 + 2 + 1 = 7

  • Example: rw- = 4 + 2 + 0 = 6

  • Example: r-x = 4 + 0 + 1 = 5

  • Example: chmod 755 file.sh (sets rwx for user, rx for group and others)



Detailed Guide with Examples:

  1. View Current Permissions:

$ ls -l file.txt
Output (example): -rw-r--r-- 1 user user 1024 Aug 15 10:00 file.txt
  • Add Execute Permission (Symbolic):

$ chmod u+x file.sh
  1. Remove Write Permission (Symbolic):

$ chmod g-w directory
  1. Set Read-Only for Others (Symbolic):

$ chmod o=r file.txt
  1. Set Permissions (Octal):

$ chmod 755 file.sh (rwx for user, rx for group and others)
$ chmod 644 file.txt (rw- for user, r-- for group and others)
  1. Recursive Permissions:

$ chmod -R 755 directory (applies permissions recursively to all files and subdirectories)

Practice Session 1: With Example Commands

  1. Create a file and directory:

$ touch testfile.txt
$ mkdir testdir
  1. View current permissions of testfile.txt:

$ ls -l testfile.txt
  1. Add execute permission for the user to testfile.txt:

$ chmod u+x testfile.txt
  1. Set permissions of testdir to 750 (rwx for user, rx for group, no permissions for others):

$ chmod 750 testdir
  1. Remove read permissions for others from testfile.txt:

$ chmod o-r testfile.txt
  1. recursively add read and execute permissions to all files within testdir, for the group and others.

$ chmod -R g+rx,o+rx testdir

Practice Session 2: Without Example Commands

  1. Create a file named "data.txt" and a directory named "projects".

  2. Change the permissions of "data.txt" so that the owner has read and write permissions, the group has read-only permissions, and others have no permissions.

  3. Change the permissions of "projects" so that the owner has full permissions (read, write, execute), the group has read and execute permissions, and others have read-only permissions.

  4. Remove write permissions for the group from "data.txt".

  5. Add execute permission for the user to "data.txt".

  6. Recursively remove all write permissions for others within the "projects" directory.

  7. Give the group write permissions within the projects directory.


By mastering file and directory permissions, you gain greater control and security over your Linux system.

 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page