Eric's Site

GNU/Linux

A hand-written guide.
Chapter I :: The Kernel

What the kernel actually does

When you press a key, something has to receive that input and decide what to do with it. When a program wants to read a file, something has to talk to the disk on its behalf. When two programs both want memory, something has to manage it fairly. That something is the kernel.

The kernel is the central part of an operating system. It manages hardware resources such as the CPU, memory, storage devices, and network interfaces, and provides services that ordinary programs use to get their work done. It sits between applications and the hardware, handling things that programs generally should not have to manage themselves.

Linux is the kernel. When people casually say "Linux", they often mean an entire operating system built around the Linux kernel, but technically Linux itself is the kernel. A complete GNU/Linux system also includes many other pieces: libraries, system utilities, a shell, desktop software, applications, and much more.

The Linux kernel was first written by Linus Torvalds in 1991 as a Unix-like kernel for personal computers. He released it online and invited others to use, study, modify, and contribute to it. Today the Linux kernel is one of the largest collaborative software projects in the world and runs on everything from small embedded devices to servers and supercomputers.

What makes the Linux kernel cool

The Linux kernel is generally described as monolithic, which means most of its core operating-system services run in kernel space as part of one large kernel. Device drivers, filesystems, networking, memory management, and other major subsystems are integrated into the kernel architecture.

At the same time, Linux supports loadable kernel modules. A module is a piece of kernel code that can be loaded and unloaded while the system is running. This allows many drivers and other features to be added when they are needed without having to build every possible feature directly into the kernel itself.

The kernel also has sophisticated mechanisms for deciding which processes receive CPU time. Scheduling is one of the reasons a modern GNU/Linux system can remain responsive while many programs are running at once. There is much more to scheduling than simply giving every process an identical slice of time.

Linux also runs on an enormous range of hardware. The same kernel project powers systems as different as Raspberry Pis, Android devices, desktop computers, servers, routers, and supercomputers. Very few pieces of software have that kind of portability.

Linux vs Windows NT vs macOS

Windows uses the Windows NT kernel. NT has a hybrid architecture, combining ideas associated with both monolithic and microkernel designs. Much of its operating-system functionality still runs in kernel space, and the system has its own object, security, driver, and process models. The Windows source code is proprietary, so the public cannot freely study, modify, and redistribute the operating system itself.

macOS uses the XNU kernel, which combines parts of the Mach kernel with components derived from BSD. The XNU source is published by Apple, but macOS as a complete operating system is not a free software system, and Apple's licensing and hardware ecosystem place substantial restrictions on how it can be used and distributed.

What makes the Linux kernel unusual is not merely that its source code can be viewed. It is distributed under the GNU General Public License version 2, which gives recipients important freedoms to study, modify, and redistribute the software under the license's terms. Anyone with the necessary knowledge and hardware can build a modified Linux kernel.

Quick comparison

Linux is monolithic and released under the GPLv2. Windows NT is a proprietary hybrid kernel. XNU is a hybrid kernel with publicly available source components, but macOS itself is proprietary. All three are technically interesting; one major difference is how much freedom the user has to study, modify, and redistribute the software.


The GNU Project and free software

GNU
GNU Project
Tux, the Linux mascot
Linux kernel

There is a part of the GNU/Linux story that is easy to miss if you only learn about the kernel. GNU is not just another word for Linux. The GNU Project is a large collection of software and system components, and it was started by Richard Stallman in 1983 with the goal of developing a complete operating system composed of free software.

The word free here refers to freedom, not price. A free program is software that gives its users the freedom to run it, study how it works, modify it, and share copies and modified versions. This is sometimes summarized as "free as in freedom" rather than "free as in gratis."

GNU developed many important parts of the software environment commonly found on GNU/Linux systems, including the GNU C Library, GNU Coreutils, the Bash shell, the GNU Compiler Collection, and many other tools. The Linux kernel later supplied the kernel that the GNU-operating-system project had been working toward, and the combination became the basis of the systems many people now call GNU/Linux.

This is also why I prefer to write GNU/Linux when talking about the operating system rather than treating the kernel as if it were the whole system. Linux is the kernel; GNU provides a large portion of the traditional userland and system environment. Other projects provide still more pieces, including the desktop environment, graphical libraries, package manager, applications, and so on.

Richard Stallman's work helped make software freedom a major philosophical and practical idea in computing. The GNU Project was not simply an attempt to write useful programs; it was also an attempt to create a computing environment in which users remained in control of the software they were using.
Chapter II :: The Shell

The shell and the terminal

The terminal is the window you type in. The shell is the program running inside it that reads what you type and runs commands. They are two different things, although people use the words interchangeably all the time.

One of the best-known shells in the GNU/Linux world is Bash, the Bourne Again SHell. Bash was developed as part of the GNU Project and is based on ideas from the older Unix Bourne shell. Other shells such as zsh and fish are also popular and can perform many of the same basic tasks.

When you open a terminal you see a prompt. It usually looks something like this:

user@hostname:~$

The ~ is shorthand for your home directory, which is usually /home/yourname. The $ normally means you are a regular user. If you see a # instead, that usually means the shell is running as the root user. Be careful when you see that.

The shell reads your command, looks for the program or shell function it refers to, runs it, and displays the result. That simple loop is the foundation of the command line.

The filesystem layout

GNU/Linux has one big filesystem tree starting at /, called the root directory. Everything is somewhere inside it, including mounted filesystems and devices. There are no Windows-style drive letters at the top of the filesystem tree.

  • Your personal files /home/yourname/
  • System-wide programs /usr/bin/
  • System configuration /etc/
  • Temporary files /tmp/
  • Variable system data /var/
  • Kernel and boot files /boot/
  • Device files /dev/
  • Root user's home /root/

You will spend most of your time in ~, which is just your home directory. The rest of the tree contains system software, configuration, devices, logs, temporary data, and other files managed by the operating system.

Memory trick

/etc contains system-wide configuration. /var contains variable data such as logs and caches. /dev contains device files. You do not have to memorize the entire filesystem at once; after using GNU/Linux for a while, these paths become familiar naturally.

Chapter III :: Navigation

Getting around: pwd, ls, cd, mkdir

These four commands are the ones you will use constantly. They let you know where you are, see what is there, move around, and create new folders.

pwd prints your current location.

pwd

ls lists files and folders in the current directory. Add -la to see hidden files along with detailed information such as permissions and ownership.

ls
ls -la

cd changes directory. Use cd .. to go up one level. cd ~ takes you straight home.

cd Documents
cd ..
cd ~

mkdir makes a new folder. Add -p to create nested directories when necessary.

mkdir myproject
mkdir -p myproject/src/modules
Memory tricks

pwd = Print Working Directory. ls = LiSt. cd = Change Directory. mkdir = MaKe DIRectory. Say them out loud once and you probably will not forget them.


Working with files: cp, mv, rm, cat, less

cp copies a file. Add -r to copy an entire directory tree.

cp file.txt backup.txt
cp -r myfolder/ myfolder_backup/

mv moves a file or renames it. On a normal filesystem, renaming is essentially changing the file's name or directory entry rather than copying the contents somewhere new.

mv oldname.txt newname.txt
mv file.txt ~/Documents/file.txt

rm removes a file. Unlike a graphical file manager's trash, it does not normally give you a convenient way to undo the operation. Add -r when removing directories. Be careful.

rm file.txt
rm -r oldfolder/

cat writes file contents to standard output. It is especially useful for short text files, and can also concatenate several files together.

cat notes.txt

less opens a file in a pager that you can scroll through. Press q to quit.

less bigfile.txt
Memory tricks

cp = CoPy. mv = MoVe. rm = ReMove. cat comes from conCATenate. less is a pager whose name is a joke based on the older program more.


Reading and searching: head, tail, grep, find

head shows the first 10 lines of a file. tail shows the last 10. Add -n 20 to change the number of lines. tail -f is especially useful for watching a log file as new lines are added.

head notes.txt
tail -n 20 notes.txt
tail -f /var/log/syslog

grep searches for matching text. It prints lines that match the pattern you give it. Add -r to search directories recursively and -i to ignore differences in case.

grep "error" logfile.txt
grep -ri "kernel" /var/log/

The name grep comes from an old Unix command pattern meaning global / regular expression / print. It is one of those names that becomes much less mysterious once you have used it for a while.

find searches for files and directories according to conditions such as name, type, age, or permissions. It is one of the most useful command-line search tools.

find . -name "*.txt"
find /home -name "config.cfg"
Memory tricks

head reads the head of a file. tail reads the tail. grep searches for matching text. find finds files. Not every command needs a clever mnemonic.

Chapter IV :: Processes and Permissions

Processes: ps, top, kill

Everything running on your system is represented by a process, and processes have numeric identifiers called PIDs.

ps shows processes. ps aux is a common way to see a broad view of processes running on the system.

ps aux

top provides a live view of processes and resource usage. Press q to quit. Many systems also have htop, which provides a more interactive interface.

top
htop

kill sends a signal to a process. You normally give it the PID of the process you want to affect. kill -9 sends SIGKILL, which forces the process to terminate and cannot be caught or ignored by that process. It should generally be a last resort.

kill 1234
kill -9 1234
Memory tricks

ps = Process Status. top shows processes and their resource usage. kill sends a signal to a process. Signal 9 is SIGKILL, the forceful one.


sudo, chmod, chown

sudo runs a command with another user's privileges, most commonly those of the root user. You will often use it when installing software or changing system-wide files. It normally asks for your own password.

sudo pacman -Syu
sudo nano /etc/hosts

chmod changes file permissions. Permissions control who can read, write, or execute a file. The numeric form is useful to learn: 755 gives the owner read, write, and execute permissions while everyone else gets read and execute. 644 gives the owner read and write permissions while everyone else gets read-only access.

chmod 755 script.sh
chmod 644 config.txt

chown changes the owner and group associated with a file. The usual format is user:group.

chown eric:eric myfile.txt
sudo chown root:root /etc/someconfig
Memory tricks

sudo is commonly remembered as "superuser do." chmod = CHange MODe. chown = CHange OWNer. For permissions, remember 7 means read + write + execute, 5 means read + execute, and 4 means read only.

Chapter V :: Networking and Power Tools

Networking: ping, ssh, curl, wget

ping sends test packets to a host and reports whether replies are received. It can also show round-trip time. Press Ctrl+C to stop it.

ping google.com

ssh connects to another machine over an encrypted network connection. Once connected, you normally get a shell on the remote machine just as though you were sitting in front of it.

ssh user@192.168.1.10
ssh user@someserver.com
Once you are connected, remember: All your base are belong to us.

curl transfers data to and from URLs and can print the result directly in the terminal. It is especially useful for testing websites, APIs, and network services. wget is also designed for retrieving data from the network and is commonly used for downloading files.

curl https://example.com
wget https://example.com/file.tar.gz
Memory tricks

ping is named after a sonar ping. ssh = Secure SHell. curl is a URL transfer tool. wget is a network retrieval utility.


Pipes, redirection, and man

The pipe character | connects commands together. It takes the standard output of one command and sends it to the standard input of another. This is one of the most powerful ideas in the command-line environment.

# find running processes and search for "firefox"

ps aux | grep firefox


# list files and search for ones containing "config"

ls -la | grep config

Redirection lets you send output somewhere other than the terminal. > writes output to a file and replaces its previous contents. >> appends output to the end of an existing file.

echo "hello" > myfile.txt
echo "another line" >> myfile.txt

man opens the manual page for a command. When you forget how something works, the manual is one of the first places to look.

man ls
man grep
Memory tricks

Think of | as a literal pipe passing output from one program into another. > sends output into a file. >> sends it there without replacing what is already present. man = MANual.

Chapter VI :: Reference

The 25 commands

Here is everything from this guide in one place. These commands are enough to get you surprisingly far in a GNU/Linux terminal, especially once you start combining them.

Command What it does Mnemonic
pwd Print current directory location Print Working Directory
ls List files in current directory LiSt
cd Change directory Change Directory
mkdir Create a new directory MaKe DIRectory
cp Copy a file or directory CoPy
mv Move or rename a file MoVe
rm Remove a file or directory ReMove
cat Print or concatenate file contents conCATenate
less Scroll through a file less does more than more
head Show first N lines of a file read the HEAD
tail Show last N lines or follow live output read the TAIL
grep Search text for matching patterns global / regular expression / print
find Search for files by name or type FIND files
ps Show running processes Process Status
top Live process monitor TOP processes
kill Send a signal to a process KILL a process
sudo Run a command with another user's privileges superuser do
chmod Change file permissions CHange MODe
chown Change file ownership CHange OWNer
ping Test network reachability sonar PING
ssh Connect to a remote machine Secure SHell
curl Transfer data to or from a URL URL transfer tool
wget Retrieve files and other network resources network GET
| Pipe output into another command a literal pipe between programs
man Read the manual for a command MANual

Finished

That's the environment.

You now understand what the Linux kernel is and how it fits into a GNU/Linux system, why the kernel is built the way it is, how it compares with Windows NT and macOS, what the GNU Project has to do with the operating system, what the shell actually does, how the filesystem is laid out, and you have 25 commands with mnemonics to remember them by.

That covers a large part of what you will actually do in a terminal day to day. The rest is mostly learning new tools and combining familiar ones in different ways. Once pipes, redirection, permissions, and the filesystem start to make sense, the terminal becomes much less mysterious.

The best way to get comfortable is to use the terminal for things you would normally do with a file manager. Browse your folders with ls and cd, copy files with cp, search with grep, and read the manuals with man. You will remember far more by actually using the commands than by trying to memorize a list.

And if you ever forget anything, man <command> is usually a good first stop. GNU/Linux systems have a tremendous amount of documentation available locally and online, and learning to use that documentation is part of learning the system itself.

A small parting thought: GNU/Linux is not merely a different way of running programs. It is also the result of many people building software that users are allowed to study, improve, and share. That history is worth knowing, even if all you wanted to do was learn cd.