Navigating Linux
Navigating Linux
Sayan Ghosh
IIT Madras
BS Data Science and Applications
Disclaimer
This document is a companion activity book for the System Commands (BSSE2001) course taught by Prof.
Gandham Phanikumar at IIT Madras BS Program. This book contains resources, references, questions
and solutions to some common questions on Linux commands, shell scripting, grep, sed, awk, and other
system commands.
This was prepared with the help and guidance of the course instructors:
Copyright
© This book is released under the public domain, meaning it is freely available for use and distribution
without restriction. However, while the content itself is not subject to copyright, it is requested that proper
attribution be given if any part of this book is quoted or referenced. This ensures recognition of the original
authorship and helps maintain transparency in the dissemination of information.
Colophon
This document was typeset with the help of KOMA-Script and LATEX using the kaobook class.
The source code of this book is available at:
https://github.com/sayan01/se2001-book
(You are welcome to contribute!)
Edition
Compiled on June 13, 2024
UNIX is basically a simple operating system, but you have to be a genius to
understand the simplicity.
– Dennis Ritchie
Preface
Through this work I have tried to make learning and understanding the basics of Linux fun and easy. I have
tried to make the book as practical as possible, with many examples and exercises. The structure of the book
follows the structure of the course BSSE2001 - System Commands, taught by Prof. Gandham Phanikumar at
IIT Madras BS Program. .
The book takes inspiration from the previous works done for the course,
The book covers basic commands, their motivation, use cases, and examples. The book also covers some
advanced topics like shell scripting, regular expressions, and text processing using sed and awk.
This is not a substitute for the course, but a companion to it. The book is a work in progress and any
contribution is welcome at https://github.com/sayan01/se2001-book
Sayan Ghosh
Contents
Preface v
Contents vii
1 Essentials of Linux 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 What is Linux? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.2 Desktop Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.3 Window Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.4 Why Linux? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.5 What is Shell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.6 Shell vs Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.7 Why the Command Line? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.8 Command Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 Simple Commands in GNU Core Utils . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.1 File System Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.2 Manuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2.3 System Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.2.4 File Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.2.5 Text Processing and Pagers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.2.6 Aliases and Types of Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.2.7 User Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.2.8 Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.3 Navigating the File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.3.1 What is a File System? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.3.2 In Memory File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.3.3 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1.3.4 Basic Commands for Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.4 File Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1.4.1 Read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.4.2 Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.4.3 Execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.4.4 Interesting Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.4.5 Changing Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.4.6 Special Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.4.7 Octal Representation of Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.5 Types of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.5.1 Regular Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.5.2 Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.5.3 Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.5.4 Character Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.5.5 Block Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
1.5.6 Named Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
1.5.7 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.5.8 Types of Regular Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.6 Inodes and Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
1.6.1 Inodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
1.6.2 Separation of Data, Metadata, and Filename . . . . . . . . . . . . . . . . . . . . . . . 48
1.6.3 Directory Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
1.6.4 Hard Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
1.6.5 Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
1.6.6 Symlink vs Hard Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
1.6.7 Identifying Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
1.6.8 What are . and ..? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.1 A Teletype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.2 Ken Thompson . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.3 Dennis Ritchie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
2.4 Xerox Alto, one of the first VDU terminals with a GUI, released in 1973 . . . . . . . . . . . . . . 60
2.5 A first generation Dectape (bottom right corner, white round tape) being used with a PDP-11
computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.6 George Coulouris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.7 Bill Joy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.9 Stevie Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.8 The Keyboard layout of the ADM-3A terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.10 Bram Moolenaar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.11 The inital version of Vim, when it was called Vi IMitation . . . . . . . . . . . . . . . . . . . . . . 64
2.12 Vim 9.0 Start screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.13 Neo Vim Window editing this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.14 Simplified Modes in Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.15 Detailed Modes in Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.16 Vim Cheat Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
2.18 Richard Stallman - founder of GNU and FSF projects . . . . . . . . . . . . . . . . . . . . . . . . 80
2.17 Emacs Logo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.19 Guy L. Steele Jr. combined many divergent TECO with macros to create EMACS . . . . . . . . . 81
2.20 James Gosling - creator of Gosling Emacs and later Java . . . . . . . . . . . . . . . . . . . . . . . 81
2.21 ADM-3A terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
2.22 Space Cadet Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
2.23 Nano Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.1 Types of Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.2 Growth Rate of Different Functions - Note how quickly 𝑛 2 grows . . . . . . . . . . . . . . . . . 87
List of Tables
distribution that lets the user decide which packages they want to
install.
• Manjaro - A distribution based on Arch Linux which is known
for its user-friendliness. It is a rolling release distribution that
is easier to install for new users. It uses a different repository
for packages with additional testing.
• EndeavourOS - A distribution based on Arch Linux which is
known for its simplicity and minimalism. It is a rolling release
distribution that is easier to install for new users. It uses the
same repository for packages as Arch Linux.
• Artix Linux - It uses the OpenRC init system instead of
systemd. It also offers other init systems like runit, s6, dinit.
▶ openSUSE - It is a free and open-source Linux distribution devel-
oped by the openSUSE project. It is offered in two main variations:
1: “Free software” means software that Tumbleweed, an upstream rolling release distribution, and Leap,
respects users’ freedom and community. a stable release distribution which is sourced from SUSE Linux
Roughly, it means that the users have
Enterprise.
the freedom to run, copy, distribute,
study, change and improve the software. • Tumbleweed - Rolling Release upstream.
Thus, “free software” is a matter of
• Leap - Stable Release downstream.
liberty, not price. To understand the
concept, you should think of “free” as ▶ Gentoo - A distribution known for its customizability and per-
in “free speech,” not as in “free beer.” formance. It is a source-based distribution, which means that the
We sometimes call it “libre software,”
borrowing the French or Spanish word user compiles the software from source code. It is known for its
for “free” as in freedom, to show we do performance optimizations for the user’s hardware.
not mean the software is gratis. ▶ Void - It is an independent rolling-release Linux distribution that
uses the X Binary Package System package manager, which was
You may have paid money to get
copies of a free program, or you may designed and implemented from scratch, and the runit init system.
have obtained copies at no charge. But Excluding binary kernel blobs, a base install is composed entirely
regardless of how you got your copies, of free1 software.
you always have the freedom to copy
and change the software, even to sell
copies.
1.1.2 Desktop Environments
- GNU on Free Software
another DE with it. DE are simply packages installed on your distribution, KDE Plasma 28%
and almost all the popular DEs can be installed on all distributions. Many 8%
Others
distributions also come with multiple pre-bundled desktop environments 11%
17%
due to user preferences. Most server distributions and some enthusiast Cinnamon
distributions come with no pre-bundled desktop environment, and let Gnome Shell
the user determine which one is required, or if one is required. Figure 1.2: Desktop Environment Usage
in 2022
You might be wondering "Why should I use Linux?" Most people use
either Windows or Mac on their personal computers. Although these
consumer operating systems get the job done, they don’t let the user
6: Although Linux is just a kernel and completely control their own hardware and software. Linux 6 is a free and
not an entire operating system, through-
open-source operating system that gives the user complete control over
out this book I would be referring to
GNU/Linux, the combination of GNU their system. It is highly customizable and can be tailored to the user’s
core utilities and the Linux kernel, as needs. It is also known for its stability and security. It is used in almost all
Linux in short. servers, supercomputers, and embedded systems. It is also used in many
consumer devices like Android phones, smart TVs, and smartwatches.
In this course we will be covering how to navigate the linux file system,
how to manage files, how to manage the system, and how to write scripts
to automate tasks. In the later part of the course we go over concepts
such as pattern matching and text processing.
This course does not go into details of the linux kernel, but rather attempts
to make the reader familiar with the GNU core utils and able to navigate
around a linux server easily.
The most popular shell in Linux is the bash shell. It is the default shell in
7: POSIX, or Portable Operating System
Interface, is a set of standards that de- most distributions. It is a POSIX-compliant 7 shell. There are many other
fine the interfaces and environment that shells available, such as zsh, fish 8 , dash, csh, ksh, and tcsh. Each shell
operating systems use to access POSIX- has its own features and syntax, but most of the keywords and syntax are
compliant applications. POSIX standards
are based on the Unix operating system
the same. In this course we will be covering only the bash shell and its
and were released in the late 1980s. syntax, but most of what we learn here is also applicable on other shells
8: Fish is a non-POSIX compliant shell as well.
that is known for its features like auto-
suggestion, syntax highlighting, and tab
completions. Although a useful alterna-
tive to other shells for scripting, it should 1.1.6 Shell vs Terminal
not be set as the default shell.
Definition 1.1.5 (Terminal) A terminal is a program that provides a
way to interact with the shell. It is a program that provides a text-based
interface to the shell. It is also known as a terminal emulator.
The terminal is the window that you see when you open a terminal
program. It provides a way to interact with the shell. The shell is the
program that interprets the commands that you type in the terminal.
1.1 Introduction 5
The terminal is the window that you see, and the shell is the program
that runs in that window. Whereas the shell is the application that
is parsing your input and running the commands and keywords, the
terminal is the application that lets you see the shell graphically. There are
multiple different terminal emulators, providing a lot of customization
and beautification to the terminal, as well as providing useful features
such as scroll back, copying and pasting, and so on.
Some popular terminal emulators are:
In most terminal emulators, there are some basic shortcuts that can be
used to make the terminal experience more efficient. Some of the basic
shortcuts are listed in Table Table 1.1.
Both the command line interface (CLI) and the graphical user interface
(GUI) are simply shells over the operating system’s kernel. They let you
interact with the kernel, perform actions and run applications.
GUI:
6 1 Essentials of Linux
The GUI requires a mouse and a keyboard, and is more intuitive and
easier to use for beginners. But it is also slower and less efficient than
the CLI. The GUI severely limits the user’s ability to automate tasks and
perform complex operations. The user can only perform those operations
that the developers of the GUI have thought of and implemented.
CLI:
The CLI is faster and more efficient than the GUI as it lets the user use the
keyboard to perform actions. Instead of clicking on pre-defined buttons,
the CLI lets you construct your instruction to the computer using syntax
and semantics. The CLI lets you combine simple commands that do one
thing well to perform complex operations. The biggest advantage of the
CLI is that it lets you automate tasks. It might be faster for some users
to rename a file from file1 to file01 using the GUI, but it will always be
faster to automate this using the CLI if you want to do this for thousands
of files in the folder.
In this course we will be learning how to use the CLI to navigate the file
system, manage files, manage the system, process text, and write scripts
to automate tasks.
Here, username is the name of the user, hostname is the name of the
computer, and $ indicates that the shell is running as a normal user. The
∼ symbol indicates that the current working directory is the user’s home
9: The home directory is the direc- directory. 9 This prompt can be changed and customized according to
tory where the user’s files and set- the user’s preferences using the PS1 variable discussed in Chapter ??.
tings are stored. It is usually located at
/home/username. This can be shorted to
~ in the shell.
1.2 Simple Commands in GNU Core Utils 7
Definition 1.2.1 (GNU Core Utils) The GNU Core Utilities are the
basic file, shell, and text manipulation utilities of the GNU operating
system. These are the utilities that are used to interact with the
operating system and perform basic operations. a
a GNU Core Utils
Figure 1.4: GNU Core Utils Logo
The shell lets you simply type in the name of the command and press
enter to run it. You can also pass arguments to the command to modify
its behavior. Although the commands are simple, they are powerful and
can be combined to perform complex operations. 10 10: The combination of commands to
perform complex operations is called pip-
ing. This will be covered later.
Some basic commands in the core-utils are listed in Table Table 1.2.
Table 1.2: Basic Commands in GNU Core
Command Description Utils
ls List the contents of a directory
cd Change the current working directory
pwd Print the current working directory
mkdir Create a new directory
rmdir Remove a directory
touch Create a new file
rm Remove a file
cp Copy a file
mv Move a file
echo Print a message
cat Concatenate and display the contents of a file
less Display the contents of a file one page at a time
head Display the first few lines of a file
tail Display the last few lines of a file
find Find files and directories
locate Find files and directories
which Find the location of a command
uname Print system information
ps Display information about running processes
kill Terminate a process
chmod Change the permissions of a file
chown Change the owner of a file
chgrp Change the group of a file
date Print the current date and time
cal, ncal Print a calendar
df Display disk space usage
du Display disk usage
free Display memory usage
top Display system information
history Display the command history
sleep Pause the shell for a specified time
true Do nothing, successfully
false Do nothing, unsuccessfully
tee Read from stdin and write to stdout and files
whoami Print the current user
groups Print the groups the user belongs to
clear Clear the terminal screen
exit Exit the shell
8 1 Essentials of Linux
pwd:
The pwd command prints the current working directory. The current
working directory is the directory that the shell is currently in. The shell
starts in the user’s home directory when it is opened. The pwd command
prints the full path of the current working directory.
1 $ pwd
2 /home/username
ls:
ls can also list the details of the files using the -l flag.
1 $ ls -l
2 total 24
3 drwxr-xr-x 2 username group 4096 Mar 1 12:00 Desktop
4 drwxr-xr-x 2 username group 4096 Mar 1 12:00 Documents
5 drwxr-xr-x 2 username group 4096 Mar 1 12:00 Downloads
6 drwxr-xr-x 2 username group 4096 Mar 1 12:00 Music
7 drwxr-xr-x 2 username group 4096 Mar 1 12:00 Pictures
8 drwxr-xr-x 2 username group 4096 Mar 1 12:00 Videos
As seen in Figure 1.5, the first column is the file type and permissions.
The second column is the number of links to the file or directory. The
third and fourth columns are the owner and group of the file or directory.
Figure 1.5: ls -l Output The fifth column is the size of the file or directory. The sixth, seventh,
and eighth columns are the last modified date and time of the file or
12: More details about the file permis-
directory. The ninth column is the name of the file or directory. 12
sions and the file types will be covered
later in the course. We can also list the inode numbers 13 using the -i flag.
13: An inode is a data structure on a
1 $ ls -i
filesystem on Linux and other Unix-like
2 123456 Desktop 123457 Documents 123458 Downloads 123459 Music
operating systems that stores all the in-
formation about a file except its name 123460 Pictures 123461 Videos
and its actual data. This includes the file
type, the file’s owner, the file’s group, Inodes will be discussed in detail later in the course.
the file’s permissions, the file’s size, the
file’s last modified date and time, and cd:
the file’s location on the disk. The inode
is the reference pointer to the data in the The cd command changes the current working directory. It takes the path
disk. to the directory as an argument.
1.2 Simple Commands in GNU Core Utils 9
1 $ cd Documents
2 $ pwd
3 /home/username/Documents
Question 1.2.1 ls can only show files and directories in the cwd15 , 15: cwd means Current Working Direc-
not subdirectories. True or False? tory
Answer 1.2.1 False. ls can show files and directories in the cwd, and
also in subdirectories. The -R flag can be used to show files and
directories in subdirectories, recursively.
1.2.2 Manuals
man:
How to remember so many flags and options for each of the commands?
The man command is used to display the manual pages for a command.
1 $ man ls
This will display the manual page for the ls command. The manual page
is divided into sections, and you can navigate through the sections using
the arrow keys. Press q to exit the manual page.
4 NAME
5 ls - list directory contents
6
7 SYNOPSIS
8 ls [OPTION]... [FILE]...
9
10 DESCRIPTION
11 List information about the FILEs (the current directory by
default). Sort entries alphabetically if none of -cftuvSUX
nor --sort is specified.
12
13 Mandatory arguments to long options are mandatory for short
options too.
14
15 -a, --all
16 do not ignore entries starting with .
17
18 -A, --almost-all
19 do not list implied . and ..
20 ...
The manual page provides information about the command, its syntax,
options, and usage. It is a good practice to refer to the manual page of a
command before using it.
To exit the manual page, press q.
There are multiple sections in the manual page, man takes the section
number as an argument to display the manual page from that section.
1 $ man 1 ls
This will display the manual page for the ls command from section 1.
The details of the sections can be seen in Table Table 1.3.
Table 1.3: Manual Page Sections
Section Description
1 User Commands
2 System Calls
3 Library Functions
4 Special Files usually found in /dev
5 File Formats and conventions
6 Games
7 Miscellaneous
8 System Administration
9 Kernel Developer’s Manual
Man pages only provide information about the commands and utilities
that are installed on the system. They do not provide information about
the shell builtins or the shell syntax. For that, you can refer to the shell’s
documentation or use the help command.
Some commands also have a --help flag that displays the usage and
options of the command.
Some commands have their own info pages, which are more detailed
than the man pages.
To be proficient with shell commands, one needs to read the man, info,
16: An useful video by Robert Elder
about the differences between man, info,
and help pages. 16
and help can be found on YouTube.
1.2 Simple Commands in GNU Core Utils 11
Exercise 1.2.1 Run man, info, and --help on all the commands
discussed in this section. Note the differences in the information
provided by each of them. Read the documentations carefully and try
to understand how each command works, and the pattern in which
the documentations are written.
info:
The info command is used to display the info pages for a command. The
info pages are more detailed than the man pages for some commands. It
is navigable like a hypertext document. There are links to chapters inside
the info pages that can be followed using the arrow keys and entered
using the enter key. The Table Table 1.4 lists some of the keys that can be
used to navigate the info pages.
Table 1.4: Keys in Info Pages
Key Description
h Display the help page
q Exit the info page
n Move to the next node
p Move to the previous node
u Move up one level
d Move down one level
l Move to the last node
t Move to the top node
g Move to a specific node
<enter> Follow the link
m Display the menu
s Search for a string
S Search for a string (case-sensitive)
help:
The help command is a shell builtin that displays information about the
shell builtins and the shell syntax.
1 $ help read
This will list the information about the read builtin command.
The help command can also be used to display information about the
shell syntax.
1 $ help for
This will list the information about the for loop in the shell.
Help pages are not paged, and the output is displayed in the terminal.
To page the output, one can use the less command.
1 $ help read | less
uname:
The uname command prints system information. It can take flags to print
specific information about the system. By default, it prints only the kernel
name.
12 1 Essentials of Linux
1 $ uname
2 Linux
17: Here rex is the hostname of the sys- The -a flag prints all the system information. 17
tem, 6.8.2-arch2-1 is the kernel ver-
sion, x86_64 is the architecture, and 1 $ uname -a
GNU/Linux is the operating system. 2 Linux rex 6.8.2-arch2-1 #1 SMP PREEMPT_DYNAMIC Thu, 28 Mar 2024
17:06:35 +0000 x86_64 GNU/Linux
ps:
There are a lot of flags that can be passed to the ps command to display
more information about the processes. These will be covered in Chapter
??.
kill:
The kill command can also take the signal number as an argument to
send a signal to the process. For example, the SIGKILL signal can be sent
19: The SIGKILL signal is used to termi-
nate a process immediately. It cannot be to the process to terminate it. 19
caught or ignored by the process. It is
1 $ kill -9 12345
numbered as 9.
free:
The free command is used to display the amount of free and used
memory in the system.
1.2 Simple Commands in GNU Core Utils 13
1 $ free
2 total used free shared buff/
cache available
3 Mem: 8167840 1234560 4567890 123456
2367890 4567890
4 Swap: 2097148 0 2097148
The free command can take the -h flag to display the memory in
human-readable format.
1 $ free -h
2 total used free shared buff/
cache available
3 Mem: 7.8Gi 1.2Gi 4.3Gi 120Mi
2.3Gi 4.3Gi
4 Swap: 2.0Gi 0B 2.0Gi
df:
The df command is used to display the amount of disk space available
on the filesystems.
1 $ df
2 Filesystem 1K-blocks Used Available Use% Mounted on
3 /dev/sda1 12345678 1234567 11111111 10% /
4 /dev/sda2 12345678 1234567 11111111 10% /home
The df command can take the -h flag to display the disk space in
human-readable format.
1 $ df -h
2 Filesystem Size Used Avail Use% Mounted on
3 /dev/sda1 12G 1.2G 9.9G 11% /
4 /dev/sda2 12G 1.2G 9.9G 11% /home
du:
The du command is used to display the disk usage of directories and files.
By default, it displays the disk usage of the current directory.
1 $ du
2 4 ./Desktop
3 4 ./Documents
4 4 ./Downloads
5 4 ./Music
6 4 ./Pictures
7 4 ./Videos
8 28
The du command can take the -h flag to display the disk usage in
human-readable format. The -s flag displays the total disk usage of the
directory.
1 $ du -sh
2 28K .
Answer 1.2.2 uname -r will print the kernel version of your system.
14 1 Essentials of Linux
Question 1.2.3 How to see how long your system is running for?
What about the time it was booted up?
Answer 1.2.3 uptime will show how long the system is running for.
uptime -s will show the time the system was booted up.
The -s flag is to show the time of last boot.
Question 1.2.4 How to see the amount of free memory? What about
free hard disk space? If we are unable to understand the big numbers,
how to convert them to human readable format? What is difference
between MB and MiB?
file:
The file command is used to determine the type of a file. It can take
multiple file names as arguments.
1 $ file file1
2 file1: ASCII text
3 $ file /bin/bash
4 /bin/bash: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV
), dynamically linked, interpreter /lib64/ld-linux-x86-64.so
.2, for GNU/Linux 3.2.0, BuildID[sha1]=1234567890abcdef,
stripped
mkdir:
The mkdir command is used to create new directories. It can take multiple
directory names as arguments.
1 $ mkdir a b c
2 $ ls -F
3 a/ b/ c/
1.2 Simple Commands in GNU Core Utils 15
Exercise 1.2.2 Run man ls to find out what the -F flag does, and why
we used it in the above example.
touch:
The touch command is used to create new files. It can take multiple file
names as arguments. If a file is already present, the touch command
updates the last modified date and time of the file, but does not modify
the contents of the file.
1 $ touch file1 file2 file3
2 $ ls -l
3 -rw-r--r-- 1 username group 0 Mar 1 12:00 file1
4 -rw-r--r-- 1 username group 0 Mar 1 12:00 file2
5 -rw-r--r-- 1 username group 0 Mar 1 12:00 file3
6 $ sleep 60
7 $ touch file3
8 $ ls -l
9 -rw-r--r-- 1 username group 0 Mar 1 12:00 file1
10 -rw-r--r-- 1 username group 0 Mar 1 12:00 file2
11 -rw-r--r-- 1 username group 0 Mar 1 12:01 file3
Exercise 1.2.3 Notice the difference in the last modified date and time
of the file3 file from the other files. Also notice the sleep command
used to pause the shell for 60 seconds.
rmdir:
Remark 1.2.2 The rmdir command can only remove empty directories.
This is a safety feature so that users dont accidentally delete directories
with files in them. To remove directories with files in them along with
those files, use the rm command.
rm:
To force rm to always ask for confirmation before deleting files, use the
-i flag.
1 $ rm -i file3
2 rm: remove regular empty file ’file3’? y
cp:
The cp command is used to copy files. It takes the source file and the
destination file as arguments.
1 $ touch file1
2 $ ls -F
3 file1
4 $ cp file1 file2
5 $ ls -F
6 file1 file2
Exercise 1.2.4 Why did we use the -R flag in the above example?
What does it do?
mv:
echo:
The echo command is used to print a message to the terminal. It can take
multiple arguments and print them to the terminal.
1 $ echo Hello, World!
2 Hello, World!
The echo command can also take the -e flag to interpret backslash
escapes.
1 $ echo -e "Hello, \nWorld!"
2 Hello,
3 World!
Some escape characters in echo are listed in Table Table 1.5 on the
following page.
cat:
1 $ cat file1
2 Hello, World! from file1
The cat command can take multiple files as arguments and display their
contents one after another.
1 $ cat file1 file2
2 Hello, World! from file1
3 Hello, World! from file2
less:
1 $ head -n 5 /etc/passwd
2 root:x:0:0:root:/root:/usr/bin/bash
3 bin:x:1:1::/:/usr/bin/nologin
4 daemon:x:2:2::/:/usr/bin/nologin
5 mail:x:8:12::/var/spool/mail:/usr/bin/nologin
6 ftp:x:14:11::/srv/ftp:/usr/bin/nologin
Remark 1.2.3 Here we are listing the file /etc/passwd which contains
information about the users on the system. The file will usually be
present on all Unix-like systems and have a lot of system users. 24 24: A system user is a user that is used by
the system to run services and daemons.
It does not belong to any human and
tail: usually is not logged into. System users
have a user ID less than 1000.
The tail command is used to display the last few lines of a file. By
default, it displays the last 10 lines of a file.
1 $ tail /etc/passwd
2 rtkit:x:133:133:RealtimeKit:/proc:/usr/bin/nologin
3 sddm:x:964:964:SDDM Greeter Account:/var/lib/sddm:/usr/bin/
nologin
4 usbmux:x:140:140:usbmux user:/:/usr/bin/nologin
5 sayan:x:1000:1001:Sayan:/home/sayan:/bin/bash
6 qemu:x:962:962:QEMU user:/:/usr/bin/nologin
7 cups:x:209:209:cups helper user:/:/usr/bin/nologin
8 dhcpcd:x:959:959:dhcpcd privilege separation:/:/usr/bin/nologin
9 saned:x:957:957:SANE daemon user:/:/usr/bin/nologin
The tail command can take the -n flag to display the last n lines of a
file.
1 $ tail -n 5 /etc/passwd
2 sayan:x:1000:1001:Sayan:/home/sayan:/bin/bash
3 qemu:x:962:962:QEMU user:/:/usr/bin/nologin
4 cups:x:209:209:cups helper user:/:/usr/bin/nologin
5 dhcpcd:x:959:959:dhcpcd privilege separation:/:/usr/bin/nologin
6 saned:x:957:957:SANE daemon user:/:/usr/bin/nologin
Exercise 1.2.8 Notice that the UID (3rd column) of the sayan user
is 1000. The last column is /bin/bash instead of /usr/bin/nologin
like others. This is because it is a regular user and not a system user.
wc:
The wc command is used to count the number of lines, words, and
characters in a file. By default, it displays the number of lines, words,
and characters in a file.
1 $ wc /etc/passwd
2 43 103 2426 /etc/passwd
We can also use the -l, -w, and -c flags to display only the number of
lines, words, and characters respectively.
1 $ wc -l /etc/passwd
2 43 /etc/passwd
20 1 Essentials of Linux
Answer 1.2.5 cat file1 file2 file3 will print the contents of
file1, file2, and file3 in the order given. The contents of the files
will be printed one after the other.
Answer 1.2.6 Yes, cat > file1 will write to file1. The input will
be taken from the terminal and written to file1. The input will be
written to file1 until the user presses Ctrl+D to indicate end of
input. This is redirection, which we see in later weeks.
Question 1.2.7 How to list only first 10 lines of a file? How about first
5? Last 5? How about lines 105 to lines 152?
Answer 1.2.7 head filename will list the first 10 lines of filename.
head -n 5 filename will list the first 5 lines of filename.
tail -n 5 filename will list the last 5 lines of filename.
head -n 152 filename | tail -n 48 will list lines 105 to 152 of
filename. This uses | which is a pipe, which we will see in later
weeks.
Question 1.2.8 Do you know how many lines a file contains? How
can we count it? What about words? Characters?
alias:
But this alias is temporary and will be lost when the shell is closed. To
make the alias permanent, add it to the shell configuration file. For bash,
this is the .bashrc file in the home directory.
1 $ echo "alias ll=’ls -l’" >> ~/.bashrc
This will add the alias to the .bashrc file. To make the changes take
effect, either close the terminal and open a new one, or run the source
command.
1 $ source ~/.bashrc
We can see the aliases that are currently set using the alias command.
1 $ alias
2 alias ll=’ls -l’
We can also see what a particular alias expands to using the alias
command with the alias name as an argument.
22 1 Essentials of Linux
1 $ alias ll
2 alias ll=’ls -l’
which:
The which command is used to show the path of the command that will
be executed.
1 $ which ls
2 /sbin/ls
We can also list all the paths where the command is present using the -a
flag.
1 $ which -a ls
2 /sbin/ls
3 /bin/ls
4 /usr/bin/ls
5 /usr/sbin/ls
This means that if we delete the /sbin/ls file, the /bin/ls file will be
executed when we run the ls command.
whatis:
Here the brackets show the section of the manual where the command is
present. This short exerpt is taken from its man page itself.
whereis:
locate:
The locate command is used to find files by name. The file can be present
anywhere in the system and if it is indexed by the mlocate database, it
can be found using the locate command.
1 $ touch tmp/48/hellohowareyou
2 $ pwd
3 /home/sayan
4 $ locate hellohowareyou
5 /home/sayan/tmp/48/hellohowareyou
Note: you may have to run updatedb to update the database before using
locate. This can only be run by the root user or using sudo.
type:
The type command is used to show how the shell will interpret a
command. Usually some commands are both an executable and a shell
built-in. The type command will show which one will be executed.
1 $ type ls
2 ls is hashed (/sbin/ls)
3 $ type cd
4 cd is a shell builtin
We can also use the -a flag to show all the ways the command can be
interpreted.
1 $ type -a pwd
2 pwd is a shell builtin
3 pwd is /sbin/pwd
4 pwd is /bin/pwd
5 pwd is /usr/bin/pwd
6 pwd is /usr/sbin/pwd
Here we can see that the pwd command is a shell built-in, and is also
present in multiple locations in the system. But if we run the pwd com-
mand, the shell built-in will be executed.
type is also useful when you are not sure whether to use man or help
for a command. Generally for a shell built-in, help is used, and for an
executable the info and the man pages are used.
The type command will show which type the command is.
Exercise 1.2.11 Find the path of the true command using which.
Find a short description of the true command using whatis. Is the
executable you found actually the one that is executed when you run
true? Check using type true
whoami:
The whoami command is used to print the username of the current user.
1 $ whoami
2 sayan
groups:
The groups command is used to display the groups that the current user
belongs to.
1.2 Simple Commands in GNU Core Utils 25
1 $ groups
2 sys wheel rfkill autologin sayan
passwd
The passwd command is used to change the password of the current user.
26: This executable is a special one, as
The root user can also change the password of other users. 26 it is a setuid program. This will be dis-
cussed in detail in Subsection 1.4.6.
who:
The who command is used to display the users who are currently logged
in.
1 $ who
2 sayan tty2 2024-05-22 13:49
3 sayan pts/0 2024-05-22 15:58 (:0)
4 sayan pts/1 2024-05-22 15:58 (tmux(1082933).%2)
5 sayan pts/2 2024-05-22 15:58 (tmux(1082933).%1)
6 sayan pts/3 2024-05-22 15:58 (tmux(1082933).%3)
7 sayan pts/4 2024-05-22 15:58 (tmux(1082933).%4)
8 sayan pts/5 2024-05-22 15:58 (tmux(1082933).%5)
9 sayan pts/6 2024-05-22 15:58 (tmux(1082933).%6)
10 sayan pts/7 2024-05-22 15:58 (tmux(1082933).%7)
11 sayan pts/8 2024-05-22 15:58 (tmux(1082933).%8)
12 sayan pts/9 2024-05-22 15:58 (tmux(1082933).%9)
13 sayan pts/10 2024-05-22 17:58 (:0)
14 sayan pts/11 2024-05-22 18:24 (tmux(1082933).%10)
15 sayan pts/12 2024-05-22 18:24 (tmux(1082933).%11)
Exercise 1.2.12 Run the who command on the system commands VM.
What is the output?
w:
The w command is used to display the users who are currently logged in
and what they are doing.
1 $ w
2 19:47:07 up 5:57, 1 user, load average: 0.77, 0.80, 0.68
3 USER TTY LOGIN@ IDLE JCPU PCPU WHAT
4 sayan tty2 13:49 5:57m 19:10 21.82s dwm
This is different from the who command as it only considers the login
shell. Here dwm is the window manager running on the tty2 terminal.
date:
The date command is used to print formatted date and time information.
Without any arguments, it prints the current date and time.
1 $ date
2 Mon May 20 06:23:07 PM IST 2024
We can specify the date and time to be printed using the -d flag.
26 1 Essentials of Linux
Exercise 1.2.13 Why did we get an error when trying to print the
date 2019-02-29?
We can also modify the format of the date and time using the + flag and
different format specifiers. Some of the important format specifiers are
listed in Table Table 1.6. Rest of the format specifiers can be found in the
date manual page.
We can even mention relative dates and times using the date command.
1 $ date -d "next year"
2 Tue May 19 06:23:07 PM IST 2025
3 $ date -d "next month"
4 Thu Jun 20 06:23:07 PM IST 2024
5 $ date -d "tomorrow"
6 Tue May 21 06:23:07 PM IST 2024
7 $ date -d "yesterday"
8 Sun May 19 06:23:07 PM IST 2024
cal:
We can specify the month and year to print the calendar of that month
and year.
1 $ cal 2 2024
2 February 2024
3 Su Mo Tu We Th Fr Sa
4 1 2 3
5 4 5 6 7 8 9 10
6 11 12 13 14 15 16 17
7 18 19 20 21 22 23 24
8 25 26 27 28 29
There are multiple flags that can be passed to the cal command to display
different types of calendars and of multiple months or of entire year.
Question 1.2.14 How to print the current date and time in some
custom format?
Answer 1.2.14 date -d today +%Y-%m-%d will print the current date
in the format YYYY-MM-DD. The format can be changed by changing
the format specifiers. The format specifiers are given in the man date
page. The -d today can be dropped, but is mentioned to show that
the date can be changed to any date. It can be strings like ’2024-01-01’
or ’5 days ago’ or ’yesterday’, etc.
These are some of the basic commands that are used in the terminal.
Each of these commands has many more options and flags that can be
used to customize their behavior. It is left as an exercise to the reader to
explore the manual pages of these commands and try out the different
options and flags.
Many of the commands that we have
discussed here are also explained in the
form of short videos on Robert Elder’s
Youtube Channel.
28 1 Essentials of Linux
Unlike Windows which has different drive letters for different partitions,
Linux follows a unified file structure. The filesystem hierarchy is a tree of
27: A non-directory is a leaf node of a directories and files27 . The root28 of the filesystem tree is the directory
tree.
/. The basic filesystem hierarchy structure can be seen in Figure 1.6 and
28: The root of a tree is the first node
from which the tree originates. A tree Table Table 1.7.
can have only one root.
But what does so many directories mean? What do they do? What is the
purpose of each directory?
Some directories do not store data on the disk, but are used to store
information about the system. These directories are called virtual directo-
ries. For example, the /proc directory is a virtual directory that provides
information about the running processes. The /sys directory is another
virtual directory that provides information about the system. The /tmp is
a volatile directory whose data is deleted as soon as the system is turned
off. The /run directory is another volatile directory that stores runtime
data.
Figure 1.6: Linux Filesystem Hierarchy Rest directories are stored on the disk. The reason for having so many
directories is to categorize the type of files they store. For example, all
29: In Linux, you do not install appli- the executable binaries of different applications and utilities installed in
cations by downloading them from the the system is stored in /bin and /sbin directories. All the shared libraries
internet and running an installer like in
installed on the system are stored in /lib directory. Sometimes some
Windows. You use a package manager
to install applications. The package man- applications are installed in /opt directory which are not installed directly
ager downloads the application from the by the package manager. 29
internet and installs it on your system
automatically. This way the package man- We also need to store the user’s documents and files. This is done in the
ager can also keep track of the installed /home directory. Each user has their own directory in the /home directory.
applications and their dependencies and
whether they should be updated. This
The root user’s directory is /root. All the application’s configuration files
is similar to the Play Store on mobile are stored in the user’s home directory in the /home directory itself. This
phones.
1.3 Navigating the File System 29
Some file systems like proc, sys, dev, run, and tmp are not stored on the
disk, but are stored in memory.
30 1 Essentials of Linux
They have a special purpose and are used to store information about the
system. These are called virtual directories.
These cannot be stored in a disk as it would be too slow to access them.
Many of these files are very short lived yet are accessed very frequently.
So these are stored in memory to speed up the access.
/dev and /run are mounted as tmpfs filesystems.
This can be seen by running the mount command or the df command.
1 $ mount
2 /dev/sda1 on / type ext4 (rw,noatime)
3 devtmpfs on /dev type devtmpfs (rw,nosuid,size=4096k,nr_inodes
=990693,mode=755,inode64)
4 tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64)
5 devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,
mode=620,ptmxmode=000)
6 sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
7 securityfs on /sys/kernel/security type securityfs (rw,nosuid,
nodev,noexec,relatime)
8 cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,
relatime,nsdelegate,memory_recursiveprot)
9 pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,
relatime)
10 efivarfs on /sys/firmware/efi/efivars type efivarfs (rw,nosuid,
nodev,noexec,relatime)
11 bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode
=700)
12 configfs on /sys/kernel/config type configfs (rw,nosuid,nodev,
noexec,relatime)
13 proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
14 tmpfs on /run type tmpfs (rw,nosuid,nodev,size=1590108k,nr_inodes
=819200,mode=755,inode64)
15 systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd
=36,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino
=5327)
16 hugetlbfs on /dev/hugepages type hugetlbfs (rw,nosuid,nodev,
relatime,pagesize=2M)
17 mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime
)
18 debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,
relatime)
19 tracefs on /sys/kernel/tracing type tracefs (rw,nosuid,nodev,
noexec,relatime)
20 fusectl on /sys/fs/fuse/connections type fusectl (rw,nosuid,nodev,
noexec,relatime)
21 systemd-1 on /data type autofs (rw,relatime,fd=47,pgrp=1,timeout
=60,minproto=5,maxproto=5,direct,pipe_ino=2930)
22 tmpfs on /tmp type tmpfs (rw,noatime,inode64)
23 /dev/sda4 on /efi type vfat (rw,relatime,fmask=0137,dmask=0027,
codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=
remount-ro)
24 /dev/sda2 on /home type ext4 (rw,noatime)
25 binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,
nosuid,nodev,noexec,relatime)
26 tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size
=795052k,nr_inodes=198763,mode=700,uid=1000,gid=1001,inode64)
27 portal on /run/user/1000/doc type fuse.portal (rw,nosuid,nodev,
relatime,user_id=1000,group_id=1001)
1.3 Navigating the File System 31
proc:
These folders are simply for information and do not store any data. This
is why they have a size of 0. Each folder is owned by the user who started
the process.
Inside each of these directories, there are files that contain information
about the process.
You can enter the folder of a process that is started by you and see the
information about the process.
1 $ cd /proc/301408
2 $ ls -l | head -n15
3 total 0
4 -r--r--r-- 1 sayan sayan 0 May 23 16:55 arch_status
5 dr-xr-xr-x 2 sayan sayan 0 May 23 16:55 attr
6 -rw-r--r-- 1 sayan sayan 0 May 23 16:55 autogroup
7 -r-------- 1 sayan sayan 0 May 23 16:55 auxv
8 -r--r--r-- 1 sayan sayan 0 May 23 16:55 cgroup
9 --w------- 1 sayan sayan 0 May 23 16:55 clear_refs
10 -r--r--r-- 1 sayan sayan 0 May 23 16:55 cmdline
11 -rw-r--r-- 1 sayan sayan 0 May 23 16:55 comm
12 -rw-r--r-- 1 sayan sayan 0 May 23 16:55 coredump_filter
13 -r--r--r-- 1 sayan sayan 0 May 23 16:55 cpu_resctrl_groups
14 -r--r--r-- 1 sayan sayan 0 May 23 16:55 cpuset
15 lrwxrwxrwx 1 sayan sayan 0 May 23 16:55 cwd -> /home/sayan/docs/
projects/sc-handbook
16 -r-------- 1 sayan sayan 0 May 23 16:55 environ
17 lrwxrwxrwx 1 sayan sayan 0 May 23 13:41 exe -> /usr/bin/entr
Here you can see that the command line of the process is stored in the
cmdline file. Here the process is of a command called entr.
32 1 Essentials of Linux
You can also see the current working directory (cwd) of the process.
There are some other files in the /proc directory that contain information
about the system.
sys:
Sys is a newer filesystem that is used to store information about the system.
It is neatly organized and is easier to navigate than proc. This highly uses
33: A symlink is a special type of file symlinks to organize the folders while maintaining redundancy as well.
that points to another file or directory. It 33
is similar to a shortcut in Windows. This
will be discussed in detail in Subsection Try running the following code snippet in a terminal if you have a
??.
caps lock key on your keyboard and are running linux directly on your
34: This will only work on your own
system, not on the system commands bare-metal. 34
VM, since you do not have the priviledge
1 $ cd /sys/class/leds
to modify the files there. Make sure you
have the ability to run commands as root 2 $ echo 1 | sudo tee *capslock/brightness
and are able to use sudo. It is also unlikely
to work on a virtual machine. It will also If you are running a linux system directly on your hardware, you will
not work on linux systems older than 2.6. see the caps lock key light up. Most modern keyboards will quickly turn
off the light again as the capslock is technically not turned on, only the
led was turned on manually by you.
1.3.3 Paths
To refer to and identify the directory you are talking about, we use a
path.
Absolute Path:
The traversal to the directory from the root directory is called the absolute
path. For example, if we want to refer to the directory named alex inside
the directory home in the root of the file system, then it is qualified as:
1 /home/alex
Relative Path:
The traversal to the directory from the current working directory is called
the relative path. For example, if we want to refer to the directory named
alex inside the directory home from the /usr/share directory, then it will
be qualified as:
1 ../../home/alex
Remark 1.3.1 The .. in the path refers to the parent directory. It is used
Figure 1.7: Relative Path
in relative paths to refer to directories whose path requires travelling
up the tree.
The file system can be navigated in the Linux command line using the
following commands:
▶ pwd: Print the current working directory
▶ ls: List the contents of the current directory
▶ cd: Change the current working directory
▶ mkdir: Create a new directory
▶ rmdir: Remove a directory
▶ touch: Create a new file
▶ rm: Remove a file
▶ pushd: Push the current directory to a stack
▶ popd: Pop the current directory from a stack37 37: pushd and popd are useful for
quickly switching between directories
More details about these commands can be found in their respective man in scripts.
pages. For example, to find more about the ls command, you can type
man ls.
Question 1.3.1 What is the command to list the contents of the current
directory?
Answer 1.3.1 ls
34 1 Essentials of Linux
Question 1.3.2 What is the command to list the contents of the current
directory including hidden files?
Answer 1.3.2 ls -a
Question 1.3.3 What is the command to list the contents of the current
directory in a long list format? (show permissions, owner, group, size,
and time)
Answer 1.3.3 ls -l
Question 1.3.4 What is the command to list the contents of the current
directory in a long list format and also show hidden files?
Answer 1.3.5 ls -1
This can also be done by passing the output of ls to cat or storing
the output of ls in a file and then using cat to print it. We will see
38: that is a one, not an L these in later weeks.38
1.4 File Permissions 35
We have already briefly seen how to see the permissions of a file using
the ls -l command.
1 $ touch hello.txt
2 $ mkdir world
3 $ ls -l
4 total 4
5 -rw-r--r-- 1 sayan sayan 0 May 21 15:20 hello.txt
6 drwxr-xr-x 2 sayan sayan 4096 May 21 15:21 world
▶ The first character shows the type of the file. - for a regular file and
39: There are other types of files as well,
d for a directory and more. 39 like l for a symbolic link, c for a character
▶ The next three characters show the permissions for the owner of device, b for a block device, s for a socket,
the file. and p for a pipe. These will be discussed
later.
▶ The next three characters show the permissions for the group of
the file.
▶ The last three characters show the permissions for others.
Definition 1.4.4 (Others) Others are all the users who are not the
owner of the file and are not in the group of the file. This is symboli-
cally coded as o.
36 1 Essentials of Linux
There are three actions that can be performed on a file: read, write, and
execute.
1.4.1 Read
▶ For a file, the read permission allows the file to be read. You can
use commands like cat or less to read the contents of the file if
the user has read permissions.
▶ For a directory, the read permission allows the directory to be listed
using ls.
1.4.2 Write
▶ For a file, the write permission allows the file to be modified. You
41: Redirection is a way to send the
can use commands like echo along with redirection 41 or a text
output of a command to a file.
editor like vim or nano to write to the file if the user has write
permissions.
▶ For a directory, the write permission allows the directory to be
modified. You can create, delete, or rename files in the directory if
the user has write permissions.
1.4.3 Execute
▶ For a file, the execute permission allows the file to be executed. This
is usually only needed for special files like executables, scripts, or
libraries. You can run the file as a program if the user has execute
permissions.
▶ For a directory, the execute permission allows the directory to
be traversed into. You can change to the directory if the user
has execute permissions using cd. You can also only long-list the
contents of the directory if the user has execute permissions on
that directory.
the modified version of the file with the same name. But if you try to
simply modify the file directly, you will get permission error.
1 $ mkdir test
2 $ cd test
3 $ echo "hello world" > file1
4 $ chmod 400 file1 # 400 means read permission only
5 $ cat file1
6 hello world
7 $ echo "hello universe" > file1 # unable to write
8 -bash: file1: Permission denied
9 $ rm file1 # can remove as we have write permission on folder
10 rm: remove write-protected regular file ’file1’? y
11 $ echo "hello universe" > file1 # can create new file
12 $ cat file1
13 hello universe
However, this only works on files. You cannot remove a directory if you
do not have write permission on the directory, even if you have write
permission on its parent directory.
If you have all the permissions to a directory, but dont have execute
permission on its parent directory, you cannot access the subdirectory, or
even list its contents.
38 1 Essentials of Linux
1 $ mkdir test
2 $ mkdir test/test2 # subdirectory
3 $ touch test/test2/1 # file inside subdirectory
4 $ chmod 700 test/test2 # all permissions to subdirectory
5 $ chmod 600 test # removing execute permission from parent
directory
6 $ ls test
7 test2
8 $ cd test/test2 # cannot access subdirectory
9 -bash: cd: test/test2: Permission denied
10 $ ls test/test2 # cannot even list contents of subdirectory
11 ls: cannot access ’test/test2’: Permission denied
Synopsis:
1 chmod [OPTION]... MODE[,MODE]... FILE...
2 chmod [OPTION]... OCTAL-MODE FILE...
SetUID:
This is useful for programs that need to access system resources that are
only available to the owner or group of the file.
SetGID:
Exercise 1.4.1 Log into the system commands VM and cd into the
/tmp directory. Create a file in the /tmp directory. Try to find if there
are files created by other users in the /tmp directory using ls -l
command. If there are files created by other users, try to delete them.
a
The permissions of a file for the file’s owner, group, and others can be
43: If the octal is 4 digits, the first digit is represented as a 3 or 4 digit octal number. 43 Each of the octal digits is
for special permissions like setuid, setgid,
and sticky bit.
the sum of the permissions for the owner, group, and others.
Thus if a file has read, write, and execute permissions for the owner, read
and execute permissions for the group, and only read permission for
others, the octal representation of the permissions would be 751.
The octal format is usually used more than the symbolic format as it is
easier to understand and remember and it is more concise.
1.4 File Permissions 41
Answer 1.4.1 ls -l
The permissions are the first 10 characters of the output.
stat -c %A filename will list only the permissions of a file.
There are other format specifiers of stat to show different statistics
which can be found in man stat.
We had briefly seen that the output of ls -l shows the type of the file as
the first character of the permissions.
There are 7 types of files in a linux file system as shown in Table
Table 1.10.
Table 1.10: Types of Files
Directories are files that contain a list of other files. Directories do not
contain data, they contain references to other files. Usually the size of
a directory is equal to the block size of the filesystem. Directories have
some special permissions that are different from regular files as discussed
in Section 1.4.
Symbolic links are files that point to other files. They only consume the
space of the path they are pointing to. Symlinks 44 are useful to create 44: Symlinks is short for symbolic links.
Here the event and mouse files are character devices that represent input
devices like the keyboard and mouse. Note the c in the permissions,
which indicates that these are character devices.
Block devices are files that represent devices that are accessed as a block
of data. For example hard drives, SSDs, and USB drives are block devices.
These files also do not store actual data on the disk, but represent devices.
Any block file can be mounted as a filesystem. We can interact with
block devices using the read and write system calls to interact with the
hardware directly. For example, the /dev/sda file represents the first
hard drive in the system.
Here if is the input file, of is the output file, bs is the block size, and
status is to show the progress of the operation.
data to another process. A process can only write data to a named pipe if
another process is reading from the named pipe. 48 48: and vice versa
1 $ mkfifo pipe1
2 $ ls -l pipe1
3 prw-r--r-- -1 sayan sayan 0 May 22 18:22 pipe1
Here the p in the permissions indicates that this is a named pipe. If you
now try to write to the named pipe, the command will hang until another
process reads from the named pipe. Try the following in two different
terminals:
Terminal 1:
1 $ echo "hello" > pipe1
Terminal 2:
1 $ cat pipe1
You will notice that whichever command you run first will hang until
the other command is run.
1.5.7 Sockets
Terminal 2:
1 $ echo "hello" | nc -U socket.sock
Notice here, that if you run the command in terminal 2 first, it will error
out with the text:
1 nc: socket.sock: Connection refused
Only if we run them in correct order can you see the message "hello"
49: The nc command is the netcat com-
being printed in terminal 1. 49 mand. It is a powerful tool for network
debugging and exploration. It can be
You can press Ctrl+C to stop the nc command in both terminals.
used to create sockets, listen on ports,
and send and receive data over the net-
work. This will be discussed in more
1.5.8 Types of Regular Files detail in the networking section and in
the Modern Application Development
course.
Regular files can be further classified into different types based on the
data they contain. In Linux systems, the type of a file is determined by
its MIME type. The extension of a file does not determine its type, the
The bytes at the start of a file used to
contents of the file do. It is thus common to have files without extensions identify the type of file are called the
in Linux systems, as they provide no value. magic bytes.
More details can be found at:
The file command can be used to determine the type of a file. https://en.wikipedia.org/wiki/
List_of_file_signatures
46 1 Essentials of Linux
1 $ file /etc/passwd
2 /etc/passwd: ASCII text
3 $ file /bin/bash
4 /bin/bash: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV)
, dynamically linked, interpreter /lib64/ld-linux-x86-64.so
.2, BuildID[sha1]=165d3a5ffe12a4f1a9b71c84f48d94d5e714d3db,
for GNU/Linux 4.4.0, stripped
Question 1.5.1 What types of files are possible in a linux file system?
Question 1.5.2 How to know what kind of file a file is? Can we
determine using its extension? Can we determine using its contents?
What does MIME mean? How to get that?
Answer 1.5.2 The file command can be used to determine the type
of a file.
The extension of a file does not determine its type.
The contents of a file can be used to determine its type.
MIME stands for Multipurpose Internet Mail Extensions.
It is a standard that indicates the nature and format of a document.
file -i filename will give the MIME type of filename.
1.6 Inodes and Links 47
1.6.1 Inodes
Whenever you run ls -l and see all the details of a file, you are seeing
the metadata of the file. These metadata, however, are not stored in the
file itself. These data about the files are stored in a special data structure
called an inode.
You can check how many inodes are used in a filesystem using the df
-i command.
1 $ df -i
2 Filesystem Inodes IUsed IFree IUse% Mounted on
3 /dev/sda1 6397952 909213 5488739 15% /
4 /dev/sda4 0 0 0 - /efi
5 /dev/sda2 21569536 2129841 19439695 10% /home
6 /dev/sdb3 32776192 2380 32773812 1% /data
7 $ df
8 Filesystem 1K-blocks Used Available Use% Mounted on
9 /dev/sda1 100063312 63760072 31174076 68% /
10 /dev/sda4 1021952 235760 786192 24% /efi
11 /dev/sda2 338553420 273477568 47805068 86% /home
12 /dev/sdb3 514944248 444194244 44518760 91% /data
You can notice the number of inodes present, number of inodes used, and
number of inodes that are free. The IUse% column shows the percentage
of inodes used. This however, does not mean how much of space is used,
but how many files can be created.
Observe that although the /data partition has only 1% of inodes used, it
has 91% of space used. This is because the files in the /data partition are
large files, and thus the number of inodes used is less. Remember that
a file will take up one inode, no matter how large it is. But the space it
takes up will be the size of the file.
We can also see the inode number of a file using the ls -i command.
1 $ ls -i
2 1234567 file1
3 1234568 file2
4 1234569 file3
Remark 1.6.1 The inode number is unique only within the filesystem.
If you copy a file from one filesystem to another, the inode number
will change.
48 1 Essentials of Linux
In UNIX systems, the data of a file and the metadata of a file are stored
separately. The inodes are stored in a inode-array or table, and contain
the metadata of the file and the pointer to it in the storage block. These
50: A system call is a request in a op- metadata can be retrieved using the stat system call. 50
erating system made via a software in-
terrupt by an active process for a service
Conveniently, a userland utility to list the metadata of a file is also called
performed by the kernel. The diagram in
Figure 1.10 shows how system calls work. stat.
1 $ stat /etc/profile
2 File: /etc/profile
3 Size: 993 Blocks: 8 IO Block: 4096 regular
file
4 Device: 8,1 Inode: 2622512 Links: 1
5 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/
root)
6 Access: 2024-05-21 18:30:27.000000000 +0530
7 Modify: 2024-04-07 23:32:30.000000000 +0530
Figure 1.10: System Calls 8 Change: 2024-05-21 18:30:27.047718323 +0530
9 Birth: 2024-05-21 18:30:27.047718323 +0530
We can also specify the format of the output of the stat command using
the -format or -c flag to print only the metadata we want.
The data of the file is stored in the storage block. The inode number
indexes a table of inodes on the file system. From the inode number, the
kernel’s file system driver can access the inode contents, including the
location of the file, thereby allowing access to the file.
On many older file systems, inodes are stored in one or more fixed-size
areas that are set up at file system creation time, so the maximum number
of inodes is fixed at file system creation, limiting the maximum number
of files the file system can hold.
Some Unix-style file systems such as JFS, XFS, ZFS, OpenZFS, ReiserFS,
btrfs, and APFS omit a fixed-size inode table, but must store equivalent
data in order to provide equivalent capabilities. Common alternatives to
the fixed-size table include B-trees and the derived B+ trees.
Remark 1.6.2 Although the inodes store the metadata of the file, the
filename is not stored in the inode. It is stored in the directory entry.
Thus the filename, file metdata, and file data are stored separately.
If multiple entries of the directory entry points to the same inode, they
are called hard links. Hard links can have different names, but they are
the same file. As they point to the same inode, they also have the same
metadata.
This is useful if you want to have the same file in multiple directories
without taking up more space. It is also useful if you want to keep a
backup of a important file which is accessed by many people. If someone
accidentally deletes the file, the other hard links will still be there and
able to access the file.
Definition 1.6.2 (Hard Links) Hard Links are just pointers to the
same inode. They are the same file. They are not pointers to the path
of the file. They are pointers to the file itself. They are not affected
by the deletion of the other file. When creating a hard link, you need
to provide the path of the original file, and thus it has to be either
50 1 Essentials of Linux
Hard links can be created for files only, and not directories. It can be
created using the ln command.
1 $ ln file1 file2
This will create a hard link named file2 that points to the same inode
as file1.
Remark 1.6.3 Hard links are not dependent on the original file. They
are the same file and equivalent. The first link to be created has no
special status.
Historically directories could also have hard links, but this would cause
the file tree to stop being a Directed Acyclic Graph 51 and become
a Directed Cyclic Graph if a hardlink of an ancestor was put as a
subdirectory. This would create confusions and infinite walks in the file
system. Modern systems generally prohibit this confusing state, except
that the parent of root is still defined as root. 52
As hard links depend on the inode, they can only exist in the same
filesystem as inodes are unique to a filesystem only.
Definition 1.6.3 (Soft Links) Soft Links are special kinds of files that
just store the path given to them. Thus the path given while making
soft links should either be an absolute path, or relative from the
location of the soft link to the location of the original file. It should
not be relative from current working directory.a
a This is a common mistake.
Symlinks are created using the symlink system call. This can be done
using the ln -s command.
1 $ echo "hello" > file1
2 $ ln -s file1 file2
3 $ ls -l
1.6 Inodes and Links 51
4 total 4
5 -rw-r--r-- 1 sayan sayan 6 May 23 15:27 file1
6 lrwxrwxrwx 1 sayan sayan 5 May 23 15:27 file2 -> file1
7 $ cat file2
8 hello
Interesting Observation:
Usually we have seen that if we use ls -l with a directory as its argument,
it lists the contents of the directory.
The only way to list the directory itself is to use ls -ld.
But if a symlink is made to a directory, then ls -l on that symlink will
list only the symlink.
To list the contents of the symlinked directory we have to append a / to
the symlink.
1 $ ln -s /etc /tmp/etc
2 $ ls -l /tmp/etc
3 lrwxrwxrwx 1 sayan sayan 4 May 23 15:30 /tmp/etc -> /etc
4 $ ls -l /tmp/etc/ | head -n5
5 total 1956
6 -rw-r--r-- 1 root root 44 Mar 18 21:50 adjtime
7 drwxr-xr-x 3 root root 4096 Nov 17 2023 alsa
8 -rw-r--r-- 1 root root 541 Apr 8 20:53 anacrontab
9 drwxr-xr-x 4 root root 4096 May 19 00:44 apparmor.d
Here I used head to limit the number of lines shown as the directory is
large. 53 53: This way of combining commands
will be discussed later.
The symlink file stores only the path provided to it while creating it. This
was historically stored in the data block which was pointed to by the
inode. But this made it slower to access the symlink.
Modern systems store the symlink value in the inode itself if its not too
large. Inodes usually have a limited space allocated for each of them, so
a symlink with a small target path is stored directly in the inode. This is
called a fast symlink.
However if the target path is too large, it is stored in the data block
pointed to by the inode. This is retroactively called a slow symlink.
This act of storing the target path in the inode is called inlining.
Symlinks do not have a permission set, thus they always report lrwxrwxrwx
as their permissions.
The size reported of a symlink file is independent of the actual file’s
size.
1 $ echo "hello" > file1
2 $ ln -s file1 file2
3 $ ls -l
4 -rw-r--r-- 1 sayan sayan 6 May 23 15:27 file1
5 lrwxrwxrwx 1 sayan sayan 5 May 23 15:27 file2 -> file1
6 $ echo "a very big file" > file2
7 $ ls -l
8 -rw-r--r-- 1 sayan sayan 16 May 23 15:40 file1
9 lrwxrwxrwx 1 sayan sayan 5 May 23 15:27 file2 -> file1
1 $ ln -s /a/very/long/and/non-existant/path link1
2 $ ln -s small link2
3 $ ls -l
4 total 0
5 lrwxrwxrwx 1 sayan sayan 34 May 23 15:41 link1 -> /a/very/long/and
/non-existant/path
6 lrwxrwxrwx 1 sayan sayan 5 May 23 15:41 link2 -> small
Notice that the size of link1 is 34, the length of the target path, and the
size of link2 is 5, the length of the target path.
Soft Links:
To identify if a file is a symlink or a hard link, you can use the ls -l
command. If the file is a symlink, the first character of the permissions
will be l. ls -l will also show the target of the symlink after a -> symbol.
However, you cannot acertain if a file has a sofy link pointing to it
somewhere else or not.
Hard Links:
To identify if a file is a hard link, you can use the ls -i command. Hard
links will have the same inode number as each other. The inode number
is the first column of the output of ls -i.
1.6 Inodes and Links 53
Also the number of links to the file will be more than 1. The number of
links is the second 54 column of the output of ls -l. 54: third if using ls -li
Even if a hard link is not present in current directory, you can acertain
that a file has a hard link pointing to it somewhere else using the number
of hardlinks column of ls -l.
1 $ touch file1
2 $ ln -s file1 file2
3 $ ln file1 file3
4 $ ls -li
5 total 0
6 4850335 -rw-r--r-- 2 sayan sayan 0 May 23 15:56 file1
7 4851092 lrwxrwxrwx 1 sayan sayan 5 May 23 15:56 file2 -> file1
8 4850335 -rw-r--r-- 2 sayan sayan 0 May 23 15:56 file3
. and .. are special directory entries. They are hard links to the current
directory and the parent directory respectively. Each directory has a .
entry pointing to itself and a .. entry pointing to its parent directory.
Due to this, the number of hard links to a directory is exactly equal to
the number of subdirectories it has plus 2.
This is because each subdirectory has a .. entry pointing to the parent
directory, and the parent directory has a . entry pointing to itself.
So the directory’s name in its parent directory is 1 link, the . in the
directory is 1 link, and all the subdirectories have a .. entry pointing to
the directory, which is 1 link each.
This formula always stands because a user cannot create additional hard
links to a directory.
Answer 1.6.1 ls -i will list the inodes of a file. The inodes are the
first column of the output of ls -i This can be combined with other
flags like -l or -a to show more details.
Answer 1.6.4 realpath filename will give the real path of filename.
You can also use readlink -f filename to get the real path.
Command Line Editors 2
2.1 Introduction 2.1 Introduction . . . . . . . . 55
2.1.1 Types of Editors . . . . . . 55
2.1.2 Why Command Line
Now that we know how to go about navigating the linux based operating Editors? . . . . . . . . . . . 55
systems, we might want to view and edit files. This is where command 2.1.3 Mouse Support . . . . . . 55
line editors come in. 2.1.4 Editor war . . . . . . . . . 56
2.1.5 Differences between Vim
and Emacs . . . . . . . . . 56
Definition 2.1.1 A command line editor is a type of text editor that
2.1.6 Nano: The peacemaker
operates entirely from the command line interface. They usually do
amidst the editor war . . 58
not require a graphical user interface or a mouse. a
2.2 Vim . . . . . . . . . . . . . 58
a This means that CLI editors are the only way to edit files when you are connected to 2.2.1 History . . . . . . . . . . . 58
a remote server. Since remote servers do not have any graphical server like X11, you 2.2.2 Ed Commands . . . . . . . 65
cannot use graphical editors like gedit or kate.
2.2.3 Exploring Vim . . . . . . . 72
2.3 Emacs . . . . . . . . . . . . 80
2.3.1 History . . . . . . . . . . . 80
2.1.1 Types of Editors 2.3.2 Exploring Emacs . . . . . 82
2.4 Nano . . . . . . . . . . . . . 84
▶ Graphical Editors: These are editors that require a graphical user 2.4.1 History . . . . . . . . . . . 84
2.4.2 Exploring Nano . . . . . . 85
interface. Examples include gedit ‗ , kate † , vs code, etc.
2.4.3 Editing A Script in Nano 85
▶ Command Line Editors: These are editors that operate entirely
from the command line interface.
Command line editors are very powerful and efficient. They let you edit
files without having to leave the terminal. This is usually faster than
opening a graphical editor. In many cases like sshing 1 into a remote 1: SSH stands for Secure Shell. It is a
server, command line editors are the only way to edit files. Another cryptographic network protocol for op-
erating network services securely over
reason for the popularity of command line editors is that they are very an unsecured network. This will be dis-
lightweight. cussed in detail in a later chapter.
Most command line editors do not have mouse support, and others do
not encourage it. But wont it be difficult to navigate without a mouse?
Not really. Once you get used to the keyboard shortcuts, you will find
that you can navigate way faster than with a mouse.
Mouse editors usually require the user to click on certain buttons, or
to follow multi-click procedures in nested menus to perform certain
tasks.
Whereas in keyboard based editors, all the actions that can be performed
are mapped to some keyboard shortcuts.
Modern CLI editors usually also allow the user to totally customize the
keyboard shortcuts.
This being said, most modern CLI editors do have mouse support as well
if the user is running them in a terminal emulator that supports mouse
2: X11 and Wayland are display servers
over a X11 or Wayland display server. 2
that are used to render graphical appli-
cations. Although not directly covered,
these can be explored in details on the
internet. 2.1.4 Editor war
Although there are many command line editors available, the most
popular ones are vim and emacs.
Definition 2.1.2 The editor war is the rivalry between users of the
Emacs and vi (now usually Vim, or more recently Neovim) text
editors. The rivalry has become an enduring part of hacker culture
3: More on this including the history and the free software community. 3
and the humor can be found on the in-
ternet.
Vim
Vim is a modal editor, meaning that it has different modes for different
tasks. Most editors are modeless, this makes vim a bit difficult to learn.
However, once familiar with it, it is very powerful and efficient. Vim
heavily relies on alphanumeric keys for navigation and editing. Vim
4: qutebrowser is a browser that uses vim- keybindings are so popular that many other editors and even some
like keybindings. Firefox and Chromium browsers 4 have vim-like keybindings.
based browsers also have extensions that
provide vim-like keybindings. These al- Emacs
low the user to navigate the browser
using vim-like keybindings and without
Emacs is a modeless editor, meaning that it does not have different modes
ever touching the mouse.
for different tasks. Emacs is also very powerful and efficient. It uses
multi-key combinations for navigation and editing.
Keystroke execution
Emacs commands are key combinations for which modifier keys are
held down while other keys are pressed; a command gets executed once
completely typed.
Vim retains each permutation of typed keys (e.g. order matters). This
creates a path in the decision tree which unambiguously identifies any
command.
Vi is a smaller and faster program, but with less capacity for customization.
vim has evolved from vi to provide significantly more functionality and
customization than vi, making it comparable to Emacs.
User environment
Emacs, while also initially designed for use on a console, had X11 GUI
support added in Emacs 18, and made the default in version 19. Current
Emacs GUIs include full support for proportional spacing and font-size
variation. Emacs also supports embedded images and hypertext.
Function/navigation interface
Emacs uses metakey chords. Keys or key chords can be defined as prefix
keys, which put Emacs into a mode where it waits for additional key
presses that constitute a key binding. Key bindings can be mode-specific,
further customizing the interaction style. Emacs provides a command
line accessed by M-x that can be configured to autocomplete in various
ways. Emacs also provides a defalias macro, allowing alternate names
for commands.
Vi uses distinct editing modes. Under "insert mode", keys insert characters
into the document. Under "normal mode" (also known as "command
mode", not to be confused with "command-line mode", which allows the
user to enter commands), bare keypresses execute vi commands.
Keyboard
Vi does not use the Alt key and seldom uses the Ctrl key. vi’s keyset is
mainly restricted to the alphanumeric keys, and the escape key. This is
an enduring relic of its teletype heritage, but has the effect of making
most of vi’s functionality accessible without frequent awkward finger
reaches.
Emacs has full support for all Unicode-compatible writing systems and
allows multiple scripts to be freely intermixed.
Nano is a simple command line editor that is easy to use. It does not
have the steep learning curve of vim or emacs. But it is not as powerful
as vim or emacs as well. It is a common choice for beginners who just
want to append a few lines to a file or make a few changes. It is also a
non-modal editor like editor which uses modifier chording like emacs.
However, it mostly uses the control key for this purpose and has only
simple keybindings such as Ctrl+O to save and Ctrl+X to exit.
2.2 Vim
2.2.1 History
Teletypes
2.2 Vim 59
Very early computers used to use teletypes as the output device. These
were devices that used ink and paper to actually print the output of
the computer. These did not have an automatic refresh rate like modern
monitors. Only when the computer sent a signal to the teletype, would
Figure 2.1: A Teletype
the teletype print the output.
QED
QED was a text editor developed by Butler Lampson and Peter Deutsch
in 1967 for the Berkeley Timesharing System. It was a character-oriented
editor that was used to create and edit text files. It used to print or edit
only one character at a time on the screen. This is because the computers
at that time used to use a teletype machine as the output device, and not
a monitor.
Ken Thompson used this QED at Berkeley before he came to Bell Labs,
and among the first things he did on arriving was to write a new version
for the MIT CTSS system. Written in IBM 7090 assembly language, it
differed from the Berkeley version most notably in introducing regular Figure 2.2: Ken Thompson
expressions 5 for specifying strings to seek within the document being
edited, and to specify a substring for which a substitution should be 5: Regular expressions are a sequence
made. Until that time, text editors could search for a literal string, and of characters that define a search pattern.
substitute for one, but not specify more general strings. Usually this pattern is used by string
searching algorithms for "find" or "find
Ken not only introduced a new idea, he found an inventive implemen- and replace" operations on strings. This
will be discussed in detail in a later chap-
tation: on-the-fly compiling. Ken’s QED compiled machine code for ter.
each regular expression that created a NDFA (non-deterministic finite
automaton) to do the search. He published this in C. ACM 11 #6, and also
received a patent for the technique: US Patent #3568156.
While the Berkeley QED was character-oriented, the CTSS version was
line-oriented. Ken’s CTSS qed adopted from the Berkeley one the notion
of multiple buffers to edit several files simultaneously and to move and
copy text among them, and also the idea of executing a given buffer as
editor commands, thus providing programmability.
When developing the MULTICS project, Ken Thompson wrote yet another
version of QED for that system, now in BCPL 6 and now created trees for
regular expressions instead of compiling to machine language.
In 1967 when Dennis Ritchie joined the project, Bell Labs had slowly
started to move away from Multics. While he was developing the initial
stages of Unix, he rewrote QED yet again, this time for the GE-TSS system
in Assembly language. This was well documented, and was originally Figure 2.3: Dennis Ritchie
intented to be published as a paper. 7
6: BCPL ("Basic Combined Program-
ED ming Language") is a procedural, im-
perative, and structured programming
After their experience with multiple implementations of QED, Ken language. Originally intended for writ-
Thompson wrote ed in 1969. ing compilers for other languages, BCPL
is no longer in common use.
60 2 Command Line Editors
7: At that time, systems did not have a This was now written in the newly developed B language, a predecessor
standardized CPU architecture or a gen- to C. This implementation was much simpler than QED, and was line
eralized low level compiler. Due to this,
applications were not portable across
oriented. It stripped out much of regular expression support, and only
systems. Each machine needed its own had the support for *. It also got rid of multiple buffers and executing
version of the application to be written contents of buffer.
from the scratch, mostly in assembly lan-
guage. Slowly, with time, Dennis Ritchie created the C language, which is widely
The reference manual for GE-TSS QED in use even today.
can still be found on Dennis Ritchie’s
website Much of this information is taken Ken Thompson re-wrote ed in C, and added back some of the complex
from his blog. features of QED, like back references in regular expressions.
Remark 2.2.1 Since all of Bell-Labs and AT&T’s software was propri-
etary, the source code for ed was not available to the public. Thus, the
ed editor accessible today in GNU/Linux, is another implementation
of the original ed editor by the GNU project.
However, ed was not very user friendly and it was very terse. Although
this was originally intented, since it would be very slow to print a lot
of diagnostic messages on a teletype, slowly, as people moved to faster
computers and monitors, they wanted a more user friendly editor.
VDU Terminals
These terminals were able to show video output, instead of just printing
the output on paper. Although initially these were very expensive, and
were not a household item, they were present in the research parks like
Figure 2.4: Xerox Alto, one of the first Xerox PARC.
VDU terminals with a GUI, released in
1973 EM
8: George named em as Editor for Mor- George Coulouris (not the actor) was one of the people who had access
tals because Ken Thompson visited his to these terminals in his work at the Queen Mary College in London.
lab at QMC while he was developing it
and said something like: "yeah, I’ve seen The drawbacks of ed were very apparent to him when using on these
editors like that, but I don’t feel a need machines.
for them, I don’t want to see the state
of the file when I’m editing". This made He found that the UNIX’s raw mode, which was at that time totally
George think that Ken was not a mortal,
unused, could be used to give some of the convenience and immediacy
and thus he named it Editor for Mortals.
of feedback for text editing.
Although em added a lot of features to ed, it was still a line editor, that is,
you could only see one line at a time. The difference from ed was that it
Figure 2.5: A first generation Dectape allowed visual editing, meaning you can see the state of the line as you
(bottom right corner, white round tape) are editing it.
being used with a PDP-11 computer
2.2 Vim 61
Whereas most of the development of Multics and Unix was done in the
United States, the development of em was done in the United Kingdom,
in the Queen Mary College, which was the first college in the UK to have
UNIX.
EN
There he met Bill Joy, who was a PhD student at Berkeley. On showing
him the editor, Bill Joy was very impressed and wanted to use it on the
PDP-11 computers at Berkeley. The system support team at Berkley were
using PDP-11 which used VDU Terminals, an environment where em
would really shine.
He explained that ’em’ was an extension of ’ed’ that gave key-stroke level
interaction for editing within a single line, displaying the up-to-date line
on the screen (a sort of single-line screen editor). This was achieved by
setting the terminal mode to ’raw’ so that single characters could be read
as they were typed - an eccentric thing for a program to do in 1976.
George then took a vacation for a few weeks, but when he returned, he
found that Bill had taken his ex as a starting point and had added a lot of
features to it. He called it en initially, which later became ex.
EX
Bill Joy took inspiration from several other ed clones as well, and their
own tweaks to ed, although the primary inspiration was em. Bill and
Chuck Haley built upon em to make en, which later became ex.
This editor had a lot of improvements over em, such as adding the ability
to add abbreviations (using the ab command), and adding keybindings
(maps).
It also added the ability to mark some line using the k key followed by
any letter, and then jump to that line from any arbitrary line using the ’
key followed by the letter.
Slowly, with time, the modern systems were able able to handle the
raw mode, and real time editing more and more. This led to the natural
progression, What if we could see the entire file at once, and not just one
line at a time? Figure 2.7: Bill Joy
VI
10: This visual mode is not the same as
Bill added the visual mode to ex in 1977. 10 This was not a separate editor, the visual mode in vim.
but rather just another mode of the ex editor. You could open ex in visual
mode using the -v flag to ex.
62 2 Command Line Editors
1 $ ex -v filename
This visual mode was the first time a text editor was modal. This means
that the editor had different modes for different tasks. When you want
to edit text, you would go to the insert mode, and type the text. When
you want to navigate, you would go to the normal mode, and use the
navigation keys and other motions defined in vi.
Bill Joy was also one of the people working on the Berkeley Software
Distribution (BSD) of Unix. Thus he bundled vi with the first BSD
distribution of UNIX released in 1978. The pre-installed nature of vi in
the BSD Distribution made it very popular.
However, since both the source code of ed was restricted by Bell Labs
- AT&T, and the source code of vi was restricted by the University of
California, Berkeley, they could not be modified by the users or distributed
freely.
Vi Clones
The vi clones were written because the source code for the original
version was not freely available until recently. This made it impossible
to extend the functionality of vi. It also precluded porting vi to other
operating systems, including Linux.
The clones add numerous new features which make them significantly
easier to use than the original vi, especially for neophytes. A particularly
useful feature in many of them is the ability to edit files in multiple
windows. This facilitates working on more than one file at the same time,
including cutting and pasting text among them.
Many of the clones also offer GUI versions of vi that operate under the X
Windows system and can take advantage of bit-mapped (high resolution)
displays and the mouse.
Vim
Bram Moolenaar, a Dutch programmer, was impressed by STeVIe, a vi
clone for the Atari ST. But he was working with the Commodore Amiga
at that time, and there was no vi clone for the Amiga. So Bram began
working on the stevie clone for the AmigaOS in 1988.
He released the first public release (v 1.14) in 1991 as visible in Figure
2.11.
Since Vim was based off of Stevie, and not ed or vi so it could be freely
distributed. It was licensed under a charityware license, named as Vim
License. The license stated that if you liked the software, you should
consider making a donation to a charity of your choice.
Moolenaar was an advocate of a NGO based in Kibaale, Uganda, which
he founded to support children whose parents have died of AIDS. In
1994, he volunteered as a water and sanitation engineer for the Kibaale Figure 2.10: Bram Moolenaar
Children’s Centre and made several return trips over the following
twenty-five years.
Later Vim was re-branded as ‘Vi IMproved‘ as seen in Figure 2.12.
64 2 Command Line Editors
Vim has been in development for over 30 years now, and is still actively
maintained. It has added a lot of features over the years, such as syntax
highlighting, plugins, completion, PCRE support, mouse support, etc.
neovim
Recently there have been efforts to modernize the vim codebase. Since
it is more than 30 years old, it has a lot of legacy code. The scripting
Figure 2.12: Vim 9.0 Start screen language of vim is also not a standard programming language, but rather
a custom language called vimscript.
To counter this, a new project called neovim has been started. It uses lua
as the scripting language, and has a lot of modern features like out of the
14: LSP stands for Language Server Pro- box support for LSP, 14 better mouse integration, etc.
tocol. It is a protocol that allows the editor
to communicate with a language server In this course, we will be learning only about basic vi commands and we
to provide features like autocompletion, will be using vim as the editor.
go to definition, etc. This makes vim
more like an IDE.
2.2.2 Ed Commands
Lets start with creating a file, which we will then open in the ed editor.
1 $ echo "line-1 hello world
2 line-2 welcome to line editor
3 line-3 ed is perhaps the oldest editor out there
4 line-4 end of file" > test.txt
66 2 Command Line Editors
This creates a file in the current working directory with the name
test.txt and the contents as given above.
As soon as we run it, you will see a number, which is the number of
characters in the file. The terminal may seem hung, since there is no
prompt, of either the bash shell, or of the ed editor. This is because ed is
a line editor, and it does not print the contents of the file on the screen.
Off the bat, we can observe the terseness of the ed editor since it does
not even print a prompt. To turn it on, we can use the P command. The
default prompt is *.
1 ed test.txt
2 117
3 P
4 *
Now we can see the prompt * is always present, whenever the ed editor
expects a command from the user.
Lets go to the first line of the file using the 1 command. We can also go
to the last line of the file using the $ command.
1 *1
2 line-1 hello world
3 *$
4 line-4 end of file
5 *
To print out all the lines of the file, we can use the , or % with p command.
1 *,p
2 line-1 hello world
3 line-2 welcome to line editor
4 line-3 ed is perhaps the oldest editor out there
5 line-4 end of file
1 *%p
2 line-1 hello world
3 line-2 welcome to line editor
4 line-3 ed is perhaps the oldest editor out there
5 line-4 end of file
We can also print any arbitrary line range using the line numbers sepa-
rated by a comma and followed by the p command.
1 *2,3p
2 line-2 welcome to line editor
3 line-3 ed is perhaps the oldest editor out there
2.2 Vim 67
One of the pioneering features of ed was the ability to search for a pattern
in the file. Let us quickly explain the syntax of the search command. 15 15: The details of regular expressions
will be covered in a later chapter.
1 */hello/
2 line-1 hello world
We may or may not include the p command after the last / in the search
command.
We can advance to the next line using the + command.
1 *p
2 line-1 hello world
3 *+
4 line-2 welcome to line editor
We can also print all the lines from the current line to the end of the file
using the ;p command.
1 *.
2 line-2 welcome to line editor
3 *;p
4 line-2 welcome to line editor
5 line-3 ed is perhaps the oldest editor out there
6 line-4 end of file
The output after running the r !date command is the number of char-
acters read into the buffer. We can then print the entire buffer using the
%p command.
To delete a line, we can use the d command. Lets say we do not want the
date output in the file. We can re-open the file in ed and remove the last
line.
1 $ ed test.txt
2 149
3 P
4 *$
5 Mon Jun 10 11:38:49 PM IST 2024
6 *d
7 *%p
8 line-1 hello world
9 line-2 welcome to line editor
10 line-3 ed is perhaps the oldest editor out there
11 line-4 end of file
12 *wq
13 117
We can add lines to the file using the a command. This appends the line
after the current line. On entering this mode, the editor will keep on
taking input for as many lines as we want to add. To end the input, we
can use the . command on a new line.
1 $ ed test.txt
2 117
3 P
4 *3
5 line-3 ed is perhaps the oldest editor out there
6 *a
7 perhaps not, since we know it was inspired from QED
8 which was made multiple times by thompson and ritchie
9 before ed was made.
10 .
11 *%p
12 line-1 hello world
13 line-2 welcome to line editor
14 line-3 ed is perhaps the oldest editor out there
15 perhaps not, since we know it was inspired from QED
16 which was made multiple times by thompson and ritchie
17 before ed was made.
18 line-4 end of file
19 *
5 *%p
6 line-1 hello universe
7 line-2 welcome to line editor
8 line-3 ed is perhaps the oldest editor out there
9 perhaps not, since we know it was inspired from QED
10 which was made multiple times by thompson and ritchie
11 before ed was made.
12 line-4 end of file
13 *
We can print the name of the currently opened file using the f com-
mand.
1 *f
2 test.txt
If we wish to join two lines, we can use the j command. Let us join lines
4,5, and 6.
1 *4
2 perhaps not, since we know it was inspired from QED
3 *5
4 which was made multiple times by thompson and ritchie
5 *6
6 before ed was made.
7 *4,5j
8 *4
9 perhaps not, since we know it was inspired from QEDwhich was made
multiple times by thompson and ritchie
10 *5
11 before ed was made.
12 *4,5j
13 *4
14 perhaps not, since we know it was inspired from QEDwhich was made
multiple times by thompson and ritchiebefore ed was made.
15 *
Here we can see that we do the joining in two steps, first the lines 4 and
5 are joined, and then the newly modified line 4 and 5 are joined.
We can move a line from its current position to another line using the m
command.
Lets insert a line-0 at the end of the file and then move it to the beginning
of the file.
1 *7
2 line-4 end of file
3 *a
4 line-0 in the beginning, there was light
5 .
6 *8
7 line-0 in the beginning, there was light
8 *m0
9 *1,4p
10 line-0 in the beginning, there was light
11 line-1 hello universe
12 line-2 welcome to line editor
13 line-3 ed is perhaps the oldest editor out there
14 *
70 2 Command Line Editors
17: The undo command in ed is not as We can also undo the last change using the u command. 17
powerful as the undo command in vim.
In vim, we can undo multiple changes 1 *1
using the u command. In ed, we can 2 line-0 in the beginning, there was light
only undo the last change. If we run 3 *s/light/darkness
the u command multiple times, it will 4 line-0 in the beginning, there was darkness
undo the last change of undoing the last
change, basically redoing the last change.
5 *u
6 *.
7 line-0 in the beginning, there was light
8 *
If search and replace is not exactly what we want, and we want to totally
change the line, we can use the c command. It will let us type a new line,
which will replace the current line.
1 *%p
2 line-0 in the beginning, there was light
3 line-1 hello universe
4 line-2 welcome to line editor
5 line-3 ed is perhaps the oldest editor out there
6 perhaps not, since we know it was inspired from QEDwhich was made
multiple times by thompson and ritchiebefore ed was made.
7 line-4 end of file
8 *4
9 line-3 ed is perhaps the oldest editor out there
10 *c
11 line-4 ed is the standard editor for UNIX
12 .
13 *4
14 line-4 ed is the standard editor for UNIX
15 *
Just like the a command, we can also use the i command to insert a line
at the current position. This will move the current line to the next line.
1 *6
2 line-4 end of file
3 *i
4 before end of file
5 .
6 *6,$p
7 before end of file
8 line-4 end of file
9 *
This opens a temporary files that goes through a lot of sections of vim,
explaining the commands in detail. This opens the text file in vim itself,
so you can actually try out each exercise as and when you read it.
Many exercises are present in this file to help you remember and master
commands. Feel free to modify the file since it is a temporary file and
any changes made is lost if the command is re-run.
Pressing the Esc key takes you to the normal mode from any other
mode.
Since we are already familar with commands in ed, most of the commands
are same/similar in ex mode of vim.
Table 2.5: Ex Commands in Vim
Key Description
:f show name of file
:p print current line
:a append at current line
:c change current line
:d delete current line
:i insert line at current position
:j join lines
:s search and replace regex pattern in current line
:m move current line to position
:u undo latest change
:w [filename] write buffer to filename
:q quit if no change
:wq write buffer to filename and quit
:x write buffer to filename and quit
:q! quit without saving
:r filename read file contents into buffer
:r !command read command output into buffer
:e filename edit a file
:sp [filename] split the screen and open another file
:vsp [filename] vertical split the screen and open another file
There are many more commands in the ex mode of vim. Along with
implementing the original ed commands, it also has a lot of additional
74 2 Command Line Editors
commands to make it more integrated with the vim editor, such as the
ability to open split windows, new tabs, buffers, and perform normal
mode commands in ex mode.
Basic Navigation
The basic keys for moving around in a text file in vim are the h,j,k,l
keys. They move the cursor one character to the left, down, up, and right
respectively. These keys are chosen because they are present on the home
row of the keyboard, and do not require the user to move their hands
19: These were historically chosen be- from the home row to navigate. 19
cause the ADM-3A terminal had these
keys for navigation as seen in Figure 2.8. Along with these, we have keys to navigate word by word, or to move to
the next pattern match, or the next paragraph, spelling error, etc.
Replacing Text
Usually in other text editors, if you have a word, phrase, or line which
you want to replace with another, you would either press the backspace
or delete key to remove the text, and then type the new text. However, in
vim, there is a more efficient way to replace text.
Toggling Case
You can toggle the case of a character, word, line, or any arbitrary chunk
of the file using the or the g command.
You might start to see a pattern emerging here. Many commands in vim
do a particular command, and on which text it operates is determined by
the character followed by it. Such as c to change, d to delete, y to yank,
etc. The text on which the command operates is mentioned using w for
the word, 0 for till beginning of line, etc.
This is not a coincidence, but rather a design of vim to make it more
efficient to use. The first command is called the operator command, and
the second command is called the motion command.
Vim follows a operator-count-motion pattern. For example: d2w deletes
the next 2 words. This makes it very easy to learn and remember
commands, since you are literally typing out what you want to do.
Deleting or Cutting Text
In Vim, the delete command is used to cut text from the file.
Motion - till, in, around
By now you should notice that dw doesn’t always delete the word under
the cursor. Technically dw means delete till the beginning of the next
word. So if you press dw at the beginning of a word, it will delete the
word under the cursor. But if your cursor is in the middle of a word and
you type dw, it will only delete the part of the word till the beginning of
the next word from the cursor position.
76 2 Command Line Editors
To delete the entire word under the cursor, regardless of where the cursor
is in the word, you can use the diw this means delete inside word.
However, now you may notice that diw doesn’t delete the space after
the word. This results in two consequtive spaces, one from the end of
the word, and one from the beginning of the word being left behind. To
delete the space as well, you can use daw which means delete around
word.
This works not just with w but with any other motion such as delete
inside paragraph, which will delete the entire paragraph under the
cursor, resulting in two empty lines being left behind, and delete around
paragraph, which will delete the entire paragraph under the cursor, and
only one empty line being left behind.
Try out the same with deleting inside other items, such as brackets,
parenthesis, braces, quotes, etc. The syntax remains the same, di{, di[,
di(, di", di’, etc.
and
1 0y$
are not the same. The first command yanks the entire line, including
the newline character at the end of the line. The second one yanks
the entire line, but does not include the newline character at the end
of the line. Thus if you directly press p after the first command, it
will paste the line below the current line, and if you press p after the
2.2 Vim 77
second command, it will paste the line at the end of the current line.
The flags at the end of the search and replace command can be g to
replace all occurrences in the line, and c to confirm each replacement.
The address can be a single line number, a range of line numbers, or a
pattern to search for. The pattern can be a simple string, or a regular
expression.
Some examples of addresses are shown in Table 2.12.
Insert Mode
You can enter insert mode from escape mode using the keys listed in Table
2.13. In insert mode, if you want to insert any non-graphical character,
you can do that by pressing Ctrl+V followed by the key combination for
78 2 Command Line Editors
the character. For example, to insert a newline character, you can press
Ctrl+V followed by Enter.
These are just the basic commands to get you started with vim. You can
refer to vim cheat sheets present online to get more familiar with the
commands.
2.3 Emacs
2.3.1 History
TECO
TECO was developed at MIT in 1962. It was a text editor used to correct
the output of the PDP-1 computer. It is short for Tape Editor and Corrector.
Unlike most modern text editors, TECO used separate modes in which
the user would either add text, edit existing text, or display the document
One could not place characters directly into a document by typing them
into TECO, but would instead enter a character (’i’) in the TECO command
language telling it to switch to input mode, enter the required characters,
during which time the edited text was not displayed on the screen, and
finally enter a character (<esc>) to switch the editor back to command
Figure 2.18: Richard Stallman - founder mode. This is very similar to how vi works.
of GNU and FSF projects
Stallman’s Visit to Stanford
Initially TECO was able to only edit the file sequentially, page by page.
This was due to earlier memory restrictions of the PDP-1. Stallman
2.3 Emacs 81
modified TECO to read the entire file into the buffer, and then edit the
buffer in memory allowing for random access to the file.
Too Many Macros!
The new version of TECO quickly became popular at the AI Lab and soon
accumulated a large collection of custom macros whose names often
ended in MAC or MACS, which stood for macro. This quickly got out of
hand as there were many divergent macros, and a user would be totally
lost when using a co-worker’s terminal.
In 1979, Guy Steele combined many of the popular macros into a single
file, which he called EMACS, which stood for Editing MACroS, or E with
MACroS.
To prevent thousands of forks of EMACS, Stallman declared that ‘EMACS
was distributed on a basis of communal sharing, which means all improve-
ments must be given back to me to be incorporated and distributed.’
Till now, the EMACS, like TECO, ran on the PDP-10 which ran the ITS Figure 2.19: Guy L. Steele Jr. combined
many divergent TECO with macros to
operating system and not UNIX. create EMACS
EINE ZWEI SINE and other clones
22: EINE stands for Eine Is Not EMACS
No, that is not German. These are some of the popular clones of EMACS 23: ZWEI stands for ZWEI Was Eine
made for other operating systems. Initially
These kinds of recursive acronyms are
EINE 22 was a text editor developed in the late 1970s. In terms of features, common in the nix world. For example,
its goal was to ‘do what Stallman’s PDP-10 (original) Emacs does’. Unlike GNU stands for GNU’s Not Unix, WINE
the original TECO-based Emacs, but like Multics Emacs, EINE was (A compatibility layer to run Windows
written in Lisp. It used Lisp Machine Lisp. applications) is short for WINE Is Not an
Emulator.
In the 1980s, EINE was developed into ZWEI 23 . Innovations included 24: SINE stands for SINE Is Not EINE
programmability in Lisp Machine Lisp, and a new and more flexible
doubly linked list method of internally representing buffers.
SINE 24 was written by Owen Theodore Anderson in 1981.
In 1978, Bernard Greenberg wrote a version of EMACS for the Multics
operating system called Multics EMACS. This used Multics Lisp.
Gosling Emacs
In 1981, James Gosling wrote Gosling Emacs for UNIX. It was written in
C and used Mocklisp, a language with lisp-like syntax, but not a lisp. It
was not free software.
GNU Emacs Figure 2.20: James Gosling - creator of
Gosling Emacs and later Java
In 1983, Stallman started the GNU project to create a free software
alternatives to proprietary softwares and ultimately to create a free 25
25: Recall from the previous chapter
operating system. that free software does not mean a soft-
ware provided gratis, but a software
In 1984, Stallman started GNU Emacs, a free software alternative to
which respects the user’s freedom to run,
Gosling Emacs. It was written in C and used a true Lisp dialect, Emacs copy, distribute, and modify the software.
Lisp as the extension language. Emacs Lisp was also implemented in C. It is like free speech, not free beer.
This is the version of Emacs that is most popular today and available on
most operating systems repositories.
How the developer’s keyboard influences the editors they make
Remember that ed was made while using ADM-3A which looked like
Figure 2.21.
Figure 2.21: ADM-3A terminal
82 2 Command Line Editors
Whereas emacs was made while the Knight keyboard and the Space
Cadet keyboard were in use, which can be seen in Figure 2.22.
Notice how the ADM-3A has very limited modifier keys, and does not
even have arrow keys. Instead it uses h,j,k,l keys as arrow keys with
a modifier. This is why vi uses mostly key combinations and a modal
interface. Vi also uses the Esc key to switch between modes, which is
Figure 2.22: Space Cadet Keyboard present conveniently in place of the Caps Lock or Tab key in modern
keyboard layouts.
The Space Cadet keyboard has many modifier keys, and even a key for
the Meta key. This is why emacs uses many key modifier combinations,
and has a lot of keybindings.
Most of emacs keybindings use modifier keys such as the Ctrl key, and
the Meta key. The Meta key is usually the Alt key in modern keyboards.
In the reference manual and here, we will be representing the Meta key
as M- and the Ctrl key as C-.
Basic Navigation
These keys are used to move around in the file. Like vim, emacs also
focusses on keeping the hands free from the mouse, and on the keyboard.
All the navigation can be done through the keyboard.
Exiting Emacs
We can exit emacs either with or without saving the file. We can also
suspend emacs and return to the shell. This is a keymapping of the shell,
and not of emacs.
2.3 Emacs 83
Searching Text
Emacs can search for a fixed string, or a regular expression and replace it
with another string.
2.4 Nano
Although vim and emacs are the most popular command line text editors,
nano is also a very useful text editor for beginners. It is very simple and
does not have a steep learning curve.
It is a non-modal text editor, which means that it does not have different
Figure 2.23: Nano Text Editor modes for different actions. You can directly start typing text as soon as
you open nano.
2.4.1 History
26: It is believed that pine stands for
Pine is Not Elm, Elm being another text- Pine 26 was a text-based email client developed at the University of
based email client. However, the author
clarifies that it was not named with that
Washington. It was created in 1989. The email client also had a text editor
in mind. Although if a backronym was built in called Pico.
to be made, he prefered ‘Pine is Nearly
Elm’ or ‘Pine is No-longer Elm’ Although the license of Pine and Pico may seem open source, it was
27: Up to version 3.91, the Pine license not. The license was restrictive and did not allow for modification or
was similar to BSD, and it stated that redistribution. 27
‘Permission to use, copy, modify, and
distribute this software and its documen- Due to this, many people created clones of Pico with free software
tation for any purpose and without fee to
licenses. One of the most popular clones was TIP (TIP isn’t Pico) which
the University of Washington is hereby
granted ...’ The university registered a was created by Chris Allegretta in 1999. Later in 2000 the name was
trademark for the Pine name with respect changed to Nano. 28 In 2001, nano became part of the GNU project.
to ‘computer programs used in commu-
nication and electronic mail applications’ GNU nano implements several features that Pico lacks, including syntax
in March 1995. From version 3.92, the highlighting, line numbers, regular expression search and replace, line-
holder of the copyright, the University of
by-line scrolling, multiple buffers, indenting groups of lines, rebindable
Washington, changed the license so that
even if the source code was still avail- key support, and the undoing and redoing of edit changes.
able, they did not allow modifications
and changes to Pine to be distributed In most modern linux systems, the nano binary is present along with the
by anyone other than themselves. They pico binary, which is actually a symbolic link to the nano binary.
also claimed that even the old license
never allowed distribution of modified You can explore this by finding the path of the executable using the which
versions.
command and long-listing the executable.
28: Mathematically, nano is 10−9 or one
billionth. and pico is 10−12 or one tril- 1 $ which pico
lionth. or put relatively, nano is 1000 2 /usr/bin/pico
times bigger than pico, although the size 3 $ ls -l /usr/bin/pico
of nano binary is smaller than pico.
4 lrwxrwxrwx 1 root root 22 Sep 6 2023 /usr/bin/pico -> /etc/
alternatives/pico
5 $ ls -l /etc/alternatives/pico
6 lrwxrwxrwx 1 root root 9 Sep 6 2023 /etc/alternatives/pico -> /
bin/nano
In nano, the Control key is represented by the ^symbol. The Meta or Alt
key is represented by the M-.
File Handling
Editing
Nano is a simple editor, and you can do without learning any more
commands than the ones listed above, but here are some more basic
commands for editing text.
There are many more commands in nano, but they are omitted from here You can also find third-party cheat sheets
for brevity. You can find the complete list of keybindings by pressing ^G online.
key in nano, or by running info nano.
Since learning nano is mostly to be able to edit a text file even if you are
not familiar with either vim or emacs, let us try to edit a simple script file
to confirm that you can use nano.
1 $ touch myscript.sh
2 $ chmod u+x myscript.sh
3 $ nano myscript.sh
Now try to write a simple script in the file. An example script is shown
below.
86 2 Command Line Editors
1 #!/bin/bash
2 read -rp ’What is your name? ’ name
3 echo "Hello $name"
4 date=$(date "+%H:%M on a %A")
5 echo "Currently it is $date"
If you do not understand how the script
works, do not worry. It will be covered Now save the file by pressing ^S
in depth in later chapters.
Remark 2.4.2 In some systems, the ^S key will freeze the terminal.
Any key you press after this will seem to not have any effect. This is
because it is interpreted as the XOFF and is used to lock the scrolling
of the terminal. To unfreeze the terminal, press ^Q. In such a system,
you can save the file by pressing ^O and then typing out the name
of the file if not present already, and pressing Enter. To disable this
behaviour, you can add the line
1 set -ixon
Now that we are able to edit a text file using text editors, we are ready to
write scripts to solve problems.
Networking and SSH 3
3.1 Networking . . . . . . . 87
3.1 Networking 3.1.1 What is networking? . 87
3.1.2 Types of Networks . . 87
3.1.1 What is networking? 3.1.3 Devices in a Network . 88
3.1.4 IP Addresses . . . . . . 89
3.1.5 Subnetting . . . . . . . 89
Have you ever tried to get some work done on a computer while the
3.1.6 Private and Public IP
internet was down? It’s a nightmare. Modern day computing relies highly
Addresses . . . . . . . . 91
on networking. But what is networking? 3.1.7 CIDR . . . . . . . . . . . 92
3.1.8 Ports . . . . . . . . . . . 92
Definition 3.1.1 (Networking) A computer network comprises two 3.1.9 Protocols . . . . . . . . . 93
or more computers that are connected—either by cables (wired) or 3.1.10 Firewalls . . . . . . . . . 94
3.1.11 SELinux . . . . . . . . . 94
wifi (wireless)—with the purpose of transmitting, exchanging, or
3.1.12 Network Tools . . . . . 95
sharing data and resources.
3.2 SSH . . . . . . . . . . . . 101
3.2.1 What is SSH? . . . . . . 101
We have been using the computer, and linux, for a while now but the 3.2.2 History . . . . . . . . . . 101
utility of a computer increases exponentially when it is connected to 3.2.3 How does SSH work? . 101
a network. It allows computers to share files and resources, and to 3.2.4 Key-based Authentica-
communicate with each other. Current day world wide web is built on tion . . . . . . . . . . . . 102
the internet. 3.2.5 Configuring your SSH
keys . . . . . . . . . . . . 102
3.2.6 Sharing your public key 103
Definition 3.1.2 (Internet) Internet is a global network of networks 3.2.7 How to login to a re-
that connects millions of computers worldwide. It allows computers mote server . . . . . . . 104
to connect to other computers across the world through a hierarchy 3.2.8 Call an exorcist, there’s
of routers and servers. a daemon in my com-
puter . . . . . . . . . . . 104
Learning about networking and how networking works is useful, al- One succinct blogpost explaining how
though we won’t be devling into details in this book. It is left as an the internet works from which the figure
exercise for the reader to explore external resources if they are inter- Figure 3.1 is taken is available at https:
//www.highspeedinternet.com/
ested. resources/how-the-internet-works
If the end goal is to connect computers with each other, one naive solution
might be to connect all the computers with each other. Although this
might seem intuitive at first, this quickly gets out of hand when the
number of computers keep increasing.
Figure 3.1: Types of Networks
If we have 𝑛 computers, then the number of connections required to
connect all the computers with each other is given by the formula
𝑛(𝑛 − 1) 𝑛 2 − 𝑛
=
2 2
This means it will cost a lot to connect all the computers to each other.
This is applicable not only in computer networking with physical wires, Figure 3.2: Growth Rate of Different
Functions - Note how quickly 𝑛 2 grows
88 3 Networking and SSH
but in many other fields. Take an examples of airlines and airplane routes.
If there were 𝑛 airports, then the number of routes required to connect
all the airports is given by the same formula. This would be disastrous
for the economy and the environment if we ran so many airplanes daily.
So what gives?
Hub and Spoke Network
The solution to this problem is to use a hub and spoke model, where
there are one, or multiple, central hubs which connect to many other
nodes. Any path from any node to another goes through one or more
hubs. This reduces the number of connections required to connect all the
Figure 3.3: Hub and Spoke Model Em- nodes.
ployed by Airlines
This is the solution used in airlines, and also in most computer networks.
1
1: Although computer networks use a
hub model for the local area network, Due to this, networks can be classified into three broad categories based
the network of networks, especially the
on their geographical area coverage.
gateway routers follow a mesh model to
ensure redundancy and make the net-
▶ Local Area Network (LAN): A network that covers a small geo-
work more robust.
graphical area, like a home, office, or a building.
▶ Metropolitan Area Network (MAN): A network that covers a
larger geographical area, like a city or a town.
▶ Wide Area Network (WAN): A network that covers a large geo-
graphical area, like a country or the entire world.
identify the devices. This means that the networks are identified by their
IP addresses. This is what we use to connect to the internet. The internet
is nothing but a whole lot of routers communicating with each other
to find the optimal path to send the packets to its destination. Border
Gateway Protocol (BGP) is the protocol used by routers to communicate
with each other and find the optimal path. They usually are connected in
a mesh network to ensure redundancy and robustness.
Level 3 Switch
3.1.4 IP Addresses
So how do routers know which where to send the data packets? This
is where IP addresses come in. To communicate over the internet, two
computers need to know their public IP addresses. The routers then finds
the optimal path to send the data packets to the destination network.
IP addresses are of two types: IPv4 and IPv6. The most common one is
IPv4, which is a 32-bit address represented as four octets separated by
dots. For example,
162.136.73.21
3: An octet is a group of 8 bits. Since an
Here, each octet can take values from 0 to 255. 3 Technically all such IP address is 32 bits, it is represented as 4
groups of 8 bits. 8 bits can only represent
combinations are possible IP addresses, resulting in 232 = 4 , 294 , 967 , 296
numbers from 0 to 255. since 28 = 256.
possible IP addresses. That is a lot of IP addresses, but not enough for
the growing number of devices in the world.
2001 : 0 𝑑𝑏 8 : 85 𝑎 3 : 0000 : 0000 : 8 𝑎 2 𝑒 : 0370 : 7334 Notice that there are some groups of
zeros in the address. These can be com-
pressed by writing only one zero in place
This results in 2128 = 340282366920938463463374607431768211456 possi- of multiple zeros in each group. Further,
ble IP addresses, which is a lot more than IPv4. any leading zeros can be omitted for
each group. Making the above address
as 2001:db8:85a3:0:0:8a2e:370:7334.
Further, if there are multiple groups
3.1.5 Subnetting of zeros, they can be compressed to
::. This can be used only once in
an address. Doing this, the above ad-
Legacy Classes
dress can be compressed further to
2001:db8:85a3::8a2e:370:7334.
10.125.42.62 → 00001010.01111101.00101010.00111110
90 3 Networking and SSH
▶ Class A: The first bit is ‘0‘. The IP addresses in the range 0.0.0.0 to
127.255.255.255.
▶ Class B: The first two bits are ‘10‘. IP addresses in the range 128.0.0.0
to 191.255.255.255.
▶ Class C: The first three bits are ‘110‘. IP addresses in the range
192.0.0.0 to 223.255.255.255.
▶ Class D: The first four bits are ‘1110‘. IP addresses in the range
224.0.0.0 to 239.255.255.255. These are reserved for multicast
addresses.
▶ Class E: The first four bits are ‘1111‘. IP addresses in the range
240.0.0.0 to 255.255.255.255. These are reserved for experimental
purposes.
Class A assigns the first octet to the network part, this is used to identify
which network the machine is in. The remaining three octets are used to
identify the host in that network. This means that a class A network can
have 224 − 2 = 16 , 777 , 214 hosts. However, there can only be 27 = 128
class A networks. Thus, class A networks are used by large organizations
which have many hosts, but not many large organizations exist, so 128
networks are enough.
Similarly, class B assigns the first two octets to identify the network and
the remaining two octets to identify the host. This means that a class B
network can have 216 − 2 = 65 , 534 hosts. And there can be 214 = 16 , 384
class B networks. These are used by medium-sized organizations, which
are plenty in number, and have a moderate number of hosts.
The same goes for class C networks, where the first three octets are used
to identify the network and the last octet is used to identify the host. This
network can have 28 − 2 = 254 hosts. And there can be 221 = 2 , 097 , 152
class C networks. These are used by small organizations, which are plenty
in number, and have a small number of hosts.
Subnet Masks
Usually, each network has only one subnet, which contains all the hosts in
that network. However, the network can be further divided into smaller
subnetworks, each containing a subset of the hosts. This is useful in large
organizations where the network is divided into departments, and each
department is given a subnetwork.
To indicate which part of the IP address is the network part and which
part is the host part, we use a subnet mask. A subnet mask is a 32-bit
number where the first 𝑛 bits are 1s and the remaining bits are 0s. The
number of 1s in the subnet mask indicates the number of bits used to
3.1 Networking 91
As we know, it belongs to the class C network, where the first three octets
are used to identify the network, and the rest is used to identify the host.
So the default network mask is
3.1.7 CIDR
3.1.8 Ports
Ports usually refer to physical holes in a computer where you can connect
a cable. However, in networking, ports refer to logical endpoints for
communication.
3.1.9 Protocols
There are many protocols used in networking, some of the most common
ones are
These protocols act on the different layers of the OSI model. For example,
HTTP, HTTPS, FTP, SSH, etc. are application layer protocols, while TCP,
UDP, etc. are transport layer protocols.
94 3 Networking and SSH
3.1.10 Firewalls
3.1.11 SELinux
This principle states that a user should have only the minimum privileges
required to perform their job. This principle is applied throughout linux,
and also in SELinux.
You can check if SELinux is enabled by running
1 $ sestatus
SELinux uses the concept of RBAC to control the access of users and
processes to files and directories.
There are four components in the SELinux context that are used to control
the access of users and processes to files and directories, as shown in
Figure 3.6.
Modes of SELinux
ip
To find out the private IP address of the NICs of your system, you can 10: ip a also works.
run the ip addr command. 10
96 3 Networking and SSH
Tool Description
ip Show / manipulate routing, devices, policy routing and tunnels
ping To see if the remote machine is up
traceroute Diagnostics the hop timings to the remote machine
nslookup Ask for conversion of IP address to name
dig DNS lookup utility
netstat Print network connections
mxtoolbox Public accessibility of your server
whois Information about the domain
nmap Network port scanner
wireshark Network protocol analyzer and packet sniffer
1 $ ip addr
2 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state
UNKNOWN group default qlen 1000
3 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
4 inet 127.0.0.1/8 scope host lo
5 valid_lft forever preferred_lft forever
6 inet6 ::1/128 scope host noprefixroute
7 valid_lft forever preferred_lft forever
8 2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel
state UP group default qlen 1000
9 link/ether 1c:1b:0d:e1:5d:61 brd ff:ff:ff:ff:ff:ff
10 altname enp3s0
11 inet 192.168.0.109/24 brd 192.168.0.255 scope global dynamic
eno1
12 valid_lft 7046sec preferred_lft 7046sec
13 inet6 fe80::68e2:97e0:38ec:4abc/64 scope link noprefixroute
14 valid_lft forever preferred_lft forever
Here you can see there are two interfaces, lo and eno1. The lo interface
is the loopback interface, and the eno1 interface is the actual network
interface. The IP address of the lo interface is usually always 127.0.0.1.
This address is used to refer to the same system in terms of IP address
without knowing the actual private IP of the system in the LAN.
Remark 3.1.2 The NIC name can be different in different systems. For
a ethernet connection, it is usually eno1 or eth0 which is the legacy
name. For a wifi NIC, it is usually wlan0.
Remark 3.1.3 Earlier the tool used to check the network status was
ifconfig. However, this tool is deprecated now and should not be
used. The new tool to check the network status is ip.
3.1 Networking 97
ping
The ping command is used to check if a remote server is up and running.
It sends an ICMP 11 packet to the remote server and waits for a response. 11: Internet Control Message Protocol
(ICMP) is a supporting protocol in the
Internet protocol suite. It is used by net-
Remark 3.1.4 Only a positive response from the server indicates work devices, including routers, to send
that the server is up and running. A negative response does not error messages and operational informa-
tion indicating success or failure when
necessarily mean that the server is down. Servers can be configured
communicating with another IP address.
to not respond to ICMP packets.
The response of the ping command shows the time taken for the packet
to reach the server and also the resolved IP address of the server.
nslookup
Another tool to lookup the associated IP address of a domain name is
the nslookup command.
1 $ nslookup google.com
2 Server: 192.168.0.1
3 Address: 192.168.0.1#53
4
5 Non-authoritative answer:
6 Name: google.com
7 Address: 172.217.163.206
8 Name: google.com
9 Address: 2404:6800:4007:810::200e
Here you can see the resolved IP address of the domain is 172.217.163.206.
If you copy this IP address and paste it in your browser, you can see that
the website of google opens up. The second address returned is the IPv6
IP Address.
The first lines mentioning the Server is the DNS Server which returned
the resolution of the IP address from the queried domain name.
Remark 3.1.5 Notice that the DNS Server mentioned in the above
output is actually a private IP. This is the IP address of the router in
the LAN which acts as the DNS Server cache. However if you type
the domain of a website which you have not visited, or have visited
long ago into nslookup, then the DNS Server mentioned will be the
public address of the DNS Server, which might be your ISP’s DNS
98 3 Networking and SSH
You can also use mxtoolbox to check the IP address of your server from
the public internet.
dig
And we can then feed the IP address to dig again, to find the domain
name associated with the IP address.
1 $ dig -x 172.217.163.206
2
3 ; <<>> DiG 9.18.27 <<>> -x 172.217.163.206
4 ;; global options: +cmd
5 ;; Got answer:
6 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15781
3.1 Networking 99
Note that the answer we got after running google.com through dig and
then through dig -x (maa05s06-in-f14.1e100.net) is different from the
original domain name.
To get the output of dig in a more readable and concise format, you can
use the +short or +noall option.
1 $ dig +noall +answer google.com
2 google.com. 244 IN A 172.217.163.206
netstat
3.2 SSH
SSH is now the ubiquitous protocol for secure remote access to servers,
and is used by system administrators all over the world to manage their
servers.
SSH lets a user of a computer to log into the computer from another
computer over the network, to execute any command in the terminal that
they have access to.
SSH can also be used to transfer files between two computers using the
scp command.
3.2.2 History
There are multiple implementations of the SSH protocol, the most pop-
ular being OpenSSH, developed by the OpenBSD project. This is the
implementation that is used in most of the linux distributions as well.
To login into a remote server, all you need to do is provide the user-
name and the IP address or the domain name of the server to the ssh
command.
1 $ ssh username@ipaddress
OR
Figure 3.8: Symmetric Encryption
1 $ ssh username@domainname
SSH allows user to login to a remote server using their username and
password, but this is not encouraged since it lets the user to be vulnerable
to brute-force attacks.
One of the most powerful features of SSH is its ability to use public-
private key pairs for authentication. In our course, we emphasize the
importance of this method. Instead of relying on passwords, which can
be vulnerable to brute-force attacks, a pair of cryptographic keys is used.
The public key is stored on the server, while the private key is kept secure
Figure 3.9: Asymmetric Encryption
on your local machine. This ensures a highly secure and convenient way
of accessing remote servers without the need for constantly entering
passwords.
For this course, it is a must for you to not only create, but also understand
SSH keys. Let us quickly see how to create a ssh key-pair which can be
used to login into a remote server.
Here you have to either simply press enter to continue with the default
location, or you can also type a custom location where you want to save
the key. If this is your first time creating keys, it is recommended to use
the default location.
Next, it will ask you to enter a passphrase. You can enter a passphrase
for added security, or you can simply press enter to continue without a
passphrase. If you do add a passphrase, you will have to always enter
the passphrase whenever you use the key. We can continue without a
passphrase for now by pressing enter.
1 Enter passphrase (empty for no passphrase):
2 Enter same passphrase again:
3
4 Your identification has been saved in /home/username/.ssh/
id_ed25519
5 Your public key has been saved in /home/username/.ssh/id_ed25519.
pub
3.2 SSH 103
Our key pair has been generated. The private key is stored in /home/username/.ssh/id_-
ed25519 and the public key is stored in /home/username/.ssh/id_-
ed25519.pub.
Make sure to never share your private key with anyone. Ideally, you
dont even need to see the private key yourself. You should only share
the public key with the server you want to login to.
ssh-copy-id
Finally, to share the public key with the server, there are usually multiple
ways. If the server allows you to login using a password, you can simply
use the ssh-copy-id command. This command will take your username
and password to login to the server, and then copy the public key which
you provide to the server.
1 $ ssh-copy-id -i /key/to/public/key username@ipaddress
Remark 3.2.2 The -i flag is used to specify the path to the public key.
You can drop the .pub from the path as well (making it the path to the
private key), since ssh-copy-id will automatically look for the public
key. However, this flag is not required if you are using the default
location. This is why using the default location is recommended for
beginners. The simplified syntax then becomes
1 $ ssh-copy-id username@ipaddress
The same applies for logging into the server using the ssh command.
manual install
However, most servers do not allow password login at all, since it defeats
the purpose of using a public-private key pair. In such cases, you need to
somehow copy the public key to the server.
If you have physical access to the server, you can simply copy the public
key to the server in the /.ssh/authorized_keys file of the server.
1 $ file ~/someoneskey.pub
2 ~/someoneskey.pub: OpenSSH ED25519 public key
3 $ cat ~/someoneskey.pub >> ~/.ssh/authorized_keys
104 3 Networking and SSH
Remark 3.2.3 Make sure to use the » operator and not the > operator.
The » operator appends the contents of the file to the end of the file,
while the > operator overwrites the file, we do not want that.
However, in case of our course, you do not have access to the server
as well. To submit your public key, you have to login into the web-
site https://se2001.ds.study.iitm.ac.in/passwordless using your
institute credentials, and then submit your public key in the form pro-
vided.
You can print out the contents of the public key using the cat command
and copy the contents into the form.
1 [test1@rex ~]$ cat .ssh/id_ed25519.pub
2 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDxh5EuvzQkGvsqlMQW3rOkY+wyo+2
d6Y5CSqNGlLs2a test1@rex
You should copy the entire contents of the file, including your username
and hostname.
You can then login into the server using the ssh command.
1 $ ssh [email protected]
If successful, you will be logged into the server and the prompt will
change to the server’s prompt.
1 [test1@rex ~]$ ssh [email protected]
2 Last login: Mon Jun 3 07:43:22 2024 from 192.168.2.3
3 29f1001234@se2001:~$
Notice that the prompt has changed from test1@rex which was the
prompt of your local machine, to rollnumber@se2001 which is the
prompt of the server.
There are many daemons running in your computer. You can use
systemctl status to see the loaded and active daemons in your com-
puter.
1 $ systemctl status
2 * rex
3 State: running
4 Units: 419 loaded (incl. loaded aliases)
5 Jobs: 0 queued
6 Failed: 0 units
7 Since: Thu 2024-06-13 12:55:42 IST; 7h ago
8 systemd: 255.6-1-arch
9 CGroup: /
10 |-init.scope
11 | ‘-1 /usr/lib/systemd/systemd --switched-root --system
--deserialize=43
12 |-system.slice
13 | |-NetworkManager.service
14 | | ‘-547 /usr/bin/NetworkManager --no-daemon
15 | |-adb.service
16 | | ‘-558 adb -L tcp:5037 fork-server server --reply-fd
4
17 | |-avahi-daemon.service
18 | | |-550 "avahi-daemon: running [rex.local]"
19 | | ‘-557 "avahi-daemon: chroot helper"
20 | |-cronie.service
21 | | ‘-621 /usr/sbin/crond -n
22 | |-cups.service
23 | | ‘-629 /usr/bin/cupsd -l
24 | |-dbus-broker.service
25 | | |-545 /usr/bin/dbus-broker-launch --scope system --
audit
Here you can see some of the important daemons running, such as
NetworkManager which is used to manage the network connections,
cronie which is used to run scheduled tasks, cups which is used to
manage printers, etc.
sshd
sshd is the daemon that runs on the server and listens to any incoming
SSH connections. It is the daemon that lets you login into the server using
the SSH protocol.
Your own system might not be running the sshd daemon, since you are
not running a server. However, you can check if the sshd daemon is
running using the systemctl command.
1 $ systemctl status sshd
2 * sshd.service - OpenSSH Daemon
3 Loaded: loaded (/usr/lib/systemd/system/sshd.service;
disabled; preset: disabled)
4 Active: inactive (dead)
Here you can see that the sshd daemon is currently inactive. This is
because I am not running a server and don’t usually login remotely to my
system. However, the output of the same command would be something
like as shown below if it is enabled on your system.
1 $ systemctl status sshd
106 3 Networking and SSH
If we run the same command on the server, we can see that it is running.
However, we wont be able to read the logs of the server, since we are not
authorized.
1 $ ssh [email protected]
2 username@se2001:~$ systemctl status sshd
3 * ssh.service - OpenBSD Secure Shell server
4 Loaded: loaded (/lib/systemd/system/ssh.service; enabled;
vendor preset: enabled)
5 Active: active (running) since Thu 2024-06-13 12:32:47 UTC; 1
h 57min ago
6 Docs: man:sshd(8)
7 man:sshd_config(5)
8 Process: 732 ExecStartPre=/usr/sbin/sshd -t (code=exited,
status=0/SUCCESS)
9 Main PID: 745 (sshd)
10 Tasks: 1 (limit: 4557)
11 Memory: 22.0M
12 CPU: 8.769s
13 CGroup: /system.slice/ssh.service
14 ‘-745 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100
startups"
15
16 Warning: some journal files were not opened due to insufficient
permissions.
Remark 3.2.4 Notice that there are some differences in the output
when run from my local system and from the system commands
server. Such as, the name of the service is ssh on the server, while
it is sshd on my local system. Also the full name is OpenBSD Secure
Shell server on the server, while it is OpenSSH Daemon on my local
system. The path of the service file is also different. This is because
the server is running a ubuntu distribution whereas my local system
runs an arch distribution. They have different packages for ssh, and
hence the differences.
I have set the LC_ALL environment variable to the locale C while generating the above
outputs to prevent latex errors. Ideally if you run the command, you will see a prettier
unicode output.