1 unix admin tools. 2 overview review of file manipulation utilities unix process subsystem overview...

45
1 UNIX Admin Tools

Upload: iris-cameron

Post on 17-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

1

UNIX Admin Tools

Page 2: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

2

Overview

• Review of file manipulation utilities

• UNIX process subsystem

• Overview of the UNIX shells csh/ksh

Page 3: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

3

File Attributes

• Stored in the file I-node

• File’s ownership: user and group

• file permissions: read, write, execute

• file modification times

• file type: regular, directory, link, symbolic link, special file

Page 4: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

4

Utilities for Manipulating file attributes

• chmod change file permissions

• chown change file owner

• chgrp change file group

• only owner or super-user can change file attributes

• upon creation, default permissions given to file modified by process umask value

Page 5: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

5

File Permissions

• Three types of permissions:• read, process may read contents of file

• write, process may write contents of file

• execute, process may execute file

• three sets of permisions:• permissions for owner

• permissions for group

• permissions for other

• access checks made against process’s effective ids

Page 6: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

6

Chmod command• Symbolic access modes

• example: chmod +r file

• Octal access modesoctal read write execute0 no no no

1 no no yes

2 no yes no

3 no yes yes

4 yes no no

5 yes no yes

6 yes yes no

7 yes yes yes

Page 7: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

7

Directory permissions

• Same types and sets of permissions as for files– read: means process may a read a dir (i.e., list

files)– write: process add/rm files in dir– execute: process can “search”, access files, in

dir or subdir

Page 8: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

8

Common Utilities for Managing files and directories

• pwd print process current dir• cat, ed, vi, emacs… create files• ls list contents of directory• rm remove file• mv rename file• cp copy a file• ln create a hard link to a file• mkdir and rmdir create and remove dir• lp: print a file• wc counts the words in a file

Page 9: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

9

Unix Processes

Definitions:

• program: collection of bytes and data stored in a file

• image: computer execution environment

• process: execution of an image

• multi-tasking: many processes can execute simultaneously in Unix.

Page 10: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

10

Unix Process Groups

• process id: unique id assigned to process upon creation

• process group id: id of the group to which the process belongs to

• foreground process group: is the process group associated with a terminal at a time

• background process group: processes created by you not in the foreground group

Page 11: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

11

Process Relationships• A process spawns another process using the

fork(2) system call.• The creating process is the parent process• The newly created process is the child process.• fork() returns 0 to the child process• fork() returns the process_id of the child to the

parent process

Page 12: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

12

Process Relationship (continued)

• exec(2) :To run a new program, the child, will issue the exec( ) system call and overwrites itself with the code and initial data of the new program, thus initiating the execution of the new program

• wait(2): a parent can suspend its execution until one or more child processes complete via a wait(2) system call

Page 13: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

13

Process Relationships (continued)

• exit(2) :upon terminations, process can set an exit status available to parent. Code used – zero for success– non-zero for failure

Page 14: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

14

Example: Program that creates a new process to copy files

main(argc,argv)

int(argcl

char *argv[];

{/* assumes 2 args, source and target files */

if ( fork() == 0) {

/* child process */

execl("cp"."cp",argv[1],argv[2],0);

}

/* parent process */

wait(int *) 0);

printf("copy done\n");

}

Page 15: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

15

Fork operation

A fte r fo rk op era tion

p aren t p rocess d a ta ch ild p rocess d a ta

sh ared text

Page 16: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

16

After exec of prog2 in child

A fte r exec "p rog 2 " in ch ild

p rog text d a ta u n ch an g ed

p aren t p rocess d a ta

p rog 2 text d a ta

ch ild p rocess d a ta

(prog2 is cp in example)

Page 17: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

17

Unix process genealogyP rocess g en era tion

g e tty

in itexecs

/b in /sh

log inexecs

g e ttyexecs

in itexecs

g e tty

In itexecs

In it p rocess 1fo rks in it p rocesses

Page 18: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

18

Process permissions

• real id and one of more real group id set at login.

• effective uid and effective group id determine process access to read/write/search/execute files or dir.

• umask() file mode creation mask, used when file or dir created by process

Page 19: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

19

Signals• Signal: mesg a process can send to a process or

process group, if it has appropriate permissions.• mesg number represented by a symbolic name

• for each signal, receiving process can:– explicitly ignore signal– specify action to be taken upron receipt (signal

handler)– otherwise, default action takes place (usually

process is killed)

Page 20: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

20

Signals (continued)

Example:

• When a child exists, it send a SIGCHLD signal to its parent.

• When the parent issues a wait, it tells the system it wants to catch the SIGCHLD signal

• When a parent does not issue a wait, it ignores the SIGCHLD signal

Page 21: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

21

Inter-process CommunicationRelated Processes• signals• read/write regular files

• pipes: when a process B tries to read from a pipe

– returns data if process A has written to pipe– returns with EOF, if no other process has pipe open

for writing– suspends execution until process A writes data to it

• child returns exit value to waiting parent process

Page 22: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

22

Interprocess Communication

Unrleated Processes– FIFO (named Pipes)– System V IPC

• msg queues• semaphores• shared memory

– sockets (client/server model)

Page 23: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

23

Process Environment includes:

• Process id and process group id• open files • current working directory• real and effective user and group ids• file creation mask (umask)• resource limits• signal action settings• set of named local variables

Page 24: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

24

File Descriptors• each process associates a number or handle,

called file descriptor, (fd) with each file it has opened.

• At login, three files associated with terminal– standard input: fd 0, open for reading

– standard output: fd 1, open for writing

– standard error: fd 2, open for reading,writing

• process inherits parent’s file descriptors unless specified (close-on-exec)

Page 25: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

25

Process Subsystem utilities

• ps monitors status of processes• kill terminate a process (by pid)• wait parent process wait for one of its

children to terminate• nohup makes a command immune to

the hangup and terminate signal• sleep sleep in seconds• nice run processes at low priority

Page 26: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

26

Setuid and Setgid Mechanisms

• Mechanism pattented

• process effective uids are different from its real uids when it executes a set-uid or set-gid program.

• the process effective uid and gid become that of the executable

• example: changing your passwd

Page 27: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

27

Security Problems

• Permissions on the executable program

• and directory in which it is contained must be correct, otherwise easily replaced by Trojan Horse.

• Some systems remove setuid and setgid bits whenever files are modified as a security precaution.

Page 28: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

28

Overview of the shell

• Command line interpreter and programming language between operating system and user

• user may select which shell to run:– /bin/csh Cshell– /bin/ksh Korn shell– other shells

• shell scripts: files of UNIX and shell commands executed from a UNIX shell

Page 29: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

29

Working with the shell

• Shell invoked automatically during a login session or manually at the prompt by user– 1. Reads a special startup file for initialization– 2. Displays prompt and waits for user command– 3. Executes user command and goes to step 2,

unless contrl D, then shell terminates

Page 30: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

30

Redirection of input/ouput

• Redirection of output: >, >>– example:$ man ls > info.ls

• Redirection of input: <– example: $ cat <input.data

• using filters: pipes– example: $ cat file| wc -l;

/* counts the number of line in file */

Page 31: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

31

Shell Core Features• Simple and complex commands• redirection of input/output• pipes• wildcards• command substitution• background processes• shell variables• here documents• built-in cmds• programming constructs

Page 32: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

32

Simple Commands supported

• simple command: sequence of non blanks arguments separated by blanks or tabs.

• 1st argument (numbered zero) usually specifies the name of the command to be executed.

• Any remaining arguments (with a few exceptions, see meta-characters)– Are passed as arguments to that command.

– Arguments may be filenames, pathnames, directories or special options

Page 33: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

33

Complex commands

• Multiple commands

• Command groupings

• Conditional command execution

Page 34: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

34

File name expansion

• Wildcards* matches any string of characters

? matches any single character

[list] matches any character in list

[lower-upper] matches any character in range lower-upper inclusive

Page 35: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

35

Shell Scripts• A shell script is a regular text file that contains

shell or UNIX commands• Before running it , it must have execute

permissions ( see chmod +x filename)• Very useful for automating repetitive task and

administrative tools and for storing commands for later execution

Page 36: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

36

Shell Scripts (continued)

• When a script is run , kernel determines which shell it is written for by examining the first line of the script– If 1st line is just #, then it is interpreted by a C shell

– If 1st line is of the form #!pathname, then the executable

– Pathname is used to interpret the script

– If neither rule 1 nor rule 2 applies, the script is interpreted by a Bourne shell.

Page 37: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

37

Here Documents• Shell provides alternative ways of supplying standard input to

commands• Shell allows in-line input redirection using << called here

documents• format

command [arg(s)] << arbitrary-delimiter

command input

:

:

arbitrary-delimiter• arbitrary-delimiter should be a string that does not appear in text

Page 38: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

38

Shell Variables• Shell has several mechanisms for creating variables. A

variable is a name• Representing a string value

– Shell variables can save time and reduce typing errors, variables

• Allow you to store and manipulate information • two types: local and environmental

– local are set by the user of by the shell itself

– Positional parameters variables are normally set only on a command line

Page 39: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

39

Environmental Variables

NAME MEANING

$HOME absolute pathname of your home directory

$PATH a list of directories to search for

$MAIL absolute pathname to mailbox

$USER your user id

$SHELL absolute pathname of login shell

$TERM type of your terminal

Page 40: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

40

Positional parameters• when a shell procedure is invoked, the shell

implicitly creates positional parameters. The name for a positional parameter is a number.

• Positional parameters are used mainly in scripts.– $0 is the argument in position zero on the command

line

– $1 is the first argument

– $1.. $9 $n refers to the nth argument on the command line if applicable

– $# the number of positional parameters, not counting 0

– $* the list of all arguments

Page 41: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

41

QUOTING• Quoting restores the literal meaning to characters

that are processed specially by the shell. The literal quotes are not passed on to the command

• Single quotes ( ' ) inhibit wildcard replacement, variable substitution, and command substitution

• Double quotes ( " ) inhibit wildcard replacement only

• When quotes are nested, only the outer quotes have any effect

Page 42: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

42

BUILT-IN commands• commands that are internal to the shell

• Faster to execute and more efficient than other commands– Shell does not have to fork to execute the

command– Trade-off: redirection of input/output not

allowed for most of these

Page 43: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

43

Built-in commands (continued)

• built-in commands common to the 3 shells:echo exec

cd shift

wait umask

exit eval

 

Page 44: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

44

End of Lecture

• Questions?

Page 45: 1 UNIX Admin Tools. 2 Overview Review of file manipulation utilities UNIX process subsystem Overview of the UNIX shells csh/ksh

45

Subshells

• When a parent shell forks a child to execute a command, the new child shell is sometimes called a subshell. This happens when:– a group command is executed ( $(cmd1; cmd2;

cmd3) )– a shell script is executed ( $myscript )– a background job is executed ( cmd1&)

• A shell inherits the parent's environment but not the parent's local variables.