Академический Документы
Профессиональный Документы
Культура Документы
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.
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.
1) Open source
2) Close source
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 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
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.
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
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
Versions of REDHAT
REDHAT( 1 – 9)
Redhat started its certification program in 1999. There are list of certifications provided by REDHAT
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.
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.
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.
Types of Files:
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 +%A-%B: shows only day and month (like Sunday January)
#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”.
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
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
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.
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.
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>
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)
/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)
10) Add some contents in ‘public’ file and display its output on terminal without opening it.
Symbolic link :
There are two types of links:-
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
Filter commands: less, more, sed, cut, awk, head, tail, sort these commands are used to filter the
output.
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.
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.
A file containing 15 numbers, applying tail command displays bottom 10 lines. Same option
will be applied to display particular number of lines.
The screenshot below uses cut to filter for the username and userid in the /etc/passwd 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), -
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
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}’ .
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
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-# 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}’
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.
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
Types of users:-
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.
-MD5 or SHA
Warning days = 7
To change minimum password age, warning days, password inactive days etc :-
# chage username.
Group Administration:-
To add o group :- # groupadd groupname
To give password:- # gpasswd groupname
Group name
x = Password pointer
gid = Group ID (Secondary GID)
Group members
Group name
Password
Admin of group
Group members
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
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.
When you check for the permissions it is showing a + sign after normal permission,
that indicate that ACL is applied on directory.
Umask will decide that what should be the default permissions for a file and directory
when it is created.
Maximum full permissions for a file is 666 at the time of creating and for directory is 777
# 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.
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
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
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.
16. What are the important files for User Management task?
/etc/passwd
/etc/shadow
/etc/group
/etc/default/passwd
/etc/default/login
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#
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#
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#
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
Crontab format:-
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
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 )
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
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)
Or
# 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
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
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
/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.
/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
/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.
/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
cleanfiles.sh
sanbaisbackup.xml
/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"
/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
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
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
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
# 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
In our next segment we will see how to create a partition in a Linux system by command line.
Steps to create a partition
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.
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.
Standard partition scheme has restriction on reducing or extending the size of a partition, What
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
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
#mkdir /lvm (Now you need to create a directory to mount the above created LV on it)
# 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
# pvcreate /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:-
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.
# 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.
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
In this portion we will learn how to reduce an lvm , means to reduce size of LVM
# 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
Above command will defragment the partition into 300Mb and free up space.
# 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.
Above Command will shrink the size & will make the filesystem size 10GB.
8. Describe the process for adding a new hard disk to Linux LVM to grow /home?
12. why not more then 4 primary partitions are possible to create
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.
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:
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:
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.
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
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
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
# vim /etc/fstab
# 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:
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.
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
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:
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: