History of UNIX and LINUX
In 1969 Dennis Ritchie and Ken Thompson developed C Language
and the UNIX operating system at AT&T Bell Labs.
By 1975 AT&T started selling UNIX commercially. Berkeley people
created free BSD Unix by the end of seventies.
In Eighties many companies started developing their own UNIX. IBM
created AIX, Sun created Solaris, HP created HP-UX, etc.
Richard Stallman started GNU project with a goal to make OS that
was freely available to everyone.
Linus Torvalds created a brand new Linux Kernel.
What is RHEL 7.0 Server?
RHEL 7.0 is a Linux distribution.
You need a subscription to use it.
You get support according to the subscription purchased
with patches and updates regularly.
You also get access to customer portal for redhat at
access.redhat.com where detailed documentation is
available for only customers.
All enterprise applications are guaranteed to run such as
Hadoop, SAP, Oracle, etc. on specific hardware models.
Centos 7.3 vs. Fedora 26
RedHat is involved in 2 free alternatives:
1.Centos is Community Enterprise OS where you have a
recompiled version of RHEL where all items that were
not available for free were removed from the software
and provides no support.
2.Fedora is development platform for RHEL which offers
access to latest software. It is a testing ground for new
features that might not be included in future RHEL
releases.
UBUNTU, DEBIAN, SUSE
Ubuntu started in 2004 owned by Canonical, targets
graphical Linux desktop. LTS version is released every
2 years for servers with extended support for 7 years.
Current version is 17.04.
There is no company behind Debian and is created by
community of developers.
SUSE is an independent brand earlier owned by
Novell in 2004. SUSE Leap is free. SLES is commercial.
Centos, Scientific Linux, Oracle Enterprise Linux are
based on RHEL whereas Linux Mint, Ubuntu share a
lot with Debian.
Installing RHEL 7.0
Server with GUI requires 1 GB RAM and without GUI
requires 512 MB RAM.
20 GB Hard disk space.
Need a hypervisor to install virtually like VMware
Player or Oracle virtual box.
So let us begin with Installation.
Terminals, Consoles and Shells
Terminals that are started in non graphical
environment are referred to through the devices
/dev/tty1 through /dev/tty6 are physical/virtual
terminals.
Terminals that are started in graphical environment
are pseudo terminals referred to using numbers in
the /dev/pts directory
Executing Commands
There are 3 types of commands in Linux
1. Aliases
2. Internal Commands
3. External Commands
Aliases are executed before anything else.
alias newcommand=‘oldcommand’
Internal command is a part of shell itself and is available
when the shell is loaded without any memory lookup
from disk
External command exists as an executable file on disk so
it is a bit slower.
Basic commands
whoami
hostname
date
uname
passwd
touch
last
Finding Help
--help
man –k or apropos
info
pinfo
/usr/share/doc
History
bash shell has a bash history feature to keep track of last
commands you have used.
When a shell session is closed, the history of that session
is updated to .bash_history in home directory of user
who started a specific shell session.
history will list all commands in bash history.
Ctrl + R for backward search
!number to execute command with a specific number in
history
!?sometext to execute last command with some text
Standard Input Output and Error
Name Default Destination Redirection File Descriptor
STDIN Keyboard < or 0< 0
STDOUT Monitor > or 1> 1
STDERR Monitor 2> 2
Redirector Explanation
< or 0< Redirects STDIN
> or 1> Redirects STDOUT. Current contents of file are overwritten
>> or 1>> Redirects STDOUT. Output is appended to that file
2> Redirects STDERR.
&> Redirects STDERR to the same destination as STDOUT.
2>&1 Redirects STDERR to the same destination as STDOUT.
Piping
Redirection is used as alternatives for keyboard and
monitor whereas pipe is used to catch the output of
one command and use it as input for the next.
Redirection works for files whereas piping works for
processes.
ps aux | awk ‘{ print $2 }’ | sort –n | less
find / -name “*.rpm” 2> /dev/null
create a simple script
echo who > myscript
echo ls >> myscript
chmod +x myscript
./myscript
Environment Configuration Files
/etc/profile: This is the generic file that is processed
by all users upon login
/etc/bashrc: This file is processed when subshells are
started
~/.bash_profile: In this file, user-specific login shell
variables can be defined
~/.bashrc: In this user-specific file, subshell variables
can be defined
Linux File System Hierarchy
Directory Use
/ The root file system where the file system tree starts.
/bin Executable programs needed to repair system during troubleshooting.
/boot Files that are needed to boot the Linux kernel.
/dev Device files used to access physical devices.
/etc Configuration files used by programs and services on the server.
/home Local user home directories.
/lib, /lib64 Shared libraries used by programs from /boot, /bin and /sbin
/media, /mnt Directories used for mounting devices in the file system tree.
/opt Optional packages to be installed on the server.
/proc File system structure that gives access to kernel information.
/root The home directory of the root user.
Linux File System Hierarchy
Directory Use
/run Contains process and user specific information that has been created
since the last boot.
/sbin Like /bin but for system administration commands that are not used
by regular users.
/srv Directory that may be used for data that is used by services like NFS,
FTP and HTTP
/sys Used as an interface for different hardware devices that is managed
by Linux kernel and associated processes.
/tmp Temporary files that may be deleted during boot without warning.
/usr Contain operating system files only. Many subdirectories mimic the
contents of root file system.
/var Contains log files, mails and spool files which may change in size.
ls
ls -l
ls -a
ls -ltr
ls -ld
Globbing or wildcards
Globbing is also known as using wildcards to match
filenames
man 7 glob
Wildcard Use
* Any number of characters
? One specific character
[auo] One character within the range
[!auo] No character within the range
mounts
mount command gives an overview of all mounted
devices. /proc/mounts file is read where the kernel
keeps information of all current mounts.
findmnt command shows mounts and the relation
that exists between different mounts.
df –Th command shows available disk space on
mounted devices.
• -T shows file system type
• -h shows human readable way
$PATH variable
External commands use $PATH variable that defines a
list of directories to be searched for a matching file
name when a user enters a command. $PATH does
not contain current directory so use a ./command to
start the command from the current directory.
use which command to find out where the shell will
get the command from.
env command gives overview of current variables
defined in your shell environment
Absolute vs. Relative Pathnames
An absolute pathname is a complete path reference
to a file or directory. It starts with /
A relative pathname is relative to the current
directory. It contains only the elements that are
required to get from the current directory up to the
item you need.
Absolute path is /home/user1/file1
Relative path is from /home you refer user1/file1
Copy files and directories
cp source destination
cp /somedir/* /tmp Copies all files excluding hidden.
cp –a /somedir/ . Copies entire directory /somedir
including its contents to the current
directory. Subdirectory somedir will be
created in the current directory.
cp –a /somedir/. . Copies all files, regular and hidden to the
current directory
Moving and Renaming files and directories
mv source destination
mv file1 /tmp Moves file file1 from current directory to
/tmp
mv file1 newfile Renames file file1 to newfile
mkdir mydir; This first creates directory with the
mv folder /tmp name mydir and then moves this
directory to /tmp.
Deleting files and directories
rm filename
rm –rf directory
• -r for recursive, includes the contents of directory.
• -f for no confirmation.
rmdir works for only empty directories
Inodes
Every file on Linux has an inode that stores
information about the creation, access date,
modified date, owners, permissions and the data
block where the file contents are stored.
Name of the file is not stored in inode.
Inode knows how many filenames are associated but
does not know the filenames. These names are
called as hardlinks.
Hard links vs. Soft links
Symbolic Links
First Name of File Second Name of
Hard Link File Hard Link
INODE
Actual contents of the file
i.e. Blocks containing data
0101000100000000100000001111111
Hard Links
Hardlinks must exist all on the same device.
You cannot create hard links to directories.
The number of aliases the original file has. When the
last name is removed, the contents are also
removed.
No difference exists between first and second
hardlink. Removing first hardlink does not affect
other.
Symbolic Links or Soft Links
Symbolic link does not link directly to the inode but
to the name of the file.
Possible to create on other devices as well as on
directories.
When the original file is removed, the symbolic link
becomes invalid.
ln source destination
• -s to create symbolic links.
• permissions are managed on targets & not soft links.
which whereis locate and find
which command
whereis command
locate command
updatedb
find . –print
find /etc -iname “*hosts*”
find /home/user1 –name “*.txt” –print
find ~ –maxdepth 1 –type f ! –iname “.*”
find ~ –mindepth 5 –type d -name i* ! –iname “.*”
search on file times
Access time (-atime): It is the last timestamp of when
the file was accessed by a user
Modification time (-mtime): It is the last timestamp
of when the file content was modified
Change time (-ctime): It is the last timestamp of
when the metadata for a file was modified
(such as permissions or ownership)
find
find . –type f –atime -2 –print
find . –type f –atime 2 –print
find . –type f –atime +2 –print
find . –type d –amin -2 –print
find . –type d–cmin 2 –print
find . –type d –mmin +2 –print
find ~ -type f –newer file1.txt -print
find ~ -perm 764 -type f
find ~ -maxdepth 1 -user user1 -type f ! -iname ".*“
find advanced usage
find /etc -name “*hosts*” -exec cp {} ~/search_dir \;
find /home/user1 -user root -exec chown
user1:user1 {} \;
find . -name "*.sh" -exec cat {} \; > output_file
find /etc -maxdepth 1 -type f –size +4k -size -10k -
exec ls -l {} \;
find . -maxdepth 1 -type f -empty -delete
find /etc -maxdepth 1 -exec grep -l “student” {} \; -
exec cp {} ~/myfiles \; 2> /dev/null
vim
Start vim Command mode
Press i
Press :wq
!
Press Esc
Quit vim Insert mode
vim
vim command Explanation
esc Switches input mode to command mode
I Switches command mode to input mode
:wq Writes the current file and quits
:q! Quits without applying changes.
dd Deletes the current line
yy Copies the current line
p Pastes the current selection
u Undo the last command
ctrl+r Redo the last command
gg Goes to the first line in the document
G Goes to the last line in the document
A Append to the end of the current line
vim
vim command Explanation
/text Searches for text from current position forward
?text Searches for text from current position backward
^ Goes to the first position in the current line
$ Goes to the last position in the current line
!command Adds the output of the command in the current file
:%s/old/new/g Replaces all occurrences of old with new
:line number To go to line number
:5,10 r path To read lines from 5-10 file specified in path
:5,10 w path To write lines from 5-10 to a file specified in path
v Enters visual mode user d to cut or y to copy the selection
vim +number To open file at a specified line number. Use set nu to set line number
vim –o or -O Equivalent to :split or :vsplit use ctrl+w to switch
Text Processing Tools
Command Explanation
less Opens the text file in pager.
cat Dumps the contents of the text file on the screen.
head Displays first 10 lines of the text file.
tail Displays last 10 lines of the text file.
cut Used to filter specific columns or characters from a text file.
sort Sorts contents of a text file.
wc Counts the number of lines, words and characters in a file.
tr translates a set of characters
Regular Expressions
Regular expressions are text patterns that are used
by tools like grep and others.
Don’t confuse regular expressions with globbing!
They look like file globbing, but they are not same.
grep ‘a*’ a*
Use with specific tools only ( grep, vim, awk and sed )
man 7 regex for details.
Regular Expressions
Character Definition Example Result
^ Start of a string ^abc abc, abcdef, abc123
$ End of a string abc$ abc, blahabc, 456abc
. Any character except newline a.c abc, aac, a2c
| Alteration i.e. 1 or 8 1|8 1,8
{…} Explicit quantity of preceding character ab{2}c abbc
[…] Explicit set of characters to match a[bB]c abc, aBc
(…) Group of characters (123){3} 123123123
* 0 or more of the preceding character ab*c ac, abc, abbbbbc
? 0 or 1 of the preceding character ab?c ac, abc
+ 1 or more of the preceding character ab+c abc, abbbbc
Note extended regular expressions + ? { } ( ) |
Regular Expressions
Regular Expression Use
^text Line starts with text
text $ Line end with text
. Wildcard matches any single character
[abc] Matches a, b or c.
* Matches 0 or an infinite number of previous character.
\{2\} Matches exactly 2 of the previous character.
\{1,3\} Match a minimum of 1 and a maximum of 3 of the previous
character.
colou?r Match 0 or 1 of the previous character. This makes the previous
character optional. i.e. color and colour.
Regular Expressions
Option Use
-i Not case sensitive. Matches both uppercase and lowercase.
-v Only show lines that do not contain the regular expression.
-r Searches files in the current directory and all subdirectories.
-e Search for lines matching more than one regular expression.
-w Exact match
-o Number of occurrences.
sed
sed 2q /etc/passwd
sed –n /^root/p /etc/passwd
sed –n 5p /etc/passwd
sed –i ‘s/old-text/new-text/g’ ~/myfile
sed –i –e ‘2d;20,25d’ ~/myfile
sed –ne ‘/^$/d’ myfile
Root user
Root account is needed for
1. Installing software.
2. Managing users.
3. Creating partitions on disk devices.
su - opens a subshell as a different user.
sudo allows you to setup an environment where
specific tasks are executed with administrative
privileges
Policykit allows you to setup graphical utilities to run
with administrative privileges