0% found this document useful (0 votes)
21 views46 pages

Ug Oss U2

This document provides a comprehensive overview of Linux, covering essential commands, the distinction between kernel mode and user mode, and the structure of the Linux file system. It details various Linux file systems, their characteristics, and the Linux security model focusing on user and group management. The document serves as a guide for both beginners and professionals in navigating and utilizing Linux effectively.

Uploaded by

Vimal Cheyyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views46 pages

Ug Oss U2

This document provides a comprehensive overview of Linux, covering essential commands, the distinction between kernel mode and user mode, and the structure of the Linux file system. It details various Linux file systems, their characteristics, and the Linux security model focusing on user and group management. The document serves as a guide for both beginners and professionals in navigating and utilizing Linux effectively.

Uploaded by

Vimal Cheyyar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

OPEN SOURCE SOFTWARE

UNIT II: LINUX


Introduction: Linux Essential Commands–Kernel Mode and user mode–File system Concept–
Standard Files–The Linux Security Model–Vi Editor–Partitions Creation–Shell Introduction–
String Processing–Investigation and Managing Processes–Network Clients–Installing Application

Linux Essential Commands


Linux is a free and open-source software that operates on its own operating system. The
term ‘Linux’ stands for GNU + Linux. Initially developed by Linus Torvalds, it was created
alongside the source code of Unix. While Linux is extensively utilized for various purposes, its
applications are well-known to many.

Use of Linux Commands


Linux commands are a type of Unix command or shell procedure. They are the basic
tools used to interact with Linux on an individual level. Linux commands are used to perform a
variety of tasks, including displaying information about files and directories.
Linux operating system is used on servers, desktops, and maybe even your smartphone. It
has a lot of command line tools that can be used for virtually everything on the system.
All users should be familiar with most of these commands as they are required for most
operating system tasks and computer programming.
The Linux command is a utility of the Linux operating system. All basic and advanced
tasks can be done by executing commands. The commands are executed on the Linux terminal.
The terminal is a command-line interface to interact with the system, which is similar to the
command prompt in the Windows OS. Commands in Linux are case-sensitive.
Linux provides a powerful command-line interface compared to other operating systems
such as Windows and MacOS. We can do basic work and advanced work through its terminal. We
can do some basic tasks such as creating a file, deleting a file, moving a file, and more. In addition,
we can also perform advanced tasks such as administrative tasks (including package installation,
user management), networking tasks (ssh connection), security tasks, and many more.
Linux terminal is a user-friendly terminal as it provides various support options. To open the
Linux terminal, press "CTRL + ALT + T" keys together, and execute a command by pressing the
'ENTER' key.
In this topic, we will discuss the top 50 most frequently used Linux commands with their
examples. These commands are very useful for a beginner and professional both. We have
divided these commands into following sections so that you can easily identify their usage:
Here we have put all Basic Linux Commands that every Linux user (as a beginner)
should know. These are not all that you should know, but these are the basic and most commonly
used commands.
Basic Linux Terminal Commands
S.No. Linux Commands Functions
Displays information about files
1 Is
in the current directory.
Displays the current working
2 pwd
directory.
Basic Linux Terminal Commands
3 mkdir Creates a directory.
To navigate between different
4 cd
folders.
Removes empty directories
5 rmdir
from the directory lists.
Moves files from one directory
6 cp
to another.
7 mv Rename and Replace the files
8 rm Delete files
Command to get basic
9 uname
information about the OS
10 locate Find a file in the database.
11 touch Create empty files
12 ln Create shortcuts to other files
13 cat Display file contents on terminal
14 clear Clear terminal
Display the processes in
15 ps
terminal
Access manual for all Linux
16 man
commands
Search for a specific string in an
17 grep
output
Display active processes on the
18 echo
terminal
download files from the
19 wget
internet.
Create or update passwords for
20 whoami
existing users
21 sort sort the file content
22 cal View Calendar in terminal
View the exact location of any
23 whereis command typed after this
command
Check the details of the file
24 df
system
Check the lines, word count, and
25 wc characters in a file using
different options
Kernel Mode and user mode
User Mode: When a Program is booted up on an Operating system let’s say Windows, then it
launches the program in user mode. When a user-mode program requests to run, a process and
virtual address space (address space for that process) are created for it by Windows. User-mode
programs are less privileged than user-mode applications and are not allowed to access the
system resources directly. For instance, if an application under user mode wants to access
system resources, it will have to first go through the Operating system kernel by using syscalls.

Kernel Mode: The kernel is the core program on which all the other operating system
components rely, it is used to access the hardware components and schedule which processes
should run on a computer system and when, and it also manages the application software and
hardware interaction. Hence it is the most privileged program, unlike other programs, it can
directly interact with the hardware. When programs running under user mode need hardware
access for example webcam, then first it has to go through the kernel by using a syscall, and to
carry out these requests the CPU switches from user mode to kernel mode at the time of
execution. After finally completing the execution of the process the CPU again switches back to
the user mode.

Difference Between Kernel mode and User mode:


Criteria Kernel Mode User Mode
In user mode, the application
In kernel mode, the program program do not have direct
Access to Resources has direct and unrestricted access to system resources. In
access to system resources. order to access the resources, a
system call must be made.
In Kernel mode, the whole
In user mode, a single process
Interruptions operating system might go
fails if an interrupt occurs.
down if an interrupt occurs
Kernel mode is also known as User mode is also known as the
Modes the master mode, privileged unprivileged mode, restricted
mode, or system mode. mode, or slave mode.
In kernel mode, all processes
In user mode, all processes get
Virtual address space share a single virtual address
separate virtual address space.
space.
In kernel mode, the applications While in user mode the
Level of privilege have more privileges as applications have fewer
compared to user mode. privileges.
As kernel mode can access both
While user mode needs to
the user programs as well as the
Restrictions access kernel programs as it
kernel programs there are no
cannot directly access them.
restrictions.
The mode bit of kernel-mode is While; the mode bit of user-
Mode bit value
0. mode is 1.
Criteria Kernel Mode User Mode
It can only make references to
It is capable of referencing both
Memory References memory allocated for user
memory areas.
mode.
A system crash in kernel mode
In user mode, a system crash
is severe and makes things
System Crash can be recovered by simply
more complicated.
resuming the session.

Only essential functionality is User programs can access and


Access permitted to operate in this execute in this mode for a given
mode. system.
The user mode is a standard
The kernel mode can refer to and typical viewing mode,
any memory block in the which implies that information
system and can also direct the cannot be executed on its own
Functionality
CPU for the execution of an or reference any memory block;
instruction, making it a very it needs an Application Protocol
potent and significant mode. Interface (API) to achieve these
things.
File system Concept
Operating systems, the software that powers your computer, rely on a crucial element
known as the file system. Think of it as a virtual organizational tool that manages, stores, and
retrieves your data efficiently. In the Linux world, a diverse range of file systems has emerged,
each crafted to address specific needs and preferences. This article aims to simplify the
intricacies of Linux file systems, guiding beginners through their layers, characteristics, and
implementations. By shedding light on these nuances, we empower users to make informed
choices in navigating the dynamic landscape of Linux operating systems.

What is the Linux File System


The Linux file system is a multifaceted structure comprised of three essential layers. At its
foundation, the Logical File System serves as the interface between user applications and the file
system, managing operations like opening, reading, and closing files. Above this, the Virtual File
System facilitates the concurrent operation of multiple physical file systems, providing a
standardized interface for compatibility.
Finally, the Physical File System is responsible for the tangible management and storage
of physical memory blocks on the disk, ensuring efficient data allocation and retrieval. Together,
these layers form a cohesive architecture, orchestrating the organized and efficient handling of
data in the Linux operating system.
In this article, we will be focusing on the file system for hard disks on a Linux OS and
discuss which type of file system is suitable. The architecture of a file system comprises three
layers mentioned below.
Linux File System Structure
A file system mainly consists of 3 layers. From top to bottom:
1. Logical File System:
The Logical File System acts as the interface between the user applications and the file system
itself. It facilitates essential operations such as opening, reading, and closing files. Essentially, it
serves as the user-friendly front-end, ensuring that applications can interact with the file system
in a way that aligns with user expectations.
2. Virtual File System:
The Virtual File System (VFS) is a crucial layer that enables the concurrent operation of multiple
instances of physical file systems. It provides a standardized interface, allowing different file
systems to coexist and operate simultaneously. This layer abstracts the underlying complexities,
ensuring compatibility and cohesion between various file system implementations.
3. Physical File System:
The Physical File System is responsible for the tangible management and storage of physical
memory blocks on the disk. It handles the low-level details of storing and retrieving data,
interacting directly with the hardware components. This layer ensures the efficient allocation
and utilization of physical storage resources, contributing to the overall performance and
reliability of the file system.

Characteristics of a File System


 Space Management: how the data is stored on a storage device. Pertaining to the
memory blocks and fragmentation practices applied in it.
 Filename: a file system may have certain restrictions to file names such as the name
length, the use of special characters, and case sensitive-ness.
 Directory: the directories/folders may store files in a linear or hierarchical manner while
maintaining an index table of all the files contained in that directory or subdirectory.
 Metadata: for each file stored, the file system stores various information about that file’s
existence such as its data length, its access permissions, device type, modified date-time,
and other attributes. This is called metadata.
 Utilities: file systems provide features for initializing, deleting, renaming, moving,
copying, backup, recovery, and control access of files and folders.
 Design: due to their implementations, file systems have limitations on the amount of data
they can store.

Some important terms:


1) Journaling:
Journaling file systems keep a log called the journal, that keeps track of the changes made to a file
but not yet permanently committed to the disk so that in case of a system failure the lost changes
can be brought back.
2) Versioning:
Versioning file systems store previously saved versions of a file, i.e., the copies of a file are stored
based on previous commits to the disk in a minutely or hourly manner to create a backup.
3) Inode:
The index node is the representation of any file or directory based on the parameters – size,
permission, ownership, and location of the file and directory.
Now, we come to part where we discuss the various implementations of the file system in Linux
for disk storage devices.

Linux File Systems:


Note: Cluster and distributed file systems will not be included for simplicity.

1) ext (Extended File System):


Implemented in 1992, it is the first file system specifically designed for Linux. It is the first
member of the ext family of file systems.
2) ext2:
The second ext was developed in 1993. It is a non-journaling file system that is preferred to be
used with flash drives and SSDs. It solved the problems of separate timestamp for access, inode
modification and data modification. Due to not being journaled, it is slow to load at boot time.
3) Xiafs:
Also developed in 1993, this file system was less powerful and functional than ext2 and is no
longer in use anywhere.
4) ext3:
The third ext developed in 1999 is a journaling file system. It is reliable and unlike ext2, it
prevents long delays at system boot if the file system is in an inconsistent state after an unclean
shutdown. Other factors that make it better and different than ext2 are online file system growth
and HTree indexing for large directories.
5) JFS (Journaled File System):
First created by IBM in 1990, the original JFS was taken to open source to be implemented for
Linux in 1999. JFS performs well under different kinds of load but is not commonly used
anymore due to the release of ext4 in 2006 which gives better performance.
6) ReiserFS:
It is a journal file system developed in 2001. Despite its earlier issues, it has tail packing as a
scheme to reduce internal fragmentation. It uses a B+ Tree that gives less than linear time in
directory lookups and updates. It was the default file system in SUSE Linux till version 6.4, until
switching to ext3 in 2006 for version 10.2.
7) XFS:
XFS is a 64-bit journaling file system and was ported to Linux in 2001. It now acts as the default
file system for many Linux distributions. It provides features like snapshots, online
defragmentation, sparse files, variable block sizes, and excellent capacity. It also excels at parallel
I/O operations.
8) SquashFS:
Developed in 2002, this file system is read-only and is used only with embedded systems where
low overhead is needed.
9) Reiser4:
It is an incremental model to ReiserFS. It was developed in 2004. However, it is not widely
adapted or supported on many Linux distributions.
10) ext4:
The fourth ext developed in 2006, is a journaling file system. It has backward compatibility with
ext3 and ext2 and it provides several other features, some of which are persistent pre-allocation,
unlimited number of subdirectories, metadata checksumming and large file size. ext4 is the
default file system for many Linux distributions and also has compatibility with Windows and
Macintosh.
11) btrfs (Better/Butter/B-tree FS):
It was developed in 2007. It provides many features such as snapshotting, drive pooling, data
scrubbing, self-healing and online defragmentation. It is the default file system for Fedora
Workstation.
12) bcachefs:
This is a copy-on-write file system that was first announced in 2015 with the goal of performing
better than btrfs and ext4. Its features include full filesystem encryption, native compression,
snapshots, and 64-bit check summing.
13) Others:
Linux also has support for file systems of operating systems such as NTFS and exFAT, but these
do not support standard Unix permission settings. They are mostly used for interoperability with
other operating systems.

The Linux Security Model


Linux for the Oracle DBA - Users and Groups
Management of users and groups is central to security on Linux. The main concerns are
first to assure that a user has rights to access the system and second, to determine what they can
do with a given file, process or system resource.
This chapter will be an examination of the overall security model, then how to set up and
maintain user security on the system will be covered.

The Linux Security Model


Based on the UNIX model, all files, directories, running processes and system resources on
the Linux system are associated with a userand group. The security can be set independently for
the user, or owner, and group.
A third set of permissions is maintained for everyone on the system who is neither the
owner nor in the group associated with a resource. This is commonly referred to as the
permissions for 'other' users.
These security levels, user, group and other, each have a set of permissions associated
with them. Typical permissions are read, writeand executeand depending on the type of
resource, these will determine what a given user is allowed to do with the resource.
In this example, the ls -l command is used to show the ownership and permissions of the file
status.sh.
The permissions portion of the output breaks down further to indicate the level of access
for the owner, group and other users. In this example, the owner has read, write and execute, the
group has read and execute and other has only read.
Managing these permissions will be delved into in Chapter 12 on file management.
Each user on the system is associated with a primary group but can also belong to additional
groups, adding to the flexibility of Linux security. A user has access rights granted to his or her
primary group as well as any additional groups to which the user belongs.
Though we use user and group names are used to interact with the system, these
identifiers are tracked within the system by ID numbers. The user ID (UID)and group ID
(GID) numbers are associated with user and group names through the /etc/passwd and
/etc/group files, respectively.
With this understanding of why users and groups are so important, now take a look at
how to create, modify and remove them.

Vi Editor
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.
The vi editor has two modes:
 Command Mode: In command mode, actions are taken on the file. The vi editor starts in
command mode. Here, the typed words will act as commands in vi editor. To pass a
command, you need to be in command mode.
 Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will
take you to the command mode from insert mode.
By default, the vi editor starts in command mode. To enter text, you have to be in insert mode,
just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear on the
screen but you'll be in insert mode. Now you can type anything.

To exit from insert mode press Esc key, you'll be directed to command mode.
If you are not sure which mode you are in, press Esc key twice and you'll be in command mode.

Using vi
The vi editor tool is an interactive tool as it displays changes made in the file on the screen
while you edit the file.
In vi editor you can insert, edit or remove a word as cursor moves throughout the file.
Commands are specified for each function like to delete it's x or dd.
The vi editor is case-sensitive. For example, p allows you to paste after the current line
while P allows you to paste before the current line.

vi syntax:
vi <fileName>

In the terminal when you'll type vi command with a file name, the terminal will get clear
and content of the file will be displayed. If there is no such file, then a new file will be created and
once completed file will be saved with the mentioned file name.

Linux vi example
Let's understand vi through an example:
To start vi open your terminal and type vi command followed by file name. If your file is in some
other directory, you can specify the file path. And if in case, your file doesn't exist, it will create a
new file with the specified name at the given location.

Example:
vi /home/sssit/Downloads/file.txt

Look at the above snapshot, we are creating a new file file.txt (as this file doesn't exist) and have
entered the full path for the directory Downloads.
Command mode
This is what you'll see when you'll press enter after the above command. If you'll start typing,
nothing will appear as you are in command mode. By default vi opens in command mode.
Look at the above snapshot, it is blank as it is a new file. To start typing, you have to move to the
insert mode. At the end of the terminal window, directory name and file name are displayed.

Insert mode
To move to the insert mode press i. Although, there are other commands also to move to insert
mode which we'll study in next page.

Look at the above snapshot, after pressing i we have entered into insert mode. Now we can write
anything. To move to the next line press enter.
Once you have done with your typing, press esc key to return to the command mode.

To save and quit


You can save and quit vi editor from command mode. Before writing save or quit command you
have to press colon (:). Colon allows you to give instructions to vi.
exit vi table:
Commands Action
:wq Save and quit
:w Save
:q Quit
:w fname Save as fname
ZZ Save and quit
:q! Quit discarding changes made
:w! Save (and write to non-writable file)

To exit from vi, first ensure that you are in command mode. Now, type :wq and press enter. It will
save and quit vi.

Type :wq to save and exit the file.

Look at the above snapshot, command :wq will save and quit the vi editor. When you'll type it in
command mode, it will automatically come at bottom left corner.
If you want to quit without saving the file, use :q. This command will only work when you have
not made any changes in the file.

Look at the above snapshot, this file is modified and hence on typing :q it displays this message
at bottom left corner.
The above file can be saved with the command :!q. It discards the changes made in the file and
save it.
Look at the above snapshot, we have typed :!q, it will save our file by discarding the changes
made.

Vi Commands
Linux vi editor is different from other editors. You have to use different keys to use different
functions. Although, it's quite easy and interesting to use vi editor.
The vi editor commands are case sensitive.
Have a look at the vi commands in the following table.

To switch from command to insert mode:


Command Action
i Start typing before the current character
I Start typing at the start of current line
a Start typing after the current character
A Start typing at the end of current line
o Start typing on a new line after the current line
O Start typing on a new line before the current line

To move around a file:


Commands Action
j To move down
k To move up
h To move left
l To move right
To jump lines:
Commands Action
G Will direct you at the last line of the file
`` Will direct you to your last position in the file
To delete:
Commands Action
x Delete the current character
X Delete the character before the cursor
r Replace the current character
xp Switch two characters
dd Delete the current line
D Delete the current line from current character to the end of the line
dG delete from the current line to the end of the file

To repeat and undo:


Commands Action
u Undo the last command
. Repeat the last command
Command to cut, copy and paste:
Commands Action
dd Delete a line
yy (yank yank) copy a line
p Paste after the current line
P Paste before the current line
Command to cut, copy and paste in blocks:
Commands Action
<n>dd Delete the specified n number of lines
<n>yy Copy the specified n number of lines
Start and end of line:
Commands Action
θ Bring at the start of the current line
^ Bring at the start of the current line
$ Bring at the end of the current line
dθ Delete till start of a line
d$ Delete till end of a line
Joining lines:
Commands Action
J Join two lines
yyp Repeat the current line
ddp Swap two lines
Move forward or backward:
Commands Action
w Move one word forward
b Move one word backward
<n>w Move specified number of words forward
dw Delete one word
yw Copy one word
<n>dw Delete specified number of words
Search a string:
Commands Action
/string Forward search for given string
?string Backward search for given string
/^string Forward search string at beginning of a line
/string$ Forward search string at end of a line
n Go to next occurrence of searched string
/\<he\> Search for the word he (and not for there, here, etc.)
/pl[abc]ce Search for place, plbce, and plcce

Partitions Creation
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.

Why do we need it?


 To upgrade Hard Disk (to incorporate a new Hard Disk into the system)
 Dual Booting (Multiple Operating Systems on the same system)
 Efficient disk management
 Ensure backup and security.
 Work with different File Systems using the same system.
How to Partition Disks in Linux?
(For the purpose of demonstration Cent OS 7 and VMware are used in the screenshots of
the article.)
In order to successfully partition a disk and to make it useful, we need to ensure that we have
completed the below four steps, regardless of the Operating system and Hardware of the system.
1. Attach the disk to the proper port.
2. Create partitions in the disk.
3. Create a file system on the partition.
4. Mounting the file systems

Attaching Disk:
This step points to physically connecting the disk to the system. The below screenshot specifies
the system configuration of my CentOS 7.
It is clearly specified that the system has only 20 GB of Hard Disk. After installing the necessary
software and downloading some movies, I found that the system Hard Disk is almost completely
used up. Now I will have two options in front of me, either buy a new system with a higher Hard
Disk capacity or upgrade the existing one. Most of you will go for the second option, just like me,
unless the system is very old. Let us upgrade the system with an additional 20 GB of Hard Disk.
In order to attach the new virtual Hard Disk in VMware, click on the “Edit virtual machine
settings” link and choose the Add option on the next window.

Choose the Hard Disk option and move forward.


Choose the recommended disk type. (SCSI is recommended here since the Operating System that
I am using is CentOS)

Complete the attachment of the disk by specifying the required size of Hard Disk needed and
choosing the recommended selections.
The final configuration shows that our system has a new Hard Disk of 20 GB size. The newly
added Hard disk is of no use to our system unless we complete the remaining steps.

Create Partitions in the Disk


Power on the system and log in to the system. Ensure that you are logged in as root (or any user
of the sudo group). Once the system UI comes up, open the Terminal. To view the available
Hard Disks in our system, use the command lsblk or cat /proc/partitions. Both commands will
display the same results, but in different ways.
We can find that the system has two disks -> sda and sdb. sda is our older Hard Disk. We can say
so, as that disk is already partitioned as sda1 and sda2. We can partition the disk using CLI as
well as GUI. We will demonstrate CLI based partitioning in this article.
We found that the Hard Disk that we are going to partition can be found at /dev/sdb. Use the
command.
fdisk /dev/sdb
It will take us to a different console where we can use the partitioning specific commands. We
will be concentrating more on the following commands (or flags).
m -> help
p -> print partition table
n -> create new partition
d -> delete partition
q -> quit without writing
w -> write to disk
While partitioning, we should be aware of certain factors.
 On a disk, we can have a maximum of four partitions.
 The partitions are of two types.
o Primary
o Extended
 Extended partitions can have logical partitions inside it.
 Among the four possible partitions, the possible combinations are.
o All 4 primary partitions
o 3 primary partitions and 1 extended partition
 n
 This will create a new partition. Specify the type of partition using the p for primary and e
for extended.
 p
 This will create a primary partition. The console will prompt for the number to be given
to the partition. We can give any number from 1 to 4. Let me give 1. Then choose the
starting position (cylinder) of partition 1. Press enters to start partitioning from the
beginning of the disk.
 You can specify the size of the partition in two ways, either as the last cylinder number or
by specifying the size directly. If we need partition1 to be of 4 GB size (as a whole
number), use.
 +4G
 Pressing enter will create our 1st partition successfully. Follow the same steps, until we
create 4 partitions on our newly attached disk.
We have created 3 primary partitions and an extended partition in the disk. The extended
partition, as the name suggests, can be further divided into multiple logical partitions. Once 4
partitions are created, no more partitions can be created on the same disk. We can check
whether we have done the partitioning in the right way by printing the partition table using the
p command. If everything is as per the expectation, then write the changes to the disk using the
w command, else use the q command to quit without writing.

Create a File System on the Partition


Computers use particular kinds of file systems to store and organize data on media, like Hard
Disk, CD, DVD, etc. The commonly used Linux file systems are ext2, ext3, ext4, JFS, ReiserFS, XFS,
FAT (usually in Windows OS) and B-treeFS. Inorder to specify the file system to be used in each
partition, we can use the mkfs (make file system) command.
mkfs.ext4 -j /dev/sdb1
What this command does is that it will make the first partition’s file system to be ext4 (format
the partition to ext4). -j flag is used to allow/support journaling. It helps in throwing errors into
the journal, in case of system failure.
mkfs.fat /dev/sdb2
This command is used to format the 2nd partition available in /dev/sdb2 to the FAT file system.
Even though we formatted the disks, it is of no use to us, unless we mount it on a directory.

Mounting the file systems


Let us make three directories, for mounting the first, second, and third partition using the mkdir
(make directory) command.
mkdir /mount1
mkdir /mount2
mkdir /mount3
Mounting the formatted partitions can be achieved using the mount command.
mount /dev/sdb1 /mount1
mount /dev/sdb2 /mount2
mount /dev/sdb3 /mount3
The first part of the command is the keyword mount, followed by the partition and the directory
to which the mounting is to be done. To view the disk details, we can use the df command. h flag
helps to display the output in a human-readable format.
df -h
Unmounting a disk can be achieved by unmount command.
unmount /dev/sdb2
All these mountains are temporary in nature. Once we reboot the system, mounting will be
reverted. To make it permanent, we must edit the File System Table of the Operating System. We
should be highly careful whirling with this file. A small error in this file can cause the system to
be unbootable and can make the entire system to be useless. Let us open the file in any editor (I
will be using the VI editor).
vi /etc/fstab
Add our mounted file systems’ details in the file, in the order.
mounted partition [ space ] directory [ space ] file system type [ space ]
defaults [ space ] 0 [ space ] 0
Save the file and come back to the terminal. Mount the partitions permanently using the mount -
a command
mount -a
We can verify whether our partitions are available for our purposes by visiting the root folder of
the Operating System (since we have mounted the partitions on the mount1, mount2, mount3
directories in the root folder). We can also verify the existence of our newly created partitions
using the lsblk command.
lsblk
We can find our partitions in the table, each of 4 GB size.

Shell Introduction
Linux Shell
The shell can be defined as a command interpreter within an operating system like
Linux/GNU or Unix. It is a program that runs other programs. The shell facilitates every user of
the computer as an interface to the Unix/GNU Linux system. Hence, the user can execute
different tools/utilities or commands with a few input data.
The shell sends the result to the user over the screen when it has completed running a
program which is the common output device. That's why it is known as "command
interpreter".
The shell is not just a command interpreter. Also, the shell is a programming language
with complete constructs of a programming language such as functions, variables, loops,
conditional execution, and many others.
For this reason, GNU/Unix Linux Shell is stronger than the Windows shell.
Broadly, the shell is categorized into two main categories which are explained below:

Graphical Shells
These shells specifies the manipulation of programs that are based on the graphical user
interface (GUI) by permitting for operations like moving, closing, resizing, and opening
windows and switching focus among windows as well. Ubuntu OS or Windows OS could be
examined as a good example that offers a graphical user interface to the user to interact with the
program. Various users don't need for typing in any command for all the actions.
Command-line Shell
Various shells could be accessed with the help of a command-line interface by users. A
unique program known as Command prompt in Windows or Terminal in macOS/Linux is
offered for typing in the human-understandable commands like "ls", "cat", etc and after that, it is
being run. The result is further shown to the user on the terminal.
Working on a command-line shell is a complicated for many beginners due to it is hard to
remember several commands. Command-line shell is very dominant and it permits users for
storing commands in a file and run them together. In this way, a repetitive action could be
automated easily. Usually, these files are known as Shell scripts in macOS/Linux systems and
batch files in Windows.
There are various types of shells which are discussed as follows:

Bash Shell
In the bash shell, bash means Bourne Again Shell. It is a default shell over several distributions of
Linux today. It is a sh-compatible shell. It could be installed over Windows OS. It facilitates
practical improvements on sh for interactive and programming use which contains:
 Job Control
 Command-line editing
 Shell Aliases and Functions
 Unlimited size command history
 Integer arithmetic in a base from 2-64

Csh/Tcsh Shell
Tcsh is an upgraded C shell. This shell can be used as a shell script command processor and
interactive login shell.
Tcsh shell includes the following characteristics:
 C like syntax
 Filename completion and programmable word
 Command-line editor
 Job control
 Spelling correction

Ksh Shell
Ksh means for Korn shell. It was developed and designed by David G. Korn. Ksh shell is a high-
level, powerful, and complete programming language and it is a reciprocal command language as
well just like various other GNU/Unix Linux shells. The usage and syntax of the C shell are very
same as the C programming language.

Zsh Shell
Zsh shell is developed to be reciprocal and it combines various aspects of other GNU/Unix Linux
shells like ksh, tcsh, and bash. Also, the POSIX shell standard specifications were based on the
Korn shell.
Also, it is a strong scripting language like other available shells. Some of its unique features are
listed as follows:
 Startup files
 Filename generation
 Login/Logout watching
 Concept index
 Closing comments
 Variable index
 Key index
 Function index and various others that we could find out within the man pages.
All these shells do a similar job but take different commands and facilitate distinct built-in
functions.

Fish
Fish stands for "friendly interactive shell". It was produced in 2005. Fish shell was developed
to be fully user-friendly and interactive just like other shells. It contains some good features
which are mentioned below:
 Web-based configuration
 Man page completions
 Auto-suggestions
 Support for term256 terminal automation
 Completely scripted with clean scripts

Shell Prompt
It is known as a command prompt and it is issued via the shell. We can type any command while
the prompt is shown.
Shell reads our input after we click Enter. It illustrates the command we want to be run by seeing
at the initial word of our input. A word can be defined as the character's unbroken set. Tabs and
spaces separate words.
The below is a common data command example that shows the current time and date:
We can also customize our command prompt with the help of PS1 (environment variable).

Shell Scripting
The common concept of the shell script is the command list. A good shell script will contain
comments which are preceded via # simbol.
Shell functions and scripts are interpreted. It means they aren't compiled.
There are also conditional tests like value Y is greater than value Z, loops permitting us to
proceed by massive data amounts, files to store and read data, and variables to store and read
data, and these scripts may contain functions.
The shells are usually interactive which means they receive commands as input through the
users and run them. Although sometimes we routinely wish to run a set of commands, hence, we
have to type within the commands all-time inside the terminal.
A shell script includes syntax similarly to other programming languages. When we have prior
experience along with a programming language such as C/C++, Python, etc. It will be very easy to
begin with it. The shell script combines the below components:
Functions
 Control flow: if, else, then, shell loops, case, etc.
 Shell commands: touch, pwd, echo, ls, cd, etc.
 Shell keywords: break, if, else, etc.

Shell Scripts Need


There are several causes to write these shell scripts:
 For avoiding automation and repetitive work
 Shell scripting is used by system admins for many routine backups
 Including new functionalities to the shell
 System monitoring etc.

Shell Script Advantages


 The syntax and command are exactly similar to those entered directly in a command line.
Thus, the programmers don't have to switch to completely different syntax
 Interactive debugging
 Quick start
 It is much quicker to write the shell scripts etc.

Shell Script Disadvantages


 A single error can modify the command which could be harmful, so prone to very costly
errors.
 Design flaws in the language implementation and syntax.
 Slower execution speed.
 Offer minimal data structure dissimilar other scripting languages.
 Not well suited for complex and large tasks etc.

String Processing
The shell allows some common string operations which can be very useful for script writing.
String Length
# 1234567890123456
STRING="this is a string"
echo ${#STRING} # 16

Index
Find the numerical position in $STRING of any single character in $SUBSTRING that matches.
Note that the 'expr' command is used in this case.
STRING="this is a string"
SUBSTRING="hat"
expr index "$STRING" "$SUBSTRING" # 1 is the position of the first 't' in $STRING

Substring Extraction
Extract substring of length $LEN from $STRING starting after position $POS. Note that first
position is 0.
STRING="this is a string"
POS=1
LEN=3
echo ${STRING:$POS:$LEN} # his
If :$LEN is omitted, extract substring from $POS to end of line
STRING="this is a string"
echo ${STRING:1} # $STRING contents without leading character
echo ${STRING:12} # ring

Simple data extraction example:


# Code to extract the First name from the data record
DATARECORD="last=Clifford,first=Johnny Boy,state=CA"
COMMA1=`expr index "$DATARECORD" ','` # 14 position of first comma
CHOP1FIELD=${DATARECORD:$COMMA1} #
COMMA2=`expr index "$CHOP1FIELD" ','`
LENGTH=`expr $COMMA2 - 6 - 1`
FIRSTNAME=${CHOP1FIELD:6:$LENGTH} # Johnny Boy
echo $FIRSTNAME

Substring Replacement
STRING="to be or not to be"
Replace first occurrence of substring with replacement
STRING="to be or not to be"
echo ${STRING[@]/be/eat} # to eat or not to be
Replace all occurrences of substring
STRING="to be or not to be"
echo ${STRING[@]//be/eat} # to eat or not to eat
Delete all occurrences of substring (replace with empty string)
STRING="to be or not to be"
echo ${STRING[@]// not/} # to be or to be
Replace occurrence of substring if at the beginning of $STRING
STRING="to be or not to be"
echo ${STRING[@]/#to be/eat now} # eat now or not to be
Replace occurrence of substring if at the end of $STRING
STRING="to be or not to be"
echo ${STRING[@]/%be/eat} # to be or not to eat
replace occurrence of substring with shell command output
STRING="to be or not to be"
echo ${STRING[@]/%be/be on $(date +%Y-%m-%d)} # to be or not to be on 2012-06-14

Investigation and Managing Processes


1. Process
 A process is a set of instructions loaded into memory.
 Numeric Process ID (PID) used for identification.
 UID, GID and SELinux context determines filesystem access.
 The Linux Kernel tracks every aspect of a process by its PID under /proc/PID.
Listing Process
 The ps command is used to view the process information.
 By Default, shows processes from the current terminal

Options
 a: Shows processes from all the terminals.
 x: Shows all the processes owned by you, or shows all the processes when used together
with the a option (such as: ps ax) Including processes that are not controlled by a terminal
Such as Daemon processes, This shows up as ? in the tty column of the output
 u: Shows process owner information.
 f: Shows process parentage
 o: Shows custom information Such as pid, tty, stat, nice, %cpu, %mem, time, comm,
command, euser, ruser

Examples:
[mitesh@Matrix ~]$ ps
[mitesh@Matrix ~]$ ps a
[mitesh@Matrix ~]$ ps x
[mitesh@Matrix ~]$ ps u
[mitesh@Matrix ~]$ ps f
[mitesh@Matrix ~]$ ps xo pid,tty,stat,%cpu,%mem,time,command,euser,ruser
[mitesh@Matrix ~]$ ps axo pid,tty,stat,%cpu,%mem,time,command,euser,ruser

Process Status
 Every process has a state property, which describes whether the process is actively using
the cpu (Running), in memory but not doing anything (Sleeping), waiting for a resource to
become available (Uninterruptable Sleep) or terminated but not flushed from the process
list (Zombie).
 Running and Sleeping are normal, but the presence of Uninterruptable Sleep or Zombie
processes may indicate problems lurking on your system.

Uninterruptable Sleep
 Process is sleeping and can not be woken up until an event occurs.
 It can not be woken up by a signal.
 Typically, the result of I/O operations, such as a failed network connections (For NFS
Hard Mounts).

Zombie
 Just before a process dies, it sends a signal to its parent and waits for an acknowledgment
before terminating.
 Even if the parent process does not immediately acknowledge this signal, all resources
except for the process identity number (PID) are released.
 Zombie processes are cleared from the system during the next system reboot And do not
adversely affect system performance.
Finding Process
# Most Flexible
[mitesh@Matrix ~]$ ps axo pid,tty,comm | grep 'cups'
1516 ? cupsd
3066 pts/1 eggcups

# By predefined patterns: pgrep


[mitesh@Matrix ~]$ pgrep -U root
[mitesh@Matrix ~]$ pgrep -G mitesh

[mitesh@Matrix ~]$ pgrep cups


1516
3066

# By exact program name: pidof


[mitesh@Matrix ~]$ pidof cupsd
1516

Signals
 Signals are simple messages that can be communicated to processes with commands like
kill.
 Sent directly to processes, no user interface required.
 Programs associate actions with each signal.
 Signals are specified by name or number when sent man 7 signals shows complete list
 Signal 1 HUP (SIGHUP) Re-read Configuration Files
 Signal 9 KILL (SIGKILL) Terminate Immediately
 Signal 15 TERM (SIGTERM) Terminate Cleanly
 Signal 18 CONT (SIGCONT) Continue If Stopped
 Signal 19 STOP (SIGSTOP) Stop Process

Sending Signals to Process


 By PID: kill [signal] pid ...
 By Pattern: pkill [signal] pattern
 By Name: killall [signal] command ...
 kill can send many signals, but processes only respond to those signals whose they have
been programmed to recognize.
 For Example: Most services are programmed to reload their configuration when they
receive a HUP(1) signals.
 Some processes are terminated when they completed their tasks.
Interactive applications may need the user to issue a quit command.
In other cases, processes may need to be terminated with Ctrl+c, which sends an INT(2)
signal to the process.
 The process is shutdown cleanly means Terminate child process first & Complete any
pending I/O operations.
NOTE!: The KILL(9) signal should be used only if a process will not respond to a Ctrl+c or a
TERM(15) signals. Using KILL(9) signal on a routine basis may cause zombie processes and lost
data.

# The following are all identical and will send default TERM(15) signal to the process with PID
number 3705
[mitesh@Matrix ~]$ kill 3705
[mitesh@Matrix ~]$ kill -15 3705
[mitesh@Matrix ~]$ kill -TERM 3705
[mitesh@Matrix ~]$ kill -SIGTERM 3705

Scheduling Priority
 Every running process has a scheduling priority: A ranking among running processes
determining which should get the attention of the processor.
 Priority is affected by a process’ nice value.
 The nice value range from -20 to 19 ( Default is 0 )
o -20: Highest CPU
o 19: Lowest CPU

Altering Scheduling Priority


 Niceness value may be altered…
 When starting a process
[mitesh@Matrix ~]$ nice -n 5 command
 After Starting the process
[mitesh@Matrix ~]$ renice 5 -p PID
NOTE!: Only root may decrease nice value. Non-privileged users start a process at any positive
nice value but cannot lower it once raised.
[mitesh@Matrix ~]$ nice -n 10 myprog
[mitesh@Matrix ~]$ renice 15 -p PID
[root@Matrix ~]# renice -19 -p PID

Process Management Tools


CLI - top, htop
 Display list of processes running on your system, updated every 3 seconds.
 You can use keystrokes to kill, renice and change the sorting order of processes.
 Use ? key to view the complete list of hotkeys.
 You can exit top by pressing the q key.
GUI - gnome-system-monitor
 The gnome-system-monitor, which can be run from the console Or by selecting
Applications -> System Tools -> System Monitor
 Display real time process information
 Allows killing, re-nicing, sorting
Job Control
Background Process
 Append the ampersand to the command line: firefox &

Suspended Running Program


 Use `Ctrl+z
 Send STOP(19) signal

Manage Background Or Suspended Jobs


 List Job Numbers and Names: jobs
 Resume in the Background: bg [%jobnum]
 Resume in the Foreground: fg [%jobnum]
 Send a Signal: kill [SIGNAL] [%jobnum]

Examples:
[mitesh@Matrix ~]$ ping 127.0.0.1 &> /dev/null
^Z
[1]+ Stopped ping 127.0.0.1 &>/dev/null

[mitesh@Matrix ~]$ bg
[1]+ ping 127.0.0.1 &>/dev/null &

[mitesh@Matrix ~]$ firefox &


[2] 4162

NOTE!: The number next to [2] after backgrounding firefox is the PID
NOTE!: The + or -`` signs next to the job numbers tells which job is the default
+ sign is the default job

Scheduling Process
 One time jobs use at, Recurring jobs use crontab
/-----------------------------------------------------------------------\
| |
| Create | at time | crontab -e |
| List | at -l | crontab -l |
| Details | at -c jobnum | |
| Remove | at -d jobnum | crontab -r |
| Edit | | crontab -e |
| |
\-----------------------------------------------------------------------/
 Non-redirected output is mailed to the user
 The root can modify jobs for other users
at command
 Scheduling One Time Job with at command
 One Command Per Line
 Terminated With Ctrl+d

Options
/-----------------------------------------------------------------------\
| at 8:00am December 7 at 7 am Thursday |
| at midnight + 23 minutes at now + 5 minutes |
\-----------------------------------------------------------------------/
/-------------------------------------------------------------------------------------------------------\
| Command Alias Meanning
|
|--------------------------------------------------------------------------------------------------------
|
|
| atq | at -l | Lists the jobs currently pending.
|
| atrm | at -d jobnum | Deletes the job.
|
| | at -c jobnum | Cats the full environment for the specified
job. |
|
|
\-------------------------------------------------------------------------------------------------------/

Example:
[mitesh@Matrix ~]$ at 0200
at> date
at> cal
at> <EOT>
job 1 at 2011-08-26 02:00
[mitesh@Matrix ~]$ atq
[mitesh@Matrix ~]$ at -l
1 2011-08-26 02:00 a mitesh
crontab command
 Scheduling Recurring Jobs with crontab command
 The cron mechanism is controlled by a process named crond.
 This process runs every minute and determines if an entry in user’s cron tables need to be
executed.
 The crontabs are stored in /var/spool/cron/
 The root can modify the jobs for other users with crontab -u username and any of the
other options, such as -e.
Crontab File Format
 Comment lines begin with #.
 One entry per line, no limit to line length.
 Entry consist of five space-delimited fields followed by a command name.
 Fields are Minute, Hour, Day Of Month, Month, Day Of week.
 An asterisk (*) in a field represent all valid values.
 Multiple values are separated by commas.
 See man 5 crontab for more details
/-----------------------------------------------------------------------\
| |
| Minute | 0-59 |
| Hour | 0-23 |
| Day Of Month | 1-31 |
| Month | 1-12 (Or Jan, Feb, Mar, Etc) |
| Day Of Week | 0-7 (Or Sun, Mon, Tue, Etc) |
| (0 or 7 = Sunday, 1 = Monday) |
| |
\-----------------------------------------------------------------------/
# * * * * * command to execute
#│││││
#│││││
# │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday,
the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)

Example:
[mitesh@Matrix ~]$ crontab -e
#Min Hour DOM Month DOW Command
0 0 31 10 * mail -s "boo" $LOGNAME < boo.txt
0 2 * * * netstat -tulpn | diff - /media/cdrom/baseline
0 4 * * 1,3,5 find ~ -name core | xargs rm -f {}
crontab in details
 The cron mechanism is controlled by a process named crond.
 This process runs every minute and determines if an entry in user’s cron tables need to be
executed.
 The crontabs are stored in /var/spool/cron/`
 The root can modify the jobs for other users with crontab -u username and any of the
other options, such as -e.
Crontab File Format
 Comment lines begin with #.
 One entry per line, no limit to line length.
 Entry consist of five space-delimited fields followed by a command name.
 Fields are Minute, Hour, Day Of Month, Month, Day Of week.
 An asterisk (*) in a field represent all valid values.
 Multiple values are separated by commas.
 Special Time Specification Nicknames: @reboot, @yearly, @annually, @monthly,
@weekly, @daily, @hourly
 See man 5 crontab for more details

Examples:
[mitesh@Matrix ~]$ crontab -e
#Min Hour DOM Month DOW Command
0 0 31 10 * mail -s "boo" $LOGNAME < boo.txt
0 2 * * * netstat -tulpn | diff - /media/cdrom/baseline
0 4 * * 1,3,5 find ~ -name core | xargs rm -f {}

*/2 * * * * echo "Every 2 Minutes" &> /dev/tty1


*/5 * * * * echo "Every 5 Minutes" &> /dev/tty1
@reboot echo "Runs Once After Reboot" &> /dev/tty1

[mitesh@Matrix ~]$ echo '*/15 8-17 * * 1-5 echo Breaktime' | crontab


The Cron Access Control
/---------------------------------------------------------------------------------------------------------------------------
--------------------\
| --------------- -------------- Only root can install the crontab
files. |
|
|
| /etc/cron.allow -------------- The root & All The Listed users in
cron.allow can install the crontab files. |
|
|
| --------------- /etc/cron.deny All The users except The users in
cron.deny can install the crontab files. |
|
|
| /etc/cron.allow /etc/cron.deny The cron.deny file is ignored.
|
| The root & All The Listed users in
cron.allow can install the crontab files. |
\---------------------------------------------------------------------------------------------------------------------------
--------------------/
NOTE!: Denying A User Through The Use Of Above Files Does Not Disable Their Installed
crontab.

System Crontab Files


 Different Format Than User Crontab Files
 Default System Crontab File Is /etc/crontab
 The /etc/cron.d/ Directory Contains The Additional System Crontab Files
Example:
[mitesh@Matrix ~]$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
#run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
02 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
Default Daily Cron Jobs
 The /etc/cron.daily Are Usually Used For:
o Clean Up Temporary Directories
o Update mlocate & whatis Database
o Perform Other Housekeeping Tasks
A) The tmpwatch:
o Deletes All Files In /tmp Directory Which Is Not Accessed For 240 Hours (10 Days)
o Deletes All Files In /var/tmp Directory Which Is Not Accessed For 720 Hours (30
Days)
B) The logrotate:
o Keeps Log Files From Getting Too Large
o Rotates Log Files On
o Predefined Intervals (Weekly)
o When Reach The Predefined Size
o Old Files Are Optionally Compressed
o Configuration Files:
 /etc/logrotate.conf (Global Configuration)
 /etc/logrotate.d/ (Override Global Configuration)

Example: The /var/log/messages Is Rotated Weekly To /var/log/messages-yyyymmdd


The Anacron System
 The Anacron Runs The Missed Cron Jobs When The System Boots.
 The Anacron Command Is Used To Run The Missed Daily,Weekly & Monthly Cron Jobs.

Example:
 According To The /etc/crontab File
 At 4:02 Every Morning, All Of The Executables In The /etc/cron.daily/ Directory Will Be
Run As root User.
 Now Suppose Your Laptop Is Almost Always Off At The 4:02 AM, Then The mlocate &
whatis Database Is Never Be Updated.

Configuration File
 /etc/anacrontab
 Field1: If The Cron Jobs Not Been Run For The Specified No Of Days
 Field2: Wait For The Specified No Of Minutes Before Runs
 Field3: Job Identifier
 Field4: The Cron Job To Run
Examples:
[mitesh@Matrix ~]$ cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
# Period In Days Delay In Minutes Job-Identifier Command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
How Anacron Works
 According To The /etc/crontab File
 The 1st Command To Run Is 0anacron.
 The 0anacron Command Sets The Last Run Timestamp In A /var/spool/anacron/cron.
{daily,weekly,monthly} Files.
 On The System Boot Up, The Anacron Commands Runs.
 The /etc/anacrontab File Specify How Often The Commands In cron.daily/ cron.weekly/
and cron.monthly/ Should Be Runs.
 If These Commands Are Not Runs In This Time Then
 The Anacron Command Waits For The Specified No Of Minutes In The /etc/anacrontab
File & Then Runs The Commands

Grouping Commands
 Two ways to group commands
Compound
 Example: date; who | wc -l
 Commands run back to back
Subshell
 Commands inside parentheses are run in their own instance of bash, called subshell.
 Example: (date; who | wc -l)
 All output is sent to a single STDOUT and STDERR
 Suppose you want to maintain a count of the number of users logged on, Along with a
time/date stamp, in a log file.
Examples:
[mitesh@Matrix ~]$ date >> logfile
[mitesh@Matrix ~]$ who | wc -l >> logfile
[mitesh@Matrix ~]$ date; who | wc -l
Tue Aug 30 14:04:31 IST 2011
3
[mitesh@Matrix ~]$ date; who | wc -l >> logfile
Tue Aug 30 14:05:08 IST 2011
[mitesh@Matrix ~]$ (date; who | wc -l) >> logfile
Exit Status
 Processes report success or failure with an exit status.
 0 for success
 1-255 for failure
 $? stores the exit status of the most recent command
 exit [num] terminates and set status to num

Examples:
[mitesh@Matrix ~]$ ping -c1 -w1 localhost &> /dev/null
[mitesh@Matrix ~]$ echo $?
0
[mitesh@Matrix ~]$ ping -c1 -w1 station999 &> /dev/null
[mitesh@Matrix ~]$ echo $?
2

Conditional Execution Operators


 * Commands can be run conditionally based on exit status.
 && Represents conditional AND THEN
 || Represents conditional OR ELSE
NOTE!: When executing two commands separated by &&,
The 2nd command runs if the 1st command exit successfully (Exit status 0).
When executing two commands separated by ||,
The 2nd command runs if the 1st command fails (Exit status in the range of 1 to 255).

Examples:
[mitesh@Matrix ~]$ grep -q 'no_such_user' /etc/passwd || echo "No such user"
No such user
[mitesh@Matrix ~]$ ping -c1 -w2 localhost &> /dev/null \
> && echo "Localhost is up" \
> || (echo "Localhost is unreachable"; exit 1)
Localhost is up
[mitesh@Matrix ~]$ echo $?
0
[mitesh@Matrix ~]$ ping -c1 -w2 station999 &> /dev/null \
> && echo "Station999 is up" \
> || (echo "station999 is unreachable"; exit 1)
station999 is unreachable
[mitesh@Matrix ~]$ echo $?
1
#!/bin/bash
for x in $(seq 1 10)
do
echo adding test$x
(
echo -ne "test$x\t"
useradd test$x 2>&1 > /dev/null && mkpasswd test$x
) >> /tmp/userlog
done
echo 'cat /tmp/userlog to see new passwords'

test command
 The test command evaluates true or false scenarios to simplify conditional execution.
 Returns 0 for true
 Returns 1 for false
NOTE!: Strings should be compared using a Mathematical Operator, While Numbers are
compared using an Abbreviation (-eq).

Examples:
# Long Form
$ test "$A" = "$B" && echo "Strings are equal"
$ test "$A" -eq "$B" && echo "Integers are equal"
# Shorthand
$ [ "$A" = "$B" ] && echo "Strings are equal"
$ [ "$A" -eq "$B" ] && echo "Integers are equal"

File Tests
 Use the following command for complete list man test
/-----------------------------------------------------------------------------------------------\
|
|
| -d FILE | FILE exists and is a directory
|
| -e FILE | FILE exists
|
| -f FILE | FILE exists and is a regular file |
| -h FILE | FILE exists and is a symbolic link (same as -L)
|
| -L FILE | FILE exists and is a symbolic link (same as -h)
|
| -r FILE | FILE exists and read permission is granted |
| -s FILE | FILE exists and has a size greater than zero |
| -w FILE | FILE exists and write permission is granted |
| -x FILE | FILE exists and execute (or search) permission is granted |
| -O FILE | FILE exists and is owned by the effective user ID |
| -G FILE | FILE exists and is owned by the effective group ID |
|
|
\-----------------------------------------------------------------------------------------------/
Example:
$ [ -f ~/lib/functions ] && source ~/lib/functions
Scripting If Statements
 Every process reports an exit status.
 0 for success
 1-255 for failure
 Execute instructions based on a exit status of the command.
#!/bin/bash
if ping -c1 -w2 station1 &> /dev/null
then
echo "Station1 is up"
elif grep "station1" ~/maintenance.txt &> /dev/null
then
echo "Station1 is undergoing maintenance"
else
echo "Station1 is unexpectedly DOWN!"
exit 1
fi
 The exit status can be checked within the body of the if as shown in the example, Or you
can assign the exit status to a variable using a subshell, as in:
o STATUS=$(test -x /bin/ping6)
 The if structure can be combined with conditional operator
#!/bin/bash
if test -x /bin/ping6; then
ping6 -c1 ::1 &> /dev/null && echo "IPv6 stack is up"
elif test -x /bin/ping; then
ping -c1 127.0.0.1 &> /dev/null && echo "No IPv6, but IPv4 stack is up"
else
echo "Oops! This should not happen."
exit 255
fi

NOTE!: This script checks for the IPv6 version of the ping command (ping6) exists.
If it does, it uses ping6 to send a test packet to the system’s IPv6 Loopback Interface.
Else The script checks for the IPv4 version of the ping command (ping) exists.
If it does, it uses ping to send a test packet to the system’s IPv4 Loopback Interface.
If neither IPv6 nor IPv4 exists,
Something is probably wrong and a non-zero return code is issued along with a warning
message.
 FOR GOOD EXAMPLE OF REAL WORLD SCRIPTS, LOOK AT THE SCRIPTS IN /etc/init.d/*

Network Clients
Web Clients
Links
 Links is a Text Based (Non GUI) Web Broser.
 Provided by the elinks package.
 Full support for Frames and SSL.
Examples:
[mitesh@Matrix ~]$ links http://www.redhat.com
[mitesh@Matrix ~]$ links -dump http://www.redhat.com
[mitesh@Matrix ~]$ links -source http://www.redhat.com

Wget
 The wget is a non-interactive network downloader.
 Retrieves files via HTTP and FTP.
Options:
/-----------------------------------------------------------------------------------------------\
|
|
| --continue (-c) | Continue Partially Downloaded File |
|
|
| --tries=number | Retry the specified number of times to download the files |
| --wait=seconds | Wait the specified number of seconds between the retrievals
|
|
|
| --recursive | Turn on recursive retrieving
|
| --level=depth | Specify recursion maximum depth level
|
| | (The default maximum depth is 5) |
| --convert-links | After download is complete convert all links, |
| | To make them suitable for local viewing
|
|
|
\-----------------------------------------------------------------------------------------------/

Examples:
[mitesh@Matrix ~]$ wget http://www.redhat.com/training/index.html
[mitesh@Matrix ~]$ wget -c ftp://ftp.site.com/selinux.tar.gz
[mitesh@Matrix ~]$ wget --tries=50 --wait=30 ftp://ftp.site.com/files
[mitesh@Matrix ~]$ wget --recursive --level=1 --convert-links http://www.site.com

Google Chrome/Firefox
 Tabbed Browsing
 Popup Blocking
 Cookie Manageent
 Themes and Extensions
 Multi-Engine Search Bar
 Supports For Many Popular Plug-ins
Email and Messaging
Graphical Mail Clients
Evolution
 Flexible Email & Groupware Tool
 Used For
o Email Access
o Maintain a Calendar, Tasklist and Contacts Database
Thunderbird
 Standalone Mozilla Email Client
 Used For
o Email Access
o Usenet and RSS Supports

Security and Anti-spam Features:


 Evolution and Thunderbird both support the Bayesian Spam Fileters.
 Evolution and Thunderbird both allows emails to be signed and encrypted.
 Evolution integrates supports for the GPG (GNU Privacy Guard) utility for this purpose.
 By Default, Thunderbird uses S/MIME for this purpose but use GPG with the help of the
plugin called EnigMail.

Non-GUI Mail Clients


Mutt
 Mappable Hotkeys
 Highly Configurable
 Context Sensitive Help with ?
 Message Threading & Colorizing
 GNU Privacy Guard (GPG) Integration
 Supports POP, IMAP and LocalMailboxes

Examples:
 You Can Read One Mailbox (POP Account, IMAP Account or Local Mailbox) At A Time.
# Start The Local Mailbox
[mitesh@Matrix ~]$ mutt
# The mutt -f Starts The Specified Mailbox
[mitesh@Matrix ~]$ mutt -f imaps://user@server
Note!: You Can Change Which Mailbox Mutt Viewed By Default, By Altering Its Configuration File
~/.muttrc.

Open SSH - Secure Remote Shell


 SSH Allows Remote Logins & Remote Command Execution Via A Secure Encrypted
Connections.

Syntax:
ssh [user@]hostname
ssh [user@]hostname command
Examples:
[mitesh@Matrix ~]$ ssh localhost
[mitesh@Matrix ~]$ ssh [email protected]
[mitesh@Matrix ~]$ ssh neo@localhost 'df -h'
neo@localhost's password:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_Matrix-lv_root 50G 2.4G 45G 6% /
tmpfs 495M 472K 494M 1% /dev/shm
/dev/sda1 485M 30M 430M 7% /boot
/dev/mapper/vg_Matrix-lv_home 59G 3.2G 53G 6% /home

scp - Secure File Transfer


 The scp works like cp command.
 The scp is a secure remote file copy program.

Syntax:
scp [OPTIONS] [[user@]host1:]file1... [[user@]host2:]file2
* Options:
-r(recursive): Recursively copy an entire directory tree
-p(preserve): Preserve the permissions, ownership, and time stamps

Examples:
# Download the process.log file
[mitesh@Matrix ~]$ scp neo@localhost:process.log .
neo@localhost's password:
process.log 100% 8843 8.6KB/s 00:00

# Upload the userstat.log file


[mitesh@Matrix ~]$ scp userstat.log neo@localhost:.
neo@localhost\'s password:
userstat.log 100% 8843 8.6KB/s 00:00

rsync - Efficient File Sync


 The rsync is a faster than scp.
 The rsync copies the difference in like files.
 The rsync uses a secure ssh connections for transport the files.
 The rsync is a fast, versatile, remote (and local) file-copying tool.

Examples:
[mitesh@Matrix ~]$ rsync --progress neo@localhost:file1 .
neo@localhost's password:
file1
55 100% 53.71kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 36 bytes received 129 bytes 36.67 bytes/sec
total size is 55 speedup is 0.33
[mitesh@Matrix ~]$ cat file1
Hello Linux!
Welcome to the OSS!

Open SSH Keybased Authentication


 SSH Allows You To Authenticate Using The Private-Public Key Schema.
 Optional, Password less, But Still Secure Authentication
Uses Two Keys Generated By The ssh-keygen
1. Private Key
o Private Key Stay On Your System
o Usually Passphrase Protected (Recommended)
2. Public Key
o Public Key Is Copied To The Destination With ssh-copy-id
o By Default ssh-copy-id copy the ~/.ssh/id_rsa.pub

Syntax:
ssh-copy-id [user@]hostname
Examples:
ssh-copy-id -i ~/.ssh/identity.pub [user@]hostname
ssh-copy-id -i ~/.ssh/id_rsa.pub [user@]hostname
ssh-copy-id -i ~/.ssh/id_dsa.pub [user@]hostname

NOTE!: Older system may not have ssh-copy-id command


Which will require you to manually copy your public keys to the destination system,
And append it to the ~/.ssh/authorized_keys file, or create the file if it does not exist.
The ~/.ssh/authorized_keys file must be readable by you. Otherwise ssh will ignore it.

Structure Of ssh-keygen
/---------------------------------------------------------------\
| |
| identity Private 600 |
| RSA1 |
| identity.pub Public 644 |
| |
| ssh-keygen |
| |
| id_rsa Private 600 |
| RSA |
| id_rsa.pub Public 644 |
| |
| id_dsa Private 600 |
| DSA |
| id_dsa.pub Public 644 |
| |
\---------------------------------------------------------------/

Examples:
# Generate Protocol2 RSA Key Pairs
[mitesh@Matrix ~]$ ssh-keygen
# Generate Protocol2 DSA Key Pairs
[mitesh@Matrix ~]$ ssh-keygen -t dsa
# Generate Protocol1 Key Pairs
[mitesh@Matrix ~]$ ssh-keygen -t rsa1

NOTE!: By Default ssh-keygen Generates Protocol2 RSA Key Pairs.

ssh-agent
 If you created your private key with a passphrase, which is highly recommended for the
security reasons, Then whenever the key is used to connect to the other machines, you
must type your passphrase.
 An Authentication Agent Stores The Decrypted Private Keys
 Thus, Passphrase only need to be entered once.
 In GNOME Authentication Agent is provided automatically.
 Otherwise, You will need to create a Agent-Managed Shell with ssh-agent command
[mitesh@Matrix ~]$ ssh-agent bash
[mitesh@Matrix ~]$
 The Keys are added to the Agent with ssh-add command
 If no filename is specified, The Keys stored in ~/.ssh/id_rsa and ~/.ssh/id_dsa will be
used
 You can view the list of stored keys by running
[mitesh@Matrix ~]$ ssh-add -l
NOTE!: When you are finished, Just type exit to return your original shell.
When the Agent is terminated, All your keys are forgotten.

FTP Clients
 Several FTP Clients are availables such as sftp, lftp, lftpget, gftp and many more.
sftp
 Interactive Secure File Transfer Program.
 The sftp uses a secure ssh connections for transport the files.
lftp:
 More Featureful File Transfer Program.

Examples:
[mitesh@Matrix ~]$ lftp ftp.example.com
[mitesh@Matrix ~]$ lftp -u joe ftp.example.com

lftpget
 Non-interactive File Transfer Program.

Examples:
[mitesh@Matrix ~]$ lftpget ftp://ftp.example.com/pub/file.txt
gftp
 Graphical File Transfer Program.
 Applications -> Internet -> gFTP
 Allows Drag and Drop Transfer
 Anonymous or Authenticated Access
 Optional Secure Transfer vis SSH (sftp)

SMB Clients
 FTP Like Client To Access SMB/CIFS Resources On Server
 SMB Clients are used to access the shared resources on the MS Windows Network

Options:
-W: Workgroup or Domain
-U: Username
-N: Suppress password prompt (Useful when accessing a service that does not require a
password)
File Transfer with Nautilus
 Nautilus are also used to access the remote file shares.
 Places -> Connect to Server
 Allows Drag and Drop File Transfer
 Graphically Browse With Multiple Protocols
 Suported Connection Types:
 SSH
 FTP
 SFTP
 WebDAV (HTTP)
 Secure WebDAV (HTTPS)
 Microsoft Windows Shares
Nautilus URL
ftp://ftp.example.com | Connects via ftp to ftp.example.com
sftp://[email protected] | Same as above, but connecting securely via ssh as
user

smb:// | List all available SMB Servers


smb://example | List SMB Share on example server
smb://user@example | Same as above but authenticate as user

Xorg Clients
 One Of The Most Important Features Of Xorg Is Its Client/Server Architecture, That Makes
Any Graphical Application On Linux System Completely Network Transparent.
 That Means You Can Start Any Graphical Application On The Remote System & That
Application Displayed On The Local System.
 The Graphical Application Running In This Way
 Affects Files On The Remote System
 Take Up Resources (Cpu Cycles, Memory, Etc) On The Remote System
 And That Graphical Application Only Displayed On The Local System
Note!: All Graphical Applications Are X Clients & Connects To The Remote X Server Via TCP/IP
By Default, The Data Transfer Is Not Encrypted But Can Be Tunneled Securely Over An SSH
Connection
ssh -X [user@]hostname xterm &

Network Diagnostic Tools


ping
 Detects If It Is Possible To Communicate With Other Systems.
 Many System No Longer Respond To The Pings.
host
 DNS Lookup Utility.
 It is normally used to convert Hostnames to IP addresses and vice versa.
dig
 DNS Lookup Utility.
 Similar to host but greater in details.
netstat
 Provides a Number Of Network Statistics.
traceroute
 Display The List Of Computers(IP Addresses) Through Which The Packet Is Passed To
Reach Their Destination.

Installing Application
In Windows operating systems every application comes with a setup file along with a .exe
extension that becomes much easier to install. but on the other hand in Linux-based operating
systems, it becomes different to install.

In Linux distros, there are three different methods to install the application.
1. Using Terminal.
2. Using Software center.
3. Using snapcraft & other package managers.

Install programs in Linux using Terminal (CLI)


For installing applications in Linux we are using Ubuntu Linux to install an application and these
methods are with Ubuntu Linux-based operating systems such as Zorin OS, Linux Mint, and Pop
OS.

Step 1. Go to terminal
For installing an application through the terminal, first of all, we need to open the terminal by
using the shortcut key Ctrl+Alt+T or going into your Dashboard and searching for Terminal.
Step 2. Get a specific command name to install.
Linux has a huge list of commands. run apt update && apt list | wc to get exactly the available
commands in your apt repository (mine has 74909 commands). So it is really hard to remember
these commands. There are some cool ways to find the desired command or tool to install.
apropos the command helps when you know a few keywords related to the command that
define its uses or functionality. It searches the Linux man pages with the help of the keyword
provided by the user to find the command and its functions.
If you don't have one, you can make it. Get information about the tool with apt show tool_name |
grep Description . Now you can use some basic BASH skills to get more info about programs.
Use apt list | cut -d '\' -f1 | xargs apt show | grep Description 2>/dev/null and then grep any
keyword for more results.

Step 3. Install one or multiple software


For example, we are installing a VLC media player. type this command sudo apt-get install vlc to
install the VLC player. (No need to use sudo prefix if you've root user privileges)

and enter your sudo password (your password isn't visible and no hidden sign *)
after a few moments later a question appears in your terminal
Do you want to continue?
press Y follow with the enter key as shown in the above image.
Now your application is visible in your dashboard.
How to Install programs in Linux using the Software center
In Ubuntu Linux, there is a utility application Ubuntu software that is used for software
installation. This is the easiest way to install applications.
Step 1. Go to the application/software store.
Search Ubuntu software on the dashboard as shown in the picture.
Step 2. Search and install programs with ease.

Select your desired application you can also choose by categories either search application
entering the name in the search bar. Once you have found the application you are looking for,
simply click on it.
This will open a page inside the Ubuntu software center with a description of the application.
Click install to install the application on your computer. You can also submit your valuable
feedback and rate the applications.
Note - Authentication is also required for installing an application so you have to enter your
password to install the application.

Install programs in Linux using Snapcraft


snap is a software packaging and development system developed by Canonical for the Linux
operating system with the help of snap store developers that can publish their applications
directly to users.

Install snap software environment on Ubuntu


In Ubuntu 16.04 and older versions, we need to install snaps packages; all the latest Ubuntu and
its derivatives are by default supported. Open the terminal and run this command to install the
snap package management environment.
$ sudo install snapd

Then after we can install the application using the snaps command from the software website.
(make sure that snapd daemon is running)
$ sudo snap install <package>
for making the concept clear we are installing VLC using snap
Open terminal and Enter this command.
$ sudo snap install vlc
A few moments later, the VLC media player will be installed.

Now VLC media player is installed in your Ubuntu operating system you can find it in your
dashboard.

Conclusion
Now you have learned three different ways to install all popular tools in Linux easily using
command-line package managers and software stores.

You might also like