Linux File Permissions and the chmod Command

Overview of Permissions in Linux File System

  • The chmod command is used to change permissions of files and directories in Linux.
  • Understanding permissions is vital for file security and access control.

Viewing Permissions

  • Use ls -lha to view detailed file listings
  • -l: long listing format
  • -h: human-readable file sizes
  • -a: show all files, including hidden files
  • Hidden files begin with a period (.)

Permission String Breakdown

  • The permission string consists of four sections:
  1. File Type:
    • -: signifies a normal file
    • d: signifies a directory
  2. User Permissions: Three characters indicating the permissions for the file owner. (e.g., rwx)
  3. Group Permissions: Three characters for the group that owns the file.
  4. Other (World) Permissions: Three characters for all other users.
Permission Characters and Their Meanings
  • r: read
  • w: write
  • x: execute
  • A hyphen - indicates that a permission is not granted.

Example Permissions Structure

  • For a permission string like -rwxr-xr--:
  • rwx for the user (read, write, execute)
  • r-x for the group (read, execute)
  • r-- for others (read only)
Understanding Ownership
  • Ownership of files: User (often root), Group.
  • Example: root owns ansiblebootstrap.sh.

Changing Permissions with chmod

  1. Basic Command Structure: chmod [options] [permissions] [filename]
  2. Adding Permissions: Use +
  • Example: chmod g+w ansiblebootstrap.sh (adds write permission for group)
  1. Removing Permissions: Use -
  • Example: chmod g-w ansiblebootstrap.sh (removes write permission for group)
  1. Copying Permissions: Use = to set permissions equal to another category.
  • Example: chmod g=u ansiblebootstrap.sh (group permissions set equal to user permissions)
  1. Combining Commands: Multiple operations can be applied in one command.
  • Example: chmod g-w,u-x,o-r ansiblebootstrap.sh

Numeric Representation of Permissions

  • Permissions can also be set using a three-digit numerical system:
  • Values:
    • r = 4
    • w = 2
    • x = 1
  • Example: chmod 644 ansiblebootstrap.sh
  • First digit for user: 6 (4+2) = rw-
  • Second digit for group: 4 = r--
  • Third digit for others: 4 = r--

File vs. Directory Permissions

  • Files:

  • r: View contents, read the file.

  • w: Modify contents (write).

  • x: Execute the file if it's a script or program.

  • Directories:

  • r: List contents of the directory.

  • w: Add or remove files in the directory.

  • x: Navigate into the directory (using cd). Without it, you get a "Permission denied" error.

Summary

  • Understanding and modifying file permissions is essential for effective file system management in Linux.
  • The chmod command, using both symbolic and numeric methods, allows for flexible permission settings, promoting security and access management.