Sunteți pe pagina 1din 332

Adapted

Mohammad Ariful Hyder Associate Professor, Dept. of CSE Dhaka City College, Dhaka.

Unix/Linux History
Unix first version created in Bell Labs-1969 Write Unix system in C language-1973 by Denis

Ritchie AT&T licenses source code for low cost, Trademarkes Unix name, licensees must create new name for their operating systems Many Unix Flavors:, IBM-AIX, Solaris,UnixWare, HP-UX

30 January 2013

Unix System Administration & Programming

Unix Principles
Multiuser, Multitasking
Configuration data stored in text Ability to chain programs together to perform

complex tasks Working with Wide variety of computers High Level of Security

30 January 2013

Unix System Administration & Programming

GNU Project / FSF


GNU (Gnu Not Unix) Project started in 1984 (R. Stallman)
Goal: create free Unix clone By 1990, nearly all required users applications created

(gcc, emacs,..)

Free Software Foundation


Non-profit organization that manages GNU project

30 January 2013

Unix System Administration & Programming

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

30 January 2013

Unix System Administration & Programming

GPL- GNU General Public License-2


freedom to run the program, for any purpose. freedom to modify the program to suit your

needs. freedom to redistribute copies, either gratis or for a fee. freedom to distribute modified versions of the program, so that the community can benefit from your improvements

30 January 2013

Unix System Administration & Programming

Linux Origins
Linus Torvalds
Finnish college student in 1991 create Linux kernel Code was made available over the internet for free Coders world wide took part in developing the OS

When Linux Kernel combined with GNU applications, complete free Unix-like OS appears (GNU/Linux OS)

30 January 2013

Unix System Administration & Programming

Why Linux
Linux and GNU software are distributed under the terms of the GNU Public License Agreement (GPL)
Linux is continually being developed by individuals throughout the world Skilled programmers can submit additions and

improvements to the Linux OS to Linux Development Team over the internet

30 January 2013

Unix System Administration & Programming

Why Linux
Applications are developed that are both free and commercial
Linux is for Intel based platforms, Power PC, Alpha-

based Linux is for Intel based, multiprocessor compatibility (SMPs)

Fully supported Many distribution (redhat,suse,fedora,ubuntu ...)


30 January 2013 Unix System Administration & Programming 9

Red Hat Enterprise Linux


Custom version of recent Linux kernel Utility and applications Installation and configuration software

Support available

30 January 2013

Unix System Administration & Programming

10

Installing Red Hat Linux


Partitioning Hard disks
Primary Partition + Swap partition

Installation method Network Configuration


IP ,Netmask, Gateway, Hostname +Domain name

+Name Server

Configuring Boot manager (LILO, Grub) Select Package

30 January 2013

Unix System Administration & Programming

11

Hard disk partitioning


hda: Primary Master IDE hdb: Primary Slave IDE hdc: Secondary Master IDE

hdd: Secondary Slave IDE


sda: First hard on SCSI bus. sdb: Second hard on SCSI bus.

30 January 2013

Unix System Administration & Programming

12

Hard disk partitioning-2


(Primary Partitions): 1234 (Logical Partitions): 567 hdxy IDE sdxy SCSI
x (name of hard disk) y (partition number ) Example: hda2,hdb5,sda6

30 January 2013

Unix System Administration & Programming

13

Hard disk partitioning-3


Track

Sector Cylinders

Sectors
Head

30 January 2013

Unix System Administration & Programming

14

Hard disk partitioning-4


Series of
Sectors addressed by (CHS) First sector name MBR: Master Boot Record contain:
Boot program Partition Table:4 Primary partitions

1234
MBR
30 January 2013

Partition 1

Partition 2

Partition 3

Partition 4
15

Unix System Administration & Programming

Hard disk partitioning-5


Primary Partition 1 Primary Partition 2 Primary Partition 3 Primary Partition 4

1234
Status Start FS End

A
0000 1BEh 1FEh

2
2001 3501 6001

FAT32
FAT16 EXT2 NTFS

2000
3500 6000 8000

Boot Program Partition Table

30 January 2013

Unix System Administration & Programming

16

Hard disk partitioning-6


MBR
Primary Partition 1 Primary Partition 2 Primary Partition 3 Primary Partition 4

1234

Boot Sectors P. P. 1 Type: FAT32


M B R
30 January 2013 Unix System Administration & Programming 17

P. P. 2 Type: Extended

P. P. 3 Type: Ext2

P. P. 4 Type: NTFS

Hard disk partitioning-7


P1: FAT32
M B R

P2: Extended D: E: F:

C:

P1: FAT32
M B R

P2: EXT3

P3: Extended D: < 1024 E: F:


Swap

C:

30 January 2013

Unix System Administration & Programming

18

Installation Mode
New or Upgrade from where
CD HTTP, FTP, NFS, Hard disk Kickstart

30 January 2013

Unix System Administration & Programming

19

Local Login
Text mode login at Virtual consoles
Multiple non-GUI logins are possible through the use of

virtual consoles There are by default 6 available virtual consoles Available through Ctrl-Alt-F[1-6]

Graphical login
If X is running, it is available as Ctrl-Alt-F7

30 January 2013

Unix System Administration & Programming

20

Running Commands
Command have the following syntax:
command [options] [arguments]

Each item is separated by a space

Options modify the commands behavior


Arguments are filenames or other information needed by the command

Separated commands with semicolon (;)


Example: date, cal
30 January 2013 Unix System Administration & Programming 21

Changing Your Password


Password should be changed after first login
From the Red Hat menu, choose
Preferances Password

From a terminal, use passwd

30 January 2013

Unix System Administration & Programming

22

Linux Graphical Environments


XFree86: standard X windows System
GNOME Desktop environment based on the GTK+ toolkit

KDE Desktop environment based on the Qt toolkit

30 January 2013

Unix System Administration & Programming

23

Linux File Hierarchy Concepts


Files and directory are organized into a singlerooted inverted tree structure
Names are case-sensitive, delimited by the /

(forward slash) character Each shell and system process has a current directory

30 January 2013

Unix System Administration & Programming

24

Linux File Hierarchy Concepts


/
Directories may

contain plain files or other directories Leads to a tree structure for the filesystem Root directory: /

bin

users tmp
rocket cs2204

doug joke.txt

lab1.txt lab2.txt

30 January 2013

Unix System Administration & Programming

25

Changing Directory
pwd displays the absolute path to the current directory

cd changes directories
- To an absolute path:

$ cd /home/issa/work To relative path:$ cd project/docs To a directory one level up: $ cd .. To your directory home: $ cd To your previous working directory: $ cd -

30 January 2013

Unix System Administration & Programming

26

Listing Directory Contents


List the content of a current directory or a speified directory
Usage:

ls [options] [files or dirs]

30 January 2013

Unix System Administration & Programming

27

The Home Directory


Every user has a home directory, typically under /home Contain user-specific configuration files as well as user data Represented by the ~ character

30 January 2013

Unix System Administration & Programming

28

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

30 January 2013

Unix System Administration & Programming

29

Relative pathnames
relative pathnames do not begin with a slash (/) Specifies location relative to your current working

directory Can be used as a shorter way to specify a file name

30 January 2013

Unix System Administration & Programming

30

pathnames
Separate directories by /
Absolute path
start at root and follow the tree e.g. /users/doug/joke.txt

/
bin users tmp rocket

Relative path
start at working directory .. refers to level above; . refers

doug

to working dir. If /users/doug/cs2204 is working dir, this refer to the same file as previous one

joke.txt cs2204 lab1.txt lab2.txt

../joke.txt
Unix System Administration & Programming 31

30 January 2013

File names
File names may be up to 255 characters All characters are valid, except the /
It may be unwise to use certain special characters in

File names are case-sensitive


Example: MAIL, Mail, mail, and mAil Again, possible, but may not be wise

30 January 2013

Unix System Administration & Programming

32

Copying files and Directories


cp copy files and directories Usage:

cp [options] file destination


More than one files may be copied at a time if the

destination is a directory

cp [options] file1 file2 dest

30 January 2013

Unix System Administration & Programming

33

Copying Files and Directories: The Destination


If the destination exists and is a directory, the copy is placed there with the same name If the destination exist and is a file, the copy overwrites the destination file If the destination does not exist, the copy is created with that name
30 January 2013 Unix System Administration & Programming 34

Moving and Renaming Files and Directories


mv move and/or rename files and directories Usage:

mv [options] file destination


More than one files may be moved at a time if the

destination is a directory

mv [options] file1 file2 dest

30 January 2013

Unix System Administration & Programming

35

Moving and Renaming Files and Directories: the Destination


If the destination exists and is a directory, the source

files or directory is moved there with the same name


If the destination exist and is a file, the source file is

moved to that filename, overwriting the file


If the destination does not exist, the source file or

directory is renamed with that name


30 January 2013 Unix System Administration & Programming 36

Removing and Creating Files


rm remove files Usage:

rm [options] filenames

-i -r -f

interactive recursive force

touch create empty files or update filestamps

30 January 2013

Unix System Administration & Programming

37

Creating and Removing Directories


mkdir make a directory rmdir remove an empty directory rm r recursively remove a directory of its content

and all

30 January 2013

Unix System Administration & Programming

38

Determining File Content


Files can contain many types of data Check file type before opening to determine appropriate command or application to use Syntax
file [options] filename (s)

30 January 2013

Unix System Administration & Programming

39

Viewing an Entire Text File


Syntax
cat [options] [file ]

Contents of the files are displayed sequentially

with no break Files display concatenated

30 January 2013

Unix System Administration & Programming

40

Viewing Files One Screenful At a Time


Syntax
less [options] [filename]

Display the contents of a text file one screen at a

time less is the pager used by man Syntax


more [options] [filename]

30 January 2013

Unix System Administration & Programming

41

slocate
output is based on a file index database, updated only once every day.
Syntax
slocate [pattern] locate [pattern]

Example
locate profile

All file or directory pathnames matching the supplied pattern will be displayed
30 January 2013 Unix System Administration & Programming 42

Getting Help: man Pages


man

display pages from reference manual

Manual contains multiple sections Key string or key word searches

Examples
man man man man ls -k copy -f profile 5 passwd

30 January 2013

Unix System Administration & Programming

43

Other Help Utilities


command --help The whatis and apropos info
Text based with sections organized by subject
Command is info section_name

Gnome documentation

30 January 2013

Unix System Administration & Programming

44

bash Introduction
Bourne Again shell Developed for GNU project The de facto standard Linux shell Backward compatible with Bourne shell (sh) the original (standard) UNIX shell. Bourne shell (sh) original UNIX shell written by Steven Bourne at AT&T

30 January 2013

Unix System Administration & Programming

45

bash Heritage
Bourne shell (sh) original UNIX shell written by Steven Bourne at AT&T
C shell (csh) written by bill joy at UC Berkeley
Added many features such as command completion,

history and job control

Korn shell (ksh) written by David Korn at AT&T

30 January 2013

Unix System Administration & Programming

46

bash Heritage-2
Bourne Again Shell (bash)
Implements many of the extra features found in csh, ksh
Command line completion Command line editing Command line history Sophisticated prompt control

30 January 2013

Unix System Administration & Programming

47

bash Heritage
C shell (csh) written by bill joy at UC Berkeley
Added many features such as command completion,

history and job control

Korn shell (ksh) written by David Korn at AT&T

Enhanced C shell (tcsh) a community effort

30 January 2013

Unix System Administration & Programming

48

bash Heritage-2
Bourne Again Shell (bash)
Implements many of the extra features found in csh, ksh
Command line completion Command line editing Command line history Sophisticated prompt control

30 January 2013

Unix System Administration & Programming

49

File Globbing
Globbing is wildcard expansion:
* matches zero or more characters
? matches any single character [a-z] matches a range of characters [^a-z] matches all except the range

Example

$ls /usr/bin/b* $ls /usr/bin/?b* $ls a[0-9] $ls [!Aa]*


30 January 2013 Unix System Administration & Programming 50

The Tab key


Type <TAB> key to complete command line:
For the command name, it will complete a command name
For an argument, it will complete a file name

Examaple:
slo<TAB> slocate ls myf<TAB>

30 January 2013

Unix System Administration & Programming

51

history
bash stores a history of commands youve entered, which can be used to repeat commands Use history command to see a list of remembered commands: $ history
Use bang character !

!x execute last command begin with x !2 execute command no 2 !! Execute Last command
30 January 2013 Unix System Administration & Programming 52

History-2
Use the up and down arrow keys to scroll through

previous commands Type <CTRL-R> to search for a command in command history


(reverse-i-search)`:

To recall last argument from previous command:

<ESC>. Or <ALT-.>

30 January 2013

Unix System Administration & Programming

53

Tilde
Tilde (~)
Refer to your home directory:

$ cat ~/.bashrc
May refer to another users home directory:

$ ls ~issa/.bashrc

30 January 2013

Unix System Administration & Programming

54

Variable and String


Parameter/Variable: ($)
Substitute the value of variable in a command line

$ cat $HOME/.bash_profile $ echo $PATH

Curly braces: { }
A string is created for every pattern inside the braces

regardless if any file exist $ rm hello.{c,o}

30 January 2013

Unix System Administration & Programming

55

Command and Math


Command output: `` or $()

substitute output from a command in a command line $ echo Hostname: `hostname` $ echo $(date)

Arithmetic: $[ ] Substitute result of arithmetic expression in a command line


$ echo Area: $[ $x * $y ]

30 January 2013

Unix System Administration & Programming

56

Protecting from 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:


$ echo This long string will be echoed \ back as one long line This long string will be echoed back as one long line

30 January 2013

Unix System Administration & Programming

57

Protecting from Expansion: Quotes


Single quotes () inhibit all expansion Double quotes () inhibit all expansion except

$ (dollar sign) including $() , ` (back tick), command substitution \ (backslash),single character inhibition

! (exclamation point), history substitution

30 January 2013

Unix System Administration & Programming

58

Command Editing Tricks


<Ctrl-a> move to beginning of line
<Ctrl-e> move cursor to end of line <Esc>f move cursor to beginning of next word

<Esc>b move cursor to beginning of previous or current word


<Ctrl-u> delete to beginning of line

<Ctrl-k> delete to end of line

30 January 2013

Unix System Administration & Programming

59

Command Editing Tricks Editing Modes


By default, bash uses emacs-style key bindings for command editing Type set o vi
to change key bindings to vi-style

Make vi the default by adding the command above to $HOME/.inputrc

30 January 2013

Unix System Administration & Programming

60

The bash Shell Variable


A variable is a label that has a value
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 set to display all variables env display environment variables

30 January 2013

Unix System Administration & Programming

61

Local Variable
Conventionally all upper-case Setting variable value
$ CREDIT=300 $ echo $CREDIT

Common Local Variables


HISTFILESIZE determine how many command to be saved

in the history file on logout COLUMN sets the width of the terminal LINES sets the height of terminals PS1 sets the prompt

30 January 2013

Unix System Administration & Programming

62

Aliases
Aliases let you create shortcuts to command

$ Alias dir=ls laF Use alias to see all set aliases To see alias value: $ alias dir $ alias dir=ls laF

30 January 2013

Unix System Administration & Programming

63

Environment variables
Shell variables exist only in current shell instance

Environment variables passed to subshells


Shell variables can be exported into environment

$ CREDIT=300; export CREDIT

30 January 2013

Unix System Administration & Programming

64

Common Environment variables


HOME Path to users home directory

LANG Identification of default language program should use


PWD Users current working directory

EDITOR default editor program


LESS options to pass to the command less TERM terminal type

30 January 2013

Unix System Administration & Programming

65

Environment variables
PATH colon separated list of locations where commands can be found which command (not variable) showing path of executable

$which ls Path to command can also be given explicitly $ /bin/ls /etc $ ./myls /etc

30 January 2013

Unix System Administration & Programming

66

Script of command executed at login

Shell Startup Script

Include:
Configure the shell by setting local variables Configure other program through environment variables

Establish aliases
Run program on startup

30 January 2013

Unix System Administration & Programming

67

Shell Startup Script


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 script

30 January 2013

Unix System Administration & Programming

68

Startup Script: order of execution


Login shells
/etc/profile

/etc/profile.d
~/.bash_profile

~.bashrc /etc/bashrc Non-Login shells ~.bashrc /etc/bashrc . /etc/profile.d


30 January 2013

Unix System Administration & Programming

69

/etc/profile System-wide startup script


Paresed by all users with Bourne-style shells, including bash and sh Usually sets default PATH variable, user limit, and other variables and settings bash only source (execute) /etc/profile if the shell is a login shell

30 January 2013

Unix System Administration & Programming

70

/etc/profile.d
Some application-specific startup script in this directory Script called by a for-loop in /etc/profile Script set up variables and run initialization procedures

30 January 2013

Unix System Administration & Programming

71

For user-specific settings

~/.bash_profile and ~.bashrc

Common to place variable setting, aliases

variables and run initialization procedures Commands that place output to the screen, such as the date command should go in .bash_profile not .bashrc

30 January 2013

Unix System Administration & Programming

72

~/.bash_logout
Execute 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

30 January 2013

Unix System Administration & Programming

73

Overview of vi and vim

vi: the visual editor, standard Linux and Unix editor vim: the vi improved standard Red Hat editor On Red hat operating systems, the vi command invokes vim Derived from earlier Unix editors
ed->ex->vi->vim

30 January 2013

Unix System Administration & Programming

74

Starting of vi and vim

To start vim : vi filename


If the file exists, the file is opened and the content are

displayed
If the file doesnt exists,

vi create it when the edits are

saved for the first time

30 January 2013

Unix System Administration & Programming

75

Three Modes of vi and vim


Command mode
Cursor movement
Change, delete, yank, put

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

Ex mode
Configuring, exiting, saving Search and replace

30 January 2013

Unix System Administration & Programming

76

Three Modes of vi and vim

command mode

i
insert mode

wrong command ESC

:
last line mode

Unix System Administration & Programming

30 January 2013

77

Cursor Movement Insert mode h left, j down , k up , l right


w word ahead, b word back Arrow keys also work a append after the cursor i insert before the cursor A append to end of line I insert at beginning of line o open a line below O open a line above
30 January 2013 Unix System Administration & Programming 78

Command mode
<esc> takes you from insert mode back to command mode cc change line, dd delete line, yy yank (copy) line

cw change word, dw delete word, yw yank word


Use p or P to put (paste) copied or deleted data

30 January 2013

Unix System Administration & Programming

79

Undoing Changes
u undo most recent change
U undo all changes to current line since the cursor landed on the line

<Ctrl-r> redo last undone change

30 January 2013

Unix System Administration & Programming

80

Searching for Text

/text search downwards for text ?text search Upwards for text n continue search in the same direction N continue search in the opposite direction

30 January 2013

Unix System Administration & Programming

81

Saving and Exiting: ex mode


:wq save changes
:q or q! abonden changes :w save without exit

:e! abandon changes without exit


:wq! Forcing save changes :w! Forcing save without exit

30 January 2013

Unix System Administration & Programming

82

A few tricks
5dd delete 5 line (a number can precede any of the tow character change, delete, yank, or put command
x delete a character

rc replace a character with c


R replace character for character until <esc>

30 January 2013

Unix System Administration & Programming

83

Users
Every user of the system is assigned a unique User ID number
Users names and uid are stored in /etc/passwd

User are assigned a home directory and a program that is run when they log in (usually shell)
Users cannot read, write or execute each others files without permission

30 January 2013

Unix System Administration & Programming

84

Groups
Users are assigned to groups with unique group ID number (gid) 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

30 January 2013

Unix System Administration & Programming

85

The root users


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

An unlimited capacity to damage the system! Normal (unprivileged) users potential to do damage is limited

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

30 January 2013

Unix System Administration & Programming

86

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

Authentication information is stored in plain text files:


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

30 January 2013

Unix System Administration & Programming

87

System Users and Groups


Server programs such as web or print servers typically run as unprivileged users, not as root
Examples: mail, lp, nobody

Running programs in this way limits the amount of damage any single program can do to the system

30 January 2013

Unix System Administration & Programming

88

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

30 January 2013

Unix System Administration & Programming

89

Users Information Commands


Find out who you are
whoami

Find out what groups you belong to


groups

Find out who is logged in


users, who, w

Login / reboot history


last

30 January 2013

Unix System Administration & Programming

90

The 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 Every process has an owner and group affiliation , and can only access the resources its owner or group can access
30 January 2013 Unix System Administration & Programming 91

Linux File Security


Every file and directory has permission set that determine who can access it Permission are set for:
The owner of the file (called user) The group members All others

Permissions that are set are called read, write and

execute permissions

30 January 2013

Unix System Administration & Programming

92

Linux Process Security


When a process accesses a file the user and group of the process are compared with the user and group of the file
If the user matches, the user permissions apply

If the group matches, but the user doesnt, the group

permissions apply If neither match, the other permission apply

30 January 2013

Unix System Administration & Programming

93

Examining Permissions
File permissions may be viewed using ls
$ ls l /bin/login
-rwxr-xr-x 1 root root 19080 Apr 1 18:26 /bin/login

File type and file access permission are symbolized by a 10-character string

30 January 2013

Unix System Administration & Programming

94

Using Categories
File permissions are established for each of three user categories. Each category also has a one-letter symbol:
u - the files owners (user)

g - other users in the files group


o everyone else (others)

30 January 2013

Unix System Administration & Programming

95

File Permission Types


Four symbols are used when displaying permission:
r : permission to read a file or list a directorys content w : permission to write to a file or create and remove

files from a directory x : permission to execute a program file or change into a directory and do long listing of the directory - : no permission (in place of the r, w, or x)

30 January 2013

Unix System Administration & Programming

96

Examining Directories
The first character in the long listing distinguish directories (d) from regular files (-)
$ ls l /bin
drwxr-xr-x 2 root root 4096 Apr 1 18:26 /bin/

$ ls l /bin/df -rwxr-xr-x 1 root

root

4096 Apr 1 18:26 /bin/df

Others file types indicators exist

30 January 2013

Unix System Administration & Programming

97

Examining User Permissions


Characters 2,3, and 4 in the access mode identify user permissions
$ ls l /bin/login
-rwxr-xr-x 1 root root 19080 Apr 1 18:26 /bin/login

This file can be read, written, and executed by its owner, and read and executed by everyone else

30 January 2013

Unix System Administration & Programming

98

Examining group Permissions


Characters 5,6, and 7 in the access mode identify group permissions
$ ls l README
-rw-rw-r-1 issa teachers 9080 Oct 1 8:26 README

This file can be read and written by people in group teachers, but it cannot be executed.

30 January 2013

Unix System Administration & Programming

99

Examining Other Permissions


Characters 8,9, and 10 in the access mode identify permissions for others:
$ ls l README
-rw-rw-r-1 issa teachers 9080 Oct 1 8:26 README

This file can be read by people outside group teachers, but it cannot be changed or executed

30 January 2013

Unix System Administration & Programming

100

Changing Permissions Symbolic method


To change access mode :
$ chmod [-R] mode file

Changes the access mode of files and directories

30 January 2013

Unix System Administration & Programming

101

Changing Permissions Numeric method


Uses a three-digit mode numbers
First digit specifies owners permission Second digit specifies group permission Third digit represents others permission

Permissions are calculated by adding :


4 (for read) 2 (for write) 1 (for execute)

Example
$ chmod 640 myfile
30 January 2013 Unix System Administration & Programming 102

File and directory permissions


Read and write for all is the default (666) for files and 777 for directory umask Can be used to withhold permission upon file creation

Non-privileged users umask is 0002 and the roots umask is 0022


File will have permissions of 664

Default Mode

umask New File Mode


30 January 2013

666 -002 664


103

Unix System Administration & Programming

Special Permissions
Special permissions: a fourth permission set (in addition to user/group/other),Applicable in four cases:
SUID (set user ID) for an executable, Processes are

granted access to system resources based on user who owns the file. SGID (set group ID) for an executable, Same with SUID except group is affected. SGID a directory: Files created in that directory will have their group set to the directory's group.
30 January 2013 Unix System Administration & Programming 104

Special Permissions
Sticky bit for a directory, If set on a directory, then a user

may only delete files that he owns or for which he has explicit write permission granted, even when he has write access to the directory. (e.g. /tmp )

30 January 2013

Unix System Administration & Programming

105

SUID Permissions
When you login, your login shell process values are your user ID and group ID
E.g., if you run passwd (owned by root), THE user ID is

your ID, not root; then how can it update /etc/passwd file owned by root ? SUID bit enables this functionality When an executable file with set user ID (SUID) permission is executed, command run with permission of the owner of the command, not executor of the command File permission of /bin/passwd is r-sr-sr-x
30 January 2013 Unix System Administration & Programming 106

Partitions and Filesystems


Disk drives are divided into partitions Partitions are formatted with filesystems alloying users to store data
Default filesystem: ext3, The Third Extended Linux

Filesystems Other common filesystems:


ext2 , msdos, FAT, NTFS Iso9660(typically used for CDs)

30 January 2013

Unix System Administration & Programming

107

Inode
An inode tables contains a list of all files in an ext2 or ext3 filesystem An inode (index nodes) is an entry in the table, containing information about a file (the metadata), including:
File type, permission, link count, UID, GID The files size and various time stamps

Pointers to the files data blocks on disk

One inode is associated with each file. The system uses inodes as the definition of a file.
30 January 2013 Unix System Administration & Programming 108

Directory
The computers reference for a file is the inode number
The human way to reference a file is by file name A directory is a mapping between the human name for the file and the computers inode number
5 4 7 2
30 January 2013 Unix System Administration & Programming

apples home .

oranges

109

cp and inode
The cp command:
allocates a free inode number, placing a new entry in

the inode table Create a directory entry, referencing the file human file name to the inode number Copies data into new file

30 January 2013

Unix System Administration & Programming

110

mv and inode
If the destination of the mv command is on the same file system as the source, the mv command:
Creates a new directory entry, with the news 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 the data on the disk: no data is moved!

30 January 2013

Unix System Administration & Programming

111

rm and inode
The rm command:
Decrement the link count, thus freeing the inode

number to be reused Places data bocks on the free list Removed the directory entry

Data is not actually removed, but will be overwritten when the data blocks are used by

another file

30 January 2013

Unix System Administration & Programming

112

Symbolic (or Soft) links


A symbolic link point to another file Ls l display the link name and the refenced file
$ ls l pf
lrwxrwxrwx 1 issa issa 11 Oct 1 8:26 ps/etc/passwd

File type: l for symbolic link

Syntax
ln s filename [linkname]

30 January 2013

Unix System Administration & Programming

113

Hard links
One physical file on the filesystem Each link references the files inode File is present in the filesystem as long as at least one link remain Cannot span drives or partitions Syntax
ln filename [linkname]

30 January 2013

Unix System Administration & Programming

114

The 7 Fundamental filetypes


d l b c p

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

30 January 2013

Unix System Administration & Programming

115

Checking Free Space


df report disk space usage
Report total kilobytes, kilobytes used, kilobytes

free per file system -h display sizes in easier to read units du report disk space usage Report kilobytes used per directory Includes subtotals for each subdirectory -s option only report single directory entry Also takes the -h option
30 January 2013 Unix System Administration & Programming 116

Find command
List files and pathnames matching a specific file name or criteria Syntax
find <dirs> [conditions] [action]

Recurses down in file tree in dirs Optionally executes command on each line of output

30 January 2013

Unix System Administration & Programming

117

Finding and processing Files


-exec action execute a command Syntax
find <dirs> [conditions] -exec cmd {} \;

Found files are passed to cmd as arguments


{} is a place holder for the filename

The escaped semicolon delimit the generated

commands

30 January 2013

Unix System Administration & Programming

118

Finding and processing Files


Search Option
-name filename size +n -amin +n

Description
Finds files with the given filename Finds files with the size n Finds files that were accessed n minutes ago

-mtime +n

Finds files that were modified n days ago

30 January 2013

Unix System Administration & Programming

119

Finding Examples
Example:
find . -print

find all files in the current directory (and all subdirectories) & display the results find . -name *.c -print find all files in the current directory (and all subdirectories) with the extension .c & display the results

30 January 2013

Unix System Administration & Programming

120

Finding Examples
find / -name myfile -print

This command tells the find command to look inside the / directory and every subdirectory to look for a file or directory with the name myfile and to display each match it finds This command tells the find command to look inside the current directory and every subdirectory to look for a file with name begin with foo that have been accessed in the last 10 minutes and to display each match it finds
Unix System Administration & Programming 121

find . -name foo* -amin -10 -print

30 January 2013

Finding Examples
Example: (for student)
find . -size +5000k find . -name "*.tmp" -exec rm {} \;

30 January 2013

Unix System Administration & Programming

122

Mounting Removable Media


Removable media must be mounted before the data can be accessed CD often will be mounted automatically Use the mount command to mount:
mount /media/floppy (for mount floppy)

Media must be unmounted before being removed


CDs:

eject Floppies: umount /media/floppy

30 January 2013

Unix System Administration & Programming

123

Formatting a floppy
One of the this command depending on which filesystem i want to use :
mkfs t ext2 /dev/fd0 mk2fs /dev/fd0 mkfs t vfat /dev/fd0 mformat a:

30 January 2013

Unix System Administration & Programming

124

Why Archive Files


Archiving places many files into one target file
Easier to back up, store, and transfer

tar -standard Linux archiving command Syntax


tar cvf archive_name files c create a new archive v produce verbose messages f archive_name is the name of new file

Options do need a leading dash


30 January 2013 Unix System Administration & Programming 125

Inspecting Archives
Syntax
tar tf archive_name.tar

tar

tvf archive_name.tar

First form display a list of all files in the archive

The v cause a long listing of each file in the archive

30 January 2013

Unix System Administration & Programming

126

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

30 January 2013

Unix System Administration & Programming

127

Why Use File Compression?


Results in smaller file size Text files can be compressed up to 75%

Binary files usually dont compress much


tar -archives are often compressed

30 January 2013

Unix System Administration & Programming

128

Compression Utilities
gzip,gunzip
Standard linux compression utility Up to 75% compression for text files

bzip2,bunzip2
newer linux compression utility Generally archives better compression than gzip

30 January 2013

Unix System Administration & Programming

129

Using Compression
Sample compression commands
gzip termcap gzip -v termcap gunzip -c termcap.gz | wc l gunzip termcap

30 January 2013

Unix System Administration & Programming

130

Compressing archives
Often tar archive are compress
tar can compress/uncompress archives Compression switches- use during creation and

extraction
z for gzip compression j for bzip2 compression

30 January 2013

Unix System Administration & Programming

131

TAR with Compression

file1

tar
.tar

gzip
.tar.gz

subdirectory

30 January 2013

Unix System Administration & Programming

132

tar to Unformatted Floppies


Floppies can used like tape drives
Low level format required File system not needed Use tar to write to the floppy /dev/fd0 is the destination Floppy cannot be mounted

Example:
$ tar czvf /dev/fd0 mydir

30 January 2013

Unix System Administration & Programming

133

Standard Input and Output


Linux provides three I/O channel to processes
Standard input (0): keyboard is default

Standard output (1): terminal windows is default


Standard error (2) : terminal windows is default

30 January 2013

Unix System Administration & Programming

134

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

30 January 2013

Unix System Administration & Programming

135

Redirecting Output
In order to study redirecting standard output, and error , we will use the find command.
find /etc name passwd

This command will search for all files named passwd in /etc and its subdirectory
By default both the standard output and standard

error are displayed on the screen.

30 January 2013

Unix System Administration & Programming

136

Redirecting Standard Output


Redirecting standard output with >
find /etc name passwd > findresult

standard error is still displayed on the screen.

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 >

30 January 2013

Unix System Administration & Programming

137

Redirecting Standard Error


Redirect standard error with 2>
Example : redirect standard error to a file:
find /etc name passwd 2> finderrors

Standard outputs still displayed on the screen.


To append data to an existing file, use 2>> to redirect instead of 2>

30 January 2013

Unix System Administration & Programming

138

Redirecting Both Standard Redirection of standard output and standard error Output and Error
can be performed simultaneously
find /etc name passwd 2> error > result

Each I/O channel can be redirect ed to different files, or the same file:
find /etc name passwd > alloutput 2>&1 Or find /etc name passwd &> alloutput

30 January 2013

Unix System Administration & Programming

139

Redirecting Input
Redirect standard Input with <
Some commands only operate on standard input
tr doesnt accept filenames as arguments- it require its

input to be redirected from somewhere Tr A-Z a-z < .bash_profile This command will translate the uppercase character in bash_profile to lowercase

30 January 2013

Unix System Administration & Programming

140

Using pipes To connect processes


Pipes (the | character) let you redirect output from on command to become input to another command
ls /usr/lib | less

The mail command takes the message contents as standard input


ls l | mail s here the listing issa

Can create pipelines a powerful feature of linux


cut f

-d: passwd | sort -r | less


Unix System Administration & Programming 141

30 January 2013

tee
Let you tee a pipe: redirect output to a file while still piping it to another program
set | tee set.out | less Output from set command is written to file set.out while

also being piped to less

30 January 2013

Unix System Administration & Programming

142

tee
Let writing to output and simultaneously
date | tee file1 file2

Thu Jun 10 11:10:34 CEST 2007 cat file1 Thu Jun 10 11:10:34 CEST 2007 cat file2 Thu Jun 10 11:10:34 CEST 2007 uptime | tee -a file2 11:10:51 up 21 days, 21:21, 57 users, load average: 0.04, 0.16, 0.26 cat file2 Thu Jun 10 11:10:34 CEST 2007 11:10:51 up 21 days, 21:21, 57 users, load average: 0.04, 0.16, 0.26
30 January 2013 Unix System Administration & Programming 143

String Processing Tools head few line (default : 10 head command display first
lines) of text in a file
head /tmp/output.txt

Use n or --lines parameter to change number of line displayed


head n 20 /tmp/output.txt

30 January 2013

Unix System Administration & Programming

144

String Processing Tools tail command displaytail few line (default : 10 last
lines) of text in a file
tail

/etc/passwd

Use n or --lines parameter to change number of line displayed


tail n 5 /etc/passwd

Use f to follow the end of a text file as it changes


tail f 5 make.out

Used to watch log files


30 January 2013 Unix System Administration & Programming 145

word count also counts lines and characters


wc

story.txt 39 237 1901 story.txt

Use l for only line count


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

String Processing Tools wc


30 January 2013 Unix System Administration & Programming 146

String Processing Tools sort Sort text to stout original file unchanged
sort

[option]

file(s)

Common option:
-r -n -f -u -t -k Reverse sort to sort descending Numeric sort Ignore case of characters in strings unique (remove duplicate lines in output) x Use x as field separator pos1 sort from field pos1
Unix System Administration & Programming 147

30 January 2013

$ cat b
fish dog animal Bird

String Processing Tools sort

$ sort -r b
fish dog bird animal
30 January 2013 Unix System Administration & Programming 148

String Processing Tools sort Sort numeric file as text


$ cat c
5412 this line should go last 998 this line should go second 50 this line should go first 999 this line should go third

$ sort c
50 this line should go first 5412 this line should go last 998 this line should go second 999 this line should go third

30 January 2013

Unix System Administration & Programming

149

String Processing Tools sort Sort numeric file as numbers


$ cat c
5412 this line should go last 998 this line should go second 50 this line should go first 999 this line should go third

$ sort -n

50 this line should go first 998 this line should go second 999 this line should go third 5412 this line should go last
30 January 2013 Unix System Administration & Programming 150

String Processing Tools uniq


Remove successive, duplicate lines in a file Can use in conjunction with sort to remove all duplicated (or use sort u) Use c to count number of occurrences of duplicate data

30 January 2013

Unix System Administration & Programming

151

String Processing Tools uniq


Just sorted:
$ cat a | sort
bird bird dog dog fish fish fly

sort | uniq:
$ cat a | sort | uniq
bird dog fish fly

30 January 2013

Unix System Administration & Programming

152

String Processing Tools Display specific column of file data


cut f4 result.dat

Common option:
-f

cut

Specifies field or column -d Specifies field delimiter (default is TAB) cut f3 d: /etc/passwd -c Cuts by characters cut c2-5 /usr/share/dict/words
30 January 2013 Unix System Administration & Programming 153

Example output Processing Tools String second word on each line:


Delimiter: space Fields: 2

cut

$ cat a
the quick brown fox jumped over a quick brown fox

$ cut -f 2 -d ' ' a


quick over

30 January 2013

Unix System Administration & Programming

154

Example output Processing 5, 7-end String characters 1-3, Tools


$ cat a

cut

the quick brown fox jumped over a quick brown fox

$ cut -c 1-3,5,7- a
theqick brown fox jume over a quick brown fox

30 January 2013

Unix System Administration & Programming

155

String Processing tr -character translator


tr A-Z a-z

Tools

tr, paste < .bash_profile

This command will translate the uppercase character in

bash_profile to lowercase

paste - merge line of files


Paste file1 file2
30 January 2013 Unix System Administration & Programming 156

Version for differences Compares two filesComparison


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

with

diff

30 January 2013

Unix System Administration & Programming

157

Interactive spell-checker
aspell

Easy way to check spelling in a file


check letter.txt

Spell checking with aspell

Can create personal dictionaries

30 January 2013

Unix System Administration & Programming

158

Regular Expressions
You can use and even administer Linux systems

without understanding regular expressions but you will be doing things the hard way Regular expressions are endemic to Unix
vi, ed, sed, and emacs
Awk, Tcl, Perl and Python grep, egrep, fgrep

30 January 2013

Unix System Administration & Programming

159

So What Is a Regular Expression?


A regular expression is simply a description of a pattern that describes a set of possible characters in an input string Weve already seen some simple examples of regular expressions (known as regex from here on)
In vi when searching :/c[aou]t searches for cat, cot, or

cut In the shell


ls *.txt cat chapter? cp Week[1234].pdf /home/clyde/krf


Unix System Administration & Programming 160

30 January 2013

Downside of Regular Expressions


There is considerable variation from utility to

utility
The shell is limited to fairly simple metacharacter

substitution (*,?, []) and doesnt really support regex Regex in vi are also fairly limited Regex in sed are not exactly the same as regex in Perl, or Awk, or grep, or egrep

This puts the onus on the user to examine the man

page or other documentation for these utilities to determine which flavor of regex are supported
30 January 2013 Unix System Administration & Programming 161

So How Do We Build a Regex?


The simplest regex is a normal character
c , for example, will match a c anywhere while an a will

do the same for an a

The next thing is a . (period)


This will match any single occurrence of any character

except a newline For example . will match a z or an e or a ? or even another . w.n will match win, wan, won, wen, wmn, went, and wanton as well as w*n and w9n
30 January 2013 Unix System Administration & Programming 162

Protecting Regex Metacharacters


Since many of the special characters used in regexs also have special meaning to the shell, its a good idea to get in the habit of single quoting your regexs
This will protect any special characters from being

operated on by the shell If you habitually do it, you wont have to worry about when it is necessary

30 January 2013

Unix System Administration & Programming

163

Multiple Occurrences in a Pattern


The * (asterisk or star) is used to define zero or

more occurrences of the single character preceding it


abc*d will match abd, abcd, abccd, abcccd, or even

abcccccccccccccccccccccccccccccccccccd Note the difference between the * in a regex and the shells usage

In a regex, a * only stands for zero or more occurrences of a single preceding character, In the shell, the * stands for any number of characters that may or may not be different

30 January 2013

Unix System Administration & Programming

164

Specifying Begin or End of Line


The ^ specifies the beginning of a line
^The then will match any The that are the first

characters on a line

The $ matches the end of line


well$ will match well only if they are the last

characters on a line prior to the NEWLINE character Note that well (notice the space at the end) would NOT match well$

^Ken$ would only match a line that started with Ken and then had no other characters on the line
30 January 2013 Unix System Administration & Programming

165

Character Classes [ ]
The square brackets [ ] are used to define character

classes
[aA]wk will match awk or Awk

Ranges can also be specified in character classes


[1-9] is the same as [123456789] [abcde] is equivalent to [a-e] You can also combine multiple ranges

[abcde123456789] is equivalent to [a-e1-9]

Note that the - character has a special meaning in a

character class BUT ONLY if it is used within a range, [123] would match the characters -, 1, 2, or 3
30 January 2013 Unix System Administration & Programming 166

Negating a Character Class


The ^, when used as the first character in a character class definition, serves to negate the definition
For example [^aeiou] matches any character except a,

e, I, o, or u Used anywhere else within a character class, the ^ simply stands for a ^

[ab^&] would match a a, b, ^, or &

Note also that within a character class, the ^ does not

stand for beginning of line


30 January 2013 Unix System Administration & Programming 167

Escaping Special Characters


Even though we are single quoting our regexs so the shell wont interpret the special characters, sometimes we still want to use a special character as itself To do this, we escape the character with a \ (backslash) Suppose we want to search for the character sequence 8*9*
Unless we do something special, this will match zero or

30 January 2013

more 8s followed by zero or more 9s, not what we want 8\*9\* will fix this - now the asterisks are treated as regular characters
Unix System Administration & Programming

168

Reading a Regex
If you get in the habit of literally reading a regex, it

will be much easier for you to determine what one does


^Ken could be read as matching the word Ken at the

beginning of a line A better way to read it is the beginning of a line followed by a capital K followed by an e followed by an n ^corn$ would be read as the beginning of a line followed immediately by a c followed by an o followed by an r followed by an n followed immediately by a NEWLINE
30 January 2013 Unix System Administration & Programming 169

Alternation
Regex also provides an alternation character ( | )

for matching one or another subexpression


(K|T)en will match Ken or Ten ^(From|Subject): will match the From and Subject lines

of a typical email message

It matches a beginning of line followed by either the characters From or Subject followed by a :

The parenthesis ( ) are used to limit the scope of

the alternation
At(ten|nine)tion then matches Attention or

Atninetion, not Atten or ninetion as would happen without the parenthesis - Atten|ninetion
30 January 2013 Unix System Administration & Programming 170

Optional Items
The ? (question mark) specifies an optional character, the single character that immediately precedes it
For example, if I am looking for the month of July, it may

be specified a July or Jul I could use (July|Jul) to search or I could use July?

30 January 2013

Unix System Administration & Programming

171

Repetition
The * (asterisk or star) has already been seen to specify zero or more occurrences of the immediately preceding character
+ (plus) means one or more
abc+d will match abcd, abccd, or abccccccd but will

not match abd while abc?d will match abd and abcd but not abccd

30 January 2013

Unix System Administration & Programming

172

Regex Summary
Character Name . [] [^] ^ $ \< \> ? * + {n,m} | () \1, \2,
30 January 2013

Meaning any one character any character listed any character not listed position at start of line position at end of line position at beginning of word position at end of word matches optional preceding character matches zero or more occurrences matches one or more occurrences matches m to n occurrences matches either expression it separates limits scope of | or encloses subexpressions for backreferencing Matches text previously matched within first, second, etc set of parenthesis
173

dot character class negated character class caret dollar backslash less-than backslash greater-than question mark asterisk or star plus sign n to m bar, or parenthesis backreference

Unix System Administration & Programming

grep
grep comes from the ed search command global

regular expression print or g\re\p This was such a useful command that it was written as a standalone utility There are two other variants, egrep and fgrep that comprise the grep family grep is the answer to the moments where you know you want a the file that contains a specific phrase but you cant remember its name
30 January 2013 Unix System Administration & Programming

174

grep Family
Syntax
grep [-hilnw] [-e expression] [filename] egrep [-hiln] [-e expression] [-f filename] [expression] [filename] fgrep [-hilnx] [-e string] [-f filename] [string] [filename] -h - Do not display filenames -i - Ignore case -l - List only filenames containing matching lines -n - Precede each matching line with its line number -w - Search for the expression as a word (grep only) -x - Match whole line only (fgrep only)
30 January 2013 Unix System Administration & Programming 175

Family Differences
grep - uses regular expressions for pattern

matching fgrep - file grep, does not use regular expressions, only matches fixed strings but can get search strings from a file egrep - exponential grep, uses a more powerful set of regular expressions

30 January 2013

Unix System Administration & Programming

176

Regex in the grep Family


The following one-character regexs match a single character
c - an ordinary character \c - an escaped special character . * [ \ ^ $ A \ followed by < > ( ) { or } A . (period) [string] any single character contained within the

brackets

30 January 2013

Unix System Administration & Programming

177

Regexs for grep and egrep


Regular Expression
c \m ^ $ . [xyz] [a-z] [^] \n r* r+ r? r1r2 r1|r2 \(r\) (r)

Meaning
Normal (nonmeta) character Escape a character Start of Line End of Line Any single character except NEWLINE Any of x, y, z, Range Any single character not listed The nth tagged expression Zero or more r's One or more r's (egrep only) Zero or one r's (egrep only) Concatenation: r1 followed by r2 r1 or r2 (egrep only) Tagged regular expression r (grep only) Regular expression r

30 January 2013

Unix System Administration & Programming

178

grep Family Expressions


Regular Expression Matches x xyz \m ^ $ . [xy^$x] [^xy^$z] [a-z] [^a-z] r* r1r2
30 January 2013

grep, fgrep, and egrep Ordinary characters match themselves (NEWLINES and metacharacters excluded) Ordinary strings match themselves grep and egrep Matches literal character m Start of line End of line Any single character Any of x, y, ^, $, or z Any one character other than x, y, ^, $, or z Any single character in given range Any single character not in range zero or more occurrences of regex r Matches r1 followed by r2
Unix System Administration & Programming 179

sed command
sed stream editor A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). Replace some substring with another
$ cat a
bird barks mouse runs

$ sed 's/barks/flies/' < a


bird flies mouse runs
30 January 2013 Unix System Administration & Programming 180

sed command
Replace some characters with others
Replacing b with Q, i with X

$ cat a
bird barks mouse runs

$ cat a | sed 'y/bi/QX/'


QXrd Qarks mouse runs

30 January 2013

Unix System Administration & Programming

181

Advanced example
Calculate number of hits per remote host in Apache log file, most active hosts first.
Line format:
159.148.123.123 - - [28/Oct/2004:18:11:36 +0300] "GET /somefolder/file.php HTTP/1.1" 200 127602 "-" "Opera/7.54 (X11; Linux i686; U) [en]"

30 January 2013

Unix System Administration & Programming

182

Advanced example
$ cat access_log | cut -d ' ' -f 1 | sort | uniq -c | sort n r
First, cut out the host part (1st field), sort it get the number of repeated lines before the line (uniq c :

prefix lines by the number of occurrences), sort it numerically, reversed so that largest number comes first
Output:
348698 123485 12313 159.148.111.222 159.148.48.54 80.123.123.4

30 January 2013

Unix System Administration & Programming

183

Processes
Operating systems are about managing resources
Files are about storage (and I/O) resources
Processes are about CPU resources

Definition :A process is a single program running in its own

virtual address space, it receives a share (or time slice) of the CPU Processes and commands are not the same

A simple command (ls) generates a single process complex commands or scripts can invoke several simultaneous executing processes

30 January 2013

Unix System Administration & Programming

184

Process types
Interactive
initiated and controlled by terminal session can accept input from user as it runs can output results to the terminal Linux has job control to manage processes

Daemons
Typically started by init process

30 January 2013

Unix System Administration & Programming

185

Job control for interactive processes


foreground process
by default interactive processes run in the foreground

and the shell must wait until they complete only one process can be running in the foreground for each user. When a command is entered, thye shell will not process further input until the process is complete and the shell prompt is redisplayed But Remember that Linux is a multi-user system foreground and background relate to user sessions. Hence multiple foreground processes can be running.

30 January 2013

Unix System Administration & Programming

186

Job control for interactive processes


Background process
if a process has no output to terminal and will take

some time to run, rather than waiting, it can be run as a background process Once started in the background control returns immediately to the shell. a user can initiate multiple simultaneous background processes with the bash shell following a command with an & places in the background eg makewhatis &
Unix System Administration & Programming 187

30 January 2013

Job control for interactive processes


The jobs command show what process are suspended or running in the background Moving a process from the foreground to the background takes two steps
A process running in the foreground can be suspended

( ctrl-Z) A suspended process can be placed in the background (use bg command)

One suspended or background process can be brought to the foreground (fg command)
30 January 2013 Unix System Administration & Programming 188

Job control for interactive processes


Command regular_command command & jobs Meaning Runs this command in the foreground. Run this command in the background Show commands running in the background. Suspend (stop, but not quit) a process running in the foreground (suspend). Interrupt (terminate and quit) a process running in the foreground. Every process running in the background gets a number assigned to it. Reactivate a suspended program in the background. Puts the job back in the foreground. End a process
Unix System Administration & Programming 189

Ctrl+Z
Ctrl+C %n bg %n fg %n kill
30 January 2013

Compound Commands
List of commands seperated by semi-colons List inside () to run inside a subshell
$ ( cd /usr; du )

30 January 2013

Unix System Administration & Programming

190

Daemons
server process running in the background often started at boot time offer service to other processes examples
ftpd - file transfer process daemon cron daemon - scheduling daemon httpd - web server

30 January 2013

Unix System Administration & Programming

191

Process Attributes
the ps and top commands can be used to look at current processes
PID - process ID : each process has a unique ID PPID parent process ID : The process that start a process

nice value - priority (-20 highest to 19 = lowest)


TTY associated terminal (TTY teletype terminal)

30 January 2013

Unix System Administration & Programming

192

Process Attributes
Displaying process information
ps

PID 4245 5314

TTY pts/7 pts/7

TIME 00:00:00 00:00:00

CMD bash ps

ps aux or ps auxw options most commonly used ps -ef | grep username top (process monitoring) uptime pstree
30 January 2013 Unix System Administration & Programming 193

Process States
process can be in one of many states
R runnable (on the run queue) S sleeping T - stopped D uninterupptible sleep Z defunt (zombie) process

30 January 2013

Unix System Administration & Programming

194

Process life cycle


On startup a single process (called init, with PID 1) is created All subsequent process are derived from by the fork-exec mechanism
When a process forks it creates an exact copy of itself

called the child processes The exec system call them places the image of the new process over the copy before allowing it to be scheduled to run when a process dies (finishes or is killed) it sends a signal to its parent
30 January 2013 Unix System Administration & Programming 195

Illustration of Process Control Calls

30 January 2013

Unix System Administration & Programming

196

Process Control
A process is a program in execution. Processes are identified by their process identifier, an

integer. Process control system calls


fork creates a new process execve is used after a fork to replace on of the two processess

virtual memory space with a new program exit terminates a process A parent may wait for a child process to terminate; wait provides the process id of a terminated child so that the parent can tell which child terminated. wait3 allows the parent to collect performance statistics about the child

A zombie process results when the parent of a defunct

child process exits before the terminated child.


30 January 2013 Unix System Administration & Programming 197

Process Control (Cont.)


Processes communicate via pipes; queues of bytes between two processes that are accessed by a file descriptor. All user processes are descendants of one original

process, init. init forks a getty process: initializes terminal line parameters and passes the users login name to login.
login sets the numeric user identifier of the process to

that of the user executes a shell which forks subprocesses for user commands.
30 January 2013 Unix System Administration & Programming 198

Signals
Facility for handling exceptional conditions

similar to software interrupts Most desirable way to end a process is to let it end normaly The interrupt signal, SIGINT, is used to stop a command before that command completes (usually produced by ^C) If all else fail, send a kill signal

30 January 2013

Unix System Administration & Programming

199

Signals
Signal name number SIGTERM 15 Meaning Terminate the process in an orderly way. Interrupt the process. A process can ignore this signal. Interrupt the process. A process can not ignore this signal. For daemons: reread the configuration file.

SIGINT
SIGKILL

2
9

SIGHUP

30 January 2013

Unix System Administration & Programming

200

Sending Signals to Process


Syntax
kill

[-signal] pid(s) kill [-signal] %jobID Send the specified signal to a process Default signal is TERM killall kill -l list all available signal

30 January 2013

Unix System Administration & Programming

201

Altering Process Scheduling Priority


At process invocation time
Syntax nice
[-n adjustment] command

Process are scheduled with a defult pririty of 0

Priority value range (-20 highest to 19 = lowest)


renice change the priority of a running process
renice

# [ [-p|-g ] PID] [[-u] user]

# is the priority value Once the priority value is raised, a non-privileged user cannot lower it
30 January 2013 Unix System Administration & Programming 202

Linux Process Hierarchy


[0] /etc/inittab init [1]

Daemon e.g. httpd

Login shell

Child

Child

Child

Grandchild

Grandchild

30 January 2013

Unix System Administration & Programming

203

pstree

Kernel threads, not real processes Forked processes for network connections
30 January 2013 Unix System Administration & Programming 204

Scheduling processes 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

30 January 2013

Unix System Administration & Programming

205

Scheduling processes-cron
Many aspects of system administration require

things to be done on a routine basis


Rotating logs building help files checking disk space

checking permissions

Linux provides scheduling mechanism refereed to as cron. Cron has two parts

Daemon - crond table of actions /etc/crontab

30 January 2013

Unix System Administration & Programming

206

Cron
the crond Daemon is started at boot time the daemon wakes up every minute to check its table of actions
if their is something to do -> run command

if nothing to do --> go back to sleep for 1 min

Cron table is a list (time,commnd) pairs. The format is


minute hour day month dayofweek command

30 January 2013

Unix System Administration & Programming

207

Crontab
Commands can be scheduled by
minute (0 59) Hour ( 0 to 23) Day of the month (1 - 31) Month ( 1 to 12) Day of the week (0=Sunday 6 = sat, or use

mon,tues,wed)

* Matches everything 1-3 Matches range 1,5 Matches Series


30 January 2013 Unix System Administration & Programming 208

Crontab
Example
01 * * * * command1 # hourly at 1 minute past * 1 * * * command2 # daily at 1 am 04 1 1-5 * * command3 # run at 4 minute past 1 each day

30 January 2013

Unix System Administration & Programming

209

Cron
Under Redhat Linux the cron table is used to execute a set of commands in some special directories
/etc/cron.hourly /etc/cron.daily contains logrotate, makewhatis,slocate,tmpwatch /etc/cron.weekly /etc/cron.monthly You can add you own commands to the appropriate directory, but remember they need to be batch commands as they will run automatically

30 January 2013

Unix System Administration & Programming

210

crontab commands
crontab l List crontab e Edit crontab l > cronfile crontab cronfile administrator can explicitly give permission to specific users or exclude specific user by entering their user identification in :
/etc/cron.d/cron.allow
/etc/cron.d/cron.deny

30 January 2013

Unix System Administration & Programming

211

Common Uses for CRON


Cleaning the filesystem Distribution of config files Rotating log files Backups

30 January 2013

Unix System Administration & Programming

212

Shell scripts
Everything that can be called from command line, can also be called from shell script Shell Script is series of commands written in plain text file. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file. Interpreted line by line The same effect when entering the lines one by one in interactive shell
30 January 2013 Unix System Administration & Programming 213

Running a shell script


As a parameter to shell interpreter

bash some_script.sh First line: #!/bin/bash


Make it executable (chmod +x some_script.sh) ./some_script.sh

Specifying interpreter on first line

30 January 2013

Unix System Administration & Programming

214

Debugging Shell Scripts


Display each command before it runs the command
Set the x option for the current shell

$set x $bash x script.sh set x or #!/bin/bash -x

Use the x to invoke the script

Add the set command at the top of the script

Then each command that the script executes is preceded

by a plus sign (+)


Distinguish the output of trace from any output that the script

produces

Turn off the debug with set +x


30 January 2013 Unix System Administration & Programming 215

Sample shell script


#!/bin/bash # comment line echo "what a fine day: " date
Interpreter to be used

Regular commands to execute

Output, when called by ./test.sh: what a fine day:

Thu Oct 28 23:37:39 EEST 2004

30 January 2013

Unix System Administration & Programming

216

Variables
Sample hello world with variables
#!/bin/bash STR="Hello World!" echo $STR

When assigning, no $ is used When getting the contents use $ No data types string, number, character, all the same No declaring, just assign
30 January 2013 Unix System Administration & Programming 217

Another sample shell script


EXAMPLE: backissa.sh

#!/bin/bash DATE=`date +%Y%m%d` WHAT='/home/issa' DEST="$DATE.tgz" tar cvzf $DEST $WHAT

Results in calling:
tar cvzf 20071028.tgz /home/issa
30 January 2013 Unix System Administration & Programming 218

Command line arguments


Automatically defined variables
$0 contains shell script name $1 contains first argument $2 2nd $* - contains all arguments as a string

30 January 2013

Unix System Administration & Programming

219

Arithmetic expansion
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. The format for arithmetic expansion is:
$((expression))

A=5 B=4 C=$(($A*$B)) echo $C

30 January 2013

Unix System Administration & Programming

220

Control structures
if then for in while until case break and continue

30 January 2013

Unix System Administration & Programming

221

if then
Structure
if test-command then commands fi

Example:
if test $word1 = $word2 then echo Match fi
30 January 2013 Unix System Administration & Programming 222

test
Command test is a built-in command Syntax
test expression [ expression ]
The test command evaluate an expression Returns a condition code indicating that the expression is either

true (0) or false (not 0)

Argument
Expression contains one or more criteria

Logical AND operator to separate two criteria: -a Logical OR operator to separate two criteria: -o Negate any criterion: ! Group criteria with parentheses

Separate each element with a SPACE


30 January 2013 Unix System Administration & Programming 223

Test Criteria
Relop -gt -ge -eq -ne -le -lt Description Greater than Greater than or equal to Equal to Not euqal to Less than or equal to Less than

30 January 2013

Unix System Administration & Programming

224

Example
Create a shell script to check there is at least one parameter
Something like this:

if test $# -eq 0 then echo you must supply at least one arguments exit 1 fi
30 January 2013 Unix System Administration & Programming 225

Test Criteria
Option
-d filename -f filename -r filename -s filename -u filename

Test Performed on file


Exists and is a directory file Exists and is a regular file Exists and it readable Exists and has a length greater than 0 Exists and has setuid bit set

-w filename
-x filename

Exists and it writable


Exists and it is executable

30 January 2013

Unix System Administration & Programming

226

Example
Check weather or not the parameter is a non-zero readable file name
Continue with the previous script and add something

like
if [ -r $filename a s $filename ] then fi

30 January 2013

Unix System Administration & Programming

227

Test Criteria
String testing
Criteria String -n string -z string String1 = string2 String1 != string2 meaning True if string is not the null string True if string has a length greater than zero True if string has a length of zero True if string1 is equal to string2 True if string1 is not equal to string2

30 January 2013

Unix System Administration & Programming

228

Example
Check users confirmation
First, read user input echo -n Please confirm: [Yes | No] read user_input Then, compare it with standard answer yes if [ $user_input = Yes ] then echo Thanks for your confirmation! fi What will happen if no around $user_input and user

just typed return?


30 January 2013 Unix System Administration & Programming 229

ifthenelse
Structure
if test-command then commands else commands fi

You can use semicolon (;) ends a command the same way a NEWLINE does.
if [ ]; then i if [ 5 = 5 ]; then echo "equal"; fi
30 January 2013 Unix System Administration & Programming 230

ifthenelif
Structure
if test-command then commands elif test-command then commands . . . else commands fi
30 January 2013 Unix System Administration & Programming 231

for
Structure
for loop-index do commands done
Automatically takes on the value of each of command

line arguments, one at a time. Which implies


for arg in $@

30 January 2013

Unix System Administration & Programming

232

Example
Structure
for loop-index in argument_list do commands done

Example:
for file in * do if [ -d $file ]; then echo $file fi done
30 January 2013 Unix System Administration & Programming 233

Example-2
for i in 1 2 3 4 5 do echo "Welcome $i times" done

30 January 2013

Unix System Administration & Programming

234

Example-3
for (( expr1; expr2; expr3 )) do repeat while expr2 is true done
for (( i = 0 ; i <= 5; i++ do echo "Welcome $i times" done ))

30 January 2013

Unix System Administration & Programming

235

Example-4
You can use for together with file name expansion to do some action for several files
#!/bin/bash for x in *txt; do cat $x done;

30 January 2013

Unix System Administration & Programming

236

while
Structure
while test_command do commands done

Example:
while [ $number lt 10 ] do number=`expr $number + 1` done
30 January 2013 Unix System Administration & Programming 237

until
Structure
until test_command do commands done

Example:
secretname=jenny name=noname until [ $name = $secretname ] do echo Your guess: \c read name done
30 January 2013 Unix System Administration & Programming 238

break and continue


Interrupt for, while or until loop The break statement
transfer control to the statement AFTER the done

statement terminate execution of the loop

The continue statement


Transfer control to the statement TO the done

statement Skip the test statements for the current iteration Continues execution of the loop
30 January 2013 Unix System Administration & Programming

239

Example
for index in 1 2 3 4 5 6 7 8 9 10 do if [ $index le 3 ]; then echo continue continue fi echo $index if [ $index ge 8 ]; then echo break break fi done

30 January 2013

Unix System Administration & Programming

240

case
Structure
case test_string in pattern-1 ) commands_1 ;; pattern-2 ) commands_2 ;; esac

default case: catch all pattern *)


30 January 2013 Unix System Administration & Programming 241

case
Special characters used in patterns
Pattern * ? [] | Matches Matches any string of characters. Matches any single character. Defines a character class. A hyphen specifies a range of characters Separates alternative choices that satisfy a particular branch of the case structure

30 January 2013

Unix System Administration & Programming

242

Example
#!/bin/bash echo \n Command MENU\n echo a. Current data and time echo b. Users currently logged in echo c. Name of the working directory\n echo Enter a,b, or c: \c read answer echo

30 January 2013

Unix System Administration & Programming

243

case $answer in Example-continue a) date ;; b) who ;; c) pwd ;; *) echo There is no selection: $answer ;; esac

30 January 2013

Unix System Administration & Programming

244

echo and read


The backslash quoted characters in echo
\c suppress the new line
\n new line \r return \t tab

Read
read variable1 [variable2 ] Read one line of standard input Assign each word to the corresponding variable, with the leftover words assigned to last variables If only one variable is specified, the entire line will be assigned to that variable.
30 January 2013 Unix System Administration & Programming 245

Built-in: exec
Execute a command:
Syntax: exec command argument Run a command without creating a new process

Run a command in the environment of the original process Exec does not return control to the original program Exec can be the used only with the last command that you want to run in a script Example, run the following command in your current shell, what will happen? $exec who

30 January 2013

Unix System Administration & Programming

246

Built-in: exec
Redirect standard output, input or error of a shell script from within the script

exec < infile exec > outfile 2> errfile

Example: $ more redirect.sh exec > /dev/tty echo "this is a test of redirection"

$ ./redirect.sh 1 > /dev/null 2 >& 1 this is a test of redirection


30 January 2013 Unix System Administration & Programming 247

Catch a signal: builtin trap


Built-in trap
Syntax: trap commands signal-numbers
Shell executes the commands when it catches one of the signals Then resumes executing the script where it left off.

Just capture the signal, not doing anything with it trap signal_number

Often used to clean up temp files Signals


SIGHUP SIGINT SIGKILL SIGTERM SIGSTP

1 2 9 15 24

disconnect line control-c kill with -9 default kill control-z

30 January 2013

Unix System Administration & Programming

248

Example
$ cat inter #!/bin/bash trap 'echo PROGRAM INTERRUPTED' 2 while true do echo "programming running." sleep 1 done

30 January 2013

Unix System Administration & Programming

249

A partial list of built-in


bg, fg, jobs
break, continue
cd, pwd echo, read

eval
exec exit export, unset test

job control change the loop working directory display/read scan and evaluate the command execute a program exit from current shell export/ remove a val or fun compare arguments

30 January 2013

Unix System Administration & Programming

250

A partial list of builtin


kill

set
shift times

trap
type

sends a signal to a process or job sets flag or argument promotes each command line argument displays total times for the current shell traps a signal show whether linux command, build-in, file creation mask waits for a process to terminate. print the value of one or more resource limits
Unix System Administration & Programming 251

function umask wait ulimit


30 January 2013

functions
A shell function is similar to a shell script
It stores a series of commands for execution at a later time. The shell stores functions in the memory Shell executes a shell function in the same shell that called it.

Where to define
In .profile In your script Or in command line

Remove a function
Use unset built-in

30 January 2013

Unix System Administration & Programming

252

functions
Syntax

function_name() { commands }

30 January 2013

Unix System Administration & Programming

253

Example
$ whoson() >{ > date > echo "users currently logged on" > who } $ whoson Tue Feb 1 23:28:44 EST 2005 users currently logged on issa :0 Jan 31 08:46 issa pts/1 Jan 31 08:54 (:0.0) issa pts/2 Jan 31 09:02 (:0.0)
30 January 2013 Unix System Administration & Programming 254

$ more .profile setenv() { if [ $# -eq 2 ] then eval $1=$2 export $1 else echo "usage: setenv NAME VALUE" 1>&2 fi } $ . .profile $ setenv T_LIBRARY /usr/local/t $ echo $T_LIBRARY /usr/local/t
30 January 2013 Unix System Administration & Programming 255

Example-2

Boot Terminology
Loader:
Program that moves bits from disk (usually)

to memory and then transfers CPU control to the newly loaded bits (executable).

Bootloader / Bootstrap:
Program that loads the first program (the kernel).

Boot PROM / PROM Monitor / BIOS:


Persistent code that is already loaded on power-up.

Boot Manager:
Program that lets you choose the first program to load.

30 January 2013

Unix System Administration & Programming

256

Boot Sequence overview


Bios Initialization Boot Loader (LILO,GRUB) Kernel initialization init start and enters desired runlevel by executing:
/etc/rc.d/rc.sysinit /etc/rc.d/rc and /etc/rc.d/rc?.d/ /etc/rc.d/rc.local X Display Manager if default runlevel =5

30 January 2013

Unix System Administration & Programming

257

Bios Initialization
Peripherals detected Boot device selected First boot of boot device read and executed

30 January 2013

Unix System Administration & Programming

258

GRUB and grub.conf


GRUB- the Grand Unified Bootloader
Command-line interface available at boot prompt Boot from ext2/ext3, ReiserFS, JFS, FAT, .. Filesystems

Support MD5 password protection

/boot/grub/grub.conf Change to grub.conf take effect immediatly

30 January 2013

Unix System Administration & Programming

259

kernel Initialization
Kernel boot time functions
Device detection Device driver initialization Mounts root filesystem read only Load initial process (init)

30 January 2013

Unix System Administration & Programming

260

init Initialization
init read its config: /etc/inittab
Initial runlevel System initialization script Run level specific script directories Trap certain key sequence Define UPS power fail / restore scripts Spawn gettys on virtual consoles

Initialize X in run level 5

30 January 2013

Unix System Administration & Programming

261

Startup Sequence
Power-on
LILO/GRUB

Boot loader

Load Kernel
Linux

Create init process

hardware

rc.sysinit

runlevel 0-6

rc.local

Ready

Initialisation Scripts
30 January 2013 Unix System Administration & Programming 262

Some Linux inittab action valus


Action
Boot Bootwait Ctrlaltdel Initdefault off Once Powerfail Set the detault runlevel Disable the entry For every runlevel When init receive SIGPWR signal

Meaning
Runs when system boots Init waits for complete

Powerokwait
Respawn Sysinit Wait
30 January 2013

SIGPWR and /etc/powerstatus has ok


Restart the process whenever it terminates Before any boot Upon entrying the run mode and waits to complete
Unix System Administration & Programming 263

System Initialization Script : rc.sysinit


/etc/rc.d/rc.sysinit - does a range of basic task:
configures networking sets hostname checks the root file system for repairs

check root file system quota & turns quotas for groups

and users mount non-root file systems and checks them for repairs turns on swapping (virtual memory subsystem) checks and loads modules (drivers) Set the system clock
Unix System Administration & Programming 264

30 January 2013

Linux Run Levels


Linux defines 7 run levels
The run level is selected by either The default in /etc/inittab at boot Passing an argument from boot loader Running init x after boot ( where x is the desired run level) Show current and previous run level /sbin/runlevel or who r Each run level defines a set of commands that are run to

stop and start processes. The actual commands are held in /etc/rc.d/init.d directory
30 January 2013 Unix System Administration & Programming 265

Linux Run Levels


The run level directories rc0.d, rc2.drc6.d contain

links to the actual commands Each command is prefixed with S or K and a number 0099 S prefix means that a process should be started e.g. S10network K prefix means that a process should be stopped (killed) K70syslog The numbers determine the order in which the commands are run from lowest first to highest last

30 January 2013

Unix System Administration & Programming

266

Linux Run Levels 0-2


Runlevel 0 Directory /etc/rc.d/rc0.d
Actually shutdown sequence Kill all processes Turn off virtual memory system (i.e. swap partition) unmounts swap and file systems

Runlevel 1 /etc/rc.d/rc1.d
single user mode used for maintenance by system administrators when

they need sole control of machine, e.g. reconfiguring hardware installing software

Runlevel 2 /etc/rc.d/rc2.d
Multi-user + networking (minus NFS)
30 January 2013 Unix System Administration & Programming 267

Linux Run level 3-6


Runlevel 3 /etc/rc.d/rc3.d
Default run level
multi-user + NFS

Runlevel 4 /etc/rc.d/rc4.d
not defined ( available for customization)

Runlevel 5 /etc/rc.d/rc5.d
Same as 3 under Redhat this but includes starting X

windows

Runlevel 6 /etc/rc.d/rc6.d
reboot similar to 0 but allow allows option to shutdown (halt)
30 January 2013

or reboot

Unix System Administration & Programming

268

/etc/rc.d/rc.local
Run after the runlevel specific script Common place for custom modification

30 January 2013

Unix System Administration & Programming

269

Shutdown
shutdown allows...
users to be warned the systems is going down the contents of disk caches to be written disk file systems to be marked as having been closed properly

( avoid file system check on next startup)

Access to the shutdown command is restricted (it is in /sbin)


shutdown -h now (h for halt)

Other command for shutdown the system: halt, poweroff, init 0

30 January 2013

Unix System Administration & Programming

270

System Reboot
Rebooting rarely fixes problems in Linux
If you feel a reboot is necessary try bringing the system

down to runlevel 1 and then back up to runlevel 3 or 5. this is much faster thn reboot

Rebooting the system


shutdown -r now reboot init 6

30 January 2013

Unix System Administration & Programming

271

Controlling services
Utilities to control default service startup
redhat-config services: graphical utility that requires

an X interface ntsysv: usable in virtual consols chkconfig : fast, versatile command line utility that work well and is usable with script

Utilities to control service manually


service: immediately start and stop a standalone

service chkconfig: immediately start and stop xinetd managed services and others
30 January 2013 Unix System Administration & Programming 272

Kernel Modules
Modular kernel components
components that need not be resident in the kernel for

all configurations and hardware


Peripheral device drivers Supplementary filesystems

Modules configurable at load time /lib/modules Controlling modules : lsmod, insmod, rmmod Controlling modules intelligently : modprobe, depmod

30 January 2013

Unix System Administration & Programming

273

Kernel Module Configuration


Module Configuration: /etc/modprobe.conf
Module aliases Module parameters

Module examination: /sbin/modinfo

30 January 2013

Unix System Administration & Programming

274

The /proc file system


/proc is a virtual file system containing information about the running kernel Content of files under /proc may be viewed using cat e.g.
cat /proc/interrupts

Provide information on system hardware , networking setting and activity, memory usage The /proc/sys subdirectory allow administrator to modify certain parameters of running kernel, but the modifications are temporary and not saved at system shutdown
30 January 2013 Unix System Administration & Programming 275

/proc/sys configuration with sysctl


The sysctl command manage such setting in a static and centralized fashion :
/etc/sysctl.conf

sysctl is called at boot time by rc.sysinit and use setting in /etc/sysctl.conf

30 January 2013

Unix System Administration & Programming

276

General Hardware resources


dmesg and /var/log/dmesg kudzu detect and configure new or changed hardware on a system
/etc/sysconfig/hwconf /usr/share/hwdata/

/proc filesystem hwbrowser

30 January 2013

Unix System Administration & Programming

277

System Monitoring
top, gnome-system-monitor display snapshot of processes vmstat report virtual memory stats iostat list information on resource usage, including I/O statistics free -summary of system memory usage

30 January 2013

Unix System Administration & Programming

278

File System
File System is developed for
create/store/load/delete/seek file on media Media example Magnetic Media Tape Floppy disk Hard disk Optical Media Cdrom Dvd

30 January 2013

Unix System Administration & Programming

279

File System of windows OS


Windows 98 and Windows ME

Support FAT16, FAT32

Windows 2000 Support FAT16,FAT32, NTFS Windows XP

Support FAT32, NTFS

30 January 2013

Unix System Administration & Programming

280

File System in Linux


File System in Linux is divided into 2 type
Linux Swap is used in virtual memory system Linux File System is used to store file there is various type of file system

ext2 ( is first introduced in kernel 2.0.x ) reiserfs ( is first introduced in kernel 2.2.x ) ext3 ( is first introduced in kernel 2.4.x )

30 January 2013

Unix System Administration & Programming

281

Different between Linux and Windows File System


Characteristic
File System

Windows
NTFS, FAT

Linux
ext2,ext3 Each partitions is mounted under /

Reference Root of each Each partition will partition point be own root Ex. C:, D:, F:

30 January 2013

Unix System Administration & Programming

282

Linux File System Layout


/

/etc
/boot /dev /usr

/home
/proc /var /lib /tmp

- first of mount point in linux - keep linux default configuration - keep important linux booting files - keep all device files - keep all user binary and X library - keep user home directory - is pseudo file system for tracking running process and state of linux system - keeping mail, log file and printer spooling - contain shared library that is required by system program - contain system temporary file

30 January 2013

Unix System Administration & Programming

283

How Linux mount this layout?


/ hda3

hda1

/var

/home

hda4

Using mount command


Mount [-t FS_TYPE] [-o OPTIONS] DEVICE MOUNT_POINT

type ex. ext2, ext3, reiserfs, device ex. /dev/hda1, /dev/hdb1, target-dir ex. /, /var, /home, /mnt/cdrom,

30 January 2013

Unix System Administration & Programming

284

How to use mount command?


/ hda3, ext3

hda1,ext3

/var

/home

hda4,ext3

mount command example


mount t ext3 /dev/hda3

/ mount t ext3 /dev/hda4 /home mount t ext3 /dev/hda1 /var


30 January 2013 Unix System Administration & Programming 285

check current mounted partition? mount Check current mounted by using


command without parameter Example result for checking current mounted partition

30 January 2013

Unix System Administration & Programming

286

How to use un-mount partition?


umount is used to un-mount partition
umount directory umount device

Example
umount /var

or umount /hda1 mount -t auto /dev/fd0 /mnt/floppy umount /mnt/floppy

30 January 2013

Unix System Administration & Programming

287

Where is linux store list of mounting partition when booting ?


It is locate in /etc/fstab file Example of fstab file

30 January 2013

Unix System Administration & Programming

288

Assume we would like to add windows partition (hdb1, FAT32) that linux have to mount at boot time at /mnt/windows
file system = /dev/hdb1

How can we add new mount partition entry in /etc/fstab?

mount point = /mnt/window


type = vfat Options = defaults Dump = 0 Pass = 1

30 January 2013

Unix System Administration & Programming

289

From the previous information, we have to add following line in /etc/fstab files
/dev/hba1 /mnt/windows vfat defaults 0 0

add new mount partition entry in /etc/fstab?

30 January 2013

Unix System Administration & Programming

290

Managing partition
Create Partitions using :
fdisk /dev/hda fdisk -l /dev/hda or cat /proc/partitions

display a report

Gnu parted advanced (create,copy,resize,etc.. )

30 January 2013

Unix System Administration & Programming

291

Managing file system


mkfs ( general command) mkfs.msdos -v /dev/fd0 mke2fs -v /dev/hda7 e2label /dev/hda7 e2fsck /dev/hda7 resize2sf /dev/hda7

30 January 2013

Unix System Administration & Programming

292

Managing file system


mkswap /dev/hdb6 swapon (activate swap space) tune2fs -j /dev/hda7 (convert from ext2 to ext3) Before convert you have to remount the file system as read only mount /dev/hda7 -o remount,ro

30 January 2013

Unix System Administration & Programming

293

Packages
In more widespread linux distributions, programs are distributed in binary RPM, DEB or modified TGZ formats RPM (RedHat Package Management) packages are used on RedHat, Mandrake, Suse, Conectiva DEB are used on the open source distribution Debian and its forks Modified TGZ are used on SlackWare

30 January 2013

Unix System Administration & Programming

294

Packaging Approaches: Source vs. Binary


There are two fundamentally different

approaches for packaging-based software distributions:


providing source packages containing

source package
distribution size package size package dependencies installation reproducability installation run-time stability installation system alignment installation time

binary package

the vendor sources plus instructions for automated build and installation. providing binary packages containing the final installation files only.

Most packaging facilities support both

approaches (including RPM), although often not equally well. Both approaches have each their pros and cons, nevertheless all software distributions focus on one of them.
30 January 2013

Unix System Administration & Programming

295

Dependencies

Dependencies management is a very useful feature of package management software

They keep systems in a consistent state and guarantee the applications to run in the expected way
rpm or dpkg commands have limited dependencies management features

They can report which library a package relies on, but the library can itself rely on other packages

30 January 2013

Unix System Administration & Programming

296

Dependencies - Example

Installing a package mod_test1 : # rpm -Uvh mod_test1-xxx.rpm error: failed dependencies: package-a1 is needed by mod_test1 # rpm -Uvh package-a1-xxxx.rpm error: failed dependencies: package-b2 is needed by package-a1 # rpm -Uvh package-b2-xxxx.rpm And so on

30 January 2013

Unix System Administration & Programming

297

Main Package Distribution Formats in Linux


There is no standard package manager in Linux Packages Distributed in Binaries or Source Code form Main Package Management Standards
Tarball files (.tar.gz/.tar.bz2)

The old-fashioned way of distributing software in Linux/Unix Compatible with all distros Main package manager in Slackware, Gentoo Introduced by RedHat and has been adopted by many other distributions (Fedora, Mandrake, SuSe) . The most popular Linux package format Introduced by Debian distribution
Unix System Administration & Programming 298

RPM (RedHat Package Manager) (.rpm)

DEB (Debian Package Manager) (.deb)

30 January 2013

Installing from Tarball files


Software Packages coming in source code archives have to be

compiled before installed Usually come in .tar.gz or .tar.bz2 archives Typical compilation/installation steps
Unpack the archive:

tar xzvf <package_name>.tar.gz tar xvjf <package_name>.tar.bz2 cd <extracted_dir_name> ./configure make make install
299

Change to the extracted directory

Run source configuration script as follows:

Build the source code using the GNU Make utility as follows:

Install the package as follows:

INSTALL or README files also exist in this directory giving


30 January 2013

application-specific usage information Unix System Administration & Programming

Backup Tools
tar
tar jxvf mytarball.tar.bz2 - Extract files from

30 January 2013

mytarball.tar.bz2 tar zxvf mytarball.tar.gz - Extract files from mytarball.tar.gz -z : Use gzip compression -j : Use bzip2 compression -x : Extract -v : Verbose -f : Use file gzip compression is used for tarballs with the extensions .tar.gz, .tgz, Bzip2 compression which is slightly better but requires more CPU is used in tarballs with the extensions .tar.bz2 and .
Unix System Administration & Programming

300

Managing Software in RedHat-based distributions


Using the command line, packages are installed using rpm utility program rpm i install package
rpm -i <package_name>.rpm

rpm e rpm U rpm q


30 January 2013

erase package upgrade package query packages (e.g., -a = all)


Unix System Administration & Programming 301

rpm e <package_name>

rpm U <package_name>.rpm

Package Management
Using RPM
To install or upgrade a package: rpm -Uvh package-1.0.i386.rpm
To remove a package: rpm -e package-1.0

To determine what version of a package you have

installed:

rpm -qa | grep package_name

To determine what package a file "belongs" to: rpm -qf file

30 January 2013

Unix System Administration & Programming

302

Example: rpm -q
rpm -q -i telnet

rpm -qi -p /mnt/cdrom/RedHat/RPMS/ggv*.rpm # Gives some info about ggv


rpm -qf /boot/vmlinuz

kernel-2.4.18.i586 rpm -qf `which sol` gnome-games

30 January 2013

Unix System Administration & Programming

303

Other Packaging Methods


The Debian case (APT - Advanced Packaging Tool)
The first distribution used organised on-line

package repositories APT utilities set (apt-get, apt-cache etc.) is provided for managing packages on these repositories Can manage packages in binaries and source format Provides packages inter-dependency auto-resolve Contacts repositories listed in /etc/apt/sources.list file

apt-get remove gnome


Unix System Administration & Programming 304

30 January 2013

APT for RPM

Brazilian distribution Conectiva ported the apt system on its linux distribution, which uses rpm packages Apt is now available for all distributions using rpm, also thanks to the SourceForge project apt4rpm (http://apt4rpm.sourceforge.net) Some features of Debian apt are not yet available, like package priority or requests to change configuration files, because of some differences in deb and rpm format
Unix System Administration & Programming 305

30 January 2013

Advanced Package Tool for RPM


High level package management tool from Debian

ported to RPM based systems (Conectiva Linux, RedHat, Fedora, SUSE, Scientific Linux ...) Installs the whole software with one command: apt-get install lcg-ui Automatically resolves dependencies for specified packages and downloads required rpms Central configuration file with repositories' URLs (/etc/apt/sources.list or /etc/apt/sources.list.d) Automated or half-automated software upgrades Note! APT requires that there are no broken dependencies in the OS
Unix System Administration & Programming 306

30 January 2013

User administrator

chmod, chown , chgrp useradd username userdel username Also can use adduser groupadd groupname gpasswd -a myuser mygroup groups username chage
chage -M 365 student

30 January 2013

Unix System Administration & Programming

307

Host Configuration
To connect a host to the network, the administrator

needs the following information.


Host name for the system Domain name for the system IP address for the system Netmask for the network (if applicable) Default router (gateway) for the network Name service used on the network Name or address of the machine providing name service

30 January 2013

Unix System Administration & Programming

309

Host Configuration
Every operating system stores network configuration

information in files. The /etc/sysconfig directory contains scripts and data files required to configure many system services, including networking in redhat Some of these files specify information about the hosts address and host name Other files specify which network services the host will allow, and which other hosts on the network provide services the host may require.

30 January 2013

Unix System Administration & Programming

310

Configuration Utility
netconfig text based network configuration utility
Only write config files. Does not activate device or changes Used by kudzu when new network card found at boot time

Redhat-config-network gnome based network

configuration utility

30 January 2013

Unix System Administration & Programming

311

Configuration NIC
Setting IP address : Static or Dynamic Static Configuration: Ifconfig
The ifconfig command is used to bind the IP address, host

name, net mask, broadcast address, and other network configuration parameters to a particular network interface. The ifconfig command is run at boot time by the startup scripts called by the init process. You can use the ifconfig command to examine and/or modify interface parameters while the system is up and running. When issued with the a flag, ifconfig prints the configuration information for all interfaces.
30 January 2013 Unix System Administration & Programming 312

Configuration NIC
Example Ifconfig eth0 inet 192.168.17.23 broadcast 192.168.17.255 Netmask 255.255.255.0 To instructs the kernel to activate the eth0 Ifconfig eth0 up

30 January 2013

Unix System Administration & Programming

313

Configuration NIC
The ifcfg-ifname File
The /etc/sysconfig/network-scripts/ifcfg-ifname files supply

the configuration information for each network interface. These script files contain a series of keywords and values parsed at boot time.
DEVICE="eth0" # The interface name BOOTPROTO="none" # Set to DHCP to use DHCP IPADDR="172.16.205.99" # Hosts static IP address NETWORK=172.16.205.96 # The network number BROADCAST=172.16.205.127 # The broadcast address NETMASK="255.255.255.240" # The netmask ONBOOT="yes" # yes to configure at boot
30 January 2013 Unix System Administration & Programming 314

Host Configuration
The ifup Script
The /etc/sysconfig/network-scripts/ifup script file is invoked at boot time by

init. The script reads the interface files and parses the entries. The information from the entries is used to configure each network interface with the proper values.

The ifdown Script


The /etc/sysconfig/network-scripts/ifdown script file is invoked at shutdown

time by init. The script reads the interface files and parses the entries. The information from the entries is used to shut down each network interface.

The network-functions Script


The /etc/sysconfig/network-scripts/network-functions script file contains a

library of functions used by the ifup and ifdown scripts. The functions include code for setting the net mask, host name, broadcast addresses, and gateways. The functions perform operations required to configure an interface up or down.
30 January 2013 Unix System Administration & Programming 315

DHCP
Using DHCP to Assign IP Addresses
A tool that simplifies IP configuration is the Dynamic Host

Configuration Protocol (DHCP). DHCP automatically assigns an IP address to a host at boot time. Hosts that use the DHCP protocol to obtain their network configuration information are typically referred to as DHCP clients. Hosts that supply DHCP configuration information to other hosts are referred to as DHCP servers.

DHCP Client Configuration


In most cases, the simplest method of configuring a host as a DHCP

client is to do so when the operating system installation process is invoked. In redhat the daemon is now dhclient which started automatically by /sbin/ifup script.

30 January 2013

Unix System Administration & Programming

316

Global Network Parametres


/etc/sysconfig/network

Networking=yes|no Hostname=<fqdn > Gateway=<gateway IP> NISDOMAIN=<NIS domainname>

30 January 2013

Unix System Administration & Programming

317

Default Route
Global default defined in:

/etc/sysconfig/network Gateway=<gateway IP>

Default gateway can also be defined in:

/etc/sysconfig/network-scripts/ifcfg-xxx

Global default routes overrides ifcfg-xxx rout

30 January 2013

Unix System Administration & Programming

318

Static Routes
Static routes defined by interface

/etc/sysconfig/network-scripts/route-eth0 /etc/sysconfig/networking/devices/etho.route route n netstat -n

Display with:

30 January 2013

Unix System Administration & Programming

319

netstat command
netstat
The netstat program is available under most operating

systems. In general, netstat can display packet counts, and configuration information for your system. Some of the typical uses of netstat include display of input/output packet counts per interface, display of TCP/UDP sockets in use on a host, and display of the routing tables for a host.

30 January 2013

Unix System Administration & Programming

320

Name Resolution
hostname display or set the systems name
Is initially set by rc.sysinit from $hostname variable in /etc/sysconfig/network

/etc/hosts - local database of hostname to IP address mapping


Checked before DNS Useful for small isolated networks

30 January 2013

Unix System Administration & Programming

321

Name Resolution
/etc/hosts File
One of the most frequently used network administration files is the

/etc/hosts file Even Windows systems have a version of the hosts file, called lmhosts. The file is a registry of IP addresses and associated host names known to a system. At a minimum, it must contain the loop-back address (127.0.0.1) and the IP address for the host. The hosts file is one of the resources consulted by applications in order to resolve a host name to an IP address when communications are requested. The format of host file entries follows.
IP address 192.168.44.55 Fully.Qualified.Name issa.svuonline.org host_alias issa
322

30 January 2013

Unix System Administration & Programming

DNS Client Configuration


/etc/resolv.conf File use the information in the /etc/resolv.conf file to configure the name service client on the host. The file consists of keywords and values. Some of the more common keywords follow. domain: DNS domain of this host nameserver: IP address of the name server(s) this host should contact. The preferred name server should be listed first.

30 January 2013

Unix System Administration & Programming

323

DNS Utility
Many useful utilities are included in the bind-utils

RPM package, including:

host: gather host/domain information host ns1.redhat.com Host a redhat.com


Can send query to name server directly dig @ns1.redhat.com redhat.com

dig ( domain name groper) flexible tools


nslookup

30 January 2013

Unix System Administration & Programming

324

/etc/services file
The /etc/services file contains a list of network

ports and services that correspond to those ports. For example, port 25 is defined as the SMTP port, whereas port 80 is reserved as the hypertext transport protocol daemon (httpd) port. To add a new service to a host, the administrator must add a port number and service name pair to the /etc/services file.
30 January 2013 Unix System Administration & Programming 325

Network Diagnostics
ping
Network packet loss and latency measurement tool

netstat
multipurpose network information tools

traceroute
The traceroute utility does exactly what its name implies: it

traces the route from point A to point B. This utility can be very useful when trying to determine why two hosts cannot communicate. For example, to trace the route from a specific local host to a distant host, the administrator could invoke the following command.
30 January 2013 Unix System Administration & Programming 326

File Sharing
To share files across the network, Linux uses two primary mechanism:
Network File System (NFS): for sharing with other Linux

and UNIX systems Samba: for sharing files with Windows-based computer

30 January 2013

Unix System Administration & Programming

327

NFS
Linux can be both an NFS client and an NFS server The NFS daemon is mountd /etc/exports control which file systems can be exported
/mnt/export issa.svuonline.org(ro) You have to run exportfs following changes to /etc/exports

or restart the NFS daemon manually client make the mount request with mount

mount issa:/mnt/export /mnt/local The : is the indicator to the mount command that this is NFS mount
Unix System Administration & Programming 328

30 January 2013

Samba
Provide file and print services to SMB/CIFS clients (Server Message Block/Common Internet File System). It is used to integrate windows-machines with Linux servers Can customize configuration per machine basis using

smb.conf file Samba include web-based interface for configuration called swat (Samba Web Administration Tools)

30 January 2013

Unix System Administration & Programming

329

Web Services - apache


Default location for web pages can be configured in the httpd.conf (usually in /etc/httpd/conf) Other file locations :
/etc/httpd/conf contain configuration files /etc/httppd/logs has the access and error logs>

Start apache the first time


/usr/sbin/apachectl start

30 January 2013

Unix System Administration & Programming

330

Mail Systems
Mail User Agent: many choices from console or graphic interface Mail Transport Agent: responsible for receiving and delivering message using the Simple Mail Transport Protocol (SMTP)
Accept messages from the user agent
Understand destination address Deliver mail originating on the local machine to the proper

mailboxes if local, or a delivery agent if remote Receive incoming mail from other delivery agents and deliver to local user
30 January 2013 Unix System Administration & Programming 331

Mail Systems
The default mail transport agents is Sendmail, can be difficult to configure, several newer ( qmail, postfix, and exim) sendmail behavior is governed by sendmail.cf file

30 January 2013

Unix System Administration & Programming

332

S-ar putea să vă placă și