Introduction to Linux
In 1991, Linux, named after its developer Linus Torvalds, originated as a side project.
Linux is an operating system found extensively in various environments like servers, personal computers, mobile devices, and embedded systems.
It is an open-source and free operating system derived from Unix, developed by a global community of users and developers, with no single owner.
The goal behind Linux was to create an operating system that anyone could use and modify as they liked.
Linux has come a long way from its early command-line days. It used to lack graphical interfaces, but developers added them, along with various software applications.
Linux's popularity has grown with increased adoption by businesses and organizations, coinciding with the widespread use of cloud computing.
Now, it's a widely used and flexible operating system, running on everything from small devices to supercomputers. The continuous improvement is thanks to a dedicated community of developers and users.
Why is Linux so popular?
It's all about its built-in features. Let's check them out:
Customizable:
- You can change and personalize Linux to fit your needs.
Open-source:
- Everyone can see and work on the code, which keeps things transparent and encourages the community to help it grow.
Security:
- Linux is less likely to get viruses or malware, making it a safe pick for businesses and people.
Stability:
- It's known for being stable and reliable, making it great for servers and systems that need to work all the time.
Cost-effective:
- Linux is usually free, or very affordable, making it a smart choice for both individuals and businesses.
Technical Terms:
Distros :
A Linux distribution, or distro for short, refers to the official version of the Linux operating system.
(or)
A variant of Linux, bundled with software packages tailored for specific use cases.
Most Linux distributions come with everything you need, including:
The Linux kernel (the core of the operating system).
The desktop environment.
Applications for various tasks and a variety of other useful tools.
Top Linux Distributions:
Ubuntu
Kali Linux
CentOS
ArchLinux
Linux Mint
Commands :
- Commands are instructions that are entered in the terminal, a command-line interface, to interact with the Linux operating system.
Shell :
The shell is, essentially, a program that serves as the interface between the user and the kernel (the core part of the operating system).
It takes the commands entered by the user through the terminal and translates them into a form that the operating system can understand and execute.
It allows users to run commands, manage files, and perform various other tasks by interpreting their inputs and interacting with the system's resources.
Types of shells :
Boarn Shell
Boarn again Shell {BASH}
C Shell {csh}
Koarn Shell {ksh}
z shell {zsh}
Terminal :
A terminal, also known as a command-line interface (CLI) or console, is a text-based interface used to interact with the operating system.
Terminals are often used for tasks that may not have a graphical user interface (GUI).
it provides a way for users to input commands to the system by typing text.
The terminal displays text-based feedback from the system, including the results of executed commands, error messages, and other information.
Linux Architecture :
Linux Architecture provides insights into how the operating system manages resources, executes processes, and interacts with both users and hardware.
Linux architecture consists of several key components:
Hardware:
The hardware layer of Linux is the lowest level of the operating system track. It plays a vital role in managing all the hardware components.
It includes device drivers, kernel functions, memory management, CPU control, and I/O operations.
Kernel:
- The core of the operating system, is responsible for managing hardware resources, providing essential services, and facilitating communication between software and hardware.
Shell:
The command-line interface through which users interact with the operating system.
It interprets commands and scripts, enabling users to execute various tasks.
System Utilities:
- Essential tools and utilities for managing the system, and performing tasks such as file manipulation, process control, user management, network configuration, and system configuration.
Applications:
- Applications in the Linux architecture are software programs that run on top of the operating system and interact with its components.
Linux File System :
A file system is a system used to store and arrange data on a computer.
In Linux, it plays a vital role in the operating system by overseeing the organization of data on various storage devices.
The Linux file system is crafted to offer a structured hierarchy for files and directories, ensuring a straightforward way to navigate and administer data within the system.
The Linux file system is like the blueprint for how the Linux operating system keeps things organized and stores information on a computer.
It follows a tree-like structure starting from the root directory, which is the main directory containing all other directories and files.
This design is built upon the Unix file system, known for its scalability, flexibility, and user-friendly approach to handling data.
A fundamental aspect of the Linux Operating System is its concept that everything, from devices and programs to system information, is treated as a file.
This includes even the most basic commands like for example ls and cat—they are essentially files in the Linux system.
Linux Filesystem Hierarchy :
In the Linux file system, directories, or folders, play a crucial role. They act as containers for organizing and structuring files and other directories.
This framework begins at the root directory (/) and extends into subdirectories as required.
Utilizing this hierarchical structure, complex systems can be logically organized, making it easier to locate and manage files within the system.
/ (Root Directory):
The '/' root directory is the top-level directory in the Linux file system. All other directories and files are contained within the root directory.
To view this, run the following 'List' command :
ls
/bin:
The /bin stands for binaries.
This directory contains essential command-line tools and programs that are required for basic system administration tasks.
/boot:
- The /boot directory contains the boot loader files and kernel images needed to start the system.
/dev:
- The /dev directory contains device files that represent hardware devices and virtual devices such as terminals, printers, and disks.
/etc:
- The /etc directory contains system configuration files that are used by various applications and services on the system.
/home:
The /home directory contains the home directories of users on the system.
Each user has a subdirectory within /home where they can store their files and settings.
/lib:
- The /lib directory contains shared library files that are needed by various programs on the system.
/media:
- The /media directory is used to mount removable media such as CDs, DVDs, and USB drives.
/mnt:
- The /mnt directory is used to mount file systems temporarily, such as network file systems or disk images.
/opt:
- The /opt directory is used to store additional software packages that are not part of the core system.
/proc:
- The /proc directory is a virtual file system that provides information about running processes and system resources.
/run:
- The /run directory contains temporary files that are created by system services and daemons.
/sbin:
- The /sbin directory contains system binaries and administrative tools that are required for system maintenance.
/srv:
- The /srv directory is used to store data for services provided by the system.
/sys:
- The /sys directory is a virtual file system that provides information about the system's hardware and devices.
/tmp:
- The /tmp directory contains temporary files that are created by applications and services running on the system.
/usr:
- The /usr directory contains user-level programs, libraries, documentation, and shared data files.
/var:
- The /var directory contains variable data files that change frequently, such as log files and system databases.
Linux Commands :
- Linux commands are crucial tools for managing, configuring, troubleshooting, and automating tasks in Linux systems.
They are essential for system administrators, developers, and Linux users to efficiently interact with and manage Linux systems.
Linux Directory Commands :
To store a value in a variable :
export NAME=king
echo :
To Print some content on the terminal :
echo 'some content' echo "some content" echo some content
To print the value of some variable :
echo $NAME
Print Working Directory :
To print the current or present working directory
pwd
Change Directory :
To Navigate to a Directory :
cd <dir name/path>
To navigate on step back to the immediate parent directory:
cd ..
To navigate 'n' directories back:
cd ../n time
To Directly go back to the root directory:
cd
List :
To list the directories and files present in the current working directory
ls
To list the content of a specific directory path :
ls <dir name>
To get more information about that directory :
ls -l
To list all the hidden files in that particular directory :
ls -a
we can combine the above commands to get details of all the files including hidden files:
ls -al
To show the content of directories and all the sub-directories:
ls -R
List Contents and Save to a File:
ls | tee directory_contents.txt
Make Directory :
To create a directory/folder :
mkdir <directory name>
To create multiple directories in one :
mkdir dir1 dir2 dir3
To create nested directories:
mkdir -p /dir1/dir2/dir3
Remove Directory:
To remove an empty directory :
rmdir <dir name/path>
To remove a directory that is not empty :
rm -rf <dir name/path>
To Locate a directory :
To Locate a directory :
where <directory path>
Linux File Commands :
Create a File :
To create a file :
touch <file name>
To create multiple files :
touch <file1 path/name> <file2 path/name> <file3 path/name>..
Remove a File:
To remove or delete a file:
rm <file path/name>
To remove or delete multiple files:
rm <file1 path/name> <file2 path/name> <file3 path/name> ..
Move a File:
To move a file from the source path to the destination path:
mv <source path> <destination path>
Copy a File:
To copy a file from the source path to the destination path:
cp <source path> <destination path>
Copy a File and Save Output to a Log:
cp <source> <destination> | tee copy_log.txt
Translate a File:
To convert lowercase to uppercase:
echo "hello" | tr 'a-z' 'A-Z'
To convert uppercase to lowercase:
echo "HELLO" | tr 'A-Z' 'a-z'
You can use
tr
on files as well. For instance:create an
input.txt
write something in lowercase and then use the following commands :cat input.txt | tr 'a-z' 'A-Z' > output.txt cat output.txt
Linux File Content Commands :
Count Lines, Words, and Characters in a File:
wc <file>
Display the Content of the File :
To display the contents of a file :
cat <file name/path>
View File Content and Save to a File:
cat <file> | tee file_contents.txt
Display the contents of a file page-wise:
more :
more 'file name/path' displays the contents of that file one by one page at a time.
Once pressing the spacebar moves to the next page.
more <file name/path>
less :
It is a command in Linux that is used to display the contents of a file page-wise with advanced features of search and navigation.
less <file name/path>
Linux User Commands :
Whoami
:- This command in Linux is used to display the currently logged-in user.
switch user
su
:- The
su
command in Linux is used to switch to another user account from the current account, including the root user account.
- The
Add a User:
To create a new user account :
useradd new-user-name
Add a Group :
To create a new group :
groupadd <groupname>
Delete User :
To Remove a user account :
userdel user-name
Password:
To change the password of a user account :
passwd user-name
Linux Filter Commands :
Search for a pattern in a file or stream:
Global Regular Expression Print (grep):
The grep command in Linux is used to search for a pattern in a file or stream.
grep [options] pattern [file]
options :
w: complete the word
i: ignores case-sensitivity
n: line number
r: searches in sub-directories too
c: cout for number of appearance
l: list all files
Let's analyze a random log file (it contains words like TRACE, INFO) using the grep command :
grep TRACE application.log #it will retun all the occurances of TRACE
To copy those logs into some file:
grep TRACE application.log > traceonly.log
awk :
The
awk
command in Linux is a text-processing tool that allows you to process and manipulate text or data files, typically in the form of columns and rows.It excels at extracting specific information, performing calculations, and formatting output based on defined patterns.
To extract a particular pattern using awk like grep :
#it will find all the occurance of TRACE awk /TRACE/ application.log
To extract the first two columns of the log file :
awk {'print $1,$2'} traceonly.log # this will only print the values in first two columns
To add the line number for each row :
awk {'print NR,$1,$2,$5'} traceonly.log
To print any n lines of the file :
#print first 10 lines awk {'print NR,$1,$2,$5'} traceonly.log | head #print last 10 lines awk {'print NR,$1,$2,$5'} traceonly.log | tail #print first 20 lines awk {'print NR,$1,$2,$5'} traceonly.log | head -20
To specifically define a range of lines to be considered :
#print from line number 20 to 30 awk 'NR>=20 && NR<=30 {print NR,$1,$2,$5}' traceonly.log
To spot a specific pattern and print required columns :
#prints only the occurances of TRACE and required columns awk '/TRACE/ {print NR,$1,$2,$5}' traceonly.log #To print specific lines awk 'NR>=20 && NR<=30 && /TRACE/ {print NR,$1,$2,$5}' traceonly.log
To store the above result in another log file :
#prints lines ranging from line number 20-30 awk 'NR>=20 && NR<=30 && /TRACE/ {print NR,$1,$2,$5}' traceonly.log> fun.log #prints lines ranging from time stam ranging from 8:52:00 - 8:54:00 awk '$2>="8:52:00" && $2<="8:54:00" && /TRACE/ {print NR,$1,$2,$5}' traceonly.log>fun.log
find :
To find a specific file or folder in a specific path based on the name of the file or folder :
find <path> -name <filename> #example 1 find . -name app.py #example 2 find ./data-science-projects -name Readme.md find ./data-science-projects -name *.py
To find a specific file or folder in a specific path based on the owner of the file or folder :
find ./data-science-projects -user king
To find a specific file or folder in a specific path based on the group of that file or folder :
find ./data-science-projects -group dev
sort:
The sort command in Linux is used to sort the lines of a text file in ascending or descending order based on certain criteria.
sort [options] [file]
Linux Utility Commands :
ps:
It is a Linux command for listing active processes.
ps -ef ps [options]
Process management in Linux involves handling and controlling the execution of processes.
Process ID (PID):
Each process is identified by a unique Process ID (PID).
PIDs are assigned sequentially when a process is started.
List Running Processes:
ps aux
kill:
The kill command in Linux is used to terminate processes.
Signals:
- Signals are software interrupts used to communicate with processes. The
kill
command can send signals to processes.
- Signals are software interrupts used to communicate with processes. The
kill [signal] [PID]
System Control and monitoring commands :
Reboot the System:
sudo reboot
Shutdown the System:
sudo shutdown -h now
View System Uptime:
uptime
Check System Information:
uname -a
View System Logs:
dmesg
Monitor System Resources :
top
Monitor Disk Space Usage:
df -h
Monitor Memory Usage:
free -m
Display Network Statistics:
netstat -i
Check Open Ports:
sudo ss -tulpn
Check CPU Information:
lscpu
Linux Networking Commands :
Commands related to networking are employed for the administration of network configurations, inspecting network status, and resolving issues connected to networking.
ifconfig:
It is a Linux command used to configure network interfaces.
ifconfig
ping:
This is a Linux command used to test network connectivity between a local host and a remote host.
ping <ipaddress or domain-name>
Ping a Host and Save Output to a Log:
ping <hostname or IP> | tee ping_log.txt
netstat:
It is a Linux command used to display network connections and related network statistics.
As an example:
netstat -tuln
- shows a list of all TCP (t) and UDP (u), listening (l) and established (n) connections present in the network.
traceroute:
This is a Linux command used to trace the route that packets take from a source host to a destination host over an IP network.
For Example:
traceroute google.com
- traces the route that packets take to reach the google.com domain, displaying the IP addresses of intermediate hops along with their response times.
File Permissions in Linux :
In Linux, each file and directory has an owner and a particular set of permissions.
The owner is the user who has created the file or directory and has permission to determine who can read, write, or execute the file or directory.
Linux users can safeguard their data and keep their systems secure by understanding how file ownership and permissions function in the operating system.
Based on the ownership, we can categorize the Linux file owners into 3 categories:
User :
one who has created the file.
The user category is denoted by the username of the owner.
Only the owner of the file has the right to modify or delete the file by default.
Groups :
group of users who have the same permissions to access the file.
The group category is denoted by the group name of the users.
Consider a project where several people need access to a file. we could add all users to a group and give the group read/write access to all files instead of giving each user their permissions.
This would prevent anyone outside of the group from reading or changing the files and also will reduce the time for manually adding permissions to each user.
Others :
The others category refers to all other users who are not the owner of the file or a member of the group.
The others category is denoted by the keyword "others".
By default, other users have no permission to access the file.
File Permission :
Three basic types of permissions can be assigned to a file or directory in Linux:
Read Permission (r ): user or the group the ability to view the contents.
Write Permission (w): user or group the ability to modify or delete a file or directory.
Execute Permission (x): user or group the ability to execute a file or change it into a directory.
How to Manage Ownership and Groups :
First, display all the permissions, groups, and owners associated with the directory using the following commands :
ls -l
d represents directory
r represents read permission
w represents write permission
x represents execute permission
- represents no permission
To change the user/owner associated with the file :
chown username filename
To change the group associated with the file :
chown :groupname filename
or
chgrp groupname filename
or
sudo usermod -aG new_group username
To change both the owner and group in one go.
chown username:groupname filename
How to Manage Permissions?
To manage permissions for a particular file or directory :
chmod permissions resource-name
Permission :
let us consider a few examples :
suppose we have Three files, one Directory, Two users : [root, king] and Two groups [root, king] :
ls -l -rw-r--r-- 1 root root 0 Nov 17 21:32 file1.txt -rw-r--r-- 1 king root 0 Nov 17 21:32 file2.txt -rw-r--r-- 1 king king 0 Nov 17 21:32 file3.txt drwxr-xr-x 2 root king 64 Nov 17 21:33 test
Assign
rwx
permissions for file1.txt only to the owner of the file :chmod 700 file1.txt
Assign
rw
permissions for file2.txt only to the group of the file :chmod 060 file2.txt
Assign
r
permissions for file3.txt to all users of the file :chmod 444 file3.txt
change the permission of the files present within the test directory:
chmod -R 444 docs
Result:
ls -l -rwx------ 1 root root 0 Nov 17 21:32 file1.txt ----rw---- 1 king root 0 Nov 17 21:32 file2.txt -r--r--r-- 1 king king 0 Nov 17 21:32 file3.txt dr--r--r-- 2 root king 64 Nov 17 21:33 test
Special Permission Modes :
SUID (Set User ID):
- Assigns the same rights as the file owner to a user that executes a file.
SGID (Set Group ID):
Assigns the same rights as the group that created the file to the person who executes it.
The process of installation using a package manager typically involves the following steps:
Package Manager / Process of Installation :
A package manager is a tool used in Linux operating systems to simplify the process of installing, updating, configuring, and removing software packages.
few package managers commonly used in various Linux distributions:
Advanced Package Tool (APT):
Used by Debian and Ubuntu-based systems.
Commands include
apt-get
andapt
.
YUM (Yellowdog Updater Modified):
- Commonly used in Red Hat-based systems such as Fedora and CentOS.
DNF (Dandified YUM):
- The next-generation package manager replacing YUM used in Fedora and newer versions of CentOS.
Pacman:
- Used in Arch Linux and its derivatives. Commands include
pacman
for package management.
- Used in Arch Linux and its derivatives. Commands include
Homebrew:
- Primarily used on macOS, but also available for Linux. It is a package manager for installing Unix tools.
The commands to install and uninstall packages :
Updates the local package list, ensuring the system has the latest information about available packages.
sudo apt update
Installs a specific package on the system.
sudo apt install package_name
Uninstalls a specific package from the system.
sudo apt remove package_name
Removes orphaned dependencies that are no longer needed by any installed packages.
sudo apt autoremove
Searches for packages related to the specified package_name.
sudo apt search package_name
Show System Activity in Real-Time :
sudo apt-get install sysstat # On Debian/Ubuntu sudo yum install sysstat # On CentOS/RHEL
sar