Red Hat HPC Training

Download Report

Transcript Red Hat HPC Training

Welcome!
RHCE-I Part
Durgesh Tomar
Linux History and
Introduction
Overview: Linux History and
Introduction
2
What is Linux ?

A fully-networked 32/64-Bit Unix-like Operating System

Multi-user, Multitasking, Multiprocessor

Has the X Windows GUI

Coexists with other Operating Systems

Runs on multiple platforms

Includes the Source Code
3
Why Linux

Linux is Unix like OS.

Multi-user and multi-tasking i.e.

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.

a user can have more than one process (program) executing at the
same time.

Virus free
4
Linux Origin
Richerd Stallman
Linus Torvalds
5
Linux Origin

In 1984 Richard Stallmon - The GNU Project a Free Software Foundation
project .The idea was to provide a complete working UNIX like Operating
System at the earliest. The development of a complete OS was going to
take a long time ,to speed up his project, Richard’s looked for utilities
already available with source that can be added to GNU Project. Most of
the work was completed but still the core kernel was not available.

Around the same period i.e. in the late 80’s and early 90’s , Linus Torvalds a
Graduate student in Finland, began developing UNIX like kernel and in 1991
he first announced his work in email message on the comp.os.minux mailing
list.

This was what Richard Stallmon looking for, and added it in his Free
Software Foundation project.
6
What is GNU Open Source?

Freedom 0: The freedom to run the program, as you wish

Freedom 1: The freedom to study the source code and change it to
do what you wish

Freedom 2: The freedom to copy and redistribute the program when
you wish

Freedom 3: The freedom to distribute modified versions, when you
wish
7
Linux Destros


Distribution Concept
Free Distributions








Fedora Core
Debian and Ubuntu
Gentoo
Slackware
Knoppix
centOS
Many, many more
Commercial Distributions



Red Hat Enterprise
Suse
Yellow Dog
8
Red Hat Distribution

Red Hat Enterprise Linux



Stable, thoroughly tested software
Audience are corporate and enterprise users.
Fedora Project




More new applications
Red hat sponsored project, but no official Red Hat support
Community supported.
Audience are personal systems.
9
Red Hat Enterprise Linux

Commercial Distribution

Enterprise-targeted operating system

Focused on mature open source technology

12-18 month release cycle

Versions available started from 2.1, 3.0, 4.0 now 5.0

Support available up to 24 x 7 coverage plans

Supports many processor architectures
Intel x86-compatible, Intel Itanium 2, AMD64, IBM PowerPC on
eserver iSeries and eServer pSeries and IBM Mainframe on
eServer zSeries and S/390
10
The Fedora Project

Red Hat-sponsored open source project

Focused on latest open source technology

Rapid four to six month release cycle

Available as free download from the internet

An open, community-supported proving ground for technologies
which may be used in upcoming enterprise products

Red Hat does not provide formal support for Fedora Project
11
Installation of Red Hat Enterprise Linux
12
Types of Installations


Fresh Installation ( New )
Upgradation
Methods of Installations


Attended
Unattended
Modes of Installations

GUI Based (Graphical Installation)

CLI Based (Text Based Installation)
13
Sources of Installation

CD-ROM

Hard Disk

Network




NFS Server
FTP Server
HTTP Server
PXE installation
14
Conventions for Naming the
Disks and Partitions

Linux uses a very different naming scheme that provides far more information than that
used by MSDOS or Microsoft Windows
/dev/xxyN

The dev refers to the devices directory. The devices directory is where all information
about devices is stored

The xx refers to the type of hard disk. The xx is replaced by hd for an Integrated Drive
Electronics (IDE) hard disk and by sd for a Small Computer Systems Interface (SCSI) hard
disk
IDE Hard Disk
= /dev/hd
SCSI Hard Disk = /dev/sd
15
/dev/xxyN
Primary Master
Primary Slave
Secondary Master
Secondary Slave
= /dev/hda
= /dev/hdb
= /dev/hdc
= /dev/hdd
The y is replaced by a letter representing each disk. For
example, a would be the first disk and b the second
16
/dev/xxyN
•
The N is replaced by a number for each partition on a drive. For
example, 1 would be the first partition and 2 the second
Primary partition
/dev/hda1
Extended partition(/d ev/hda2)
/dev/hda5
1st logical partition
/dev/hda6
2nd logical partition
Primary masterIDE hard disk (/dev/hda)
17
Primary partition
/dev/hda1
Extended partition(/d ev/hda2)
/dev/hda5
1st logical partition
/dev/hda6
2nd logical partition
Primary masterIDE hard disk (/dev/hda)

Primary partitions are represented by the numbers 1, 2, 3, and 4.
Logical partitions start with 5.


/dev/hda1 is the device file for the first primary partition on the
first EIDE disk
/dev/hda5 is the first logical partition on the second IDE disk
18
Mount Point

The Linux operating system is a filing system

Whenever you want to attach another filing system to Linux, you
must tell Linux where to see the contents of the file system you
are attaching. This is called the mount point.

The mount point for the CD-ROM file system is
/media/cdrom
19
You may install the boot loader in one of two
places:

The master boot record (MBR) — This is the recommended
place to install a boot loader, unless the MBR already starts
another operating system loader, such as System Commander.
The MBR is a special area on your hard drive that is automatically
loaded by your computer's BIOS, and is the earliest point at which
the boot loader can take control of the boot process. If you install it
in the MBR, when your machine boots, GRUB presents you the
grub menu which can be configured to boot Red Hat Enterprise
Linux or any other operating system.

The first sector of your boot partition — This is recommended if
you are already using another boot loader on your system. In this
case, your other boot loader takes control first. You can then
configure that boot loader to start GRUB, which then boots Red
Hat Enterprise Linux.
20
What is SWAP Partition ?
swap partitions are used to support virtual memory. In
other words, data is written to a swap partition when
there is not enough RAM to store the data your
system is processing.
21
Size of SWAP ?

At least 256 MB

Twice the amount of RAM on your machine

Swap should equal 2x physical RAM for up to 2 GB of physical RAM,
and then 1x physical RAM for any amount above 2 GB, but never less
than 32 MB.

Red Hat Enterprise Linux supports up to 32 swap files
22
Sample Partition Structure
Mount Point
Size
/boot
200 MB
/
10000 MB
/usr
2048 MB
/var
512 MB
/home
512 MB
SWAP
Use recommended size
23
Thank You !!!
Logging in to a Linux System

Access to a system requires authentication.

Whether it is a text base or graphical login, you need to provide a correct
username and password, to login into the system.
25
Switching between Virtual consoles and GUI
environment

Linux system will run six virtual consoles and one Graphical consoles

You can switch between Virtual Consoles by pressing CTRL+ALT+F[1-6]

You can switch to Graphical Console by pressing CTRL+ALT+F7

Each Virtual consoles keep his own history of data displayed on the screen
in video memory. But as the information is in buffers, the information is lost
when you change to another virtual console. User can scroll the screen
using Shift+PgUp an Shift+ PgDn.
26
X Windows System

RHEL’s graphical environment is the X Window System.

Where X is a client/server protocol that regulates the communication
between applications(client) and the System that provides display
services (Server).

X does not define the look or behavior but it defines the
communication between server and client.

The actual look and behavior of the graphical environment is
determined by the Desktop environment.

RHEL provide two desktop environment:

GNOME (GNU Network Object Model Environment).

KDE (K-Desktop Environment).
27
Starting the X Server

If system come up in virtual console, user can start X Server manually by
executing command

startx &

Here & is not the part of startx but merely make this process a background
process (& is covered later in the course).

When startx is executed the system will start X Server on CTRL+ALT+F7
automatically and will automatically switch to the X Server.

You can still return to virtual console(s) by pressing CTRL+ALT+F1 to
CTRL+ALT+F6 and can return back to X Server by pressing
CTRL+ALT+F7.
28
Starting the terminal under X Server

You can right click on the Desktop and
select “Open Terminal”.

You can click the
Applications > Accessories > Terminal

Press ALT+F2, write gnome-terminal and press run.
29
Changing password

You can change your password by executing passwd command.

Password Policy for Privileged user (root):





Password Policy for Un-Privileged user:





Can change password of anyone.
Can give password less than eight characters (but will be warned during
password change).
Can use dictionary words (but will be warned during password change).
Not asked for current password, while changing the password.
Can only change his own password.
Cannot give password less than eight characters.
Cannot use dictionary words.
Need to supply current password, before he can change the password.
General Password policy:


Password is case sensitive.
Can use numbers, spaces, lower, upper case letters, punctuation and other
special characters.
30
The root user

The root is a special administrative account and is also called SuperUser
and has complete control over the system.

Thus root user can do unlimited damage to the system.

Do not login as root unless necessary.
31
Changing identity

su – <username> can be used to change identity.

Without <username> su assumes that you wish to become root.

When – is passed as an argument to su a login shell of that user/root is
created.
32
Who are u?

whoami command can be used to check who am i.

id command is another useful command which provide details about the
who you are and what groups you are in.

If id command is executed with a username it provides details about that
user.

id command is in /usr/bin/ and can be used by unprivileged user as well.

Id command can also be used to display SELinux context when with -Z
switch.
33
Text Editors



nano
vim
gedit
34
Running commands and Getting Help
35
Running commands

Commands have the following syntax:

command options arguments

Where options modify a command’s behavior.

Single letter options usually precede by –

Full word options usually precede by --

Arguments are on which command will take action.

If you enter a command and do not get a prompt back, the command may
be busy executing or may be waiting for input.

You can interrupt a command by pressing CTRL+c

You can separate multiple commands on the same line with semicolons.

mkdir backups; cp *.txt backups/
36
Simple command

date command
Print or set the system date and time.

date without any parameter prints the current system date.

You can print date is specific format by specifying format in +“format”

-s option is used to set the system date and time.
37
Simple command

cal command : Display a calendar


cal without any parameter will display calendar for the current month and year.
.
cal –y 2009 will display complete calendar for the year 2009

cal 9 2009 will display calendar for September 2009

38
Getting help

whatis

command --help

man

info

Extended Documentation

Red Hat Documentation
39
whatis <command>

Searches for the given command name in a database of short descriptions.
If it finds a match, the description is printed to your screen.


e.g.:

[root@localhost root]# whatis cal

cal (1) - displays a calendar
(1) represents “chapter” of the Linux Manual where more through
documentation can be found.

The database is regenerated automatically every night (automatic update).
This means that on newly-installed system whatis will not work at first
because the database does not yet exist.

Running makewhatis command as root regenerate whatis database.
40
<command> --help

provides short help (but more than whatis) on usage,
arguments(long and short arguments) etc.

Not every command provide this type of help e.g. cal command.
41
man <command>

Man pages normally contain sections:

Its NAME and a short description of what it does

A SYNOPSIS of its usage, including available switches

A longer DESCRIPTION of the command's functionality

A switch-by-switch listing of its OPTIONS

Any FILES associated with this command.

Any known BUGS in the command.

EXAMPLES, showing how to use the command.

A SEE ALSO section for further reference.
42
man <command> - contd…

The linux man page is divided into sections, each of which covers a
particular topic, and every man page is associated with exactly one
of these sections. The sections are:
1) User commands
2) System calls
3) Library calls
4) Special files
5) File formats
6) Games
7) Miscellaneous
8) Administrative commands

e.g.

man [<chapter>] <command>

man 1 passwd
-> refers to the user command

man 5 passwd
-> refers to the file format
43
man <command> - contd…


Navigating man page:

Navigate with arrows, pgup, pgdown

/<text>
Search for text

n/N
Next / Previous match

q
quit viewing page
-k option searches and list all commands in whatis database whose
short description match the specified keyword e.g.


man -k delete
Note that this uses the “whatis” database.
44
info <command>

Similar to man, but often more in-depth.

Sometimes they are just copies of the corresponding man page.

The structure of an info page is similar to that of a website.

Links are denoted by a preceding asterisk (*).

Navigating info page:

Navigate with arrows, pgup, pgdown.

Tab
move to next link

Enter
follow link the cursor is on

n/p/u
view next/previous/up-one node

s[<text>]
search for text

q
quit viewing page
45
Extended Documentation

Located at /usr/share/doc directory

It can contain :

sample configuration files,

HTML/PDF/PS documentation

License details
46
Red Hat Documentation

Available on docs CD or Red Hat website

These docs are available in html format and HTML.

http://www.redhat.com/docs/

The documents contains books on


Installation Guide

Deployment Guide

Virtualization Guide
Knowledge base is the another option to get help, which contains
common questions and their solutions:

http://kbase.redhat.com
47
Browsing the Filesystem
48
Filesystem Basics

File and directories are organized into a single-rooted inverted-tree
structure.

The base of the inverted-tree hierarchy is know as / (root), the top of
the file structure.

A forward slash separate elements of a pathname e.g. /var/ftp/pub

Names in linux are case-sensitive.

.. Refers to the parent directory of any particular directory – i.e. one
level up the current working directory in the file hierarchy.

. Refers to the current directory.

Files and directories whose name begin with a . are hidden.
49
home directories

Every user has a home directory.

When a user logs in, he begins in his home directory.

Root's home directory is /root.

Most non-root /unprivileged user’s home directories are in the /home
tree, usually named after the user.
50
Some Important Directories



Home Directories:

/root
: root’s home directory.

/home/username
: non-root users home directory
User executables:

/bin
: Essential user binaries

/usr/bin
: Non-essential user binaries

/usr/local/bin
: Third party user binaries
System Executables:

/sbin
: Essential system binaries

/usr/sbin
: Non-essential system binaries

/usr/local/sbin
: Third party system binaries
51
Some Important Directories


Mountpoints:

/media
:
The /media/ directory contains subdirectories used as mount points for
removeable media such as usb storage media, DVDs, CD-ROMs, and Zip disks

/mnt
:
Filesystems that are non-removable media but are not part of the Red Hat
Enterprise Linux hierarchy are usually mounted here, such as NFS file system
mounts.
Configuration files:


/etc
: Most of the configuration files are stored in the directory
Temporary files:

/tmp/
: used by application to store temporary data.
52
Some Important Directories

Kernels and bootloader:


/var
: contains regularly changing system files, such as log, print spools.
Shared Library: /lib, /usr/lib, /usr/local/lib


: boot loader, Kernel, initrd and loader’s configuration files are stored.
Server Data:


/boot
contains library that provide shared code used by many linux applications.
System Information:

/proc provide information about a running Linux system and allows some
tweaking while a system is running.

/sys provide information about hardware.
53
Some Important Directories

Device files:

/dev/
:
The /dev/ directory contains device nodes that either represent devices that are
attached to the system or virtual devices that are provided by the kernel.
54
Current working directory

The current working directory is the directory in which you are
currently working.

You can type pwd at the command line, which will return the
absolute path to you current working directory.
55
File and directory names

Names may be up to 255 characters.

All characters (including special characters) are valid, except the
forward-slash.

Names are case-sensitive meaning thereby MAIL, mAIL, Mail are
three different names.
56
Absolute and relative pathname

The location of a directory or file can be specified by either of two
methods:

By its absolute pathname or

its relative pathname
57
Absolute and relative pathname

Absolute path:

Absolute pathnames being with a slash (/).

Absolute path contains the name of each directory that must be
traversed form the root file system, in order, to reach the object being
named.

The absolute pathname specifies a 'road map' from the root of the file
tree to its location in the file system.

This 'road map' is valid regardless of the current directory.

e.g.:

cd /var/ftp/pub
58
Absolute and relative pathname

Relative Pathnames

Relative pathnames do not begin with /

It contains the name of each directory that must be traversed from the
current directory to reach the object being named.

The first component of the pathname must exist in the current directory
for the pathname to be valid.

The special directory name .. refers to the parent of the current
directory, and can be used as part of a pathname.
59
Changing Directories

cd command is used to change directory

cd without any argument will move the user to his home directory.

~ represents users home directory. Thus cd ~ will also move the user to his
home directory.

cd .. will switch the user to the parent of his current directory.

– represents the last working directory, thus cd – will move the user to the
previous working directory.

cd path-to-directory will move the user to the directory who’s path is
mentioned. Here path-to-directory can be relative or absolute.
60
Listing Directory Contents

ls command will display directory contents

ls [option] [file or directory]

ls –a
:
display contents including hidden files

ls –l
:
display extra information

ls –R <dir>
:
traverse directory recursively and display contents.

ls –ld <dir>
:
display information about the specified directory

ls –lh
:
displays the size in readable format i.e. 1K 1M 1G etc.
61
Copying files and directories

cp command – copy files and directories

cp [options] source-name destination

-R
:
recursively copy the specified directory

-p
:
Preserve permissions, ownership and time stamps

-a
:
copies file and directories recursively while preserving permission

When more than two arguments are given, the last argument is interpreted
as a destination directory and the rest are treated as source.

If the destination is a directory, the copy is place there

If the destination is a file, the copy command overwrites the destination

If the destination does not exist, the copy is renamed.
62
Moving and renaming files and directories


mv – move and / or rename files and directories

mv [option] <source_file> <destination>

mv [option] <source_file1> <source_file2> <source_file3> <destination>
More than one file may be moved at a time if the destination is a
directory.


mv /etc/*.conf .
Moves all conf file from /etc to current dir
When the source path and the destination path are same the file is
renamed.
63
Create and removing files

Creating file

touch command : create empty files or update file timestamps

If the last time you accessed a file was at 10:02pm and you touch the file at 10:45pm,
the file will show its last access at 10:45pm.

If you touch a file that does not exist, an empty file will be created.

cat command: cat command can also be used to create a file.

cat > sunil.txt
This is my first cat file.
Press ctrl+d

Removing files

rm command– remove files. One or more files can be removed. By default, rm will
not remove directories.

-r
will delete directories and its contents recursively.

-i
ask before removing

-f
suppress warnings about write protected files
64
Create and removing directories

mkdir command
can be used to create directories.


mkdir directory-name will create a directory with a name “directory-name”

-p
Make any missing parent directories for each argument.
rmdir command
can be used to delete an empty directory.

rm –r command
will recursively remove a directory tree.
65
Using Nautilus

Context menu


Right click to rename, cut, copy or paste
Drag and drop

Drag
:
move on same filesystem, copy on different filesystem

Ctrl + Drag
:
always copy

Alt+ Drag
:
ask whether to copy, move or create symbolic link.
66
Determining file content

file command can be used to check file type e.g.
67
Thank You !!!
Users, groups and permissions
69
Linux Security Model

Users and groups are used to control access to files and
resources

Users log in to the system by supplying their user name and
password

Every file on the system is owned by a user and associated with
a group and can only access the resources its owner or group
can access.
70
Users

Every user of the system is assigned a unique User ID number
(the uid) which normally starts from 500.

User's names and uids are stored in /etc/passwd

Users are assigned a home directory present in /home and have
full access to their home directories.

Users cannot read, write or execute each others files without
permission, although there are some exceptions.
71
Groups

Users are assigned to groups with unique group ID numbers
(the gid) which normally starts from 500.

gids are stored in /etc/group.

Each user is given their own private group.

They can also be added to other groups to gain additional
access.

All users in a group can share files that belong to the group.

By default, a user belongs to a group that is named the same as
their username.

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.
72
What is Permission ?

Specifies what rights are you granting to users to access the
resources are available in the computer, so that important
resources such as files are protected from unauthorized users.

Every file and directory has permissions set that determine who
can access it.

Permission are set for:


The owner of the file.

The group members

All others
Permissions that are set are called read, write and execute
permissions.
73
74
Permissions
r
Owner has read
w
Owner has write
x
Owner has execute
r
Group has read
-
Group does not have write
x
Group has execute
r
Others have read
-
Others do not have write
x
Others have execute
75
Methods of Implementing Permissions

Symbolic Method

Numeric Method
76
Permissions- Symbolic Method

Four symbols are used when displaying and applying permissions

r
Permission to read a file or list a directory’s contents

w
Permission to write to a file or create and remove files
from a directory

x
Permission to execute a program or change into a
directory and do a long listing of the directory

-
no permission ( in place of the r, w, or x )
u user
g group
o others
a all three categories
+ Add a permission
- Remove a permission
= Assign a permission
77
Applying Permission with Symbolic Method

chmod command is used to change access modes. When –R is
used permission are changed recursively.

Symbolic Method

chmod u+w,go-w somefile  file name
( Grants write access to owner but denies it to group and other. )

chmod u=rw somefile  file name
( Sets read and write permission to owner, with execute turned off,
regardless of the current permission )

chmod +r somefile  file name
( Make the file world-readable )
[ Note : +, - and = are operators to add, remove or overwrite
the permission ]
78
Permissions- Numeric Method

Four numbers are used when displaying or applying permissions

4
Permission to read a file or list a directory’s contents

2
Permission to write to a file or create and remove files
from a directory

1
Permission to execute a program or change into a
directory and do a long listing of the directory

0
no permission ( in place of the 4, 2, or 1 )
79
Applying Permission with Numeric Method
chmod command is used to change access modes
Numeric Method
●
●
●
chmod 664 somefile  file name
( Grants Read and Write access to owner and group but
read only it to and other. )
chmod 600 somefile  file name
( Sets read and write permission to owner, with execute
turned off, with others and group have no permissions )
chmod 444 somefile 
file name
( Make the file world-readable )
80
Permissions on files

Read permission means the contents of the file can be
examined with a command, such as cat or less.

Write permission means the file can be edited and saved.

Execute permission means the shell will attempt to execute the
file when its name is entered as a command.
81
Permissions on directories

Read permission means the contents of the directory can be
listed with ls.

Write permission means the file may be created in the
directory.

Execute permission means that the user can cd to that
directory and do a long listing (read permission without execute
permission permits a listing, but not a long listing).

A file may be removed by anyone who has write permission to
the directory in which the file resides regardless of the
ownership or permission on the file itself.
82
Changing ownership and group

Changing owner:

chown [-R] user_name <file or directory>

Changing group:

chgrp [-R] group_name <file or directory>

Changing owner and group:

chown [-R] user_name:group_name <file or directory>

-R operate on file and directories recursively.
83
Linux Process Security

When a process tries to access a file, the security context of
the process is matched against the owner and group affiliation
of the file.

If the user is the owner of the file, the user permissions
apply regardless of the group and other permissions.

If the user is the member of the file's group, the group
permissions apply, regardless of the other permission.

If the user is neither owner of the file nor the group of the
file, then the other permissions apply.
84
Umask

umask is a value which is subtracted from default to determine
new file / directory permissions

Default permission for files is 666

Default permission for directories is 777

Default Permission – Umask = Effective Permission


Non-Privileged Users umask is 002

root’s umask is 022

Example :
666 – 002 = 664

666 – 022 = 644

777 – 022 = 755
The umask is set in file /etc/bashrc at line 9 and 11
85
Using the Bash Shell
86
Wildcard expansion

ls chap*
Displays all the files starting with chap along with file chap

ls chap??
Displays files having name chap with exactly two any character after chap.

ls chap[ijk]
Display files having name chap with either i, j or k after chap.

ls chap[c-f]
Display files having name chap with the next letter c, C, d, D, e, E, f. RHEL uses UTF8 encoding of characters, which means that each capital letter comes directly after the
corresponding lower-case letter. This means chapF will not be display.

ls *[a-Z]
will display all files which either contains only characters or end with character.

ls [a-Z]*
will display all files which either contains only characters or begin with character.
87
Command Line shortcuts – The Tab Key

Tab key can be used to complete command line.


Type ls e<tab>
As the directory contain a only file named elephant.ppt which begin with
letter e, the whole command will be completed as:


ls elephant.ppt
If there are two files elephant.ppt and encyclopedia.ppt the command will
not be completed as there will be a confusion that which file name should be
used to complete the command.
88
History

Bash stores a history of commands you have entered, which can be used to
repeat instead of re-typing.


The history is stored in every users home directory in bash_history file.

!!
-
repeat last command.

!c
-
repeat last command that started with c.

!n
-
repeat a command by its number in history output.

!?abc
-
repeat last command that contains
Repeat the last command with old changed to new, e.g.:

# cp filter.c /usr/local/src/project
# ^filter^frontend
cp frontend.c /usr/local/src/project
89
History Tricks

Use up and down arrow keys to scroll through previous commands.

ctrl+r to search for a command in command history
(reverse-i-search)`':

To recall last argument from previous command
esc+ .
Alt + .
!$
90
Command Line expansion

Tilde (~)
refer to users home directory.

cat ~/.bash_profile
will print details of .bash_profile present in users home directory.

Command Expansion: $() or ` `
echo “This system’s name is $(hostname)”
echo “This system’s name is `hostname`”
Both the above will display
This system’s name is station35.example.com
91
Command Line expansion

Brace Expansion: { }
echo file{1,2,3}
above will display file1, file2 and file3
mkdir -p work/{inbox,outbox,pending}/{normal,urgent,imp}
92
Bash variables

variables are named values, useful for storing data or command output.

Value of the variable can be set as variable=value

Values can be referred as $variable

e.g.:
files=$(ls /etc)
echo $files
93
Scripting basics

text files that contain a series of commands or statements to be executed.

useful for

automating commonly used commands

performing system administration and troubleshooting.

creating simple applications

manipulation of text or files.
94
Creating Shell scripts

Step 1: use a text editor such as vi to create a text file. First line contains
the magic “shbang” sequence #!, which this tells the operating system which
interpreter to use in order to execute the script.
#!/bin/bash

Comment start with a #

Step2:

make the script executable:
chmod a+x myscript.sh

To execute the new script:
./myscript.sh
95
Sample shell script

#!/bin/bash

#This script displays some information about your environment

echo “Greetings, the date and time are $(date)”

echo “Your working directory is: $(pwd)”
96
shell script

Use mutt to check mail. The user sunil datta and rakhi should exist.
97
shell script
98
File Tests
operator
-d FILE
-e FILE
-f FILE
-h FILE
-r FILE
-s FILE
-w FILE
-x FILE
-O FILE
-G FILE
Meaning
True if file is a directory
true if file exists
true if file exists and is a regular file
true if file is a symbolic link
true if file is readable by you
true if file exists and is not empty
true if the file is writable by you
true if the file is executable by you
true if the file is effectively owned by you
true if the file is effectively owned by your group
99
shell script
100
Thank You !!!
Standard I/O and Pipes
102
Standard Input and Output

Linux Provides three I/O channels to processes

Standard input

Standard Output

Standard Error
103
Standard input

Standard input:

The stream can come from

keyboard(default),

a file (using a feature called redirection).

Another program (using the concept of a pipeline)

Standard Output:

The standard output stream also has three similar destinations:


From terminal(default).

from a file.

Input from another program.
The standard output of commands, which ordinarily displays on the terminal,
can be redirected into a file or piped into another command.
104
Standard Input and Output

Standard Error:

The standard error stream includes all error messages written to the
terminal.

The output may be generated either by the command or by the shell, but in
either case, the default destination is the terminal.

The standard error, which also ordinarily displays on the terminal, can be
redirected into a file.
105
Standard Input and Output

Each of these three streams have a number, called file descriptor, which is
used for identification.

The file number 0 represented by standard input (STDIN),

While 1 signifies the standard output file (STDOUT).

The standard error file is represented by the descriptor 2 (STDERR).

These descriptors are implicitly prefixed out the redirection symbols.

The fact that there are two output channels allows separation of error
messages from normal output.
106
Redirecting output to a file

STDOUT and STDERR can be redirected to files.

Supported operators include


> redirect STDOUT to file

2> redirect STDERR to file

&> redirect all (STDOUT and STDERR) to file.

< send file as input to command
>> will append the data to file and > will overwrite the file contents.
107
Redirecting example

Below command will find passwd file in /etc


Will direct output to file and error on screen


find /etc –name passwd > output.txt
Will direct output on screen and error to file


find /etc –name passwd
find /etc –name passwd 2> error.txt
Will direct both output and error to file

find /etc –name passwd &> output_error.txt
108
Redirecting example

Will direct output to find.out and error to find.err


Will direct output to file.txt and error to /dev/null


find /etc –name passwd > find.out 2> find.err
find /etc –name passwd > find.txt 2> /dev/null
Will forward STDOUT and STDERR to program less

find /etc –name passwd 2>&1 | less
109
Redirecting STDOUT to a program (piping)

Redirect STDOUT to tr command.


ls –C | tr ‘a-z’ ‘A-Z’
tr command translate characters
110
Redirecting to Multiple Targets (tee)

tee command read from standard input and write to standard output and
files.


ls | tee ls.txt | less
In above command tee will store the output of ls in file ls.txt and will also
pipe the output of less to STDOUT .
111
Redirecting STDIN from a file

< can be used to redirect STDIN from file to a command


tr ‘A-Z’ ‘a-z’ < .bash_profile
This command will translate the uppercase characters in .bash_profile to
lowercase. The above command is equivalent to

cat .bash_profile | tr ‘A-Z’ ‘a-z’
112
Text Processing Tools
113
Viewing file contents less and cat

cat [option] command
dump one or more files to STDOUT. If you dump the content of a binary file
with cat, you will make it unusable. You can use reset to clean up your
garbled terminal.

cat –b command will print number before each non-blank line.

less command
view file one page at a time.

e.g. ls –l | less

/text searches for text

n/N repeats the last search down/up respectively.
114
Viewing file head and tail


head command

displays first few lines (default: 10 lines) of text in a file.

-n or --lines displays specific no. of lines.

head -n 3 /etc/passwd displays first 3 lines.
tail command

displays last few lines (default: 10 lines) of text in a file.

-n or --lines displays specific no. of lines.

tail -n 3 /etc/passwd displays last 3 lines.

using -f causes tail to continue to display the file in “real time”, showing additions
to the end of the file as they occur.

useful for watching growing files.

E.g.: tail –f /var/log/messages
115
grep command

grep scans a file for the occurrence of a pattern and can display the lines
having selected pattern.

grep can also select lines not containing the pattern.

grep options pattern filename(s)

-i case-insensitive search.

-n print line numbers of matches

-v print lines not containing pattern

-r recursively search a directory
116
grep command Examples

create a file have no director record


Search for cat and Cat in file pets


grep -v 'director' emp.lst > otherlist
grep ‘[cC]at’ pets
Extract line containing word passwd from the output of ls /etc command

ls /etc | grep passwd
117
Extract text by column or field cut


Display specific columns of file data

-f field-list
: specifies field or column.

-d input_delim_byte
: specifies field delimiter (default tab).

-c character-list
: cut by characters.
e.g.

Will display 3 field -d: specifies that the separator is :


display first 2 characters of each line.


cut -f3 -d: /etc/passwd
cut -c1-2 /etc/passwd
Retrieve ip address from the output of ifconfig:

ifconfig | grep ‘inet addr’ | cut –d: -f2 | cut –d ‘ ’ –f1
118
wc command - word count


wc - also counts lines and characters

–l
for only line count

–w
for only word count

–c
for only character count
Without any option wc prints New line count, words, total characters
119
sort command

sorts text to stdout – original file unchanged
sort [options] file(s)

common options


–r
perform reverse sort (descending)

–n
perform numeric sort, else alphabetic sort

–f
ignore case of characters in strings

–u
unique(remove duplicate lines in output)

–tx
'x' – use x as field separator

–k
p1
sort from field p1

–k
p1,p2
sort from field p1, ending at p2
sort /etc/passwd by the field uid, treat : as column separator, -n treat it as
numeric sort.

sort -t : -k 3 -n /etc/passwd
120
uniq command – eliminating duplicate lines

print only unique line occurrences in a file.

input to uniq must first be sorted.


-fn
avoid comparing first n lines.

-sn
avoid comparing first n characters.

-u
only print unique lines.

-d
print only duplicate lines

-c
prefix lines by the number of times that line was repeated.
e.g.

sort userlist.txt | uniq –c
121
Compare files with diff command


Compare files line by line.

diff list1.txt list2.txt

4c4

< meeta

---

> sonu

9c9

< sana

---

> beena
4th line and on 9th line doesn’t match.
122
Duplicating file changes - patch command


patch command is used to store the output of a diff –u to an original file.

diff –u file1.txt file2.txt > file.patch

patch –b file1.txt file.patch
Here

–u use the unified output format.

–b will make a backup file before patching.
123
aspell and look command

aspell command

is an interactive spell checker.

It offers suggestions for corrections via a simple menu-driven interface.

check option is used to spell check a file.

e.g.:

aspell check errorfile.txt

look command

A quick spelling dictionary lookup can be performed with look.

e.g.:

look exer
124
sed command – text manipulator

The entire pets file will be sent to standard output, but the replacement of
first occurrence of cat for dog will be performed on line 10 through 35.


sed ‘10,35 s/cat/dog/’ pets
In this example first occurrence of cat will be replaced with dog, the
replacement will start from line having word digby and will end at line having
word duncan.

sed ‘/digby/,/duncan/s/cat/dog/’ pets

Multiple editing can be done using –e option. Here cat will be replaced with
dog and cow will be replaced with goat in the entire file in the entire file
because of option g.

sed –e ‘s/cat/dog/g’ –e ‘s/cow/goat/g’ pets
125
sed command – text manipulator


When there are numerous editing instructions to be performed, it will be
better to use the –f option to accept instructions from a file. Create an
instruction in a file named myedits with the following contents:

s/cat/dog/g

s/cow/goat/g

s/donkey/monkey/g
Now execute the following sed command, which will execute multiple
instruction

sed –f myedits pets
126
Special characters for complex searches
regular expression

^ represents beginning of line

$ represents end of line

Used with grep, sed

Will match lines beginning with “C or c” followed by “hapter”


grep ^[Cc]hapter /etc/passwd
Will match lines having O or o followed by ne at the end

grep [Oo]ne$ /etc/passwd
127
Vim – An Advanced Text Editor
128
vi editor

vi - the basic text editor

three modes

Command mode (default)
: Move cursor, cut/past text, change mode

Insert mode
: Modify text

esc mode
: Save and quit etc

To enter insert mode press i key

To exit insert mode press ESC key

To start vim editor
vim filename
129
Modifying a file – Insert mode

i
begins insert mode at the cursor

A
append to end of line

I
Insert at beginning of line

o
Insert new line after current cursor position

O
Insert new line before current cursor position
130
Saving a file and Exiting vim – Ex mode

:w
write the file to disk

:w!
write the file to disk forcibly

:wq
write the file to disk and quit

:q
quit vim editor

:q!
Quit vim editor forcibly
131
Moving around – command mode

Move by word
w, b

Move by sentence
), (

Move by paragraph
}, {

Jump to line
:x

Jump to end
G
or xG
132
Search and replace – command mode

/text
search text downward

?text
search text upward

n/N
search above/below

1,5s/cat/dog
replace cat with dog at line 1 to 5

%s/cat/dog
replace first occurrence of cat with dog in complete file

%s/cat/dog/g
replace cat with dog in complete file
133
Manipulating text – Command Mode
Change
(replace)
Delete (cut)
Yank (copy)
Line
cc
dd
yy
Letter
cl
dl
yl
Word
cw
dw
yw
Sentence ahead
c)
d)
y)
Sentence behind c(
d(
y(
Paragraph above c{
d{
y{
Paragraph below c}
d}
y}
134
Put (paste)

p put the data below the current line.

P put the data above the current line.
135
Undoing changes – command mode

u undo changes

Ctrl + r redo last undone change
136
Configuring vim

Display number on the left margin before each line.


:set nu
or
: set number
Causes searches to be case-insenstive:

:set ic
or
:set ignorecase
137
Basic system configuration
138
Managing Ethernet connection

The Ethernet devices have the prefix eth. The first detected Ethernet card is
assigned the name eth0, the second eth1 and so forth.

Every system also has a special network device called the lo, which
represents the loopback device with address 127.0.0.1.

You can view the basic settings of the network device by running the
ifconfig command. E.g.: ifconfig
ifconfig ethx

If the network interface is down, it will not be shown in ifconfig’s output.

Device can be brought up and down by an administrator using the ifup and
ifdown command.


E.g.: ifup ethx
ifdown ethx
system-config-network provides a graphical interface for managing all the
network settings.
139
Network configuration file

/etc/sysconfig/network-scripts/ifcfg-ethX

Complete list of options are in /usr/share/doc/initscripts-*/sysconfig.txt
Dynamic configuration
Static configuration
Device=ethX
HWADDR=0:2:8A:A6:30:45
BOOTPROTO=dhcp
ONBOOT=yes
Type=Ethernet
Device=ethX
HWADDR=0:2:8A:A6:30:45
IPADDR=192.168.0.13
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
ONBOOT=yes
Type=Ethernet
140
Network configuration file

DEVICE: specifies the device alias

HWADDR: Associates the configuration with a specific device, identified by
a MAC address.

BOOTPROTO: From where IP address should be retrieved. DHCP or
STATIC.

IPADDR and NETMASK: Basic IP settings. Only necessary when not using
DHCP.

GATEWAY: IP address of the system to send messages destined for hosts
on another network.

ONBOOT: whether to bring the device up automatically on boot. yes or no.
Default no.

USERCTL: whether to allow non-root users to bring this device up and
down. yes or no, default no.

TYPE: specifies the type of network interface, Ethernet or Wireless. Setting
to Wireless would cause supplementary configuration scripts to run and look
for wifi-specific settings.
141
Global Network Settings

Global network settings are stored in

/etc/sysconfig/network

NETWORKING=yes

HOSTNAME=server1.example.com

GATEWAY=192.168.2.254

NETWORKING: whether to enable networking.

GATEWAY: The IP address of the system to send messages destined for
hosts on another network.

HOSTNAME: The system’s hostname. If DHCP is configured to provide
HOSTNAME, it is probably not necessary to define this.

If you do not define this and your system is not using DHCP then it will ask
DNS what name is associated with your IP address and use that.

IF DNS does not have a name associated with your IP, your system will be
assigned the name localhost.localdomain.
142
Network configuration files –
DNS configuration

DNS, allows name to be assigned to and used in place of IP addresses.

Local DNS configuration is performed using the /etc/resolv.conf.

There are generally two settings in /etc/resolv.conf:


search example.com cracker.org
specifies domains that should be tried when an incomplete DNS name is given to
a command.

nameserver 192.168.1.250
specifies that IP address of a DNS server your system should use.
If I run the command ping server1, the system will first attempt to resolve the
name server server1.example.com, if fails then tries the name server
server1.cracker.org.
143
Printer in Linux

Printers may be local or networked.

Supported Printer connections:



Local (parallel, serial or usb)

Unix/Linux Printer server

Windows Print Server

Netware print sever

HP JetDirect
Printer can be configured by:

system-config-printer

System > Administration > Printing

localhost:631/
Configuration file:

/etc/cups/printers.conf

/etc/cups/cupsd.conf
144
Printer in Linux

lpr sends a job to the queue to be printed.

lpr –P accounting -#5 report.ps

-P <Printer name>

-#5 specifies number of copies

lpq views the contents of the queue.


lpq –P accounting
lprm removes a job from the queue.

lprm –P accounting 916

Where 916 is the job id, that can be checked using lpq command

Command line management tool: lpadmin
145
Utilities



evince :
views PDF documents
lpstat command: Shows print cups status information

–d shows the current default destination.

–p shows the printers and whether or not they are enabled for printing.

–l Shows a long listing of printers, classes, or jobs.
ps2pdf : converts Postscript to pdf
146
Investigating and Managing Processes
147
What is a Process?

A process in an executing program with several components and properties.

A process is a shell command or a program in execution.

When you log in, a process is created. This process is executing the shell

When you execute a shell command, a new process is created. When the
command terminates, the process dies.

A single user can have many processes executing at the same time
148
ps command

The command ps is used to determine the status of active processes.
The command returns the process id (PID) number and other
information such as the amount of CPU time the process has used
(TIME) and the command which invoked the process (CMD).

Options may be combined.

a
includes processes on all terminal

x
includes processes not attached to terminal

u vikas
prints process owner information

o property1, property2 …. Where property is pid, comm, %cpu, %mem,
state, tty, euser, ruser

E.g.:
ps axo pid,%cpu,comm
149
Process State

Every process has a state property, which describes whether the process is
actively using the cpu, in memory but not doing anything (sleep), waiting for
a resource to become available (uninterruptable sleep) or terminated, but
not flushed from the process list (Zombie).

Uninterruptable sleep: Process is sleeping and can not be woken up until an
event occurs. It can not be woken up by a signal. Typically, the result of an
I/O operation, such as a failed network connection (for NFS hard mounts).

Just before a process dies, it sends a signal to its parent and waits for an
acknowledgement before terminating. Even if the parent process does not
immediately acknowledge this signal, all resources except for the process
identity number (PID) are released. These are called ZOMBIE
PROCESSES and are cleared from the system during the next system
reboot and do not adversely affect system performance.
150
Finding Processes

Lookup for processes

pgrep –U root

pgrep –G student

-U
Only match processes whose real user id is listed.

-G
only match processes whose real groupid is listed.

Find a process ID of a running program.

pidof gedit

pgrep gedit
151
signals

Signals are simple messages that can be communicated to process with
commands like kill.

Signal 15, TERM (default)
-
Terminate cleanly

Signal 9, KILL
-
Terminate immediately

Signal 1, HUP
-
Re-read configuration files

Most services are programmed to reload their configuration when they
received a HUP.
152
Sending Signals to Processes

Kill can send many signals, but processes only respond to the signals they
have been programmed to recognize.

kill 3428

kill -15 3428

kill –TERM 3428

User pidof gedit or pgrep gedit can be used to know the process id of the
process, gedit in this case.

killall – kill process by name

killall gedit
153
Scheduling priority

Scheduling priority determines access to the CPU.

Values ranges from -20(highest) to 19(lowest), default is 0.

nice - run a program with modified scheduling priority.

nice -n 15 myprog

Where –n specifies the priority number.

non-privileged users may not set niceness value to less than zero.
154
Scheduling priority

renice - alter priority of running processes

only the superuser is permitted to raise the priority of currently running
process.

Non super-users can not increase scheduling priorities of their own
processes, even if they were the ones that decreased the priorities in the
first place.

can be used to modify the priority of all of the processes of a particular user.


renice -15 -u joe
Can be used to modify the priority of a process

renice -15 –p pid
155
Process Management Tools

CLI:
top command

GUI:
gnome-system-monitor

Capability:

Display real-time process information

Allow sorting, killing and renicing.
156
Job Control

When a process is started from a terminal's command line, it is normally
running in the foreground.

When a command is entered, the shell will not process further input until the
process is completed and the shell prompt is redisplayed.

The typeahead buffer allows you to type other commands, but they will not
be processed until the pending process completes, or “returns”.

running a command in the background allows another process to run
concurrently on the same terminal.

A background process is still the child of the processes that spawned it.

the parent process, however, does not wait for the child process to
terminate before continuing.

When a process is started in the background, a new bash “sub-shell” is
created.
157
Job Control

Both find and ps now have the same PPID, the pid of the shell. the shell has
spawned two processes, one running in the foreground, and the other in the
background.

[root@server1 ~]# find / -name abc.txt &

[1] 3600

[root@server1 ~]# ps -f

UID PID PPID C STIME TTY TIME CMD

root 3314 3302 0 16:46 pts/2 00:00:00 bash

root 3600 3314 3 17:36 pts/2 00:00:00 find / -name abc.txt

root 3601 3314 0 17:36 pts/2 00:00:00 ps -f
158
Job Control

You can run as many jobs in the background as the system load permits.

Make sure that both standard output and standard error are redirected
suitable, using /dev/null, if necessary.

shell dies on logout, but not its child.

Kernel reassign the PPID of the find process to the process that has a PID
1.

This is the system process init, which is the parent of all shells.

when the user logs out, init takes over the parentage of the find process.
159
Suspending a Process

Below command check the process “find”.

ps -x | grep find

Foreground jobs can be suspended: temporarily stopped, without being
killed, using the <ctrl-z> keystroke.

once a process is suspended, it can be resumed in the background, using
the bg command, or resumed in the foreground, using the fg command.

job numbers are referenced with %.

Syntax

fg [%job_number]

bg [%job_number]
160
Listing Background and Suspended Jobs

Jobs displays all process running in the background or that are suspended.

the number in brackets is a job number, used to kill jobs or bring them back
to the foreground.

[root@server1 ~]# jobs

[1]+ Stopped find / -name abc.txt
161
Scheduling a process to execute later

at time-executes commands at a specified time.

[root@server1 ~]# at 11:56 am
at> find / -name abc.txt >>findresult.txt
at> <EOT>
job 3 at 2005-10-05 11:56
[root@server1 ~]#
#press <ctrl-d>

at –l
list

at –c jobnum
show details

at –d jobnum
Delete the job having job number jobnum
162
crontab

cron file settings are:

min

min
: 0-59

hour
: 0-23

day_of_month
: 1-31

month
: 1-12 or Jan, Feb,

day_of_week
: 0-7

*

Range are separated by e.g. 8-11 hours

Non-continuous range are separated by , e.g. 8,9,11 hours

Mixed range are as e.g.: 0-4,8-12 hours

0-23/2 at hour position means every other hour i.e. 0,2,4,6,8,10,12,14,16,18

*/2 at hour position means every two hour
hour
:
day_of_month
month
day_of_week
command
;0 means 12AM
;not case sensitive
;0 & 7-Sunday, 1-Monday, not case sensitive
every
163
crontab

If there is a mismatch as below: 30 4 1,15 * 5

Will run command at 4:30AM on the 1st and 15th of each month, plus every
Friday.

Pound-sign (#) are comments

Job files created by each user is stored in /var/spool/cron which is not
accessible by non privileged users.

E.g.:

* *
*
*
*
touch > /root/hello.txt
164
crontab

Cron job of user sunil can be Created/Edit cron by root using -e switch:

crontab -u sunil -e
;Creating/Edit cron for user sunil by root

crontab -e
;Creating/Edit cron by user himself

View cron jobs: Cron jobs can be viewed by using l switch as:

crontab -lu sunil
;View cron jobs of user sunil by root

crontab -l
;View cron jobs for himself

To remove crontab: cron jobs can be removed using r switch as:

crontab -ru sunil
;remove crontab job of user sunil by root

crontab -r
;To remove crontab job by user himself

Job files created by each user is stored in /var/spool/cron which is not
accessible by non privileged users
165
System crontab files

/etc/crontab is master crontab file which runs executables in

/etc/cron.hourly

/etc/cron.daily

/etc/cron.weekly

/etc/cron.monthly

/etc/crontab and /etc/cron.d/ are different from user crontabs.

Here sixth field is a username which will be used to execute the command in
the seventh field.
166
cron access control

If neither /etc/cron.allow nor /etc/cron.deny exists only root is allowed to
install new crontab files.

If cron.allow does not exist, all users listed in cron.deny are not allowed to
use cron.

If the file cron.allow exists, only users listed in it are allowed to use cron, and
the cron.deny file is ignored.

The format of both access control files is one username on each line.
Whitespace is not permitted in either file.

The cron daemon (crond) does not have to be restarted if the access control
files are modified.

The access control files are read each time a user tries to add or delete a
cron task.

Note that denying a user through the user of the above files does not
disable their installed crontab.
167
The anacron system

The anacron run cron jobs that did not run when the computer is down

Configuration file: /etc/anacrontab

Field 1: if the job has not been run in this many days

Field 2: wait this number of minutes after reboot and then run it.

Field 3: job identifier

Field 4: The job to run.
168
Grouping Command


Suppose you want to maintain a count of the number of users logged on,
along with a time /date stamp, in the log file.

date >> logfile

who | wc –l >> logfile
This can also be done as:


(date; who | wc –l) >> logfile
Command inside parentheses are run in their own instance of bash, called a
SUBSHELL. The output of all commands run inside a subshell are sent to
the subshell’s STDOUT and STDERR, making it possible to send multiple
programs through the same pipe.
169
Configuring the Bash shell
170
Environment Variables

Bash variables are local to a single shell by default



Set with VARIABLE=VALUE
Environment variables are inherited by child shells.

HI=“Hello, pleased to meet you.”

echo $HI
The set, env, and echo commands can be used to display all variables,
environment variables, and a single variable value respectively.

set | less

env | less

echo $HOME
171
Common Local Variables

HISTORYFILE
specifies the file in which history commands are stored on logout.

HISTSIZE
specifies the number of history commands to keep while operating
interactively.

HISTFILESIZE
determines how many commands to be saved in the history file on logout.

COLUMNS
sets the width of the terminal

PS1
sets the prompt
-
172
Changing prompt PS1

can be changed each time it is displayed by using special escaped
sequences.

\d
the date

\h
short hostname

\t
the current time

\u
user name

\w
the current working directory

\!
The history number of the current command

\$
show $ if you are a non-privileged user and a # if you are a
privileged user

e.g.:


PS1=‘\u@\h:\w ,\!>\$’
default PS1 variable has the value:

PS1=‘[\u@\h \w]\$’
173
Aliases

aliases are shortcut names for longer commands.

alias dir='ls –l’

use alias by itself to see all set aliases.

use alias followed by an alias name to see alias value.


alias dir
To remove alias use unalias command as

unalias dir
174
Preventing expansion

Backslash (\) makes the next character literal

echo “your cost: \$5.00”

E.g.: To find a final with name foo*

Avoid using echo \*\*\* SALE \*\*\* Use

: find / -name foo\*
echo '*** SALE ***' or echo “*** SALE ***”

Using as last character on line to “continue command on next line”

e.g.:

echo “this long sample string \

will be echoed back as one long line.”
175
Preventing expansion

Quoting prevents expansion:

Single quotes ‘ inhibit all expansion
echo 'The current date is `date`'
The current date is `date`

Double quotes “ inhibit all expansion except


$
variable expansion

`
command substitution

\
single character inhibition

!
History substitution
E.g.:

echo “The current date is `date`”

The current date is Sat Apr 27 17:45:25 EDT 2000
176
Login vs no-login shells

Startup is configured differently for login and non-login shells:

Login shells are:


Any shell created at login (including X login)

su -
Non-login shells are:

su

Graphical terminal

Executed scripts

Any other bash instances
177
Script executed at login time

/etc/motd

/etc/profile
can be used to store message of the day.

/etc/profile.d/

~/.bash_profile

~/.bashrc

/etc/bashrc
178
script executed in no-login shell

~/.bashrc

/etc/bashrc

/etc/profile.d/
179
script executed during logout

~/.bash_logout

Run when a login shell exits

Used for

Creating automatic backups

Cleaning out temporary files.
180
profile

Stored in /etc/profile (global) and ~/.bash_profile

The profile shell script is the first startup script run when a login shell is
started. It only runs for login shells, no-login shells do not invoke this script.

The profile set series of variable including PATH, USER, LOGNAME, MAIL,
HOSTNAME, HISTSIZE, and INPUTRC.

PATH variable is set in the /etc/profile script and is then modified in
~/.bash_profile script.
181
bashrc

Stored in /etc/bashrc (global) and ~/.bashrc (user)

umask and PS1 for privileged and unprivileged user is stored in /etc/bashrc

Aliases are stored in ~/.bashrc e.g.: aliasing for cp –i is stored in ~/.bashrc.

Any changes made in bashrc will be available in new terminal, but not in the
terminal already opened. However, the original terminal where you made
those changes still has the old settings. From the original terminal, run one
of the following:

. /etc/bashrc

or

source /etc/bashrc
182
Finding and Processing Files
183
locate

Find files by name

Locate queries a pre-built database of files looking for files that corresponds
to search criteria.

The database must be generated by an administrator running the updatedb
command.

Can also be automated by an administrator enabling the DAILY_UPDATE
option in /etc/updatedb.conf.


-i performs a case-insensitive search

-n x
shows only the first x matches
E.g.

locate –in 5 passwd
184
find

find [directory . . .] [criteria . . .]

find - search for files in a directory hierarchy.

searches directory trees in real-time.

Can be configured to execute commands when file is found.

find file ending with name .png starting from / root.


Case-insensitive search for file with name ending with .png


find –iname “*.png”
Search file in /etc that contains *.png in their name


find / –name “*.png”
find /etc –iname *.png
Search all the file owned by user joe and the group joe

find /home –user joe –group joe
185
find and Logical operator

-or
-o

-not
!

-and
-a

find file having owner joe but which does not belong to group joe


find file having owner either joe or sunil


find –user joe –or –user sunil
Find file having owner joe and group admin


find –user joe –not –group joe
find –user joe –and –group admin
Find files having user other than joe or jane

find –not \(– user joe –or –user jane \)
186
find and permissions

-perm mode, -perm +mode, -perm –mode

Find file having permission 755


Find file on which everyone can write


find –perm +222
Find file on which anyone can write


find –perm 755
find –perm -222
Find file on which others can write

find –perm -002
187
find and size

Size can be specified in k for KB, G for GB etc.

Find file of exactly size 10M


Find file of size over 10M


find –size 10M
find –size +10M
Find file of size less than 10M

find –size -10M
188
find and date

ctime -- In UNIX, it is not possible to tell the actual creation time of a file.
The ctime--change time--is the time when changes were made to the file or
directory's inode (owner, permissions, etc.). The ctime is also updated when
the contents of a file change. It is needed by the dump command to
determine if the file needs to be backed up. You can view the ctime with the
ls -lc command.

atime -- The atime--access time--is the time when the data of a file was last
accessed. Displaying the contents of a file or executing a shell script will
update a file's atime, for example. You can view the atime with the ls -lu
command.

mtime -- The mtime--modify time--is the time when the actual contents of a
file was last modified. This is the time displayed in a long directoring listing
(ls -l).
189
find and date

In Linux, the stat command will show these three times.

Examples

$ cat file

file's atime is updated.

$ chmod g+w file

file's ctime is updated.

$ echo "File contents" > file

file's atime, ctime and mtime are updated.
190
find and date

[root@ns1 ~]# stat install.log.syslog

File: `install.log.syslog'

Size: 3713
Blocks: 16
IO Block: 4096 regular file

Device: 302h/770d
Inode: 1011843

Access: (0644/-rw-r--r--) Uid: (

Access: 2010-01-28 17:35:36.000000000 +0530

Modify: 2010-01-21 09:01:30.000000000 +0530

Change: 2010-01-21 09:01:45.000000000 +0530
0/
Links: 1
root) Gid: (
0/
root)
191
find and date

We will change the permission of the file, and will see that there is a
change in ctime.

[root@ns1 ~]# chmod g+w install.log.syslog

[root@ns1 ~]# stat install.log.syslog

File: `install.log.syslog'

Size: 3713
Blocks: 16
IO Block: 4096 regular file

Device: 302h/770d
Inode: 1011843

Access: (0664/-rw-rw-r--) Uid: (

Access: 2010-01-28 17:35:36.000000000 +0530

Modify: 2010-01-21 09:01:30.000000000 +0530

Change: 2010-01-28 18:20:46.000000000 +0530
0/
Links: 1
root) Gid: (
0/
root)
192
find and date

now we will cat the file and will see that there is a change in atime.

[root@ns1 ~]# cat install.log.syslog

[root@ns1 ~]# stat install.log.syslog

File: `install.log.syslog'

Size: 3713
Blocks: 16
IO Block: 4096 regular file

Device: 302h/770d
Inode: 1011843

Access: (0664/-rw-rw-r--) Uid: (

Access: 2010-01-28 18:21:57.000000000 +0530

Modify: 2010-01-21 09:01:30.000000000 +0530

Change: 2010-01-28 18:20:46.000000000 +0530
0/
Links: 1
root) Gid: (
0/
root)
193
find and date

now we will edit the file and will save it and will see change in access,
modify and change time.

[root@ns1 ~]# stat install.log.syslog

File: `install.log.syslog'

Size: 3713
Blocks: 16
IO Block: 4096 regular file

Device: 302h/770d
Inode: 1011843

Access: (0664/-rw-rw-r--) Uid: (

Access: 2010-01-28 18:23:32.000000000 +0530

Modify: 2010-01-28 18:23:31.000000000 +0530

Change: 2010-01-28 18:23:31.000000000 +0530
0/
Links: 1
root) Gid: (
0/
root)
194
find and date

-mtime
file data was last modified.

-ctime
when file data or metadata last changed.

-atime
date of the last time the file was read

Find files whose last accessed time stamp is exactly five days ago.


Find files whose last accessed time stamp is more than five days ago.


find / -atime +5
Find file whose last accessed time stamp is less than five days ago.


find / -atime 5
find / -atime -5
stat filename can be used to check file’s mtime, ctime and atime.
195
find and executing command

find file having size more than 100M and prompt to move file to
/tmp/largefiles

find –size +100M –ok mv {} /tmp/largefiles/ \;

-ok ask the user for confirmation before executing.



Find file *.conf and copy them with changed extention of .orig

find –name “*.conf” –exec cp {} {}.orig \;

-exec execute the command without asking.
find files with others having write permission and change to deny write.


find ~ -perm -002 –exec chmod o-w {} \;
Display directories in /home directory

find /home –type d –ls

-type specified what type and d represents directory.

-exec and –ok must end with \;
196
find and xargs

Used when you want to execute a command on

find . –type l –print | xargs rm

Remove all the symbolic links in your current directory.
197
Network Clients
198
Links and firefox

links a non-GUI web brower, provided by elinks rpm

Firefox is a lightweight, feature-rich web broweser
199
wget

Retrieves file via HTTP or FTP

Useful for shell scripts

Download files, retry 50 times and wait for 30 second before each retry.


wget --tries=50 --wait=30 ftp://ftp.site.com/files
Mirror a full or partial website, you can limit number of levels of recursive:

wget --recursive --level=1 --convert-links http://www.site.com/

--convert-links

After the download is complete, convert the links in the document to make
them suitable for local viewing.
200
Email and messaging


Email client

Evolution

Thunderbird

Mutt
Messanger

pidgin
201
FTP client

CLI – lftp (lftp-<version>.rpm)

lftp ftp.example.com

lftp –u joe ftp.example.com

GUI – gFTP (gftp-<version>.rpm)

Applications > internet > gFTP
202
rsync


Efficiently copies file to or from remote systems. Transfer just the
differences between two sets of files.

-r
act on subdirectories recursively without preserving permission

-a
act on subdirectories recursively with preserving permission

--progress
print progress bar

--partial
continue partially download files
rsync *.conf 192.168.1.20:/home/joe
203
Network Diagnostic Tools

ping
used to detect the communication with another system.

traceroute
displays the computers through which a packet pass to reach another
computer.

host

Performs hostname to IP address transitions, as well as the reverse.

dig
performs a service similar to host in greater detail.
204
Network Diagnostic Tools

netstat
provides a number of network statistics.

gnome-nettool
a graphical frontend for the tools listed above.
205
The Linux Filesystem in-depth
206
inodes

ext2 and ext3 file systems keep a list of files in the file system in a table
called an inode table.

Individual entry in the inode table is called an inode.

Every file and directory has a inode number.

Directories have inode numbers associated with files.

Inode is referenced by its number, the inode number, which is unique within
a file system.
207
inodes


Data stored in the inode is:

The file type

File permissions

Link count: the number of file names associated with the inode number.

User id number of the file owner.

The group id number of the associated group.

Time stamps, including, last access, last modification and inode change
time.

Location of the data on the hard disk.
Display the inode number:

ls -il
208
cp and inodes

When a file is copied to a new name in the same directory, the cp command
allocates a free inode number, placing a new entry in the inode table.

[root@server1 ~]# ls -il abc.txt

589038 -rw-r--r-- 1 root root 1756 Oct 7 2005 abc.txt

[root@server1 ~]# cp abc.txt penguin.txt

[root@server1 ~]# ls -il penguin.txt

589036 -rw-r--r-- 1 root root 1756 Oct 7 06:15 penguin.txt
209
mv and inodes


When a file is moved, the underling file, either as inode entry or as data on
the hard disk does not move. What moves is the entry in a directory. e.g.:

[root@lclient1 ~]# ls -il tux

524890 -r-xr-xr-x

[root@lclient1 ~]# mv tux dump/

[root@lclient1 ~]# ls -il dump/

524890 -r-xr-xr-x
1 root
1 root
root
root
24004820 Jan 1 02:31 tux
24004820 Jan 1 02:31 tux
Renaming a file using mv command will not change its inode.
210
rm and inodes

Decrements the link count, thus freeing the inode number to be reused.

The block locations that the file was using are placed on the free list.

Removes the directory entry.

Data is not actually removed, but will be overwritten when the data blocks
are used by another file.
211
Hard links

A hard link is a path name that references an inode.

The individual file is referenced by its inode number, the file name is merely
for human convenience.

Because name of a file is separate from an inode (it is stored in a directory,
not in the inode), it is possible to have multiple file names pointing to the
same inode number.

To create an additional hard link to an existing file, use the ln command:

[root@lclient1 dir1]# ln 2.rar 3.rar

[root@lclient1 dir1]# ls -il

total 46944

524890 -r-xr-xr-x
2 root
root
24004820 Jan 1 02:31 2.rar

524890 -r-xr-xr-x
2 root
root
24004820 Jan 1 02:31 3.rar
212
Hard links


Hard Link

Two files have the exact same node.

There is only one underlying file, but there are two entry points.

The link count has been incremented to two, because two path names
point to the same file.
Restrictions :

The two file names must be on the same filesystem, because they share
an inode number and an inode table is unique to a file system.

It is not possible to use the ln command to create hard links to
directories.
213
Soft(Symbolic) links

A symbolic link is a file that points to another file.

Removing a symbolic link removes the actual link itself, not the underlying
file.

To create a symbolic link, use the ln command with the -s option:

ln –s /etc/passwd password

ls –il password /etc/passwd

Note that as a symbolic link is a separate file from the original, the original
file and the link will have different inode number.

Permissions set on the symbolic link are irrelevant. The size of the symbolic
link count to the number of character in the path to which the symbolic link is
pointing to.
214
Seven Fundamental Filetypes
ls –l symbol
File Type
-
Regular file
d
Directory
l
Symbolic Link
b
Block Special file
c
Character special file
p
Named pipe
s
socket
215
Seven Fundamental Filetypes

c character special file: Files referencing hardware are not regular files; they
are one of two type of special files. Character special files are used to
communicate with hardware one character at a time. E.g. /dev/pts/1 terminal

b block special file: used to communicate with hardware a block of data at a
time: 512bytes, 1024 bytes, 2048 bytes. /dev/hda /dev/hda1 /dev/hda2

Generally, block and character special files are located in the /dev/ directory.

p named pipe: a file that passes data between processes. E.g. /dev/initctl

s socket: a stylized mechanism for inter-process communications.

find / -type s | xargs ls –l

file /dev/hda
216
Checking Free space



df – report disk space usage

Reports total kilobytes, kilobytes used, kilobytes free per file system

-h display sizes in easier to read units.
du – reports file space usage

Reports kilobytes used per directory

Includes subtotals for each subdirectory

-s option only reports single directory summary.

-h display sizes in easier to read units.
Applications > System Tools > Disk Usage Analyzer or baobab reports disk
space usage graphically.
217
tar command

The `tar' program is used to create and manipulate `tar' archives.

Archiving files is the process of incorporating a copy of files into a single
archive file.

tar archives are usually created with .tar filename extension.

-c
:
create new archive

-x
:
Extracts members from the archive into the file system.

-t
:
lists the members in an archive.

-v
:
produce verbose messages

-f archivename
: name of file archive
tar –cvf /tmp/etc.tar /etc

Create an archive of the /etc directory as:

List the contents of the archive: tar –tf /tmp/etc.tar

Extract tar archive in current directory: tar –xvf /tmp/etc.tar
218
tar command



tar compress the archive using gzip compression tool when –z option is
used

tar –czvf /tmp/etc.tar.gz /etc

tar –tzf /tmp/etc.tar.gz

tar –xzvf /tmp/etc.tar.gz
tar compress the archive using bzip2 compression tool when –j option is
used

tar –cjvf /tmp/etc.tar.bz2 /etc

tar –tjf /tmp/etc.tar.bz2

tar –xjvf /tmp/etc.tar.bz2
When compressed with –z the archive file suffixes .tar.gz or .tgz and when
compresed with –j the archive file suffixes .tar.bz2
219
Other archive tools

Other CLI archive tools


zip and unzip
GUI archive tools

file-roller
220
Thank You !!!
221
Thank You !!!