Вы находитесь на странице: 1из 118

SANBIAS

This is a small endeavor by team SANBIAS in the field of training. Our aim is to reach every
person who is willing to get best and depth knowledge of LINUX by our special training
programs which is different from regular training methods. Having years of experience in
training field we are willing to serve geeks to provide them class knowledge.

What is an Operating System..?


Generally what we think about operating system that it is an interface between USER and
Hardware, It is true at a level but if we have a look at the image below we will find that user
needs a mediator to access hardware.
So operating system is KERNEL + APPLICATION SOFTWARE

 User gives instructions to the system through OS interface. For example when a
user opens a folder, a program runs in background which opens the folder. A
program is a set of instruction, in LINUX we generally called it “COMMAND”
 Linux based Operating system has powerful command line interface. User need to
give commands in human language for example cd, exit etc. Machine does not
understand human Language. To convert human language into machine language
we need a mediator generally known as shell.
 Shell is a compiler which gives platform to executes commands given by user.
Shell interacts with kernel and kernel gives output after interacting with
hardware.
 So OS is not interface between user and hardware but KERNEL and SHELL is.

Types of operating systems


There are dozens of operating systems available in market but generally operating systems are
of two types

1) Open source
2) Close source

Open source vs. Close source


Open-source software (OSS) is computer software with its source code made available with a
license in which the copyright holder provides the rights to study, change, and distribute the
software to anyone and for any purpose. Example:- C, C++, MySql, PHP, PERL, linux kernel.

Close source (or proprietary) is a type of computer program source code development. With
closed source software, the source code is not shared with the public for anyone to look at or
make changes to. Closed source is the opposite of open source. IE, MS Office, etc.
LINUX
Linux is a kernel, based on UNIX. It is similar to UNIX kernel but not same. Linux advanced
features makes it different from other. That is why most of the server are deployed on linux
platform

 Introduction to Linux:
o Fresh implementation of Unix APIs

o Open source development model

o Supports wide variety of hardware

o Supports many networking protocols and configurations

o Fully supported

1) Linux is a UNIX like OS: Linux is a similar to UNIX as the various UNIX versions are to each other.
2) Multi-User and Multi-tasking: Linux is multi-user and multi-tasking operating system. That means that more
than one person can be logged on to the same Linux computer at the same time. The same user could even be
logged into their account from two or more terminals at the same time ; Linux is also Multi-Tasking. A user can
have more than one program executing at the same time.
3) Wide hardware support: Red Hat Linux support most pieces modern X86 compatible PC hardware.
4) Fully Supported: Red Hat Linux is a fully supported distribution Red hat Inc. provides many support program
for the smallest to the largest companies.

Features of linux

 Portable - Portability means softwares can works on different types of hardwares in


same way.Linux kernel and application programs supports their installation on any kind
of hardware platform.

 Open Source - Linux source code is freely available and it is community based
development project. Multiple teams works in collaboration to enhance the capability of
Linux operating system and it is continuously evolving.
 Multi-User - Linux is a multiuser system means multiple users can access system
resources like memory/ ram/ application programs at same time.

 Multiprogramming - Linux is a multiprogramming system means multiple applications


can run at same time.

 Hierarchical File System - Linux provides a standard file structure in which system files/
user files are arranged.

 Shell - Linux provides a special interpreter program which can be used to execute
commands of the operating system. It can be used to do various types of operations, call
application programs etc.

 Security - Linux provides user security using authentication features like password
protection/ controlled access to specific files/ encryption of data

 Better Memory Management - Fences are another new feature in Linux 3.17, and
they're aimed at helping the kernel manage memory better. "A fence can be attached to
a buffer which is being filled or consumed by [hardware] to allow userspace to pass the
buffer without waiting to another device
Kernel
Kernel is the core component of an Operating System. It handles the processes and services
running on a system. From managing process to sharing them resources is the primary task of
KERNEL. It acts as an interface between the user applications and the hardware. A kernel is
actually a large block of code which keeps the system up and running from the time of
booting, till shutdown. The main tasks of the kernel are :

 Process management.
 Device management.
 Memory management.
 Interrupt handling.
 I/O communication.
 File system management.

Types of kernel
There are two types of kernel

1) Monolothic
2) Micro kernel

Application Kernel
User Hardware

Interface Kernel Tasks


1) GUI:-Graphical User Interface. 1) Process Management.
2) CLI:-Command Line Interface. 2) Memory Management.
3) Device Management.
4) System Initialization.
5) Security.
6) File System Management.
 History of Linux:
1 .UNIX - 1969 (Assembly Language)
2. UNIX - 1971 ( C Language)
3. GNU-1983 (GNU Not UNIX)
4. FSF- 1985.
5. GPL-1989 (General Public License)-Primary license for open source software.
6. Linux- 1991. (Linus Torvalds)

 Flavors of Linux:

There are so many linux flavour available in the market. As we can see in the above image
these are the most using distribututions of linux. Every individual flavour has its own feature
so all are best.
1. Red Hat
2. Fedora RPM Family
3. Centos
4. Backtrack
5. Mint
6. Ubuntu.
7.
Redhat
Red Hat, Inc. is an American multinational software company providing open-source software products to the

enterprise community, founded in 1993.

Versions of REDHAT

REDHAT( 1 – 9)

RHEL 1- 7 (Redhat Enterprises Linux 1-7)

Redhat started its certification program in 1999. There are list of certifications provided by REDHAT

 Red Hat Certified System Administrator (RHCSA)

 Red Hat Certified Engineer (RHCE)

 Red Hat Certified Virtualization Administrator (RHCVA)

 Red Hat Certificate of Storage (RH236)

 Red Hat Certified Datacenter Specialist (RHCDS)

 Red Hat Certified Security Specialist (RHCSS)

 Red Hat OpenStack Administration (CL210)

 Red Hat Certificate of Expertise in Server Hardening (RH 413)

 JBoss Certified Application Administrator (JBCAA)

 Red Hat Certified Architect (RHCA)


RHCSA

• Introduction to open source technology & features of Linux


• Understanding of file system, MBR, Bootable Media.
• Installation of RHEL operating system by (Installation media,
Network, USB).
• Understanding of files & Directory with graphical & CLI.
• Learn some basic commands & some bash tricks.
• Advanced Commands like find, awk, sed, grep.
• Concepts of soft links &Hardlinks through inodes.
• Understanding of Authentication, Authorization & Auditing.
• User administration, permissions Sudo access & access control list.
• Managing file system (Partitions, Lvm, Swap).
• Configuration of backup process and compression of data.
• Understanding a boot mechanism.
• Monitoring & Managing Processes & Log Messages.
• Installation & Configuration of Software (RPM , YUM )
• Automatic Installation with Kickstart.
• Controlling Services & daemons.
• Configuring & Securing SSH Services.
• Manage Redhat Linux Networking.
• Scheduling commands using cron & anacron jobs.
• Centralized Authentication using Identity management services.
• Scheduling Tasks & Maintaining Kernel
• Configuring Selinux Securities.
• Limiting Network Communication with firewalld.
• Understanding Virtualization in RHEL7.
• Accessing network Storage Using NFS & SAMBA.
RHCE

• Controlling Services and daemons(systemctl commands)


• Managing IPV6 networking
• Configuration link aggregation and bridging
• Network post security
• Managing DNS for servers
• Configuring Email transmission
• Providing remote block storage(ISCSI Storage)
• Providing file-based storage(NFS & SAMBA)
• Configuring mariaDB database
• Providing Apache HTTPD web service
• Writing bash scripts.
• Bash conditionals and control structure
• Configuring the shell environment
• Linux containers and docker
Interview questions
1) What is Linux and why is it so popular?

Ans : Linux is an operating system based on UNIX, and was first introduced by Linus Torvalds commonly used
in servers. It is based on the Linux Kernel, and can run on different hardware platforms manufactured by Intel,
MIPS, HP, IBM, SPARC and Motorola. Another popular element in Linux is its mascot, a penguin figure named
Tux.Popularity of Linux is because of the following reasons

• It is free and open source. We can download Linux for free and customize it as per our needs.

• It is very robust and adaptable.

• Immense amount of libraries and utilities

2) What is BASH?

Ans : BASH is short for Bourne Again SHell. It was written by Steve Bourne as a replacement to the original
Bourne Shell (represented by /bin/sh). It combines all the features from the original version of Bourne Shell,
plus additional functions to make it easier and more convenient to use. It has since been adapted as the default
shell for most systems running Linux.

3) What is the core of Linux Operating System?

Ans : The core of the Linux operating system is Kernel. It is broken down into Shell, Command, Script, and
Terminal. Shell is a command Line Interpreter, Command is user Instruction to Computer, Script is collection of
commands stored in a file, and Terminal is a command Line Interface.

4) What is the basic difference between UNIX and Linux Operating System?

Ans : Linux is free and open-source software (allowing programmers to program with Linux not around it), the
kernel of which is created by Linus Torvalds and community. UNIX, on the other hand, is UNIX is copyrighted
name only big companies are allowed to use the UNIX copyright and name, so IBM AIX and Sun Solaris and
HP-UX all are UNIX operating systems.

5) What is LILO?

Ans : LILO is a boot loader for Linux. It is used mainly to load the Linux operating system into main memory
so that it can begin its operations.

6) What is an INODE?

Ans : All files have its description stored in a structure called “inode‟. The inode contains info about the file-size, access
and modification time, permission and so on. In addition to descriptions about the file, the inode contains pointers to
the data blocks of the file
RHEL 6/7-:
Ctrl+Alt+F1—GUI

Ctrl+Alt-F2

Ctrl+Alt-F3 CLI

Ctrl+Alt-F6

RHEL 5:
Ctrl+Alt-F1

Ctrl+Alt-F2 CLI

Ctrl+Alt-F6

Ctrl+Alt+F7 -- GUI.

 File System Hierarchy

/root /home /etc /tmp /var /boot /mnt /proc

/dev /usr /bin


/ This is top level directory
It is parent directory for all other directories
It is called as ROOT directory
It is represented by forward slash (/)

/root it is home directory for root user(super user)


It provides working environment for root user

/home it is home directory for other users


It provide working environment for other user (other than root)

/boot It contains bootable files for linux


GRUB (Grand Unified Boot Load)

/etc it contains all configuration files


Like /etc/passwd ……….. User info
/etc/resolve.conf…. Preferred DNS

/usr by default softwares are installed in /usr directory

/bin it contains commands used by all users


(Binary files)
/sbin it contains commands used by one Super User (root)
(Super users’ binary files)

/dev it contains device files


Like /dev/hda ….for hard disk

/proc it contain process files


Its content are not permanent, they keep changing
It is also called Virtual Directory
Its file contain useful information used by OS
Like /proc/meminfo …… information of RAM/SWaP
/proc/cpuinfo … information of CPU

/var it is containing variable data like mails, log files

/mnt it is default mount point for partition


It is empty by default

/media it contains all of removable media like CD-ROM, pen drive

/lib it contains library files which are used by OS


Basic Commands
#arch: To show architecture of system.

#free: Shows RAM in KB

#free –m: Shows RAM in MB

#hostname: Gives system name

#uname or #uname –r: Shows kernel version

For RHEL 6 Kernel version is 2.6 & for RHEL 7 it is 3.10

#uname –a: gives all information.

 Types of Files:

Symbol File Type


- Regular File or Normal File.
d Directory
l Link File
c Character Specific File
p Pipeline File
b Block File
s Socket File

List of basic commands

Pwd ls -Rl mv (cut paste)


Cd ls -li mv (rename)
cd .. touch rm
cd - vi, vim whatis
cd ~ cat whereis
Ls Echo history
ls -l mkdir
ls -al cp

Syntax to write commands

Command options argument1 argument2 argument 3 …. argument n

First we have to write command, then options to customize commands for desired output and then argument
on which we are running our command.
1) pwd - Present Working Directory. This command show your current location. It gives your current
working directory as output.

In above image when I run command “pwd” it show /root/Desktop. It means my current location is in Desktop
under /root directory. Desktop of root user.

NOTE:- Desktop, Downloads and Documents these three directories always start with capital “D”

2) cd – change directory. In order to change directory from present directory to another directory we will
use cd command.

Here I “cd /test” means changing directory from current location to “test” directory exists in “/” directory. The
“/” after “test” will not affect anything so don’t get confused. Take it easy..!!

If you run only “cd” command it will take you to current logged-in user’s home location. If you are login as root
and you run cd command, it will take you to “/root” which is home location of ROOT user.

As you can see our current location was “test” directory, I ran “cd” command without argument and it
changed our location to home location of current logged-in user. You can see “~”. This sign “~” is called tilds. It
means home location. Pwd is showing current location which is “/root”, the home location of root user.
3) cd .. – cd following with “..” will take us to parent directory. If you are going one directory up we
will use “cd ..”

Our current location is “/root”. After running “cd ..” we moved to “/ “ directory means a directory above.
We can see it by “pwd”.

4) “cd - “ cd command with “-” is use to go back to the last changed directory.

“cd /test” changes the directory to /test and “cd /ankit” changed the directory from “test” to “/ankit”.
When “cd - ” executed it changed the directory again to “ /test ” because “ /test ” was our last directory.

5) “cd ~” - Very simple, this command changes our current location to home location of the currently
logged-in user.

Present working directory is test. “cd ~” change it to “~” or “/root”, means home location of root user.
6) “ls” - ls command is use to list file and directories. There are two things if you are using “ls” command
with argument, it will display argument’s files & directories and “ls” without argument display the current
directory’s files & directories.

ls without argument displays output of current directory. When ls is execute on “ / ” it displays contents
of “ / ”

7) “ ls -l” - To display long listing of files and directories. Long listing means output with more
information like file’s Inode number, file’s owner etc, “ls” only displays name of files and directory.
Same thing here if we are passing argument or running command without argument, it will display
output according to given argument.

“ls -l” without argument displaying long listing of current working directory. And “ls -l“on “/test “
displaying long listing of contents of “/test” directory.
 “ll” command is alias of “ls -l”, here is the sample output:-

#date: Shows system current date and time.

*To change system date and time: #date MMDDHHMMYYYY.SS

Where MM-Month, DD-Day, HH-Hours, MM-Minutes, YYYY-Year, SS-seconds.

*To check date after 10 days: #date –date=“10 days”

*To check date before 10 days: #date –date=”10 days ago”

#date +%A-%B: shows only day and month (like Sunday January)

#cal : shows calendar of current month.

#cal year:shows calendar of specific year(e.g. #cal 2015 )

#cal month year: shows calendar of month (e.g #cal 02 2015: shows cal of feb 2015)
8) “ ls -al ” – command to see all files along hidden files and directories. Hidden files and directories
name always start with dot “.”

In last column we can see some file’s name are starting with “. “(Dot). They are hidden files.

9) “ ls -Rl ” displays long listing but in tree or hierarchy structure. Sample Output :
10) “ls -li” – command to check inode number of particular files & directories or multiple files and
directories.

“ls -li” on “/kapil” file displaying inode number of “/kapil” file and “ls -li” on “/test/a” directory list all
the files in “/test/a” directory and displaying inode number of all files.

11) “touch” - this command creates an empty file and second use of “touch” command is “Time Stamp
Modifier”. Whenever we access any file or directory, a time stamp creates on it automatically the time we
access that file. Touch command change that time stamp from that time to the time we run touch
command on that file.
Touch command created file in “/” by name “mohit”. After listing “/” we can see mohit file presents there
which is an empty file created by touch command above. After long listing of “/mohit” we can see “23:04”
in second last column of output. This time is last access time of “”/mohit””. If we want to change the time
stamp we can do it by run touch command again on “/mohit”.

We can see difference of last access time of “/mohit”.

12) vi and vim – vi and vim are command line text editor in Linux. By the help of these commands we can
create a file, or modify an existing file. We can either use vi or vim to create a new file. Difference
between touch and vi, vim is touch only create an empty file but through vi, vim we can create and add
content in the file.

Vi is older version and vim is advance version of “vi”. The basic difference is “vim” editor can
highlight syntax error in file as this editor supports multiple languages like c, c++, php etc. Steps
to create a new file through vim. Difference between vi and vim editor:
Vi Vim
1) Black and white Colored
2) No encryption provided. Encryption is provided.
3) No visual mode. Visual mode.
Steps to create a file through “vim”:
 vim testfile ## “testfile” is the name of new file

After pressing enter a new blank file will be open. Right now we can’t insert contents in file for

we are in command mode. To insert or add contents in this file:-

 press “ i ” ## “ i ” means insert mode, now you can write in file.

 Add your desired contents.

 To save your file you must close insert mode. Press “esc” escape to go back in command mode

 In command mode we will give command of saving the file and quit the editor. To do so press

shift+: (means command mode) and then type “ wq! ”

 W –save

 Q – quit

 ! – forcefully

In case if you don’t want to save file, press shift+: and the “q!” means quit without saving.
13) “cat” – command to display contents of a file, without opening it. cat command displays output of the
file on terminal. I have made a file with name “testfile” in above example. Now I am going to show you
the contents of “ testfile” by cat command.

“Hello, Welcome to SANBAIS” is the content of file displayed without opening it.

14) “echo” – it displays argument.

15) “mkdir” – Command to create a directory or multiple directories. Syntax of the command is:- “mkdir
directory-name”

In above example I have created two directories with same name. The question is what is the location
of both directories? First directory is created in “pwd(present working directory)”. Keep in mind that if
you are creating any file or directory without giving “/” as starting path then it will be created in
current location.
Second directory is created in “/” directory.
Okay. I am going to create recursive directories.

In first command I tried to make collaborative directories, but not succeeded. So I applied “-p” option
along.
16) “ cp ” - command to copy files and directories into other directory.

Syntax - cp <source file & directory> <destination directory>

I created a file with name “file1” by touch in ~ (home location). I copied this file into “/ankit” directory. It
is a single file so it copied without any error. If you copy a folder consists of multiple files and folders
inside, then you must add “-rfv” option along “cp” command. “rfv” r means to copy files and
directories recursively under source directory. F means forcefully. And v means verbose or visual
mode, this option displays copying file process on terminal.

17) “mv” command for cut and paste files and directories. Syntax :- “mv <source file> <destination>

/ankit directory moved to /new directory.

18) “mv” - command to rename file or directory. Syntax :- “mv <file or directory> <newname>”

I created a file in “/” with name “oldfile” and renamed it with new name “newfile”
19) “ rm ”command to remove files and directories

In first command I created a file “/abc” and removed it using rm command. But in second command I
tried to remove a directory “/abc” but It gave an error. When we remove a directory containing files
and folders inside it then we use “-rvf” option along rm command, in order to remove recursive files
and directories.

20) “whatis”- command to display one line description of command. Syntax:- “whatis <command name>”

21) “whereis” – command to check the location of command from where it executes and the location of
command’s documentation. Syntax:- “whereis <command>”

22) “man” – command to display manual of command. Every command has its manual in system. Through
manual we can check what a command does and what additional options can be applied along that
command.
Syntax:- “man <command name>
Example:- man cp, man ls, man rm etc.

23) “history” command to display all the history of commands run till you logged in
Some more drilling on basic Commands
1) Create a file in root’s home directory by name “secret”

2) Create an empty file in “/etc” directory by name “linux” (don’t use vi or vim)

3) Create multiple directories as given below:

 New/new1 (in /root)

 /a/b/c/d (in /)

 Cd /a/b/c and then create two directories by any name in a single command.

4) Copy all the contents of /tmp directory in ‘new1’ directory created in que3

5) Remove all the contents of /tmp directory (don’t remove /tmp directory but its content

only)

6) Rename the ‘secret’(created in 1st que) file with ‘public’.

7) What is the inode number of ‘public’ file.

8) Create a hidden file with your name at any location.

9) What is the path of ‘cp’ command.

10) Add some contents in ‘public’ file and display its output on terminal without opening it.
 Symbolic link :
There are two types of links:-

No Soft Link Hard Link


1. Size of link file is equal to no. of characters Size of both file is
in the name of original file same

2 Can be created across the Partition Can’t be created across the partition

3 Inode no. of source and link file is different Inode no. of both file are same

4 If original file is deleted, link is broken and If original file is deleted then also link will contain
data is lost data

5 shortcut file backup files

Creating a soft link file:- Creating a Hard link file:-


#ln –s <source file > <destination file> #ln <source file ><destination file>

 Filter commands: less, more, sed, cut, awk, head, tail, sort these commands are used to filter the
output.

1) less: less command is used to see the output line wise

Ex- # less filename

2) more: same like less command.

Ex- # more filename


3) sed: (stream editor): sed command is used to find a word in file and replace it with word required to
be in the output.

# sed ‘s/searchfor/replacewith/g’ filename

Where s-substitute & g-global (replace multiple keywords)

 Note: it will only modify the output, but there will be no change in the original file
To make changes in original file: #sed –i ‘/s/searchfor/replacewith/g’ filename (where i-insert)
Head
This command is use to display top 10 lines of a file. If you want see a file’s top 10 lines
without opening it then use head command. You can also specify number of top lines you
want to see. For example top 5 lines or 15 lines or 20 lines.

Syntax:- “ head <filename> “

A file names “text” have some contents in it as we can see in above image. Now I am going to
display top lines of these file by “head” command.

Here is the output of head command which is displaying top 10 lines of the file “text”. If you
want to see specific number of top lines then specify numbers following by “-” hyphen as
option. For example:-
# head -5 /text
Above command will display top 5 lines. Means you can customize your output by specifying
line numbers in options.
Tail
Tail is opposite to head. To display bottom 10 lines we use tail command.

Syntax: - “tail <filename> “

A file containing 15 numbers, applying tail command displays bottom 10 lines. Same option
will be applied to display particular number of lines.

Using “-5” option displaying bottom 5 lines.


CUT
The cut filter can select columns from files, depending on a delimiter or a count of bytes.

The screenshot below uses cut to filter for the username and userid in the /etc/passwd file.

It uses the colon as a delimiter, and selects fields 1 and 3.

Syntax:- “cut -d”delimeter” -fn <file>”

In above line cut is the command and “-d” means delimiter, it can be space, comma or any

symbol. “–fn” means column number, for example “–f1”(column 1), “–f2” (coulumn 2), -

f1,2,3,4(column number 1, 2, 3, 4).

Ex- # cut –d “:” –f1 filename – cut 1st column of file (where d-differentiator & f- field no)

# cut –d “:” –f1,2 filename – cut 1st & 2nd column of file

# cut –d “:” –f1-5 filename – cut 1st five columns of file

# cut –c 1-10 filename – cut 1st ten characters of file

The screenshot above uses “cut” filter to get column number 1 and 3 using delimiter as “:”
colon and from the file /etc/passwd, with output of bottom 6 lines.
WC
Counting words, lines and characters is easy with wc.
Syntax: - “wc <file>”
“-l” option to count lines.
“-w” option to count words.
“-c” option to count characters.

AWK
awk is use to process columns from a file.
Syntax:- “awk ‘{print $n}’ <file>”
“$n” means column number to print, you can use comma “, “to define more than one table,
for example ‘{print $1,$3,$6}’ .

We can see a table in above image having multiple columns.


 First command is displaying first column because “$1” is given after print.
 Second command displaying multiple column defined by using comma”,”.
 In third command I used “-F” option. It stands for field separator, means print the column
according to defined field separator. In third example I defined semicolon “:” as field
separator and printed the column number second and third, means print the 2nd & 3rd
column after semicolon “:”.
 Fourth command is displaying the column number 2nd & 3rd but according to defined field
separator. I have defined space” ” as a field separator. It means on first occurrence of a
space “ ” print the second and third column after that first space. So easy, isn’t It.?
 Fifth command is displaying first column but after first occurrence of space because we
defined space as a field separator.
 Grep command:- Global Regular Expression Print (Grep) this command is used to find
particular string from a file .
Ex- # grep –opt string filename Ex- #grep root /etc/passwd
o To avoid case sensitivity of the word :- # grep –i string filename (where i-case
insensitive)
o To display the things except the given word:-# grep –v string filename
o To display the searched word in color: # grep –color string filename

 Find command: This command is used to find out location of files or directories.
Ex-# find place –opt filename

Options:

Option Use
-name For searching a file with its name
-type For searching a particular type of file
-inum For searching a file with particular inode no
-user For files whose owner is particular user
-group For files belonging to particular group
-size For searching files of particular size

Ex-
 finding a file with a name :- # find / -name komal

 finding a file with its inode no:- #find / -inum 1001

 finding a file of particular size:- #find / -size 2M

 locate command:- This command is used to find out location of files/directories.

Ex- # locate file/dir name


 Redirection and pipelining:

Redirection: Redirection is a process where we can copy the output of any


commands(s), file(s) into a new file. There are two ways of redirecting the output into a
file i.e. using >(redirection) and >>(append)

Note: if the given name of the file is not available a new file will be created
automatically. If the file already exists then it will overwrite contents of that file.

Ex-# head -5 /etc/passwd > passwd.txt

 Appending another output in same the same file

Ex-# tail -5 /etc/passwd >> passwd.txt (this will not overwrite the file )

Pipelining: chain of processes in which output of one command is fed in input to other
command.
Ex- ll | awk –F “ ” ‘{print $NF}’

GREP SED AWK Interview questions

1. Write a command to print the lines that has the pattern "July" in all the files in a
particular directory?

grep July *
This will print all the lines in all files that contain the word “July” along with the file
name. If any of the files contain words like "JULY" or "July", the above command would
not print those lines.

2. Write a command to print the lines that has the word "July" in all the files in a
directory and also suppress the filename in the output.

grep -h july *
3. Write a command to print the lines that has the word "July" while ignoring the case.

grep -i July *
The option i make the grep command to treat the pattern as case insensitive.

4. When you use a single file as input to the grep command to search for a pattern, it
won't print the filename in the output. Now write a grep command to print the filename
in the output without using the '-H' option.
grep pattern filename /dev/null
The /dev/null or null device is special file that discards the data written to it. So, the
/dev/null is always an empty file.

Another way to print the filename is using the '-H' option. The grep command for this is
grep -H pattern filename

5. Write a Unix command to display the lines in a file that do not contain the word
"July"?
grep -v July filename
The '-v' option tells the grep to print the lines that do not contain the specified pattern.

6. Write a command to print the file names in a directory that has the word "July"?
grep -l July *
The '-l' option make the grep command to print only the filename without printing the
content of the file. As soon as the grep command finds the pattern in a file, it prints the
pattern and stops searching other lines in the file.

7. Write a command to print the file names in a directory that does not contain the
word "July"?
grep -L July *
The '-L' option makes the grep command to print the filenames that do not contain the
specified pattern.

8. Write a command to print the line numbers along with the line that has the word
"July"?
grep -n July filename
The '-n' option is used to print the line numbers in a file. The line numbers start from 1
9. Write a command to print the lines that starts with the word "start"?
grep '^start' filename
The '^' symbol specifies the grep command to search for the pattern at the start of the
line.

10. Write a command to print the lines which end with the word "end"?
grep 'end$' filename
The '$' symbol specifies the grep command to search for the pattern at the end of the
line.

11. Write a command to select only those lines containing "July" as a whole word?
grep -w July filename
The '-w' option makes the grep command to search for exact whole words. If the
specified pattern is found in a string, then it is not considered as a whole word. For
example: In the string "mikejulymak", the pattern "July" is found. However "July" is not
a whole word in that string.

12 How to Print only Blank Line of File.


sed -n '/^$/p' Test_file.txt

13 To Print First and Last Line using Sed Command


sed -n ‘1p’ Test_file.txt
sed –n ‘$p’ Test_file.txt

14 To Print all line Except First Line


sed –n ‘1!p’ Test_file.txt

15 Delete all Line except First Line


sed –n ‘1!d’ Test_file.txt

16 How to get only Zero Byte files which are present in the directory
ls -ltr| awk '/^-/ { if($5 ==0) print $9 }'
17 How add a First record and Last Record to the current file in Linux
sed -i -e '1i Header' -e '$a Trailor' test_file.txt

18 How to display Even number of records into one file and Odd number of records into
another file
awk 'NR %2 == 0' test_files.txt
awk 'NR %2 != 0' test_files.txt

19 Remove all empty lines:


sed '/^$/d' test_file.txt
sed '/./!d' test_file.txt
 User Administration:-
 To add new user:- # useradd username
Ex- # useradd komal

 To give password :- # passwd username

User’s name is stored in /etc/passwd file


Password is stored in /etc/shadow file in encrypted format

Types of users:-

No. User Type Example User Id Group ID


1. Super User Root 0 0
2. System User Ssh, ftp, apache etc 1-999 1-999
3. Normal User Komal, lalit etc 1000- 1000-

1) Super User:- Super user or the root user is the most powerful user. He is the
administrator user.

2) System User:- System users are the users created by the software or applications. For
example if we install Apache it will create a user apache. These kinds of users are
known as system users.

3) Normal User:- Normal users are the users created by root users. Only the root user
has the permission
to create or remove user.

 /etc/passwd & /etc/shadow file have specific format:

1) /etc/passwd:- There are 7 fields

Loginname:x:UID:GID: :home dir:shell


The above fields are
 login name = User name
 x = link to password file i.e. /etc/shadow
 UID = User id
 GID = group id
 Blank field = command (brief information about the user )
 Home directory = home directory of the user
 shell

2) /etc/shadow:- There are 8 fields

 Login name = Username

 Password = There are 3 fields


$ algorithm $Hash values $ password (encrypted format)

-MD5 or SHA

 Last password change since 1st Jan 1970

 Minimum Password age (By default 0)

 Maximum password age =99999

 Warning days = 7

 Password inactive days

 Password expiration date

To change minimum password age, warning days, password inactive days etc :-
# chage username.

To display information about user:-


# chage –l username
After creating a user if we need to modify the attributes of user like changing uid, changing
secondary group id or adding a comment, locking or unlocking the user account, can be done
by following command

# useramod –option argument username

1) To change uid of user:- # usermod –u uid username

2) To change gid of user:- # usermod – g gid username (primary gid)

3) To change shell of user:- #usermod –s shell username

4) To change home dir of user:- # usermod –d dir namr username

5) To change username :- # usermod –l old login name new login name

6) To lock user:- # usermod –L username

7) To unlock user:- #usermod –U username

8) To delete a user:- # userdel username

 Group Administration:-
 To add o group :- # groupadd groupname
 To give password:- # gpasswd groupname

Group information is stored in /etc/group file


Password is stored in /etc/gshadow file
 A user’s group primary group is defined in the /etc/passwd file and secondary group are
defined in the /etc/group file.

/etc/group & /etc/gshadow file have specific format:-


1) /etc/group:- There are 4 fields

 Group name
 x = Password pointer
 gid = Group ID (Secondary GID)
 Group members

2) /etc/gshadow:-There are 4 fields

 Group name
 Password
 Admin of group
 Group members

To change group id :- # groupmod –g gid groupname

To change group name:- # groupmod –n newname oldname

Adding members to a group:- # usermod –G groupname username

To delete group:- # groupdel groupname


 Permissions:-
Assigning different permissions to any file or directory:
There are two ways:
1) Symbolic Method:-
Syntax:- #chomd category action permission file/directory name

Category Action Permission


user (u) + allow/grant r-read (4)
group (g) - remove w-write(2)
other (o) x-execute(1)

Ex:- 1) Assigning different permissions to the file (user=rwx, group=rw and other=r)
#chomod u+rwx,g+rw,o+r filename
2) Assigning full permissions to a file:- #chomd ugo+rwx filename

2) Absolute Method:-using numbers instead of symbols

Syntax:- #chomd permission(no) file/directory name.


For read = 4 , write =2 , execute= 1
Ex- 1) Assigning different permissions to the file (user=rwx, group=rw and other=r)
#chmod 761 filename (where 7 means rwx i.e. 4+2+1, rw=6 i.e. 4+2 and 1 indicate
x)

2) Assigning full permissions to a file:- #chomd 777 filename

 To change a owner of file or directory:- #chown -R username file/directory name


 To change a group of file or directory:-#chgrp --R groupname file/dir name
(Where R- recursive)
 Special Permissions:-
There are three special permissions assigned to file or directory apart from
basic permissions:
1) Sticky Bit
2) SGID- Set Group ID
3) SUID- Set User ID

Permission Symbol Numeric Form Example


Sticky Bit T 1 #chmod o+t or #chomd 1776
SGID S or s 2 #chmod g+s or #chomd 2766
SUID S or s 4 #chmod u+s or #chmod 4766
Where s=setgid + execute & S=setgid only ,same for SUID also

1) Sticky Bit:- If sticky bit is applied on a file or directory, then only root and owner of that
file or directory can delete it. Even if others are having full permissions they cannot
delete the file or directory.
Ex- #chomd 1776 file/directory name or #chmod o+s file.dir name

2) SGID- Set Group Id- SGID can be used on a directory so that every file created in that
directory will have the directory group owner rather than the group owner of the user
creating the file.
Ex- #chmod 2776 dir name or #chmod g+s dir name

3) SUID-Set User Id :- If SETUID bit is set, when the file will be executed by user, the process
will have the same rights as the owner of the file begin executed. Many of the system
commands are the best example for SUID, basically the owner of the commands will be
root, but still a normal user can execute it.

 ACL (Access Control List):-

ACLs allow fine-grained permissions to be allocated to a file. You want to allow


groups and users to read, and write files in that directory, as well as create new files
into the directory . Such special permissions can be give using ACL.

Syntax to apply ACL:- setfacl –option argument file/dir name


Options:
Sr No Option Use
1. -m Modify an ACL
2. -x Removes an ACL
3 -R Recurses into
subdirectories

 To assign ACL permissions to particular user:


Syntax :- #setfacl –m u:username:permissions file/directory name
Ex- #setfacl –m u:john:rwx file/dir name

 To verify ACL:- #getfacl file/dir name

When you check for the permissions it is showing a + sign after normal permission,
that indicate that ACL is applied on directory.

 To assign ACL permissions to particular group:


Syntax :- #setfacl –m g:groupname:permissions file/directory name
Ex- #setfacl –m g:john:rw file/dir name

 Removing ACL for a particular user


Syntax:- #setfacl –x u:username dir name
Ex- #setfacl –x u:john dir name

 Removing all ACL permission from a file or directory


Syntax:- #setfacl –b dir name
Ex:- #setfacl –b dir name
 Umask:

Umask will decide that what should be the default permissions for a file and directory
when it is created.

The default umask value is 0022

 To check default umask :- #umask


 Calculation of default permissions for file & directory:-

Maximum full permissions for a file is 666 at the time of creating and for directory is 777

Full permission for file is 666(rw-rw-rw-)


Default value of umask is - 022(----w--w-)
Default permissions for a file is 644 (rw-r--r--)

Full permission for dir is 777(rwxrwxrwx)


Default value of umask is - 022(----w--w-)
Default permissions for a dir is 755 (rwxr-r-xr-x)

 Modifying umask value:-

# umask 002

The Modified default permission for a file will be 666-002=644 i.e. rw,rw,r and for
the directory it will be 777-002=775 i.e. rwx,rwx,r-x
1. How can you create a user without useradd command
Ans. Create home directory manually by the name of user and copy /etc/skel direcoty contents
in it. Now edit /etc/passwd and add user info manually, like uname uid gid home dir etc.

2. What is the default permission on user's home directory?


Ans: 700

3. What is the difference between .bash_profile and .bashrc?


Ans: Every time you login to a Linux (Red Hat) machine .bash_profile file is executed
But In case you are already logged in and you open a new terminal then .bashrc file is
executed

4. What is the command to create a user with a pre defined uid, shell and home directory?
Ans : useradd -m -d /path/to/home -s /bin/bash -u 550 deepak

5. Explain each field of /etc/passwd


Ans: deepak:x:512:512:User:/home/deepak:/bin/bash
1st field: username
2nd field: x tells that an encrypted password is stored in /etc/shadow
3rd field: uid
4th field: gid
5th field: Description
6th field: home directory
7th field: default login shell

6. How to change primary group for any user?


Ans: usermod -g groupname username

7. If I delete a user, does it's home directory gets deleted? If not then what is the command to
delete the home directory of user along with the user
Ans: No.
# userdel -r username

8. Name any 3 files which are automatically created inside any user's home directory when a
user is added
Ans: bashrc, .bash_profile, .bash_history
9. What is the command to view all the currently logged in users?
Ans - w

10. What is the command to change and view the expiry date for any user?
Ans: chage

11. What are the details you get with finger command?
Ans: Login Details
Mail
Home directory
Last login

12. How can you give a normal user all the root level privileges?
Ans: Add the user to wheel group and uncomment the wheel group line in sudoers file
Give the user all command permission in sudoers
How to give normal user root privileges using sudo in Linux/Unix

13. Name any 3 groups of which root is a member by default


Ans: root
bin
daemon
sys
adm
disk
wheel

14. How can you give sudo access to any user without asking him to provide passord every
time he runs a command?
Ans: Add an extra parameter NOPASSWD in sudoers file while giving the user permission to
run root level commands 1.

15 How to create a user ? Brief with full syntax.


/usr/sbin/useradd -u uid -g gid -c " User Descriptions" -m -d "Home Directory Path" -s "Shell"
username
Example :
#useradd -u 535 -g unix -c "Unixrock blog" -m -d /export/home/unixrock -s /usr/bin/bash
unixrock
Options Description
–u UID ( From 0 to 65535 ) , 0 is reserved for root
-g Primary Group
-m Force to Create Home Directory Specified by –d and copy default skeleton files in
/etc/skel folder
-d User Home Directory Path
-c User Descriptions
-s Shell Path

16. What are the important files for User Management task?
/etc/passwd
/etc/shadow
/etc/group
/etc/default/passwd
/etc/default/login

17. Describe the /etc/passwd fields ?


/etc/passwd file is having 7 fields
username:password:uid:gid:comment:home-directory-path:login-shell
Example :
unixrock:x:535:121:Unixrock blog:/export/home/unixrock:/usr/bin/bash

18. Describe the /etc/shadow file fields ?


/etc/shadow file is having 9 fields
loginID:password:lastchg:min:max:warn:inactive:expire:reserved
Example:
unixrock:R1EbI61VDyM2I:15995:7:91:7:::

19. Describe the /etc/group file fields ?


/etc/group file is having 4 fields
groupname:group-pwd:GID:user-list
Example:
unixrock::121:unixrock, raj

20. What is the different between "su UserID" and "su - UserID" ?
"su UserID" - Doesn't check the PATH and Current Working Directory
"su - UserID" - Load the User's Profiles (PATH/Current Working Directory)
21. Default permission of Passwd/Shadow/Group files ?
/etc/passwd 644
/etc/shadow 644
/etc/group 400
bash-3.00# ls -ld /etc/passwd /etc/shadow /etc/group
-rw-r--r-- 1 root sys 459 Dec 10 16:32 /etc/group
-rw-r--r-- 1 root sys 18498 Dec 11 09:09 /etc/passwd
-r-------- 1 root sys 10334 Dec 11 09:35 /etc/shadow
bash-3.00#

22. Default permission of file and Directory ?


Default permission of file is 644 (666 - 022 (umask vaule))
Default permission of directory is 755 (777 -022 (umask vaule))

23. How to view the list of users who currently logged in the system ?
"who" command will show the users who logged in the current system.
The command refers /var/adm/utmpx to obtain the information.
bash-3.00# who
root pts/2 Sep 10 22:11 (192.168.10.22)
root pts/3 Dec 11 22:55 (192.168.10.22)
bash-3.00#
bash-3.00# ls -ld /var/adm/utmpx
-rw-r--r-- 1 root bin 2976 Dec 11 22:55 /var/adm/utmpx
bash-3.00# file /var/adm/utmpx
/var/adm/utmpx: data
bash-3.00#

24. How to view the User's login and logout details ?


"last " command will show the users login and logout details.
The command refers /var/adm/wtmpx to obtain the information.
bash-3.00# last
root pts/3 192.168.10.22 Wed Dec 11 22:55 still logged in
root sshd 192.168.10.22 Wed Dec 11 22:55 still logged in
root pts/2 192.168.10.22 Tue Sep 10 22:11 still logged in
root sshd 192.168.10.22 Tue Sep 10 22:11 - 22:55 (92+00:43)
reboot system boot Tue Sep 10 22:03
reboot system down Fri Sep 6 17:59
wtmp begins Tue Aug 13 01:32
bash-3.00#
bash-3.00# ls -ld /var/adm/wtmpx
-rw-r--r-- 1 adm adm 68820 Dec 11 22:55 /var/adm/wtmpx
bash-3.00# file /var/adm/wtmpx
/var/adm/wtmpx: data
bash-3.00#

25. How to view details information about the User?


"finger username" will show the details about the user.
bash-3.00# finger unixrock
Login name: unixrock
Directory: /home/unixrock Shell: /bin/sh
Never logged in.
No unread mail
No Plan.
bash-3.00#

26. How to check Primary and Secondary Group of One User ?


"id -a username" will show the user's Primary and Secondary groups.
FYI, One User can be added in 15 no; of Secondary groups, But Only one Primary Group.
bash-3.00# id -a unixrock
uid=100(unixrock) gid=1(other) groups=1(other)
bash-3.00#
gid - Primary Group
groups - Secondary Group

27. How to rename the existing User ID ?


# usermod -l <newname> <oldname>
bash-3.00# usermod -l unixrock_new unixrock
UX: usermod: unixrock name too long.
bash-3.00# grep -i unixrock /etc/passwd
unixrock_new:x:100:1::/home/unixrock:/bin/sh
bash-3.00#

28. How to lock the User Account ?


# passwd -l UserID
bash-3.00# passwd -s unixrock
unixrock PS
bash-3.00# passwd -l unixrock
passwd: password information changed for unixrock
bash-3.00# passwd -s unixrock
unixrock LK
bash-3.00#

29. How to unlock the User Account?


# passwd -u <UserID>
bash-3.00# passwd -s unixrock
unixrock LK
bash-3.00# passwd -u unixrock
passwd: password information changed for unixrock
bash-3.00# passwd -s unixrock
unixrock PS
bash-3.00#

30. How to make the user account as non-expriry ?


# passwd -x -1 <userID>
bash-3.00# passwd -s unixrock
unixrock PS 12/11/13 7 91 7
bash-3.00#
bash-3.00# passwd -x -1 unixrock
passwd: password information changed for unixrock
bash-3.00#
bash-3.00# passwd -s unixrock
unixrock PS
bash-3.00#

31. How do we set force passwd change for User's first login ?
# passwd -f <UserID>
bash-3.00# passwd -s unixrock
unixrock PS 12/11/13 7 91 7
bash-3.00#
bash-3.00# passwd -f unixrock
passwd: password information changed for unixrock
bash-3.00#
bash-3.00# passwd -s unixrock
unixrock PS 00/00/00 7 91 7
bash-3.00#

32. How to delete the User ID ?


# userdel <UserID> or # userdel -r <UserID>

-r option will delete the User's Home directory too.

33. Type of SHELLs ? What is initialization file for those SHELLS ?

/bin/bash - Bourne Again shell


/bin/csh - C shell
/bin/ksh - Korn shell
/bin/tcsh - TC shell
/bin/zsh - Z shell

Bourne /etc/profile $HOME/.profile /bin/sh /etc/skel/local.profile


Korn /etc/profile $HOME/.profile /bin/ksh /etc/skel/local.profile
$HOME/.kshrc
C /etc/.login $HOME/.cshrc /bin/csh /etc/skel/local.cshrc
$HOME/.login /etc/skell/local.login

34. How to Check the User's Crontabs? How to allow the User to access the Cron?
# crontab -l <username>
or
# ls -ltr /var/spool/cron/crontabs/

/etc/cron.d/cron.allow : If the file exists, the users can use crontab whoever listed in that file.

35. How to check User's Present Working Directory Path? How to check the Obsolete Path of
running process?
Find the Present Working Directory Path
# pwd

Find the Obsolete Path of Process.


# pwdx
 Job Scheduling:-
In any operating system, it is possible to create jobs that you want to reoccur. This
process known as job scheduling, is usually done based on user-defined jobs.
The importance of the job scheduling is that the critical tasks like taking backups
which the clients usually wants to be taken in nights, can easily be performed
without the intervention of the administrator.
1) at jobs :- “at” is used to schedule the job for a particular time or interval, in other
words it used only for one time or only for one interval.
Syntax for at jobs:
#at <time>
Task
Ctrl+d to save it.
 To check the list of at jobs :- #at –l or #atq
 To remove a job
1) Check the job id
2) To remove a job the syntax is
#atrm <job id>
Ex- #atrm 17

 Restricting a user from using at jobs:-


1) Add user names to /etc/at.deny
2) To allow only few out of many users remove at.deny and create /etc/at.allow and
add user names who are allowed to use at jobs in it.
3) If both at.allow and at.deny exits, then at.allow will have higher priority.
4) If neither at.allow nor at.deny exits, then only root can use at jobs.
2)Crontab jobs:- See above to images for crontab examples

Crontab format:-

Minute Hour Day of Month Month Day of week Command

(0-59) (0-23) (1-31) (1-12) (0-7) Sunday=0 or 7

 Crontab commands:-
 Edit your crontab file, or create one if it doesn’t already exist :- #crontab –e
 Display your crontab file :- # crontab -l
 Remove your crontab file.:- #crontab -r
 #crontab –u :- If combined with –e , edit particular user’s crontab file and if
combined with –l , display a particular user’s crontab file, if combined with –r,
deletes a particular user’s crontab file.
 Ex:- #crontab –u username –e

 Restricting a user from using cron jobs:-


1) Add user names to /etc/cron.deny

2) To allow only few out of many users remove cron.deny and create /etc/cron.allow

and add user names who are allowed to use at jobs in it.

3) If both cron.allow and cron.deny exits, then cron.allow will have higher priority.

4) If neither cron.allow nor cron.deny exits, then only root can use at jobs.
 Process Management:-
Execution of instructions called as process. Process management is done by
kernel.
Process information is stored in /proc directory. Each process has its own unique
process id (pid) & some processes also have parent process id (ppid).
Ex- systemd process have pid 1 (acco to RHEL 7)
init process have pid 1 (acco. to RHEL 6 )

 To monitor the process ps command is used :- ps command gives the running


processes of terminal.
Syntax:- #ps
 To see all offline processes of system :- #ps –aux or # ps –elf
 To see the process running by the logged in user:- #ps –u <user name>

Signals in Linux:

Signs are a way of sending simple messages to processes Every signal has unique
signal name, an abbreviation that begins with SIG (SIGINT for interrupt signal, for
example).

Signals can be generated by the process itself, or they can be sent from one
process to another. A variety of signals can be generated or delivered, and they have
many uses for programmers.

To see a complete list of signals in the Linux environment, uses the command kill –l
The most common signals used are:
 1 for reloading the process
 9 for killing the process
 15 for Terminating the process
 20 for stopping the process
To kill the process completely:- # kill -9 pid

 Background process:- To run a process at background just add “&” at end of the
command

Syntax:- <command > &

Ex- # tar –cvf storage.tar /etc &

 To check background processes:- #bg or # jobs


 Foreground process:- # fg %job no
 To kill background process:- # kill -9 %job no

Likely “ps” top too displays user, pid, cpu, memory, commands etc.
NICE and RENICE
Now I would like to introduce you to LEVELS of a process. LEVEL..?? What does a LEVEL
means..?
In Linux system every process has a priority which decides that which of the process should get
more cpu and memory utilization. It’s all about giving priority level to the processes and these
levels are known as NICE levels. Whenever a system starts kernel set nice levels to the default
system processes.
The range of nice levels is from -20 to +19. Negative nice value means process has more
priority and positive value means process has lower priority and zero means priority is neutral.
We can see nice levels of processes by “TOP” command. See the 4th column of above image. As
we can see there default NICE levels of the process.
The process of changing default NICE level to a new NICE level is called “RENICE”. Let see how
to change default nice level.
Run top command and select the process, whose nice level is to be changed. We can see
Firefox process in the image given below

Firefox NICE level is “zero”, now we are going to change it to “-5“ (-5 is taken just for an
example, you need to ne choose as per your requirement)

# pgrep firefox (shows PID in output, For example “3802”)

Or

# pidof firefox (shows PID in output, For example “3802”)

# renice -n -5 3802

The above command “renice” is use to change nice priority of a process. “ -n ” option means
the level from

“-20” to “+19”
In the above image gripped PID of Firefox and reniced it using renice command. See the image
below to check the new NICE value of Firefox process.
 Monitoring the process using top command

TOP – Like ps command TOP also displays running process output but “top” utility
displays output in runtime format as we see process in “TASK MANAGER” program in
windows. TOP utility shows output in every three seconds of interval. We can change
its refresh time either reducing or extending refresh interval.

Likely “ps” top too displays user, pid, cpu, memory, commands etc.
 Tar:-
‘tar’ is used to create archive files. It can pack files or directories into a ‘tar’ file. It is
like WinZip in Windows, without much compression.

 To create a tar file:- # tar –cvf <tar file name> <source file> where c-create, f-
filename

Ex:- #tar –cvf storage.tar /etc /bin

 To see the contents of tar file without extracting them:-


# tar –tvf <tar file name>
Ex- # tar –tvf storage.tar
 To add some files to tar file:- #tar –rvf <tar file name> file1,file2

 Compression of tar file:-


3 types:
Sr Type Option Extension
No
1. Gzip -z .gz
2. bzip2 -j .bz2
3. Xz -J .xz

 To compress a tar file:-


# tar –cvzf <com filename.gz> <tar file name> OR
# gzip filename

 To extract a file:-
#tar –xvf filename OR
#gunzip filename
Method of backups
There are different types of backups we can use. Every method has its own importance.
It is not only about creating backup of files and directories but to backup filesystem too.
If we talk about company level some of them use backup tools where some of them use
these traditional methods listed below. These are the following methods of backup we
are going to learn

 tar (Tape Archive)


 gzip/gunzip
 bzip2/bunzip2
 dump & restore
 cpio
 dd
We will study dump, restore, cpio and dd later.

TAR
The tar utility gets its name from Tape ARchive. Tar is the most common method of
backup. Syntax:-
“ tar -cvf <destination-directorybackupname.tar> <source data> “
Options “-cvf”
 c for create backup
 v for verbose or visual mode
 f means forcefully
I will show you by taking backup of “/etc” directory.

In above image I created backup of “/etc” in “/root” directory with name etc.tar.

Other type of compression is “.tar.gz”. In order to create “gz” compression use “-cvzf”
along above command. “.tar.gz” will reduce the size more than “.tar” compression. You
must specify the extension as “.tar.gz” to do “gz”(gzip) compression.

Third type of compression is “bzip”. Bzip compress more than “.tar” & “.tar.gz”. command
will be same to do compression, the change is, use “-cvjf” option and backup file
extension will be “.tar.bz”.
To check size of compressed data, command is “du -h”. “du” stands for DISK USAGE. “-
h” option is use to see output in human readable format like in “kb, mb, gb”.

Size of “/etc” directory is 34mb but after compression it is clearly seen the difference of
compression size in above image.

We learnt three types of compression. Now we are going to extract these compressed
files. Extraction is easy, we will use “x” n place of “c” in options. Have a look.
In all above three images the command is same where in options “x” is used which means

to extract. In last two images I specified options “z” & “j”, because we are extracting

“gzip” & “gzip” compression which need these option. Next thing you must know that

wherever you want to extract these files just go to that directory and after that run

command to extract. In above images I didn’t change the directory which means these

files will be extracted in current directory.


1. Create tar Archive File
The below example command will create a tar archive file sanbais-14-09-12.tar for a
directory /home/sanbais in current working directory. See the example command in action.
# tar -cvf sanbais-14-09-12.tar /home/sanbais/

/home/sanbais/
/home/sanbais/cleanfiles.sh
/home/sanbais/openvpn-2.1.4.tar.gz
/home/sanbais/sanbais-14-09-12.tar
/home/sanbais/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
/home/sanbais/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

Let’s discuss the each option we have used in the above command for creating tar archive file.

 c – Creates a new .tar archive file.


 v – Verbosely show the .tar file progress.
 f – File name type of the archive file.
2. Create tar.gz Archive File
To create a compressed gzip archive file we use the option as z. For example the below
command will create a compressed MyImages-14-09-12.tar.gz file for the
directory /home/MyImages. (Note : tar.gz and tgz both are similar).
# tar cvzf MyImages-14-09-12.tar.gz /home/MyImages
OR
# tar cvzf MyImages-14-09-12.tgz /home/MyImages

/home/MyImages/
/home/MyImages/Sara-Khan-and-model-Priyanka-Shah.jpg
/home/MyImages/RobertKristenviolent101201.jpg
/home/MyImages/Justintimerlakenaked101125.jpg
/home/MyImages/Mileynudephoto101203.jpg
/home/MyImages/JenniferRobert101130.jpg
/home/MyImages/katrinabarbiedoll231110.jpg
/home/MyImages/the-japanese-wife-press-conference.jpg
/home/MyImages/ReesewitherspoonCIA101202.jpg
3. Create tar.bz2 Archive File
The bz2 feature compress and create archive file less than the size of the gzip.
The bz2 compression takes more time to compress and decompress files as compared
to gzip which takes less time. To create highly compressed tar file we use option as j. The
following example of command will create a Phpfiles-org.tar.bz2 file for a
directory /home/php. (Note: tar.bz2 and tbz is similar as tb2).
# tar cvfj Phpfiles-org.tar.bz2 /home/php
OR
# tar cvfj Phpfiles-org.tar.tbz /home/php
OR
# tar cvfj Phpfiles-org.tar.tb2 /home/php

/home/php/
/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
/home/php/video_title.php
/home/php/report.php
/home/php/object.html
/home/php/video.php

4. Untar tar Archive File


To untar or extract a tar file, just issue following command using option x (extract). For
example the below command will untar the file public_html-14-09-12.tar in present working
directory. If you want untar in a different directory then use option as -C (specified directory).
## Untar files in Current Directory ##
# tar -xvf public_html-14-09-12.tar

## Untar files in specified Directory ##


# tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/

/home/public_html/videos/
/home/public_html/videos/views.php
/home/public_html/videos/index.php
5. Uncompress tar.gz Archive File
To Uncompress tar.gz archive file, just run following command. If would like to untar in
different directory just use option -C and the path of the directory, like we shown in the above
example.

# tar -xvf thumbnails-14-09-12.tar.gz

/home/public_html/videos/thumbnails/
/home/public_html/videos/thumbnails/katdeepika231110.jpg
/home/public_html/videos/thumbnails/katrinabarbiedoll231110.jpg
/home/public_html/videos/thumbnails/onceuponatime101125.jpg
/home/public_html/videos/thumbnails/playbutton.png
/home/public_html/videos/thumbnails/ReesewitherspoonCIA101202.jpg
/home/public_html/videos/thumbnails/snagItNarration.jpg
/home/public_html/videos/thumbnails/Minissha-Lamba.jpg
/home/public_html/videos/thumbnails/Lindsaydance101201.jpg
/home/public_html/videos/thumbnails/Mileynudephoto101203.jpg

6. Uncompress tar.bz2 Archive File


To Uncompress highly compressed tar.bz2 file, just use the following command. The below
example command will untar all the .flv files from the archive file.

# tar -xvf videos-14-09-12.tar.bz2


/home/public_html/videos/flv/katrinabarbiedoll231110.flv
/home/public_html/videos/flv/BrookmuellerCIA101125.flv
/home/public_html/videos/flv/dollybackinbb4101125.flv
/home/public_html/videos/flv/JenniferRobert101130.flv
/home/public_html/videos/flv/JustinAwardmovie101125.flv
/home/public_html/videos/flv/Lakme-Fashion-Week.flv
/home/public_html/videos/flv/Mileynudephoto101203.flv
7. List Content of tar Archive File
To list the content of tar archive file, just run the following command with option t (list
content). The below command will list the content of uploadprogress.tar file.

# tar -tvf uploadprogress.tar

-rw-r--r-- chregu/staff 2276 2011-08-15 18:51:10 package2.xml


-rw-r--r-- chregu/staff 7877 2011-08-15 18:51:10 uploadprogress/examples/index.php
-rw-r--r-- chregu/staff 1685 2011-08-15 18:51:10 uploadprogress/examples/server.php
-rw-r--r-- chregu/staff 1697 2011-08-15 18:51:10 uploadprogress/examples/info.php
-rw-r--r-- chregu/staff 367 2011-08-15 18:51:10 uploadprogress/config.m4
-rw-r--r-- chregu/staff 303 2011-08-15 18:51:10 uploadprogress/config.w32
-rw-r--r-- chregu/staff 3563 2011-08-15 18:51:10 uploadprogress/php_uploadprogress.h
-rw-r--r-- chregu/staff 15433 2011-08-15 18:51:10 uploadprogress/uploadprogress.c
-rw-r--r-- chregu/staff 1433 2011-08-15 18:51:10 package.xml

8. List Content tar.gz Archive File


Use the following command to list the content of tar.gz file.

# tar -tvf staging.sanbais.com.tar.gz

-rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.sanbais.com-access_log


-rw-r--r-- root/root 587 2012-08-29 18:35:12 staging.sanbais.com-access_log.1
-rw-r--r-- root/root 156 2012-01-21 07:17:56 staging.sanbais.com-access_log.2
-rw-r--r-- root/root 156 2011-12-21 11:30:56 staging.sanbais.com-access_log.3
-rw-r--r-- root/root 156 2011-11-20 17:28:24 staging.sanbais.com-access_log.4
-rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.sanbais.com-error_log
-rw-r--r-- root/root 3981 2012-08-29 18:35:12 staging.sanbais.com-error_log.1
-rw-r--r-- root/root 211 2012-01-21 07:17:56 staging.sanbais.com-error_log.2
9. List Content tar.bz2 Archive File
To list the content of tar.bz2 file, issue the following command.
# tar -tvf Phpfiles-org.tar.bz2

drwxr-xr-x root/root 0 2012-09-15 03:06:08 /home/php/


-rw-r--r-- root/root 1751 2012-09-15 03:06:08 /home/php/iframe_ew.php
-rw-r--r-- root/root 11220 2012-09-15 03:06:08 /home/php/videos_all.php
-rw-r--r-- root/root 2152 2012-09-15 03:06:08 /home/php/rss.php
-rw-r--r-- root/root 3021 2012-09-15 03:06:08 /home/php/index.php
-rw-r--r-- root/root 2554 2012-09-15 03:06:08 /home/php/vendor.php
-rw-r--r-- root/root 406 2012-09-15 03:06:08 /home/php/video_title.php
-rw-r--r-- root/root 4116 2012-09-15 03:06:08 /home/php/report.php

10. Untar Single file from tar File


To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the following command.
# tar -xvf cleanfiles.sh.tar cleanfiles.sh
OR
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh

cleanfiles.sh

11. Untar Single file from tar.gz File


To extract a single file sanbaisbackup.xml from sanbaisbackup.tar.gz archive file, use the
command as follows.
# tar -zxvf sanbaisbackup.tar.gz sanbaisbackup.xml
OR
# tar --extract --file=sanbaisbackup.tar.gz sanbaisbackup.xml

sanbaisbackup.xml

12. Untar Single file from tar.bz2 File


To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following
option.
# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php
OR
# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php

/home/php/index.php

13. Untar Multiple files from tar, tar.gz and tar.bz2 File
To extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the
below command will extract “file 1” “file 2” from the archive files.
# tar -xvf sanbais-14-09-12.tar "file 1" "file 2"

# tar -zxvf MyImages-14-09-12.tar.gz "file 1" "file 2"

# tar -jxvf Phpfiles-org.tar.bz2 "file 1" "file 2"

14. Extract Group of Files using Wildcard


To extract a group of files we use wildcard based extracting. For example, to extract a group of
all files whose pattern begins with .php from a tar, tar.gz and tar.bz2 archive file.
# tar -xvf Phpfiles-org.tar --wildcards '*.php'

# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'

# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'

/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
15. Add Files or Directories to tar Archive File
To add files or directories to existing tar archived file we use the option r (append). For
example we add filexyz.txt and directory php to existing sanbais-14-09-12.tar archive file.
# tar -rvf sanbais-14-09-12.tar xyz.txt

# tar -rvf sanbais-14-09-12.tar php

drwxr-xr-x root/root 0 2012-09-15 02:24:21 home/sanbais/


-rw-r--r-- root/root 15740615 2012-09-15 02:23:42 home/sanbais/cleanfiles.sh
-rw-r--r-- root/root 863726 2012-09-15 02:23:41 home/sanbais/openvpn-2.1.4.tar.gz
-rw-r--r-- root/root 21063680 2012-09-15 02:24:21 home/sanbais/sanbais-14-09-12.tar
-rw-r--r-- root/root 4437600 2012-09-15 02:23:41 home/sanbais/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
-rw-r--r-- root/root 12680 2012-09-15 02:23:41 home/sanbais/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
-rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt
drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/
-rw-r--r-- root/root 1751 2012-09-15 03:06:08 php/iframe_ew.php
-rw-r--r-- root/root 11220 2012-09-15 03:06:08 php/videos_all.php
-rw-r--r-- root/root 2152 2012-09-15 03:06:08 php/rss.php
-rw-r--r-- root/root 3021 2012-09-15 03:06:08 php/index.php
-rw-r--r-- root/root 2554 2012-09-15 03:06:08 php/vendor.php
-rw-r--r-- root/root 406 2012-09-15 03:06:08 php/video_title.php

16. Add Files or Directories to tar.gz and tar.bz2 files


The tar command don’t have a option to add files or directories to a existing
compressed tar.gz and tar.bz2archive file. If we do try will get tbe following error.
# tar -rvf MyImages-14-09-12.tar.gz xyz.txt

# tar -rvf Phpfiles-org.tar.bz2 xyz.txt

tar: This does not look like a tar archive


tar: Skipping to next header
xyz.txt
tar: Error exit delayed from previous errors
17. How To Verify tar, tar.gz and tar.bz2 Archive File
To verfify any tar or compressed archived file we use option as W (verify). To do, just use the
following examples of command. (Note : You cannot do verification on a compressed ( *.tar.gz,
*.tar.bz2 ) archive file).
# tar tvfW sanbais-14-09-12.tar

tar: This does not look like a tar archive


tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: VERIFY FAILURE: 30740 invalid headers detected
Verify -rw-r--r-- root/root 863726 2012-09-15 02:23:41 /home/sanbais/openvpn-2.1.4.tar.gz
Verify -rw-r--r-- root/root 21063680 2012-09-15 02:24:21 /home/sanbais/sanbais-14-09-12.tar
tar: /home/sanbais/sanbais-14-09-12.tar: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root 4437600 2012-09-15 02:23:41 home/sanbais/phpmyadmin-2.11.11.3-
1.el5.rf.noarch.rpm
tar: /home/sanbais/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm: Warning: Cannot stat: No such file or
directory
Verify -rw-r--r-- root/root 12680 2012-09-15 02:23:41 home/sanbais/rpmforge-release-0.5.2-
2.el5.rf.i386.rpm
tar: /home/sanbais/rpmforge-release-0.5.2-2.el5.rf.i386.rpm: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt
Verify drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/

18. Check the Size of the tar, tar.gz and tar.bz2 Archive File
To check the size of any tar, tar.gz and tar.bz2 archive file, use the following command. For
example the below command will display the size of archvie file in Kilobytes (KB).
# tar -czf - sanbais-14-09-12.tar | wc -c
12820480

# tar -czf - MyImages-14-09-12.tar.gz | wc -c


112640

# tar -czf - Phpfiles-org.tar.bz2 | wc -c


20480
Tar Usage and Options
 c – create a archive file.
 x – extract a archive file.
 v – show the progress of archive file.
 f – filename of archive file.
 t – viewing content of archive file.
 j – filter archive through bzip2.
 z – filter archive through gzip.
 r – append or update files or directories to existing archive file.
 W – Verify a archive file.
 wildcards – Specify patters in unix tar command.
Partitions:-
The first sector of the HARD DISK is known as MBR (Master Boot Record). Master Boot Record
contains the partition table of the partitions exist in hard disk. We know the size of partition table
is 64 Bytes. A single partition consumes 16 bytes of this 64 bytes, so we can create maximum four
partitions of a hard disk .

In the beginning due to size of MBR we were bounded to create maximum four partitions.
These partitions are called as primary partitions. This concept is old, things are different now.
All thanks to the extended partition. Extended partition is the just modified parimary partition,
modified in a way, that it react like a container that can hold a lot partitions that we called
logical partitions.
There are two mostly used partitioning scheme so called as
 MBR Partitioning Scheme
 GPT Partitioning Scheme
MBR Partitioning Scheme

At the beginning of the disk there is a lable called as Master Boot Record, this record contains
partition table.

After exploring the MBR we can see the MBR lable and partition table having four partition.

First three partitions are primary partitions. Fourth partition is extended partition. This

extended partition contains further partitions and those partitions are known as logical

partitions. See the below image to check what actually an extended partition is.
As you can see in above image there are three primary partitions fourth partition (extended) is

having same amount of space but contains numbers of logical partitions in it. For example if

you want to make FIVE partitions then first three will be primary, fourth partition will be

extended and two partitions further will be created, means total number of partitions will be

SIX
Partitions

Hard disks and storage devices are normally divided up into smaller chunks called partitions. A

partition is a way to compartmentalize a disk. Different parts of it can be formatted with

different file systems or used for different purposes. For example, one partition could contain

user home directories while another could contain system data and logs. If a user fills up the

home directory partition with data, the system partition may still have space available.

Placing data in two separate file systems on two separate partitions helps in planning data

storage. Storage devices are represented by a special file type called block device. The block

device is stored in the /dev directory. In Red Hat Enterprise Linux, the first SCSI, PATA/SATA, or

USB hard drive detected is /dev/sda, the second is /dev/sdb, and so on. This name represents

the whole drive. The first primary partition on /dev/sda is /dev/sda1, the second partition is

/dev/sda2, and so on.


To list all partitions in linux system:-

# fdisk -l

As you can see in above image, the highlighted text shows the hard disk (sda) of size 21GB. The

table is the list of all partitions having in system. Keep in mind that “sda1” would be the first

partition always. Third and Fourth column in the above image are the starting sector and

ending sector of the partition. ID denotes the code of the partition, means which type of

partition category it is. Last column shows the type of partition.

In our next segment we will see how to create a partition in a Linux system by command line.
Steps to create a partition

To create a partition run following command:-


# fdisk /dev/sda
Here sda is your hard disk. to check you hard disk name run “fdisk –l”
Above command will open a prompt. In this prompt press “m” and it will open the list of
options you can do to a partition.

 “n” to create a new partiton


 “d” to delete a new partition
 “w” to save changes we made last.
 “q” to quit changes we made last to a partition.
 “p” to print the partition table.
# fdisk /dev/sda to create the partition of sda device. Now it will ask you to give the
command, you need to press “n” to create a new partition. It notified ”Adding Logical Partition
7” for there are already 6 partitions created where first three are primary and fourth one is
extended and rest will be logical.

Partprobe command inform and update the operating system that partition table has
changed.
After creation of new partition we need to format it and mount it on a directory and that
directory so called mount point.
# mkfs.xfs /dev/sda7
# blkid (block ID) this command shows a unique hexadecimal code of each partition known as
“UUID”. Each code is the unique identity for each partition. Copy this “UUID” of formatted
partition above.
# mkdir /new
A directory is created in “/” with name “new”. You can create it anywhere. Now I am going to
mount the partition “/dev/sda7” on “/new” directory. Before proceeding you must know
about the types of mounting. Here they are:-
 Temporary mounting
 Permanent mounting
Temporary mounting means to mount the device or partition temporarily which will be
unmounted after rebooting the system. For example we mount our cd ,dvd, or usb device on a
directory, it will be unmounted automatically after reboot.

Permanent mounting means we want to mount the device or partition permanently on


system. In order to mount permanently, we have to add following entries in “/etc/fstab” file.
This file contains list of all partitions. To add a new partition in the “fstab” file we need to add
these following entries.
1st column – Here we need to define the “UUID” of the partition to be mounted or we can
direct type “/dev/sdax”. To give UUID is good practice.
2nd column – we add mount point here, where we want to mount the partition.
3rd column – here we define the file system from which we formatted the partition.
4th column – type “defaults” here which means to apply default options on this partition like
rw, suid, dev, exec, auto, nouser, and async.
5th column - The 5th column in /etc/fstab is the dump option. Dump checks it and uses the
number to decide if a file system should be backed up. If it's zero, dump will ignore that file
system. Generally we use zero to disable it.
6th column – The 6th column defines if partition should be checked by “fsck” while booting or
not. Zero means do not check & rest 1, 2 are the priority order of file system checking.
So we are now familiar with the entries to be given in “/etc/fstab”. Let us proceed to mounting
process.

To mount the partition temporarily, use following command:-


# mount “/dev/sda7” /new
Above command will mount the sda7 partition on /new directory. We are using sda7 as an
example here your partition could be different.
To mount the partition permanent on your system first run “blkid” command and copy the
“UUID” of “/dev/sda7”. Now open /etc/fstab file and paste the UUID. In second column define
mount point /new, in third column define filesystem xfs, in fourth column define the value
“defaults”, in 5th and 6th column define 0 & 0. It will look like this:-
Copy the UUID as highlighted. Have a look on “/etc/fstab” file.

See the last line, here I add the sda7 entry.


Now save the file and run command:-
# mount -a
This command mount the entry of “/etc/fstab” file. To check if partition mounted or not, run:-
# df -h
See the below image to see output.
Sda7 is highlighted and it is mounted on /new.

In your case partition number of partitions can be different. Always keep in mind that if you
have only three partitions and you are going to make fourth one then you must create
extended partition first. To do so follow the below instruction:-
# fdisk /dev/sda
Press “n”
Press “e” to define next partition as extended
Now hit enter for the first sector of the fourth partiton
Again press enter to take whole free space in extended partition
Then press “w” to save and run command partprobe.

LVM – Logical Volume Manager

Standard partition scheme has restriction on reducing or extending the size of a partition, What

If we want to reduce or increase the size of a partition in future.


LVM (Logical Volume Manager) is the tool for managing volume by modifying size of volumes.
Steps to create LVM partition:-

Create a partition of hard disk (sda). You can create a small partition or you can use
whole hard disk as single
# fdisk /dev/sda
Press “n” to create a new partition

Select primary partition by pressing “enter”

Press “enter” again to select default first sector of partition

Press enter to allocate complete space to this partition or you can create particular size
partition by giving size through +sizeM or +sizeG. Example: +500M or +2G

Now press “t” (t means to change the type of partition, for we are making partition as
LVM type we have to set partition type from standard to LVM). After pressing “t“ It will
ask for which partition number will be change, press “enter” or “1” for default . Now
press “L”, it will show list of codes. Code means every partition has a type. Generally we
are familiar with standard type of partition which holds the code “8e”. Now after
pressing “L” we will see the code of LVM type and that will be “8e”, type 8e and press
enter. It will change the partition type from standard type to LVM type.
pvdisplay – to display PV
Press “w” to save above.
vgdisplay – to display VG
# partprobe (To update partition table in system)
lvdisplay – to display LV
#pvcreate /dev/sda1 (To initialize the partition as physical volume)

#vgcreate myvg /dev/sda1 (this command will create a volume group by name
“myvg” we will add more PVs in “myvg” Volume Group)

# lvcreate –L +sizeG or +sizeM –n lvname vgname (Now our task will be to create
a Logical Volume)

In above command lvcreate command is use to create a new “LV”, “-L” options
specifies size “-n” option
specifies name of the LV to be created in this is example I am giving “mylv” and then we
need to specify VGname from which we are creating LV.

After creation of Logical Volume, we will format it with ext4 or xfs file system by:-

# mkfs.ext4 /dev/myvg/mylv

#blkid (It will show UUID, copy the UUID )

#mkdir /lvm (Now you need to create a directory to mount the above created LV on it)

#vim /etc/fstab (Open the “/etc/fstab” file for permanent mounting )

Add following lines

“Paste the copied UUID” /lvm ext4 defaults 0 0

Save the file and run command:-

# mount –a (to mount the given entry in fstab file) and # df –h (to check if partition
is mounted).

Suppose if there is no space left in volume group for all space has been allocated and if
we want to extend the size of volume group, we will add a new PV(Physical Volume) to
extend the size of VG(Volume Group)
To extend VG we need a new PV. In above figure I add a new PV “sdb1” into the VG and then
size of VG extended to 500gb more. Now the total size is 1TB. Now steps to extend VG

Create a partition of “sdb” and set its type to LVM by giving code 8e

Now convert this partition into physical volume to initialize

# pvcreate /dev/sdb1

#vgextend myvg /dev/sdb1

#vgdisplay

When you run command vgdisplay you will see a row containing PE. PE stands for Physical
extents. Its size is 4mb. Physical extents is use to make a LOGICAL VOLUME of fixed size. Let
see how:-

Suppose we are creating a lv of 100 PE(Physical extents)

#lvcreate –l 100 -n mylv myg

Above command will create a LV of exact 400mb size because we have specify number of
extents by “-l” (Small “l”)

Options, here we defined 100 counts and each extent size is 4 mb so 100*4mb = 400mb.

Moving further. Now we will see how to change default size of physical extent.

Default size of an individual extent is 4mb. If we wish to increase its size, yes we can. Keep in
mind that extent size will always be the power of 2 like 2,4,8,16,32,64. In next example I will
set the extent size to 8mb from default 4mb size.

Command will be:

# vgcreate –s 8m myvg /dev/sda1

# vgdisplay (It will show the complete info of VG along physical extent. Here we will see the
default size has changed from 4mb to 8mb)
Our next segment is about extending and reducing size of LV.

First we will learn how to extend a LV.


Suppose I have a LV of size 300Mb, I want to extend it to 500Mb. Command will be

# lvextend –L 500M /dev/VG/LV (Now new size will be 500Mb)

Here I gave new size of “500Mb” it means lv will be of exact 500Mb size. If we want to add size
instead of increasing by fixed size as I did in above example, we will do it as following

# lvextend -L +500M /dev/VG/LV (Now new sie will be 800Mb)

Now run command to resize the increased size by :

#resize2fs -f /dev/VG/LV (It will resize the size)

#df –h (to check the size of LV )

In this portion we will learn how to reduce an lvm , means to reduce size of LVM

Suppose I have a LV of size 500Mb and I am going it reduce it to 300Mb

First unmounts the LV

# umount /dev/VG/LV

Now run e2fsck command on LV, this command is used for force checking if file system is clean
or not.

# e2fsck -f /dev/VG/LV

# resize2fs -f /dev/VG/LV 300M

Above command will defragment the partition into 300Mb and free up space.

#lvreduce -L 300M /dev/VG/LV

# Mount -a
LVM Interview Questions
Flexible capacity
When using logical volumes, file systems can extend across multiple disks, since you can
aggregate disks and partitions into a single logical volume.
Resizable storage pools
You can extend logical volumes or reduce logical volumes in size with simple software
commands, without reformatting and repartitioning the underlying disk devices.
Online data relocation
To deploy newer, faster, or more resilient storage subsystems, you can move data while your
system is active. Data can be rearranged on disks while the disks are in use. For example, you
can empty a hot-swappable disk before removing it.
Convenient device naming
Logical storage volumes can be managed in user-defined and custom named groups.
Disk striping
You can create a logical volume that stripes data across two or more disks. This can
dramatically increase throughput.
Mirroring volumes
Logical volumes provide a convenient way to configure a mirror for your data.
Volume Snapshots
Using logical volumes, you can take device snapshots for consistent backups or to test the
effect of changes without affecting the real data.

Q:1 Why is LVM is required ?


Ans: LVM stands for Logical Volume Manager , to resize filesystem’s size online we required
LVM partition in Linux. Size of LVM partition can be extended and reduced using the lvextend &
lvreduce commands respectively.

Q:2 How to increase the size of LVM partition ?


Ans: Below are the Logical Steps :
– Use the lvextend command (lvextend -L +100M /dev/<Name of the LVM Partition> , in this
example we are extending the size by 100MB.
– resize2fs /dev/<Name of the LVM Partition>
– check the size of partition using ‘df -h’ command
Q:3 How to reduce or shrink the size of LVM partition ?
Ans: Below are the logical Steps to reduce size of LVM partition :
-Umount the filesystem using umount command,
-use resize2fs command , e.g resiz2fs /dev/mapper/myvg-mylv 10G
-Now use the lvreduce command , e.g lvreduce -L 10G /dev/mapper/myvg-mylv

Above Command will shrink the size & will make the filesystem size 10GB.

Q:4 How to create partition from the raw disk ?


Ans: Using fdisk utility we can create partitions from the raw disk. Below are the steps to create
partition from the raw disk :
– fdisk /dev/hd* (IDE) or /dev/sd* (SCSI)
– Type n to create a new partition
– After creating partition , type w command to write the changes to the partition table.

5. What does the last two sections define in fstab file?

6. What is the difference between ext2, ext3, ext4 and xfs?

7. How to see how many VG’s are there?

8. Describe the process for adding a new hard disk to Linux LVM to grow /home?

9. What is PE meaning in LVM and what is the defualt size of a PE.

11. How Many primary partition is possible in a storage.

12. why not more then 4 primary partitions are possible to create

13. What is swap partition and Ho

1.Is it possible to increase the logical volume on fly ?


Answer: Yes. We can increase the logical volume without umount it.

2.How to reduce the logical volume ? is it possible to reduce on fly ?


Answer: No.You can’t reduce the logical volume on fly. Here is the steps to reduce the logical
volume on redhat Linux.

Un-mount the filesystem


Run e2fsck on the volume device
Reduce the Filesystem.(resize2fs)
Reduce the logical Volume(lvreduce)
Mount the filesystem back for production.

The detailed step by step guide is available here.

3.How to do you scan the new LUN or disk for LVM physical volume ?
Answer:Use “pvscan” to scan existing physical volume from newly connected SAN or DISKS.

4.How to scan disks for existing volume group ?


Answer:Use “vgscan” to scan existing volume group from newly connected SAN or DISKS.
But you should use “pvscan” prior to executing this command.

5.How to scan a logical volume from exising volume group?


Answer: lvscan

6.How to stop the logical volume ? or deactivate the logical volume ?


Answer: “lvchange -an /dev/vg_name/lv_name”

7.How to activated the logical volume which in deactivated state ?


Answer: “lvchange -ay /dev/vg_name/lv_name” .

8.How to disable the volume group ? or Deactivate the volume group ?


Answer:”vgchange -an volume_group_name” .

9.How to enable the volume group ? or Activate the volume group ?


Answer:”vgchange -ay volume_group_name” .

10.How do you find that what are the disks are used for logical volume mirroring ?
Answer: use “lvs -a -o +devices”
11. What are steps to perform in order to increase the logical volume on fly ?
Answer:

Extend the logical volume


Increase the Filesystem size
Verify the status using df command or lvs command.

12.How to list the imported volume groups ?


Answer: Use “vgs” command to display the imported volume group.

13.How to list the available logical volumes on the system?


Answer: Use “lvs” command to list the available logical volumes on the system.

14.How to list the available physical volumes in LVM?


Answer: Use “pvs” command to list the available physical volumes.

15.How to see the detailed volume group information ?


Answer: Use “vgdisplay vg_name”

16.How to see the detailed logical volume information ?


Answer: Use “lvdisplay /dev/vg_name/lv_name”

17.How to see the detailed physical volume information ?


Answer: Use “pvdisplay /dev/disk_name” Ex: pvdisplay /dev/sde

18.How to rename volume Group ? can we rename the VG on fly ?


Answer:Yes.Its possible to rename the volume group on fly.But the mounted volumes will not
reflect the same unless you re-mount the volume with new VG name.
Need to update the /etc/fstab with new VG name to mount the volumes across the system
reboot.

19.How to take a LVM configuration backup ?


Answer:Use “vgcfgbackup vg_name” to take the latest configuration backup of volume
group.The default volume group backup location is “/etc/lvm/backup” .
Refer:http://www.unixarena.com/2013/08/linux-lvm-volume-group-operations.html

20.How to re-create the device files for LVM volumes ?


Answer:Run “vgmknodes” to recreate the LVM devices files.
21.What is lvmdump ?
Answer: “lvmdump” is tool for LVM2 to collect the various information for diagnostic
purposes.By default, it creates a tarball suitable for submission along with a problem report

22.How to replace the failed hard disk in LVM ?

23.How to create a mirrored logical volume ?

24.How to create a striped Logical volume ?

25.How to convert the linear volume to mirror volume ?

26.How are snapshots in LVM2 different from LVM1 in Redhat Linux?


Answer:LVM1 snapshots are readonly by default where LVM2 snapshots were read/write.

27.What are the steps involved to create the logical volume from scratch ?
Answer:
i.Create a physical volume using pvcreate command.
#pvcreate /dev/sdc
ii.Create a volume group using “vgcreate” command
#vgcreate vg02 /dev/sdc
iii.Create a logical volume using “lvcreate” command
#lvcreate -L 100M -n vol1 vg02
iv.Create a filesystem on logical volume using mkfs command.
#mkfs -t ext4 /dev/vg02/vol1
v.Mount the filesystem using mount command for use.
#mount -t ext4 /dev/vg02/vol1 /vol1
28.How to extent the volume group ?
Answer:Using “vgextend” we can increase the volume group.

29.Assume Volume group “vg02″ is already exists.How do you extend the volume group with
50GB ? Provide all the steps with commands.

Answer:
1.Get the 50GB lun from SAN team.(/dev/sdd)
2.Create physcical volume ( # pvcreate /dev/sdd )
2.Extend the volume group (# vgextend vg02 /dev/sdd)

30.If the vg02 has two physical volumes called /dev/sdc/ & /dev/sdd. How do you remove
/dev/sdd from vg02.

Answer: “vgreduce vg02 /dev/sdd/”

31.How to decommission/remove LVM completely from the host ?

Answer:
1.Un-mount all the logical filesystems
2.Remove the logical volumes using “lvremove” command.
3.Destroy the volume group using “vgremove” command.
4.Use “pvremove” command remove the physical volumes from the syste
Swap memory or Swap Space

Swap space in Linux is used when the amount of physical memory(RAM) is full. If the system
needs more memory resources and the RAM is full, inactive pages in memory are moved to
the swap space. While swap space can help machines with a small amount of RAM, it should
not be considered a replacement for more RAM

Swap permits better use of the physical memory space by migrating less-used pages to slower
and less expensive storage.

There are two methods of creating SWAP memory in linux System

1) During Installation.
2) If we haven’t created swap during installation, we can create it at run time. I will show
you how can we add SWAP

First Look at the criteria to create SWAP

1. Systems with 4GB of ram or less require a minimum of 2GB of swap space
2. Systems with 4GB to 16GB of ram require a minimum of 4GB of swap space
3. Systems with 16GB to 64GB of ram require a minimum of 8GB of swap space
4. Systems with 64GB to 256GB of ram require a minimum of 16GB of swap space
Configuration Part:-

Create a new Partition of “nGb” according to your RAM. I have 4 GB of RAM and I will create
2Gb SWAP partition:-

# fdisk /dev/sda

Press “n” for new partition

Select Partition’s First sector by pressing “ENTER”

Now give size as “+sizeM” or “ +sizeG”. According to my configuration I am giving +2G

Press “t” to change the type of partition from standard to SWAP

Select Partition number

Give code “82” for SWAP

Press “w” to save above configuration

# partprobe (To update the partition table in system)

# mkswap /dev/sdaX (mkswap command will initialize sdaX partition as SWAP)

# swapon –a /dev/sdaX (This command will enable sdaX partition as SWAP)

Now if we reboots the machine our SWAP will get disable. To make it permanent, we will add
swap entry in /etc/fstab file. To do so

# blkid (Copy the UUID of /dev/sdaX from the output)

# vim /etc/fstab

“Paste the copied UUID” swap swap defaults 00

Save the file and run

# mount –a

# free -m (This command will show you the size and status of RAM and SWAP)
Linux Boot Process
Linux boot process is one of most interesting topics in liunx. You need to go line by line to
understand the boot process. Here we go..

1) POST – When we turn on our system, the first process executes is “POST”. POST
means power on self test. This program checks whether hardware and cables are
plugged in properly, hardware like RAM & CMOS are there or not. Other POST tasks are
:-
 verify CPU registers
 verify some basic components like DMA, timer, interrupt controller
 find, size, and verify system main memory
 initialize BIOS
 pass control to other specialized extension BIOSes (if installed)
 identify, organize, and select which devices are available for booting
 The functions above are served by the POST in all BIOS versions back to the very first.
In later BIOS versions, POST will also:

 discover, initialize, and catalog all system buses and devices


 provide a user interface for system's configuration
 construct whatever system environment is required by the target operating
system

2) After POST, BIOS executes. BIOS stands for Basic input output system. BIOS Provides
input output at very basic level like, output for keyboard and mouse at booting time.
BIOS provides a menu from which we can change booting priority. Booting priority
means the media from which we want to boot our system. BIOS menu appears from
“F2” “esc” or “del” key varies from make of the system.
3) BIOS boots the system by reading 1st sector of the media priority which is set in BIOS
menu. It only boots the media if that media is bootable.

Bootable media – The media which contains booting files in its first sector. Suppose we
want to make a DVD bootable. The application which we are using to make it bootable,
copies the booting files called “boot loader” from iso image of OS into the first sector of
DVD.

4) If we already have installed operating system on our system, our BIOS will read the first
sector of the “HARD DISK” or if we are installing a new OS through USB or DVD, BIOS
will read the first sector of these media.
5) First sector is called MBR (Master Boot Record). Size of MBR is 512 kb. As above
mentioned first sector contains BOOT Loader. By default Linux’s Boot Loader is called
GRUB2 ( in RHEL 7) and GRUB ( in RHEL 6).
6) Boot loader size exceeds from 512kb, so MBR(First sector) divides in three parts.

Boot loader contains IPL(Initial Program Loader) and other booting related files which
helps to boot the system.
Partition Table contains partitions information.
Magic Number or 0xAA55 is a signature of your boot disk. If this 0xAA55 not found then
you’ll get error message from BIOS that it didn’t find a bootable disk.
When BIOS unable to execute MBR it displays “boot manager missing”

7) The system firmware reads a boot loader from disk, then passes control of the system to
the boot loader. On a Red Hat Enterprise Linux 7 system, this will typically be grub2

8) The boot loader loads its configuration from disk, and presents the user with a menu of
possible configurations to boot. For example if you have multiple OS or we can say you
have dual booted your system with Linux and windows, then boot loader displays the
list of boot loader to boot whether to boot windows or Linux OS.
Configured using: /etc/grub.d/, /etc/default/grub, and (not manually)
/boot/grub2/grub.cfg. (These are configuration files & directory of GRUB).

9) After the user has made a choice (or an automatic timeout has happened), the boot
loader loads the configured kernel and initramfs from disk and places them in memory.
An initramfs is a gzip-ed cpio archive containing kernel modules for all hardware
necessary at boot, init scripts, and more. On Red Hat Enterprise Linux 7, the initramfs
contains an entire usable system by itself.
Configured using: /etc/dracut.conf

10) The boot loader hands control of the system over to the kernel, passing in any options
specified on the kernel command line in the boot loader, and the location of the
initramfs in memory
Configured using: /etc/grub.d/, /etc/default/grub, and (not manually)
/boot/grub2/grub.cfg.

11) The kernel initializes all hardware for which it can find a driver in the initramfs, then
executes /sbin/init from the initramfs as PID 1. On Red Hat Enterprise Linux 7, the
initramfs contains a working copy of systemd as /sbin/init, as well as a udev daemon.
Configured using: init= command-line parameter.

12) The systemd instance from the initramfs executes all units for the initrd.target target.
This includes mounting the actual root file system on /sysroot.
Configured using: /etc/fstab

13) The kernel root file system is switched (pivoted) from the initramfs root file system to
the system root file system that was previously mounted on /sysroot. systemd then re-
executes itself using the copy of systemd installed on the system.

14) systemd looks for a default target, either passed in from the kernel command line or
configured on the system, then starts (and stops) units to comply with the configuration
for that target, solving dependencies between units automatically. In its essence, a
systemd target is a set of units that should be activated to reach a desired system state.
These targets will typically include at least a text-based login or a graphical login screen
being spawned.
Configured using: /etc/systemd/system/default.target, /etc/systemd/system/
Boot, reboot, and shut down

To power off or reboot a running system from the command line, administrators can use
the systemctl command. systemctl poweroff will stop all running services, unmount all
file systems (or remount them read-only when they cannot be unmounted), and then
power down the system.
systemctl reboot will stop all running services, unmount all file systems, and then reboot
the system.

For the ease of backward compatibility, the poweroff and reboot commands still exist,
but in Red Hat Enterprise Linux 7 they are symbolic links to the systemctl tool.

Important:-
systemctl halt and halt are also available to stop the system, but unlike their poweroff
equivalents, these commands do not power off the system; they bring a system down to
a point where it is safe to manually power it off.
Selecting a systemd target A systemd target is a set of systemd units that should be
started to reach a desired state. The most important of these targets are listed in the
following table.

It is possible for a target to be a part of another target; for example, the


graphical.target includes multi-user.target, which in turn depends on basic.target and
others.
Selecting a target at runtime:- On a running system, administrators can choose to switch
to a different target using the systemctl isolate command; for example:-
[root@sanbais ~]# systemctl isolate multi-user.target

Isolating a target will stop all services not required by that target (and its dependencies),
and start any required services that have not yet been started.

Note:- Not all targets can be isolated. Only targets that have AllowIsolate=yes set in
their unit files can be isolated; for example, the graphical.target target can be isolated,
but the cryptsetup.target target cannot

Setting a default target


When the system starts, and control is passed over to systemd from the initramfs,
systemd will try to activate the default.target target. Normally the default.target target
will be a symbolic link (in /etc/systemd/system/) to either graphical.target or
multiuser.target.
Instead of editing this symbolic link by hand, the systemctl tool comes with two
commands to manage this link: get-default and set-default.

[root@sanbais ~]# systemctl get-default


multi-user.target
[root@sanbais ~]# systemctl set-default graphical.target rm
'/etc/systemd/system/default.target' ln -s '/usr/lib/systemd/system/graphical.target'
'/etc/systemd/system/default.target'
[root@sanbais ~]# systemctl get-default graphical.target

Selecting a different target at boot time To select a different target at boot time, a
special option can be appended to the kernel command line from the boot loader:
systemd.unit=.
For example, to boot the system into a rescue shell where configuration changes can be
made without (almost) any service running, the following can be appended from the
interactive boot loader menu before starting:
systemd.unit=rescue.target

This configuration change will only affect a single boot, making it a useful tool for
troubleshooting the boot process.

To use this method of selecting a different target, use the following procedure for Red
Hat Enterprise Linux 7 systems:

1. (Re)boot the system.


2. Interrupt the boot loader menu countdown by pressing any key.
3. Move the cursor to the entry to be started.
4. Press e to edit the current entry.
5. Move the cursor to the line that starts with linux16. This is the kernel command line.
6. Append systemd.unit=desired.target.
7. Press Ctrl+x to boot with these change

Recovering the root password


One task that every system administrator should be able to accomplish is recovering a
lost root password. If the administrator is still logged in, either as an unprivileged user
but with full sudo access, or as root, this task is trivial. When the administrator is not
logged in, this task becomes slightly more involved.
A number of methods exist to set a new root password. A system administrator could,
for example, boot the system using a Live CD, mount the root file system from there,
and edit /etc/shadow. In this section, we will explore a method that does not require
the use of external media.
Note
On Red Hat Enterprise Linux 6 and earlier, an administrator could boot the system into
runlevel 1, and be presented with a root prompt. The closest analogs to runlevel 1 on a
Red Hat Enterprise Linux 7 machine are the rescue.target and emergency.target targets,
both of which require the root password to log in.

On Red Hat Enterprise Linux 7, it is possible to have the scripts that run from the
initramfs pause at certain points, provide a root shell, and then continue when that shell
exits. While this is mostly meant for debugging, it can also be used to recover a lost root
password:
1. Reboot the system.
2. Interrupt the boot loader countdown by pressing any key.
3. Move the cursor to the entry that needs to be booted.
4. Press e to edit the selected entry.
5. Move the cursor to the kernel command line (the line that starts with linux16).
6. Append rd.break (this will break just before control is handed from the initramfs to
the actual system).
Note: the initramfs prompt will show up on whatever console is specified last on the
kernel commandline.
7. Press Ctrl+x to boot with the changes.

At this point, a root shell will be presented, with the root file system for the actual
system mounted read-only on /sysroot.
Important
SELinux is not yet enabled at this point, so any new files being created will not have an
SELinux context assigned to them. Keep in mind that some tools (such as passwd) first
create a new file, then move it in place of the file they are intended to edit, effectively
creating a new file without an SELinux context.
To recover the root password from this point, use the following procedure:

1. Remount /sysroot as read-write.


switch_root:/# mount -oremount,rw /sysroot
2. Switch into a chroot jail, where /sysroot is treated as the root of the file system tree.
switch_root:/# chroot /sysroot
3. Set a new root password:
sh-4.2# passwd root
4. Make sure that all unlabeled files (including /etc/shadow at this point) get relabeled
during boot.
sh-4.2# touch /.autorelabel
5. Type exit twice. The first will exit the chroot jail, and the second will exit the initramfs
debug shell.
At this point, the system will continue booting, perform a full SELinux relabel, then
reboot again.

Вам также может понравиться