Skip to content

Latest commit

 

History

History
144 lines (109 loc) · 3.48 KB

permissions.md

File metadata and controls

144 lines (109 loc) · 3.48 KB

file permissions


Slide: Understanding File Permissions in Linux

  • What are File Permissions?
    • File permissions determine who can read, write, or execute a file or directory.
    • Every file has three permission types:
      • Read (r): Ability to view the contents of a file or list a directory’s contents.
      • Write (w): Ability to modify or delete a file or its contents.
      • Execute (x): Ability to run a file as a program or access a directory.

Slide: File Permission Structure

  • Viewing Permissions
    Use ls -l to see permissions for files and directories:

    $ ls -l

    Example output:
    -rwxr-xr-- 1 user group 4096 Oct 11 10:15 script.sh

    • Permission Breakdown:
      • -rwxr-xr--
        • The first character: - (file) or d (directory).
        • Owner: rwx (read, write, execute for the file owner).
        • Group: r-x (read, execute for users in the file's group).
        • Others: r-- (read-only for all others).

Slide: Changing File Permissions with chmod

  • Basic Syntax:

    chmod <permissions> <file>
  • 1. Using Numeric Mode

    • Permissions can be represented by numbers:
      • 4 = Read (r)
      • 2 = Write (w)
      • 1 = Execute (x)
    • Combine these for different roles:
      • Owner | Group | Others: 777 (rwx for all), 644 (rw- for owner, r-- for group and others)

    Example:

    chmod 755 script.sh

    This gives the owner full access (rwx), while group and others get read and execute access (r-x).

  • 2. Using Symbolic Mode

    • Permissions can also be changed with symbols:
      • u = user/owner
      • g = group
      • o = others
      • a = all (user, group, others)

    Example:

    chmod u+x script.sh

    This adds execute permission to the owner.

    Example:

    chmod g-w file.txt

    This removes write permission for the group.


Slide: Common chmod Examples

  1. Give execute permission to all users:

    chmod a+x script.sh
  2. Remove write permission for others:

    chmod o-w file.txt
  3. Set full permissions for the owner, read-only for group and others:

    chmod 744 file.txt

Slide: Changing File Ownership with chown

  • chown allows you to change the owner and group of a file.

    chown <owner>:<group> <file>
  • Example: Change the owner to user1 and group to staff:

    chown user1:staff file.txt
  • Change ownership of a directory recursively:

    chown -R user1:staff /path/to/directory

Slide: Viewing Effective Permissions with stat

  • Use stat to view detailed file permissions and other metadata:
    stat file.txt
    This will show the numeric mode (e.g., 0644) and other attributes of the file.

Slide: Understanding Special Permissions

  • Setuid (Set User ID): Allows a file to be executed with the permissions of its owner.

    chmod u+s file.sh
  • Setgid (Set Group ID): Ensures new files created in a directory inherit the group of the directory.

    chmod g+s directory/
  • Sticky Bit: Prevents users from deleting files in a directory unless they own them.

    chmod +t /shared_directory

This expanded section provides an in-depth understanding of file permissions, chmod, chown, and special permission settings in Linux.