Linux Fundamentals
Here you will find basic concepts about the Linux operating system.
Fundamentals
Linux is an operating system like Windows, iOS, Android, or macOS.
Philosophy
Linux is built on core principles:
Everything Is a File: Most system elements, including configuration settings, are stored as text files.
Small, Single-Purpose Programs: Linux provides many tools, each designed to perform one task well.
Chaining Programs: Small programs can be combined to handle complex tasks efficiently.
Avoid captive user interfaces: Emphasis is placed on command-line interfaces (shells) rather than restrictive, captive user interfaces.
Configuration data stored in a text file: Configurations are accessible in text format (e.g., the
/etc/passwd
file), making it easier to understand and modify system behavior.
Components
Bootloader: Initiates the boot process (e.g., GRUB is commonly used in many distros).
Kernel: The core of Linux that manages hardware resources and system processes.
Daemons: Background services that ensure functions like scheduling, printing, and networking run smoothly.
Shell: The command-line interface (e.g., Bash, Zsh) that allows users to interact directly with the OS.
Graphics Server & Window Manager: Provide the graphical user interface (GUI) for visual interactions (examples include GNOME, KDE, and Cinnamon).
Utilities: Applications that perform specific tasks to enhance system functionality.
Linux Distributions
Linux distributions (or "distros") are operating systems built on the Linux kernel. They are adapted for various purposes for example, servers, desktops, embedded systems, and mobile devices.
Examples of distros and their usages:
Fedora/Ubuntu: Used as a desktop linux that's suitable for beginners and everyday users.
Kali Linux/ ParrotOS: Used by cybersecurity specialists.
Debian is known for its stability and reliability, making it a favorite for desktops, servers, and embedded systems. Other operating systems are based on Debian, including but not limited to Kali Linux and Ubuntu.
File System Hierarchy
/
The root directory containing essential files for booting and system operation.
/bin
Contains essential command binaries.
/boot
Static bootloader files, kernel images, and related files.
/dev
Device files that allow access to hardware components.
/etc
Configuration files for the system and installed applications.
/home
User directories for personal data storage.
/lib
Shared library files that are required for system boot.
/media
Mount points for external removable media (e.g., USB drives).
/mnt
Temporary mount point for filesystems.
/opt
Optional software and third-party applications.
/root
The home directory for the root user.
/sbin
System administration binaries used by the root user.
/tmp
Temporary files created by the system and applications.
/usr
User-related programs, libraries, and documentation.
/var
Variable data files, such as logs, emails, and spool files.
The Linux Shell
The Linux shell (or terminal) is a text-based interface that lets you communicate directly with the operating system. Using the shell, you can navigate directories, manage files, execute programs, and automate tasks with scripts, often more efficiently than through a GUI.
The most commonly used shell in Linux is the Bourne-Again Shell (BASH), and is part of the GNU project.
The default prompt typically displays your username, hostname, and current working directory.
For example: <username>
@<hostname>:<current_directory>$
Root User: The prompt often changes to a hash (
#
) instead of a dollar sign ($
), indicating administrative privileges.Home Directory: Represented by a tilde (
~
).
The PS1 variable in Linux systems controls how your prompt appears. These settings are typically found in your shell’s configuration file (e.g.,
.bashrc
for Bash).
Getting Help With Commands
When you encounter an unfamiliar tool or need to understand optional parameters, you have several resources at your disposal:
Manual Pages (man): Use the
man
command to view detailed manuals. For example:man ls
Help Options: Most commands offer quick help via:
command --help
orcommand -h
Apropos: Use
apropos <keyword>
to search the short descriptions of all man pages for a given keyword.
System Information Commands
Below are some commands that help us in understanding system details, processes, and user settings:
whoami
: Displays the current username.id
: Shows user identity and group memberships.hostname
: Prints the system's hostname.uname
: Displays system and kernel information.ifconfig
: Views the network interfaces.ps
: List current processes.who
: Shows who is logged into the system.env
: Displays or sets environment variables.
Navigating the Filesystem
Just as a Windows user relies on the mouse to navigate, Linux users rely on the terminal to move through directories and manage files.
pwd
: Displays the current directory.ls
:Lists files and directories.ls -la
shows all files, including hidden ones (those starting with a dot).
cd
:Changes the current directory.You can specify a full path without navigating one folder at a time.
A single dot (
.
) refers to the current directory.Two dots (
..
) refer to the parent directory.
touch <filename>
: Creates a new, empty file.cat <filename>
: Displays a file's contents.mkdir <dirname>
: Creates a new directory.Use the
-p
option to create multiple parent directories in one go.
tree <dirname>
: Visualizes the directory hierarchy.mv <filename> <new-filename>
: Moves or renames a file.cp <filename> <new-filepath>
: Copies a file.
Text Editing in the Terminal
nano
is a beginner-friendly, text-based editor:When we open nano, below we see two lines with short descriptions. The caret (
^
) stands for our[CTRL]
key.vim
is an another open-source editor for all kinds of ASCII text. It is an improved clone of the previousvi
.Vim is a modal editor that can distinguish between text and command input. it offers a total of 6 modes:
Normal
Default mode where keystrokes are interpreted as commands (no text insertion).
Insert
For entering text; characters are added to the buffer.
Visual
For selecting text blocks to edit, delete, or copy.
Command
For executing single-line commands (accessed by typing :
) to perform operations like saving, quitting, or searching.
Replace
Overwrites existing text with new input.
Ex
Allows sequential command execution, similar to the old Ex editor.
vimtutor
can be used to practice and learn Vim’s commands interactively
Shortcuts
Clearing the Terminal: Use the
clear
command or the shortcut[Ctrl] + [L]
.Command History:
Use the arrow keys (↑/↓) to scroll through previous commands.
Use
[Ctrl] + [R]
to search command history by typing part of a previous command.
Auto-Complete: Typing part of a path (e.g.,
cd /dev/s
) and pressing[TAB]
lets the shell auto-complete directory names.
Finding Files and Directories
which
: Returns the full path of the executable that will be run for the specified command. This allows us to determine if specific programs are available on the operating system.find <location> <options>
: Searches for files and directories in the specified location with filtering options.-type f
: Searches for files.-name *.<ext>
: Finds files with names matching the*.<ext>
pattern.-user root
: Filters for files owned by the root user.-size +#k
: Finds files larger than#
KiB.-newermt yyyy-mm-dd
: Finds files modified after yyyy-mm-dd.-exec ls -al {} \;
: Executes the command (ls -al
) on each found file (the curly brackets act as placeholders).
locate <pattern>
: Quickly searches for files using a local database.sudo updatedb
: Updates the locate database.
File Descriptors and Redirections
A file descriptor (FD) in Unix/Linux operating systems is a reference, maintained by the kernel, that allows the system to manage Input/Output (I/O) operations. It acts as a unique identifier for I/O resources.
By default, the first three file descriptors in Linux are:
STDIN (0): Standard input (data fed into a command).
STDOUT (1): Standard output (regular output from a command).
STDERR (2): Standard error (output for error messages).
We can redirect the descriptors to other sources.
Example:
2>/dev/null
redirects the error messages to null (Discard)Example:
2> stderr.txt 1> stdout.txt
redirects both the error messages and the output to the files.Example:
cat < stdout.txt
redirects the content instdout.txt
to the cat command (Inputted to thecat
commant hence the<
)
Another way to redirect
STDOUT
is to use pipes (|
). These are useful when we want to use theSTDOUT
from one program to be processed by another.Example:
find /etc/ -name *.conf 2>/dev/null | grep systemd | wc -l
When we use the greater-than sign (>
) to redirect our STDOUT
, a new file is automatically created if it does not already exist. If this file exists, it will be overwritten without asking for confirmation. If we want to append STDOUT
to our existing file, we can use the double greater-than sign (>>
).
Filter Contents
Sometimes you want to inspect file contents without opening a text editor. Instead, you can use pagers, tools that let you view files interactively, one screen at a time.
more
: displays file contents screen by screen.Example:
cat /etc/passwd | more
Start at the beginning and scroll forward; press
[Q]
to quit.
less <filename>
: Similar but offers additional features overmore
.head <filename>
: Shows by default only the first 10 lines of a file.tail <filename>
: Shows by default only the last 10 lines of a file.sort
: Sorts the data using different options.grep "pattern"
: Filters results using specified patterns.The option
-v
can be used to set reverse filtering.
cut
: Extracts specific sections from each line of a file based on a delimiter.Example:
cut -d":" -f1
Grabs the first field delimited by a colon (:
)
tr "<section-to-replace>" "<new-section>"
: Replaces a specific section from each line by another provided section.column -t
: Format output into aligned columnsawk
: Process text and extract columns with ease.Example:
awk '{print $1, $NF}'
Displays the first and last fields of each line.
sed
: Makes substitutionsExmaple:
sed 's/bin/HTB/g'
Changes bin with HTB globally.
wc
: Counts words, lines, or characters.
Permission Management
Linux permissions are assigned to both individual users and groups. A user may belong to multiple groups, with each membership potentially granting additional rights to perform specific actions on files and directories.
Every file or directory has an owner and an associated group. Permissions are defined separately for the owner, the group, and others, specifying which actions are allowed.
When a new file or directory is created, it automatically becomes owned by the creator and is associated with their primary group.
A user can belong to multiple groups, with each membership potentially granting additional rights to perform certain actions on files and directories.
Permission Representation
Linux permissions can be expressed using either letters or numbers. In numeric mode, a three-digit value specifies the permissions for the owner, group, and others (for example,
744
):First digit: Owner permissions
Second digit: Group permissions
Third digit: Other users
Each permission has an assigned numeric value:
r (read): 4
w (write): 2
x (execute): 1
For example, consider a file that has read, write, and execute permissions for its owner, and only read permission for the group and others:
Owner:
rwx
= 4 + 2 + 1 = 7Group:
r--
= 4 + 0 + 0 = 4Others:
r--
= 4 + 0 + 0 = 4
Permission Meanings
Read (r):
Files: Allows you to access the file's contents.
Directories: Permits you to list the directory’s contents (i.e., the names of files and subdirectories), though not necessarily view the contents of those files.
Write (w):
Files: Enables you to modify or change the file's contents.
Directories: Allows you to modify the directory’s contents—such as creating, deleting, or renaming files and subdirectories.
Execute (x):
Files: Permits you to run the file as a program.
Directories: Provides access to the directory. With execute permission, you can traverse the directory (using commands like
cd
), view detailed file information (with commands likels -l
), and access subdirectories.
Special Permissions
SUID (Set User ID): When set on an executable file, the SUID bit ensures the file runs with the privileges of the file’s owner, regardless of who executes it.
SGID (Set Group ID): When applied, SGID makes a file execute with the privileges of its group owner. In directories, it causes new files to inherit the group ownership of the directory, which is particularly useful in collaborative environments.
Sticky Bit: Applied at the directory level, the sticky bit restricts file deletion. Even if a user has write permissions for a directory, only the file’s owner (or root) can remove or rename its files.
Last updated