`

Introduction to Linux


Kernel & Shell

Kernel Link

Aspect Kernel Shell
Role Core part of the OS, manages hardware and resources User interface for interacting with the OS (GUI OR CLI)
Primary Function Resource management, process management, security Command execution, scripting, user interaction
Interaction Directly interacts with hardware Interacts with users, translates commands to OS actions
Examples Linux Kernel, Windows NT Kernel, macOS XNU Kernel Bash, Windows Command Prompt

Highlights

Understanding Key Linux Directories

Directories

Directory Description
/ The root directory. The top level of the file system.
/bin Essential user command binaries needed for single-user mode and for system repair.
/boot Contains the Linux kernel, initial RAM disk image (for drivers needed at boot time), and boot loader configuration files.
/dev Contains device files that represent hardware components.
/etc Configuration files for the system and installed applications.
/home User home directories, where each user stores their personal files and directories.
/lib Essential shared libraries and kernel modules needed for booting and running the system.
/media Mount points for removable media such as USB drives and CDs.
/mnt Temporary mount points for filesystems, typically used for manual mounts.
/opt Optional application software packages. example homebrew
/proc Virtual filesystem providing information about system processes and other kernel information.
/root Home directory for the root user.
/sbin System binaries (administration tools) for the superuser.
/tmp Temporary files created by users and applications.
/var Variable data files like logs, databases, and mail spools.

Reading the ls -l Command

The ls -l command in Unix/Linux systems lists files and directories in a long format, providing detailed information about each item. Here’s an explanation of the output structure:

Example Output

-rw-r--r-- 1 user group 1234 Jul 29 10:00 example.txt

Breakdown of Each Field

  1. File Type and Permissions (-rw-r--r--):
    • -: The first character indicates the file type.
      • -: Regular file
      • d: Directory
    • rw-r--r--: The next nine characters represent the file permissions, divided into three sets of three characters each.
      • The first set (rw-) is for the owner.
        • r: Read permission
        • w: Write permission
        • -: No execute permission
      • The second set (r--) is for the group.
        • r: Read permission
        • -: No write permission
        • -: No execute permission
      • The third set (r--) is for others (everyone else / others).
        • r: Read permission
        • -: No write permission
        • -: No execute permission
  2. Number of Hard Links (1):
    • This number shows the count of hard links to the file or directory. For directories, this number includes the directory itself and its subdirectories.
  3. Owner (user):
    • The username of the file’s owner.
  4. Group (group):
    • The group name associated with the file.
  5. File Size (1234):
    • The size of the file in bytes.
  6. Modification Timestamp (Jul 29 10:00):
    • The date and time when the file was last modified. The format is typically Mon Day HH:MM or Mon Day Year for older files (more than six months old).
  7. File Name (example.txt):
    • The name of the file or directory.

Example of Interpreting ls -l Output

Let’s break down another example:

drwxr-xr-x 2 riteshSir staff 4096 Jul 28 15:32 documents

Summary of ls -l Output Structure

Field Description
File Type and Permissions Indicates file type and access permissions
Number of Hard Links Count of hard links to the file/directory
Owner Username of the file’s owner
Group Group name associated with the file
File Size Size of the file in bytes
Modification Timestamp Date and time of last modification
File Name Name of the file or directory

The ls -l command provides a comprehensive overview of files and directories, making it easier to manage and understand their attributes.


chmod vs chgrp vs chown

chmod, chown, and chgrp are Unix/Linux commands used for managing file and directory permissions and ownership. Here’s a detailed comparison and explanation of each:

chmod (Change Mode)

Purpose:

Syntax:

chmod [options] mode file

Examples:

  1. Add execute permission for the owner:
    chmod u+x filename
    

    After this command, if the original permissions were -rw-r--r--, they will become -rwxr--r--.

  2. Remove write permission for the group:
    chmod g-w filename
    

    After this command, if the original permissions were -rw-rw-r--, they will become -rw-r--r--.

  3. Set specific permissions:
    chmod 755 filename
    

    This sets the permissions to -rwxr-xr-x.

Permission Notation:

chown (Change Owner)

Purpose:

Syntax:

chown [options] owner[:group] file

Examples:

  1. Change owner to gauravSir:
    chown gauravSir filename
    
  2. Change owner to gauravSir and group to kaksha:
    chown gauravSir:kaksha filename
    
  3. Change group only (equivalent to chgrp):
    chown :gauravSir filename
    

chgrp (Change Group)

Purpose:

Syntax:

chgrp [options] group file

Examples:

  1. Change group to UCA:
    chgrp uca filename
    

Comparison and Differences

Command Purpose Affects Syntax Example
chmod Change file/directory permissions Permissions chmod 755 filename
chown Change file/directory owner Owner (and optionally group) chown gaurav:kaksha filename
chgrp Change file/directory group Group chgrp uca filename

Key Points

Practical Usage

  1. Setting Permissions for Collaboration:
    • If you have a project directory that multiple users should access and modify, you might use chmod to set the appropriate permissions and chown/chgrp to ensure the correct ownership.
  2. Managing User Access:
    • When a user leaves a project, you might need to change ownership of their files to another user using chown.
  3. Group-Based Access Control:
    • If a new team joins a project, you might use chgrp to change the group ownership of files and directories so that the new team can access them.

Calculating Persmissions for the File

Calculating file permissions involves understanding the numeric and symbolic representations of the read, write, and execute permissions for the owner, group, and others.

Numeric Representation of Permissions

Each permission (read, write, execute) is represented by a specific number:

Combining Permissions

Permissions are often combined to form a total value for each of the three groups: owner, group, and others. Here are the combinations:

Example

Let’s calculate the permissions for a file with the following symbolic permissions: -rw-r--r--

  1. Owner (rw-): Read and write
    • Read (r): 4
    • Write (w): 2
    • Total: 4 + 2 = 6
  2. Group (r--): Read only
    • Read (r): 4
    • Total: 4
  3. Others (r--): Read only
    • Read (r): 4
    • Total: 4

So, the numeric representation of -rw-r--r-- is 644.

Setting Permissions with chmod

You can use the chmod command to set these permissions:

chmod 644 filename

Example: Full Permissions Calculation

Let’s calculate another example with rwxr-xr--:

  1. Owner (rwx): Read, write, and execute
    • Read (r): 4
    • Write (w): 2
    • Execute (x): 1
    • Total: 4 + 2 + 1 = 7
  2. Group (r-x): Read and execute
    • Read (r): 4
    • Execute (x): 1
    • Total: 4 + 1 = 5
  3. Others (r--): Read only
    • Read (r): 4
    • Total: 4

So, the numeric representation of rwxr-xr-- is 754.

Certainly! The symbolic mode of the chmod command allows you to change file and directory permissions using a combination of letters and symbols. Here’s a detailed explanation:

Components of Symbolic Mode

  1. User Categories:
    • u: User (the file’s owner)
    • g: Group (the group associated with the file)
    • o: Others (everyone else)
    • a: All (user, group, and others)
  2. Operators:
    • +: Add the specified permissions.
    • -: Remove the specified permissions.
    • =: Set the specified permissions, replacing the existing ones.
  3. Permissions:
    • r: Read permission
    • w: Write permission
    • x: Execute permission

Syntax

chmod [user][operator][permissions] filename

Examples of Symbolic Mode Usage

  1. Adding Permissions:

    • Add read and write permissions for the owner:
      chmod u+rw filename
      

      This command grants read and write permissions to the owner of the file while leaving other permissions unchanged.

    • Add execute permission for the group:
      chmod g+x filename
      

      This command adds execute permission for the group.

    • Add read permission for everyone:
      chmod a+r filename
      

      This command adds read permission for the owner, group, and others.

  2. Removing Permissions:

    • Remove write permission for the group:
      chmod g-w filename
      

      This command removes write permission from the group.

    • Remove execute permission for others:
      chmod o-x filename
      

      This command removes execute permission for others.

    • Remove all permissions for the group:
      chmod g= filename
      

      This command removes all permissions (read, write, execute) for the group, effectively leaving the group with no permissions.

  3. Setting Permissions Explicitly:

    • Set read and execute permissions for the owner and read permission for the group:
      chmod u=rx,g=r filename
      

      This command sets the permissions to:

      • Owner: Read and execute
      • Group: Read
      • Others: No change
    • Set read, write, and execute permissions for the owner, and read and execute permissions for the group and others:
      chmod u=rwx,g=rx,o=rx filename
      

      This command sets the permissions to:

      • Owner: Read, write, and execute
      • Group: Read and execute
      • Others: Read and execute

Symbolic Mode Summary Table

Command Meaning Example
u+rw Add read and write permissions for the owner chmod u+rw file.txt
g-x Remove execute permission for the group chmod g-x file.txt
o=r Set read-only permissions for others chmod o=r file.txt
a+rx Add read and execute permissions for everyone chmod a+rx file.txt
u=rwx,g=rx Set read, write, and execute for owner; read and execute for group chmod u=rwx,g=rx file.txt

Understanding Default Permission for a new File Creation using umask

Understanding the default permissions for new file creation and how umask affects them is crucial for managing file security and access control in Unix/Linux systems.

Default Permissions for New Files

When you create a new file, the system assigns it a default set of permissions. By default, these permissions are typically:

However, these default permissions are modified by the umask value.

What is umask?

The umask (user file creation mask) is a system setting that determines which permissions are removed from the default permissions when a new file or directory is created. It essentially defines what permissions should be “masked” out.

Calculating Permissions with umask

To determine the final permissions:

  1. Find the Default Permissions:
    • Files: 666
    • Directories: 777
  2. Subtract the umask Value:
    • umask is a mask value that removes permissions. For example, a umask of 022 removes write permissions for the group and others.

Examples

Example 1: umask 022

Example 2: umask 027

Example 3: umask 077

Checking Current umask

To see the current umask value, use the umask command:

umask