Linux Syllabus
Modules Titles
Installing and connecting to a Linux Distributions
Linux System Installing VirtualBox on Windows
Installing CentOS Linux Using an
Image for VirtualBox
Installing Linux from Scratch
Installing CentOS Linux from
Scratch
Getting Connected
Connecting Directly
Linux Fundamentals Linux Directory Structure
Shell Interpreter
Basic Linux Commands
Command Line Interface
Working with Directories
Listing Files and Understanding ls
Output
File and Directory Permissions
Explained - Part 1
File and Directory Permissions
Explained - Part 2
Finding Files and Directories
Viewing Files and the Nano Editor
Editing Files with Emacs
Graphical Editors
Deleting, Copying, Moving, and
Renaming Files
Intermediate Linux Skills Wildcards - Part 1
Wildcards - Part 2
Input, Output, and Redirection
Comparing Files
Searching Files and Using Pipe
Transferring and Copying Files over
the Network
Customizing the Shell Prompt
Shell Aliases
Environment Variables
Processes and Job Control
Scheduling Repeated Jobs with Cron
Switching Users and Running
Commands as Others
Shell History and Tab Completion
Installing Software
The Linux Boot Process and The Linux Boot Process
System Logging The Linux Boot Process - Demo
System Logging
Disk Management Disk Management Introduction
Creating Partitions with the fdisk
Command
File Systems
Logical Volume Manager(LVM) Introduction to Logical Volume
Manager (LVM)
Logical Volume Manager (LVM) -
Layers of Abstraction
Creating Physical Volume (PV),
Volume Group (VG), and Logical
Volume (LV)
Extending Volume Group (VG) and
Logical Volume (LV)
Mirroring Logical Volume (LV)
Removing Logical Volume (LV),
Physical Volume (PV), and Volume
Group (VG)
Migrating Data from One Storage
Device to Another
Logical Volume Manager (LVM) -
Summary
User Management Managing Users and Groups - Part 1
Managing Users and Groups - Part 2
Networking TCP/IP Networking for Linux
System Administrators
Domain Name System (DNS) and
Hostnames
Dynamic Host Configuration
Protocol (DHCP) and Dynamic and
Static Addressing
Network Troubleshooting - Part 1
Network Troubleshooting - Part 2
Advanced Linux Permissions Special Permission Modes - Part 1
Special Permission Modes - Part 2
Shell Scripting Shell Scripting - Part 1
Shell Scripting - Part 2
Module 1: Introduction to Linux
Overview of Operating Systems
Definition of an operating system
Types of operating systems
Introduction to Linux
History of Linux
Linux distributions
Getting Started with Linux
Installing a Linux distribution
Basic Linux commands
File system navigation
Module 2: Linux File System
File System Hierarchy
Understanding the Linux file system structure
Key directories and their purposes
File and Directory Operations
Working with files and directories
File permissions and ownership
File Manipulation Commands
Copying, moving, and deleting files
Using wildcards
Module 3: Advanced Linux Commands
Text Processing Tools
grep, awk, sed for text manipulation
Archiving and Compression
tar and gzip commands
Process Management
Managing processes with ps and kill
Background and foreground processes
Module 4: Shell Scripting
Introduction to Shell Scripting
- Basics of shell scripting
- Writing and running a simple script
- 11. Variables, Conditions, and Loops
- Working with variables
- Conditional statements and loops
- Practical Shell Scripting Examples
- Writing scripts for common tasks
- Scripting best practices
Module 5: System Administration
User and Group Management
Adding, modifying, and deleting users
Managing groups
Package Management
Package managers (apt, yum, etc.)
Installing and updating software packages
Disk Management
Managing disks and partitions (fdisk, du, df)
Filesystem maintenance
Module 6: Networking
Basic Networking Commands
- Configuring network interfaces (ifconfig, ip)
- Testing network connectivity (ping, traceroute)
SSH and Remote Access
- Using SSH for secure remote access
- File transfer with scp
Firewall Configuration
- Configuring firewalls (ufw, iptables)
Module 7: Security
User Authentication and Permissions
- Managing user passwords (passwd)
- File and directory permissions
Security Best Practices
- Keeping the system updated
- Regular backups
Module 8: Project and Practical Application
Real-world Project
- Applying Linux skills to a practical project
- Troubleshooting and problem-solving
Module 1
Installing and Connecting to a Linux System
Linux Distributions
Linux is a popular operating system that is used by millions of people
worldwide. It is known for its open-source nature, flexibility, and security. There
are many different Linux distributions, each designed to meet the needs of
different types of users.
Some of Linux Distributions are:
1. Ubuntu
Ubuntu is a popular Linux distribution that is targeted towards a
wide range of users, including −
Desktop Users − Ubuntu aims to provide a user-friendly and polished
desktop experience for general users who are looking for a free and open-
source alternative to proprietary operating systems such as Windows and
macOS.
Developers − Ubuntu has a strong focus on supporting developers and
providing tools and frameworks for software development.
System Administrators − Ubuntu offers a stable and secure server edition
with long-term support (LTS) releases, making it suitable for server
deployments in enterprise environments.
Cloud Computing − Ubuntu is widely used in cloud computing
environments, with special editions such as Ubuntu Server and Ubuntu
Core optimized for cloud deployments.
2. Debian
Debian, one of the oldest and most respected Linux distributions,
targets a specific set of users, including −
System Administrators − Debian's stable release cycle and reliable
package management system make it a preferred choice for system
administrators who require a stable and secure operating system for
managing servers and networks.
Developers − Debian offers a wide range of development tools, libraries,
and compilers, making it a suitable choice for developers who want a solid
foundation for building software applications.
Education and Research Users − Debian is often used in educational and
research settings due to its stability, flexibility, and wide range of software
packages for various fields of study and research.
3. Fedora
Fedora is a popular Linux distribution that is targeted towards several
types of users, including −
Advanced Linux Users − Fedora is known for its community-driven
development model, which attracts advanced Linux users who prefer a
more hands-on approach and enjoy experimenting with new technologies.
Education and Research Users − Fedora offers a wide range of
educational and research software packages, making it suitable for
students, teachers, researchers, and educational institutions.
4. CentOS
CentOS aims to provide a stable, secure, and reliable operating
system for business-critical applications. Its target users include −
Enterprise users − CentOS is commonly used in enterprise environments
where stability and security are of utmost importance.
Data centres − CentOS is widely used in data centres, which are facilities
used to house computer systems and related components, such as
telecommunications and storage systems.
5. Kali Linux
Its main purpose is to provide a robust and comprehensive platform
for penetration testing, forensics, and security auditing. As such, the target
users of Kali Linux include −
Cybersecurity professionals − Kali Linux is widely used by cybersecurity
professionals, including penetration testers, security consultants, and
network security specialists.
Security researchers − Kali Linux is popular among security researchers
who conduct in-depth research and analysis of various security topics,
including malware analysis, reverse engineering, and vulnerability
discovery.
Forensics experts − Kali Linux provides a wide range of tools for data
recovery, analysis, and forensic investigation of digital devices
Installing VirtualBox on Windows
Minimum Requirements
Ram: 4 GB
25 GB Free Disk
CPU: 2 Cores
Ubuntu ISO image
Virtual Box with Extension
Step 1: google Oracle Virtual Box
Step 2: Install VM Box on Local Machine
Step 3: On Google Type [Link]/download and Download ISO Image
Step 4: Load ISO image and Set the requirement software requirements and
Start
Module - 2
Linux Fundamentals
Linux Directory Structure
In Linux/Unix operating system everything is a file even directories are
files, files are files, and devices like mouse, keyboard, printer, etc are also files.
Here we are going to see the Directory Structure in Linux.
Types of files in the Linux system.
General Files – It is also called ordinary files. It may be an image, video,
program, or simple text file. These types of files can be in ASCII or Binary
format. It is the most commonly used file in the Linux system.
Directory Files – These types of files are a warehouse for other file types. It
may be a directory file within a directory (subdirectory).
Device Files – In a Windows-like operating system, devices like CD-ROM, and
hard drives are represented as drive letters like F: G: H whereas in the Linux
system devices are represented as files. As for example, /dev/sda1, /dev/sda2,
and so on
Directories Description
/bin binary or executable programs.
/etc system configuration files.
/home home directory. It is the default current directory.
/opt optional or third-party software.
/tmp temporary space, typically cleared on reboot.
/usr User related programs.
/var log files.
/boot It contains all the boot-related information files and folders such
as conf, grub, etc.
/dev It is the location of the device files such as dev/sda1, dev/sda2,
etc.
/lib It contains kernel modules and a shared library.
/srv It contains server-specific and server-related files.
System Configuration File
Configuration Description
Files
/etc/bashrc It is used by bash shell that contains system defaults and
aliases.
/etc/crontab A shell script to run specified commands on a
predefined time interval.
/etc/exports It contains information on the file system available on
the network.
/etc/fstab Information of the Disk Drive and their mount point.
/etc/group It is a text file to define Information of Security Group.
/etc/[Link] It is the grub bootloader configuration file.
/etc/init.d Service startup Script.
/etc/[Link] It contains lilo bootloader configuration file.
/etc/hosts Information of IP and corresponding hostnames
/etc/[Link] It contains a list of hosts allowed accessing services on
the local machine.
/etc/[Link] List of hosts denied accessing services on the local
machine.
/etc/inittab INIT process and their interaction at the various run
levels.
/etc/issue Allows editing the pre-login message.
/etc/[Link] It contains the configuration files for the system
modules.
/etc/motd It contains the message of the day.
/etc/mtab Currently mounted blocks information.
/etc/passwd It contains username, password of the system, users in
a shadow file.
User related files
User Related Descriptions
Files
/usr/bin It contains most of the executable files.
/usr/bin/X11 Symbolic link of /usr/bin.
/usr/include It contains standard files used by C program.
/usr/share It contains architecture independent shareable text files.
/usr/lib It contains object files and libraries.
/usr/sbin It contains commands for Super User, for System
Administration.
Log Files
Log Files Descriptions
/var/log/lastlog It stores user’s last login info.
/var/log/messages It has all the global system messages
/var/log/wtmp It keeps a history of login and logout information.
Shell Interpreter
A shell interpreter is a command-line interpreter that lets Linux and
Unix users control their operating systems with command-line interfaces. It is a
special user program that provides an interface to the user to use operating system
services. Shells accept human-readable commands from the user and convert
them into something which the kernel can understand. They are command
language interpreters that execute commands read from input devices such as
keyboards or from files.
Kernel
The kernel is a computer program that is the core of a computer’s
operating system, with complete control over everything in the system. It
manages the following resources of the Linux system –
File management
Process management
I/O management
Memory management
Device management etc.
Command Line Shell
Shell can be accessed by users using a command line interface. A special
program called Terminal in Linux/macOS, or Command Prompt in Windows
OS is provided to type in the human-readable commands such as “cd”, “mkdir”
etc. and then it is being executed. The result is then displayed on the terminal to
the user.
Basic Linux Commands
Some Basic Commands of Linux
Linux Commands Functions
Is Displays information about files in the current
directory.
pwd Displays the current working directory.
mkdir Creates a directory.
cd To navigate between different folders.
rmdir Removes empty directories from the directory
lists.
cp Moves files from one directory to another.
mv Rename and replace the files
rm Delete files
uname Command to get basic information about the OS
locate Find a file in the database.
touch Create empty files
ln Create shortcuts to other files
cat Display file contents on terminal
clear Clear terminal
ps Display the processes in terminal
man Access manual for all Linux commands
grep Search for a specific string in an output
echo Display active processes on the terminal
wget download files from the internet.
whoami Create or update passwords for existing users
sort sort the file content
cal View Calendar in terminal
whereis View the exact location of any command typed
after this command
df Check the details of the file system
wc Check the lines, word count, and characters in a
file using different options
Difference Between wget and curl
‘wget’ (short for "web get") is primarily used to download files from
HTTP, HTTPS, and FTP servers. It supports recursive downloading,
meaning it can download all the files linked to from a given web page.
Example: wget [Link]
‘curl’ (short for "client URL") is a more versatile tool that not only
downloads files but also supports sending and receiving data using
various protocols like HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP,
LDAP, and more. It can be used to interact with web services, upload
files, and perform various other network-related tasks.
Example: curl [Link]
Command Line Interface
The Command Line Interface (CLI), is a non-graphical, text-based
interface to the computer system, where the user types in a command and the
computer then successfully executes it. The Terminal is the platform or the IDE
that provides the command line interface (CLI) environment to the user.
Working with Directories
A directory in Linux is a type of file system object that can be used to
organize files. A directory can contain files and other directories. It is similar to a
folder in Windows.
Directories are used to organize files in a hierarchical structure, similar to
the way that a tree is organized with branches and leaves.
Each directory in Linux has a name and a path. The name is used to identify
the directory within its parent directory, and the path is used to locate the directory
in the file system. The root directory in Linux is represented by a forward slash
(/), and all other directories are organized under it. For example, the /home
directory contains the user’s home directories, while the /var directory contains
variable data files, such as log files
Create a directory
$ mkdir demo : This would create a demo directory under the current directory.
$mkdir demo1 demo2 : if you wanted to create two directories called “demo1”
and “demo2” in the current directory
create a parent directory in Linux
The -p option stands for “parents” and it tells the mkdir command to create any
necessary parent directories that don’t already exist.
If you want to create a hierarchy of directories, you can use the -p option with the
mkdir command
$ mkdir -p /home/user1/test/dir1 : This will create a directory called “test”
under the “user1” directory, and a directory called “dir1” under the “test”
directory
More Linux commands about directory in Linux
The following are some Linux commands that can be used to manage directories:
rmdir: This command is used to delete directories. It can only be used if the
directory is empty.
rm: This command is used to delete files.
chmod: This command is used to change the permissions of files and directories.
chown: This command is used to change the owner of a file or directory.
mv: This command is used to move or rename files and directories.
cp: This command is used to copy files and directories.
cd: To change into a directory
ls -l: To list the contents of a directory
pwd: To find out your current working directory
Listing Files and Understanding “ls” Output
The “ls” command lists files and directories within the file system, and
shows detailed information about them. It is a part of the GNU core utilities
package which is installed on all Linux distribution.
“ls” displays a list of the names of all files in the current working directory
$ ls
Output
cache db empty games lib local lock log mail opt run spool tmp
Long Listing Format
The default output of the ls command shows only the names of the files and
directories, which is not very informative.
The -l (lowercase L) option tells ls to print files in a long listing format.
When the long listing format is used, you can see the following file information:
The file type.
The file permissions.
Number of hard links to the file.
File owner.
File group.
File size.
Date and Time.
$ ls -l /etc/hosts
Output
-rw-r--r-- 1 root root 337 Oct 4 11:31 /etc/hosts
Show Hidden Files
By default, the ls command will not show hidden files. In Linux, a hidden file is
any file that begins with a dot (.).
To display all files including the hidden files use the -a option:
$ ls -la
drwxr-x--- 10 linuxize linuxize 4096 Feb 12 16:28 .
drwxr-xr-x 18 linuxize linuxize 4096 Dec 26 09:21 ..
-rw------- 1 linuxize linuxize 1630 Nov 18 2017 .bash_history
drwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 bin
drwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 Desktop
drwxr-xr-x 4 linuxize linuxize 4096 Dec 12 2017 .npm
drwx------ 2 linuxize linuxize 4096 Mar 4 2018 .ssh
File and Directory Permissions Explained - Part 1
Linux is a multi-user operating system, so it has security to prevent
people from accessing each other’s confidential files. When you execute a “ls”
command, you are not given any information about the security of the files,
because by default “ls” only lists the names of files. You can get more information
by using an “option” with the “ls” command. All options start with a ‘-‘. For
example, to execute “ls” with the “long listing” option, you would type ls -l .
When you do so, each file will be listed on a separate line in a long format.
1. The first character = ‘-‘, which means it’s a file ‘d’, which means it’s a
directory.
2. The next nine characters = (rw-r–r–) show the security
3. The next column shows the owner of the file. (Here it is `root`)
4. The next column shows the group owner of the file. (Here it is `root`
which has special access to these files)
5. The next column shows the size of the file in bytes.
6. The next column shows the date and time the file was last modified.
7. Last Column = File_name or Directory_name. (For example, here are:
prac, snap, test, example)
What are the three permission groups in Linux?
First, you must think of those nine characters as three sets of three characters
(see the box at the bottom). Each of the three “rwx” characters refers to a
different operation you can perform on the file.
Owners: These permissions apply exclusively to the individuals who own the
files or directories.
Groups: Permissions can be assigned to a specific group of users, impacting only
those within that particular group.
All Users: These permissions apply universally to all users on the system,
presenting the highest security risk. Assigning permissions to all users should be
done cautiously to prevent potential security vulnerabilities.
--- --- ---
rwx rwx rwx
user group other
What are the three kinds of file permissions in Linux?
There are three kinds of file permissions in Linux Read, write, and execute.
Letters Definition
‘r’ “read” the file’s contents.
‘w’ “write”, or modify, the file’s
contents.
‘x’ “execute” the file. This permission is
given only if the file is a program.
Commands to change the permission
The command you use to change the security permissions on files is called
“chmod”, which stands for “change mode” because the nine security characters
are collectively called the security “mode” of the file.
$ chmod 777 <filename>: Gives read, write execute permission to the file
$ chmod 435 <filename>: Both the codes give read (code=4) user permission,
write and execute (code=3) for the group and read and execute (code=5) for
others.
Finding Files and Directories
The find command is one of the most powerful tools in the Linux system
administrators’ arsenal. It searches for files and directories in a directory
hierarchy based on a user given expression and can perform user-specified action
on each matched file.
You can use the find command to search for files and directories based
on their permissions, type, date, ownership, size, and more. It can also be
combined with other tools such as grep or sed.
find Command Syntax
The general syntax for the find command is as follows:
find [options] [path...] [expression]
The options attribute controls the treatment of the symbolic links, debugging
options, and optimization method.
The path... attribute defines the starting directory or directories where find will
search the files.
The expression attribute is made up of options, search patterns, and actions
separated by operators.
Find Command : find file based on name
$ find . -type f -name <filename> - finds file “f”
$ find . -type d -name <filename> - finds directory “d”
$ find . -type f -size – finds based on file size
$ find . -type f -name “*.txt” – Finds based on extension
$ find . -type f -mtime +10 – finds files created 10 days ago
$ find . -type f -mtime -10 - finds files created 10 days before
$ find . -type f -mmin +10 - file created above 10 min
$ find . -type f -mmin -10 - file created before 10 min
$ find . -type f -perm 777 - based on permission
Viewing Files and the Nano Editor
Files are one of the most important things that you interact with on a
Linux PC. Some of the most common files you will encounter on a Linux system
include configuration files, log files, and scripts.
The ability to easily view files from the command line is a powerful
feature that Linux provides to its users. This guide will show you the different
command-line utilities that you can use to view files in Linux.
1. Cat
The cat utility is one of the most used commands for viewing file content in
Linux. You can use the command for concatenating and printing standard file
output. To view the contents of a file using cat, simply type the command name
followed by the file you want to view.
$ cat <filename>
2. Nl
The “nl” command, short for number lines, is very similar to the cat command,
with the exception that the nl command numbers the output lines by default.
$ nl <filename>
3. Head
Sometimes you might only want to view the first few lines of a file, and this is
where the head utility comes in handy. By default, only the first 10 lines of a file
are shown.
$ head -10 <filename>: To view first 10 lines
4. Tail
The tail command works in an almost opposite manner to the head utility i.e. it
outputs the last part of a file. By default, the last 10 lines of a file are shown
$ tail-10 <filename>: To view first 10 lines
Editors
1. Vi
The default editor that comes with the UNIX operating system is called
vi (visual editor). Using vi editor, we can edit an existing file or create a new file
from scratch. we can also use this editor to just read a text file. The advanced
version of the vi editor is the vim editor.
$vi <filename>
Common vi Editor Commands for Inserting and Replacing text:
To edit the file, we need to be in the insert mode. There are many ways to enter
insert mode from the command mode.
Command Description
i Inserts text before current cursor location
a Insert text after current cursor location
A Insert text at the end of current line
o Creates a new line for text entry below cursor location and
switches to insert mode.
O Creates a new line for text entry above cursor location and
switches to insert mode.
s Replaces single character under the cursor with any number of
characters and switches to insert mode.
R Overwrites text from the cursor to the right, without switching to
insert mode.
Save and Exit Commands in Vi Editor in Linux:
Need to press [Esc] key followed by the colon (:) before typing the following
commands:
Commands Description
q Quit
q! Quit without saving changes i.e. discard changes.
r [filename] Read data from file called [file_name]
wq Write and quit (save and exit).
w Write to file called [file_name] (save as).
w! Overwrite to file called [file_name] (save as forcefully).
!cmd Runs shell commands and returns to Command mode.
Searching and replacing in (ex-Mode) in Vi Editor (Linux):
vi also has powerful search and replacement capabilities. The formal syntax for
searching is:
:s/string
:s/string1/string2/g : To replace string 1 with string 2
2. Nano
Nano is a user-friendly, simple and WYSIWYG(What You See Is What
You Get) text editor, which improves the features and user-friendliness of UW
Pico text editor. Unlike vim editor or any other command-line editor, it doesn’t
have any mode. It has an easy GUI(Graphical User Interface) which allows users
to interact directly with the text in spite of switching between the modes as in vim
editor.
$nano new_filename
press Ctrl+o : To save a file
Ctrl+o is used to cut
Ctrl+u is used to paste the text.
Editing Files with Emacs
The Emacs is referred to a family of editors, which means it has many
versions or flavors or iterations. The most commonly used version of Emacs
editor is GNU Emacs and was created by Richard Stallman. The main difference
between text editors like vi, vim, nano, and the Emacs is that is faster, powerful,
and simple in terms of usage because of its simple user interface. Unlike the vi
editor, the Emacs editor does not use an insert mode, and it is by default in editing
mode, i.e., whatever you type will directly be written to the buffer, unless you
manually enter command mode by using keyboard shortcuts. Installing the Emacs
Editor.
To use emacs editor, use command – “emacs [-option] [file name]” (without
quotation marks)
$ emacs <filename>: This command creates a file called [Link] if it doesn’t
already exist. If the file with that name already exists, it’s content is copied to the
memory buffer and shown at the editing buffer area.
Note: Using the emacs command with no filename opens the default interface of
the emacs editor, as shown in the below image. This screen is user-friendly and
you can navigate using the link options highlighted in the screen, like the option
visit new file creates a new file buffer for you to start writing.
Emacs Common Options:
1. –file file_name, –find-file file_name, –visit file_name This option is used
to provide file name to edit. However, in most cases, this is not required
and directly file name can be mentioned.
2. +number The number here specifies the line number in the file which is
followed in the command, and the cursor is moved to that line. There
should be no space between the number and the + sign.
3. +line:column Here line represents the line number or row and the column
represents the number of characters. The cursor is automatically placed to
this position in the file that is followed.
4. -q, –no-init-file This option prevents Emacs from loading an initialization
or init file.
5. –no-splash This option prevents Emacs from showing splash screen at
startup.
6. -u user, –user user Load user’s init file.
7. –version To display version and license information.
8. –help Display help.
General Shortcuts:
ctrl-x ctrl-f : Find file or Open a file. This command prompts for a file name
and opens it in buffer for editing. Also, it creates a new file if it doesn’t
already exist.
ctrl-x ctrl-s : Save File. This saves the current buffer content to the file.
ctrl-x ctrl-w : Write to file. This command prompts for a file name to save
buffer.
Graphical editors in Linux
The command line editors nano , vi and emacs are appropicate to use
when you connect to a server via SSH or when you are using a terminal emulator
application.
If you are running Linux as a desktop operating system, you also have
access to graphical editors.
1. emacs only works at the command line, but it also has a graphical mode.
The key strokes that work on command line version also work in the
graphical verison.
2. gedit is the default text editor for the Gnome desktop environment. It is
very similar to Notepad.
3. gvim is the graphical version of vi. The key strokes that work on command
line version also work in the graphical verison.
4. kedit is similar to gedit, it is the default editor for the KDE desktop
environment.
Deleting, Copying, Moving, and Renaming Files
Deleting files
1. rm (remove)
Deleting files using the Terminal is simple, and generally wrapped up by one
command: rm. You can expand rm as remove. In the CLI mode, the only attribute
that the rm command requires is the name of the file. For example, if I need to
delete a file named ‘test’ which is in my working directory, I have to write:
$ rm test
2. rm -r
First one, the ‘-r’ option. The rm command doesn’t remove any directories that
have files in them. The ‘r’ in any basic Linux command (generally) stands for
recursive. What that means is that the command will act on any sub-directories
or the files contained by a directory, assuming that you run the command on the
directory.
So if you require to delete any directory, you have to use the ‘rm -r‘ option. For
example, if I have a file test inside the directory testdir, a plain rm command
won’t remove it, but with the -r option, it will.
$rm -r testdir:
3. rm -rf
Next, the -f option. It is the force delete option. It deletes files and directories
Copying
1. cp
The command used is cp. The cp command requires two attributes. One,
the location of the file that you want to copy, and next, the location where you
want to copy. Now here, I will copy a file named test to the directory testdir.
$cp test testdir
2. cp -r
It copies the files in a directory recursively. If you use plain cp to copy a
directory that has files, the cp command will simply omit the directory and move
on. So, a -r option is necessary to copy a folder.
$ cp -r testdir1 testdir2
3. cp -i
The next option is -i. The -i option is called the interactive mode. If you use this
option, the CLI will prompt you before copying every file to the destination. If
you want to copy, you enter ‘yes’ or just press enter. If you don’t want to copy,
enter ‘no’.
$ cp -i *
Moving
mv
Moving files is similar to copying files. You need to use the command,
then the location of the file you want to move, and the destination. The command,
in this case, is mv, which naturally means move. But a piece of good news here.
The mv command does not require the -r option even to move directories. Here’s
an example of moving testdir2 to testdir.
$ mv testdir2 testdir
Renaming
For renaming, as you know, the mv command is used as well. So if you
notice, Linux doesn’t rename the same file, but moves the file to the same
directory again, but with a new name. So it is necessarily the same contents of the
file, but the file is new, with a new name.
Here’s an example of the mv command to rename. I will rename the file test as
tested.
$ mv test tested
Module – 3
Intermediate Linux Skills
Wildcards - Part 1
Wildcards in Linux is used to match filenames. There are three main
wildcards in Linux:
An asterisk (*) – matches one or more occurrences of any character,
including no character.
Question mark (?) – represents or matches a single occurrence of any
character.
Bracketed characters ([ ]) – matches any occurrence of character enclosed
in the square brackets.
Syntax of Wildcards used in Linux
The basic set of wildcards in Linux includes below:
* – This wildcard represents all the characters
? – This wildcard represents a single character
[] – This wildcard represents a range of characters.
Options Description
* To list out all the files.
A* Any file that begins with the letter ‘A.’
A*txt Any file that begins with the letter ‘A’
and ends with a txt
List?? Any file that begins with List followed
by 2 characters.
[abc]* Any file that begins with either ‘a’ or
‘b’ or ‘c’ and ending with any number
of characters.
[[:upper:]] Any file that begins with an uppercase
letter
[![:digit:]]* Any file that does not begin with a
numeral
[a-d]? Any file that begins from a range of a-
d and followed by exactly 1 character.
A??f Any file that begins with the letter ‘A’
followed by exactly two characters and
ending with ‘f’.
Let us take the below example where there are few files. We will take this
example and do pattern matching with wildcards in Linux
1. ‘?’
When we use the wildcard as ‘?’, it will search for characters starting with S and
ending with f and exactly one character in between them.
Example:
$ ll A?f
Here, we can see that files starting with ‘A’ followed by exactly one character
and ending with ‘f’ are displayed.
2. ‘*.’
The wildcard ‘*’ means it will match any number of characters or a set of
characters. For example, S**n will match anything between S and n. The
number of characters between them do not count.
$ ll A*f
Here, we can see in the result that files starting with ‘A’ and ending with ‘f’ are
displayed. Here ‘*’ can include any number of characters in between them.
3. []
The wildcard ‘[]’ means it will match characters that are enclosed in square
braces.
$ ll A[b-c]f
Here, we can see in the result that files starting with ‘A’ followed by a range of
characters from b to c and ending with f are displayed.
4. Combination of all wildcards
When we try to search for any pattern in Linux, there might be a situation where
we would need to combine one or more wildcards with matching the pattern
that we are searching for.
$ ls -l ?*f
Here, one character and any number of characters are displayed with ending
characters as f are displayed in the result set.
Input, Output, and Redirection
In Linux, whenever an individual runs a command, it can take input,
give output, or do both. Redirection helps us redirect these input and output
functionalities to the files or folders we want, and we can use special commands
or characters to do so.
For example, if we run the “date” command, it gives us output on the
screen. However, if we want to save the result in a file instead, we can use
output redirection. This way, we can store the output of the date command in a
file and refer to it later. These redirections can come in handy when we work
with multiple and large outputs or inputs since we can use file data directly as
input and store results in files.
All this can be done easily on the terminal using some simple
commands. In this article, we will talk about different types of Redirections in
linux and how to use Redirections in linux.
Types of Redirections
1. Overwrite Redirection:
Overwrite redirection is useful when you want to store/save the output of a
command to a file and replace all the existing content of that file. for example,
if you run a command that gives a report, and you want to save the report to the
existing file of the previous report you can use overwrite redirection to do this.
“>” standard output
“<” standard input
Implementation:
So whatever you will write after running this command, will be redirected and
copied to the “[Link]”. This is standard output redirection.
$ cat > [Link]
Now, this is standard input redirection, cat command will take the input
from “[Link]” and print it to the terminal screen. This line of code also shows the
real working and meaning of the cat command that is copy and paste. Many
people have a misconception that the cat is used to create a file, but it is not true,
the main work of the cat is to copy the input and give the output to the screen.
$ cat < [Link]
2. Append Redirection:
With the help of this Redirection, you can append the output to the file without
compromising the existing data of the file.
“>>” standard output
“<<” standard input
Implementation:
A here-document is used to redirect input into an interactive shell
script or program. You can run any program within a shell script without user
action by supplying the required input for the interactive program, or interactive
shell script.
3. Merge Redirection:
This allows you to redirect the output of a command or a program to a specific
file descriptor instead of standard output. the syntax for using this is “>&”
operator followed by the file descriptor number.
“p >& q” Merges output from stream p with stream q
“p <& q” Merges input from stream p with stream q
Implementation:
Error Redirection: Error redirection is transferring the errors generated by
some false commands to a file rather than STDOUT.
Whenever a program is executed at the terminal, 3 files are generated: standard
input(0), standard output(1), standard error(2). These files are always created
whenever a program is run. By default, an error stream is displayed on the
screen.
Examples:
1. In the below-mentioned example, the file descriptor used above is 2
(STDERR). Using “2>” re-directs the error output to a file named “[Link]”
and nothing is displayed on STDOUT.
$ somerandomcommand 2>[Link]
2. Here, 2>&1 means that STDERR redirects to the target of STDOUT. More
formally, the error message generated by “2” gets merged with the current
output “1”.
$ ls GEEK GFG > [Link] 2>&1
4. Pipe redirection
With pipe redirection, you send the standard output of a command to standard
input of another command.
Let me show you a practical example. Say, you want to count the
number of visible files in the current directory. You can use ls -1 (it’s numeral
one, not letter L) to display the files in the current directory:
You probably already know that wc command is used for counting number of
lines in a file. If you combine both of these commands with pipe, here’s what
you get:
$ ls -1 | wc -l
Output: 4
Comparing Files
1. diff command in Linux with examples
diff stands for difference. This command is used to display the differences in the
files by comparing the files line by line. Unlike its fellow members, cmp and
comm, it tells us which lines in one file have is to be changed to make the two
files identical.
The important thing to remember is that diff uses certain special symbols and
instructions that are required to make two files identical. It tells you the
instructions on how to change the first file to make it match the second file.
$ diff [options] File1 File2
2. cmp Command:
cmp compares two files byte by byte.
It outputs the first mismatch and byte position.
Efficient for binary files.
3. sdiff Command:
sdiff provides a side-by-side comparison of files.
Differences are highlighted, making it easy to identify variations.
Searching Files and Using Pipe
A pipe is a form of redirection (transfer of standard output to some
other destination) that is used in Linux and other Unix-like operating systems to
send the output of one command/program/process to another
command/program/process for further processing. The Unix/Linux systems allow
the stdout of a command to be connected to the stdin of another command. You
can make it do so by using the pipe character ‘|’.
The pipe is used to combine two or more commands, and in this, the
output of one command acts as input to another command, and this command’s
output may act as input to the next command, and so on. It can also be visualized
as a temporary connection between two or more commands/ programs/ processes.
The command line programs that do the further processing are referred to as
filters.
This direct connection between commands/ programs/ processes
allows them to operate simultaneously and permits data to be transferred between
them continuously rather than having to pass it through temporary text files or
through the display screen.
Pipes are unidirectional i.e., data flows from left to right through the pipeline.
$ ls | grep [Link]
In this first we are using `ls` to list all file and directories in the current directory,
then passing its output to `grep` command and searching for file name `[Link]`.
The output of the ls command is sent to the input of the grep command, and the
result is a list of files that match the search term.
Transferring and Copying Files over the Network
1. Using ftp
ftp is a network protocol for transferring files between a client and a server. We
can use the ftp command-line client to connect to an ftp server and perform file
transfers over the internet.
2. Using sftp
Apart from ftp, we can use sftp to transfer files securely. sftp uses the ssh protocol
to authenticate and establish a secure connection. With sftp, we can set up the ssh
key to ease our authentication process.
3. Using ssh Through scp
In Linux, we can share files between computers using scp. scp utilizes ssh to
securely transfer files. We use the following syntax to copy files from the source
machine to the destination machine:
$ scp /path/to/local/file username@destination:/path/to/destination
On the other hand, we can copy files from the remote machine to our local
machine by reversing the source and destination paths in the scp command
4. Web-Based File Sharing
To use web-based file sharing, we need to set up a web server and configure it to
serve files from a specific directory. In Linux, we can configure Apache2 to server
files.
5. Using rsync
rysnc is a local and remote file-copying tool included with most Linux
distros. When we use this tool, we can quickly transfer and sync files to or from
remote locations. Files and directories copied with rysnc retain their links,
devices, owners, groups, and permissions.
The first time, the program copies the entire content of a file or directory
from source to destination. However, it only copies the changed blocks and bytes
from the next instance. rsync uses a remote-update protocol that transfers only
the differences between the files transferred. This makes it faster than scp. While
sending and receiving data, rsync uses compression and decompression methods
that consume less bandwidth.
Customizing the Shell Prompt
The shell prompt in Linux is a powerful tool that allows users to customize
their command-line interface (CLI) to provide useful information and improve
user experience. Customizing the prompt involves modifying the PS1 (Prompt
String 1) environment variable.
1. Understanding PS1:
PS1 is an environment variable that defines the format of the shell prompt.
It can include various placeholders representing information like the
username, hostname, current directory, and more.
2. Basic Customization:
To set a basic custom prompt, use the PS1 variable in the shell.
Example:
$ PS1="CustomPrompt$ "
This sets the prompt to "CustomPrompt$ ".
3. Common Placeholders:
Common placeholders include:
- \u: Username
- \h: Hostname
- \w: Current working directory
- \n: Newline
- \t: Time (24-hour format)
Example:
$PS1="\u@\h:\w$ "This sets the prompt to
"username@hostname:/current/directory$ ".
4. Colorizing the Prompt:
Colors can be added to make the prompt visually appealing.
Use ANSI escape codes for colors.
Example:
$ PS1="\[\033[1;32m\]\u@\h:\w$\[\033[0m\] - "This sets a green-colored
prompt.
5. Customizing for Root (Superuser):
It's common to have a distinct prompt for the superuser (root).
6. Multiline Prompt:
For a multiline prompt, use the \n placeholder.
Example:
$ PS1="\u@\h:\w\n$ "
This sets a prompt with each component on a new line.
7. Advanced Customization:
Use command substitution to include dynamic information.
Example:
$ PS1="\u@\h [\$(date +'%H:%M:%S')]:\w$ "
This includes the current time in the prompt.
8. Making Changes Permanent:
To make changes permanent, add the PS1 customization to the user's
profile file (e.g., .bashrc or .bash_profile).
Example (for Bash):
$ echo 'PS1="\u@\h:\w$ "' >> ~/.bashrc
Shell Aliases
Alias command instructs the shell to replace one string with another string
while executing the commands.
When we often have to use a single big command multiple times, in those
cases, we create something called as alias for that command. Alias is like a
shortcut command which will have same functionality as if we are writing the
whole command
$ alias [-p] [name[=value] ... ]
Environment variables
Environment variables or ENVs basically define the behaviour of the
environment. They can affect the ongoing processes or programs executed in the
environment. Every Linux process has an associated set of environment variables
that influence its behaviour and interactions with other processes.
Accessing Environment Variables
In Linux, environment variables are typically accessed through the shell. The
shell is a command-line interface that interprets and executes commands entered
by the user. It provides a way to set, modify, and retrieve environment variables.
The most common shell in Linux is the Bash shell (Bourne Again Shell), which
is the default shell for many distributions.
Scope of an environment variable
The scope of any variable is the region from which it can be accessed or over
which it is defined. An environment variable in Linux can have global or local
scope.
$name
$echo $name
Some commonly used environment variables
Environment Variables Description
$USER Gives search path for commands.
$PATH Gives search path for commands.
$HOME Gives path of home directory.
$PWD Gives the path of present working directory.
$HOSTNAME Gives name of the host.
$LANG Gives the default system language.
$EDITOR Gives default file editor.
$UID Gives user ID of current user.
$SHELL Gives location of current user’s shell program
Processes and Job Control
In the Linux operating system, jobs refer to processes that are running in
the background or foreground. Job control refers to the ability to manipulate these
processes, including suspending, resuming, and terminating them. This can be
useful for managing multiple tasks or for debugging problems with a process.
Job control is made possible by the shell, which is a command-line
interface that allows users to interact with the operating system. The most
common shell in Linux is the Bourne Again Shell (BASH), but other shells such
as the Z Shell (ZSH) and the Korn Shell (KSH) are also available.
Understanding Processes and Jobs in Linux
In Linux, every program that is running is considered a process. A process
can be a standalone program or a part of a larger program.
Each process is assigned a unique identifier called a process ID (PID). The
PID can be used to refer to the process and perform actions on it, such as
suspending or terminating it.
A job is a process that is either running in the foreground or the
background. The foreground is the active window in the terminal, and the
background is any process that is running but not actively being used in the
terminal.
By default, when you run a command in the terminal, it runs in the
foreground. You can tell that a process is running in the foreground because it
displays output and you cannot enter any more commands until it finishes.
To run a process in the background, you can use the & symbol at the end
of the command.
$ sleep 30 &
[1] 12345
In this example, the sleep command causes the process to sleep for 30
seconds. The & symbol causes the process to run in the background, and the
output [1] 12345 indicates that it is job number 1 with a PID of 12345.
$ ps (Process status) can be used to see/list all the running processes.
$ ps –f -f (full) can be used along with ps
Fields described by ps are described as:
UID: User ID that this process belongs to (the person running it)
PID: Process ID
PPID: Parent process ID (the ID of the process that started it)
C: CPU utilization of process
STIME: Process start time
TTY: Terminal type associated with the process
TIME: CPU time is taken by the process
CMD: The command that started this process
Scheduling Repeated Jobs with Cron
What is a cron?
Cron is a job scheduling utility present in Unix like systems. The crond
daemon enables cron functionality and runs in background. The cron reads the
crontab (cron tables) for running predefined scripts.
By using a specific syntax, you can configure a cron job to schedule scripts
or other commands to run automatically.
Cron job syntax
Crontabs use the following flags for adding and listing cron jobs.
crontab -e: edits crontab entries to add, delete, or edit cron jobs.
crontab -l: list all the cron jobs for the current user.
crontab -u username -l: list another user's crons.
crontab -u username -e: edit another user's crons.
Syntax:
$ * * * * * sh /path/to/[Link]
In the above example,
* * * * * represents minute(s) hour(s) day(s) month(s) weekday(s), respectively.
Minutes 0-59 Command would be executed at the specific minute.
Hours 0-23 Command would be executed at the specific hour.
Days 1-31 Commands would be executed in these days of the
months.
Months 1-12 The month in which tasks need to be executed.
Weekdays 0-6 Days of the week where commands would run. Here, 0 is
Sunday.
* * * * * sh /path/to/script/[Link]
| | | | | |
| | | | | Command or Script to Execute
| | | | |
| | | | |
| | | | |
| | | | Day of the Week(0-6)
| | | |
| | | Month of the Year(1-12)
| | |
| | Day of the Month(1-31)
| |
| Hour(0-23)
|
Min(0-59)
Switching Users and Running Commands as Others
1. Using su
su is a command-line tool that is commonly used to switch users in Linux.
Additionally, it also allows us to execute scripts or commands as another user.
2. Sudo
sudo is another command-line tool that allows users to execute scripts as
another user. In this article, we’ll be skipping the details about the sudo command.
Instead, we’ll focus on utilizing sudo to execute scripts as another user.
Shell History and Tab Completion
1. Shell History:
Viewing Command History: The history command displays a list of
previously executed commands along with their line numbers.
$ history
Repeating Previous Commands: The !! (double exclamation mark) repeats
the last command.
$ !!
Executing Specific Commands from History: Use ! followed by the
command number to execute a specific command from history.
$ !123
Searching Command History: Press Ctrl + R and start typing to search for
a command in history.
Use the arrow keys to navigate through the search results.
History Substitution: Modify and rerun previous commands using history
substitution.
!!:s/old/new/ - This substitutes "old" with "new" in the last command.
2. Tab Completion:
Basic Tab Completion: Pressing the Tab key completes commands, file names,
and directories.
Example: Typing ls /pa and pressing Tab completes to ls /path.
Tab Completion for Commands: Pressing Tab twice shows a list of available
commands.
Example: Type ls and press Tab twice to see a list of ls variations.
Module – 4
The Linux Boot Process and System Logging
The Linux Boot Process
Many processes are running in the background when we press the system’s
power button. It is very important to learn the booting process to understand the
working of any operating system.
Stages of Linux Boot Process:
The machine’s BIOS (Basic Input/Output System) or boot microcode
hundreds and runs a boot loader.
Boot loader finds the kernel image on the disk and loads it into memory, to
start the system.
The kernel initializes the devices and their drivers.
The kernel mounts the basis filesystem.
The kernel starts a program referred to as init with a method ID zero
init sets the remainder of the system processes in motion.
For some purpose, init starts a method permitting you to log in, typically
at the top or close to the top of the boot sequence.
When the Linux kernel starts, it receives a group of text-based kernel
parameters containing some further system details. The parameters specify many
alternative kinds of behavior, like the number of diagnostic outputs the kernel
ought to manufacture and device driver–specific choices. The Ro parameter
instructs the kernel to mount the basis filesystem in read-only mode upon user
area begins. This normal read-only mode ensures that fsck can safely check the
basis filesystem before attempting to do something serious. When the check, the
bootup method remounts the basis filesystem in read-write mode.
Boot Loader Overview:
1. GRUB – A near-universal normal on Linux systems, with BIOS/MBR and
UEFI versions.
2. LILO – One of the primary Linux bootloaders. ELILO could be a UEFI
version.
3. SYSLINUX – It may be organized to run from many alternative styles of
filesystems.
4. LOADLIN – Boots a kernel from DOS.
5. System-boot a straightforward UEFI boot manager.
6. coreboot – A superior replacement for the computer BIOS which will
embody a kernel.
7. Linux Kernel EFISTUB A kernel plug-in for loading the kernel directly
from associate EFI/UEFI System Partition.
8. EFI Linux – A UEFI boot loader meant to function as a model and
reference for different UEFI boot loaders.
Linux Boot Process Demo
Linux system logging
As a Linux system administrator, inspecting log files is one of the most
common tasks that you may have to perform. Linux logs are crucial they store
important information about some errors that may happen on your system. They
might also store information about who’s trying to access your system, what a
specific service is doing, or about a system crash that happened earlier.
Linux Logging Types
When dealing with Linux logging, there are a few basics that you need to
understand before typing any commands in the terminal.
On Linux, you have two types of logging mechanisms:
Kernel logging: related to errors, warning or information entries that your
kernel may write;
User logging: linked to the user space, those log entries are related to
processes or services that may run on the host machine.
Introduction to System Logging
1. Location of System Logs:
Logs are typically stored in the /var/log directory.
Common log files include syslog, [Link], messages, and application-
specific logs.
2. Syslog Daemon:
Syslog Architecture:
Syslog is a standard protocol used for message logging.
Consists of a syslog daemon that receives and processes log messages.
Syslog Configuration:
The main configuration file is /etc/[Link] or /etc/syslog-ng/syslog-
[Link] depending on the syslog daemon in use.
Configuration defines where logs are stored, log levels, and destinations.
Logging Levels:
Messages are categorized into severity levels, including DEBUG, INFO,
WARNING, ERROR, and CRITICAL.
Allows filtering and prioritizing messages.
3. Log Rotation:
Log Rotation Configuration:
Log rotation is essential to manage log file sizes and prevent disk space
issues.
Configuration files for log rotation are typically found in
/etc/[Link] and /etc/logrotate.d/.
Rotating Log Files:
Log rotate rotates log files based on predefined rules.
Common rotation intervals include daily, weekly, or when a log file reaches
a certain size.
4. Log Files and Their Content:
/var/log/syslog:
General system messages logged by syslog daemon.
Contains messages from various system components.
/var/log/[Link]:
Authentication-related messages, including user logins.
Important for monitoring security-related events.
/var/log/messages:
General system messages.
Similar to /var/log/syslog on some distributions.
/var/log/[Link]:
Kernel-related messages.
Helpful for diagnosing kernel-level issues.
/var/log/[Link]:
Log messages related to the system boot process.
Application-Specific Logs:
Many applications log events in their specific log files (e.g.,
/var/log/nginx/[Link] for Nginx).
5. Viewing and Searching Logs:
Viewing Logs with cat and less:
Use commands like cat or less to view log files.
Example: less /var/log/syslog.
Searching Logs with grep:
Use grep to search for specific patterns in log files.
Example: grep "error" /var/log/syslog.
Using journalctl (systemd systems):
On systems using systemd, use journalctl to query and display messages
from the journal.
Example: journalctl -xe.
6. Logging Best Practices:
Custom Logging in Applications:
Developers should implement proper logging in applications to facilitate
troubleshooting.
Regular Log Review:
Regularly review log files to identify potential issues or security events.
Log Monitoring Tools:
Use log monitoring tools like logwatch, ELK Stack (Elasticsearch,
Logstash, Kibana), or Graylog for centralized log management.
Security Considerations:
Limit access to log files to prevent unauthorized access.
Use secure protocols for log forwarding.
7. Log File Permissions:
Setting Proper Permissions:
Log files should have restrictive permissions (chmod 600 or similar) to
ensure only authorized users can access them.
Example: sudo chmod 600 /var/log/syslog.
By understanding and managing system logging in Linux, administrators can
effectively monitor system health, troubleshoot issues, and enhance overall
security. Regular log review is crucial for maintaining a secure and stable system.
Module – 4
Disk Management
Disk Management Introduction
Disk management in Linux is the process of creating and manipulating disk
partition tables, mounting partitions on appropriate directories and making file
system in partitions. There are several tools that can be used for disk management,
both in command line and graphical user interface. Some of the most popular
ones are fdisk, GNU Parted, Gparted, GNOME Disks, KDE Partition Manager
and Qtparted. These tools can help you to add or remove storage devices, check
disk usage and space, and format partitions.
Disk Partitioning in Linux
Disk Partitioning is the process of dividing a disk into one or more logical
areas, often known as partitions, on which the user can work separately. It is one
step of disk formatting. If a partition is created, the disk will store the information
about the location and size of partitions in the partition table. With the partition
table, each partition can appear to the operating system as a logical disk, and users
can read and write data on those disks. The main advantage of disk partitioning
is that each partition can be managed separately.
The disk is a very important hardware resource in Linux system, how to
manage them effectively is directly related to performance of the whole system.
If you have some experiences on the Linux disk management and should know
that “df”, “du” and “fdisk” these three commands:
1. df is used to check the file system disk usage
o df -h: Running df -h prints this information in a human-readable
format (i.e., in KB, MB, GB)
2. du checks a disk space usage,
3. fdisk is used to disk partition.
4. gdisk - GUID Partition Table (GPT) Partitioning:
o gdisk is an alternative to fdisk for GUID Partition Table (GPT)
partitioning.
Fdisk command in Linux
fdisk also known as format disk is a dialog-driven command in Linux used
for creating and manipulating disk partition table. It is used for the view, create,
delete, change, resize, copy and move partitions on a hard drive using the dialog-
driven interface.
fdisk allows you to create a maximum of four primary partitions and the
number of logical partitions depends on the size of the hard disk you are using. It
allows the user:
To Create space for new partitions.
Organizing space for new drives.
Re-organizing old drives.
Copying or Moving data to new disks(partitions).
$ fdisk -l : View All Disk Partitions
$ fdisk -l /dev/sda : View Partition on a Specific Disk
$ fdisk /dev/sda : Create a Hard Disk Partition, Now you have to type “n” to
create new partition and then type “p” for making a primary partition and “e” for
making an extended or logical partition depending on the type of partition to
make, type “d” to go to the delete partition menu
File Systems
A Linux file system is a structured collection of files on a disk drive or a
partition. A partition is a segment of memory and contains some specific data. In
our machine, there can be various partitions of the memory. Generally, every
partition contains a file system.
The general-purpose computer system needs to store data systematically so
that we can easily access the files in less time. It stores the data on hard disks
(HDD) or some equivalent storage type.
The Linux file system contains the following sections:
The root directory (/)
A specific data storage format (EXT3, EXT4, BTRFS, XFS and so on)
A partition or logical volume having a particular file system.
1. Ext, Ext2, Ext3 and Ext4 file system
The file system Ext stands for Extended File System. It was primarily
developed for MINIX OS. The Ext file system is an older version, and is no longer
used due to some limitations.
Ext2 is the first Linux file system that allows managing two terabytes of data.
Ext3 is developed through Ext2; it is an upgraded version of Ext2 and contains
backward compatibility. The major drawback of Ext3 is that it does not support
servers because this file system does not support file recovery and disk snapshot.
Ext4 file system is the faster file system among all the Ext file systems. It is a
very compatible option for the SSD (solid-state drive) disks, and it is the default
file system in Linux distribution.
2. JFS File System
JFS stands for Journaled File System, and it is developed by IBM for AIX
Unix. It is an alternative to the Ext file system. It can also be used in place of
Ext4, where stability is needed with few resources. It is a handy file system when
CPU power is limited.
3. ReiserFS File System
ReiserFS is an alternative to the Ext3 file system. It has improved
performance and advanced features. In the earlier time, the ReiserFS was used as
the default file system in SUSE Linux, but later it has changed some policies, so
SUSE returned to Ext3. This file system dynamically supports the file extension,
but it has some drawbacks in performance.
4. XFS File System
XFS file system was considered as high-speed JFS, which is developed for
parallel I/O processing. NASA still using this file system with its high storage
server (300+ Terabyte server).
Module – 6
Logical Volume Manager (LVM)
Introduction to Logical Volume Manager (LVM)
Logical Volume Manager (LVM) is used on Linux to manage hard drives
and other storage devices. As the name implies, it can sort raw storage into logical
volumes, making it easy to configure and use.
One exception to the previous statement is that you can not use logical
volumes for /boot. That is because GRUB (the most common bootloader for
Linux) can't read from logical volumes. The well-known alternative to GRUB,
systemd-boot on the other hand reads only vfat filesystems, so that's not going to
work either.
Layers of Abstraction:
Physical Volumes (PV):
Definition: Physical Volumes are the lowest level in the LVM hierarchy
and represent raw storage devices.
Purpose: They act as a foundation for LVM, grouping together physical
disks or partitions.
Volume Groups (VG):
Definition: Volume Groups are created by combining one or more Physical
Volumes.
Purpose: VGs provide a logical abstraction, pooling together the storage
capacity from multiple PVs.
Logical Volumes (LV):
Definition: Logical Volumes are created within Volume Groups.
Purpose: They serve as virtual partitions and are what users and
applications interact with, similar to traditional partitions.
Creating Physical Volume (PV), Volume Group (VG), and Logical Volume
(LV):
1. Creating Physical Volume:
Command: sudo pvcreate /dev/sdX
Explanation: This command initializes a physical volume on the specified
disk (/dev/sdX), preparing it for use in LVM.
2. Creating Volume Group:
Command: sudo vgcreate vg_name /dev/sdX1 /dev/sdX2
Explanation: This command creates a volume group named vg_name
using the specified physical volumes (/dev/sdX1, /dev/sdX2).
3. Creating Logical Volume:
Command: sudo lvcreate -L 10G -n lv_name vg_name
Explanation: This command creates a logical volume named lv_name
with a size of 10 gigabytes within the volume group vg_name.
Extending Volume Group (VG) and Logical Volume (LV):
1. Adding Physical Volume to Volume Group:
Command: sudo vgextend vg_name /dev/sdX3
Explanation: This command adds a new physical volume (/dev/sdX3) to
the existing volume group (vg_name), increasing the available storage.
2. Extending Logical Volume:
Command: sudo lvextend -L +5G /dev/vg_name/lv_name
Explanation: This command extends the size of the logical volume
(lv_name) within the volume group (vg_name) by 5 gigabytes.
3. Resizing Filesystem on Logical Volume:
Command: sudo resize2fs /dev/vg_name/lv_name
Explanation: After extending the logical volume, the filesystem on it
needs to be resized to make use of the additional space.
Mirroring Logical Volume (LV):
1. Creating Mirrored Logical Volume:
Command: sudo lvcreate --type mirror --mirrors 1 --size 1G -n
mirror_lv_name vg_name
Explanation: This command creates a mirrored logical volume named
mirror_lv_name with a size of 1 gigabyte and a single mirror within the
volume group vg_name.
2. Adding Mirror to Existing Logical Volume:
Command: sudo lvconvert --type mirror --mirrors 2
vg_name/lv_name /dev/sdY
Explanation: This command adds a mirror to an existing logical volume
(lv_name) within the volume group (vg_name) using a new physical
volume (/dev/sdY).
Removing Logical Volume (LV), Physical Volume (PV), and Volume Group
(VG):
1. Removing Logical Volume:
Command: sudo lvremove /dev/vg_name/lv_name
Explanation: This command removes the specified logical volume
(lv_name) within the volume group (vg_name).
2. Removing Physical Volume from Volume Group:
Command: sudo vgreduce vg_name /dev/sdX
Explanation: This command removes a physical volume (/dev/sdX) from
the volume group (vg_name).
3. Removing Volume Group:
Command: sudo vgremove vg_name
Explanation: This command removes the entire volume group
(vg_name), including all associated logical volumes and physical volumes.
Migrating Data from One Storage Device to Another:
1. Adding a New Physical Volume:
Command: sudo pvcreate /dev/sdY
Explanation: Prepares a new disk (/dev/sdY) for use in LVM.
2. Moving Logical Volume to the New Disk:
Command: sudo pvmove /dev/sdX /dev/sdY
Explanation: Moves data from the old disk (/dev/sdX) to the new disk
(/dev/sdY).
3. Removing the Old Physical Volume:
Commands:
$ sudo vgreduce vg_name /dev/sdX sudo pvremove /dev/sdX
Explanation: Removes the old physical volume (/dev/sdX) from the
volume group and LVM.
Logical Volume Manager (LVM)
Flexibility:
LVM provides a flexible and dynamic approach to managing
storage, allowing for easy resizing and reconfiguration.
Abstraction:
It introduces layers of abstraction, making storage management
more organized and efficient.
Mirroring:
LVM supports mirroring for enhanced data reliability and fault
tolerance.
Migration:
LVM facilitates the migration of data between storage devices,
making hardware upgrades or replacements smoother.
Careful Management:
While powerful, careful management is essential to prevent data loss
or misconfigurations.
Monitoring:
Regular monitoring of storage usage and health is crucial for
maintaining a reliable and efficient storage infrastructure.
Module – 7
User Management
Managing Users and Groups
Linux is a multi-user operating system hence it is essential to know user
and group management
Managing Users
User account information is stored in the /etc/passwd file. This
information includes the account name, home directory location, and default
shell, among other values. Linux sysadmins should be able to recognize these
fields.
User Management:
1. Creating a User:
sudo adduser username
This command adds a new user with the specified username.
2. Changing User Password:
sudo passwd username
This command allows changing the password for the specified user.
3. Modifying User Information:
sudo usermod -c "New Comment" username
This command modifies the comment field for the user.
4. Granting sudo Privileges:
sudo usermod -aG sudo username
Adds the user to the sudo group, granting administrative privileges.
5. Deleting a User:
sudo deluser username
Removes the specified user account.
3. Group Management:
1. Creating a Group:
sudo addgroup groupname
This command creates a new group with the specified name.
2. Adding Users to a Group:
sudo usermod -aG groupname username
Adds a user to the specified group.
3. Changing the Primary Group of a User:
sudo usermod -g groupname username
Changes the primary group of the user.
4. Deleting a Group:
sudo delgroup groupname
Removes the specified group.
5. Viewing User and Group Information:
1. Listing Users:
cat /etc/passwd
Displays a list of all users on the system.
2. Listing Groups:
cat /etc/group
Displays a list of all groups on the system.
3. Viewing User Information:
id username
Displays detailed information about a specific user, including UID, GID,
and group memberships.
4. Viewing Group Memberships:
grep groupname /etc/group
Lists the users who are members of a specific group.
6. Managing User and Group Permissions:
1. Changing File Ownership:
sudo chown username:groupname filename
Changes the owner and group of a file.
2. Changing File Permissions:
chmod permissions filename
Changes the permissions of a file.
3. Access Control with sudo:
Users in the sudo group can execute commands with elevated privileges
using sudo.
7. User Environment Configuration:
1. User Profile Files:
The ~/.bashrc and ~/.bash_profile files contain user-specific
configurations.
2. User Home Directory:
Each user has a home directory (/home/username) where personal files
and configurations are stored.
3. Login Shells:
The default shell for a user is specified in /etc/passwd and can be
changed using the chsh command.
8. Summary and Best Practices:
Security Best Practices:
Limit the use of the root account and grant sudo privileges to
trusted users.
Regularly audit and review user and group memberships.
Organization:
Use groups to organize users with similar roles or permissions.
Assign users to groups based on their responsibilities.
Regular Maintenance:
Periodically review and update user accounts and group
memberships.
Remove or disable unused accounts for security.
Documentation:
Maintain documentation for user and group configurations,
especially for privileged users and groups.
Permissions:
Use appropriate file permissions and ownership to control access to
files and directories.
Module – 8
Networking
TCP/IP Networking for Linux System Administrators
What is a Network?
In computing, a network is a collection of two or more computers that can
communicate. In order for networking to facilitate communication between
devices, the machines on the network must be able to find each other.
The systems responsible for making this possible are TCP and IP.
Transmission Control Protocol (TCP)
Communication requires a means of transport for messages between
them, and computers communicate using digital signals carried over Ethernet
cables or radio waves or microwaves.
The specifications for this are formally defined as the TCP protocol.
Internet protocol (IP)
Computers on a network identify themselves and each other with IP
addresses, such as [Link] or [Link]. These are also generally mapped to
hostnames, such as laptop and desktop or dark star or penguin or whatever name
you give each machine.
The specifications for this are formally defined as the IP protocol.
OSI Model
TCP is short for Transmission Control Protocol. TCP/IP is shorthand for
describing the Internet Protocol Suite, which contains multiple networking
protocols. You’re familiar with the Open Systems Interconnection (OSI) model,
which categorizes networking into seven layers:
7. Application layer
6. Presentation layer
5. Session layer
4. Transport layer
3. Network layer
2. Data link layer
1. Physical layer
The application layer includes the network protocols you use every day:
SSH, TLS/SSL, HTTP, IMAP, SMTP, DNS, DHCP, streaming media protocols,
and tons more.
TCP operates in the transport layer, along with its friend UDP, the User
Datagram Protocol. TCP is more complex; it performs error-checking, and it tries
very hard to deliver your packets. There is a lot of back-and-forth communication
with TCP as it transmits and verifies transmission, and when packets get lost it
resends them. UDP is simpler and has less overhead. It sends out datagrams once,
and UDP neither knows nor cares if they reach their destination.
TCP is for ensuring that data is transferred completely and in order. If a
file transfers with even one byte missing it’s no good. UDP is good for
lightweight stateless transfers such NTP and DNS queries, and is efficient for
streaming media. If your music or video has a blip or two it doesn’t render the
whole stream unusable.
The physical layer refers to your networking hardware: Ethernet and wi-fi
interfaces, cabling, switches, whatever gadgets it takes to move your bits and
the electricity to operate them.
Ports and Sockets
Linux admins and users have to know about ports and sockets. A network
socket is the combination of an IP address and port number. Remember back in
the early days of Ubuntu, when the default installation did not include a
firewall? No ports were open in the default installation, so there were no entry
points for an attacker. “Opening a port” means starting a service, such as an
HTTP, IMAP, or SSH server. Then the service opens a listening port to wait for
incoming connections. “Opening a port” isn’t quite accurate because it’s really
referring to a socket. You can see these with the netstat command. This example
displays only listening sockets and the names of their services:
$ sudo netstat -plnt
Output:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program
name
tcp 0 0 [Link]:3306 [Link]:* LISTEN 1583/mysqld
tcp 0 0 [Link]:5901 [Link]:* LISTEN 13951/qemu-system-x
tcp 0 0 [Link]:53 [Link]:* LISTEN 2101/dnsmasq
tcp 0 0 [Link]:80 [Link]:* LISTEN 2001/apache2
tcp 0 0 [Link]:443 [Link]:* LISTEN 2013/apache2
tcp 0 0 [Link]:22 [Link]:* LISTEN 1200/sshd
tcp6 0 0 :::80 :::* LISTEN 2057/apache2
tcp6 0 0 :::22 :::* LISTEN 1200/sshd
tcp6 0 0 :::443 :::* LISTEN 2057/apache2
Domain Name System (DNS) and Hostnames
Domain Name System
Domain Name System (DNS) is a hostname for IP address translation
service. DNS is a distributed database implemented in a hierarchy of name
servers. It is an application layer protocol for message exchange between clients
and servers. It is required for the functioning of the Internet.
What is the Need of DNS?
Every host is identified by the IP address but remembering numbers is very
difficult for people also the IP addresses are not static therefore a mapping is
required to change the domain name to the IP address. So DNS is used to convert
the domain name of the websites to their numerical IP address.
Hostname
hostname command in Linux is used to obtain the DNS (Domain Name System)
name and set the system’s hostname or NIS (Network Information System)
domain name. A hostname is a name given to a computer and attached to the
network. Its main purpose is to uniquely identify over a network.
Example: We obtain the system hostname by just typing the hostname without
any attributes
Options Description Syntax
-a This option is used to get the alias name of the hostname -a
host system (if any). It will return an empty
line if no alias name is set. This option
enumerates all configured addresses on all
network interfaces.
-A This option is used to get all FQDNs (Fully hostname -A
Qualified Domain Name) of the host system.
It enumerates all configured addresses on all
network interfaces. An output may display the
same entries repetitively.
-b Used to always set a hostname. Default name hostname -b
is used if none specified.
-d This option is used to get the Domain if local hostname -d
domains are set. It will not return anything
(not even a blank line) if no local domain is
set.
-f This option is used to get the Fully Qualified hostname -f
Domain Name (FQDN). It contains short
hostname and DNS domain name.
Dynamic Host Configuration Protocol (DHCP) and Dynamic and Static
Addressing
DHCP stands for Dynamic Host Configuration Protocol. It is the critical
feature on which the users of an enterprise network communicate. DHCP helps
enterprises to smoothly manage the allocation of IP addresses to the end-user
clients’ devices such as desktops, laptops, cellphones, etc. is an application layer
protocol that is used to provide:
Subnet Mask (Option 1 - e.g., [Link])
Router Address (Option 3 - e.g., [Link])
DNS Address (Option 6 - e.g., [Link])
Vendor Class Identifier (Option 43 - e.g.,
'unifi' = [Link] ##where unifi = controller)
Why Use DHCP?
DHCP helps in managing the entire process automatically and centrally.
DHCP helps in maintaining a unique IP Address for a host using the server. DHCP
servers maintain information on TCP/IP configuration and provide configuration
of address to DHCP-enabled clients in the form of a lease offer.
Components of DHCP
The main components of DHCP include:
DHCP Server: DHCP Server is basically a server that holds IP Addresses
and other information related to configuration.
DHCP Client: It is basically a device that receives configuration
information from the server. It can be a mobile, laptop, computer, or any
other electronic device that requires a connection.
DHCP Relay: DHCP relays basically work as a communication channel
between DHCP Client and Server.
IP Address Pool: It is the pool or container of IP Addresses possessed by
the DHCP Server. It has a range of addresses that can be allocated to
devices.
Subnets: Subnets are smaller portions of the IP network partitioned to keep
networks under control.
Lease: It is simply the time that how long the information received from
the server is valid, in case of expiration of the lease, the tenant must have
to re-assign the lease.
DNS Servers: DHCP servers can also provide DNS (Domain Name
System) server information to DHCP clients, allowing them to resolve
domain names to IP addresses.
Default Gateway: DHCP servers can also provide information about the
default gateway, which is the device that packets are sent to when the
destination is outside the local network.
Options: DHCP servers can provide additional configuration options to
clients, such as the subnet mask, domain name, and time server
information.
Renewal: DHCP clients can request to renew their lease before it expires
to ensure that they continue to have a valid IP address and configuration
information.
Failover: DHCP servers can be configured for failover, where two servers
work together to provide redundancy and ensure that clients can always
obtain an IP address and configuration information, even if one server goes
down.
Dynamic Updates: DHCP servers can also be configured to dynamically
update DNS records with the IP address of DHCP clients, allowing for
easier management of network resources.
Audit Logging: DHCP servers can keep audit logs of all DHCP
transactions, providing administrators with visibility into which devices
are using which IP addresses and when leases are being assigned or
renewed.
Network Troubleshooting
Computers are often connected to each other on a network. They send
requests to each other in the form of packets that travel from the host to the
destination. Linux provides various commands from network configuration and
troubleshooting.
Here is the list of important commands:
1. ping: The ping command is used to ensure that a computer can
communicate with a specified device over the network
Example $ping [Link]
2. nsloopup: nslookup command queries the DNS in order to fetch the IP
address or the domain name from DNS records.
3. traceroute: the traceroute command is used to determine the path along
which a packet travels.
Example $traceroute [Link]
4. host: host command is used to find a domain name associated with the IP
address
5. netstat: netstat (Network Statistics) is the command that is used to display
routing tables, connection information, the status of ports, etc.
6. ifconfig: (Interface Configuration) is a utility in an operating system that
is used to set or display the IP address and netmask of a network interface.
7. dig: It is called domain information groper; it is a tool used to find query
information related to domain name and troubleshoot DNS issue in Linux.
This tool can provide various types of DNS records, such as CNAME, MX
records and records etc.
8. route: This tool helps us display and manipulate the routing table in Linux.
Information contained by this is about how network packets should be
routed through a network.
Module – 10
Special permissions
Special permissions in Linux
In Linux, there are three special permission modes: SUID, GUID, and
sticky bit 123. These modes are used to grant additional permissions to users and
groups.
SUID: When the SUID bit is set on an executable file, the file will be
executed with the same permissions as the owner of the executable file.
This is useful when a user needs to execute a script that requires root
privileges. The SUID bit can be set using the chmod command.
GUID: When the GUID bit is set on an executable file, the file will be
executed with the same permissions as the group owner of the executable
file. This is useful when a group of users needs to execute a script that
requires group privileges. The GUID bit can be set using the chmod
command.
Sticky bit: When the sticky bit is set on a directory, only the owner of the
file can delete or rename the files within that directory. This is useful when
multiple users need to write to the same directory, but you want to restrict
their ability to delete or rename each other’s files. The sticky bit can be set
using the chmod command.
Module – 11
Shell Scripting
Shell Scripting
Introduction to Shell Scripting:
Shell is an interface of the operating system. It accepts commands from
users and interprets them to the operating system. If you want to run a bunch of
commands together, you can do so by creating a shell script. Shell scripts are
very useful if you need to do a task routinely, like taking a backup. You can list
those commands and execute them all with just a single script.
Scripting: Writing a set of commands for the shell to execute.
Interpreter: Executes scripts line by line.
Basic Script Structure:
Shebang: Indicates the path of the shell to be used (e.g., #!/bin/bash).
Comments: # is used for comments.
Execution Permissions: Make the script executable using chmod +x [Link].
Variables:
Declare variables: variable_name=value.
Access variables: $variable_name.
Use quotes for strings: name="John".
Input and Output:
Read from User: read variable_name.
Print to Console: echo "Hello, $name!".
Formatted Output: printf "Name: %s\n" "$name".
Conditional Statements: Conditional statements are used to execute a block of
code only when certain conditions are met. Shell scripts support the use of
conditional statements. We use comparison operators to check the conditions.
Let’s see a few conditional statements.
if-else-fi:
if [ condition ]; then
# commands
elif [ another_condition ]; then
# commands
else
# commands
fi
Comparison Operators: -eq, -ne, -lt, -le, -gt, -ge.
Operator Description
-eq is equal to
-ne is not equal to
-gt is greater than
-ge is greater than or equal to
-lt is less than
-le is less than or equal to
Loops:
for:
for item in list; do
# commands
done
while:
while [ condition ]; do
# commands
done
until:
until [ condition ]; do
# commands
done
Functions:
Define:
function_name() {
# commands
}
Call: function_name.
Arrays:
Declaration: my_array=("item1" "item2" "item3").
Access: ${my_array[index]}.
Length: ${#my_array[@]}.
File Operations:
Read File: while IFS= read -r line; do echo "$line"; done < filename.
Write to File: echo "content" > filename.
Append to File: echo "content" >> filename.
Command-Line Arguments:
Positional Parameters: $1, $2, ...
Number of Arguments: $#.
All Arguments: $@.
Error Handling:
Exit Status: $? (0 for success, non-zero for failure).
Exit Command: exit [status].
Advanced Topics:
Regular Expressions: Used in pattern matching and text manipulation.
Pipes and Redirection: |, >, <.
Job Control: bg, fg, &.
Best Practices:
Use meaningful variable names.
Indentation for better readability.
Comment complex sections.
Test scripts on different environments.
Examples:
Simple scripts for file manipulation, system monitoring, etc.