Comprehensive Linux OS and Administration Guide

Introduction to Linux and Basic Components

  • What is Linux?     * Linux is an Operating System (OS) similar to Windows or macOS.     * It is open-source, meaning the source code is publicly available and completely free to use.     * Application areas include servers, cloud computing, mobile devices (Android), embedded systems, and DevOps tools.

  • Basic Components of the Linux OS     * Kernel: The core part of the operating system that communicates directly with the hardware such as the CPU, RAM, and disks.     * Shell: A program that allows users to interact with the OS by typing commands.     * File System: Organizes and stores data. A key tenet of Linux is that everything is treated as a file, including hardware devices.     * GUI (Graphical User Interface): This is optional. Linux can utilize environments like GNOME or KDE, though it is frequently used via the terminal.

Linux Directory Structure

The Linux file system starts at the root (//) directory. Folders beneath it serve fixed purposes:

  • //: The root of the entire file system.
  • /home/home: Contains user folders (analogous to C:\Users in Windows).
  • /bin/bin: Essential command binaries needed for basic operations (e.g., lsls, cpcp, mvmv).
  • /etc/etc: Houses system configuration files.
  • /var/var: Stores variable files, such as system logs.
  • /tmp/tmp: Temporary files that are often cleared upon reboot.
  • /usr/usr: User programs and libraries.
  • /root/root: The home directory specifically for the root (administrative) user.
  • /dev/dev: Files representing hardware devices like USB drives and hard disks.
  • /proc/proc: A virtual folder containing system and process information.

Basic Linux Commands and Operations

  • Navigation and File Management:     * pwdpwd: Show current working directory.     * lsls: List files and folders.     * cdcd: Change directory.     * mkdirmkdir: Create a new directory.     * touchtouch: Create an empty file.     * cpcp: Copy files or folders.     * mvmv: Move or rename files and folders.     * rmrm: Delete files.     * catcat: Display the full contents of a file.     * clearclear: Clear the terminal screen.     * exitexit: Close the current terminal or shell session.

User Permissions and Ownership

Linux is a multi-user environment where every file and folder is assigned specific permissions and ownership to ensure security and isolation.

  • User Types:     * root: The superuser with administrative privileges and full access.     * Normal users: Have limited access to the system.

  • Permissions Categories:     * Owner: The specific user who owns the file.     * Group: A collection of users with shared access.     * Others: Everyone else on the system.

  • Permission Types:     * rr (read): Allows viewing content.     * ww (write): Allows modifying content.     * xx (execute): Allows running a file or entering a directory.

  • Example Permission String: −rwxr−xr−−-rwxr-xr--     * The first character (−-) indicates the file type.     * rwxrwx (owner): Full permissions (read, write, execute).     * r−xr-x (group): Read and execute permissions only.     * r−−r-- (others): Read-only access.

Package Management and Software Installation

Different Linux distribution families use different package managers to install, update, and remove programs:

  • Ubuntu/Debian-based: Uses apt−getinstall<package−name>apt-get install <package-name> or aptapt.
  • Red Hat/CentOS/Fedora: Uses yuminstall<package−name>yum install <package-name> or dnfinstalldnf install.
  • Arch Linux: Uses pacmanpacman.
  • Universal Formats: Includes snapsnap, flatpakflatpak, and AppImageAppImage.

CRUD Operations in Linux

CRUD stands for Create, Read, Update, and Delete. These operations can be performed on files, directories, content, and system records.

  • CREATE Operations:     * Create a file: touch filename.txttouch\,filename.txt     * Create a file with content (Option A): echo "Hello,Linux!" > hello.txtecho\,"Hello, Linux!"\,>\,hello.txt     * Create a file with content (Option B): cat > data.txtcat\,>\,data.txt (Then type content and press Ctrl+DCtrl+D to save).     * Create a directory: mkdir projectsmkdir\,projects     * Create nested directories: mkdir −p devops/scripts/yamlmkdir\,-p\,devops/scripts/yaml

  • READ Operations:     * View content: cat filename.txtcat\,filename.txt     * Scrollable view: less filename.txtless\,filename.txt     * Page-wise output: more filename.txtmore\,filename.txt     * View first 1010 lines: head filename.txthead\,filename.txt     * View last 1010 lines: tail filename.txttail\,filename.txt     * List files: lsls     * Detailed list (permissions, size, owner): ls −lls\,-l     * Show hidden files: ls −als\,-a     * Read specific line (e.g., 3rd3^{rd} line): sed −n ′3p′ filename.txtsed\,-n\,'3p'\,filename.txt or awk ′NR==3′ filename.txtawk\,'NR==3'\,filename.txt

  • UPDATE Operations:     * Append to file: echo "Newline" >> file.txtecho\,"New line"\,>>\,file.txt     * Edit via terminal: nano filename.txtnano\,filename.txt (Beginner-friendly) or vim filename.txtvim\,filename.txt (Advanced).     * Replace text: sed −i ′s/oldtext/newtext/g′ filename.txtsed\,-i\,'s/oldtext/newtext/g'\,filename.txt     * Rename/Move: mv oldname.txt newname.txtmv\,oldname.txt\,newname.txt

  • DELETE Operations:     * Delete file: rm filename.txtrm\,filename.txt     * Delete directory: rm −r foldernamerm\,-r\,foldername     * Force delete: rm −rf foldernamerm\,-rf\,foldername     * Warning: Executing rm −rf /rm\,-rf\,/ can wipe the entire operating system and should never be used lightly.

The VIM Editor – Modes and Operations

Vim (Vi IMproved) is a modal text editor essential for editing configuration files, shell scripts, and YAML files in headless server environments.

  • Primary Modes:     1. Normal Mode: The default mode used for navigation, deleting, and copying. (Enter by pressing EscEsc).     2. Insert Mode: Used for typing text. (Enter by pressing i,I,a,A,o,orOi, I, a, A, o, or O).     3. Command-Line Mode: Used for saving, quitting, and searching. (Enter by pressing :: from Normal Mode).

  • Navigation Actions (Normal Mode):     * h,j,k,lh, j, k, l: Left, Down, Up, Right.     * w/bw / b: Jump to next/previous word.     * 0 / \: Start/End of line.     * gg/Ggg / G: Top/Bottom of file.

  • Editing Actions (Normal Mode):     * dddd: Delete (cut) line.     * yyyy: Copy (yank) line.     * p/Pp / P: Paste below/above cursor.     * uu: Undo.     * Ctrl+rCtrl+r: Redo.

  • Command-Line Mode Utilities:     * :w:w: Save.     * :q:q: Quit.     * :wq:wq or ZZZZ: Save and quit.     * :q!:q!: Force quit without saving.     * :set nu:set\,nu: Show line numbers.     * /word/word: Search for a word (nn for next match).     * ::%s/old/new/g: Global find and replace.

Numeric (Octal) Permissions and chmod

Permissions are calculated using numeric values based on binary logic:

  • rr (Read): 44
  • ww (Write): 22
  • xx (Execute): 11

Combined values (examples):

  • 77 (4+2+14+2+1): rwxrwx
  • 66 (4+2+04+2+0): rw−rw-
  • 55 (4+0+14+0+1): r−xr-x
  • 44 (4+0+04+0+0): r−−r--

Commonly used numeric codes:

  • 777777: Everyone has full access.
  • 755755: Owner has full access; others can read and execute.
  • 700700: Only the owner has access.
  • 644644: Owner can read/write; others can only read.
  • 600600: Only the owner can read/write.

Advanced Permissions and Special Bits

  • Special Permissions:     * ss (Setuid / Setgid): Runs a program with the owner or group's privileges.     * tt (Sticky Bit): Applied to directories (like /tmp/tmp) to ensure only the file owner can delete their specific files.
  • Directory-Specific Behavior:     * rr: Ability to list files (lsls).     * ww: Ability to add or remove files.     * xx: Ability to enter the directory (cdcd).

Linux User and Group Management

  • User Management Commands:     * Create: sudo adduser riyassudo\,adduser\,riyas (creates home directory and prompts for password).     * Delete: sudo deluser −−remove−home riyassudo\,deluser\,--remove-home\,riyas.     * Modify: sudo usermod −aG devops riyassudo\,usermod\,-aG\,devops\,riyas (adds user to the 'devops' group).     * Lock/Unlock: sudo usermod −L riyassudo\,usermod\,-L\,riyas (Lock); sudo usermod −U riyassudo\,usermod\,-U\,riyas (Unlock).

  • Information Files:     * /etc/passwd/etc/passwd: Account details (username, UID, GID).     * /etc/shadow/etc/shadow: Encrypted passwords.     * /etc/group/etc/group: Group definitions.     * /etc/sudoers/etc/sudoers: Permissions for sudo access.

Service Management (systemd and systemctl)

A service (or daemon) is a background process. Modern Linux distributions use systemdsystemd for management.

  • Service Operations:     * systemctl start nginxsystemctl\,start\,nginx: Start service.     * systemctl stop nginxsystemctl\,stop\,nginx: Stop service.     * systemctl restart nginxsystemctl\,restart\,nginx: Restart service.     * systemctl status nginxsystemctl\,status\,nginx: Check if service is running.     * systemctl enable nginxsystemctl\,enable\,nginx: Set service to start automatically at boot.

  • Logs: journalctl −u nginx −fjournalctl\,-u\,nginx\,-f (Follow logs for a specific unit).

  • Anatomy of a .service File (/etc/systemd/system/myapp.service/etc/systemd/system/myapp.service):     * [Unit]: Description and dependencies (e.g., After=network.targetAfter=network.target).     * [Service]: Defines ExecStartExecStart (path to script), WorkingDirectoryWorkingDirectory, UserUser, and RestartRestart policy (e.g., alwaysalways).     * [Install]: Defines the target (e.g., WantedBy=multi−user.targetWantedBy=multi-user.target).

Process Management

  • Process States:     * R: Running     * S: Sleeping     * T: Stopped     * Z: Zombie (terminated but not cleaned up)     * D: Uninterruptible sleep (I/O wait)

  • Management Tools:     * top/htoptop / htop: Live system monitoring.     * ps −efps\,-ef: Snapshot of all processes.     * kill −9 <PID>kill\,-9\,<PID>: Forcefully stop a process.     * nice/renicenice / renice: Set process priority (−20-20 is highest, 1919 is lowest).

Networking and Connectivity

  • Tools and Commands:     * ip aip\,a: View IP addresses.     * pingping: Test connectivity to a host.     * dig/nslookupdig / nslookup: DNS troubleshooting.     * ss −tulnss\,-tuln: View listening ports and services.     * curl/wgetcurl / wget: Test HTTP requests.     * nmapnmap: Port scanning.

Linux Troubleshooting Checklist

  1. Service Status: Is the service running? (systemctl statussystemctl\,status)
  2. Logs: Are there error messages in /var/log/syslog/var/log/syslog or journalctljournalctl?
  3. Ports: Is the port listening? (ss −tulnss\,-tuln)
  4. Resources: Is the disk full (df −hdf\,-h) or memory exhausted (free −hfree\,-h)?
  5. Permissions: Does the user have ownership and correct permissions?
  6. Network: Can you ping the target or trace the route?

Sample Practice Exercise: File Creation and Permissions

  1. Setup: mkdir\,devops_practice\,&&\,cd\,devops_practice.
  2. Create: touch inventory.txttouch\,inventory.txt.
  3. Append Content:     * echo "Server1: 192.168.1.1" >> inventory.txtecho\,"Server1:\,192.168.1.1"\,>>\,inventory.txt     * echo "Server2: 192.168.1.2" >> inventory.txtecho\,"Server2:\,192.168.1.2"\,>>\,inventory.txt
  4. Edit: Use sed −i ′s/192.168.1.2/10.0.0.2/′ inventory.txtsed\,-i\,'s/192.168.1.2/10.0.0.2/'\,inventory.txt to update the IP.
  5. Secure: chmod 600 inventory.txtchmod\,600\,inventory.txt (Only owner can read/write).
  6. Cleanup: rm inventory.txtrm\,inventory.txt.