Unit – 1: Introduction

Unix History: Unix was created by Dennis Ritchie and Ken Thompson in the year 1969 in AT&T Bell Laboratories. There are various distributions of Unix such as: AIX from IBM, XENIX from Microsoft, Solaris from Sun Microsystems, IRIX from SGI and HP-UX from HP.

 Unix Principle:

  • Everything is a file, including a hardware. The Unix security model is based around the security of files.
  • Configuring data stored in text. Storing configuration in text makes moving the configuration from one machine to another very easy.
  • Small single purpose program. To perform complex task, these small purpose programs can be chained.

GNU Project/ FSP

  • GNU project was started in the year 1984. Its goal was to create free UNIX clone.
  • By the year 1990, almost all the user required applications were developed.
  • Free Software Foundation (FSF) manages GNU project. It is non-profit making organization.
  • The softwares are distributed under General Public License ( GPL)
  • GNU refers to GNU is Not Unix.

 Linux Origin

  • A Finnish college student named Linus Trovalds created a Linux Kernel in the year 1991.
  • When Linux Kernel combined with GNU applications, a complete Unix-like operating system became possible.

 Red Hat Linux

  • It distributes the latest version of Linux.
  • It distributes the commonly used applications with its package.
  • Established in 1995 by Bob Young and Mark Ewing.
  • In 1996, Red Hat 4.0 Linux was named desktop operating system of the year.
  • In 1997, Red Hat 5.0 Linux was named network operating system of the year.
  • In 1998, Red Hat’s market share increased from 6.8% to 17.2% out of total Unix based operating system.
  • In 1999, RHCE Certification was introduced.
  • In 1999, Red Hat’s market share increased upto 28% out of total Unix based operating system.
  • Since 2002, RHCE Certification stands as the best professional computer based certification.

Note: The Research was conducted by Info-World magazine.

 Recommended Hardware Requirements (Red Hat Linux 9)

  • Pentium III or higher
  • 128 Mb RAM or higher
  • 5 Gb Hard Disk partition or higher
  • Bootable CD-ROM
  • Floppy Disk Drive.

Unit -2:Quick Tour

Run Levels:

  • 0 – halt
  • 1 – single user
  • 2 – multiuser
  • 3 – multiuser
  • 4 – unused
  • 5 – GUI (Graphical User Interface) or X window
  • 6 – reboot

 Shutting down the system

$ halt
$ poweroff
$ init 0
$ shutdown -h now

Virtual Console

There are total 6 virtual consoles:

tty1 –> Alt+F1
tty2 –> Alt+F2
tty3 –> Alt+F3
tty4 –> Alt+F4
tty5 –> Alt+F5
tty6 –> Alt+F6

The seventh one is for the X window (GUI based) for that Ctrl+Alt+F7. And to return back to virtual consoles from X window, user Ctrl+Alt+F1/2/3/4/5/6. X window system’s terminals are called pseudo terminals.

 User Login

When logged as root (superuser) the prompt at console seems like:

[root@localhost root]#

When logged as local user the prompt at consosle seems like:

[localuser@localhost localuser]$

Adding User and assigning password

# useradd {name}

# passwd {name}

Type New-Unix Password: *******

Re-type New-Unix Password: *******

passwd: all authentication tokens updated successfully

 Listing Files:

“ls” command displays files & subdirectories of the current directory.

Color code for file:

  • white – normal file
  • green – execute file
  • blue – directory
  • yellow – device file
  • cyan – link file
  • magenta – image file

 Creating file

$ cat > {filename}

—-

Ctrl+d (to save and exit)

$ cat {filename} (to view the file)

$ mkdir {directory name} (to make directory)

$ cd {directory name} (to change directory)

Unit – 3: File and Shell Basics

Running Commands:

syntax: command [option] [argument]

  • each block is separated by a space
  • option modifies the behavior of command
  • argument may be filename or other information needed by command

Command “pwd” is used to print the current working directory.

 Command “ls” displays the contents of current or specified directory.

Syntax: ls [option] [directory]

options:

-a lists all the contents of the specified directory including hidden, current directory(.) and parent directory(..). Files/directories whose name begin with “.” are hidden
-A is same as of -a, except it doesn’t display . & ..
-l displays the long list of the contents of the directory

Command “cd” is used to change directory. While changing directory to other subdirectories, full path name is to be given.

$ cd /directory/subdirectory

note: here the first “/” stands for the root directory like “C:/” in Ms-dos.

$ cd —> only enables the user to reach user’s home directory.

$ cd – —> reaches to the previous directory

$ tree —> displays files and directories in tree structure

 File names

  • filenames can be up to 256 characters long
  • filename that begin with “.” are known as hidden files
  • special characters need special handling

Checking free space

command “df” displays the free space for the file system

# df -h (disk free in human readable form)

 command “du” displays the disk usage

# du -h /var/log (to view the disk usage of /var/log directory)

 File Manipulation

command “mv” is used to move a file

syntax: mv [source filename] [destination filename]

  • if the source filename and destination filename is same, the file will be renamed
  • if the destination filename exists, the file will be overwritten

Command “cp” is used to copy file from one location to another inside the same or different directories.

Command “touch” is used to create an empty file. But if the filename already exists, the access time will be modified.

Command “rm” is used to delete a file

options:

-i —> interactive mode, asks confirmation for the removal of file
-r —> to remove a non-empty directory
-f —> to remove file or directory forcibly without asking any confirmation

command “rmdir” is used to delete an empty directory

Running multiple commands on the same command line

syntax: first command ; second command

syntax: first command && second command

Here, “;” causes to run the second command only after the successful execution of the first command while “&&” causes the commands to be executed sequentially independent of each other.

Example:

$ mkdir dir1; cd dir1

 Viewing text files

$ cat [option] filename

options

-s —> squeeze [replaces the multiple blank lines by a single blank line]
-n —> displays the line number
-A —> displays all including control and non-printable characters like spaces, return, etc

 Command “less” displays the contents of a file, one screen at a time either forward or backward.

Syntax: less {filename}

d —> moves half screen forward
d —> moves half screen backward
spacebar —> moves forward by one screen
enter/return —> moves ahead by a line

note: the above options are not command line options

options:

-s —> squeeze [replaces the multiple blank lines by a single blank line]
-c —> clear the screen before displaying the file

There are also other commands like “more”, “pico”, “joe”, etc to view files.

 Viewing first and last few lines of a file

Command “head” displays first few lines of a file which is 10 lines by default.

Command “tail” displays last few lines of a file which is 10 lines by default.

Example:

$ head -n 5 /etc/passwd —> displays first 5 lines of /etc/passwd file

$ tail -f /var/log/messages —> it is used to view constantly growing (updating) log file

 File Globbing

It helps to replace the same pattern on multiple filenames. In MS-DOS it is known as wild cards, whereas in Linux, it is known as metacharacters

? —> matches any single character
* —> matches any number of character
[a-z] —> matches into the range
[^a-z] —> matches all except the range

Example:

$ rm a??? —> deletes all files of 4 characters starting with “a”
$ rm a* —> deletes all files preceeding with “a”
$ rm [a-c] —> deletes all files proceeding from “a” to “c”
$ rm *.* —> deletes all files with “.” extension but not directory

Home Directory

Tild i.e. “~” represents home directory

$ ~ —>displays the path of the current user’s home directory
$ dirs
~
$ touch ~user/file1 —> makes a blank file file1 on user’s home directory

 Unit – 4: Users and Groups

User:

  • every user in the system has a unique identification i.e. UID
  • system users has UID ranging from 0 to 499
  • normal users has UID 500 onwards

 # cat /etc/passwd

<username>:<encrypted password>:UID>:<GID>:Comment:<Home directory>:<default shell>

user:x:500:500::/home/user:/bin/bash

 Group:

  • every user have a unique group and its group identification i.e. GID
  • system groups has GID ranging from 0 to 499
  • normal user’s GID is 500 onwards
  • there may be more than one users in a group
  • by default, a group for each user of the same name as of the user’s

 # cat /etc/group

<group name>:<encrypted password>:<GID>:<list of users>

 Linux security model

  • normal users have limited access
  • root users are the superusers who have the complete control over the system
  • as much as possible, the administrator should know more and more works through normal account

 [user@localhost user]$ su –

{root password}

[root@localhost root]#

The “su” command refers to switch user that gives the superuser user priviledge.

 Choosing a good valid password:

  • minimum 6 character and maximum 256 characters long
  • it should not be based on a dictionary word
  • passwords are case sensitive
  • it should not be similar to the login name
  • it should contain one non-alphanumeric character

 “whoami” —> displays the current user’s login name

“id” or “groups” —> displays the name of group the user belongs to

“users” or “w” or “who” —> prints the login name of users currently logged in to the current host(machine)

 Unit – 5: File access permissions

User categories:

u —> user
g —> group
o —> others
a —> all

File permission type symbolic notation octant notation

  • read r 4
  • write w 2
  • execute x 1

 operators

+ —> to add permission
– —> to remove permission
= —> to assign permission
When we list the content of the directory using “ls -l”, the very first field indicates the file type and its permissions.

# ls-l

drwxr-xr-x 2 root root 4096 Dec 1 04:38 bin
drwxr-xr-x 3 root root 4096 Dec 1 04:24 boot
drwxr-xr-x 20 root root 118784 Dec 6 18:53 dev
drwxr-xr-x 66 root root 8192 Dec 6 18:52 etc
drwxr-xr-x 3 root root 4096 Dec 4 12:41 home

The very first character represents the file type.

– —> normal file
l —> link file
d —> directory
b —> block
c —> character device file

 d rwx rwx rwx

type user group others

The second field represents the number of hard links.
The third field represents the owner of the file.
The fourth field represents the group name of the file.
Root local user

# umask $ umask
0022 0002
# ls -l $ ls -l
–rw-r–r– –rw-rw-r–
6 4 4 6 6 4

  • in umask, the very first character “0” is meant for the special permission
  • by default, the maximum permission is 666
  • maximum assigned is 777

maximum permission
– umask
————————–
= permission of file

 type file directory

r permission to read file permission to list the contents of directory
w permission to write or permission to create or delete files from directory
change the file
x permission to execute permission to access file in the directory

  • root access is independent of the permission type
  • at least read and execute permission is to be assigned to the directory

 “chmod” —> to change the permission of file or directory

Example for writing a command using symbolic notation to provide all permission to user, read and execute permission to group and no permission to others:

$ chmod 750 filename
$ chmod u+rwx,g+r-x,o-rwx filename

changing permission of a directory and its contents:

$ chmod -R {directory}

-R option refers to recursive that makes the permission effective to all of its contents.

 Unit – 6: Linux File System

In Linux file system, the foremost “/” represents the root directory. Its other basic directories are as follows:

  • bin —> contains basic user commands
  • sbin —> contains system commands
  • root —> the home directory of the superuser
  • mnt —> contains the mount point for file systems
  • boot —> contains the kernel and other files used during startup
  • lost+found —> contains the orphan files
  • dev —> contains the device files
  • etc —> contains the configuration files and directories
  • var —> variable files like log files
  • usr —> system related files and directories
  • proc —> a virtual directory, created during startup and is read by the kernel
  • tmp —> a scratch pad for users and programs; it has global read-write permission
  • home —> contains all user’s home directories

Note: There are two types of device files: character device file and block device file. In character device file, data transfer takes place byte by byte, making possible for the serial access. While in block device file, data transfer takes place in blocks, which ranges from 512 bytes to 32 Kbytes. For example, floppy is taken block device file.

 File system details

Superblock: it is file system metadata i.e. data of data, which contains volume name, UID, inode count, block count, size, links, etc.

# stat /etc/passwd

File: `/etc/passwd’
Size: 1840 Blocks: 8 IO Block: 4096 Regular File
Device: 302h/770d Inode: 211561 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2003-12-06 21:33:39
Modify: 2003-12-06 20:51:28
Change: 2003-12-06 20:51:28

Inode: it is an identification node, that also contains the permission, attribute, access time, modification time, etc about the file. Kernel communicates the various files with their inode number. “ls -i” is used to list the inode number of the respective directory.

Hard Link: it enables one file to have two or more names. A user can access file either by the filename itself or the link name. When listed in the directory, it seems to have the mirrored file represented by the link name. But actually, the inode of the original file and the link name is the same.

Syntax: ln {filename} {link name}

 Limitations of Hard Link:

  • directories cannot be hard linked
  • since inode are unique on the same partition but not on the entire system, hard links cannot be used across partitions or drives.

 Soft Link: it stores the name of the file to which it points. If the referenced file is renamed, moved or removed, the symbolic link will point to nowhere. Soft link can be used independent of the partitions on files as well as directories.

Syntax: ln -s {filename} {link name}
$ ls -l
-rw-rw—- filename
lrw-rw—- link name -> filename

 Note: a full path name is to be given if the soft links are to be set in different directories

Locating Files:

locate or slocate: it is used to locate or display the file or directory of specified pattern.

syntax: locate or slocate [patterm]

Example: $ locate abc —> displays all files or directories matching “abc”

Actually, locate or slocate command searches for the specified patter in the Linux database, which isdated every 24 hours. Hence, only the file created before 24 hours could be displayed.

 find: it searches the file of specified pattern directly in the file system.

Syntax: find [path] [option] [filename]

Example: $ find /usr -name hack

 note: if the search is within current directory, path is not required

options:

-atime +n or -n —> find files which were last accessed more than or less than n days ago
-mtime +n or -n —> find files which were modified more than or less than n days ago
-name pattern —> find files whose name match the pattern
-size n —> find files which has a size of n bytes
Archiving Files

Archiving refers making a bundle of files. It is usually done during backup.

Syntax: tar [options] [archive name.tar] [directory]

options:

c —> to create a new archive
v —> verbose mode
f —> refers to archive name of new file
x —> to extract archive
t —> to list archive

 Example: to create archive of /etc directory, named “etc.tar” in /tmp directory:

# tar cvf /tmp/etc.tar /etc
# tar tvf /tmp/etc.tar —> to view contents of etc.tar
# tar xvf /tmp/etc.tar —> to extract the contents of etc.tar

 Compression Utilities:

  • text files can be compressed upto 75%
  • binary files cannot be compressed to large extent i.e. 0 – 25%
  • archive files are often compresses

Compress Decompress

  • gzip gunzip (standard Linux compression utility)
  • bzip2 bunzip (more better than gzip)
  • compress uncompress (standard UNIX compress utility)

Syntax: # gzip [options] {filename}

archiving and compressing simultaneously

# tar cvzf etc.tgz /etc

 Mounting removable media:

Removable medias like floppy, cdrom and zip diskettes cannot be accessed directly. They are represented by a device file, which is to be mounted under specific mount point that generally resides on “/mnt” directory.

syntax: mount [options] [file system type] [device file] [mount point]

Example: # mount -t vfat /dev/fd0 /mnt/floppy —> enables floppy disk of Window’s FAT32 file system.

Note: the floppy will be damaged if it is not mounted properly.

mtools Package:

  • mtools provides easy access to FAT file systems
  • allows access floppy disks of FAT file system
  • most of the commands are very much similar to of MS-DOS

mdir —> to list the contents
mmd —> to create directory
mcd —> to change directory
mren —> to rename a file
mdel —> to delete a file
mtype —> to view the contents of a file

 Unit – 7: String Processing

“wc” —> word count, to display the number of lines, words and characters of a file.

Syntax: wc [options] {filename1} {filename2} {filename3}

options:

-c —> displays only number of characters
-l —> displays only the number of lines
-w —> displays only the number of words

 “grep” —> it searches the text/ pattern in the specified file or directory

Syntax: grep [pattern] [filename] or, ls -l | grep [pattern]

 “sort” —> it sorts a file; default is alphabetic

# sort [option] {filename}

options:

-n —> for numeric sort
-t —> for position

 “diff” —> compares two files as per the line, displays the differences and suppresses the similarities

syntax: diff file1 file2

< file1 contents

> file2 contents

 “cut” —> cuts specific column or character of a file

Syntax: cut [options] [argument] {filename}

options:

-c —> character specific
-f —> field specific
-d —> delimiter

 Example:

# cut -f3 -d: /etc/passwd —> cuts the third column of /etc/passwd
# cut -f1-3 -d: /etc/passwd —> cuts from first to third column of /etc/passwd
# cut -c1-10 /etc/passwd —> cuts from first character to tenth character of /etc/passwd

 “paste” —> adds or joins the output of two or more fields

syntax: paste {file1} {file2} > {file3}

 “tr” —> to translate text from upper case to lower case or vice-versa

syntax: tr ‘a-z’ ‘A-Z’ < {file1}

 “aspell” —> a very powerful spell checker

syntax: aspell check {filename}

 Unit – 8: Standard Input/Output and Pipes

Standard Input and Output:

standard input (stdin) —> keyboard
standard output (stdout) —> terminal window
standard error (stderr) —> terminal window

Redirection separation:

< —> takes standard input from a file
>> —> appends standard output to a file
> —> brings the standard output to a file
2> —> redirects the standard error to a file

Pipes: are used to connect different processes. The output of the previous command is the input for the next command, if they are separated by a pipe “|”.

Example: $ ls -l | grep root —> this command lists the file or directory named root in the current directory.

 “tee” —> it helps to store the intermediate result while letting the process to go further.

Example: $ ls -l | tee file1 | grep root —> this command creates file named file1 that contains the entire list of the current directory and also lists the file or directory named root in the current directory.

 Unit – 9: Introduction to Processes

Process:

The Linux operating system is a collection of software that manages users, programs, hardware and the Linux kernel itself. The kernel is responsible for managing the Linux subsystems: memory management, I/O management, file management and process management. Every running application is called process.

  • It ranges form 1 to 32768 simultaneously.
  • PID is assigned by kernel to each process.
  • The first PID 1 is “init” process, started at boot time that initializes other applications or commands on the basis of runlevels.
  • Except “init” process, every process has a parent.

 “ps” —> is used to report process status

Syntax: # ps -[option]

options:

-a —> displays all processes of the current terminal
-x —> displays all process of the Linux operating system
-l —> displays long listing that includes more information
-u —> displays the user name of the process owner
States of process:

R —> runnable, process is on the run queue; being executed
S —> sleeping, process is not executing and is typically waiting for an event to occur to
wake it up
T —> stopped, process is not executing
Z —> zombie, before the process dies, it notifies its parent and waits for an
acknowledgment. If it doesn’t receive, its PID is not freed up but all other resources
are freed.
D —> uninterruptible sleep, process is sleeping but cannot be woken up
W —> has no resident process
< —> high priority process
N —> low priority process
L —> pages locked into memory

Sending Signals to Processes

Syntax: # killall -9 {process name} or # kill -9 {PID} or Ctrl+c

 Monitoring Process:

“top” —> is used to monitor the processes and the resources used for last 5 seconds.

  • There are 2 important numbers apart from general information: PRI refers to priority number and NI refers to nice number.
  • These nice number can be changed using “renice” and “nice” command.
  • “nice” —> is used to change the priority of the process to be run
  • “renice” —> is used to change the priority of already running application
  • for nice number, -20 is the highest priority, +19 is the lowest priority and 0 is the default.

Syntax:

nice -n {nice number} command
renice {nice number} {PID}
Example:
# nice -n -19 find / -name httpd
# renice -19 2333

 Unit – 10: Shell Basics

Application —> Shell —> Kernel —> Hardware

Shell is command interpreter that was developed for GNU project. There are various types of shell:

sh —> Bourne shell, original Unix shell
csh —> C shell
ksh —> Korn shell
tcsh —> Enhanced C shell
bash —> Bourne again shell, standard for Linux system. It incorporates the feature of all above shells as well as some added feature like process control, redirection facility, etc.

To view the version of the current shell:

# Ctrl+x Ctrl+v

GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)

 “set” —> displays local shell variable

“env” —> displays both local and environmental variable

 User defined variables:

# rhce = 999 or, # rhce = “999” or, # rhce = ’999′ are the same

$ set —> displays rhce=999

$ echo $rhce

999

note:

  • the defined variable is available only for the current shell
  • these variables are not set permanently
  • to make variable rhce=999 available to all shells: # export rhce=999
  • ~/.bashrc contains user specific functions and aliases. The required variable can be added in this file.

# echo $PS1

[u@h W]$ where,
u —> user name
h —> hostname
H —> full hostname
W —> current working directory
d —> date format
t —> time (24 hours format)
T —> time (12 hours format)

 # PS1=”ncit %” —> changes bash prompt

ncit %

 # which {command} —> displays the path of the commands

 $ mkdir $HOME/DIR1 —> creates the directory DIR1 in current user’s home directory

$ touch file1 file2 file3 —> creates file1, file2 and file3 simultaneously in the current directory

$ rm -r file{1,2,3} —> deletes file1, file2 and file3 simultaneously

 Creating clusters of directories:

$ mkdir d1/{d2,d3,d4}/{d5,d6,d7} —> creates directories d5, d6 & d7 in all directories d2, d3 & d4 under directory d1.

$ tree d1 —> displays the directories formed under d1

 Defining variables:

“ “ —> everything is literal inside double quotes

‘ ‘ —> everything is literal inside single quotes

` ` —> backquotes, useful for executing commands

 # echo “the home directory is $HOME”

the home directory is /root

 # echo ‘the home directory is $HOME’

the home directory is $HOME

 # echo `the home directory is $HOME`

bash: the: command not found

 # echo the home directory is $HOME

the home directory is /root

 # echo the current date is `date`

the current date is Sat Dec 13 18:34:56 NPT 2003

 Note:

  • `date` is similar to $(date) command
  • $ & are treated as special characters

# echo “the cost price is $2.0”
the cost price is

# echo “the cost price is $2.0”
the cost price is $2.0

# a=5
# b=4
# echo area is $[ $a * $b ]
area is 20
# echo area is `expr $a * $b`
area is 20

# history —> displays the last 1000 commands executed by the user
# !! —> executes the very last commands
# !a —> executes the very last executed command started with “a”

Concatenating commands

(first command; second command)
first command && second command —> similar to logical AND
first command || second command —> similar to logical OR

 Aliases:

It is used to set the alias to the specific command with or without options
“alias” —> displays all the aliases defined for the current bash
# alias hello=’date’
# hello
Sat Dec 13 19:16:36 NPT 2003

 Startup scripts

/etc/profile and /etc/bashrc are the script files which are executed whenever the user login to the Linux system.

~/.bashrc and ~/.bash_profile are the user specific script files.

 Shell script basics:

Shell script is a text file containing valid Unix/Linux commands to achieve some specific goal.

Steps to execute shell script:

1) make the shell script executable
$ chmod u+x filename
2) assign the directory containing the shell script in executable search path
$ PATH=$PATH:$HOME/{directory}
Example:

#shell script to ask and display roll no.
#/bin/sh
read -p “Enter your name:” NAME
read -p “Enter your roll no.:” ROLL
echo “Your name is $NAME”
echo “Your roll no. is $ROLL”

#shell script to ask the correct password
#/bin/sh
read -p “Enter the password:” passwd
if [“$passwd” = “redhat”]
then
echo “Your guess is correct”
else
echo “Please try again”

 Unit – 11: Vi – Editor

A very powerful editor that has two modes:

a) Insert mode
b) Command mode
Syntax: vi {filename}

Insert mode:
a —> append text from current position
A —> append text at the end of the line
i —> insert text at the current cursor position
I —> insert text at the beginning of the current line
R —> replace mode
s —> substitute the current character
S —> substitute the current line
o —> open a new line below the command line
O —> open a new line above the current line
Esc —> to return to command mode

Command mode:
h —> halt
l —> move the cursor to right
j —> move the cursor to down
k —> move the cursor to up
o —> move the cursor to beginning of the line
$ —> move the cursor to the end of the line
w —> move the cursor to the beginning of the next word
b —> move the cursor to the beginning of the previous word
gg —> move the cursor top of the file

Save and Exit:
:w —> to save content
:q —> to quit (only if file is saved)
:wq or, :x or, ZZ —> to save and exit
:w {filename} —> to save the content in the specified filename
:w! —> to forcibly save the file (read only files)
:q! —> to quit without saving
:e! —> to ignore changes and save the file in its original form

Delete:
dw —> cut a word from the current cursor position
4dw —> cut 4 words from the current cursor position
dd —> cut current line
4dd —> cut 4 lines along with current line
d$ —> to cut the line till its end from the current cursor position
Copy:
yw —> to copy word
yy —> to copy line

Paste:
p —> paste to the right of the cursor position
P —> paste to the left of the cursor position

Searching and Replacing Text:

  • /{pattern} —> forward search
  • ?{pattern} —> backward search

note: use “n” to go for next position of the same pattern

:s/oldtext/ /newtext/c —> ask for confirmation
:s/oldtext /newtext/g —> no confirmation