Exploring Linux: The Essential Base for DevOps Engineers
Linux is an operating system that has revolutionized the computing world. It's the backbone of modern infrastructure, especially in the world of DevOps, where automation, scalability, and reliability are paramount. This blog will take you through the origins of Linux, its structure, the role it plays in DevOps, and provide a detailed exploration of the commands that are essential for any DevOps engineer.
The Birth of Linux: A Journey Through Time
In 1991, a Finnish student named Linus Torvalds laid the foundation for what would become one of the most important pieces of software in modern computing: the Linux kernel. While studying computer science at the University of Helsinki, Torvalds was frustrated by the limitations of the existing operating systems. He had access to a UNIX-based system but found it restrictive and expensive. This frustration led him to develop his own kernel, which he initially described as a hobby project.
Linus Torvalds posted his initial version on the MINIX newsgroup in August 1991, offering it for free. Soon, developers from around the world began contributing to the project, and the kernel rapidly evolved into a powerful, open-source operating system that now powers the majority of servers, supercomputers, and mobile devices across the globe. This open-source nature, coupled with its modular architecture, made Linux a favorite for developers and system administrators alike, and eventually, a cornerstone of the DevOps revolution.
Why Linux Dominates the DevOps World
In the realm of DevOps, Linux reigns supreme. There are several reasons why Linux is preferred over other operating systems, like Windows, especially for server and infrastructure management:
Open-source: Linux is freely available and can be modified to suit the needs of developers and engineers.
Customizability: Linux’s modular architecture allows users to customize the operating system to meet specific requirements.
Security: Linux is widely regarded as a more secure platform due to its robust permissions system and smaller attack surface.
Stability: Linux systems, especially distributions like Ubuntu, CentOS, and Debian, are known for their stability and long-term support.
Package Management: Tools like
apt
,yum
, anddnf
make installing, updating, and managing software on Linux servers incredibly efficient.Automation: The strong support for scripting and automation tools makes Linux ideal for automating repetitive tasks, which is a core principle in DevOps.
Windows, while powerful in desktop environments, falls short in areas like scripting, package management, and system resource efficiency when compared to Linux. This makes Linux the clear choice for servers and cloud infrastructure, where performance and reliability are critical.
Why DevOps Engineers Use Linux
Scalability: Linux supports a wide range of hardware architectures and can scale efficiently across systems, from small embedded devices to large server farms.
Automation: Linux allows seamless integration with tools like Docker, Kubernetes, Jenkins, and Ansible, essential for automation.
Cost-Effective: Linux is free and open-source, allowing companies to reduce costs without sacrificing functionality.
Linux Architecture Components
Linux has a modular architecture, which consists of several key components that ensure its stability and functionality.
Key Components:
Kernel: The core of the operating system that manages resources (CPU, memory, I/O devices) and communicates between hardware and software.
System Libraries: Provide standard interfaces for software applications to interact with the kernel. For example, glibc (GNU C Library) is a core system library used by almost all Linux applications.
System Daemons: These are background services (like
sshd
for SSH orcron
for scheduled tasks) that start at boot time and run continuously.User Space: Where user applications run, separated from the kernel for security. User space includes programs, desktop environments, and utilities.
Shell: A command interpreter that provides a user interface to interact with the system.
The Linux Kernel
The kernel is the heart of the Linux OS, acting as a bridge between hardware and software. It handles system resources such as CPU, memory, and I/O devices, making sure they are properly allocated and managed.
How the Kernel Works:
The kernel provides an abstraction layer between user applications and hardware. For example, when a program wants to read a file from disk, it sends a request to the kernel, which retrieves the data by interfacing with the hardware. The kernel then sends the requested data back to the program.
Key Functions of the Kernel:
Memory Management: The kernel manages RAM and swap space, allocating memory to processes and freeing it when no longer needed.
Process Management: It handles the creation, scheduling, and termination of processes.
Device Management: The kernel manages device drivers, allowing communication between software and hardware peripherals (e.g., keyboards, disks).
File System Management: It organizes and handles file operations on various file systems, such as ext4, XFS, and Btrfs.
What is the primary function of the Linux kernel?
Linux System Libraries
System libraries are essential for running applications on Linux. They provide standard routines and interfaces that allow programs to interact with system resources.
Key System Libraries:
Glibc (GNU C Library): The most commonly used system library that provides essential routines for the kernel and user space programs.
libstdc++: A widely used C++ standard library that supports object-oriented programming.
How Libraries Work:
When an application runs, it relies on libraries to perform tasks like memory allocation, file input/output, and process control. These libraries are loaded into memory dynamically during runtime.
Daemons in Linux
Daemons are background processes in Linux that are responsible for various system services. They start at boot and usually run without user intervention.
Common Daemons:
sshd
: The SSH daemon, responsible for managing incoming SSH connections.cron
: The cron daemon, used for scheduling tasks to run at specific times.systemd
: The init system and service manager, responsible for managing daemons, services, and system resources.
Managing Daemons:
You can control daemons with the systemctl
command in systemd
-based distributions:
sudo systemctl start sshd
sudo systemctl enable sshd
sudo systemctl restart cron
User Space
User space refers to the environment where user applications and utilities run. It is separated from the kernel to prevent accidental or malicious interference with the core system functions. When an application needs access to hardware or system resources, it must make a system call to the kernel.
Applications: Programs like web browsers, editors, and database servers run in user space.
Processes: All user-initiated tasks are created and managed as processes in user space.
Shells in Linux
A shell is an interface between the user and the kernel, allowing the user to execute commands, run programs, and manage the system. Shells are an essential tool for DevOps engineers because they enable command-line interaction and script automation.
Types of Shells:
Bash (Bourne Again Shell):
The default shell on most Linux systems, Bash is powerful for both interactive use and scripting.
Example:This Bash script prints numbers from 1 to 5.
for i in {1..5}; do echo "Number $i"; done
Zsh (Z Shell):
- Zsh offers advanced features like auto-completion, spelling correction, and plugins for customization. It's popular for its user-friendly enhancements.
Fish (Friendly Interactive Shell):
- Known for its simplicity and intuitive nature, Fish provides syntax highlighting and autosuggestions out of the box.
KornShell (ksh):
- A Unix shell developed for scripting and interactive use, ksh has features similar to Bash but is more common in enterprise Unix systems.
Linux File System
What Is a File System?
A file system defines how data is stored and retrieved on disk. It manages the organization of files and directories and keeps track of where data is located.
Types of File Systems in Linux
ext4: The most common file system for Linux, offering journaling, better performance, and larger storage limits.
XFS: Known for its scalability, especially on large file systems.
Btrfs: Offers advanced features like snapshotting, pooling, and checksums.
ZFS: Highly reliable, supporting RAID, compression, and error correction, often used in storage servers.
Why Ext4 and Beyond
In Linux, the Ext4 (Fourth Extended Filesystem) is one of the most commonly used file systems. Its widespread usage can be attributed to its robustness, journaling feature, and ability to handle large volumes and file sizes. Ext4 is particularly good at reducing fragmentation and improving performance, especially with solid-state drives (SSDs).
Linux Security Features and SELinux
Why Linux is Secure
Permissions: Linux has a robust permission model where each file and process is owned by a user and a group, with three permission levels: read, write, and execute.
Open-Source Community: Linux’s open-source nature means vulnerabilities are often patched quickly by a global community of developers.
Reduced attack surface: Linux uses privilege separation (root and user), minimizing the number of services running with full administrative access.
Security is a key feature of Linux, making it ideal for server environments and large-scale deployments.
SELinux (Security-Enhanced Linux): A security architecture integrated into Linux that provides a mechanism for supporting access control security policies. It uses mandatory access controls (MAC) instead of the traditional discretionary access controls (DAC).
Usage: SELinux is used in high-security environments to enforce strict policies on what processes can access, execute, or modify. For example, even if a service gets compromised, SELinux will prevent it from executing certain commands or accessing sensitive files.
Modes:
Enforcing: SELinux enforces security policies.
Permissive: SELinux logs policy violations without enforcing them.
Disabled: SELinux is turned off.
Checking SELinux Status:
sestatus
Changing SELinux Modes:
sudo setenforce 0 # Switch to permissive mode
sudo setenforce 1 # Switch to enforcing mode
The Linux File System: Structure and Directories
The Linux file system is structured in a hierarchical tree format. Understanding this structure is critical for DevOps engineers, as it organizes the files and directories that power the system. Each directory serves a unique purpose. Below is a comprehensive look at the primary directories in Linux.
1. /
(Root Directory)
The root directory is the top of the file system hierarchy. Every other directory and file resides within this directory.
- Example:
cd /
moves the user to the root directory.
2. /home
The /home
directory contains user-specific directories. Each user has their own subdirectory within /home
, where their personal files, settings, and data are stored.
- Example:
/home/devopsuser/
stores the files of the userdevopsuser
.
3. /etc
This directory stores system configuration files. Any software you install, services you configure, or system settings will typically have corresponding configuration files here.
- Example: The file
/etc/ssh/sshd_config
holds the configuration for the SSH service.
4. /var
The /var
directory contains variable data that changes during system operation, such as logs, databases, and cache files.
Example: Logs are usually stored in
/var/log/
. You can view the system logs by navigating to this directory and using thecat
ortail
commands.cat /var/log/syslog
displays the system log.tail -f /var/log/syslog
shows the most recent entries in real-time.
5. /bin
The /bin
directory contains essential user binaries (executables). These are the commands available to all users, such as ls
, cat
, and cp
.
- Example:
ls /bin
lists all the basic user commands available.
6. /sbin
Similar to /bin
, but this directory contains system binaries. These are essential commands for system administration, such as iptables
, reboot
, and fdisk
.
- Example:
sudo /sbin/reboot
restarts the system.
7. /usr
The /usr
directory contains user applications and utilities. This is where you’ll find most of the executables for software installed on the system.
- Example:
/usr/local/bin
contains locally compiled software that is not part of the distribution's main repository.
8. /opt
The /opt
directory is reserved for third-party software installations. For example, if you install custom software packages or manually compile software, it is often stored here.
- Example:
/opt/myapp/
could store a custom application you install.
9. /boot
This directory contains the files necessary for the system to boot, including the Linux kernel and bootloader configuration files.
- Example:
/boot/grub/grub.cfg
is the GRUB bootloader configuration file.
10. /dev
The /dev
directory contains device files that represent hardware components, such as hard drives, USB drives, and network interfaces.
- Example:
/dev/sda
represents the first hard drive.
11. /lib
The /lib
directory contains libraries necessary for the essential binaries in /bin
and /sbin
.
- Example:
/lib/x86_64-linux-gnu/
holds shared libraries for the system architecture.
12. /tmp
The /tmp
directory is used for storing temporary files that are deleted upon reboot.
- Example:
cd /tmp
takes you to the temporary storage directory.
Example Use Cases for DevOps:
/etc
is critical for configuration management. If you need to change how a service operates, you'll typically modify files in/etc
. For example, SSH server configurations are found in/etc/ssh/sshd_config
./var/log
is where you’ll find logs for troubleshooting. Application logs are often stored in/var/log
, which is vital for debugging services.
Linux Package Management
Package management is a crucial part of system administration. It allows you to install, update, and remove software from your system.
Popular Package Managers:
APT (Advanced Package Tool): Used in Debian-based systems like Ubuntu.
Example:
sudo apt update sudo apt install nginx
YUM/DNF: Used in RHEL-based distributions like CentOS and Fedora.
Example:
sudo yum install httpd
RPM (Red Hat Package Manager): A low-level package manager used in RHEL-based systems.
Example:
sudo rpm -ivh package.rpm
Snap: A universal package manager that works across distributions.
Example:
sudo snap install vlc
Flatpak: Another universal package manager for Linux.
Example:
flatpak install flathub com.spotify.Client
Linux Permissions and Ownership
Linux permissions are key to maintaining security and control over who can access, modify, or execute files.
File Permissions:
Permissions in Linux are divided into three categories: owner, group, and others. Each category can have read (r), write (w), and execute (x) permissions.
Read (r): The file can be read.
Write (w): The file can be modified.
Execute (x): The file can be executed.
Example of Changing Permissions:
chmod 755 script.sh
This command gives the owner read, write, and execute permissions, while the group and others only get read and execute permissions.
Changing Ownership:
chown username:groupname file.txt
This command changes the ownership of file.txt
to the specified user and group.
File and Directory Management
ls
- List Directory Contents
Usage:
ls -l /var/log
Flags:
l
: Long format listing.a
: Include hidden files.h
: Human-readable sizes.
What command would you use to list all files, including hidden ones, in a directory?
Answer: ls -a
cd
- Change Directory
- Usage:
cd /etc/nginx
mkdir
- Make Directories
Usage:
mkdir /opt/myapp
Flags:
p
: Create parent directories if they don’t exist.
rm
- Remove Files or Directories
Usage:
rm -rf /var/tmp/*
Flags:
r
: Recursively delete.f
: Force deletion.
cp
- Copy Files or Directories
Usage:
cp config.yaml /etc/myapp/config.yaml
Flags:
r
: Recursively copy.v
: Verbose output.
mv
- Move or Rename Files
Usage:
mv oldfile.txt newfile.txt
Flags:
v
: Verbose output.
touch
- Create an Empty File
- Usage:
touch index.html
find
- Search for Files in a Directory Hierarchy
Usage:
find /var/log -name "*.log"
Flags:
name
: Match files by name.type
: Filter by file type.
chmod
- Change File Permissions
- Usage:
chmod 755
script.sh
chown
- Change File Owner and Group
- Usage:
chown root:root /var/www/html
Text and File Content Manipulation
cat
- Concatenate and Display Files
- Usage:
cat /var/log/nginx/access.log
less
- View File Contents Page by Page
- Usage:
less /var/log/nginx/access.log
head
- Output the First Part of Files
Usage:
head -n 10 /var/log/nginx/error.log
Flags:
n
: Specify number of lines to display.
tail
- Output the Last Part of Files
Usage:
tail -f /var/log/nginx/error.log
Flags:
f
: Follow log updates in real time.
grep
- Search Text Using Patterns
Usage:
grep "ERROR" /var/log/nginx/error.log
Flags:
i
: Case-insensitive search.r
: Recursive search.
How would you search for the word "error" in a log file named "app.log"?
Answer: grep "error" app.log
sed
- Stream Editor for Text Manipulation
Usage:
sed 's/
localhost/127.0.0.1/g
' config.yaml
Flags:
i
: Edit files in place.
awk
- Pattern Scanning and Processing Language
- Usage:
awk '{print $1, $4}' /var/log/nginx/access.log
cut
- Remove Sections from Each Line of Files
Usage:
cut -d: -f1 /etc/passwd
Flags:
d
: Specify a delimiter.f
: Fields to extract.
sort
- Sort Lines of Text Files
- Usage:
sort access.log
uniq
- Report or Omit Repeated Lines
- Usage:
sort access.log | uniq -c
Process and System Monitoring
top
- Display System Tasks
Usage:
top
Flags:
u username
: Display processes of a specific user.
htop
- Interactive Process Viewer
- Usage:
htop
ps
- Report Process Status
Usage:
ps aux | grep nginx
Flags:
aux
: List all processes.
kill
- Terminate Processes
Usage:
kill -9 1234
Flags:
9
: Forcefully kill a process.
pkill
- Kill Processes by Name
- Usage:
pkill nginx
systemctl
- Control Systemd Services
Usage:
systemctl start nginx
Flags:
start
: Start a service.stop
: Stop a service.restart
: Restart a service.status
: Check service status.
service
- Control SysVinit Services
- Usage:
service nginx start
Disk and Filesystem Management
df
- Display Disk Space Usage
Usage:
df -h
Flags:
h
: Human-readable format.
du
- Estimate File Space Usage
Usage:
du -sh /var/log
Flags:
s
: Summarize usage.h
: Human-readable format.
mount
- Mount a Filesystem
- Usage:
mount /dev/sdb1 /mnt
umount
- Unmount a Filesystem
- Usage:
umount /mnt
fsck
- Check and Repair a Filesystem
- Usage:
fsck /dev/sda1
Networking Commands
ping
- Send ICMP Echo Requests
- Usage:
ping
google.com
netstat
- Print Network Connections
Usage:
netstat -tuln
Flags:
tuln
: Show TCP/UDP connections and listening ports.
ifconfig
- Configure Network Interfaces
- Usage:
ifconfig eth0
ip
- Show/Manipulate Routing and Devices
Usage:
ip addr show
Flags:
addr
: Display IP addresses.
curl
- Transfer Data from URLs
Usage:
curl -X GET <
https://api.example.com
\>Flags:
X
: Specify request method (GET, POST, etc).d
: Send data.
wget
- Download Files from the Web
- Usage:
wget <
https://example.com/file.zip
\>
scp
- Secure Copy over SSH
- Usage:
scp file.txt user@server:/path/to/destination
ssh
- OpenSSH Client to Log into Remote Machines
- Usage:
ssh user@server
nslookup
- Query Internet Name Servers
- Usage:
nslookup
google.com
dig
- DNS Lookup Utility
- Usage:
dig
google.com
Package Management
apt
- Advanced Package Tool (Debian/Ubuntu)
Usage:
apt install nginx
Flags:
install
: Install a package.update
: Update the package list.upgrade
: Upgrade all packages.
yum
- Yellowdog Updater Modified (RHEL/CentOS)
- Usage:
yum install nginx
dnf
- Dandified Yum (RHEL 8+/Fedora)
- Usage:
dnf install nginx
rpm
- RPM Package Manager
- Usage:
rpm -ivh package.rpm
Archiving and Compression
tar
- Archive Files
Usage:
tar -cvf archive.tar /path/to/directory
Flags:
c
: Create an archive.x
: Extract an archive.v
: Verbose output.f
: Specify filename.
gzip
- Compress Files
- Usage:
gzip file.txt
gunzip
- Decompress Gzip Files
- Usage:
gunzip file.txt.gz
Scripting and Automation
bash
- Bourne-Again Shell
Usage:
bash
script.sh
Flags:
x
: Debug mode (show command execution).
sh
- Execute Shell Scripts
- Usage:
sh
script.sh
crontab
- Schedule Periodic Tasks
Usage:
crontab -e
Flags:
e
: Edit crontab entries.
at
- Schedule a One-time Task
- Usage:
echo "
backup.sh
" | at 02:00
System Administration
sudo
- Execute Commands as Root
- Usage:
sudo apt update
passwd
- Change User Password
- Usage:
passwd username
useradd
- Create a New User
Usage:
useradd -m username
Flags:
m
: Create home directory.
usermod
- Modify User Accounts
- Usage:
usermod -aG sudo username
groupadd
- Create a New Group
- Usage:
groupadd devops
Conclusion
In this guide, we've explored nearly every aspect of Linux, focusing on the core concepts that a DevOps engineer needs to be proficient in. From understanding the kernel and filesystems to mastering user and volume management, every topic has been covered to provide a comprehensive view of Linux in a DevOps context. By mastering these Linux fundamentals, DevOps engineers can ensure efficient, secure, and scalable infrastructure management, which is crucial for the success of any modern IT operation. Whether you're automating tasks, managing servers, or deploying applications, Linux remains an indispensable tool in the DevOps toolkit.
Subscribe to my newsletter
Read articles from Md. Afzal Hassan Ehsani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Md. Afzal Hassan Ehsani
Md. Afzal Hassan Ehsani
I am a student who loves programming. 🌱 Exploring Devops. 🌱I spent time on learning cool stuffs.