Вы находитесь на странице: 1из 264

RH033

Redhat Enterprise Linux Essentials

Unit 1

Redhat Linux Essentials Overview

Redhat Enterprise Linux

Certified with Leading OEM and ISV Products

ISV Independent Software Vendor OEM Original Equipment Manufacturer


Application Binary Interface Application Programming Interface ELF Executable and Linking Format LSB Linux Standards Base BEA BEA System Inc, San Jose, California. Think Liquid HPC High Performance Computing

ABI/API Compatibility

Unix Flavors

UNIX from AT&T, at Bell Labs, in 1969. AIX from IBM HP/UX from Hewlett-Packard SunOS (Later Solaris) from Sun IRIX from SGI (Silicon Graphics)

Unix Principles

Everything is a file.

Including hardware

Configuration data stored in text Small, single-purpose programs Avoid Captive User Interfaces Ability to chain programs together to perform complex tasks.
5

GNU Project / FSF

GNU Project started in 1984


Goal : Create Free UNIX clone By, 1990, Nearly all required userspace application created. gcc, emacs etc. Non-profit Organization that manages the GNU Project Four Freedoms

Free Software Foundation

http://www.gnu.org/philosophy/free-sw.html
6

GPL GNU General Public License

Primary license for Open Source Software Encourages free software All enhancements and changes to GPL-software must also be GPL Often called CopyLeft

All rights Reversed

http://www.gnu.org/copyleft/gpl.html X Window System, has its own T&C. BSD code need to be abide the terms of Berkeley Software Distribution. Visit http://www.bsd.com
7

Linux Origins

Linux Torvalds

Finish College Student in 1991 Created Linux Kernel

When Linux Kernel Combined with GNU


Applications, complete free UNIX-like OS possible.

Why Linux?

Fresh Implementation of UNIX APIs. Open Source Development model. Supports wide variety of hardware. Supports many networking protocols and configurations. Fully supported. Linux is a UNIX-like OS. Multi-user and Multi-tasking. Wide hardware support. Checklist. Fully Supported.
9

Redhat Enterprise Linux

A distribution of Linux

gdm is the default display manager for RHEL.

Custom version of a recent Linux Kernel. Utilities and applications. Installation and configuration software Support available. RHEL includes installation and configuration software, a patched and rigorously tested recent version of the Linux Kernel, and thousands of utilities and applications.

10

Recommended Hardware Specifications for RHEL

Pentium Pro or better with 256 MB RAM

Or

64-bit Intel/AMD with 512 MB RAM 2-6 GB Disk Space Bootable CD Other processor architectures supported

Itanium 2, IBM Power, IBM Mainframe.


11

Virtual Consoles

Virtual Consoles are available through CTRL ALT F[1-6] If X is running, it is available as CTRL ALT F7 The virtual consoles enable a user to have multiple logins even when not using an X Window system. You can scroll at the virtual consoles by using SHIFTPAGEUP and SHIFT-PAGEDOWN. The scroll buffer is stored in video memory, so if you are running a graphical program such as X, the scroll buffer is lost.

12

The Xorg GUI Framework

The goal of X.Org include a faster and more open development model than Xfree86, support for a wide variety of video cards and input devices and the development of a highly modular and flexible graphical framework for Unix and Linux. http://xorg.freedesktop.org

http://www.x.org/wiki X Client activity that spawns an X server event informing the web browser to send an HTTP request to the links target ( or anchor). You do not really see the X server, but X clients. X provides the data I/O infrastructure for X clients, like a human nervous system, it sends messages when touched by client activity. An X Client running on one system can display on any X server running on any operating system, if sufficient access is granted. For each managed display, this socket is /tmp/.X11-unix/X# where # is 0 to the greatest number of permitted connections.
13

Or

Xorg Graphical Environments

Collections of applications that provide a graphical working environment with a consistent look and feel. GNOME (GNU Network Object Model Environment) The default desktop environment. KDE ( K before L) Environment based on the Qt toolkit.
14

Starting Xorg

Nothing needed if system boots to a graphical login. Just authenticate. If system boots to a virtual console login, Xorg must be started manually.

Run startx to manually start Xorg. At least 6 but no more that 255 characters. Other criteria depends on particular OS.
15

Changing Password.

Unit 2

Running Commands and Getting Help

16

Getting Help

--help whatis -- with makewhatis man info /usr/share/doc/html/en/ Software source code Documentations
17

Running Commands

Commands have the following syntax:

Command [ options] [arguments]

Each item is separated by a space. Options modify the commands behavior

Word options usually preceded by --


Single-letter options usually preceded by -

Can be passed as -a b c or -abc

Arguments are filenames or other date needed by the commands.


18

The help option

Anything in straight braces ([]) is optional. Anything followed by represents an arbitrarylength list of that thing. If you see multiple options separated by pipes (|), it means you can use any one of them. Text in straight brackets(<>) represents variable date. So <filename> means insert the filename you wish to use here. Sometimes, such variables are simply written in all CAPS.

19

The man Command


The collection of all man pages on a system is called the Linux Manual. The Linux 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:

Manual Sections.

man and man k commands

1 2 3 4 5 6 7 8

User Commands System Calls Library Calls Special Files File Formats Games Miscellaneous Administrative Commands

20

The info command

info pages are structured like a website.


Each page is divided into nodes. Links to nodes are preceded by *.

info [<command>] If you run info with no arguments you will be presented with a list links to the top nodes of every available info page.
21

Navigating info Pages

While viewing an info page Navigate with arrows, pgUp, pgDown


Tab Enter n/p/u

node s[<text>] Search for text (default: last search) q Quit Viewing page

Move to next link Follow link the cursor is on View next/previous/up-one

If you prefer the navigation keys used by man, such as using /, n and N to search, you can start info with the vi-keys arguments.
22

Extended docs

/usr/share/doc http://www.redhat.com/docs

23

Miscellaneous

man f foo is the same as whatis foo. If your system has just been installed, neither man f nor whatis will work until an administrator runs makewhatis. ls lh

24

Unit 3

Browsing the File system

25

Linux File Hierarchy Concepts


Files and directories are organized into a single-rooted inverted tree structure. Filesystem begins at the root directory, represented by a lone / (Forward slash) character. Names are case-sensitive. Paths are delimited by /. A users path is a list of directories that are searched for commands typed at the command line.
26

Some Important Directories

The essential binaries necessary to boot and maintain the system reside in /bin for regular binaries and /sbin for system binaries. Non-essential binaries, such as graphical environments, web browsers, office tools and so forth, are installed in /usr/bin and /usr/sbin. On a newly installed system, there will also be /usr/local/bin and /usr/local/sbin directories, but they will be empty. CD-ROM is mounted under /media/cdrom folder. Filesystems that are on non-removable media but are not part of the RHEL, hierarchy are usually mounted under /mnt. Visit http://www.pathname.com/fhs

27

Other Important Directories


/etc -System Configuration Files /tmp -Temporary files /boot -Kernel and boot loader /var and /svr -Server data /proc and /sys -system Information The lib directories

/lib, /usr/lib, /usr/local/lib


28

On Folders

Once a day the system automatically deletes any files over seven days old in /tmp and its subdirectories. The /var directory contains regularlychanging system files such as logs, print spools and email spools. In the future, server data may be moved to the /srv directory so that /var only contains logs, spools and so forth. Each shell and system process has a current working directory (cwd).
29

File and Directory Names

Names may be up to 255 characters. All characters are valid, except the /.

It may be unwise to use certain special characters in file or directory names. Some characters should be protected with quotes when referencing them. Example: MAIL, Mail, mail and mAiL Again, possible but may not be wise.

Names are case sensitive.


To access a file with name contains special characters, enclose the filename in quotes.
30

Absolute Pathnames

Absolute pathnames begin with a slash (/) Complete road map to file location. Can be used anytime you wish to specify a file name. To change to your previous working directory

$ cd

A dash (-) represents your previous working directory. Its a handy shortcut to use to switch back and forth between two directories.
31

Copying Files and Directories


cp copy files and directories Usage:

More than one file may be copied at a time if the destination is a directory.

cp [options] file destination cp [options] file1 file2 dest

If final argument is an existing directory, a copy of the source files are placed in that directory with the same name as the source. Otherwise, the destination is interpreted as a file name, and a copy of the source file is created with that.
32

On Copying

When copying a single file to a destination, cp first checks to see if a directory exists with the destination name. If it does, a copy of the source file is placed there with its original name. If not, the destination is assumed to be a new file name, and a copy of the source file is made with the destination name.
33

Moving and Renaming Files and Directories

mv move and/or rename files and directories. Usage:

More than one file may be moved at a time if the destination is a directory.

mv [options] file destination

If the destination directory does not exit, the slash will cause the command to fail with an error message. If the destination directory does not exist (but the destination pathname is valid), the source directory will be moved to the destination directory with the new name.
34

mv [options] file1 file2 dest

Creating and Removing Files


rm remove files Usage:


touch create empty files or update file timestamps The r option tells rm to remove files recursively and thus it will delete directories and their contents. There is no way to undo the effects of rm, except to restore from a backup. rmdir will only remove empty directories. To remove a directory and its contents, use rm r.

rm [options] filenames -i interactive -r recursive -f force

35

Using Nautilus

Gnome graphical Filesystem browser Can run in Spatial or Browser mode Accessed via

Desktop icons

Typing CTRL-SHIFT-w closes all parent windows. Spatial or Browser Mode can be edited.
36

Home: Your home directory Computer: Root Filesystem, network resources and removable media. File Browser option on Application menu.

Moving and Copying in Nautilus

Drag and Drop

Left-button: Move on same filesystem, copy on different system. Ctrl-Left-button : Always Copy Ctrl-Left-button: Ask whether to copy, move or create symbolic link (alias) Right-click or rename, cut, copy or paste.
37

Context Menu

Determining File Content

Files can contain many types of data. Check file type with file before opening to determine appropriate command or application to use. Syntax

file [options] filename(s)

file prints its best guess of the type of data contained


in a file whose name is given as an argument. It bases its guess on a comparison of the contents of the file and the patterns and offsets in its reference file, /usr/share/magic.

38

Viewing an Entire Text File

Syntax:

-A Show all characters, including control characters and non-printing characters. -s Squeeze multiple adjacent blank lines into a single blank line. -b Number each (non-blank) line of output.
39

cat [options] [files..] Contents of the files are displayed sequentially with no break. Files display concatenated.

Viewing Text Page by Page

Syntax:

less [options] [filename]

Scroll with arrows/pgUp/pgDown Useful commands while viewing:


/text n
v

--search for text --next match -- open file in text editor.

less is the pager used by man


40

On less

Less navigation commands:


<space> <return> <Ctrl-d>
g /text n q b k <Ctrl-u> G ?text N v

move ahead one full screen move ahead one line move ahead (down) half a screen move to the top of the file. search forward for text repeat last search quit move back one full screen move back one line move back (up) half a screen move to the bottom of the file search backward for text repeat last search, but in opposite direction. open file in a text editor (vi by default)

41

less options

-c -r

-s
-e

-m

Clear before displaying. Screen is redrawn instead of scrolled between screens. Display raw control characters. (May cause display problems) squeeze multiple blank lines into a single blank line Exit the second time it reaches the end of the line. Verbose prompt, like more (by default it uses a colon).
42

Unit 4

The bash Shell

43

bash introduction

Bourne Again Shell Successor to sh, the original Unix shell Developed for the GNU Project The de factor standard Linux Shell Backward-compatible with Bourne shell (sh)- the original (Standard) UNIX shell

44

bash Heritage and Features

Bourne Again Shell (bash)

Implements many of the best features from earlier shells: sh, csh, ksh , tcsh Command line completion Command line editing Command line history Sophisticated prompt control

45

Command Line Shortcuts: File Globbing

Globbing is wildcard expansion:

The use of wildcards, or metacharacters, allows one pattern to expand to multiple filenames by a process called globbing. RHEL uses UTF-8 encoding of characters, which means that each capital letter comes directly after the corresponding lower-case letter. Use echo ?o* to find the effect of wild characters.
46

* - matches zero or more characters ? matches any single character [a-z] matches a range of characters [^a-z] matches all except the range.

Some topics: Tab, history, ~


The TAB Key history


Tilde(~)

!! to repeat last command !c to repeat last command that started with c !n to repeat a command by its number in history output !?abc to repeat last command that contains (as opposed to started with) abc !-n to repeat a command entered n commands back. User ^old^new to repeat the last command with old changed to new. May refer to your home directory ( ~/.bash_profile) May refer to another users home directory. (~gmustafa/.bashrc)

47

Command Line Expansion: Variable and String

Parameter/Variable: ($)

Substitute the value of a variable in a command line.

$ cd $HOME/public/html

To see a list of variables and their values, run the set command. Curly braces: { }

A string is created for every pattern inside the braces regardless if any file exists.

$ rm hello.{c,o}

Curly braces are useful for generating patterned strings.


48

Command Line Expansion: Command and Math

Command Output or $ ()

Substitute output from a command in a command line.


$ echo Hostname: `hostname` $ echo Hostname: $(hostname) Substitute result of arithmetic expression in a command line. $echo Area : $[$X * $Y] $echo Area : `expr $X\*$Y`
49

Arithmetic - $[]

Protecting form Expansion: Backslash

Backslash (\) is the escape character and makes the next character literal.

$echo Your Cost :\$5.00

Used as last character on line to Continue command on next line $find / -name foo* $find / -name foo\*
50

Protecting From Expansion: Quotes

Quotes prevents expansion

Single quotes () inhibit all expansion

$echo *** SALE ***` $ (dollar sign variable expansion (backquotes), command substitution \ (backslash), single character inhibition ! (exclamation point), history substitution

Double quotes() inhibit all expansion except :


Double quotes inhibit file name generation expansion, but not other types.
51

History Tricks

Use the up and down arrow keys to scroll through previous commands. Type <CTRL-R> to search for a command in command history.

To recall last argument from pervious command:


{reverse-i-search)`:

You can ignore repeated duplicate commands and repeated lines that only differ in pre-pended spaces by adding the following in your .bashrc

<ESC>, (the escape key followed by a period) <ALT -.> (hold down the alt key while pressing the period)

export HISTCONTROL=ignoreboth

52

Command Editing Tricks

line. <Ctrl-e> moves to the end of line. <Ctrl-u> deletes to beginning of line <Ctrl-k> deletes to end of line. <Ctrl-arrow> moves left or right by word. * Inherited from emacs.

<Ctrl-a>

moves to beginning of

53

Command Editing Tricks: Editing Modes

By default, bash uses emacs-style keybindings for command editing. Type set o vi to change keybinding to vi-style. Make vi the default by adding the command above to $HOME/.inputrc

54

gnome-terminal

Applications/System Tools/Terminal Graphical terminal emulator with support for maintaining multiple tabbed shells simultaneously. <Ctrl-Shift-t> Open a new tab <Ctrl-PgUp/PgDn> Next/Prev tab <Ctrl-Shift-c/v> Copy/Paste
55

Unit 5

Standard I/O and Pipes

56

Standard Input and Output

Linux provides three I/O channels to processes


Standard input keyboard is default Standard Output Terminal windows is default Standard Error Terminal window is default.

Error messages could be saved in a file with the normal output going to the monitor. 0,1,2 are called file descriptor.
57

Redirecting Input and Output

Standard Input, Output, and Error can be reconnected to alternate locations.

Shell redirection operators allow standard I/O channels to be redirected to/from a file. Pipes allow standard I/O channels to connected to the input or output of programs.

Although it is also possible to pipe standard error into a file using some fairly complex syntax, this is generally not done.
58

Redirecting and Piping


Redirecting > command >> command < command 2> command 2>> command

> file > file < file 2>file 2>>file

Directs standard output of command to file Appends standard output of command to file. command receives its input from file. Error messages from command are directed to file. Error messages from command are appended to file.

Piping

| command1 | command 2

Pipes the standard output of command1 into the standard input of command2.

59

Overwriting or Appending

If the target file of file redirection with > already exists, the existing file will be overwritten. To append data to an existing file, use >> to redirect instead of >. Redirect standard output with >

Example: redirect standard output to a file.

Find /etc name passwd > findresult.

Standard error is still displayed on the screen.


60

Redirecting Standard Error

Redirect Standard Error with 2>

Example: redirect standard error to a file:

find /etc name passwd 2> finderrors

Standard output is displayed on the screen. Redirecting further standard error, appending to the same file, with 2>>.
61

Redirecting Both Standard Output and Error

Redirection of Standard Output and Standard Error can be performed simultaneously.

find / -name passwd 2> errs > results

Each I/O Channel can be redirected to different files, or to the same file:

find / -name passwd > alloutput 2>&1 (or) find / -name passwd &> alloutput
62

Redirecting Input

Redirecting Standard Input with < Some commands only operate on Standard Input

tr doesnt accept filenames as argumentsit requires its input to be rediected from somewhere. tr A-Z a-z <.bash_profile

This command will translate the uppercase characters in .bash_profile to lowercase.


63

Using Pipes To Connect Processes

Pipes ( the | character) let you redirect output from one command to become the input to another command.

$ ls /usr/lib | less

Can create pipelines a powerful feature of Linux.

$ cut f1 d: passwd | sort r | less


64

On Pipes

Two of the basic tenets of UNIX philosophy are:

Make small programs that do one thing well.

And
Expect the output of every program to become the input to another, as yet unknown, program. And then Use pipes to do complex jobs on the fly. Any command that writes to standard output can be used on the left-hand side of a pipe. Any command that reads from standard input can be used on the right-hand side of a pipe. Multiple commands can be chained together with pipes.

65

Useful Pipe Targets

ls l | less ls l | mail s Files abc@def.com ls l | lpr cat files_to_delete.txt | xargs rm f tee

Lets you tee a pipe: redirect output to a file while still piping it to another program. $set | tee set.out | less In example, output from set is written to file set.out while also being piped to less.

Command | tee stage1.out | sort | tee stage2.out | uniq c | \ tee stage3.out | sort r | tee stage4.out | less

66

Unit 6

Users, Groups and Permissions

67

The Linux Security Model

Users and groups are used to control access to files and resources. User 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. Every process has a owner and group affiliation, and can only access the resources its owner or group can access.
68

Users

Every user of the system is assigned a unique User ID number( the uid). Users names and uids are stored in /etc/passwd Users are assigned a home directory and a program that is run when they log on (Usually a shell). User cannot read, write or execute each others files without permission.
69

Groups

Users are assigned to groups with unique group ID numbers (the gid). gids are stored in /etc/group. Each user is given their own private group.

All users in a group can share files that belong to the group. The primary group can be changed using the newgrp command.
70

They can also be added to other groups to gain additional access.

The root user

The root user: a special administrative account. Sometimes called the superuser. root has complete control over the system.

An ultimate capacity to damage the system.

You should not log in as root without a very good reason.

Normal (unprivileged) users potential to do a damage is limited.


71

Linux File Security

Every file and directory has permissions set that determine who can access it. Permission are set for:

The owner of the file (called the user, arguably a misnomer) The group members All others

Permissions that are set are called read, write, and execute permissions.
72

Permission Types

Four symbols are used when displaying permissions:

A file may be removed by anyone who has write permission to the directory in which the file resides regardless of the ownership or permissions on the file itself. The first character of the long listing is the file type.

permission to read a file or list a directorys content wpermission to write to a file or create of 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 r, w, or x)

73

Linux Process Security

When a process accesses a file the user and the group of the process are compared with the user and group of the file.

Every process runs as a under the authority of a particular user and with the authority of one or more groups, this is called the processs security context.
74

If the user matches, the user permission apply. If the group matches, but the user doesnt, the group permission apply. If neither match, the other permissions apply.

Changing PermissionsSymbolic Method

To change access modes:

chmod [-R] mode file


u,g or o (a) + or (=) r,w or x (s,t) ugo+r o-wx for user, group and other for grand or deny for read, write and execute Grant read access to all. Deny write and execute to others.

Where mode is:


Examples:

Multiple comma separated operations can be give in a single command


75

Changing permissionsNumeric Method

Uses a three-digit mode number.

Permissions are calculated by adding:


First digit specifies owners permissions. Second digit specifies group permissions. Third digit represents others permissions.
4(for read) 2(for write) 1(for execute)

Example:

chmod 640 myfile


76

Unit 7

vi and vim Editor Basics and Printing

77

Overview of vi and vim

vi: the visual editor, standard Linux and Unix editor vim: the vi improved editor, standard Red Hat editor On Redhat operating systems, the vi command invokes vim. Derived from earlier Unix editors

ed ex vi vim
78

Starting vi and vim


To start vim: vi filename If the file exists, the file is opened and the contents are displayed. If the file doesnt exit, vi creates it when the edits are saved for the first time. To use vi instead:

unalias vi or \vi
79

Starting vi with different options

vi -m myfile vi R myfile vi n myfile

vi r myfile
vi x myfile

file is non-modifiable file is only modifiable by using the exclamation point :w! do not use a swap file for backup (useful for floppies) recover data from a swap file after a crash encrypt file when saving, decrypt when editing

80

Three modes of vi and vim

Command Mode

Cursor movement Change, delete, yank, put, search

Insert mode

Type in new text Return to command mode with <ESC>


Configuring, exiting, saving Search and replace
81

ex mode

Cursor Movement

h left j down k up l right w word ahead b word back ( Sentence Back ) Sentence forward { Paragraph above } Paragraph below Arrow keys (, , etc) also work. A word is defined as a series of letters of the alphabet and numbers uninterrupted by white space or punctuation. If the cursor is on a punctuation character, the word is terminated by white space or a letter of the alphabet or number.
82

Entering Insert Mode

a i o

append after the cursor. insert before the cursor. open a line below.

A append to end of line. I insert at beginning of line. O Open a line above. Pattern such as these permeate the vi and vim commands.
83

Leaving Insert Mode: <Esc>

<Esc> Takes you from insert mode back to command mode Hint: When in trouble, press <Esc> and then press <Esc> again. By doing this, you can guarantee that, regardless of the mode you were in, you will be now in command mode.
84

Change, Delete and Yank


Line Letter Word Sentence ahead Sentence behind Paragraph ahead Paragraph behind
Change cc cl cw c) c( c{ c} Delete dd dl dw d) d( d{ d} Yank(Copy) yy yl yw y) y( y{ y}

A line is yanked into a buffer, presumably to be put (or pasted) back into the document at another location.
85

Put (Paste)

Use p or P to put (paste) copied or deleted data For line oriented data:

p puts the data below the current line P puts the data above the current line

For character oriented data:

p puts the data after the cursor. P puts the data before the cursor.

Jus as in vi and vim, we yank instead of copy, we put instead of paste; take data from a buffer and place it in the documents.

86

Undoing Changes

u U

undo most recent change undo all changes to the current line since the cursor landed on the line. <Ctrl-r> redo last undone change. The u will not undo a previous u; that is, it will not toggle a change, but rather undo several pervious changes. To undo all successive changes to the current line, use the U command. To redo a change undone by a u command, use the <Ctrl-r> command.

87

Searching for Text


/text ?text n N

search downwards for text Search upwards for text continue search in the same direction. Continue search in the opposite direction.

88

Command-Mode Tricks

dtc 5dd

x rc r

delete from cursor to the letter c (does not span lines) delete five lines( a number can precede any of the two character change, delete, yank or put commands). delete a character replace a character with c replace character-for-character until <Esc>
89

Saving and Exiting: ex mode


Exit Do not exit
save changes :wq :w Abandon changes :q or :q! :e!

Forcing Changes: Save changes Exit :wq! Do not Exit :w! Abandon changes, staying in the editor, through :e! and force a write through :w!. Finally, force a write through :w1!. Finally, force a write and quit with :wq!. The last command will forcibly write the file, but only quit if the forcible write was successful.
90

Printing in Linux

Printers may be local or networked. Print requests are sent to queues. Queued jobs are sent to the printer on a first come first server basis. Jobs may be canceled before or during printing. It support CUPS IPP (Common Unix Printing Subsystem), lpd( Linux printing daemon), windows, Netware and JetDirect printers.
91

Printing commands

lpr

Send a job to the queue to be printed

Accepts ASCII, PostScript, PDF, others.

lpq View the contens of the queue. lprm Remove a job from the queue. System V printing commands such as lp, lpstat and cancel are also supported. A user may only remove his own print jobs from the queue.
92

Printing Utilities

enscript Converts text to PostScript a2ps Converts text to PostScript ggv(GNOME GhostView) PostScript and PDF Viewer xpdf PDF viewer. ps2pdf PostScript to PDF converter. pdf2ps PDF to PostScript converter. mpage Prints multiple pages per sheet.

93

Section 2
Red Hat Enterprise Linux: A Deeper Look

94

Unit 8

The Linux Filesystem In-Depth

95

Partitions and Filesystems


Disk drives are divided into partitions. Partitions are formatted with filesystems, allowing user to store data.

A filesystem is a data structure written to the media that allows users to store and access files. Ext2 at 1993, ext3 has Journaling to improve filesystem data integrity. EA Extended Attributes ACLs POSIX Access Control Lists

Default filesystem: ext3, the Third Extended Linux Filesystem. Other common filesystems: Ext2 and msdos (typically used for floppies) Iso9660 (typically used for CDs)

96

Inodes

An inode table contains a list of all files in an ext2 or ext3 filesystem. An inode (index node) is an entry in the table, containing information about a file (the metadata), including:

An individual entry in the inode table is called an inode.


97

File type, permissions, link count, UID, GID The files size and various time stamps Pointers to the files data blocks on disk. Other metadata about the file

Directories

The computers reference for a file is the inode number. The human way to reference a file is by the file name. A directory is a mapping between the human name for the file and the computers inode number. When a filename is referenced by a command or application, Linux references the directory in which the file resides, determines the inode number associated with the file name, looks up the inode information in the inode table, and , if the user has permission, returns the contends of the file. The ls i command displays the inode number.
98

cp and inodes

The cp command:

Allocates a free inode number, placing a new entry in the inode table. Creates a directory entry, referencing the files human file name to the inode number. Copies data into the new file.

When a file is copied to a new name in the same directory, the directory and the inode table get a new entry.
99

mv and inodes

If the destination of the mv command is on the same filesystem as the source, the mv command:

Creates a new directory entry with the new file name Deletes the old directory entry with the old file name Has no impact on the inode table (except for a time stamp) or the location of data on the disk. No data is moved!
100

rm and inodes

The rm command:

Decrements the link count, thus freeing the inode number to be reused. Places data block on the free list. Removes the directory entry.

Data is not actually removed, but will be overwritten the data blocks are used by another file.
101

Symbolic (or Soft) Links

A symbolic link points to another file ls l displays the link name and the referenced file.

Syntax:

File type: l for symbolic link The content of a symbolic link is the name of the file that it references.

Removing a symbolic link removes the actual link itself, not the underlying file
102

ln -s filename [linkname]

About Symbolic Links


Symbolic link is a separate file from the original. The first character of along listing for a symlink is the letter l. Symlink has permission of 777. Permission of symlink are irrelevant; the permissions set on the file pointed to by the symlink control access rights. The size of the symlink is always the number of characters in the path name.
103

Hard Links

One physical file on the system. Each link reference the files inode. File is present in the filesystem as long as at least one link remains. Cannot span drives or partitions. Syntax:

A hard link is a path name that references an inode: that is , all files are hard linked at least once.
104

ln filename [linkname]

On Hard Links

The file names must be on the same filesystem: because they share an inode number and an inode table is unique to a file system, both must be on the same file system. It is not possible to use the ln command to create additional hard links to directories.
105

The Seven Fundamental Filetypes


d l b c p s

regular files directory symbolic link block special file character special file named pipe socket
106

Extra Filetypes

character special file:


Files referencing hardware are not regular files; they are one of the two types of special files. Character special files are used to communicate with hardware one character at a time.
Used to communicate with hardware a block of data at a time: 512 bytes, 1024 bytes, 2048 bytes: whatever is appropriate for that type of hardware. Run the following command to see a list of block and character special files:

block special file:

A file that passes data between processes. It stores no data itself, but passes between one process writing data into the named pipe and another process reading data from the named pipe. A named pipe can be created using the mknod command:

named pipe:

ls l /dev | less

A stylized mechanism for interprocess communication. It is extremely rare for a user or even a system administrator to explicitly create a socket.
107

socket:

mknod

mypipe p

Miscellaneous tools

df du mount umount eject mtools(mdir,mcopy)

108

Unit 9

Configuring the Bash Shell

109

Configuring the Bash Shell

The Shell is configured through a variety of mechanisms:


Local Variables Aliases and functions The set and shopt commands

The shell can also configure other commands or applications through environment variables.
110

Variables

A variable is a label that has a value. Used to configure the shell or other programs. Variables are resident in memory. Two types: local and environment Local Variables are used only by the shell. Environment variables are passed onto other commands. Display variables and values using: set to display all variables. env to display environment variables. 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
111

Configure the Shell: Local Variables

Data in Shell scripts and environment settings stored in variables. Conventionally all upper-case. Setting variable value:

$FAV_COLOR=blue

To retrieve variable value, use $ before the variable name


$echo $FAV_COLOR $blue

For a list of variables that configure the shell, see the Shell Variables section of the bash man page.
112

Common Local Variables


HISTFILESIZE Determines how many commands to be saved in the history file on logout. COLUMNS Sets the width of the terminal( xterm, gonme-terminal or kterm) LINES Sets the height of the terminal ( xterm, gonme-terminal or kterm) HISTFILE Specifies the file in which history commands are stored on logout HISTSIZE Specifies the number of history commands to keep while operating interactively.
113

The PS1 Local Variable


PS1 sets the prompt Uses escape sequences to insert variable information in the prompt.

For a complete list of these prompting escape sequences, see the PROMPTING section of the bash man page.
114

\d the date \h short hostname (not the FQDN) \t the current time \u user name (useful if you have multiple accounts) \w the current working directory. \! The history number of the current command. \$ show $ if you are non-privileged user and a # if you are a privileged user, useful if you sometimes become superuser.

Aliases

Aliases let you create shortcuts to commands $ alias dir=ls laF Use alias by itself to see all set aliases. Use alias follow by an alias name to see alias value. $ alias dir $alias dir=ls laF In this case, if you ever want to use the rm command itself, instead of your alias, you can precede the command with a blackslash. \rm r Junk
115

Other Shell Configuration Methods

Less common, but powerful commands to configure elements of the shell.


set b

set shopt

set u set o noclobber set o vi

syntax. For a complete list of set values, see the set command under the SHELL BUILTIN COMMANDS section of the bash man page. Also in this section is a list of items configurable through the shopt command.

report termination of background commands immediately, rather than waiting for next prompt. unset variables generate an error. do not clobber with > and >& operators. use vi syntax on bash command line instead of default emacs

116

Configuring Commands: Environment Variables


Shell variables exit only in current shell instance. Environment variables passed to subshells. Shell variables can be exported into environment

To blank the value of an environment variable, use the unset built-in command. $unset EDITOR
117

EDITOR=/usr/bin/vim; export EDITOR Export EDITOR=/usr/bin/pico

Common Environment Variables


HOME LANG PWD EDITOR

LESS

SHELL USER DISPLAY VISUAL

The less command has many options; to force a set of options to always be used, set the LESS variable. For example: LESS=-emqs

Path to users home directory Identification of default language programs should use; example: en_US.UTF-8 for U.S. English. Users current working directory. Default editor programs should invoke for text editing. Options to pass to the less command. path to loging shell Username of user X Display Name Name of visual editor

118

The TERM Environment Variable


TERM Environment variable setting the terminal type. reset Command (not variable) used to reset a terminal should the screen become corrupted. If a terminal is improperly set, the computer will not be able to properly display data. For more complex adjustments to your terminal settings, set the stty command. The stty command can perform a number of terminal settings to your system.

119

The PATH Environment variable


PATH

When a command is executed and the path is not specified, then the shell will look in these directories in the given order, stopping on first match, to find the command.
which

colon separated list of locations where commands can be found.

Executables location may be specified.


$which xterm $ /usr/bin/xterm $/bin/ls /etc ./myls /etc

command showing location in the PATH of an executable

Use the built-in shell type command to ask the shell what is using to fulfill the following commands.
120

How the Shell Expands the Command line

A simplification:

Split the line into words Function and alias expansion Curly brace string expansion Tilde expansion Parameter and variable expansion Split the line into words again File glob expansion File redirection Run the command!
121

Shell Startup Scripts


Scripts of commands executed at login Uses include:

Configure the shell by setting local variables or running the set and shopt commands. Configure other programs through environment variables Establish aliases Run program on Startup.
122

Login Shells

Login shells are first shells started (i.e. when you log in) Shells launched from a login shell typically are not login shells. Login shells and non-login shells run different startup scripts. A login shell is a shell that someone started by login onto the system. A non-login shell is a shell started up in some other way, perhaps by a user or a program issuing the bash command.
123

Startup scripts: Order of Execution

Login Shells

/etc/profile

~/.bash_profile

/etc/profile.d ~/.bashrc

Non-login Shells ~/.bashrc

/etc/bashrc

/etc/bashrc

Typical sorts of commands placed in startup scripts include:


/etc/profile.d

Local variable settings, particularly PS1 Environment variable settings, such as PATH or LESS. Aliases, or perhaps unalias to remove undesired aliases set globally in earlier scripts. A umask can be setted.

124

/etc/profile

System-wide startup script for login shells. Parsed by all users with Bourne-style shells, including bash and sh. Usually sets default PATH variable, user limits, and other variables and settings. Bash only sources /etc/profile if the shell is a login shell. This script will set a series of variables including PATH, USER, LOGNAME, MAIL, HOSTNAME, HISTSIZE and INPUTRC. It will also run scripts in the /etc/profile.d directory.
125

/etc/profile.d

Some application-specific startup scripts in this directory. Scripts called by a for-loop in /etc/profile Scripts set up variables and run initialization procedures. Two copies of each script are listed in this directory, a Bourne Shell style with a .sh suffix, and a C shell style with a .csh suffix.
126

~/.bash_profile and ~/.bashrc

For user-specific settings Common to place variable settings, aliases Commands that place output to the screen, such as the date command, should go in .bash_profile, not .bashrc. The ~/.bash_profile file is only called by login shells. Never put any command that may echo something to the screen in the ~/.bashrc file; such command belong in the ~/.bash_profile file only.
127

~/.bash_logout

Resides in home directory Executed when exiting a login shell. Useful for running programs automatically at logout Example uses:

Make backup of files Delete temporary files Display Date and time of logout.
128

Mounting USB Media

Detected by the kernel as SCSI devices.

/dev/sdaX
Icon created in Computer window Mounted under /media/<Device ID>

Automatically mounted in GNOME/KDE.


Device ID is build into device by vendor.

Most vendors give USB disks a label. fstab-sync reads that label and automatically puts an entry in the /etc/fstab for it, mounting it in /media/<label> Like other disks, you can use fdisk and mke2fs to create partitions and filesystems on USB disks.
129

Mounting Floppy Disks

Must be manually mounted and unmounted

mount /media/floppy umount /media/floppy

DOS floppies can be accessed with mtools. mount and unmounts device transparently. Uses DOS naming conventions.

mdir a: mcopy /home/file.txt a:

For a complete list of the mtools commands, run mtools and/or consult the mtools info page.

130

Formatting Floppy Disks

Two types of format needed to prepare a floppy:

A low level format (rarely needed)

$fdformat /dev/fd0H1440 $mkfs t ext2 /dev/fd0 $mke2fs /dev/fd0 $mkfs t vfat /dev/fd0 mformat a:

A filesystem, one of :

Only the superuser or a non-privileged user logged into the system console can run these commands. Others do not have permission to do this.

131

Why archive Files?

Archiving places many files into one target file.

Easier to backup, store and transfer.

tar standard Linux archiving command. Originally, tar was used to create archieves on tap devices, hence its name- which stand for tape achieve.
132

Creating an Archive

Syntax:

tar cvf archive_name files


c creates a new archive. v produces verbose messages. F archive_name is the name of the new file.

133

Inspecting Archives

Syntax:

tar tf archive_name.tar tar tvf archive_name.tar

First form displays a list of all files in the archive. The v causes a long listing (like ls l) of each file in the archive.
134

Extracting an Archive

Syntax

tar xvf archive_name.tar

The archive will be extracted in the current directory.

Change to the target directory first.

Files maintain their hierarchy relative to the current directory.


135

Why Use file Compression?

Results in smaller file size Text files can be compressed over 75% Binary files usually dont compress much, if any. tar archives are often compressed. When text files often have patterns that lead to compression ratios of up to 75%, binary files rarely compress well with 0-25% being, typical. In fact, it is possible for a compressed binary file to actually be larger than the original.

136

Compression Utilites

gzip, gunzip

bzip2, bunzip2

Standard Linux compression utility Up to 75% compression for text files.


Newer Linux compression utility. Generally achieves better compression than gzip.

The gunzip command can also uncompress files, compressed with the traditional Unix compress command, making compress essentially obsolete. zip

Compatible with DOS/Windows PKzip/Winzip utilites and can compress more than one file into a single file.
137

On Compression

The traditional compress command can reduces the text file by more than half. The Standard gzip command does a better job, reducing the file to less than 1/3 of the original size. Finally, the newer bzip2 command reduces the file to /14 of the original file. The c option to the gzip command leaves the original compressed file alone, but sends and uncompressed copy of the file to standard output. The d option decompresses a file, making gzip d file.gz equivalent to gunzip file.gz

138

Compressing Archives

Often tar archives are compressed tar can compress/uncompress archives. Compression switches- use during creation and extraction

z for gzip compression j for bzip2 compression.

139

tar to Unformatted Floppies

Floppies can be used like tape drives


Low level format requied File system not needed Use tar to write to the floppy. /dev/fd0 is the destination Floppy cannot be mounted tar czvf /dev/fd0 mydir
140

Example:

Unit 11

Advanced Topics in Users, Groups, and Permissions

141

User and Group ID Numbers


User names map to user ID numbers. Group names map to group ID numbers. Data Stored on the hard disk is stored numerically. The user name and group affiliation of the file are not stored; rather, the user ID number and the group ID number are stored.
142

/etc/passwd, /etc/shadow, and /etc/group files

Authentication information is stored in plain is stored in plain text files:


The /etc/passwd file contains seven fields: user name, password placeholder (for historical reasons), uid number, gid number of the users primary group, GECOS filed (usually containing the users real name), home directory, and shell to be run when the user logs in. The /etc/group file contains four fields: group password placeholder, gid number, and a comma separated list of group members. The /etc/shadow file is referenced when someone logs in: the file contains a mapping of a user name to a password. For a complete list of fields, see the man page $man 5 shadow

/etc/passwd /etc/shadow /etc/group /etc/gshadow

143

System Users and Groups

Server programs such as web or print servers typically run as unprivileged users, not as root.

Running programs in this way limits the amount of damage any single program can do the system. These accounts exist primarily so that server programs can run as non-privileged users or as particular groups.
144

Examples: daemon, mail , lp, nobody.

Changing Your Identity

To change your password, run passwd

Insecure passwords are rejected.

To start a new shell as a different user:


su su su username su - username
145

User Information Commands

Find out who you are

whoami groups, id users, who, w last


146

Find out what groups you belong to

Find out who is logged in

Login/reboot history

Default Permissions

Default permission for files is 666 Default permission for directories is 777 umask is subtracted from default to determine new file/directory permissions. Non-privileged users umask is 002 Files will have permission of 664. Directories will have permission of 775. Roots umask is 022. Executive privilege is always denied a newly-create file, regardless of the umask in effect. Execute privilege always has to be explicitly granted to a file. Execute permission is given to a directory upon creation, unless explicitly denied by the umask.
147

Special Permissions

Special permissions: a fourth permission set (in addition to user/group/other) Applicable in four cases:

suid for an executable (4) sgid for an executable (2) sgid for a directory (2) sticky bit for all directory (1)

Set with chmod or Nautilus. chmod 3775 groupdir


148

Special Permission for Executiables

Special Permission for Executables:

In a long listing, the suid permission is displayed as a lower case s where the x would otherwise be located for the user permission (an upper case S would be present if the underlying executable permission is not set.) Commands running with the sgid permission run with the group affiliation of the group of the command.
149

suid: Command run with permission of the owner of the command, not the executor of the command. Like passwd. sgid: command runs with group affiliation of the group of the command.

Special Permission for Directories

Special permissions for directories:

Sticky bit: files in directories with the sticky bit set can only be removed by the owner and root, regardless of the write permissions of the directory. Sgid : files created in directories with the sgid bit set have group affiliations of the group of the directory.
150

On Sticky Bits

The sticky bit for a directory sets a special restriction on deletion of files: with the sticky bit set, only the owner of the file, and the superuser, can delete files within the directory. The sgid permission for a directory means that files created in the directory will inherit its group affiliation from the directory, rather than inheiriting it from the user. Often both the sticky bit and the sgid permission will be set on a group directory.
151

Section 3

RHEL Power Tools

152

Unit 13

Introduction to String Processing

153

head

Displays first few lines (default: 10 lines) of the text in a file.

$head /tmp/output.txt $head n 20 /tmp/output.txt

Use n or --lines displayed

154

tail

Displays last few lines (default: 10 lines) of text in a file

$tail /etc/passwd

Use n or -lines to change number of lines displayed.

$tail n 5 /etc/passwd

tail is often used by the system administrator to read the most recent entries in the log files. Use -f to follow the end of a text file as it changes.

$tail f make.out

Used to watch log files.


155

wc

word count also counts lines and characters

$wc story.txt

Use l for only line count Use w for only word count Use c for only character count

156

sort

Sorts text to stout original file unchanged

Common options

$ sort [options] files


-r -n -f -u -t x -k POS1 -k POS1.POS2

The argument to the k can be two numbers separated by a dot. In this case, the number before the dot is the field number and the number after the dot is the character within that field with which to begin the sort.
157

Reverses sort to sort descending Numeric sort ignore (fold) case of characters in strings. unique (remove duplicate lines in output). use x as field separator. sort from field POS1 Sort using fields POS1 ending at POS2.

uniq

Removes successive, duplicate lines in a file. Can use in conjunction with sort to remove all duplicates ( or use sort u) Use c to count number of occurrences of duplicate data. To print only unique line occurrence in a file (removing all duplicate lines), input to uniq must first be sorted. Since uniq can be given fields or columns on which to base its decisions, these are the fields or columns upon which its input must be sorted.
158

On uniq

Use u to output only the lines that are truly unique only occurring once in the input. Use d to output only print one copy of the lies that are repeated in the input. Use c to produce a frequency listing. Each line will be prepended with a number indicating how many times it appears in the input. Use fn or -sn to avoid comparing the first n fields or characters in each line respectively. $cut d: -f7 /etc/passwd | sort | uniq

159

cut

Display specific columns of file data


$cut f4 results.data -f specifies field or column -d specifies field delimiter (default is TAB)

$cut f3 d: /etc/passwd $cut c2-5 /user/share/dict/words


160

-c cuts by characters

Other String processing Tools

paste paste files together tr character translator paste combines files horizontally. It takes a line from each file and pastes them together to standard output, separated by a tab. Use d option to change the output of the output delimeter.

tr is used to translate characters; that is, given two ranges of characters, any time a character in range 1 is found, it is translated into the equivalent character in range 2. This command is commonly used in shell scripts to ensure that data is in an expected case.
161

$paste d: ids.txt data.txt > merged.txt

Version Comparison with diff

Compares two files for differences


$diff area.c /tmp/area.c 33c33 < x = y +2; ---> x = y+4; 33c33 indicates line where files differ < indicates line in first file. > indicates line in second file.
162

Spell checking with aspell

Interactive spell-checker Easy way to check spelling in a file.

Can create personal dictionary look quick spell check.

$aspell check letter.txt

$aspell l will non-interactively list the misspelled words in a file read from standard input. Visit http://aspell.sourceforge.net
163

$look must

Formatting Tools

expand expand tabs into spaces fmt - reformat text into paragraph.

fmt formats its input paragraphs of the line width you specify with wn. You can also request uniform spacing (with two spaces after each sentence) with u. fmt interprets blank line in its input as paragraph delimiters.

pr

-reformat text for printing


By default, it outputs 66-line pages including 56 lines of text and a header (which can be suppressed) $ pr f /usr/share/dict/words | lpr

164

Awk , tcl, perl


Advanced! More advanced. Simple

165

Unit 12

Advanced Uses of the vi and vim Editors

166

File Repositioning

G 1G

go to last line in the file go to first line in file (any number can be given and curser will jump to that line) <Ctrl-f> go forward one full screen <Ctrl-b>go back one full screen <Ctrl-d>go down half a screen <Ctrl-u>go up half a screen This is useful when an error message tells you that an error exists on the particular line of a file. You can use the G command preceded by that number to jump right to the offending line.
167

Screen Repositioning

H M L z<Enter> z-

go to first line on screen(high) go to middle line on screen (middle) go to last line on screen(low) Make current line first line on screen. make current line last line on screen.
168

Filtering

The output of a command can be placed in the file. The data in the file can be used as input of a command. Examples:

!!date

Replace current line with the output of date command.


The paragraph will then be replaced with the output of the sort command. Will replace the paragraph with a paragraph formatted to be less than 66 characters wide.
169

!}sort

!}fmt -66

ex mode: Search and Replace


sed style search and replace Different default addressing rule.


No address 1,12 1,$ or % .,.+10

current line only. Change lines 1 through 12 for changes in the entire file. from current line(.) to current line plus 10 lines (.+10) Example: :%s/Ohiho/Iowa/g :%s/dev/had /dev/sdag :%s/\/dev\/had/\/dev/\sda/g
170

Visual Mode

Allow selection of blocks of text

gg format to textwidth columns > indent < unindent

v character-oriented highlighting V line-orienting highlighting <Ctrl-v> block orienting highlighting Visual Keys can be used in conjunction with movement keys: w, ), }, arrows, etc. Highlighted text can be deleted, yanked, changed, filtered, search/replaced, etc. change d delete y yank(copy)

171

Advanced Reading and Saving


:r newfile :r !date :1,20w xfile :.,$w yfile :1,20w >>zfile :n otherfile :n! otherfile :n#
172

Configuring vi and vim

Configuring on the fly

Configure permanetly

:set or :set all

A few common configuration items


~/.vimrc or ~/.exrc :set :set :set :set

:set ignorecare or :se ic or :se noic :set number or :se nu or :se nonu

:se wm=15 and :se wm=0

showmatch or :se sm or :se nosm autoindent or :se ai or :se noai textwidth=65 (vim only) wrapmargin=15 :set wrapmargin=0

173

Expanding your vocabulary

Learn more cursor movements

Expanding change, delete, yank, and put vocabulary

Add the advanced material from the appendix to your skill base. Learn more configuration features. Play with filters. :help
174

Cursor Movements

0 start of current line $ end of current line ^ first nonblank character of current line. e end of next word gg top of current file n% go to a line n percent through the current file n| go to column n of the current line Read the material in :help. Learn to maneuver around the online help. Place the cursor over one of the |tags| and go to that with <Ctrl-]> keystroke, returning to the previous screen with :n#. This is a rich resource well worth being mined extensively.
175

Unit 14

String Processing with Regular Expressions

176

Pattern Matching with Regular Expressions


Regular expressions are a pattern matching engine Used by many tools, including: grep, sed, less , vi, awk Values:

Two types:

Power over ease of use Greed!

Basic Extended

177

Tools with Regular Expressions

grep : the general regular expression processor, which analyzes the contents of files a line at a time, returning line that match a pattern. Sed : the stream editor, returns the contents of a file (or stream of data), performing a specified search and replace instruction. less : which uses regular expression in search commands. vi : which uses regular expression for searches (like less) or search and replace (like sed). awk : a data oriented programming language.
178

grep

Prints lines of files where a pattern is matched

$grep gmustafa /etc/passwd $ls | grep .c

Also used as filter in pipelines.

Uses regular expressions

$grep [0-9][A-Z]\{3\}[0-9]\{3\} cars


179

Common options of grep


-v return lines that do not contain pattern -n precede returned lines with line numbers. -c only return a count of line with the matching pattern. -l only return the names of the files that have at least one line containing the pattern. -r perform a recursive search of files, starting with the named directory. -i perform a case-sensitive search.
180

Using cron

Must edit and install your cron table file Cron table file cannot be edited directly Edit the file and then install with crontab or Edit the file through crontab Syntax:

Cron table files( crontabs ) are stored in /var/spool/cron, which is not accessible by non-privileged users.
181

contab [-u user] file crontab [-l|-r|-e]

Using sed

Quote search and replace instructions Sed addresses


$ sed s/dog/cat/g pets $ sed 1,50s/dog/cat/g pets $ sed /digby/,/duncan/s/dog/cat/g pets $ sed e s/dog/cat/ e /s/hi/lo pets $ sed f myedits pets
182

Multiple sed instructions


Unit 15

Finding and processing Files

183

find

$ find [directory] [criteria] Searches directory trees in real-time


Can execute commands on found files May only search directories where the user has read and execute permission. Find has a huge amount of options that can be provided to describe exactly what kind of file should be found. You can search bases on file name, file size, last modified time stamp, inode number, and many, many more. find allows you to perform arbitrary actions on arbitrary files.

Slower but more accurate than slocate CWD is used if no starting directory given All files are matched if no criteria given.

184

Basic find examples

$ find name show.png

$find / -name *.png $find -iname show.png

Search for files named show.png

$find user gmustafa group gmustafa

Case-insensitive search for files named show.png, Snow.png, SNOW.PNG, etc. Search for files owned by the user gmustafa and the group gmustafa

The regex option in find does not work quite the way one would expect. regex applies the regular expression to the name of the file, including the absolute path to the file.

185

find and Logical operators


Criteria and ANDed together by default. Can be ORd or negated with o and not Parentheses can be used to determine logic order, but must be escaped in bash.

Logical ANDs have a higher priority than a logical OR, and a logical NOT has a higher priority than an AND or an OR. To force precedence of an expression, you can enclose options that should be grouped together in parentheses.

$ $ $ $ $

find find find find find

user gmustafa not group root -user gmustafa o user root not\(-user gmustafa o user root \) / -name *.png user gmustafa mtime+12 / -name *.png not user abcd

186

find and Permissions


Can match ownership by name or id (-user, -group, -uid, -gid) $ find / -owner gmustafa gid 500 Can match octal or symbolic permissions $ find perm 755 # if anyone can write $ find perm +2 #if anyone can write $ find perm -2 #if everyone can write $ find perm +o+w # if other can write Use not to test denied permissions. A numeric permission preceded by will match files that have at least one bit (user, group or other) for that permission set. Symbolic permission by a + will match any file where all criteria are met. To match files where someone has been denied a permission, simply write a search for files where the permission is granted and then negate it. In other words, to find files where neither the user nor the group have read access you could use something like: find not perm +ug+r
187

find and Numeric Criteria

Many find criteria take numeric values

$ find size 10M

$ find size +10M

Files with a size of exactly 10 Megabytes. Files with a size over 10 Megabytes

$ find -size -10M

$ find / -atime 5

Files with a size less than 10 Megabytes


The date of the last time the file was read (-atime) The date of the last change to the files data (-mtime) The date of the last changes to the files metadata. (-ctime)

$ find / -mtime +5

$ find / -ctime -5

188

find execution examples

$find name *.conf exec cp {} {}.orig \;

Create backup copies of configuration files, adding a .orig extension


Prompt to remove gmustafas tmp files over 3 days old. Fix world-writable files in your home directory.

$find /tmp ctime +3 user gmustafa ok rm {} \;

$find ~ -perm +2 exec chmod o-w {} \;

If your exec command does not include {}, find will still execute the command once for each file that is found.
189

Find and Access times

find can match by inode timestamps


-atime : when file was last read -mtime : when file data last changed -ctime : when file metadata last changed

Value given is in days

$find mtime -10

Files modified less than 10 days ago.


190

More of *time

While the values passed to atime, -ctime and mtime are measured in days, there are also corresponding criteria that perform searches in minutes: -amin, -cmin and mmin. You can match access times relative to the timestamps of other files using -anewer, -cnewer and newer, which test mtimes.

$ find newer recent_file.txt

To match files older than recent_file.txt you would simply negate the mnewer criteria.

Would list all files with mtimes more recent that that of recent_file.txt.

The metadata, including all three timestamps, for a file can be manually examined using the stat command.

$ find not newer recent_file.txt

191

Executing commands with find

Commands can be executed on found files.

Command must be proceeded with -exec or ok

-ok prompts before acting on each file.

Command must end with <space>\; Can use {} as a filename placeholder

$ find size +100M ok gzip {} \;

-ok options, which caused find to ask for each file. When a character is prepended with a backslash (\), bash is instructed to treat it literally, so typing \; at bashs command prompt will send; to find after bash has done its interpretations.
192

Unit 16

Investigating and Managing processes

193

What is a process?

A process is an executing program which has many components and properties


Exec thread PID Priority Memory context Environment File descriptors Security credentials
194

How Processes are created?

Once process forks a child, pointing to the same pages of memory, and marking the area as read-only. Then the child execs the new command, causing a copy-on-write fault, thus copying to a new area of memory. A process can exec, without forking

The child maintains the process ID of the parent.

195

Process Ancestry

init is the first process started at boot time always has PID 1 Except init, every process has a parent. Processes can be both a parent and a child at the same time. pstree shows the process ancestry for all process running on the system.
196

Process States

A process can be on of the many states:

-R

-S

process in the run queue. It is waiting for its turn to run or it is executing. Process is not executing, not it is ready to run. It is waiting for an event to occur or a signal to arrive to wake it up. Process is not executing because it has been stopped. Process is sleeping and can not be woken up until an even occurs. It can be woken by a signal. Typically, the result of an I/O Operations. 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 the signal, all resources except for the process identity number (PID) are released. Zombie process are cleared from the system during the next system reboot and do not adversely affect system performance.

Runnable (on the run queue)


Sleeping stopped

-T

-D

Uninterruptible sleep

-Z

Defunct (Zombie) process

197

Viewing process

ps Displays processes information Syntax: ps [options] Useful options:


The options described above are based on output conforming to the UNIX98 Standard. -l long listing. Includes more information such as the process owners UID. $ ps alx | grep lpd $ pgrep lpd The above two command are identical.

a Processes by all users x process from all terminal u show process owner w include command arguments f show process ancestry.

198

Sending Signals to Processes

Syntax:

Sends the specific signal to a process Defult signal is TERM

$ kill [-signal ] pid(s) $ kill [-signal] %jobID

$killall Read man 7 signal. kill can send many signals, but processes only respond to the signals they have been programmed to recognize. Signals may be sent to processes interactively using top and gnome-system-monitor.

$kill l lists all available signals

199

Terminating Processes

Most desirable way to end a process is to let it end normally

Commands finish, applications are exited.

Can attempt to interrupt with <Ctrl-c> (INT) or send a TERM signal If all else fails, send a KILL signal. Using KILL signals on a route basis may cause zombie process and lost data.
200

Interactive Process Management Tools


Display real-time process information Allow sorting, killing and renicing Command-line: top GUI: gnome-system-monitor Read: man top and help of gnomesystem-monitor

201

Running a Process in the Foreground

When a command is entered, the shell will not process further input until the process is complete and the shell prompt is redisplayed. i.e. job control. The typeahead buffer allows you to type other command, but they will not be processed until the pending process completes, or returns.
202

Altering process scheduling priority

At process invocation time Syntax:

Processes are scheduled with a Defult priority of 0 Priority value can range from -20 (highest priority) to 19 (lowest).

$ nice [-n adjustment] command

Non-privileged users may not set niceness value to less than zero; that is , they may not request a higher than normal priority for their processes. This is a function reserved for the superuser.
203

$ nice myprog $ nice n 15 myprog

Altering Process scheduling Priority (continued)

renice changes the priority of a running process.


Once a priority value is raised, a non-privileged user can not lower it.

$ renice # [ [ -p | g] PID] [[-u]user] # is the priority value.

The p options is not necessary. If you wish to change the priority of an entire process group, use the g option. Use u option, to modify the priority of all the process of a particular user.

$ renice 15 p < PID of users offending, greedy process> $ renice -15 p <PID of some users critical process>

$ renice 15 u gmustafa

204

Running a Process in the Background


Running a command in the background allows another process to run concurrently on the same terminal. Launch a program as a background process by appending an ampersand (&) to the end of a command: $firefox & When a process is started in the background, a new bash sub-shell is created. The bash program is then replaced with the command being executed (the fork then exec procedure). Background processes can be managed like any other process.

205

Suspending a Process

Foreground jobs can be suspended: temporarily halted without being killed Suspend a foreground process with <Ctrl-z> Suspended jobs can be:

When the job resumes, it will continue executing from the point at which it was suspended, it will not have to start over from the beginning.
206

Resumed in the background (bg) Resumed in the foreground (fg)

Listing Background and Suspended Jobs

jobs displays all process running in the background or that suspended. The number in brackets is a job number, used to kill jobs or bring them back to the foreground. Job numbers are referenced with %.

207

Resuming Suspended Jobs

When a command is suspended or backgrounded, it can be brought back to the foreground with fg. Suspended jobs can be resumed in the background with bg. Syntax:

$ fg $ bg

[%job_number] [%job_number]
208

Compound Commands

List of commands separated by semi-colons List inside () to run inside a subshell

A subshell group will combine the commands so they are treated them as one unit. When a group of commands is placed inside parentheses, a new subshell is spawned and output can be redirected as if it were one command.

$ (cd /usr; du ) &

$ (date; who | wc l) >> logfile

209

Scheduling a Process to Execute Later

Syntax:

$ at time

<commands>

$ atq [user] $ atrm [user|atJobID]

Commands will be executed at the time indicated Non-redirected output is mailed to the user.

$ $ $ $

at at at at

8:00pm December 7 7 am Thursday now + 5 minutes midnight +23 minutes


210

Scheduling Periodic Processes

The cron mechanism allows processes to be invoked periodically

User need not to be logged on

Cron jobs are listed in a crontab file The cron mechanism is controlled by a daemon called crond. This daemon wakes up every minute or so and determines if any entry users cron tables need to be executed. If the time passed for an entry to be started, it is started. A cron job can be scheduled as often as once a minute or as infrequently as once a year.
211

Using cron

Must edit and install your cron table file Cron table file cannot be edited directly

Edit the file and the install with crontab Or Edit the file through crontab $ crontab [-u user] file $ crontab [-l | -r | -e]
212

Syntax

Crontab File Format

Entry consists of five space delimited field followed by a command line

One entry per line, no limit to the line length.

Fields are minute, hour, day of month, month, and day of week. Comment line begin with #
213

Unit 17

Network Clients

214

Other GUI Web Browsers

Epiphany

Uses same rendering engine as Firefox Fully Gnome-complaint, but fewer features. KDE web browser/file manager Supports tabs, popup-blocking, etc. Uses khtml rendering engine.
215

Konqueror

Non GUI Web browsers

links

Provided by the elinks rpm Full support from frames and ssl Examples

$ links http://www.redhat.com $ links dump http://www.redhat.com $ links source http://www.redhat.com

216

wget

Retrieves files via HTTP and FTP Non-interactive useful in shell scripts. Can follow links and traverse directly trees on the remote server useful for mirroring web and FTP sites. Read info wget and wget help.

217

Remote access and file transfer


ssh/scp telnet and the r services rsync FTP clients Nautilus

218

ssh : Secure shell

Public-key encryption technology replacement for various unsecured services. Allows secure access to remote systems

$ ssh gmustafa@oss2 -or$ ssh -l gmustafa oss2 gmustafa@oss2s password: $ ssh oss2 ls /tmp $ ssh oss1 df -h
219

scp : Secure copy

Secure replacement for rcp Layered on top of ssh


$ scp source destination Remote files can be specified using:


user@host:/path/to/file

scp requires that the destination be a directory if the source is a directory or consists of more than one file.
220

Also available is sftp, an interactive file-transfer program similar to a simple ftp client. The remote hosts sshd needs to have support for sftp in order for the sftp client to work.

-r optoin enables recursion -p preserves time and permissions -c compresses datastream

telnet and the r services

Insecure protocols mostly replaced by ssh

telnet : login names and password pass over the network in clear text r services (rsh, rlogin, rcp): generally insecure authentication mechanism. telnet client can be used to connect to services running on arbitrary ports Examples: testing your mail server

$ telnet localhost 25

These tools should never be used.


221

rsync

A drop-in replacement for rcp copying to or from remote systems. Can use ssh for transport $ rsync e ssh mysong.mp2 172.27.12.13:/home/gmustafa Faster than scp copies difference in like files. Useful options to rsync -e Use an external, rsh-compatible program to connect (usually ssh) -a Recurse subdirectories, preserving permissions, ownership, etc. -r Recurse subdirectories without preserving permissions, etc. --partial continue partially downloaded files --progress print a progress bar while transferring -p Same as partial progress Read man 1 rsync
222

lftp

Versatile command line FTP client Anonymous or real-user sessions


$ lftp ftp.cdrom.com $ lftp u gmustafa ftp.myserver.com

Automated transfer with non-interactive lftpget Graphical tools gftp.


223

gFTP

Applications/Internet/gFTP Graphical FTP Client Allows Drag-and-Drop transfers Anonymous or Authenticated access Optional secure transfer via ssh (sftp).

224

smbclient

FTP-like client to access SMB/CIFS resources Examples:

View shares on a server

Access a share

$smbclient L server1

CIFS is most commonly implemented as Microsoft Windows Network Neighborhood. Useful options include:

$smbclient U student //server1/homes

-w workgroup or domain -U Username -N Suppress password prompt (otherwise you will be asked for a password).
225

Network Diagnostic Tools

ping

tracroute

Detects if it is possible to communicate with another system. Many systems no longer respond to pings. Displays the computers through which a packet must pass to reach another system. The mtr command is a repetitive version of traceroute, giving continually updated connection time statistics. Performs hostname to IP address translation, as well as the reverse. Performs a service similar to host in greater datail. Provides a service similar to host in greater detail. A graphical front end of tools listed above (as well as some others) in a single, simple interface. gnome-nettool can be run from the command line or by selecting its icon from the internet section of the Application Menu. Note that, this tool may no be installed by default. 226

mrt

host

dig

netstat

gnome-nettool (GUI)

Unit 18

Bash Shell Scripting

227

Scripting Basics

Shell scripts are text files that contains a series of commands or statements to be executed. Shell scripts are useful for:

Automating commonly used commands. Performing system administration and troubleshooting Creating simple applications Manipulations of text or files. Application prototyping
228

Creating Shell Scripts

Step 1: Use a text editor such as vi to create a text file containing commnds.

First line contains the magic shbang sequence : #!

#!/bin/bash Comments start with a #

Comment your scripts

Create shell script which is self documenting. If you enter this by pressing \ key followed by the Enter key on the most keyboards. This will enable you to enter one command that spans multiple lines.

229

Creating Shell Scripts cont.

Step 2: Make the script executable

$ chmod a+x myscript.sh

To execute the new script:

Place the script file in a directory in the executable path ORSpecify the absolute path or relative path to the script on the command line.

230

Generating Output

Use echo to generate simple output


$ echo Welcome to Redhat Linux paradise! $ echo n please enter the file name:

Use printf to generate formatted output.

$ printf the result is %0.2f\n $RESULT Syntax similar to C printf() function Does not automatically put a newline at the end of the output.

231

Handling Input

Use read to assign an input value to a shell variable:

echo n Enter the filename: read FILENAME

read reads from standard input and assigns one word to each variable. Any leftover words are assigned to the last variable. A word is defined as a character string surrounding with white space such as spaces and tabs. Can be changed by IFS. IFS=: If there are more words than variables, the last variable is assigned all the remaining words.
232

bash scripts example


#!/bin/bash echo n Enter name (First Last): read FIRST LAST printf Your First name is %s and your last name is %s \n \ $FIRST $LAST The (-p) option is used to display a prompt string. Place quotes around the string if you need to prompt the user with a multiple-word command #!/bin/bash read p Enter Several Values: value1 value2 value3 echo value 1 is $value1 echo value 2 is $value2 echo value 3 is $value3

233

Exit Status

Commands exit with an exit status


0 for success, 1 to 255 for failure Exit status of most recently executed command is kept in the $? Variable just like return values form shell functions.

Shell scripts may set an exit status with the exit command.

exit 1 # indicated an error


234

Control Structures

The three types in shell programming:

Sequential structures the program flows one line after another Selection structures code execution based on a logical decision.

if, if/else, if/elif/else and conditional operators.

Repetition structures (loops) code execution is repeated based on a logical decision

for, while and until

235

Conditional Execution

Commands may be executed conditionally, based on the exit stats of the previous command.

Examples:

&& ||

logical AND logical OR

This structures can be used in the command line as well.


236

$ grep gmustafa passwd || echo No gmustafa! $ cp a /tmp/*.o . && echo Done!

Selection Structures: Using the if statement

if selection structures execute the body of the structure only if the condition tested is true.

if [condition]; then

do something

fi

237

File Tests

File tests:

-f tests to see if file exist and is a regular file -d tests to see if a file exists and is a directory -x tests to see if a file exits and is executable if [-f $HOME/lib/functions ]; then

source ~/lib/functions

fi
238

More on File Tests


-d FILE -e FILE -f FILE -h FILE -L FILE -r FILE -s FILE -w FILE -x FILE -O FILE -G FILE group. Type help test at

True True True True True True True True True True True

if if if if if if if if if if if

file is a directory file exists. file exits and is a regular file file is a symbolic link. file is a symbolic link. the file is readable by you. file exists and is not empty. the file is writable to you. the file is executable by you. the file is effectively owned by you. the file is effectively owned by your

the shell to see a complete list.


239

String Tests

Strings may be tested as well


-z returns true if the string is empty -n returns true if the string is not empty. Operators such as =, !=, < and > may be used to compare strings as well. if [ $(id u) =0]; then echo You are logged in as root fi

240

More on String Tests


-z STRING -n STRING STRING1=STRING2 STRING1!=STRING2 STRING1<STRING2 STRING1>STRING2 -o OPTION ! EXPR EXPR1 a EXPR2 EXPR1 o EXPR2 ARG1 OP ARG2

True if string is empty. True if string is not empty. True if the strings are equal. True if the strings are not equal. True if STRING1 sorts before STRING2 lexicographically. True if STRING1 sorts after STRING2 lexicographically. True if the shell option OPTIOIN is enabled. True if EXPR is false. True if both EXPR1 AND EXPR2 is true. True if either EXPR1 OR EXPR2 is true. Arithmetic tests. OP is one of the eq, -ne, -lt, -le, -gt, or ge.

241

Selection Structures: Using if/else Statements

if/else selection structures execute the body of the if structure only if the condition tested is true, otherwise the else is executed.

if [ condition] ; then

Do something

else

Do something else

fi
242

Selection Structures: Using the case Statement

The case statement provides an alternative method for performing selections that may be cleaner than multiple if/elif/else tests.

case variable in

pattern1)

do something ;; do another thing ;;

pattern2)

esac

243

Repetition Structures: The for-loop

The for repetition structure provides a method for iterating, or looping, through a list of values and executing commands on each of these values.

for variable in list-of-values

do

commands

done
244

for and sequences

For loops are useful for iterating through numeric sequences Use bash notation for simple sequences

for I in {0..10}

Will use : 0,1,2,3,4,5,6,7,8,9,10

Use seq command for arbitrary increments For I in $(seq 0 2 10)

Will count by twos: 0, 2, 4, 6, 8, 10


245

Selection Structures: The while-loop

The while loop structure provides a useful method for performing a set of commands while a condition remains true. The syntax is: while condition do commands done While loops are known as sentinel repetition structures. An until loop works in exactly the same way, except that it continues to execute as long as the command following the until statement executes successfully; that is, it will stop the loop when the command succeeds.

246

continue and break

While loops can be disrupted during execution

continue stops the current execution of the loop and reexamines the initial condition, possibly restarting the loop. break stops the processing the loop entirely, jumping past the done statement. exit exits from the shell scripts entirely You may provide an exit status. Without an explicit exit status, the exit command will exit with at status of zero, indicating success.
247

Using Positional Parameters


Positional parameters are special variables that hold the command-line arguments to the script. The positional parameters are available are $1, $2, $3, etc. These are normally assigned to more meaningful variable names to improve clarity. $* holds all command-line arguments. The variable $0 is reserved and specifies the program name as it was executed on the command line. Variables above $9 require special handling and so they must be enclosed in curly braces, e.g. ${11}. All positional parameters are read only variables.
248

Handling Parameters with Spaces

Bash expects space-delimited parameters


Causes problem when parameters have spaces Example : script.sh arg 1 arg 2

$* contains arg 1 arg 2 Automatically assigns VAR with $1, $2, etc..

Solution: for VAR; do ; done

Handles spaces in parameters gracefully. VAR would be set to arg 1, then arg 2

249

Handling Filenames with Spaces

Bash mis-interprets spaces in filenames


Space treated as filename-delimiter by default my file interpreted as two files: my and file

IFS environment variable can be used to specify an alternate (or no) delimiter.

Always reset after changing


OLD_IFS=$IFS IFS=$(echo) <code> IFS=$OLD_IFS

#Backup old delimiter #Set empty delimiter #Do Stuff #Restore IFS
250

Using functions in shell scripts


Shell scripts may include shell functions. Shell functions may improve program readability. They also help to remove repetitious code from the scripts. Shell functions must be declared before they are used. If there is a section of shell code that is trying to accomplish the same task and it is five or more lines long then it is good candidate for being put in a separate function. This is known as functional decomposition and will improve the program readability. If there are a piece of shell code that you are likely to use again then it is also good candidate for being put in a separate function. This is called code reuse. Shell functions must be declared in the shell script before they may be used. This can be done by including the function before it gets called in the script or by sourcing another shell script using the keyword source or the (.) operator.

251

Using functions, continued

Arguments may be passed to a shell function by using their own set of positional parameters ($1, $2 etc.)

The value of $filename will be available as $1 inside the body of myFunction. Functions may return the values by using the return keyword which sets the value of the special variable $?. local make the variables unavailable outside the function. This is called information hiding and is useful for keeping function compartmentalized. In addition it will prevent us from accidentally overwriting a global variables. Functions may return vales using the return keyword which sets the value of the special variable $?.

myFunction $filename

252

Scripting at the command line


Scripts can be typed at the bash prompt. If/else/for/while statements cause extended prompt Can also separate lines with semicolon (;) Define functions for code reuse Store common functions in ~/.bashrc Bash built-in fc for long commands

Default editor is vi or $EDITOR


253

Shell Script debugging

In order to debug a shell script invoke the shell interpreter with debug options or change the shebang to include the debug options.

$bash x script_name $bash v script_name #!/bin/bash x #!/bin/bash -v


254

Appendix:

Advanced Features of the vi and vim Editors

255

Advanced uses of vi and vim


Special features of vim File and screen repositioning Setting marks in text Mapping complex commands to keystrokes (:map) Advanced read and save

256

Special Features of vim: Multiple Buffers

vim -o fn1 fn2 start <Ctrl-w>s <Ctrl-w>v <Ctrl-w>n file <Ctrl-w>q <Ctrl-w>[hjkl]

with multiple windows split file horizontally split file vertically open new window, new quit out of a window move to different window. resize window show help for windows.
257

<Ctrl-w>[+-] :help windows

Special Features of vim: Reformatting Text Width


gq reformats text width. Usage:

Make sure textwidth is set:

:set textwidth=65

Issue the gq command followed by the paragraph indicator:


gq} 1GgqG
258

Other Special Features of vim

Recording keystrokes and playback.


qc q @c

record keystrokes saving to buffer c stop recording keystrokes play back keystrokes from buffer c

Other configuration options

:syntax <LANGUAGE | off> :nohl


259

Setting a mark in text

Set a mark in text with a single quote followed by a letter of the alphabet specifying the mark name. Example: delete some lines

Move the curson to the first line to delete Set mark a: a Move the cursor to the last line to delete. Set mark b: b Delete from marks a to b: :a,bd
260

Creating Commands with :map


You can create your own commands with the :map command Place: map commands in ~/.vimrc or ~/.exrc file Example: mapping <Ctrl-x> to : syntax off : map <Ctrl-x> : syntax off :map <Ctrl-x> :syntax off :map <Ctrl-l> :w!<CR>:!aspell check %<CR>:e!%<CR> The <Ctrl-l> keystroke may not be entered into the file. To force it to be read, run <Ctrl-v> before the <Ctrl-l>: <Ctrlv><Ctrl-l> The <Ctrl-v> forces the <Ctrl-l> to be taken literally. The sequence <CR>, meaning carriage return should be entered just as shown, with the four separate characters.

261

Miscellaneous Commands

Returns to your previous location J Join with the next file. ~ Change character case >> Indent << Unindent.

262

What next?

LUGs (bdlug) http://www.redhat.com/training http://www.redhat.com/training/developter/courses/ http://www.redhat.com/training/architect/courses http://www.redhat.com/training/developter/courses/ http://www.idiom.com/free-compilers http://www.redhat.com/apps/support/knowldedgeba se http://www.tldp.org http://www.linuxquestions.org
263

Motto

It is worth working hard today to learn a thing and so be lazy tomorrow!!

264