Académique Documents
Professionnel Documents
Culture Documents
PART 1
RED HAT LINUX:
Basic Administration
CHAPTER 1
INTRODUCTION
1. The Operating System
Operating system is software that manages software and hardware resources
and provides common service. It acts as an interface between programs and
hardware resources that these programs access (like memory, hard disk and
printer). It is loaded into memory when a computer is booted and remains
active as long as the machine is up. OS performs below actions:
a. Allocates memory for programs
b. Loads CPU registers with control information related to the program
c. Keeps track of programs that are executed, helps to resume a program
d. Communicates with Hardware
e. Cleans up memory once program execution is completed
OS types:
a. Single user – MS-DOS – Single user Single task
b. Multiuser – UNIX, Linux, Windows – Multiuser Multitasking.
2. UNIX
UNIX owes its origin at Bell labs by AT&T, Massachusetts Institute of
Technology and GE. 1965 –AT &T, Bell labs and GE Dennis Ritchie, Ken
Thompson, Brain Kernighan, Douglas Mcllroy, started the developing OS in
‘Assembly language’ with multiuser and multitasking features named
MULTICS. Bell labs withdraw from the project due to size and complexity of
the project. But Dennis Ritchie and Ken Thompson decided to complete the
project in smaller scale.
Peter Neumann in 1970 named the project as UNIX (UNiplex Computing
Information Systems).
1971 first version of Unix was released.
In 1972 Ken Thompson rewrote the Unix in ‘C’ language. AT&T licensed Unix
to outside parties from late 1970s, leading to a variety of both academic and
commercial variants.
Variants:
AT & T System V
BSD University of California.
AIX IBM
HP-UX Hewlett-Packard
Xenix Microsoft
Solaris SUN Microsystems
OS X Apple Inc.
UNIX Philosophy:
a. Portability
b. Multi-tasking
c. Multi-user
d. Time-sharing
e. Plain-text to store data
f. Hierarchical file system
g. Inter-process communication
h. Software tools
i. Command-line interpreter
SHELL
1. Bourne shell (sh)
2. C shell (csh)
3. TC shell (tcsh)
4. Korn shell (ksh)
5. Bourne Again SHell (bash)
C shell (csh)
This shell was written at the University of California, Berkeley. It provides a
C-like language with which to write shell scripts - hence its name.
TC shell (tcsh)
This shell is available in the public domain. It provides all the features of the
C shell together with emacs style editing of the command line.
POSIX
People all over the globe began to develop tools for UNIX. Unfortunately,
there was no coordination to guide all the development. This caused a lot of
differentiation between the different versions of UNIX. Finally, standards
started to appear. For UNIX, many of the standards fall under the IEEE
(Institute of Electrical and Electronics Engineers) POSIX (an acronym for
Portable Operating System Interface) standard.
3. LINUX
Linux is OpenSource software released on 5th October 1991. Linux is Unix-like
OS uses monolithic kernel. Linux is originally developed for x86 platform but
ported to more computer hardware platforms. There are different
distributions in Linux like Redhat, Debian, Ubuntu, Linux Mint, Fedora,
openSUSE etc.
At commercial level:
1. Redhat Enterprise Linux
2. SUSE Linux Enterprise Server.
Freak, Unix. But his colleague Ari Lemmke uploaded the data to FTP server
with name LINUX.
GNU
GNU is recursive acronym for ‘GNU’s Not Unix’. GNU’s UNIX is Unix-like
and does not contain UNIX source code and uses free software. The goal to
bring completely free software OS. GNU started by Richard Stallman in 1983
at Massachusetts Institute of Technology (MIT).
Probably the best feature of Linux, the GNU utilities in general, and Red Hat
Linux in particular is that they are distributable under the terms of the GNU
Public License (GPL).
4. ARCHITECTURE OF UNIX
The architecture of Unix can be divided into three levels of functionality.
1. Kernel – lowest level – Schedules tasks, manages resources.
2. Shell – interface between user and kernel – interprets commands
3. Utilities – user support
5. FILESYSTEM HIERARCHY
Filesystem architecture in Linux is inverted tree like. ‘/’ is the root filesystem
and is on top of all filesystems.
CHAPTER 2
INSTALLATION
1. / (root) 15 to 20 Gb
2. /boot 200 Mb
3. SWAP twice the RAM
RHEL 6 Installation
a. Enter into BIOS settings and make CD/DVD as first boot device.
b. Make sure that VT (Virtual Technology) enabled for RHEL6 64 bit.
c. Boot the system with RHEL6 CD/DVD.
d. Follow the below screens to complete installation.
CHAPTER 3
BASIC ADMINISTRATION
# cat:
Used to create the file.
Modify the contents of file.
Redirect the contents of a file with redirection options
Use Ctrl+D to save the contents and Ctrl+C without saving the data.
#touch
Creates an empty file
$ touch test_empty.txt --- creates an empty file
$ touch test1 test2 test3 --- creates multiple empty files.
# mkdir
Create the DIRECTORY(ies), if they do not already exist.
-m, --mode=MODE
set file mode (as in chmod), not a=rwx - umask
-p, --parents
no error if existing, make parent directories as needed
-v, --verbose
$ mkdir test_dir --- creates at current location
$ mkdir /root/user/test_dir
$ mkdir –p /root/oracle/sapdata1 --- if the parent directory does not exists creates
$ mkdir –m 755 /oracle/sapdata2 --- creates sapdata2 with 755 permissions.
$ mkdir test_dir1 test_dir2 test_dir3 --- creates multiple directories
$ mkdir –p Shradda/{Linux/{Debian,Ubuntu},Solaris/zones/{globale,non-global}}
$ tree /oracle --- to see the subdirectories or directory structure.
# rm
-i --- interactively
r --- recursively
-v --- verbose
-f --- frocebly
$ rm /root/test1 --- removes file test1
$ rmdir --- removes empty directory
$ rm –r --- remove non-empty directory
$ rm –rf – removes non-empty directory --- (beware this is a destructive command)
# ls
--ls is used to list the files and directories.
Options : -a/B/d/F/i/l/m/n/o/p/Q/r/s/t/1/--full-time
-a --- shows all files including hidden files
-B --- shows other than backup files
-d --- shows current directory
-F --- shows diff between files and directories
-i --- shows the inode no of files and directories
-l --- long listing of files and directories
-m --- prints fields separated by comma
-n --- shows userid and groupid
-o --- shows 8 filed output other than group information
-r --- shows in reverse order
-t --- current modification time
--full-time --- shows 11 fields output
-1 --- shows all files and directories in single line
#cp (copy)
Copies files and directories from source to the target location. Copies with 3
possibilities.
1. file to file
2. file to directory
3. Directory to directory
-i --- interactively
-v --- verbose
-r --- recursively
-f --- forcibly
-b --- takes the backup and copy
$ cp –v testfile1 testfile2
$ cp –i testfile1 testdir1
$ cp –rf testdir1 testdir2
$ cp –b testfile1 testdir1
$ cp file1 file2 file3 testdir1 --- copies file1,file2 file3 to the testdire1
# mv (move or rename)
Move command is used to move a file or directory from one location to other
location or rename file or directory with other name.
3. $ mv /dir1 /testdir1 --- move /dir1 to new location under /testdir1 if testdir
exists else renames /dir1 to /testdir1
# VI editor
Deleting
x --- deletes cursor position character
X --- deleted before cursor position character
dd --- deleted cursor position record
dw --- deletes cursor position word forward
db --- deleted cursor position record backword
d$ --- deletes cursor position to the end of the record
d^ --- deleted cursor position to the start of the record
dG --- deletes cursor position to the end of the file
dgg --- deletes cursor position to start of the file
ndd --- deletes specified no of lines
copying
yy --- yanks cursor position record
yw --- copies oneword forward
yb --- copies oneword backword
y$ --- copies cursor position to the end of the record
y^ --- copies cursor position to the start of the record
ygg --- copies cursor to the start of the file
Inserting
a --- cursor moves to the next word and inserts
A --- cursor moves end of the record and inserts
i --- cursor will be same position and inserts
I --- cursor moves start of the record and inserts
O --- inserts new line above cursor position
o --- inserts new line below cursor position
cc --- deletes cursor position record and inserts
cw --- deletes cursor position word and inserts
colon mode
:w saves
:wq saves and quits
:wq! Saves and quits forcefully
:q quits without saving
:q! quits forcefully without saving
:se nu --- sets no to the records
:se nonu --- unsets the no to the records
:/word --- searches word
:?word --- searches for word
:se ic --- sets ignore case
:se noic --- unsets ignore case
:1,$s/unix/genunix/g --- replaces unix by genunix
:se ai sets auto indentation
:se noai --- unsets auto indentation
:abbr --- ab set abbreviation
:unabbr --- unsets abbreviation
:!command --- excutes the command in editor mode.
# uname
Uname prints system information
$ uname –n --- prints node name information
$ uname –a --- shows all information
$ uname –r --- System Kernel Version
$ uname -v --- Shows Kernel release version
$ uname –s --- shows OS
$ uname –m --- Shows mechine name
$ uname –o Prints OS name
#who
$ who to know all users login information
$ who –iH shows login information with headings.
$ who –m shows information with host name
$ who –q shows all users login count
$ who –r shows run level information
$ who –a prints all possible information
$ who –b prints last reboot time
$ who –d prints dead processes
$ who –u prints the login users info
# cal
$ cal shows current month
$ cal -3 shows previous, current and future month details
$ cal –y shows current year details
$ cal 2008 shows year 2008
# finger
Shows the user details.
$ finger prints user information
$ finger –l user prints User Information
$ finger -s prints user login name, real name and terminal name.
# date
$ date --- displays the date
$ date +%a --- day of the week in short (Sun)
$ date +A --- day of the week in long form (Sunday)
$ date +%b --- month in short (Mar)
$ date +%B --- month in long form (March)
$ date +%y --- year in short (15)
$ date +%Y --- year in long form (2015)
$ date +%x --- date (06/21/2015)
$ date +%X --- time in 12 hr format (06:30:41 PM)
$ date +%T --- time in 24 hr format
$ date +%d --- day in short (21)
$ date +%D ---date in long format (21/06/15)
2015
# date +%y
15
# date +%c
Wed Jul 29 16:18:11 2015
# date +%C
20
# date +%d
29
# date +%D
07/29/15
# date +%m
07
# date +%e
29
# date +%E
%E
# date +%F
2015-07-29
# date +%g
15
# date +%G
2015
# date +%H
16
# date +%h
Jul
# date +%I
04
# date +%k
16
# date +%M
21
# date +%m
07
# date +%M
22
# date +%n
# date +%p
PM
# date +%P
pm
# date +%r
04:23:32 PM
# date +%R
16:23
# date +%S
49
# date +%T
16:24:28
# date +%x
07/29/15
# date +%X
16:25:19
# date +%z
+0200
# date +%Z
CEST
#
PERMISSIONS
Permissions are the access authority given to a file or a directory for owner, group
and others. Permissions are read (r) with value of 4, write (w) with value of 2 and
execute permission (x) with value of 1. Total 7.
Default permissions for a normal file – 644 (full permissions 666)
Default permissions for a directory – 755 (full permissions 777)
The default permissions are set when file or directory created due to umask value
022.
File --- 666 – 022=644
Directory --- 777 – 022=755
Types of files
1. Ordinary file
2. Directory
3. Link file
4. Block special file
5. Character special file
# chmod
Chmod is to change the permissions of a file or a directory
Permissions can be changed
a. Numeric method
b. Symbolic method
Numer Method:
$ chmod 600 testfile1 (user –6, group-0, others-0 permissions)
$ chmod 750 testdir1 ( user – 7, group-5, others-0 permissions)
Symbolic Method:
Using symbols permissions can be assigned to a file or directory
+ adding permission
– deleting permission
a all
= equal value
$ chmod u+w,g-e,o+r testfile1
$ chmod ugo+rwe testfile
$ chmod ugo=r testfile
LINKING
Linking is a relationship between files. Files are linked across the directories or
paths to
1. Hide the visibility of actual path.
2. To hide the visibility of actual file.
3. To reduce the risk of deletion
4. To provide the access to the files
There are two types of lining
a. Soft link
b. Hard link
c.
Hard link:
Inode number is same, data is same. In short it is the other name given to a file.
If source is deleted still the data is safe. We cannot find which is source and with is
target.
$ ln testfile1 testfile_link
$ ln testfile1 testfile_l1 testfile_l2 ( testfile1 is source; testfile_l1 and testfile_l2 are links)
Soft link:
We can create the soft link across the directory paths.
Soft links are created both for directories and files.
Inode numbers are different for the source and target.
If the date in source is deleted then the date in target is lost.
Target contains the inode number of source instead of date.
We cannot chage the permissions of the target file.
$ ln –s /var/logs/authlog /user/vajrag/authlog
PATH
PATH is the location of file or a directory
1. Absolute path
2. Relative path
Absolute path is the complete path given basing on its location (Complete path)
Relative path is the path basing the user location or present working directory.
$ cp /var/log/authlog /user/vajrag/ -- Absolute path
$ cd /var/log/authlog ./../dir1 – Relative path
(. Is present directory .. is parent directory to pwd.)
Authlog is copied to /var/dir1/
Redirections
Its redirecting the standard output to the standard input or vice versa.
monitor are typical output device and keyboard is the standard input device.
There are sysmbols that represents the redirections.
< O r0 Is the standard output
> or 1 is the standard input
# kill
Kill command terminates a process. Kill command can be used either with process
name or by the process id. The default signal for kill is TERM (which will terminate
or "kill" the process). Use -l or -Lto list available signals. Particularly useful signals
include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in
three ways: -9, -SIGKILL or -KILL. Negative PID (process ID) values may be used to
choose whole process groups; see thePGID column in the output of the ps command.
A PID of -1 is special; it indicates all processes except two: the kill process itself,
and init.
$ kill -9 0710
$ kill –SIGKILL 0710
$ kill –SUGHUP 0720
The kill command accepts either the signal number or name (signals have
both a number and name that can be referenced). The name must be in all
caps.
# pkill
$ pkill command is like kill command but takes partial names.
$ pkill -9 -u USERNAME'
SIGHUP - The SIGHUP signal disconnects a process from the parent process. This an
also be used to restart processes. For example, "killall -SIGUP compiz" will restart
Compiz. This is usefulfordaemonswithmemoryleaks.
SIGINT - This signal is the same as pressing ctrl-c. On some systems, "delete" + "break"
sends the same signal to the process. The process is interrupted and stopped.
However, the process can ignore this signal.
SIGQUIT - This is like SIGINT with the ability to make the process produce a core
dump.
SIGTRAP - This signal is used for debugging purposes. When a process has performed
an action or a condition is met that a debugger is waiting for, this signal will be sent
to the process.
SIGABRT - This kill signal is the abort signal. Typically, a process will initiate this
kill signal on itself.
SIGBUS - When a process is sent the SIGBUS signal, it is because the process caused a
bus error. Commonly, these bus errors are due to a process trying to use fake
physical addresses or the process has its memory alignment set incorrectly.
SIGFPE - Processes that divide by zero are killed using SIGFPE. Imagine if humans
got the death penalty for such math. NOTE: The author of this article was recently
drug out to the street and shot for dividing by zero.
SIGKILL - The SIGKILL signal forces the process to stop executing immediately. The
program cannot ignore this signal. This process does not get to clean-up either.
SIGUSR1 - This indicates a user-defined condition. This signal can be set by the user
by programming the commands in sigusr1.c. This requires the programmer to know
C/C++.
SIGPIPE - When a process tries to write to a pipe that lacks an end connected to a
reader, this signal is sent to the process. A reader is a process that reads data at the
end of a pipe.
SIGALRM - SIGALRM is sent when the real time or clock time timer expires.
SIGTERM - This signal requests a process to stop running. This signal can be ignored.
The process is given time to gracefully shutdown. When a program gracefully shuts
down, that means it is given time to save its progress and release resources. In other
words, it is not forced to stop. SIGINT is very similar to SIGTERM.
SIGCHLD - When a parent process loses its child process, the parent process is sent
the SIGCHLD signal. This cleans up resources used by the child process. In
computers, a child process is a process started by another process know as a parent.
SIGCONT - To make processes continue executing after being paused by the SIGTSTP
or SIGSTOP signal, send the SIGCONT signal to the paused process. This is the
CONTinue SIGnal. This signal is beneficial to Unix job control (executing
background tasks).
SIGSTOP - This signal makes the operating system pause a process's execution. The
process cannot ignore the signal.
SIGTSTP - This signal is like pressing ctrl-z. This makes a request to the terminal
containing the process to ask the process to stop temporarily. The process can ignore
the request.
SIGTTIN - When a process attempts to read from a tty (computer terminal), the
process receives this signal.
SIGTTOU - When a process attempts to write from a tty (computer terminal), the
process receives this signal.
SIGURG - When a process has urgent data to be read or the data is very large, the
SIGURG signal is sent to the process.
SIGXCPU - When a process uses the CPU past the allotted time, the system sends the
process this signal. SIGXCPU acts like a warning; the process has time to save the
progress (if possible) and close before the system kills the process with SIGKILL.
SIGXFSZ - Filesystems have a limit to how large a file can be made. When a program
tries to violate this limit, the system will send that process the SIGXFSZ signal.
SIGVTALRM - SIGVTALRM is sent when CPU time used by the process elapses.
SIGPROF - SIGPROF is sent when CPU time used by the process and by the system
on behalf of the process elapses.
SIGWINCH - When a process is in a terminal that changes its size, the process
receives this signal.
SIGPWR - Power failures will cause the system to send this signal to processes (if the
system is still on).
SIGSYS - Processes that give a system call an invalid parameter will receive this
signal.
SIGRTMIN* - This is a set of signals that varies between systems. They are labeled
SIGRTMIN+1, SIGRTMIN+2, SIGRTMIN+3, ......., and so on (usually up to 15). These
are user-defined signals; they must be programmed in the Linux kernel's source code.
That would require the user to know C/C++.
SIGRTMAX* - This is a set of signals that varies between systems. They are labeled
SIGRTMAX-1, SIGRTMAX-2, SIGRTMAX-3, ......., and so on (usually up to 14). These
are user-defined signals; they must be programmed in the Linux kernel's source code.
That would require the user to know C/C++.
SIGINFO - Terminals may sometimes send status requests to processes. When this
happens, processes will also receive this signal.
SIGLOST - Processes trying to access locked files will get this signal.
SIGPOLL - When a process causes an asynchronous I/O event, that process is sent the
SIGPOLL signal.
# ps
The ps command on Linux is one of the most basic commands for viewing the
processes running on the system. It provides a snapshot of the current processes
along with detailed information like user id, cpu usage, memory usage, command
name etc.
Use the "u" option or "-f" option to display detailed information about the processes
$ ps aux
$ ps –ef –f
$ ps –f –u username --- displays all process of specified user.
To search the processes by their name or command use the "-C" option followed by
the search term.
$ ps -C apache2
PID TTY TIME CMD
2359 ? 00:00:00 apache2
4524 ? 00:00:00 apache2
4525 ? 00:00:00 apache2
To display processes by process id, use the "-p" option and provides the process ids
separated by comma.
$ ps -f -p 3150,7298,6544
The "-C" must be provided with the exact process name and it cannot actually search
with a partial name or wildcard.
$ ps -o pid,uname,comm -C apache2
PID USER COMMAND
2359 root apache2
4524 www-data apache2
4525 www-data apache2
4526 www-data apache2
4527 www-data apache2
4528 www-data apache
The next command lists all child apache2 processes using the pid of the main
apache2 process
$ ps --ppid 2359
The following command shows only the pid, username, cpu, memory and command
columns.
$ ps -e -o pid,uname,pcpu,pmem,comm
CHAPTER 4
FILTERS
FILTER is a command which takes input from standard input manipulates it and
writes to standard output.
a. Simple filters
b. Advanced filters
Simple filters:
1. Head
2. Tail
3. More
4. Less
5. Cut
6. Paste
7. Sort
8. Uniq
9. Tr
10. Tee
Advanced filters:
1. grep
2. sed
3. awk
# head
$ head file1 --- displays by default 10 lines of starting of a file
$ head -5 file1 --- displays only first 5 lines
$ head -20 file1 --- displays first 20 lines of a file
# tail
$ tail /testdir/testfile1 --- displays last 10 lines by default
$ tail -7 /test/testfile1 --- displays last 7 lines of a file
$ tail -20 /testdir/testfile --- displays last 20 lines of a file.
# more
The more command displays the file called name in the screen. The RETURN key
displays the next line of the file. The spacebar displays the next screen of the file.
$ cat file1 | more
# less
Less is similar to more command, but less allows both forward and backward
movements. Once you’ve opened a log file (or any file) using less file-name, use the
following keys to search. Please note that the match will be highlighted
automatically by default.
Forward Search
/ – search for a pattern which will take you to the next occurrence.
n – for next match in forward
N – for previous match in backward
Backward Search
? – search for a pattern which will take you to the previous occurrence.
n – for next match in backward direction
N – for previous match in forward direction
$ less file1
$ cat file1 | less
#wc
Word count is used to count the number of lines, number of words and number of
characters in a file.
$ wc –c file1 – displays the no of character of a file
$ wc –w file1 --- displays the no of words in a file
$ wc –l file1 --- counts no of lines in a file.
$ wc –cwl file1 --- counts characters, words and lines of a file.
$ wc file1 --- counts characters, words and lines of a file.
# cut
Used to cut the characters as desired.
--c ---cuts the characters
--d --- specifies delimiter
--f --- required field
File1:
Anil:Doctor:90000
Chaitanya:Actor:200000
Akshya:Engineer:100000
Ananya:Doctor:300000
# sort
# tee
Command used to create a multiple files with same data.
Used to divert to standard output also to standard input.
# tr
Translates the upper to lower and lower to upper case.
Squeezes the multiple characters
Deletes the character
$ tr “[a-z]” “[A-Z]” < testfile
$ tr “[A-Z]” “[a-z]” < testfile
$ tr –s “e” testfile --- squeezes the multiple ‘e’
$ tr –d “e” testfile --- deletes the character ‘e’
CHAPTER 5
Advanced Filters
#grep
Global search for regular expression.
We can search record by record.
# sed
Stream editor for filtering and transforming test
$ sed <option> <action> filename
-n --- to suppress multiple expressions
-e --- to search multiple expressions
-p --- printing
-d --- deleting
-s --- substituting
print
$ sed ‘p’ test1
$ sed –n ‘p’ test1
$ sed –ne ‘1p’ –e ‘2p’ testfile
delete
$ sed ‘1d’ testfile
$ sed –e ‘1d’ –e ‘3d’ testfile
$ sed –e ‘/sunil/p’ –e ‘/anil/p’ testfile
Substitute
$ sed ‘s/anil/anilkumar/1’ testfile
$ sed ‘s/anil/anilkumar/g’ testfile
$ sed ‘1,$s/anil/sai/g’ testfile
$ sed ‘1,4/anil/sai/g’ testfile
# awk
Aho, wenberger and kerneghan developed the filter named after them.
Operators
>= greater than equal
> greater than
< less than
<= less than eqal
== equal
!= not equal
|| or
&& and
NR No of records
NF No of fields
$ specified filed
~ matching with
!~ not matching
# find
Search for files in directory hierarchy.
-type
-name
-inum
-empty
-size
-mindepth
-maxdepth
-perm
-exec
-ctime
-mtime
-atime