Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Linux components and basic shell commands

Concise study notes on Linux architecture (kernel, user space, daemons, init, VFS, drivers) and basic shell commands, with definitions, command syntax to remember, worked examples and exam tips.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Linux is the operating system kernel used in servers, desktops, embedded devices and many cloud systems. Engineers need to know the Linux components (how kernel and user space interact, what daemons and init do, how device drivers and the VFS fit together) and basic shell commands to inspect, debug and automate system tasks.

Key ideas

(Define terms before using them. Bolded terms are defined.)

  • Kernel: the core software that runs in protected CPU mode and controls hardware, memory, processes and I/O. Assumption: CPU and MMU provide hardware protection (rings/privilege levels) so kernel can be isolated from user programs.
  • User space: the memory and execution domain where normal applications run with limited privileges.
  • Kernel space: the memory and execution domain where the kernel executes with full privileges.
  • Shell: a command interpreter program in user space that reads user input, parses commands and invokes programs (examples: bash, sh, zsh). A shell is itself a user process.
  • System call: a controlled transfer of control from user space to kernel space to request services (examples: open, read, write, fork, execve, ioctl). Exact definition: "A system call is a software mechanism that transfers control from a user process to the kernel to perform an operation that requires elevated privileges." Assumption: hardware provides a syscall/trap instruction or software interrupt.
  • Process: an instance of a running program, with an address space, registers, open file descriptors and a PID (process identifier).
  • Daemon: a background process (service) usually started by the init system. Typically runs without direct user interaction.
  • Init system: the first user-space process started by kernel (PID 1) that starts daemons and manages system state. Modern Linux often uses systemd; older systems used SysV init and runlevels.
  • Virtual File System (VFS): an abstraction layer in the kernel that provides a single uniform API to user programs for different filesystem types. VFS provides the inode and dentry abstractions and dispatches to filesystem-specific code.
  • Device driver: kernel code that controls hardware devices and implements device operations; it registers with kernel subsystems and provides file-like interfaces (character or block devices) that user programs can access via device nodes in /dev.
  • Module: loadable kernel code that can be inserted or removed at runtime (insmod, rmmod, modprobe).
  • File descriptor: a small integer that a process uses to refer to an open file, socket or device. By convention, 0 = stdin, 1 = stdout, 2 = stderr. On fork, file descriptors are duplicated in the child (they refer to the same underlying open file description).

Execution of a simple command (exact step-by-step universities expect):

  1. User types command in shell. Shell performs lexical analysis and parsing. If the command is a shell builtin, shell executes it internally. If the command name contains no '/', the shell searches $PATH to locate the executable.
  2. For an external program: shell creates a new process using fork() (system call) — child is an exact copy of parent (address space copy-on-write when supported).
  3. In the child process, shell replaces the child image with the program using execve(path, argv, envp) (system call). execve loads the binary, sets up stack argv/envp and begins execution at program entry point. Before calling execve, the shell sets up any I/O redirections and pipelines using dup2/close on file descriptors.
  4. Parent shell may waitpid() for the child or run concurrently.
  5. The executed program uses system calls (read/write/open/close/ioctl) to interact with files and devices.

System-call mechanics (conceptual derivation):

  • User-mode instruction requests kernel service -> CPU performs a controlled context switch (trap) to kernel mode -> kernel validates arguments and privileges (copying user data if needed) -> kernel performs operation -> returns result to user with registers set and CPU switched back to user mode.

Permissions (short derivation universities ask):

  • Each file has three classes: owner, group, others. Each class has three bits: read(r)=4, write(w)=2, execute(x)=1. Octal permission is owner100 + group10 + others. Example: rwxr-xr-- = owner: r(4)+w(2)+x(1)=7, group: r(4)+x(1)=5, others: r(4)=4 → octal 754. Special mode bits use a leading octal digit: setuid=4, setgid=2, sticky=1 (e.g., 4755). On directories, the sticky bit restricts unlink/rename to the owner or root; setgid on a directory causes new files to inherit the directory’s group. umask masks permission bits at file creation.

Diagram to draw for an exam (describe exactly what to draw and label):

  • Draw two horizontal layers: Top labelled "User Space" and Bottom labelled "Kernel Space". In User Space draw boxes: "Shell (bash)", "User apps". Draw arrow from Shell to Kernel labelled "system call (fork/execve) via trap/syscall". Inside Kernel Space draw boxes: "VFS", "Scheduler", "Memory manager (uses MMU)", "Device drivers". Draw hardware at bottom labelled "CPU, Disk, NIC, Memory". Label transition arrow as "context switch / trap" and annotate that user->kernel uses syscall/trap and kernel->user returns with modified registers. Also show /dev and /proc as VFS entries.

Formulas

(Use syntax lines for commands and permission math.)

read = 4, write = 2, execute = 1 — mapping of permission bits.

permission_octal = 100*owner + 10*group + others — numeric representation; owner, group, others each is sum of {4,2,1}.

chmod 754 file — set permissions to owner=rwx, group=r-x, others=r--. Applies to file or directory; for directories, execute bit means enter/search.

ls -l — show file type, permissions, link count, owner, group, size (bytes), modification time, name.

ps aux — list processes (BSD style); ps -ef — list processes (System V style). Columns vary by style: aux typically shows %CPU and %MEM, while -ef shows PPID, etc.

systemctl status name.service — check status of a systemd service. systemctl start|stop|restart name.service — control actions.

grep 'pattern' file — search file for lines matching pattern (basic regular expressions by default; use -E for extended). grep -R for recursive.

find /path -type f -mtime +N -exec rm {} \; — find files older than N days and remove them.

du -h — disk usage human-readable, df -h — filesystem free space human-readable.

mount and umount — attach/detach filesystems to VFS mount points.

man command — show manual page for a command.

Worked examples

Example 1 — Permission calculation and setting (numerical steps): Given permission string -rwxr-xr-- for file script.sh, find octal and show commands to set it.

  1. Split into three groups: owner rwx → 4+2+1 = 7, group r-x → 4+0+1 = 5, others r-- → 4+0+0 = 4.
  2. Octal = 754.
  3. Command: chmod 754 script.sh. Expected check: run stat -c '%A %a %n' script.sh to see -rwxr-xr-- 754 script.sh (stat output form may vary by distro). Final answer: 754 and chmod 754 script.sh.

Example 2 — Small shell script: count lines in .c files and print top 3 largest by size (script and sample run). Script (save as topc.sh):

#!/bin/bash
# prints total lines across .c files and top 3 largest .c files by bytes
total=$(find . -maxdepth 1 -type f -name '*.c' -exec wc -l {} + | awk 'END{print $1+0}')
echo "Total lines in .c files: $total"
find . -maxdepth 1 -type f -name '*.c' -printf '%s %p\n' | sort -nr | head -n 3

Sample directory contents (for reproducible example):

  • a.c contains 10 lines, size 120 bytes
  • b.c contains 30 lines, size 300 bytes
  • c.c contains 5 lines, size 50 bytes Run (assume files exist): Input: run ./topc.sh Output exactly:
Total lines in .c files: 45
300 ./b.c
120 ./a.c
50 ./c.c

(For exam, explain commands: find, wc -l, awk to extract total, -printf '%s %p' prints size and path.)

Example 3 — Model answer: "Explain what happens when you run /bin/ls" (theory answer):

  • Step 1: Shell parses and sees /bin/ls; it forks.
  • Step 2: Child process calls execve("/bin/ls", argv, envp). Kernel loads binary, maps code and data (ELF loader), sets up stack with argv/envp and transfers control to entry point.
  • Step 3: ls calls getdents/readdir to list directory entries; calls stat/lstat to get file metadata (permissions, size, times); writes output to stdout via write(1,...).
  • Step 4: When ls completes, it calls exit(status), kernel cleans up process resources and returns exit code to parent which waits and collects status.

Common mistakes

  • Miscalculating permission octal by forgetting execute bit (1) or mixing up group/others order.
  • Using rm -rf without understanding what it does: by default it does not follow symlinks (it removes the link itself, not the target), but it will recursively remove directory contents; always state the risk in answers.
  • Assuming commands produce identical output across distros (dates, locale, and stat formats vary). State that output may vary.

For your exam

Types of questions you will see: definitions (kernel, shell, system call), short notes (VFS, device drivers, daemons, init), derivations or step-by-step flows (how a command is executed, system call flow, permission octal calculation), diagram drawing (user/kernel layers, process lifecycle), and short shell command tasks or scripts. What a full-marks answer includes:

  • Precise definitions (bold terms are useful).
  • Stepwise numbered sequence for flows (e.g., fork -> execve -> wait), with reasons for each step.
  • Diagrams drawn as layers and labelled arrows (describe system call/trap and return path).
  • For command questions: exact syntax, purpose, and sample expected output; mention assumptions (which shell, locale).

Quick check

  1. What system call creates a new process and what system call replaces the process image?
  2. How do you express owner=rw-, group=r--, others=--- in octal and chmod form?
  3. What command shows running systemd services and how do you stop a service named "foo"?

Answers: 1) fork() creates new process, execve() (or family) replaces process image. 2) owner rw- = 4+2+0 = 6, group r-- = 4 = 4, others --- = 0 → octal 640, chmod 640 filename. 3) systemctl list-units --type=service --state=running (to show running services), stop with systemctl stop foo.service.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?