The kernel, user space, and the system call
2 min readA running system has two halves. the memory and privileges where ordinary programs run, without direct access to hardware holds everything you launch: the shell, editors, browsers, your own code. kernel space holds the kernel itself, its drivers and its scheduler. A program in user space cannot read another process's memory, write to the disk directly, or talk to a peripheral on its own.
The controlled way across this boundary is a a controlled entry point into the kernel, through which a process requests a service such as reading a file or creating a process. When a program wants to read a file, it does not open the disk controller; it calls the read() system call, and the kernel performs the operation on its behalf, copying the result back into the program's memory. There are a few hundred system calls, and everything a program can do reduces to them.
This is also why the strict name is GNU/Linux, not Linux. The Linux kernel alone cannot run your programs; you also need user-space tools such as the C library, the shell and the core utilities, most of which came from the GNU project. Both halves are open source, but they are different projects with different maintainers.
The split is enforced by hardware, not by convention. CPUs offer several privilege levels (four rings on x86, exception levels EL0 to EL3 on 64-bit ARM), and Linux uses two of them: kernel code runs at the privileged level (ring 0, EL1) and user code at the unprivileged one (ring 3, EL0). The system call is the single, checked doorway that switches between them, and a system call costs far more than a normal function call, which is why the C library buffers file I/O for you.
The terminal, the shell, and the console
2 min readA a program or device that displays text and passes your keystrokes to the program attached to it is what you type into. Originally terminals were physical teletype machines wired to a computer; today a terminal emulator such as GNOME Terminal, Konsole, or the Windows Terminal hosts your Linux session and simply draws text and sends keys.
A the program that reads the commands you type, interprets them, and starts other programs is the program the terminal is attached to. Bash is the default shell on most distributions; zsh and fish are common alternatives, and BusyBox's ash is the tiny shell you will meet on embedded boards.
A the terminal physically or logically attached to a machine for administration, especially the one that receives boot messages and the login prompt is the machine's own surface. On a server, the console is where the kernel prints early boot messages. On an embedded board, the console is usually a serial port: you connect a USB-to-serial cable, open a terminal at 115200 bits per second, and watch the boot log scroll past before a login prompt appears.
On a desktop Linux, the console is the set of virtual terminals: text logins you reach with Ctrl+Alt+F1 through F6, while the graphical desktop usually lives on F1 or F7 depending on the distribution. Each runs a getty process that prints a login prompt. When you log in there, the system reads your entry in /etc/passwd, whose seventh and last field names your login shell, and starts that shell for you.
The shell: your command interpreter
2 min readAfter you log in, the shell that starts is your login shell. A login shell reads the system-wide /etc/profile and then your own ~/.profile (on Debian-family systems that file in turn sources ~/.bashrc). A new terminal window you open later runs an interactive shell that is not a login shell; it reads ~/.bashrc instead. A shell running a script is non-interactive and reads none of those by default. This is why an alias you defined in ~/.bashrc is available in your terminal but not inside a script.
Two things tell you which shell you have, and they are not the same. The environment variable $SHELL, set at login, records what your login shell was and is what new programs consult. Your actual login shell is the last field of your /etc/passwd entry. Because $SHELL can be changed by anything, it is a hint, not proof.
echo $SHELL # typical output: /bin/bash
getent passwd "$USER" # typical output ends with :/bin/bash
A handful of commands identify you and the machine. whoami prints your username, id prints your user and group IDs, hostname prints the machine name, and uname -a prints kernel and hardware details.
id # typical output: uid=1000(learner) gid=1000(learner) groups=1000(learner),...
uname -a # typical output: Linux labpc 6.8.0-45-generic #45-Ubuntu SMP ... x86_64 x86_64 x86_64 GNU/Linux
The output above is typical; yours will differ in the hostname, version and architecture. On an ARM board the architecture line reads aarch64 or armv7l, a reminder that the same kernel runs on very different hardware.
learner@embeddex:~/lab$ # your username learner@embeddex:~/lab$ whoami learner learner@embeddex:~/lab$ # user and group IDs learner@embeddex:~/lab$ id uid=1000(learner) gid=1000(learner) groups=1000(learner),4(adm),24(cdrom),27(sudo),30(dip),46(pl ugdev),100(users) learner@embeddex:~/lab$ # the machine's name learner@embeddex:~/lab$ hostname embeddex learner@embeddex:~/lab$ # kernel and hardware details learner@embeddex:~/lab$ uname -a Linux embeddex 6.18.58.1-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Tue Oct 27 00:24:11 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux learner@embeddex:~/lab$ # your login shell, as recorded at login learner@embeddex:~/lab$ echo $SHELL /bin/bash learner@embeddex:~/lab$ # the same answer from /etc/passwd, the source of truth learner@embeddex:~/lab$ getent passwd "$USER" learner:x:1000:1000:,,,:/home/learner:/bin/bash
What happens when you press Enter
5 min readWhen you type ls -la /etc and press Enter, the terminal delivers the line to the shell, which runs it through a fixed pipeline.
First the shell parses the line into words: the command name ls and the arguments -la and /etc. A command name can be an alias, a function, a builtin, or an external program, and the shell checks them in that order: alias, then function, then builtin, then a cache of previously found programs, then the directories in $PATH. If nothing matches, the shell prints command not found and sets the exit status to 127.
Read the line: the terminal sends the keystrokes to the shell, which waits for Enter.
Parse: the shell splits the line into words, the command name first.
Look up: the shell decides what the name means, in the order alias, function, builtin, hashed program, PATH.
Fork: for an external command, the shell asks the kernel to copy itself, creating a child process.
Exec: the child replaces itself with the program, loading its code and starting it.
Run: the program executes, writing output to the terminal and an exit status when it finishes.
Wait: the parent shell waits for the child and collects its exit status.
Prompt again: the shell prints a new prompt; the status is now available as $?.
A a running instance of a program, with its own memory, IDs and state, managed by the kernel is what the shell creates when it forks. fork() duplicates the shell; execve() replaces the duplicate with the new program; wait() lets the parent wait for the child to finish. Because these three calls run every command you type, they are worth seeing once in their raw form.
Going deeperWhat the shell really calls
The shell is just a program, and you can watch it with strace, which prints each system call a process makes. Run strace -f -e trace=process bash -c 'ls /; true' and you will see clone (how the C library implements fork() on modern Linux), then execve and wait4 scroll past. The ; true matters: given a single command, bash skips the fork and simply replaces itself with ls, an optimisation you would otherwise mistake for a missing step. In C, the same three steps look like this, with error handling omitted for brevity:
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
pid_t pid = fork();
if (pid == 0) {
char *args[] = {"/bin/echo", "hello", NULL};
execve("/bin/echo", args, NULL);
return 1; /* reached only if execve fails */
}
int status;
waitpid(pid, &status, 0); /* parent waits for the child */
return 0;
}
The child calls execve to replace itself with /bin/echo; the parent blocks in waitpid until the child finishes.
This is why cd must be a builtin. A process's working directory is a property of that process. If cd were a separate program, it would change its own directory and exit, and your shell would be exactly where it was. So the shell implements cd itself, and builtins such as cd, type and export run inside the shell without any fork.
The the small integer a process returns when it finishes; 0 means success and non-zero means failure of the last command is stored in the special variable $?.
nosuchcommand; echo $? # typical output: command not found, then 127
ls /nope; echo $? # typical output: an error, then 2
Exit status 0 means success, 127 means command not found, 126 means found but not executable, and 2 is what most programs return for a usage or access error. This is how scripts decide what to do next, and how you tell a failure from a mistake.
sbin sbin.usr-is-merged snap srv sys tmp usr var learner@embeddex:~/lab$ # $? holds the last exit status; 0 means success learner@embeddex:~/lab$ echo $? 0 learner@embeddex:~/lab$ # the shell cannot find this name learner@embeddex:~/lab$ nosuchcommand bash: nosuchcommand: command not found learner@embeddex:~/lab$ # 127 means command not found learner@embeddex:~/lab$ echo $? 127 learner@embeddex:~/lab$ # found, but the path does not exist learner@embeddex:~/lab$ ls /nope ls: cannot access '/nope': No such file or directory learner@embeddex:~/lab$ # 2 means it ran but failed on its input learner@embeddex:~/lab$ echo $? 2
learner@embeddex:~/lab$ # cd runs inside the shell: a builtin learner@embeddex:~/lab$ type cd cd is a shell builtin learner@embeddex:~/lab$ # ls is an external program on disk learner@embeddex:~/lab$ type ls ls is /usr/bin/ls learner@embeddex:~/lab$ # echo is a builtin too learner@embeddex:~/lab$ type echo echo is a shell builtin learner@embeddex:~/lab$ # type -a lists every match; the builtin wins learner@embeddex:~/lab$ type -a echo echo is a shell builtin echo is /usr/bin/echo echo is /bin/echo
Key takeaways
- User space can reach hardware only through system calls; that is the whole security model in one sentence.
- The terminal draws text and sends keys, the shell interprets commands, and the console is the machine's own login surface.
- Pressing Enter makes the shell parse, look up, fork, exec and wait; cd is a builtin because a child cannot change its parent's directory.
- Exit status 0 means success, 127 means not found, and 2 usually means a usage or access error.
Reflect
- Explain to a colleague, in two sentences: describe the relationship between the kernel, user space and a distribution, naming an example of each.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Official docsGNU Bash manual
- Official docsThe Linux kernel documentation
- Man pagesyscalls(2) - Linux system calls
- Official docsBash Reference Manual: command search and execution
- Official docsGNU Bash reference manual
- Man pagebash(1)
- Man pageintro(1): introduction to user commands
Official documentation first. Links checked when the lesson was published.
