Linux 7&8
Linux 7&8
Linux 7&8
E-Mail: info@virtualpathtech.com
Phone: +91 799 309 6092
website: www.virtualpathtech.com
website: www.musab.in
LINUX 6&7 COMPARITIVE STUDY GUIDE CUM LAB MANUAL:
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness
is implied
Dedicated to:
My parents, my friends and to my students, especially dedicated to my most loving
younger brother SHAKEEB, whom we’ve lost to Cancer
Our Address:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Trademarks
The reader should recognize that the following terms, which appear in the content of this training
document, are official trademarks of Red Hat or other companies
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, is trademarks of Red Hat, Inc., registered
in the United States and other countries.
Linux ® is the registered trademark of Linus Torvalds in the United States and other countries.
XFS ® is a trademark of Silico n Graphics International Corp. or its subsidiaries in the United States
and/or other countries
http://www.virtualpathtech.com
VirtualPath Techno Solutions
FOREWORD
Musabuddin Syed is a highly acclaimed trainer, author and solutions provider. He regularly
trains students in in-house, online and corporate at VirtualPath Techno Solutions. He has an
experience of more than 10 years in industry and Training, where he has delivered more than
400 batches successfully in various technologies.
Though we have taken utmost efforts to present you this book error free, but still it may contain
some errors or mistakes. Students are encouraged to bring, if there are any mistakes or errors
in this document to our notice. So that it may be rectified in the next edition of this document.
This document provides good information on every topic and lab practices. This could become
more effective if equally good practice is done. I urge the readers/students to do rigorous practice to
polish your skill sets.
You can reach us on the following email address
info@virtualpathtech.com
musab@virtualpathtech.com
musabsyd@gmail.com
http://www.virtualpathtech.com
VirtualPath Techno Solutions
OTHER COURSES AT
VIRTUALPATH
LINUX CLUSTERS
DEVOPS AWS
NETAPP &
OPENSTACK IBM &EMC SAN
NETAPP CLUSTER
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Table of Contents
1. Introduction to Linux …………………………………………………………………………………………….… 07-13
2. Basic Commands...…………………………...…………………………………………………………………….. 14-32
3. RHEL 8 Basic Installation ………………........…………………………………………………………………. 33-45
4. Managing File Systems and Partitions ……………………………………………………………………...46-61
5. Swap Spaces Management …………........…………………………………………………………………... 62-65
6. Logical Volume Management (LVM) ………………………………………………….………………….… 66-79
7. RHEL 8 LVM based Installation ................………………………………………….……………………. 80-82
8. User and Group Administration ………………………………………………………………………….….. 83-93
9. Controlling Access to the Files ........……………………………………………………..…………….…. 94-101
10. Enhanced User Security with SUDO ………………………………………………………..……………… 102-109
11. Network Configuration and Troubleshooting………….……………………………….…………….. 110-122
12. NIC Teaming …………………………………………………………………………………………………………. 123-128
13. Managing SELinux (Basics) ……………………….……………………………………………….…………. 129-138
14. Booting Procedure of RHEL7/8 and Troubleshooting …........................................... 139-147
15. Manage Installed Services ……………………………………………………….…………………………… 148-151
16. Introduction to Firewalld …………………………………………………………….………………………. 152-155
17. Introduction to Cokpit in RHEL8 …………………………………………………………….…………..… 156-158
18. Backup and Restore (tar&gzip) .…………………………………………………………………………… 159-161
19. Job Automation with Cronjobs ….……………………………………………………………………….. 162-167
20. Administrating Remote System ……………………………………………………………………………. 168-176
21. Software Management ………………………………………………………………………………………… 177-197
22. Managing Processes …………………………………….………..……………………………………………. 198-212
23. NFS (Network File System) Server ……………………………………………………………………..… 213-221
24. Samba Server ……………………………..……………………………………………………………………….. 222-229
25. DNS (Domain Name System) Server ……………………………………………………………………… 230-239
26. Mail Server …………………………………………..………………………..……………………………………. 240-244
27. Web Server (Apache) ……………………………………………………………………………………..……. 245-259
http://www.virtualpathtech.com
VirtualPath Techno Solutions
This page has been left blank intentionally
http://www.virtualpathtech.com
VirtualPath Techno Solutions
INTRODUCTION TO UNIX & LINUX
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 7 of 273
HISTORY OF UNIX
Bell Labs’ Ken Thompson developed UNIX in 1969 so he could play games on a scavenged DEC PDP-7. With
the help of Dennis Ritchie, the inventor of the “C” programing language, Ken rewrote UNIX entirely in “C”
so that it could be used on different computers. In 1974, the OS was licensed to universities for
educational purposes. Over the years, hundreds of people added and improved upon the system, and it
spread into the commercial world. Dozens of different UNIX “flavors” appeared, each with unique
qualities, yet still having enough similarities to the original AT&T version. All of the “flavors” were based
on either AT&T’s System V or Berkeley System Distribution (BSD) UNIX, or a hybrid of both.
During the late 1980’s there were several of commercial implementations of UNIX:
• Apple Computer’s A/UX
• AT&T’s System V Release 3
• Digital Equipment Corporation’s Ultrix and OSF/1 (renamed to DEC UNIX)
• Hewlett Packard’s HP-UX
• IBM’s AIX
• Lynx’s Real-Time UNIX
• NeXT’s NeXTStep
• Santa Cruz Operation’s SCO UNIX
• Silicon Graphics’ IRIX
• SUN Microsystems’ SUN OS and Solaris
• and dozens more.
The Open Standards Foundation is a UNIX industry organization designed to keep the various UNIX flavors
working together. They created operating systems guidelines called POSIX to encourage inter-operability
of applications from one flavor of UNIX to another. Portability of applications to different gave UNIX a
distinct advantage over its mainframe competition.
Then came the GUIs. Apple’s Macintosh operating system and Microsoft’s Windows operating
environment simplified computing tasks, and made computers more appealing to a larger number of
users. UNIX wizards enjoyed the power of the command line interface, but acknowledged the difficult
learning curve for new users. The Athena Project at MIT developed the X Windows Graphical User
Interface for UNIX computers. Also known as the X11 environment, corporations developed their own
“flavors” of the UNIX GUIs based on X11. Eventually, a GUI standard called Motif was generally accepted
by the corporations and academia.
During the late 1990’s Microsoft’s Windows NT operating system started encroaching into traditional
UNIX businesses such as banking and high-end graphics. Although not as reliable as UNIX, NT became
popular because of the lower learning curve and its similarities to Windows 95 and 98. Many traditional
UNIX companies, such as DEC and Silicon Graphics abandoned their OS for NT. Others, such as SUN,
focused their efforts on niche markets, such as the Internet.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 8 of 273
Linus Torvalds had a dream. He wanted to create the coolest operating system in the world that was
free for anyone to use and modify. Based on an obscure UNIX flavor called MINIX, Linus took the
source code and created his own flavor, called Linux. Using the power of the Internet, he distributed
copies of his OS all over the world, and fellow programmers improved upon his work. In 1999, with a
dozen versions of the OS and many GUIs to choose from, Linux is causing a UNIX revival. Knowing that
people are used to the Windows tools, Linux developers are making applications that combine the
best of Windows with the best of UNIX.
UNIX Principles
• Everything is a file:- UNIX system have many powerful utilities designed to create and
manipulate files. The UNIX security model is based around the security of files. By
treating everything as a file, you can secure access to hardware in the same way as
you secure access to a document.
• Configuration data stored in text: - Storing configuration in text allows an
administrator to move a configuration from one machine to another easily, provide
the ability to roll back a system configuration to a particular date and time.
• Small, Single-Purpose Programs: - UNIX provides many utilities.
• Avoid captive user interfaces:-
• Ability to chain programs together to perform complex tasks:- A core design feature
of UNIX is that output of one program can be the input for another. This gives the
user the flexibility to combine many small programs together to perform a larger,
more complex task.
GNU Project/ FSF
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 9 of 273
Linux Origins
• LINUS TORVALDS
a) Finnish college student in 1991
b) Created Linux Kernel
• When Linux Kernel combined with GNU applications, complete free UNIX like OS was
developed.
Why Linux?
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 a 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 programs for the smallest to the largest companies.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 10 of 273
ARCHITECTURE OF UNIX
The architecture of UNIX can be divided into three levels of functionality, as shown in Figure.
The lowest level is the kernel, which schedules tasks, manages resources, and controls
security. The next level is the shell, which acts as the user interface, interpreting user
commands and starting applications. The highest level is utilities, which provides utility
functions. In other words it is the USER level, as user is the one who operates those utilities.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 11 of 273
FILESYSTEM HIERARCHY
Linux uses single rooted, inverted tree like file system hierarchy
/ 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 (/)
C:\ of windows
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 12 of 273
/proc it contain process files
Its contents are not permanent, they keep changing
It is also called as Virtual Directory
Its file contain useful information used by OS
Like /proc/meminfo... information of RAM/SWAP
/proc/cpuinfo... information of CPU
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 13 of 273
UNIX BASIC COMMANDS
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 14 of 273
Creating a Directory
#mkdir <dir name>
#mkdir mydir
Let us make some directories according to the following architecture in one command.
World
#mkdir –p World/{India/{Hyd,Bang},AUS/{Sydney,Perth},USA/{Tampa,NYC}}
Check it by using tree command or ls –R command
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 15 of 273
Copying directories from one location to other
# cp –rvfp <dir name> <destination name>
#cp –rvfp mydir2 mydir
Renaming a File
#mv <old name> <new name>
#mv file1 newfile
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 16 of 273
Renaming a Directory
• The procedure and command for renaming the directory is exactly same as renaming a
file.
#mv old name new name
#mv mydir newdir
Removing a File
#rm filename or #rm –f filename (without prompting)
Without prompting:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 17 of 273
Removing a directory with files or directories inside
A dir which is having some contents inside it cannot be removed by rmdir command. There
are two ways to delete the directory with contents.
i. Remove the contents inside the directory and then run rmdir command
ii. Run #rm –rf dirname (where r stands for recursive and f stands for forcefully.
VIM EDITOR
VI Visual display editor
VIM Visual display editor improved
This is command mode editor for files. Other editors in Linux are emacs, nano, and gedit
vi editor is most popular
It has 3 modes:
1 Command Mode
2 Insert mode (edit mode)
3 extended command mode
Note: When you open the vim editor, it will be in the command mode by default.
Insert Mode:
i To begin insert mode at the cursor position
I To insert at the beginning of line
a To append to the next word’s letter
A To Append at the end of the line
o To insert a new line below the cursor position
O To insert a new line above the cursor position
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 18 of 273
Command Mode:
gg To go to the beginning of the file
G To go to end of the file
w To move the cursor forward, word by word
b To move the cursor backward, word by word
nw To move the cursor forward to n words (5W)
nb To move the cursor backward to n words (5B)
u To undo last change (word)
U To undo the previous changes (entire line)
Ctrl+R To redo the changes
yy To copy a line
nyy To copy n lines (5yy or 4yy)
p To paste line below the cursor position
P To paste line above the cursor position
dw To delete the word letter by letter (like Backspace)
x To delete the world letter by letter (like DEL Key)
dd To delete entire line
ndd To delete n no. of lines from cursor position(5dd)
/ To search a word in the file
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 19 of 273
#ls ?ample Files with any first character and has ample
#ls -ld l* Directory listing only
#ls –ld directory name to see the permissions of a particular directory
#ls [ae]* First character of the filename must be a or e.
# ls [!ae]* ! Symbol complements the condition that follows. The characters must
not be a or e.
#ls [a-m][c-z][4-9] list all the files in specific range
Types of Files:
Symbol Type of File
- Normal file
d Directory
l Link file (shortcut)
b Block file (Harddisk, Floppy disk)
c Character file (Keyboard, Mouse)
Symbolic Link
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 20 of 273
Creating a Hard link:
#ln <source file> <Destination>
Examples of Grep:
Let us pick the information about root from the file /etc/passwd (/etc/passwd contains
information about all the users present in the system)
#grep root /etc/passwd
To avoid case sensitivity of the word (i.e. the word may be uppercase of lowercase) use -i
#grep –i linux test (lets grep the word linux whether upper of lower case in the file test)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 21 of 273
To display a word and 2 lines after the word:
#grep -nB2 wheel /etc/group
Filter Commands:
• Filter commands are used to filter the output so that the required things can easily be
picked up. The commands which are used to filter the output are
#less
#more
#head
#tail
#sort
#cut
#sed
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 22 of 273
• less:
The less command is used to see the output line wise or page wise.
Ex: less /etc/passwd
more:
more is exactly same like less
Ex: #more /etc/passwd
Note: -press Enter key to scroll down line by line (or)
Use d to go to next page
Use / to search for a word in the file
Use v to go vi mode where you can edit the file and once you save it you will back to
more command
head:
It is used to display the top 10 lines of the file.
Ex:# head /etc/passwd
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 23 of 273
To display the custom lines
#head -n /etc/passwd (where n can be any number)
tail:
It is used to display the last 10 lines of the file
#tail /etc/passwd
Sort:
It is used to sort the output in numeric or alphabetic order
#sort filename
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 24 of 273
To sort the file according to numbers
#sort –d test or #sort –h test
cut command:
The cut command is used to pick the given expression (in columns) and display the output.
# cut -d -f filename (where d stands for delimiter ex. : , “ “ etc and f stands for field)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 25 of 273
To delimit commas and print the field
#cut –d, -f1 filename
sed command:
sed stands for stream editor, which is used to search a word in the file and replace it with
the word required to be in the output
Note: it will only modify the output, but there will be no change in the original file.
I/O Redirection:
Redirection is a process where we can copy the output of any command(s), file(s) into a new
file. There are two ways of redirecting the output into a file.
Using > or >> filename after the command, and
Using tee command
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.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 26 of 273
Appending another output in same the same file
The above options of redirections will not display any output, but directly save the output
in a file. Using tee command will not only redirect the output to new file but it will also
display the output.
Note: if the given name of the file (newfile) is not available a new file will be created
automatically. If the file already exists then it will overwrite contents of the file.
#cat file2 |tee file3
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 27 of 273
Find command:
find command is used to find the files or directory’s path, it is exactly like the find option in
windows where you can search for a file.
Option Usage
-name For searching a file with its name
-inum For searching a file with particular inode number
-type For searching a particular type of file
-user For files whose owner is a particular user
-group For files belonging to particular group
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 28 of 273
Finding the files whose group is “myuser”
#find / -group myuser
File Permissions:
Permissions are applied on three levels:
• Owner or User level
• Group level
• Others level
Filetype+permission, links, owner, group name of owner, size in bytes, date of modification,
file name
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 29 of 273
1 Symbolic method (ugo):
• Symbolic mode: General form of symbolic mode is:
# chmod [who] [+/-/=] [permissions] file
who To whom the permissions to be assigned
User/owner (u); group (g); others (o)
Example:
Assigning different permissions to the file (user=rwx, group=rw and others=r)
#chmod u=rwx,g=rw,o=r myfile (where myfile is the name of the file)
Likewise you can add or remove permissions from any file for anyone (user group or
other)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 30 of 273
Assigning full permission to the file i.e. rwx to all
#chmod 777 myfile
Umask:
When we create any file using touch, cat or vi commands they get created with default file
permissions as stored in umask (User file creation mask).umask is a 4 digit octal number
which tells Unix which of the three permissions are to be denied rather than granted. Umask
will decide that what should be the default permissions for a file and directory when it is
created.
Calculation of default permissions for file and directory, basing upon the umask value
Note: For a file by default it cannot have the execute permission, so the maximum full
permission for a file at the time of creation can be 666 (i.e. 777 -111 = 666), whereas a
directory can have full permissions i.e. 777
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 31 of 273
Modifying the umask value:
#umask 002
The Modified default Permission for a file will be 666-002=664 i.e. rw,rw,r, and for the
directory it will be 777-002=775 i.e. rwx,rwx,r-x.
Note: Create a file and a directory and check for the default permissions.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 32 of 273
RHEL 8 BASIC INSTALLATION
MINIMUM RECOMMENDED
Partitions Sizes
/boot 1 GB
• Enter into BIOS setting and make CD/DVD Drive as first boot device
• Make sure that VT {Virtual Technology) is enabled for RHEL7-64 bit systems
• Insert the RHEL 7 CD/DVD into CD/DVD drive and boot the system
• If booted from CD/DVD Rom the following screen will be displayed
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 33 of 273
• Move the cursor to install Red Hat Enterprise linux 8.x, hit Enter
• Select the Language and Country for time zone, language and currency
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 34 of 273
• Select date and time to make time zone selection
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 35 of 273
• Select Software Selection for selecting software to install during installation
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 36 of 273
• Click on INSTALLATION DESTINATION for partitioning layout
• Select the Disk which you want to use for installation, Select Custom, for manual
partitioning and click on Done.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 37 of 273
• Select the style of partitioning type preferable
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 38 of 273
• Select / for root partition and assign required size in KB, MB, GB
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 39 of 273
• Finally, select swap and give the size
• Verify the partitions and make changes if required and click on Done
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 40 of 273
• Accept the changes for continue
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 41 of 273
• Click on Network&Hostname to set IP address, which can skipped and set later also.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 42 of 273
• Click on root password to assing admin level password for root user
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 43 of 273
• Finally, Click on Begin Installation to start the installation
• Once the installation is completed, reboot the machine while clicking on Reboot
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 44 of 273
• After booting first time, click on LICENSE INFORMATION to see and accept the
license agreement.
• If you have subscription, register your machine with Redhat Network for updates
and support.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 45 of 273
MANAGING PARTITIONS & FILE SYSTEMS
What is a partition?
Partitioning is a means to divide a single hard drive into many logical drives. A partition is a
contiguous set of blocks on a drive that are treated as an independent disk. A partition table
is an index that relates sections of the hard drive to partitions.
• Encapsulate your data. Since file system corruption is local to a partition, you stand to lose
only some of your data if an accident occurs.
• Increase disk space efficiency. You can format partitions with varying block sizes, depending
on your usage. If your data is in a large number of small files (less than 1k) and your partition
uses 4k sized blocks, you are wasting 3k for every file. In general, you waste on average one
half of a block for every file, so matching block size to the average size of your files is important
if you have many files.
• Limit data growth. Runaway processes or maniacal users can consume so much disk space
that the operating system no longer has room on the hard drive for its bookkeeping
operations. This will lead to disaster. By segregating space, you ensure that things other than
the operating system die when allocated disk space is exhausted.
G F
P P P P P R
E
T
E
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 46 of 273
The Structure of Disk Partition
• On the disk where O/S is installed, will have the first partition as MBR/GPT.
• MBR is a Master Boot Record, which contains two important utilities, IPL (Initial
Program Loader) and PTI (Partition Table information). Which supports up to 2TB of
disk size
• GPT is GUID Partition Table, which also contains IPL as well as PTI, supports up to 2
Zettabyte (1024 EB (Exabyte)=1 ZB (zettabyte) ).
• IPL is responsible for booting the operating the system, because it contains the boot
loader.
• In earlier versions of Linux i.e. up to RHEL 4, the default boot loader was LILO (Linux
Loader). But, since RHEL5 onwards it has been changed to GRub (Grand Unified Boot
loader), which is far more superior to LILO. In RHEL 7 GRub2 has been introduced.
• The PTI (Partition Table information) is the information about the number of partitions
on the disk, sizes of the partition and types of partitions.
Disk Identification:
Different type of disks will be having different initials in Linux
FILE SYSTEM:
• It is method of storing the data in an organized fashion on the disk. Every partition on
the disk except MBR and Extended partition should be assigned with some file system
in order to make them store the data. File system is applied on the partition by
formatting it with a particular type of file system.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 47 of 273
Types of file systems supported in RHEL 6,7 & 8:
• The file systems supported in Linux are ext2, ext3 ext4, vfat xfs in RHEL 6, 7&8 etc.
• Ext/xfs file system is the widely used file system in Linux, whereas vfat is the file
system to maintain a common storage between Linux and windows ( in case of
multiple o/s)
1. Stands for Stands for Third Stands for Fourth Xtents File system/
Second Extended Extended File Extended File X File System
File System System System
2. It was introduced It was It was introduced in It was introduced in
in 1993 introduced in 2008. 1993.
2001
3. Does not have Supports Supports Journaling Supports Journaling
journaling Journaling Feature. Feature.
feature. Feature.
4. Maximum File Maximum File Maximum File Size Maximum File Size
size can be from Size can be from can be from 16 GB can be from 16TB
16 GB to 2 TB 16 GB to 2 TB to 16 TB to 8 EB
5. Maximum ext2 Maximum ext3 Maximum ext4 file Maximum xfs file
file system size file system size system size is 1 EB system size is 16 EB
can be from 2 TB can be from 2 TB (Exabyte). 1 EB = (Exabyte). 1 EB =
to 32 TB to 32 TB 1024 PB (Petabyte). 1024 PB (Petabyte).
1 PB = 1024 TB 1 PB = 1024 TB
(Terabyte). (Terabyte).
6. Cannot convert You can convert All previous ext file N/A
ext file system to an ext2 file systems can easily
ext2. system to ext3 be converted into
file system ext4 file system.
directly (without You can also mount
backup/restore). an existing ext3 f/s
as ext4 f/s (without
having to upgrade
it).
MOUNTING:-
PARTITION
sdax DIRECTORY
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 48 of 273
• It is a method of attaching a directory to the file system in order to access the partition
and its file system is known as mounting.
• The mount point is the directory (usually an empty one) in the currently accessible file
system to which an additional file system is mounted.
• The /mnt directory exists by default on all Unix-like systems. It, or usually its
subdirectories (such as /mnt/floppy and /mnt/usb), are intended specifically for use
as mount points for removable media such as CDROMs, USB key drives and floppy
disks.
• /etc/mtab is a file which stores the information of all the currently mounted file
systems; it is dynamic and keeps changing.
• /etc/fstab is the file which is keeps information about the permanent mount point. If
you want to make your mount point permanent, so that it will be mounted even after
reboot, then you need to make an appropriate entry in this file.
LAB WORK:-
Note: Observe in the above picture that the device name is /dev/sda .
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 49 of 273
To view the existing partitions (RHEL7/8)
#fdisk –l or parted –l
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 50 of 273
Creating a new partition
#fdisk /dev/sda
• Use p to list out the partition information first and
• Use n to create a new partition.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 51 of 273
Deleting a partition
Let’s delete the partition we’ve created above i.e. /dev/sda7
• Use d to delete a partition and specify the device name, in our case it is 7.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 52 of 273
“Now then we have learnt creating a partition. Let’s see how to format a partition with a
particular file system”
After creating a partition we need to assign some file system to it so that we can start storing
the data into it. To format a partition the following syntax is used.
• Likewise you can format the different partitions with different file systems like
• #mkfs.ext3 /dev/sdax
• #mkfs.vfat /dev/sdax
Note: Even after formatting the partition, we cannot add the data into the partition. In
order to add the data in the partition it is required to be mounted.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 53 of 273
Mounting a partition
Mounting is a procedure where we attach a directory to the file system. There are two types
of mounting which will be used in Linux or any UNIX.
• Temporary Mounting
• Permanent Mounting
Temporary Mounting
In a temporary mount point we will create a directory and mount it, but this mount point will
last only till the system is up, once it is rebooted the mounting will be lost.
Syntax:
#mount <device name> <directory name (mount point)>
#mount /dev/sda5 /mnpt
xfs
• Now we have successfully mounted the partition we can access it and can store the
data
• To add the data access the mount point
• #cd /mnpt. Now, add the data and exit the directory
Unmounting a partition
#umount <mount point directory>
#umount /mnpt
Verify it with mount command.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 54 of 273
Permanent Mounting
Permanent mounting procedure is exactly same like temp mounting, but here we will update
the /etc/fstab file with the mounting details, so that it will be mounted even after the system
is reboot.
Steps To make a permanent mount point:
• Make a directory or use an existing directory
• Add entry in /etc/fstab file
• Use mount –a command to check it is mounting. (mount –a will mount all the entry
placed in /etc/fstab)
Here we will be using our existing /kernel directory as mount point which is created
previously.
#vim /etc/fstab
Check Sequence(fsck)
Note: For xfs, you can put xfs in place ext4
#mount –a
You can now access the directory and add, delete or modify the contents and can also
unmount the file system at any point
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 55 of 273
#vim /etc/fstab
Now mount it with mount –a command and verify it with mount command
Sometimes a directory reflects error while unmouting, the possible causes for it are
• You are in the same directory and trying to unmount it. Check with pwd command
• Some users are present in the directory and using the contents in it.
• Check with fuser –cu /mnpt or /dev/sda5 and lsof /mnpt or /dev/sda6
• Kill the open connections using fuser –ck /mnpt, you could also try umount -l (lazy
unmount)
• Now you can use umount command to unmount the file system.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 56 of 273
PARTITIONING WITH PARTED COMMAND
Let’s learn the way to work with parted utility and (or) command line.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 57 of 273
Getting help in parted utility
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 58 of 273
Print and confirm the partition
#print (or) p
Note: msdos for MBR & gpt for GPT partition table
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 59 of 273
Creating 500MB partition directly with parted command
#parted <disk path> mkpart <partition number> <start val> <end value>
#parted /dev/sdb mkpart 1 1 500MB
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 60 of 273
Creating a Primary/Extended/Logical partition in MBR/msdos table disk
#parted <disk path> mkpart <type of partition (primary/extended/logical)
<Start val> <end val>
#parted /dev/sda mkpart logical 22.5GB 23GB
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 61 of 273
SWAP SPACES MANAGEMENT
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 space is located on hard drives, which
have a slower access time than physical memory.
Swap space is compulsory to be created at the time of installation. But, additional swap spaces
can be created and deleted at any point of time, when it is required. Sometimes we need to
increase the swap space, so we create additional swap spaces which will be added to the
existing swap space to increase the size.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 62 of 273
Commands to be used in maintaining Swap spaces
• Create a normal partition using fdisk and change hex code to make it swap partition.
• The hex code for SWAP is 82. (To change the use t in fdisk and list all the hex code use l)
• Update the partition table using partprobe command
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 63 of 273
Format the partition with swap file system
#mkswap /dev/sda6
• To Test auto activation of swap after putting entries in fstab use the following command
#swapon –a (It works the same way how mount –a would work)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 64 of 273
Using File as a Swap Space:
At times it is seen that disks may run out ot space and a partition may not be created to add a new
swap space. In this situation a file can also be used as a swap space.
Step1: Create a file with required size like 1G for example, using dd command
#dd if=/dev/urandom of=/swap-file bs=1M count=1024
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 65 of 273
Logical Volume Management
The Linux Logical Volume Manager (LVM) is a mechanism to virtualize the disks. It can create
"virtual" disk partitions out of one or more physical hard drives, allowing you to grow, shrink,
or move those partitions from drive to drive as your needs change. It also allows you to create
larger partitions than you could achieve with a single drive. Traditional uses of LVM have
included databases and company file servers, but even home users may want large partitions
for music or video collections, or for storing online backups. LVM can also be convenient ways
to gain redundancy without sacrificing flexibility.
A typical example for the need of LVM can be, assuming that we are having a disk of size 2GB
and we start adding the data in the form of a single file, eventually it grows to the size of 2GB.
In this case the possibility is, you go for another disk which is larger than 2GB, let’s say 4GB.
But what if the file again grows more than 4GB? How far you will be migrating file from one
disk to another so on and so forth? It requires a down time as well which is not possible in
real time, so to avoid these circumstances we implement LVM and store data in LV’s whose
size can be easily increased whenever required without a downtime.
Above picture shows the structure of LVM. LVM consists of Physical Volumes, Volume Group,
Logical Volumes and finally file systems. The Physical partitions are known as Physical
Extents (PE), and the logical partitions are known as logical Extents (LE)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 66 of 273
Components of LVM in Linux:
• Physical Volumes (PV)
• Physical Extent (PE)
• Volume Group (VG)
• Logical Volume (LV)
• Logical Extent (LE)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 67 of 273
LAB WORK:-
• The above command will list all the PVs in the system, if you want to see the details only
for a particular PV, then use
#pvdisplay <partition name> i.e. #pvdisplay /dev/sda7
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 68 of 273
Creating a Volume Group (VG)
• After creating a PV, the next step is to create a Volume Group or VG
• To create a VG the syntax is
#vgcreate <name for the VG> <PV name>
#vgcreate myvg /dev/sda7
• To check all the VGs detail you can also use the command
#vgdisplay
• It will list out all the VGs in the system in detail.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 69 of 273
Logical Volume Creation
• Once we are ready with a Volume Group then it’s the time to create a Logical Volume LV
• The syntax for creating an LV is
• #lvcreate –L <size of LV> -n <name for LV> <VG name>
• #lvcreate –L 300M –n mylv myvg (To create a LV of 300MB)
• Note: The output for only lvdisplay command is very lengthy to show, it is recommended
that you run the command on the system and check it out. The syntax is given above.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 70 of 273
Adding File system to the LV and Mounting it.
• As per now we have our VG created so is our LV. In order make it accessible we need to
format it with a file system like ext4 or xfs
• The syntax for formatting an LV is exactly like formatting a normal partition, Instead of
/dev/partition name we use the path of LV that will be something like /dev/vg/lv
• #mkfs.ext4 /dev/myvg/mylv or #mkfs.xfs /dev/myvg/mylv
Mounting:
• Mounting an LV is exactly same like a normal partition, again the path for mounting will
be /dev/vg/lv
• Create a directory over which the LV should be mounted.
• #mount </dev/vgname/lvname> /directory name
• #mount /dev/myvg/mylv /mydir
• Verify the mounting with mount command
• Make it a permanent mount by making an entry in /etc/fstab
#vim /etc/fstab
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 71 of 273
Extending a Volume Group
• Extending a volume group is actually adding a new PV to the volume group.
• To extend a volume group we need to create a new partition using fdisk. Don’t forget to
change its hex code to 8e and update the partition table using partprobe command
• Create a PV on the newly created partition using pvcreate command
• Add the partition to the VG using vgextend command, the syntax for it is
• #vgextend <VG name> <PV name>
• #vgextend myvg /dev/sda8
• Verify it pvs command
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 72 of 273
• Increasing the size of the LV and updating the file system
• Verify it by df –h
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 73 of 273
• Now reduce the size by using # lvreduce -L -200M /dev/myvg/mylv command
• We know the size of LV is around 500MB, from previous picture in case of extending
the size of LV.
• Or else you can run df –h and verify it again.
• Umount the LV by using umount command
• Finally reduce the size of the LV using lvreduce command. It will prompt you about the
change type y to continue with reduction.
• Mount the LV and run the command df-h, to verify the change in the size of LV
• #mount –a ( if an entry is passed in /etc/fstab use this command), else do manual
mounting as shown below
• #df –h
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 74 of 273
Moving or Migrating the data from one pv to another.
• There might be a situation where the PV might be failing and it is required to be
replaced, in such case, we need to migrate or move the data from such PV to the other
and isolate the PV.
• The Steps to migrate the PV are
• Access the mount point of failing PV and check the data in it,
• Verify the size of the PV by pvs command or pvdisplay command.
• Unmount the file system on that PV (optional)
• Add new PV, which should be of the same size or higher than that of the
replacing PV to the volume group.
• Migrate the PVs contents to the new PV using following command
• #pvmove <Old PV> <New PV>
• Mount back the LV, access the mount point and verify the data in it.
• Remove the faulty PV from Volume Group.
Okay! So let’s do the practical following above steps.
• Access the mount point of the failing PV and check the data in it,
• Unmount the file system on that PV (this is optional as migration can be done online
aswell)
• #umount /mydir
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 75 of 273
• Add new PV which should be of the same size or higher than that of the replacing PV to
the volume group.
• In our case the size of the failing PV is around 500MB, so we need to add a PV whose size
is at least 500MB or more
• I have created another partition from fdisk i.e. /dev/sda7 with the size around 500MB
• Mount back the LV, access the mount point and verify the data in it.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 76 of 273
Deleting/Removing the LV:
• To Delete/Remove an LV, first unmount the file system.
• Remove the entry from /etc/fstab.
• Use the command lvremove i.e.
• #lvremove <LV name>
• #lvremove /dev/myvg/mylv ( it will prompt to you to continue, press y to continue)
• Verify it by using lvdisplay command
• As we was having only one LV and that is now deleted, that’s why it is not showing any
LVs after executing lvdisplay command.
Building anything requires lots of concentration, hard work, and patience, but to destroy
it, it is just a matter of seconds. Isn’t it….!
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 77 of 273
Creating a VG by customized PE size
• To create a VG with specifying an PE size,
• First create a partition and also create a pv
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 78 of 273
Creating LV by specifying no. of LE instead of giving size in MB or GB.
• To create an LV using LE, the things to keep in mind are
• Size of LE = Size of PE
• For example if the size of PE is 16, then the size of LE will also be 16.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 79 of 273
INSTALLING RHEL7/8 USING LVM PARTITIONING
• The only difference in a normal installation and LVM installation is that instead of creating
normal partition we will create a VG and then LVs for all partitions, except /boot and
swap.
• The advantage of installing Linux using LVM is that, if any of system partition is running
out of space and required more space, in case of normal partitioning it is not possible to
increase the size of a partition once it is created. But, using LVM the space can be
dynamically increased whenever it is required.
• Even if there is no space remaining in the disk some space can be borrowed from other
LVMs and can easily be assigned to required system partition to fulfill its need.
• LVM provides a greater scalability to the administrator and avoid uncertain down time to
the server.
• LVM ensures the possibility of increasing and decreasing the sizes whenever required and
prevents unnecessary loss of time.
• Start the installation normally as done previously, but only at the time of partitioning
follow the steps below.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 80 of 273
• Select an appropriate mount point and assign the size. Repeat it till all important mount
points are created
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 81 of 273
• Give appropriate size and create /, /boot/, /home/, /var and swap
Note: All the sizes listed above are based on the availability of the space. It is no-where a
recommended or minimum sizes. The sizes can be based on your requirements. But /
should get more size as it will be having “/usr”, “/opt” & “/tmp” as well, which in rhel6
used to be created separately.
• Verify the sizes and click on “Done” to continue with the installation. Complete the
installation as usual as we have done previously at the beginning of the course.
• Note: /boot is automatically created as “Standard Partition” even though the scheme is
selected as “LVM”. Isn’t it awesome?
Practice the LVM Concept well; as it is the most important part in Linux and in any UNIX
operating system as well.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 82 of 273
USER AND GROUP ADMINISTRATION
PART- I USER ADMINISTRATION
In Linux/Unix user is one who uses the system. There can be at least one or more than one
users in Linux at a time. Users on a system are identified by a username and a userid. The
username is something that users would normally refer to, but as far as the operating system
is concerned this is referred to using the user id (or uid). The username is typically a user
friendly string, such as your name, whereas the user id is a number. The words username and
userid are often (incorrectly) used interchangeably. The user id numbers should be unique
(one number per user). If you had two usernames with the same user id, effectively there
permissions would be the same and the files that they create would appear to have been
created by the same user. This should not be allowed and the useradd command will not
allow usernames to share the same userid.
• Users and groups are used to control access to files and resources
• Users login to the system by supplying their username and password
• Every file on the system is owned by a user and associated with a group
• Every process has an owner and group affiliation, and can only access the resources
its owner or group can access.
• Every user of the system is assigned a unique user ID number ( the UID)
• Users name and UID are stored in /etc/passwd
• User’s password is stored in /etc/shadow in encrypted form.
• Users are assigned a home directory and a program that is run when they login (Usually
a shell)
• Users cannot read, write or execute each other’s files without permission.
/var/ftp,
ftp, ssh,
System /var/www/html /sbin/nologin
apache 1 to 999 1 to 999
User /var/named,
nobody
etc.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 83 of 273
In Linux there are three types of users.
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 user. They are normal users like Rahul, Musab
etc. Only the root user has the permission to create or remove a user.
1. "/etc/passwd"
2. "/etc/shadow"
1. /etc/passwd
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 84 of 273
2. /etc/shadow
root:$1fdsfsgsdfsdkffefje:14757:0:99999:7:::
LAB WORK:-
Creating a user
• The syntax for creating a user in Linux is
• # useradd <username> <options> or #useradd <options> <username>
• options are
• -u user id
• -G Secondary group id
• -g primary group id
• -d home directory
• -c comment
• -s shell
• When no option is used with useradd command the options like UID, GID, home dir and
shell will be assigned default.
• #useradd <username>
• #useradd myuser
Observe that the uid, gid, home dir, and shell is assigned automatically.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 85 of 273
Let’s create a user with customized attributes
Note: If the “passwd” command is executed without any username, it would consider the
Name of active or currenty logged in user. Say, if you have logged in with root, it will
try to change the root password
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 86 of 273
Modifying the user’s attribute
• 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
OR
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 87 of 273
Unlocking a user a/c:
• Unlock the above a/c
• #usermod –U < user name >; #passwd –u <user name>
• #usermod –U myuser ; #passwd –u myuser
• Verify it in /etc/shadow file, it shows exclamation mark before user account or try using
#passwd –S username
OR
• Observe in both pictures that once the account is unlocked the exclamation is gone.
• Last password change: When the password was change last time.
• Password expires: Password expiry date
• Password inactive: After password expiry grace period before the account gets locked.
• Account expires: Date on which the account expires.
• Minimum number of days b/w password change: once the password is changed, it
cannot be changed until a min period of specified date. [0] means never.
• Max number of days b/w password change: After changing the password how long it will
be valid for.
• Number of days of warning before password expires: start of warnings to change the
password, no. of days before the password expires.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 88 of 273
Changing the password parameters:
• Changing of the password parameters can be done by two ways.
1. #chage <user name >
2. #chage <option> <value> <username>
• #chage myuser
• The second method is for, if you want to change a particular field of password aging policy
• #chage <option> <value> <username>
• The options which can be used are as follows
• -m for Min password age
• -M for Max password age
• -d for last time the password is changed. ( Note: if given d 0, it will force the user to change
password at next login)
• -W Password expiration warnings
• -I Password inactive [-1 means inactive].
• -E A/C expiration date
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 89 of 273
• Let’s see how to change only the account expiration date
Likewise you can use any option listed above and change any particular field in password
aging parameters.
At next login
Deleting a User:
• To delete a user the syntax used is
• #userdel <username> it will only delete the user but home directory will be there. To
delete the user with its home directory and mailbox, use the following command.
• #userdel –r < user name >
• #userdel –r myuser2
We’re now done with user administration, let’s see what’s in part-II
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 90 of 273
PART-II GROUP ADMINISTRATION
GROUPS
• Users are assigned to groups with unique group ID numbers (the GID)
• The group name and GID are stored in /etc/group
• Each user is given their own private group
• They can also be added to their groups to gain additional access
• All users in a group can share files that belong to the group
Each user is a member of at least one group, called a primary group. In addition, a user can
be a member of an unlimited number of secondary groups. Group membership can be used
to control the files that a user can read and edit. For example, if two users are working on the
same project you might put them in the same group so they can edit a particular file that
other users cannot access.
• A user’s primary group is defined in the /etc/passwd file and Secondary groups are
defined in the /etc/group file.
• The primary group is important because files created by this user will inherit that group
affiliation.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 91 of 273
Creating a group with user specified group id (GID)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 92 of 273
Adding and Removing Members to a Group
• Adding single or multiple users to the group with various attributes
• #gpasswd < option> <arguments> <group name>
• Options:
• -M For Adding Multiple users to a group
• -a for Adding a single user to a group
• -A for Adding a group Administrator
• -d removing a user from a group
Removing a group
• #groupdel <group name>; #groupdel mygroup
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 93 of 273
CONTROLLING ACCESS TO FILES
In this chapter we will be dealing with two things.
1. Special Permissions or Advanced Permission
2. Access Control List (ACL)
Example
• By default passwd command is having suid, so all users can run that command but if suid
is removed and a normal user wants to user execute it, then they would not be able to
use it to update /etc/shadow with new passwd.
Note: observe that in the permissions “–rwsr-xr-x” it contains an “s”, which means SUID is
placed.
• Let’s remove suid on passwd command and logged in as normal user and check the results
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 94 of 273
SGID – SET GROUP ID
Set group ID, used on executable files to allow the file to be run as if logged into the group
(like SUID but uses file group permissions)
SGID can also 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.
Example
• When a directory is created and its group is set to some group. Now if SGID is applied to
it, and the group member creates files and directory inside it, then it will get the same
group rather than getting user’s primary group
• Let’s see it practically.
• Login as other user, access the directory, create some files and check the group it is
getting. ( It will be getting the logged in user’s group)
• Try creating other files with other user(s) in the same directory, instead of getting it’s
own group it would now be inheriting directory’s group
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 95 of 273
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 or edit the contents
of the directory
• Let see it practically.
• Apply sticky bit to the directory
• Access the directory with other user and try deleting the files
Note: If the owner or root tries to modify or delete the contents it would be allowed
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 96 of 273
2. Access Control List (ACL)
• Define more fine-grained discretionary access rights for files and directories.
• Often, you want to share files among certain groups and specific users. It is a good practice
to designate a directory for that purpose. You want to allow those 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 given using ACL.
#getfacl /mydir
• Now let’s assign full permission to the directory and then apply acl on it, so that we can
analyze how acl will work.
• Okay, now we are ready to apply acl, but first lets understand the command and option
in details.
• The syntax to apply acl is
• #setfacl <option> < argument > < file or directory name >
• The options are,
• -m Modifies an ACL
• -x Removes an user/group from ACL
• -R Recurses into subdirectories
• -b completely banishing/removing the ACL from a file/directory
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 97 of 273
To assign read and execute permission to a particular user the syntax could be
• #setfacl –m u: <username>: <permissions> <file or dir name>
• #setfacl –m u:u1: rx /mydir
• Verify it by using getfacl command
• Now login as u1 user and try to create a file inside /mydir, as we have not assigned write
permission to u1 user, though it is having full permissions, still it will not allow u1 user to
create a file inside it.
Observe that when you check for the permissions it is showing a + sign after normal
permission, that indicate that ACL is applied on this directory.
Now you know how to apply acl on any file or directory, let me just give one more examples
which you can broaden your understandings.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 98 of 273
Assigning read and execute permission for a user and a group at same time.
• #setfacl –m u:u1:rx,g:g1:rx /mydir
Likewise you can explore applying acl to any user, group, or others in many ways.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 99 of 273
Removing all ACL permissions from a file or directory
• #setfacl –b <dir name>
• #setfacl –b /ktdir
As we have removed acl for a group and a user, let’s apply back some acl on ktdir and remove
it using above command
http://www.virtualpathtech.com
VirtualPath Techno Solutions
There are still much more experiments can be done, go ahead and read man pages for
more details
http://www.virtualpathtech.com
VirtualPath Techno Solutions
SUDO
• Sudo stands for either "substitute user do" or "super user do" (depending upon how you
want to look at it). What sudo does is incredibly important and crucial to many Linux
distributions. Effectively, sudo allows a user to run a program as another user (most often
the root user). There are many that think sudo is the best way to achieve "best practice
security" on Linux
• Users can login using their username and password and can issue administrative
commands placing sudo in front of the commands, e.g. sudo rpm -Uvh *.rpm , to run the
command which installs and updates programs in Linux (rpm).
• The file /etc/sudoers file has the rules that users have to follow when using sudo
command. That means that whatever commands access is provided to any user in
/etc/sudoers file, that user can only run those commands.
• Do not edit the /etc/sudoers directly; instead use "visudo" command to edit the sudoers
file. There are two reasons for that- it prevents two users from editing the file at the same
time, and it also provides limited syntax checking. Even if you are the only root user, you
need the syntax checking, so use "visudo".
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Now logged in as myuser and run admin commands like fdisk –l etc
• First try to run fdisk command normally and see what happens.
Note: Only for the first time of the session it will prompt for user’s password to continue,
but for rest of the process it will continue normally as shown below
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Now login as that user and check whether password is prompted or not
Note: - The same can be done for groups also, try it!
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Allow a group “mygroup” to run only network related commands as sudo user
• To allow a group run only network commands, first uncomment the following line
Observe that we have just remove ‘#’ before the line to make the line readable. And also
observe that it contains all networking commands.
• Just replace “ALL” with “NETWORKING” from the last field of mygroup line.
NOTE: - NETWORKING is the name of the command alias, which was uncommented line.
• Now login as one of the member of mygroup and try some commands assigned it.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Okay, now let’s create an alias for these commands and assign it to mygroup
• #visudo
• What are you waiting for! Assign it to ktgroup and save the file.
• Login as one of the users in mygroup and try newly added commands.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Login as myuser, try other commands and then go for blocked one and check whether
they are allowed or not
Note: As a user to know how many commands are allowed via sudo, login as a user and run
#sudo –l command
Note: Checkout sudoers file for more option and try it out on your own!!!!
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Networking:
It is a connection between two or more machines to communicate with each other.
2. Media
Media is the medium via which two different computer’s NIC card will be connected. The
best example for media is Cable. Example RJ 45, CAT 5 etc.
3. Topology
Topology is the scheme or design in which the computers in the network will be connected
to each other. Example for topology is Bus, Ring, star, mesh, tree topologies. The following
pictures explain it better.
Tree Network
Topology
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Examples for Protocols are TCP/IP (Transmission Control Protocol), UDP (User Datagram
Protocol), HTTP. The most widely and regularly used protocols for transferring data are
TCP and UDP. Let’s analyze some basic differences between TCP/IP and UDP.
TCP/IP UDP
Transmission Control Protocol User Datagram Protocol
It is connection Oriented Connectionless
Reliable Non-Reliable
TCP Acknowledgement will be No Acknowledgement for UDP
sent/received
Slow Communication Faster Communication
Protocol Number for TCP is 6 Protocol Number for UDP is 17
HTTP, FTP, SMTP uses TCP DNS, DHCP uses UDP
5. IP ADDRESS
An IP address can be thought of as being similar to a phone number. Just as every person
who communicates with a telephone is using a phone with a unique phone number, every
computer that is on the Internet has a unique IP address. Not only on internet but within
an organization every computer is assigned an IP address so that they can communicate
with each other. Basically IP addressing is very deep concept. To understand the concept
of IP address we need to understand some important aspect of IP Address which is
• IP Address Classes
• Subnet mask
• Gateway
The above concepts in IP Addressing are very important to understand networking clearly.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
The IP addresses are further broken down into classes. These classes are A, B, C, D, E and
their possible ranges can be seen in Figure below.
Loopback
A special IP number (127.0.0.1), that is designated for the software loopback interface of a
machine. 127.0.0.0 Through 127.255.255.255 is also reserved for loopback and is used for
internal testing on local machines.
Multicast
Multicasting allows a single message to be sent to a group of recipients. Emailing,
teleconferencing, are examples of multicasting. It uses the network infrastructure and
standards to send messages.
• Subnet Mask
A subnet mask allows users to identify which part of an IP address is reserved for the network
and which part is available for host use. By looking at the IP address alone, especially now
with classless inter-domain routing, users cannot tell which part of the address is which.
Adding the subnet mask or netmask gives users all the information needed to calculate
network and host portions of the address with ease. In summary, knowing the subnet mask
can allow users to easily calculate whether IP addresses are on the same subnet or not.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
A gateway is a network point that provides entrance into another network. On the Internet,
a node or stopping point can be either a gateway node or a host (end-point) node. Both the
computers of Internet users and the computers that serve pages to users are host nodes. The
computers that control traffic within your company's network or at your local Internet service
provider (ISP) are gateway nodes.
For example let’s say our network is 192.168. something and we want
to send a file to other computer on 10.10.network, so we need a gateway to communicate
between two computers of different networks.
RHEL6 RHEL7/8
#/etc/sysconfig/network (in rhel6) & #/etc/hostname (in rhel7) is the files which keeps the
information about the hostname assigned to the system. If you want to change the hostname
permanently, you need to change the hostname in this file.
RHEL6
RHEL7/8
#/etc/hosts a file which is responsible for resolving hostname into IP locally, in other word it
acts as local DNS if DNS server is not accessible. There is no change In this file in either versions
#/etc/resolv.conf is a file which keeps the address of DNS server to which the clients will be
accessing to resolve IP to hostname and hostname to IP.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
#host <hostname>
#host mylinux.kt.com
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note: The above change is temporary and will be last only till you are logged in, if you want
to change it permanently edit the /etc/sysconfig/network (rhel6) and /etc/hostname (rhel7)
file and then logout and login to confirm the change.
#vim /etc/sysconfig/network; #vim /etc/hostname delete the previous hostname and add
the new name.
Note: Once you logout and login again the change will be permanent, observe the highlighted
region above.
• hostnamectl command
In rhel7 a new command has been introduced to see the hostname/system details as well as
changing hostname permanently.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To activate a connection
#nmci con up <con name>
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To delete a connection
#nmcli con del <con-name>
Move the cursor to ‘Add’ to add a new connection and press Enter
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Assign the connection name, Device name and change the ipv4 from automatic to manual
to continue
http://www.virtualpathtech.com
VirtualPath Techno Solutions
move the cursor to “Quit” button and press Enter to quit the utility
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Rest of the process is same as we did earlier by modifying team by adding IP and
setting other parameters
Restart the team0 connection to update the changes and check the IP and team
details.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• SELinux is a security enhancement to Linux that allows users and administrators more
control over which users and applications can access which resources, such as files.
Standard Linux access controls, such as file modes (-rwxr-xr-x) are modifiable by the
user and applications that the user runs, whereas SELinux access controls are
determined by a policy loaded on the system and not changeable by careless users or
misbehaving applications.
• SELinux also adds finer granularity to access controls. Instead of only being able to
specify who can read, write or execute a file, for example, SELinux lets you specify who
can unlink, append only, and move a file and so on. SELinux allows you to specify
access to many resources other than files as well, such as network resources and inter-
process communication (IPC).
• SELinux provides a flexible Mandatory Access Control (MAC) system built into the
Linux kernel. Under standard Linux Discretionary Access Control (DAC), an application
or process running as a user (UID or SUID) has the user's permissions to objects such
as files, sockets, and other processes. Running a MAC kernel protects the system from
malicious or flawed applications that can damage or destroy the system. The following
picture explains more detailed about both Access controls.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note: If you want to turn on or off the SELinux security you need to make changes in the main
configuration file i.e. /etc/selinux/config file. Well we’ll see it later in this chapter.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• There are three modes in which SELinux can be at a time, they are
• Enforcing, Permissive and Disabled
• Enforcing
Enable and enforce the SELinux security policy on the system, denying access and
logging actions
• Permissive
Permissive mode is similar to Debugging Mode. In Permissive Mode, SELinux
policies and rules are applied to subjects and objects, but actions ( for example,
Access Control denials) are not affected. The biggest advantage of Permissive
Mode is that log files and error messages are generated based on the SELinux
policy implemented.
• Disabled
SELinux is turned off and no warn and log messages will be generated and stored.
Booleans
• Booleans are variables that can either be set as true or false. Booleans enhance the effect
of SELinux policies by letting the system administrator fine tune a policy. A policy may
protect a certain daemon or service by applying various access control rules. In real world
scenarios, a system administrator would not like to implement all the access controls
specified in the policy.
SELinux Policy
• The SELinux Policy is the set of rules that guide the SELinux security engine. It defines types
for file objects and domains for processes. It uses roles to limit the domains that can be
entered, and has user identities to specify the roles that can be attained. In essence, types
and domains are equivalent, the difference being that types apply to objects while
domains apply to processes.
SELinux Context
• Processes and files are labeled with a SELinux context that contains additional
information, such as a SELinux user, role, type, and, optionally, a level.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
#sestatus
RHEL6 RHEL7/8
Note: Observe that there is a small change between 6, 7&8, which the mount point
Observe that the type is admin_home_t, let’s change it to public_content_t, so that it will be
available for all users.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• To change the context for a directory and its contents, the syntax is
#chcon –R –t <argument > <dir name>
#chcon –R –t public_content_t mydir
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note: For restoring the context of only the dir except its contents do not add “R” in the
command.
• Let’s change the default context to something required for example samba_share_t
# semanage fcontext -a -t samba_share_t /myfile
• The -a option adds a new record, and the -t option defines a type (samba_share_t).
Note: running this command does not directly change the type - myfile is still labeled
with the etc_runtime_t type:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note1: if grep is not used it will list Booleans for all the services in the system and output will
be very lengthy.
Note2: Booleans can only be checked and changed when SELinux is in enforcing or Permissive
modes; if the SELinux is in disabled mode Booleans cannot be modified.
• To change any Boolean just copy the Boolean and give the option (the only possible option
for a Boolean to enable and disable is on/off). The syntax for changing Boolean value is
#setsebool < Boolean > < option (on/off) >
#setsebool allow_ftpd_anon_write on; Verify the change with getsebool command.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Now, edit the configuration file, restart the computer and check the status.
#vim /etc/selinux/config
#reboot (to reboot the system)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Have you ever wondered what happens behind the scenes from the time you press the power
button until the Linux login prompt appears?
The following are the 6 high level stages of a typical RHEL7/8 boot process.
TARGETS
Targets has replaced runlevels in 7/8
RHEL6 RHEL7/8
1. BIOS/UEFI
• BIOS stands for Basic Input/Output System, UEFI stands for Unified Extensible
Firmware Interface
• Performs some system integrity checks
• Searches, loads, and executes the boot loader program.
• It looks for boot loader in floppy, cd-rom, or hard drive. You can press a key (typically
F12 of F2, but it depends on your system) during the BIOS startup to change the boot
sequence.
• Once the boot loader program is detected and loaded into the memory, BIOS gives
the control to it.
• So, in simple terms BIOS/UEFI loads and executes the MBR/GPT boot loader.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• MBR stands for Master Boot Record, GPT stands for GUID Partition Table
• It is located in the 1st sector of the bootable disk. Typically /dev/hda, or /dev/sda
• MBR is less than 512 bytes in size. This has three components 1) primary boot loader
info in 1st 446 bytes 2) partition table info in next 64 bytes 3) mbr validation check in
last 2 bytes.
• It contains information about GRUB/GRUB2 (or LILO in old systems).
• So, in simple terms MBR loads and executes the GRUB boot loader.
3. GRUB2
• The default bootloader used on RHEL 7&8 is GRUB 2. GRUB stands for GRand Unified
Bootloader. GRUB 2 replaces the older GRUB bootloader also called as legacy GRUB.
• The GRUB 2 configuration file is located at /boot/grub2/grub.cfg and link at
/etc/grub2.cfg(Do not edit this file directly).
• GRUB 2 menu-configuration settings are taken from /etc/default/grub when
generating grub.cfg.
• If changes are made to any of these parameters, you need to run grub2-mkconfig to
re-generate the /boot/grub2/grub.cfg file
• #grub2-mkconfig –o /boot/grub2/grub.cfg
initramfs
• The job of the initial RAM file system is to preload the block device modules, such as for
IDE, SCSI, or RAID, so that the root file system, on which those modules normally reside,
can then be accessed and mounted.
• The initramfs is bound to the kernel and the kernel mounts this initramfs as part of a
two-stage boot process.
• The dracut utility creates initramfs whenever a new kernel is installed.
• Use the lsinitrd command to view the contents of the image created by dracut
4. Kernel
• Mounts the root file system as specified in the “root=” in grub2.conf
• Kernel executes the systemd program
Since system is the 1st program to be executed by Linux Kernel, it has the process id (PID) of
1. Do a ‘ps -ef | grep init’ and check the pid.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
6. Runlevel/Targets
• Prior to RHEL 7, runlevels were used to identify a set of services that would start or
stop when that runlevel was requested. Instead of runlevels, systemd uses the
concept of targets to group together sets of services that are started or stopped. A
target can also include other targets (for example, the multi-user target includes an
nfs target).
• Depending on your default init level setting, the system will execute the programs
from one of the following directories.
LAB WORK:-
To check the default run level in linux
• To see the default run level in linux the command is
#who –r
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• To start the graphical interface when you are in runlevel 3, use the following command
#startx
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Check back in grub2 config file whether the changes are applied
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Go to the end of kernel line “linux16”using ctrl+e, type rd.break. To continue booting
use ctrl+x
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• To change the password use command #passwd and change the passwd
• Okay, Now we have successfully changed the password, type exit to leave the /sysroot
and exit emergency mode. Once selinux policies relabeled system reboots, try the new
password for root user after reboot.
• There might be a situation where your boot loader i.e., GRub might got corrupted and you
want to recover it or in other word repair it. Basically the repairing of GRub means
installing a new grub on the existing one from RHEL 7/8 installation media/DVD.
• To recover the grub the steps are:
• Boot the system with RHEL 7/8 DVD
• Go to Troubleshooting and boot into Rescue installed system Mode.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Observe from above pic, that now your system has been mounted on /mnt/sysroot. It
means where our system root is residing
• Press Enter to continue.
• Change the DVD root to system root in order to access OS with root credentials by using
following command
#chroot /mnt/sysroot
http://www.virtualpathtech.com
VirtualPath Techno Solutions
All these steps are very critical and highly useful, please practice the stuff nicely.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Services are programs (called daemons) that once started run continuously in the
background and are ready for input or monitor changes in your computer and respond to
them. For example the Apache server has a daemon called httpd (the d is for daemon)
that listens on port 80 on your computer and when it receives a request for a page it sends
the appropriate data back to the client machine.
• Many services are required to run all the time however many can be safely turned of for
both security reasons as running unnecessary services opens more doors into your
computer, but also for performance reasons. It may not make much difference but your
computer should boot slightly faster with less services it has to start on boot.
• One of the techniques in every Linux administrator's toolbox to improve security of a box
is to turn off unneeded services.
Systemd is a system and service manager for Linux operating systems. It is designed to be
backwards compatible with SysV init scripts, and provides a number of features such as
parallel startup of system services at boot time, on-demand activation of daemons, support
for system state snapshots, or dependency-based service control logic. In Red Hat Enterprise
Linux 7, systemd replaces Upstart as the default init system.
Systemd is a replacement to the older traditional "System V init" system . systemd stands for
system daemon. systemd was designed to allow for better handling of dependencies and have
the ability to handle more work in parallel at system startup. systemd supports snapshotting
of your system and the restoring of your systems state, keeps track of processes stored in
what is known as a "cgroup" as opposed to the conventional "PID" method. systemd is now
shipping by default with many popular Linux distributions such as Fedora, Mandriva, Mageia,
Arch Linux, CentOS 7, RHEL 7.0 (Red Hat Enterprise Linux) and Oracle Linux 7.0. systemd refers
to runlevels as targets.
Systemd Description
0 poweroff.target
1 rescue.target
2 multi-user.target
3 multi-user.target
4 multi-user.target
5 graphical.target
6 reboot.target
http://www.virtualpathtech.com
VirtualPath Techno Solutions
service name status systemctl status name.service Check the status of the service
service --status-all systemctl list-units --type Displays the status of
service --all all services.
LAB WORK:-
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Reload the ftp services, may be required after doing some change in config file.
• To reload the service, the command is
#service crond reload
To restart the ftp or any service required when reload does not work
• To restart the ftp services, the command will be
#systemctl restart vsftpd
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• A firewall is a device that allows multiple networks to communicate with one another
according to a defined security policy. They are used when there is a need for networks
of varying levels of trust to communicate with one another. For example, a firewall
typically exists between a corporate network and a public network like the Internet. It can
also be used inside a private network to limit access to different parts of the network.
Wherever there are different levels of trust among the different parts of a network, a
firewall can and should be used.
• A firewall is a device that allows multiple networks to communicate with one another
according to a defined security policy. They are used when there is a need for networks
of varying levels of trust to communicate with one another. For example, a firewall
typically exists between a corporate network and a public network like the Internet. It can
also be used inside a private network to limit access to different parts of the network.
Wherever there are different levels of trust among the different parts of a network, a
firewall can and should be used.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• The Cockpit is a free and open source web-based server management tool. By default,
Cockpit comes preinstalled on an RHEL 8 server. But, it is not activated. A sysadmin must
enable it. One can see the server in a web browser and perform system tasks with a
GUI/mouse. It is easy to start containers, administer storage or users, configure networks,
and inspect log files on RHEL 8. The Cockpit web interface is user-friendly for new to Linux
users and seasoned sysadmins too.
• Cockpit is a useful Web based GUI tool through which sysadmins can monitor and manage
their Linux servers, it can also be used to manage networking and storage on servers,
containers, virtual machines and inspections of system and application’s logs.
It can be observed that while we login into any rhel8 machine, by default it gives a message
on the screen related to cockpit, as shown below.
In order to use cockpit start the service of cockpit and check whether in firewall the services
is allowed or not.
#systemctl enable --now cockpit.socket
http://www.virtualpathtech.com
VirtualPath Techno Solutions
By default cockpit is pre-allowed in firewall. If it is not pre-allowed, then it can be added later
by using following commands
#firewall-cmd --add-service=cockpit –permanent
#firewall-cmd –reload
Now then the cockpit services are active and allowed in firewall, we can now access it through
a browser using the following URL
https://<server IP>:9090
Note: Initially it may reflect privacy error, click on Advanced tab and proceed to continue
http://www.virtualpathtech.com
VirtualPath Techno Solutions
As you can see there are many options which can be explored to do various activities.
Go ahead and explore tabs for more activities, in-fact the many important activities can be
controlled or performed using cockpit.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
Virtualpath Techno Solutions
• Check the size of tar file by using du –h <file name > command
#du –h /opt/etc.tar
http://www.virtualpathtech.com
Virtualpath Techno Solutions
• Login to the remote system, remove gzip it and check the size.
• To gunzip a file the syntax is
#gunzip <file name>
#gunzip etc.tar.gz
Untar the file and check for the size of the file/directory
• To untar a file the syntax is
#tar -xvf <file name>
#tar –xvf etc.tar
http://www.virtualpathtech.com
Virtualpath Techno Solutions
• 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. For Red Hat or any
other Linux, this process is handled by the cron service or a daemon called crond, which
can be used to schedule tasks (also called jobs). By default, Red Hat comes with a set of
predefined jobs that occur on the system (hourly, daily, weekly, monthly, and with
arbitrary periodicity). As an administrator, however, you can define your own jobs and
allow your users to create them as well.
• 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 by scheduling a cron job. If the cron job is scheduled
carefully than the backup will be taken at any given time of the client and there will be no
need for the administrator to remain back at nights to take the backup.
Crontab format
• To assign a job in the Crontab file the format used is the following
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Treats for any value between 9 and 17. So if placed in day of month this
9-17
would be days 9 through 17. Or if put in hours it would be between 9 and 5.
Crontab Commands
Command Explanation
crontab –e Edit your crontab file, or create one if it doesn’t already exist.
crontab –l Display your crontab file.
crontab –r Remove your crontab file.
crontab -u If combined with –e, edit a 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
LAB WORK:-
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Wait for a minute and check whether time is displaying or not. Every min time will be
displayed as below.
Schedule a cron job to create a directory “mydir” under “/root” on “Sunday 22 October at
1:30 AM”
• To schedule above job edit the crontab file as shown below and restart the service
#crontab –e
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Schedule a job so that a user “myuser” should get a mail regarding meeting on 24th, 29th and
31st October at 2:25 PM.
• To set above task edit the crontab in following passion, and restart the service
#crontab –e –u <user name>
#crontab –e –u myuser
#crontab –eu myuser
Schedule a job so that a user “myuser” should get the mail from 15th to 20th and 25th to 30st
November as a reminder of some session at 2:25 PM
• This task is very much similar to the previous one but there is only a small change in
format.
#crontab –e –u myuser
#crontab –eu myuser
• There are still various method you can schedule the cron jobs, Do some R&D on it to find
out more.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• If you want to allow them to use cron job facilities again, remove their names from
/etc/cron.deny file.
Allow only two users “musab” and “rahul” to use cron jobs out of all the users in the system
• Assuming that we have 100 users in our system, putting all 98 names in /etc/cron.deny
file is a time consuming process. Instead of that, we can create one more file
/etc/cron.allow, in which we can assign names of those users who are allowed to use cron
jobs.
• Remove the /etc/cron.deny file and create /etc/cron.allow, still if both files are existing
cron.allow file will be having precedence over cron.deny file. Just to avoid confusion it is
good to remove cron.deny file
Note: /etc/cron.deny file exists by default, but we need to create /cron.allow file. If your
name is not there in cron.allow file then you will not be allowed to use cron jobs, and as
mentioned above, if both files are existing cron.allow file will be having precedence over
cron.deny file. If neither cron.deny nor cron.allow files exists then only root can use cron
jobs.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note: To see man pages on cron job use #man 4 crontabs command
All the above are few examples to use cron, do some constant R&D’s to know more about
it.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
SSH, which is an acronym for Secure SHell, was designed and created to provide the best
security when accessing another computer remotely. Not only does it encrypt the session, it
also provides better authentication facilities.
The second diagram shows how the data in an encrypted connection like SSH is encrypted on
the network and so cannot be read by anyone who doesn't have the session-negotiated keys,
which is just a fancy way of saying the data is scrambled. The server still can read the
information, but only after negotiating the encrypted session with the client.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
The first time around it will ask you if you wish to add the remote host to a list of known_hosts,
go ahead and say yes.
• Enter the password of the remote system correctly, once logged in check hostname and
ip address to confirm login.
• To leave the session, just type exit or logout command and you will be back to your own
machine through which you are logged in.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
LAB WORK
It will prompt above to mention the file where these keys shoud be stored, to keep its default
directory just press “Enter”. The default location will be /root/.ssh/ directory
Now it will ask for passphrase, which will be used instead of password. The passphrase will
only be asked every time you connect from other machine instead of its original password.
Enter your desired passphrase twice, or leave it blank for no passphrase and press enter.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
#cd /root/.ssh
• The id_rsa is a private key and id _rsa.pub is the public key which will be used later to
make password less login.
Enter the password of the client to proceed, check it on client side whether it is copied or not
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Try login to the client machine using SSH, check whether it is asking for password
• For logging into client machine the procedure is same as shown earlier.
• #ssh 192.168.10.95
LAB WORK:
To copy a file using SCP to remote machine from source location
• We are having a file myfile in “/” directory, in the server linux.mb.com who’s IP is
192.168.10.98, and we need to copy the same in other server’s i.e. cl5.mb.com with an
IP 192.168.10.95, /root directory.
• The syntax for SCP a file from source location.
#scp <file name > <remote hosts IP >:/<location to copy the file >
#scp /myfile 192.168.10.95:/root/
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To copy a file using SCP from a remote machine being in destination’s location(reverse scp)
• Let’s reverse the previous task, login to cl5 machine whose IP is 192.168.10.95, and
transfer a file from linux machine whose IP is 192.168.10.98
• Let’s first remove the earlier copied file myfile, then copy it again from destination’s
location.
• The syntax for SCP a file from destination location.
#scp <source system’s IP>:/<location of file to be copied> <destination location to copy>
Note: Password will be asked for every transfer if public key is not saved on both locations,
in our case we have already generated and copied the key, hence there is no password
prompts.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
LAB WORK:-
Copy a directory using SCP, then update it and try rsync with SSH and check if the data is
synced.
• As we have already copy a directory earlier using SCP from linux to cl5 system, let’ s use
it for rsync.
• Update the directory with some files in linux machine
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Use rsync to sync the directory on cl5 machine, with the one in linux machine
• The syntax to rsycn a directory is
#rsync <options> <encryption> <source dir> <destination IP>:/<location of destination
dir>
#rsync –rv -e ssh /mydir 192.168.10.95:/root/
Observe that it is only copying the files which are not there in destination’s folder.
Note: If you don’t want to use ssh just remove –e option from above syntax, but the drawback
of it is there will be no encryption
• To compress the data and send it in archive mode use -avz instead of -rv in rsync
Sync a file using rsync with ssh
• Let’s check the file called file1 on both linux and cl5 machines
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Like this you can use rsync in many ways to transfer the updated file or files/directory to
other system.
• --dry-run : dry-run is used to see the preview of transfer before doing actual transfer
Syntax: rsync –avz –e ssh /mydir 192.168.104.82:/opt --dry-run
• Reverse rsync : reverse rsync is referred to sync folder from destination to source
machine. It is exactly same like reverse scp seen in scp chapter
Syntax: rsync –avz –e ssh 192.168.104.82/opt/mydir /mydir
Where 192.168.104.82/opt/mydir is remote machine directory and /mydir is local
machine directory
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Features:
• RPM can verify software packages.
• RPM can be served as a powerful search engine to search for software’s.
• Components, software’s etc can be upgraded using RPM without having to reinstall
them
• Installing, reinstalling can be done with ease using RPM
• During updates RPM handles configuration files carefully, so that the customization
is not lost.
LAB WORK:-
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• One more method of checking the installed package, when you are not sure about the
package name, like whether it starts with capital letter and full name etc.
#rpm –qa | grep –i < package name>
#rpm –qa |grep –i vsft*
To check whether a package is consistent or not, before installing it. (Testing the
installation)
• To check the package’s consistency,
• Move to the directory where you have kept the rpm package which you wish to install
If the installation status shows 100%, then the package is good or consistent.
But while showing the hash progress if it shows any error, then the package is
inconsistent.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Check the installed package by using it command; finger is used to check user’s details.
#finger <user name>
#finger myuser
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Okay, so we know the package of mount let’s copy other commands content over mount
command. Let copy date command’s contents over mount command.
#cp /usr/bin/date /usr/bin/mount
• Now when you run mount command it will show date, that means it is corrupted.
• So, to fix the mount command we need to reinstall its package, let’s install the package
and check whether mount command is fixed or not. Move to the folder where you kept
the packages and install it
#rpm –ivh util-linux-ng 2.17.2-6.el6.i686
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Oops…!!! It isn’t fixed yet, now to fix it, force installation is to be done, the syntax is
#rpm –ivh <package name > - - force
# rpm –ivh util-linux-ng 2.17.2-6.el7_x86_64 - -force
Okay then, we’ve not only installed the package successfully but we have also fixed the
command. Congratulations.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Now run the following command and check for the result.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• The Yellow dog Updater Modified (YUM) is a package management application for
computers running Linux operating systems.
• Yum is a standard method of managing the installation and removal of software. Several
graphical applications exist to allow users to easily add and remove packages; however,
many are simply friendly interfaces with yum running underneath. These programs
present the user with a list of available software and pass the user's selection on for
processing. It is yum that actually downloads the packages and installs them in the
background.
• Packages are downloaded from collections called repositories, which may be online, on a
network, and/or on installation media. If one package due to be installed relies on another
being present, this dependency can usually be resolved without the user needing to know
the details. For example, a game being installed may depend on specific software to play
its music. The problem of solving such dependencies can be handled by yum because it
knows about all the other packages that are available in the repository.
• Yum will work only from Cantos 5 / Red hat 5 and latest versions of fedora. For Old
releases like RHEL 4 you need to use up2date command to update your rpm based
packages.
LAB WORK:-
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Copy entire RHEL7 DVD to “/var/ftp/pub/rhel7” directory, Where rhel7 dir is to be created
manually, it is not a default directory
• First make an directory “rhe7” under /var/ftp/pub
#mkdir /var/ftp/pub/rhel7
• Now copy the RHEL6/7 DVD to /var/ftp/pub/rhel directory with its default permission
#cp –rvfp /run/media/root/RHEL-7.1\ Server.x86_64/* /var/ftp/pub/rhel7
Note:- it will take around 3-5 minutes copy all the data, based on the DVD
http://www.virtualpathtech.com
VirtualPath Techno Solutions
I guess there’s some explanation requires about the fields we have entered.
• [MYREPO] is the repo ID, which will be displayed while using yum repository.
• name is the name given for the repository.
• baseurl is the location of the dvd dump we have made.
• enabled is to enable or disable the repository. The possible value for it is 0 and 1, where
0 means disable and 1 means enabled.
• gpgcheck gpgcheck= GNU privacy guard. gpgcheck used for signature verification from its
central database. If signature verification is successful then you sure about the security. If
you set the value of gpgcheck is 1 then it asks for signature varification else it doesn’t.
• Example of gpgkey in RHEL- RPM-GPG-KEY-redhat-release
Clean the yum cache and check the package list using yum command
• To clear the cache use the following command
#yum clean all
If the configuration is correct, then the following output will be displayed, otherwise there
will be some errors displayed.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Configure the yum client and check whether yum server is responding to it.
Configuring a yum client is very simple with just three steps.
• Make a repo file /etc/yum.repo.d/ as “mycl.repo”
• Clean the cache and check whether yum server is responding or not
Clean the cache and check whether yum server is responding or not
• Just clean the cache as we have done earlier in server’s configuration.
If your output is like this then you have successfully configured a yum client as well.
Congrats!!! Now you can configure as many as clients you want.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• If it is not installed, mount RHEL8 DVD on some mount point and navigate to “Packages”
directory and install it as shown below.
• As we know the mount point of DVD is /media, move to its location and enter into
AppStream/ Packages directory and install vsftpd package using rpm
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note: make sure around 8 GB space is free in /var to copy the data
• Now copy the RHEL8 DVD to /var/ftp/pub/rhel directory with its default permission
#cp –rvfp /media/* /var/ftp/pub/rhel8
Note:- it will take around 3-5 minutes copy all the data, based on the DVD
I guess there’s some explanation requires about the fields we have entered.
• [MYREPO] is the repo ID, which will be displayed while using yum repository.
• name is the name given for the repository.
• baseurl is the location of the dvd dump we have made.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Clean the yum cache and check the package list using yum command
• To clear the cache use the following command
#dnf clean all
If the configuration is correct, then the following output will be displayed, otherwise there
will be some errors displayed.
In order to share files with ftp, make the following changes in ftp config
#vim /etc/vsftpd/vsftpd.conf
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Clean the cache and check whether yum server is responding or not
• Just clean the cache as we have done earlier in server’s configuration.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
It will prompt you for y/n (Yes/No) to continue, type y and continue installing the package
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
As there are no updates available for it, it is not showing anything to update
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To reinstall a package
#yum reinstall <package name> -y or #dnf reinstall <package name>
The software management can be learnt more by using manual pages like man yum or dnf
and also man rpm etc.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• When you start a program or running an application in Linux, you actually execute that
program. A Linux process (a daemon), running in foreground or in the background, uses
memory and CPU resources. That's why we need to manage Linux process. Keeping
unused Linux process running in the system is a waste and also exposes your system to
security threat.
• In Linux, every running process or daemon is given an identity number called PID (Process
ID). The process id is unique. We can terminate unused program in the system by stopping
its process id.
• In order to manage Linux processes, we need to identify some process information such
as who's responsible for the process, which terminal the process is running from and what
command used to run the process.
• Interactive Processes
• System Process or Daemon
• Automatic or batch
Interactive Processes
Interactive processes are those processes that are invoked by a user and can interact with the
user. VI is an example of an interactive process. Interactive processes can be classified into
foreground and background processes. The foreground process is the process that you are
currently interacting with, and is using the terminal as its stdin (standard input) and stdout
(standard output). A background process is not interacting with the user and can be in one of
two states - paused or running.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Any process can become a daemon as long as it is run in the background, and does not interact
with the user. A simple example of this can be achieved using the [ls -R] command. This will
list all subdirectories on the computer, and is similar to the [dir /s] command on Windows.
This command can be set to run in the background by typing [ls -R &], and although technically
you have control over the shell prompt, you will be able to do little work as the screen displays
the output of the process that you have running in the background. You will also notice that
the standard pause (ctrl+z) and kill (ctrl+c) commands do little to help you.
Automatic Processes
Automatic processes are not connected to a terminal. Rather, these are tasks that can be
queued into a spooler area, where they wait to be executed on a FIFO (first-in, first-out) basis.
Such tasks can be executed using one of two criteria:
At times when the total system load is low enough to accept extra jobs: done using the Cron
command. By default, tasks are put in a queue where they wait to be executed until the
system load is lower than 0.8. In large environments, the system administrator may prefer
cron job processing when large amounts of data have to be processed or when tasks
demanding a lot of system resources have to be executed on an already loaded system. Cron
job processing is also used for optimizing system performance.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To see which process are attached with some terminals (tty) and which are not
• #ps –x
Note: The process which are showing “?” are not attached to any tty, mostly background
processes
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To see the auxiliary information of all the process, like cpu and memory consumptions
#ps –aux
Signals in Linux
• Signals are a way of sending simple messages to processes. Most of these messages are
already defined and can be found in <linux/signal.h>. However, signals can only be
processed when the process is in user mode. If a signal has been sent to a process that is
in kernel mode, it is dealt with immediately on returning to user mode.
• Every signal has a unique signal name, an abbreviation that begins with SIG (SIGINT for
interrupt signal, for example). Each signal name is a macro which stands for a positive
integer - the signal number for that kind of signal. Your programs should never make
assumptions about the numeric code for a particular kind of signal, but rather refer to
them always by the names defined. This is because the number for a given kind of signal
can vary from system to system, but the meanings of the names are standardized and
fairly uniform.
• 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.)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Likewise you can use other signals to kill the process like
#kill -15 <pid>
#kill -1 <pid>
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• When talking about processes priority is all about managing processor time. The Processor
or CPU is like a human juggling multiple tasks at the same time. Sometimes we can have
enough room to take on multiple projects. Sometimes we can only focus on one thing at
a time. Other times something important pops up and we want to devote all of our energy
into solving that problem while putting less important tasks on the back burner.
• In Linux we can set guidelines for the CPU to follow when it is looking at all the tasks it has
to do. These guidelines are called niceness or nice value. The Linux niceness scale goes
from -20 to 20. The lower the number the more priority that task gets. If the niceness
value is high number like 20 the task will be set to the lowest priority and the CPU will
process it whenever it gets a chance. The default nice value is zero.
• By using this scale we can allocate our CPU resources more appropriately. Lower priority
programs that are not important can be set to a higher nice value, while high priority
programs like daemons and services can be set to receive more of the CPU’s focus. You
can even give a specific user a lower nice value for all of his/her processes so you can limit
their ability to slow down the computer’s core services.
• There are two options to reduce/increase value of a process. You can either do it using
the nice command or the renice command.
LAB WORK:-
• Log in to other terminal and check the nice value for the above command/ process.
#ps –elf
http://www.virtualpathtech.com
VirtualPath Techno Solutions
1. CPU
2. MEMORY
3. I/O (INPUT OUTPUT)
4. NETWORK
http://www.virtualpathtech.com
VirtualPath Techno Solutions
#cat /proc/cpuinfo
#vmstat
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To continuously repeat the output of iostat every second and to restrict the same for 3
counts
#iostat 1, #iostat 1 3
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• “02:23:18″ is the current time; “up 1 day” shows how long the system has been up for;
“3 user” how many users are logged in; “load average: 0.01, 0.00, 0.23″ the load average
of the system (1minute, 5 minutes, 15 minutes).
• Shows CPU utilization details. “9.5%us” user processes are using 9.5%; “31.2%sy” system
processes are using 31.2%; “27.0%id” percentage of available cpu; “7.6%wa” time CPU is
waiting for IO.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• “543948k total” is total memory in the system; “526204K used” is the part of the RAM
that currently contains information; “17744k free” is the part of RAM that contains no
information; “17748K buffers and 129928k cached” is the buffered and cached data for
IO.
By default, top starts by showing the following task's property:
Field Description
PID Process ID
USER Effective User ID
PR Dynamic priority
NI Nice value, also known as base priority
VIRT Virtual Size of the task. This includes the size of process's executable
binary, the data area and all the loaded shared libraries.
RES The size of RAM currently consumed by the task. Swapped out portion
of the task is not included.
SHR Some memory areas could be shared between two or more task, this
field reflects that shared areas. The example of shared area are shared
library and SysV shared memory.
S Task status
%CPU The percentage of CPU time dedicated to run the task since the last
top's screen update.
%MEM The percentage of RAM currently consumed by the task.
TIME+ The total CPU time the task has been used since it started. "+" sign
means it is displayed with hundredth of a second granularity. By
default, TIME/TIME+ doesn't account the CPU time used by the task's
dead children.
Command Showing program names
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To renice a process with PID 4, then press “r” and a prompt will ask you for PID enter
4 and press enter. When prompted for renice value give any value .
Find out more on top command from internet and keep practicing
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Package : nfs-utils
• Daemons : nfs-server.service
rpc.nfsd, rpc.mountd, rpc.statd, rpc.lockd, rpc.rquotad
• Port number : 2049
• Configuration File : /etc/exports
• Other imp files : /var/lib/nfs/etab, /var/lib/nfs/rmtab
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step2: Create a directory or create a partition and mount it and make a mount point and
add data to it.
• Create a partition, format it and mount it, access the mount point and add data to it
#fdisk /dev/vda create a partition
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step3: Export the directory by editing /etc/exports file and using exportfs command
• Edit the /etc/exports file
#vim /etc/exports
Mount Options
Dir name Network range permission sync the disk before command completion
Options:
-a Exports or un-exports all directories
-r Reexport all directories
-u Unexports one or more directories
-v Provides verbose output
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step3: Check which directory is exported for this machine using showmount command
• To check the exported directories from server the syntax is
#showmount –e <server ip address>
Note: At first it may show such error, due to firewall blocking some important services on
server side. To resolve it login to server and allow following services in firewall.
• #firewall-cmd --add-service=rpc-bind --permanent
• #firewall-cmd --add-service=mountd --permanent
• #firewall-cmd –reload
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Autofs automatically mounts file systems for you when they are requested. This has a
very handy feature: It's great for handling removable media. Just CD to the right
directory, or execute ls or do anything that sends a request to the mount point: and the
daemon mounts it. After all, it's the kind of job that's beneath the dignity of a human
being First; you need to install the "autofs" package. It should include some appropriate
config files. The files you need is /etc/auto.master
1. Direct Auto-Mounting:
In direct mounting for each partner server a mount point (dir) needs to be created.
For example if there are 10 nfs share to be mounted at client side, there must be 10
directories created and managed manually.
2. Indirect Auto-Mounting:
In this type of mounting for all NFS server shares, only one mother directory needs
to be created and for each server a sub-directory will be automatically created and
used for mounting.
Note: Before going for Auto-mounting remove all kind of mounting done previously
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step1: Log into client side and check whether autofs is install or not, if not install autofs
• Check whether autofs is install or not
#rpm –q autofs
Standby time
Dir to mount NFS auto mount configuration file for this mount point
Step3: Create /etc/auto.nfs file and /nfscl directory if not created earlier
• #vim /etc/auto.nfs
Name to be used for sub-dir Permissions NFS server and directory name
Step5: log into the given directory given in /etc/auto.master i.e. /nfscl and check that if NFS
is mounted by mount command
Note: Still NFS dir will not be mounted on the client side
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Standby Time
Direct mount auto mount configuration file for this mount point
Step2: Edit /etc/auto.nfs file and /nfscl directory if not created earlier
• #vim /etc/auto.nfs
Path of the directory to be used Permissions NFS server and directory name
Step5: log into the given directory given in /etc/auto.master i.e. /nfscl and check that if NFS
is mounted by mount command
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step2: Delete the entries of the NFS share you want to remove, from /etc/exports on the
server and un-export all the directory which was exported earlier using following command
• # exportfs –auv
Note: - if you don’t have DNS and still want to use hostname instead of IP, update
hostname with its ip in /etc/hosts file and then you can use hostname instead of IP
To check ports and protocol on nfs server from client side, the following commands can be
used
#rpcinfo –p <server ip>
Finally we are done with all the NFS practical. Do hands on practice on it, as it is important
in real world
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• The whole point of networking is to allow computers to easily share information. Sharing
information with other Linux boxes, or any UNIX host, is easy—tools such as FTP and NFS
are readily available and frequently set up easily “out of the box”. Unfortunately, even the
most die-hard Linux fanatic has to admit the operating system most of the PCs in the world
are running is one of the various types of Windows. Unless you use your Linux box in a
particularly isolated environment, you will almost certainly need to exchange information
with machines running Windows. Assuming you're not planning on moving all of your files
using floppy disks, the tool you need is Samba.
• Samba is an implementation of a Common Internet File System (CIFS, also known as SMB)
protocol server that can be run on almost every variant of Unix in existence. Microsoft
clients will use this protocol to access files and printers located on your Unix
box just as if it were a native Windows server.
• Samba allows linux computers to share files and printers across a network connection. By
using its SMB protocol, your linux box can appear in Windows Network Neighborhood or
My Network Places just like any other windows machine. You can share files this way, as
well as printers. By using samba on my home network, for example, my Windows
machines have access to a printer directly hooked up to my Linux box, and my Linux box
has access to a printer directly hooked up to one of my Windows machines. In addition,
everyone can access everyone else's shared files. You can see how samba can be very
useful if you have a network of both Windows as well as Linux machines.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
.
Step3: Check the context of the directory and change it according to samba
• #ls –ldZ /samba
• #chcon –t samba_share_t /samba
Step4: Create a user or use any existing user who will be allowed to log in as samba user,
add that user to samba user
• As we have a existing user “myuser”, let’s just make it samba user
#smbpasswd –a <username>
#smbpasswd –a myuser
Give password twice and wait till it add the user
Step5: Go to the sample configuration file i.e. /etc/samba/smb.conf.example and copy the
last paragraph as shown below
• Open the /etc/samba/smb.conf and paste the copied paragrah shown below and edit
it.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step5: Test the samba parameters and restart the service and make it enable after reboot
• To test the parameters us the following command
#testparm
http://www.virtualpathtech.com
VirtualPath Techno Solutions
To connect from windows to the samba server, Right click on My Computer icon select
http://www.virtualpathtech.com
VirtualPath Techno Solutions
It will prompt for user name and passwd, give samba user and passwd and click on OK
• A window will be opened, start adding some data to it from both sides
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Check the share name of that samba server by using following command
#smbclient –L //192.168.10.93
when prompted for passwd just press enter without giving any passwd
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note: To learn how to make samba permanent mount and auto-mount visit my website
http://musab.in/2014/02/making-samba-permanent-and-auto-mount-in-linux/
http://www.virtualpathtech.com
VirtualPath Techno Solutions
The Domain Name System (DNS) is the crucial glue that keeps computer networks in harmony
by converting human-friendly hostnames to the numerical IP addresses computers require to
communicate with each other. DNS is one of the largest and most important distributed
databases the world depends on by serving billions of DNS requests daily for public IP
addresses. Most public DNS servers today are run by larger ISPs and commercial companies
but private DNS servers can also be useful for private home networks.
Like the telephone system, every device attached to the Internet has a unique number, its IP
address. Also like the telephone system there is a directory services to help you find those
numbers called DNS.
If you have someone's name and address you can call a directory services, give them the
details you know and they will (usually) give you the telephone number to call them. Likewise,
if you know a server's host name (maybe http://www.google.co.in/) you can give that name
to a DNS server and it will give you the IP address of that server.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
A database is made up of records and the DNS is a database. Therefore, common resource
record types in the DNS database are:
• A - Host's IP address. Address record allowing a computer name to be translated into
an IP address. Each computer must have this record for its IP address to be located.
These names are not assigned for clients that have dynamically assigned IP addresses,
but are a must for locating servers with static IP addresses.
• PTR - Host’s domain name, host identified by its IP address
• CNAME - Host’s canonical name allows additional names or aliases to be used to locate
a computer.
• MX - Host’s or domain’s mail exchanger.
• NS - Host’s or domain’s name server(s).
• SOA - Indicates authority for the domain (Start of Authority)
• TXT - Generic text record
• SRV - Service location record
• RP - Responsible person
• HINFO - Host information record with CPU type and operating system
The package which is used in Linux for performing DNS activity is BIND (Berkeley Internet
Name Domain)
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step5: Navigate to /var/named/ directory and create a forward and reverse zone files.
• Navigate to /var/named/ directory and copy the named.localhost file with its
permissions as my.flz and edit it.
#cd /var/named
#cp –p named.localhost my.flz
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Copy again named.localhost, this time as my.rlz or copy my.flz to my.rlz to avoid re-
typing common entries in both files, and edit it as shown below.
#cp –p my.flz my.rlz
#vim my.rlz
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Okay, now we’ve done with DNS server configuration, check whether it is resolving IP to
hostname and hostname to IP using various commands.
• Using dig command to check the DNS resolution
• Check with giving hostname of server
#dig <FQDN> of server/#dig mlinux3.vpts.com
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• With IP address:
#dig –x 192.168.10.40
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Now check with any of the options used previously like dig, ping, host or nslookup for
dns resolution
Do the same for every client and check it with various commands on every client
Also make sure that hostname should Fully Qualified Domain Name.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Electronic mail is one of the best way to communicate for computer users anywhere in the
world. If I wanted to write an email message to my friend who is sitting somewhere in the
world, I simply open up my outlook-click on compose-type my friends email address in the
“to box-mention” the subject-draft the message-attach files (if needed)-click on send. That’s
it. This is what I do to send an email to my friends. Not only me, all the computer users will
do the exact same thing. But for most of the time i didn’t know how the mail flow takes place.
How the transfer takes place and how will it reach the recipient and the intermediate process
and so on....
Mail Delivery Agent: It is an agent which is responsible for delivery of mail to the devices like
laptop, desktop, mobiles and tabs etc. lmtp, pop3, imap4 etc., are the examples of MDA
SMTP
Simple Mail Transfer Protocol will transfer the mails between the MTA’s
Let’s take a deeper look into this with a small example. The below picture will depict how mail
flow takes place between sender and recipient.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Lab Work:
Pre-requisite: DNS should be configured and server as well as client should be participant
of it
Step 1: Check the hostname of the system
#hostname
Note: Most probably postfix will be pre-install, so only dovecot is needed to be installed
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Check the mail que, to see if the mail has flown out or not
#mailq
Note: If mailq is empty, that means the mail has been sent
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Also test mails from between two different clients to test Relay host functionality.
Note: If internet connection is available and you have a registered domain on internet,
then you can send the mails directly to any mail address in the world, but unregistered
domains mail will be rejected..
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• Every Web site sits on a computer known as a Web server. This server is always
connected to the internet. Web servers are computers that deliver (serves up) Web
pages. Every Web server has an IP address and possibly a domain name.
• A web server can mean two things - a computer on which a web site is hosted and a
program that runs on such a computer. So the term web server refers to both
hardware and software.
• A web server is what makes it possible to be able to access content like web pages or
other data from anywhere as long as it is connected to the internet. The hardware
houses the content, while the software makes the content accessible through the
internet.
• The most common use of web servers is to host websites but there are other uses like
data storage or for running enterprise applications. There are also different ways to
request content from a web server. The most common request is the Hypertext
Transfer Protocol (HTTP), but there are also other requests like the Internet Message
Access Protocol (IMAP) or the File Transfer Protocol (FTP).
http://www.virtualpathtech.com
VirtualPath Techno Solutions
A simple exchange between the client machine and Web server goes like this:
1. The client's browser dissects the URL in to a number of separate parts, including address,
path name and protocol.
2. A Domain Name Server (DNS) translates the domain name the user has entered in to its IP
address, a numeric combination that represents the site's true address on the Internet (a
domain name is merely a "front" to make site addresses easier to remember).
3. The browser now determines which protocol (the language client machines use to
communicate with servers) should be used. Examples of protocols include FTP, or File Transfer
Protocol, and HTTP, Hypertext Transfer Protocol.
4. The server sends a GET request to the Web server to retrieve the address it has been given.
For example, when a user types http://www.example.com/1.jpg, the browser sends a GET
1.jpg command to example.com and waits for a response. The server now responds to the
browser's requests. It verifies that the given address exists, finds the necessary files, runs the
appropriate scripts, exchanges cookies if necessary, and returns the results back to the
browser. If it cannot locate the file, the server sends an error message to the client.
5. The browser translates the data it has been given in to HTML and displays the results to the
user.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step2: Navigate to the document root folder i.e. /var/www/html/ and create an
index.html file which will be accessed through a web browser
• #vim /var/www/html/index.html
Step4: Access the Server via web browser like Firefox, etc.
• Open Firefox web browser and type the IP Address of the web server
http://192.168.106.81
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• To open the website from command line use the following command
#curl <IP/HOSTNAME of web server>
#curl 192.168.106.81
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note: This will only work in your DNS range, from others who are not in DNS use ip
addresses
• Create an index.html
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step3: Navigate to /etc/httpd/httpd.conf and copy the old file i.e., msw.conf with new
name for new website
#cd /etc/httpd/conf.d/httpd
#cp msw.conf port.conf
#vim /etc/httpd/conf.d/port.conf
http://www.virtualpathtech.com
VirtualPath Techno Solutions
• With web browsers that support HTTP/1.1 (as nearly all now do), upon connecting to a
webserver, the browsers send the hostname from the address that the user typed into
their browser's address bar along with the requested resource itself to the web server.
The server can use the Host header field to determine which web site (or virtual host), as
well as page, to show the user. The browser specifies the address by setting the Host HTTP
header with the host specified by the user. The Host header is required in all HTTP/1.1
requests.
• For instance, a server could be receiving requests for two domains, www.example.com
and www.example.net, both of which resolve to the same IP address. For www.example.com,
the server would send the HTML file from the directory /var/www/user/Joe/site/, while
requests for www.example.net would make the server serve pages
from /var/www/user/Mary/site/.
• Example: A blog server can be hosted using Name base hosting. blog1.example.com
and blog2.example.com
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step1: Make a directory in document root i.e. /var/www/ with some name say “ktname”
#mkdir /var/www/name
Step2: Update the DNS zone configuration files with the new hostname of the web server
#vim /var/named/my.flz
#vim /var/named/my.rlz
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Hypertext transfer protocol secure (HTTPS) is the secure version of HTTP, which is the primary
protocol used to send data between a web browser and a website. HTTPS is encrypted in
order to increase security of data transfer. This is particularly important when users transmit
sensitive data, such as by logging into a bank account, email service, or health insurance
provider.
Any website, especially those that require login credentials, should use HTTPS. In modern web
browsers such as Chrome, websites that do not use HTTPS are marked differently than those
that are. Look for a green padlock in the URL bar to signify the webpage is secure. Web
browsers take HTTPS seriously; Google Chrome and other browsers flag all non-HTTPS
websites as not secure.
1. The private key - this key is controlled by the owner of a website and it’s kept, as the
reader may have speculated, private. This key lives on a web server and is used to
decrypt information encrypted by the public key.
2. The public key - this key is available to everyone who wants to interact with the server
in a way that’s secure. Information that’s encrypted by the public key can only be
decrypted by the private key.
With HTTPS, traffic is encrypted such that even if the packets are sniffed or otherwise
intercepted, they will come across as nonsensical characters. Let’s look at an example:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
In websites without HTTPS, it is possible for Internet service providers (ISPs) or other
intermediaries to inject content into webpages without the approval of the website owner.
This commonly takes the form of advertising, where an ISP looking to increase revenue injects
paid advertising into the webpages of their customers. Unsurprisingly, when this occurs, the
profits for the advertisements and the quality control of those advertisements are in no way
shared with the website owner. HTTPS eliminates the ability of unmoderated third parties to
inject advertising into web content.
Note: in this case httpd and openssl are already installed during httpd server configuration.
2. Create an self-signed SSL certificate and RSA private key to be used for securing website
#openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
Note: answer the questions asked to complete certificate and key creation
http://www.virtualpathtech.com
VirtualPath Techno Solutions
6. Create a new directory in /var/www/html with the name myweb to hold website data
and create index.html file.
7. Start the service of httpd and make it enabled to be started at boot time automatically
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://www.virtualpathtech.com
VirtualPath Techno Solutions
http://musab.in/2018/03/configuring-proxy-and-dhcp-on-centos-rhel7/
http://www.virtualpathtech.com
VirtualPath Techno Solutions
A proxy server is one that receives requests intended for another server and that acts on the
behalf of the client (as the client proxy) to obtain the requested service. It is often used when
the client and the server are incompatible for direct connection. For example, the client may
be unable to meet the security authentication requirements of the server but may be required
to access some services. It may also be used for screening purposes to enable the
administrator to control access to undesirable sites. The proxy server may also be used for
caching purposes, which enables faster access to frequently used websites. All the computers
connected to the LAN access the Internet through a single IP address, resulting in improved
security simply because the number of ports exposed is reduced.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step2: Edit the configuration file for squid i.e. “/etc/squid/squid.conf”, Add the network
range from where the clients can connect to proxy server.
#vim /etc/squid/squid.conf
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step3: Enter the proxy server ip and port number as shown below
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step3: Check with the browser can you access www.hotmail.com through your browser
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step2: Add the same file info in configuration file, i.e., /etc/squid/squid.conf
Step4: Go to client browser and check wether the sites are being blocked
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step4: Go to client’s browser and check whether with default port, i.e. 3128, whether it is
allowing internet or not.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step6: Try again by refreshing the page and it would certainly work
Note: Squid Proxy is only the basic proxy, to learn more on proxy google for the third party
tools like; Squidguard, Untangle and Smoothwall. There is lot to do with squid, try doing
google and read the /etc/squid/squid.conf for more information.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
What is DHCP?
Dynamic Host Configuration Protocol (DHCP) is a network protocol that enables a server to
automatically assign an IP address to a computer from a defined range of numbers (i.e., a
scope) configured for a given network.
Internet service providers usually use DHCP to help customers join their networks with
minimum setup effort required. Likewise, home network equipment like broadband
routersoffers DHCP support for added convenience in joining home computers to local area
networks (LANs).
http://www.virtualpathtech.com
VirtualPath Techno Solutions
When a computer uses a static IP address, it means that the computer is manually
configured to use a specific IP address. One problem with static assignment, which can
result from user error or inattention to detail, occurs when two computers are configured
with the same IP address. This creates a conflict that results in loss of service. Using DHCP to
dynamically assign IP addresses minimizes these conflicts.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Note: Pl watch the video tutorial to configure dhcp server on my website using following
url
http://musab.in/2018/03/configuring-proxy-and-dhcp-on-centos-rhel7/
Step3: Copy the example file for dhcp configuration over dhcp configuration file,
i.e., /etc/dhcp/dhcpd.conf
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Step5: Make sure the dhcp server contains same range static IP as follows
http://www.virtualpathtech.com
VirtualPath Techno Solutions
RHEL6 as a client
Step1: Make the dhcp option enabled in network configuration using #setup command.
Step2: Restart the network services and check the IP address is in dhcp scope.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Make the same changes in all the clients to get the dynamic IP.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness
is implied.
Our Address:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
FOREWORD
Though we have taken utmost efforts to present you this book error free, but still it may contain
some errors or mistakes. Students are encouraged to bring, if there are any mistakes or errors
in this document to our notice. So that it may be rectified in the next edition of this document.
This document provides good information on every topic and lab practices. This could become
more effective if equally good practice is done. I urge the readers/students to do rigorous practice to
polish your skill sets.
You can reach us on the following email address
info@virtualpathtech.com
musab@virtualpathtech.com
musabsyd@gmail.com
http://www.virtualpathtech.com
VirtualPath Techno Solutions
OTHER COURSES AT
VIRTUALPATH
LINUX 8 ADMINISTRATION
LINUX CLUSTERS
DEVOPS AWS
NETAPP &
OPENSTACK IBM &EMC SAN
NETAPP CLUSTER
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Table of Contents
http://www.virtualpathtech.com
VirtualPath Techno Solutions
ANSIBLE AUTOMATION FOR LINUX ADMIN
INTRODUCTION:
According to the official definition on docs.ansible.com, Ansible is an IT
automation tool. It can configure systems, deploy software, and orchestrate
more advanced IT tasks such as continuous deployments or zero downtime
rolling updates.
Ansible’s main goals are simplicity and ease-of-use. It also has a strong focus on
security and reliability, featuring a minimum of moving parts, usage of OpenSSH
for transport (with other transports and pull modes as alternatives), and a
language that is designed around auditability by humans–even those not
familiar with the program.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 5 of 54
Ansible was developed by Michael DeHaan and acquired by Red Hat in 2015.
The latest version of ansible is 2.10 and stable version is 2.9
The other tools which are similar to ANSIBLE are chef, puppet, salt etc. The thing
that makes ansible different from others is ANSIBLE is an agent less tool, which
does not required any special agent to be installed and running on client sides.
Usually ANSIBLE uses SSH to connect and manage UNIX/LINUX systems and
WINDOWS remote management and using PowerShell execution. It is
developed on python and most Ansible modules are written in Python, including
the ones central to letting Ansible work.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 6 of 54
Installation of ANSIBLE using EPEL repository
In the labs we are going to work with RHEL 8.3 or CentOS 8.3
1 machine as control node and 4 machines as managed hosts.
MHOST1
MHOST2
ANSIBLE
CONTROLLER OR
MASTER NODE
MHOST3
MHOST4
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 7 of 54
Check the status of repositories by using repolist command, make sure BaseOS
and Appstream local repositories are also configured in addition to EPEL.
[root@ansible-c ~]# dnf repolist
repo id repo name
AppStream RHEL8_LOCALREPO-APPSTREAM
BaseOS RHEL8_LOCALREPO-BASEOS
epel Extra Packages for Enterprise Linux 8 - x86_64
epel-modular Extra Packages for Enterprise Linux Modular 8 - x86_64
As local and EPEL both repositories are configured, its time now to install ansible
[root@ansible-c ~]# dnf install ansible –y
Page 8 of 54
SETTING UP CONTROLLER/MASTER SERVER
In this course we are going to use 1 machine as controller node or master with
4 managed nodes/hosts.
MHOST1
192.168.10.10/24
ANSIBLE MHOST2
CONTROLLER OR
MASTER NODE 192.168.10.20/24
Ansible-c
192.168.10.5/24
MHOST3
192.168.10.30/24
MHOST4
192.168.10.40/24
Configure /etc/hosts with all nodes IP and hostname for name resolution
#vim /etc/hosts
192.168.10.5 ansible-c.example.com master
192.168.10.10 mhost1.example.com mhost1
192.168.10.20 mhost2.example.com mhost2
192.168.10.30 mhost3.example.com mhost3
192.168.10.40 mhost4.example.com mhost4
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 9 of 54
SETTING UP SSH BASED PASSWORD LESS LOGIN:
• Setup password less login or SSH based trust relationship from control node
and managed hosts.
• To setup SSH based password logins, either you can copy id_rsa.pub key
manually or using a “for loop” to automate your work.
• First of all we need to generate SSH Key pair as root user using ssh-keygen
command
Copying SSH public keys on all managed hosts using for loop
[root@ansible-c ~]# ls .ssh
id_rsa id_rsa.pub
[root@ansible-c ~]# for i in 1 2 3 4
> do
> ssh-copy-id mhost$i
> done
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 10 of 54
UNDERSTANDING THE CORE COMPONENTS
OF ANSIBLE CONFIGURATION
Ansible searches for configuration file in the above order. Config file found first
is considered and others are simply ignored
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 11 of 54
Lab Exercise:
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 12 of 54
4. Un-export the variable and check the status of config, delete each file created
and check the status of config file escalation.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 13 of 54
gathering Implicit By default, Facts are always gathered
unless we set this directive to explicit in
config file.
module_name Command Default module is command module
which can be changed to some other
module.
[privilege_escalation]
become = yes
become_user = root
become_method = sudo
become_ask_pass = False
ANSIBLE INVENTORIES
• Ansible inventory file defines the hosts to be managed by ansible. Different
managed hosts can be grouped together in different groups.
• Ansible ad-hoc commands and playbooks are executed on inventory hosts
defined on command line and in playbook, respectively.
• Host and group specific variables are also defined in inventory file.
• Ansible inventory file default path is defined in ansible configuration file.
• Ansible inventory file can be defined in INI, YAML or JSON format. Most used
format is INI format.
• Ansible inventory file name is not a standard name, you can define any file in
ansible config file to be used as ansible inventory e.g. hosts or inventory or
mhosts.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 14 of 54
An Example of Ansible Inventory file in INI format
vim /home/ansible/tasks/hosts
[mygroup1]
host1
host2
192.168.10.10
host1.example.com
host[3:5]
[mygroup2]
webserver.example.com
db.example.com
Lab Exercise:
1. Add the all the hosts name in default inventory file /etc/ansible/hosts
[root@ansible-c ~]# vim /etc/ansible/hosts
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
## db-[99:101]-node.example.com
[mygroup]
mhost1
mhost2
mhost3
mhost4
:wq!
:wq!
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 15 of 54
4. Now check the hosts list in different ways as shown below
[root@ansible-c ~]# ansible mygroup --list-hosts
hosts (4):
mhost1
mhost2
mhost3
mhost4
[root@ansible-c ~]# ansible my* --list-hosts
hosts (4):
mhost1
mhost2
mhost3
mhost4
5. Let’s do a ping test to check connectivity to all hosts using ping module
[root@ansible-c ~]# ansible mygroup -m ping or [root@ansible-c ~]# ansible my* -m ping
mhost1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
mhost4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
mhost2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
mhost3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 16 of 54
6. Edit the config file as shown below and observe the results of ping test
[root@ansible-c ~]# vim /etc/ansible/ansible.cfg
7. Restore fork value back to 5, comment the line, re-test the ping test and observe the
result in output
8. Same way change the remote_port = 222 and see whether it will be able to connect
remote hosts in ping command or not. Restore back to original value and comment back
after test.
Ansible Host and Group variables (Example)
vim /home/ansible/tasks/nodes
[mygroup1]
host1 ansible_user=ansible ansible_port=222
host2
192.168.10.10
host1.xyz.com
host[3:5]
[mygroup2]
webserver.xyz.com
db.xyz.com
[mgroup2:vars]
ansible_user=ansible
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 17 of 54
Lab Exercise:
1. Create host groups and test with ansible command
[root@ansible-c ~]# vim /etc/ansible/hosts
[mygroup1]
mhost1 ansible_user=ansible ansible_port=222
mhost2
[mygroup2]
mhost[3:4]
[mygroup2:vars]
ansible_user=ansible
:wq!
Change group variable value to root user and ping test again
[root@ansible-c ~]# vim /etc/ansible/hosts
[mygroup2:vars]
ansible_user=root
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 18 of 54
Organizing Host and Group Variables
Up till now we have learnt how we can mention host and group variables
in inventory files. In this chapter we will learn we can organize hosts and group
variables by putting them in a separate files for specific host and group.
Organizing variables is a cleaner approach when you have many hosts (or host
groups) in inventory file. To organize them, we must create directories with
name host_vars and group_vars at the same path where inventory file is
present.
Note: You must create directories with standard names host_vars and
group_vars to specify host and group specify variables. Also, you must create
these directories at path where inventory file is present.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 19 of 54
ANSIBLE MODULES
Modules (also referred to as “task plugins” or “library plugins”) are discrete units
of code that can be used from the command line or in a playbook task. Ansible
executes each module, usually on the remote target node, and collects return
values. Basically, a module is a command or set of similar Ansible commands
meant to be executed on the client-side.
Page 20 of 54
• To view only windows related modules
#ansible-doc -l |grep ^win
• To view non-windows modules
#ansible-doc -l |grep -v ^win
• To view file modules to manipulate files
#ansible-doc -l |grep -v ^win |grep ^file
Or
#ansible-doc -l |grep ^file
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 21 of 54
We must keep in mind ad-hoc command would be successfully executed if user
we are using has permissions to perform that action. For example, normal user
cannot create user, so we must execute command to create user as root user.
Lab Exercise:
Make sure that ansible hosts file has proper hosts added as shown below
[root@ansible-c ~]# tail /etc/ansible/hosts
## db-[99:101]-node.example.com
[mygroup1]
mhost[1:2]
[mygroup2]
mhost[3:4]
Check connectivity using ansible all –m ping command before proceeding
Page 22 of 54
6. Creating a file
#ansible mhost3 -m file -a "path=/tmp/test state=touch"
Note: Always use #ansible-doc <module name> for details to use that module
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 23 of 54
ANSIBLE PLAYS AND PLAYBOOKS
Page 24 of 54
ANSIBLE VARIABLES WITH YAML
vars:
key: value
The defined variables in playbooks can be called using Jinja2 templating system.
For example, we can reference the variable defined above in playbook with
jinja2 expression “{{ key }}”.
We can display the variable using debug module in simple playbook to
understand this.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 25 of 54
Lab Excersice:
Run #ansible-doc debug to let more about debug module to use it in a playbook.
playbook: vars.yml
Note: Observe there are no errors reported in the playbook and it is good to execute
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 26 of 54
2. Disabling the gathering of facts for faster execution of a playbook
Facts collection will take longer time and then execution of playbook will
happen.
Run a playbook with time command and check execution time
[root@ansible-c ~]# time ansible-playbook vars.yml
real 0m3.743s
user 0m1.584s
sys 0m0.178s
vars:
dict:
key1: value1
key2: value2
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 27 of 54
1. Creating a playbook with using a dict with multiple key value pair
[root@ansible-c ~]# vim vars2.yml
---
- hosts: mhost1
gather_facts: no
vars:
dict:
key1: value1
key2: value2
tasks:
- name: Printing the value of a variable
debug:
msg: Example of dictionary " {{ dict }}"
...
playbook: vars2.yml
[root@ansible-c ~]#
[root@ansible-c ~]# ansible-playbook vars2.yml
PLAY [mhost1]
************************************************************************
PLAY RECAP
*************************************************************************
*
mhost1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
Make the change in playbook msg option “{{ dict.key1 }}” or “{{ dict[‘key1’] }}” to display
value mapped to key1 and re-run the playbook to observe the changes in output
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 28 of 54
Named List variable example:
vars:
list:
- item1
- item2
- item3
Lab Exercise:
Create a playbook to use YAML lists as variable
[root@ansible-c ~]# vim vars3.yml
---
- hosts: mhost1
gather_facts: no
vars:
list:
- item1
- item2
- item3
tasks:
- name: Printing the values of the variable
debug:
msg: Example of list " {{ list }}"
...
[root@ansible-c ~]# ansible-playbook vars3.yml --syntax-check
playbook: vars3.yml
[root@ansible-c ~]# ansible-playbook vars3.yml
PLAY [mhost1] **********************************************************************
TASK [Printing the values of the variable]*************************************************
ok: [mhost1] => {
"msg": "Example of list \" ['item1', 'item2', 'item3']\""
}
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 29 of 54
Dictionary+list mixed variable example:
In this example we are going to learn using both dictionary and list variables
together.
Ex:
vars:
users:
- name: Steve
age: 35
- name: tina
age: 30
Jinja2 expressions to refer a variable in playbook.
“{{ users }}” - To display list variable.
“{{ users[0] }}” - To display first item in the list.
“{{ users[0].name }}” - To display name in first list item.
“{{ users[0][‘name’] }}” - To display name in first list item.
Lab Exercise:
Create a playbook to use both dictionary and list as variables
[root@ansible-c ~]# vim vars4.yml
---
- hosts: mhost1
gather_facts: no
vars:
users:
- name: Steve
age: 35
- name: Tina
age: 30
tasks:
- name: Printing the values of the variable
debug:
msg: Example of dic+list " {{ users }}"
...
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 30 of 54
[root@ansible-c ~]# ansible-playbook vars4.yml
PLAY [mhost1]
***********************************************************************
PLAY RECAP
*************************************************************************
mhost1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
See the syntax and modify playbook accordingly
Create a file with .yml or .yaml extension and store vars in it.
[root@ansible-c ~]# vim varfile.yml
---
users:
- name: Steve
age: 35
- name: Tina
age: 30
...
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 31 of 54
[root@ansible-c ~]# ansible-playbook vars5.yml --syntax-check
playbook: vars5.yml
[root@ansible-c ~]# ansible-playbook vars5.yml
PLAY [mhost1]
***********************************************************************
PLAY RECAP
*********************************************************************
mhost1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
Repeat the different vars we went through in previous example
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 32 of 54
Prompting value for the variable while executing play book
Syntax:
vars_prompt:
- name: var
prompt: Enter variable
private: no
Jinja2 expressions to refer a variable in playbook.
“{{ var }}” - To display variable.
playbook: vars6.yml
[root@ansible-c ~]# ansible-playbook vars6.yml
Enter Some Value:
PLAY [mhost1]
***************************************************************
PLAY RECAP
*****************************************************************
mhost1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 33 of 54
Let’s add an option private: no and see the change
[root@ansible-c ~]# vim vars6.yml
---
- hosts: mhost1
gather_facts: no
vars_prompt:
- name: var
prompt: Enter Some Value
private: no
tasks:
- name: Printing the values of the variable
debug:
msg: Example of prompt "{{ var }}"
...
[root@ansible-c ~]# ansible-playbook vars6.yml --syntax-check
playbook: vars6.yml
[root@ansible-c ~]# ansible-playbook vars6.yml
Enter Some Value: abcd
PLAY [mhost1]
**********************************************************************
PLAY RECAP
*************************************************************************
*********
mhost1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0
rescued=0 ignored=0
Observe this time the value is displayed on the screen
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 34 of 54
Let’s create a playbook to create a user using prompt option
[root@ansible-c ~]# vim user.yml
---
- hosts: mhost1
gather_facts: no
vars_prompt:
- name: username
prompt: Enter Username
private: no
tasks:
- name: Creating a user on mhost1
user:
name: "{{ username }}"
state: present # to delete a user state: absent, remove: yes & force: yes
should be used
...
Execute the playbook and check the result;
#ansible-playbook user.yml
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 35 of 54
ANSIBLE FACTS
Ansible collects pretty much all the information about the remote hosts as it
runs a playbook. The task of collecting this remote system information is called
as Gathering Facts by ansible and the details collected are generally known as
facts or variables
This information can be obtained manually using Ansible ad-hoc command and
a specialized module named setup. In fact, ansible playbooks call this setup
module by default to perform Gathering Facts task
Ansible facts are data/information related to your remote systems like operating
systems, IP addresses assigned on different interfaces, hostname, disks,
filesystems and more.
You can access this data in the ansible_facts variable. By default, you can also
access some Ansible facts as top-level variables with the ansible_ prefix.
We can display ansible facts using setup module using ansible ad-hoc command:
#ansible managed_host -m setup
Note: As normal user all the facts cannot be collected, so facts should be collected using
root user to fetch all important facts.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 36 of 54
"quiet": true,
"rd.lvm.lv": "rhel/swap",
"resume": "/dev/mapper/rhel-swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/rhel-root"
},
Note: ansible facts will be displayed in the form of dictionary and list, key value pair
variables
Lab Exercise:
1. Create a playbook to display all facts of a specific host
[root@ansible-c ~]#vim facts.yml
---
- hosts: mhost1
gather_facts: True
tasks:
- name: Displaying the facts
debug:
msg: "{{ ansible_facts }}"
...
[root@ansible-c ~]# ansible-playbook facts.yml
Run the playbook and you’ll see all the facts of mhost1 will be displayed
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 37 of 54
3. Create a playbook to view only first IP in the list from previous example
[root@ansible-c ~]#vim facts.yml
---
- hosts: mhost1
gather_facts: True
tasks:
- name: Displaying the facts
debug:
msg: "{{ ansible_facts.all_ipv4_addresses[0] }}"
...
Note: To view all the ansible variables that can be used in playbook, use
#ansible mhost1 –m setup|grep ansible_
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 38 of 54
5. Create a playbook to check the details about the disks connected to mhost1
[root@ansible-c ~]#vim facts.yml
---
- hosts: mhost1
gather_facts: True
tasks:
- name: Displaying the facts
debug:
msg: "{{ ansible_devices }}"
...
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 39 of 54
To gather Virtual environment related facts:
ansible target_host(s) -m setup -a ‘gather_subset=virtual,!min’ --become
Default is all (All facts).
Note 1: When using ! Symbol to filter facts, always use single quotes (‘single
quotes’). With double quotation marks it will not work.
Note 2: Minimum default facts are always gathered with each subset of facts
until we exclude them (!min)
Ansible Documentation
Ansible documentation is available on website https://docs.ansible.com/
Using ansible-doc command line tool, we can conveniently check information about
modules, various directives that can be used and with some playbook examples.
For example, to display all information about copy module, execute below command.
#ansible-doc copy
In similar way you can check information about other modules, but you must know exact
name of module.
To display all modules, execute #ansible-doc –l
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 40 of 54
CONFIGURING ANSIBLE MANAGED NODES
In this chapter we are going to learn configuring managed hosts to be used with
a normal user called ansible. We will setup ansible user to get privileges of root
using sudo powers on all managed hosts.
Let’s first learn some interesting options in ansible command line to edit a file
on managed hosts.
Task. Configure ‘mhost4’ to listen on non-default SSH port 222.
• ansible user should be able to connect to mhost4 on new SSH port as well as
standard SSH port.
• Update the inventory file to tell ansible to use port 222 to connect to mhost4
vim /home/ansible/tasks/ansible.cfg
[defaults]
inventory = /home/ansible/tasks/mnodes
roles_path = /etc/ansible/roles:/home/ansible/tasks/roles
remote_port = 22
remote_user = ansible
module_name = command
forks = 5
[privilege_escalation]
become = False
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 41 of 54
Task2: Create inventory file ‘mnodes’ on the inventory path defined in ansible
config file with below requirements.
• mhost1 must be part of host group prod1.
• mhost2 must be part of host group prod2.
• mhost3 and mhost4 must be part of webservers group.
• prod1 and prod2 must be part of prod group.
• group linux should include all managed hosts.
• Make sure all hostnames used can be resolved to IP Address.
vim /home/ansible/tasks/mnodes
[prod1]
mhost1
[prod2]
mhost2
[webservers]
mhost[3:4]
[prod:children]
prod1
prod2
[linux:children]
prod
webservers
linux
webservers prod
mhost1 mhost2
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 42 of 54
Login as root and perform ping test (note: go to /home/ansible/tasks folder
and run commands)
#ansible all –m ping –k
#ansible all –m ping –k –u root
Tasks to manage hosts we need to authorize ansible user to connect all hosts
and also have sudo privileges.
• Create and distribute SSH keys to managed nodes
• Configure privilege escalation on managed nodes
• Validate a working configuration using ad hoc Ansible commands
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 43 of 54
Task2: Generate SSH Keys for user ‘ansible’ on Ansible control node.
• Use ansible ad-hoc command to create user ansible on all managed nodes
and copy the public key for ansible user to managed nodes.
• Execute this task as root user.
• Use redhat as password for this user.
Execute command as ansible user:
ssh-keygen
Task4: Use ansible ad-hoc command to configure MOTD on all managed hosts
as “Welcome to Ansible managed host”
Execute this command as ansible user:
ansible all -m copy -a “content=‘Welcome to Ansible managed host’ dest=/etc/motd’” --
become
Note: --become will ansible user sudo privilege
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 44 of 54
For AppStream Repository:
• name=AppStream
• description= DNF AppStream Repo
• baseurl= ftp://192.168.10.5/pub/rhel8/AppStream
• gpgcheck=1
• gpgkey=/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
• enabled=1
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 45 of 54
USEFUL SYSTEM ADMIN TASKS USING ANSIBLE PLAYBOOK
- hosts: prod
become: True
tasks:
- name: Installing mariadb service
yum:
name:
- mariadb-server
- mariadb-common
state: present
- name: Starting and enabling mariadb service
service:
name: mariadb
state: started
enabled: yes
...
Execute services.yml playbook
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 46 of 54
Task 2: Create a playbook ‘user.yml’ to create user on all managed hosts with
below information.
• Use username as mark.
• Set redhat as password.
• Password must be encrypted with Sha512.
[ansible@ansible-c ~]$ vim users.yml
---
-
hosts: all
become: True
gather_facts: False
tasks:
- name: Creating user
user:
name: mark
password: "{{ 'redhat' | password_hash('sha512') }}"
state: present
...
Execute the playbook and see the result
[ansible@ansible-c ~]$ ansible-playbook users.yml --syntax-check
[ansible@ansible-c ~]$ ansible-playbook users.yml
Page 47 of 54
Execute the playbook and see the result
[ansible@ansible-c ~]$ ansible-playbook file.yml --syntax-check
[ansible@ansible-c ~]$ ansible-playbook file.yml
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 48 of 54
Task 6: Create a playbook ‘cronjobs.yml’ to schedule below tasks.
• Restart rsyslog service at 23h00 and 06h00 on prod nodes every day.
• Restart rsyslog service at 02h00 on webservers nodes on every Monday.
[ansible@ansible-c tasks]$ vim cron.yml
---
-
hosts: prod
become: Yes
gather_facts: False
tasks:
- name: Scheduling restart of rsyslog on prod nodes
cron:
name: "Scheduling cron job on prod nodes"
hour: "23,6"
minute: "0"
job: /usr/bin/systemctl restart rsyslog
-
hosts: webservers
become: True
gather_facts: False
tasks:
- name: Scheduling restart of rsyslog on webservers nodes
cron:
name: "Scheduling cron job on webservers nodes"
hour: "2"
minute: "0"
weekday: "1"
job: /usr/bin/systemctl restart rsyslog
...
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 49 of 54
Task 7: Create a playbook ‘update.yml’ to update all packages on prod1 node.
[ansible@ansible-c tasks]$ vim update.yml
---
-
hosts: prod1
become: True
gather_facts: False
tasks:
- name: Update all packages on prod1 node
yum:
name: ‘*’
state: latest
...
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 50 of 54
Task 9: Create a playbook ‘group.yml’ to perform below tasks.
• Create directory path /web/html on webservers nodes.
• Create group testing on webservers nodes and group networks on prod
nodes.
[ansible@ansible-c tasks]$ vim group.yml
---
-
hosts: webservers
become: Yes
gather_facts: False
tasks:
- name: Creating directory
file:
path: /web/html
state: directory
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 51 of 54
Task 10. Create a playbook ‘context.yml’ to set selinux context type
‘httpd_sys_content_t’ on ‘/web/html’ directory on all webservers nodes.
• Setting should be persistent, and context should be restored.
• Verify the context type using ansible ad-hoc command.
[ansible@ansible-c tasks]$ vim context.yml
---
-
hosts: webservers
become: Yes
gather_facts: False
tasks:
- name: Setting Context type
sefcontext:
target: '/web/html(/.*)?'
setype: httpd_sys_content_t
state: present
- name: Restoring context type
command: restorecon -irv /web/html
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 52 of 54
Task 11: Create a playbook ‘parted.yml’ to create extended partition on all
managed nodes.
• Use all remaining space for extended partition (container for logical
partitions).
• Create one logical partition of size 200 MiB on all managed nodes.
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 53 of 54
Task 12: Create a playbook ‘mount.yml’ to format the device ‘/dev/sda5’
with ‘ext4’ filesystem.
• Mount the file system on /mnt/partition directory.
• Mount should be persistent
[ansible@ansible-c tasks]$ vim mount.yml
---
-
hosts: all
become: Yes
gather_facts: False
tasks:
- name: Creating filesystem
filesystem:
dev: /dev/sda5
fstype: ext4
- name: Mounting filesystem
mount:
src: /dev/sda5
path: /mnt/partition
fstype: ext4
state: mounted
...
http://www.virtualpathtech.com
VirtualPath Techno Solutions
Page 54 of 54