Unit 1 - Notes

CSC202

Unit 1: Introduction to Linux

1. Linux Characteristics

Linux is a Unix-like, open-source and community-developed operating system kernel. As a system administrator, understanding its core architecture is fundamental.

Core Characteristics

  • Open Source: Released under the GNU General Public License (GPL). Source code is freely available for anyone to use, modify, and redistribute.
  • Multi-user: Multiple users can access system resources simultaneously.
  • Multi-tasking: The CPU can execute multiple independent processes concurrently.
  • Portability: Linux supports a vast range of hardware architectures (x86, ARM, RISC, SPARC).
  • Security: Implements robust permission, authentication, and authorization mechanisms.
  • Hierarchical Filesystem: Everything is a file, organized in a tree structure starting from the root directory (/).

System Architecture Layers

The Linux system is divided into two main spaces: User Space and Kernel Space.

A layered concentric circle diagram illustrating the Linux Operating System Architecture. The innerm...
AI-generated image — may contain inaccuracies

  1. Hardware: The physical machine.
  2. Kernel: The core of the OS. It manages CPU, memory, and devices. It acts as the bridge between applications and hardware.
  3. Shell: A command-line interface that interprets user commands and passes them to the kernel.
  4. Utilities/Applications: Programs that perform specific tasks for the user.

2. Bash Interaction with Linux

The Bash (Bourne Again SHell) is the default CLI for most Linux distributions. It allows administrators to interact directly with the kernel.

The Command Syntax

Commands generally follow this structure:

BASH
command [options] [arguments]

  • Command: The program to run (e.g., ls).
  • Options: Modify behavior, usually prefixed with - or -- (e.g., -l for long listing).
  • Arguments: The target of the command (e.g., /home/user).

Standard Streams and Redirection

Linux treats input and output as streams of data.

Stream Descriptor Description
stdin 0 Standard Input (usually keyboard)
stdout 1 Standard Output (usually screen)
stderr 2 Standard Error (error messages)

Redirection Operators:

  • > : Overwrite stdout to a file (ls > list.txt).
  • >> : Append stdout to a file (echo "End" >> list.txt).
  • 2> : Redirect stderr (command 2> error.log).
  • | (Pipe) : Send the output of command A as input to command B (cat file.txt | grep "search").

3. Use Help in Linux

An administrator must know how to find documentation without leaving the terminal.

Man Pages (man)

The system manual is divided into sections.

  • Command: man [section] [keyword]
  • Navigation: Spacebar (next page), b (back), /search_term (search), q (quit).

Common Sections:

  1. User Commands
  2. File Formats (e.g., man 5 passwd)
  3. System Administration Commands

Other Help Tools

  • --help: Most commands support this flag for a quick summary (e.g., mkdir --help).
  • info: Provides hyperlinked, detailed documentation (common in GNU tools).
  • whatis: Displays one-line manual page descriptions.
  • apropos: Searches manual page names and descriptions for a keyword (equivalent to man -k).

4. Administering Users and Groups

Linux uses configuration files to manage identity. It does not store passwords in plain text.

Critical Configuration Files

A detailed schematic diagram explaining the structure of the /etc/passwd and /etc/shadow files. The ...
AI-generated image — may contain inaccuracies

  1. /etc/passwd: Contains user account information. Readable by all users.
    • Format: username:x:UID:GID:comment:home_directory:login_shell
  2. /etc/shadow: Contains encrypted password and aging information. Readable only by root.
  3. /etc/group: Contains group information.
    • Format: group_name:x:GID:user_list

User ID (UID) Types

  • 0 (Root): The superuser with unlimited privileges.
  • 1 - 999 (System Users): Reserved for system processes/daemons (e.g., apache, sshd). They usually have no login shell (/sbin/nologin).
  • 1000+ (Regular Users): Human users.

5 & 6. Manage User and Group Accounts

User Management Commands

  • Create User: useradd [options] username
    • -m: Create home directory.
    • -s: Specify shell.
    • -g: Primary group.
    • -G: Secondary (supplementary) groups.
      BASH
          # Example: Create user 'john', make home dir, assign to 'developers' group
          sudo useradd -m -G developers -s /bin/bash john
          
  • Modify User: usermod
    • usermod -aG sudo john (Append user to sudo group).
    • usermod -L john (Lock account).
  • Delete User: userdel
    • userdel -r john (Delete user and their home directory).
  • Set Password: passwd username

Group Management Commands

  • Create Group: groupadd groupname
  • Modify Group: groupmod -n newname oldname
  • Delete Group: groupdel groupname

7. Configure Privilege Escalation

Linux follows the principle of "Least Privilege." Administrators should not log in as root; they should escalate privileges only when necessary.

su (Switch User)

  • Switches the current shell to another user context.
  • su -: Switches to root and loads root's environment variables (path, home, etc.). Requires the target user's password.

sudo (SuperUser DO)

  • Allows a permitted user to execute a command as the superuser (or another user).
  • Requires the current user's password.
  • Provides an audit trail (logs commands in /var/log/auth.log or /var/log/secure).

The Sudoers File

Managed via the visudo command (never edit directly with vim/nano to prevent syntax errors that lock out all admins).

  • Location: /etc/sudoers
  • Syntax:
    TEXT
        username    hostname=(run_as_user)    command_list
        root        ALL=(ALL:ALL)             ALL
        john        ALL=(ALL)                 NOPASSWD: /usr/bin/apt
        

8. Troubleshoot User and Group Issues

Common Scenarios

  1. "Permission Denied":
    • Check: ls -l to view file permissions and ownership.
    • Fix: Use chown to change owner or chmod to change permissions. Add user to the correct group.
  2. User Cannot Login:
    • Check: Is the account locked? Check /etc/shadow for a ! prefix on the password hash.
    • Check: Is the shell valid? Ensure /bin/bash exists and is permitted in /etc/shells.
    • Fix: passwd -u username (unlock) or chage -E -1 username (remove expiration).
  3. "User is not in the sudoers file":
    • Fix: Log in as root (or boot to recovery) and add the user to the wheel (RHEL) or sudo (Debian) group.

9. Installing Linux

Partitioning

Before installation, the disk must be partitioned.

  • MBR (Master Boot Record): Legacy, max 2TB drives, 4 primary partitions.
  • GPT (GUID Partition Table): Modern (UEFI), supports ZB drives, 128 partitions.

Standard Partitions

  • /boot: Contains kernel and bootloader files (approx 1GB).
  • / (root): The main OS files.
  • /home: User data (often separated to preserve data during OS re-installs).
  • swap: Virtual memory used when RAM is full.

Filesystems

  • ext4: Standard, stable, journaling filesystem.
  • XFS: High performance, great for large files (default in RHEL).
  • Btrfs: Advanced features like snapshotting.

10. The Linux Boot Process

Understanding the boot process is essential for troubleshooting startup failures.

A vertical flowchart diagram depicting the Linux Boot Process from power-on to user login. The chart...
AI-generated image — may contain inaccuracies

The 6 Stages

  1. BIOS/UEFI: Performs POST (Power On Self Test). Identifies the bootable device (HDD, USB, Network).
  2. MBR/GPT: The system reads the partition table to find the bootloader.
  3. Bootloader (GRUB2): The GRand Unified Bootloader presents the user with a menu to select the OS kernel.
  4. Kernel: The kernel loads into memory, detects hardware, and mounts the initial file system.
  5. Initramfs: A temporary file system loaded into memory that contains drivers needed to mount the real root file system.
  6. Init (Systemd): The first process started by the kernel (PID 1). It brings the system to a specific state (target) by starting services.

11. Modify Boot Settings

Managing GRUB2

The bootloader configuration is generated automatically. Do not edit /boot/grub2/grub.cfg manually.

  1. Edit settings in /etc/default/grub (e.g., change timeout, kernel parameters).
  2. Rebuild the configuration:
    • grub2-mkconfig -o /boot/grub2/grub.cfg

Systemd Targets (Runlevels)

Linux boots into a specific "Target".

  • multi-user.target: Text mode, networking enabled (Server standard).
  • graphical.target: GUI mode (Desktop standard).
  • rescue.target: Single-user mode for maintenance.

Commands:

  • Check default: systemctl get-default
  • Change default: systemctl set-default multi-user.target
  • Switch immediately: systemctl isolate graphical.target

Resetting Root Password (Troubleshooting)

  1. Reboot and interrupt GRUB menu.
  2. Edit the kernel line (press e).
  3. Append rd.break or init=/bin/bash to the end of the line starting with linux.
  4. Boot (Ctrl+x).
  5. Remount file system: mount -o remount,rw /
  6. Change password: passwd root
  7. Relabel SELinux (if applicable): touch /.autorelabel
  8. Reboot.