Anda di halaman 1dari 60

UNIX Day 3

Intermediate Level

Recap Day 2
Text Processing Commands
Process Related Commands

Memory Related Commands


Compression Utilities

Session Plan Day 3


Communication Commands

Security features in UNIX


The find Utility vi editor & its important features Introduction to Shell Programming Types of shell

Shell variables
3

Communication Commands

Communication Commands
Command Description write
Writes a message to another user who is logged in Can be used by any user

Syntax $ write user_id $ mesg $ mesg y[or n] $ wall [g group] message

mesg
wall

Changes permission for receiving messages Gives permission status


Writes a message to all users who are currently logged in

Self Study: Communication Commands Demo

ftp File Transfer Protocol


Transfer files to and from a remote machine Allows the user to work with files on remote machine Syntax
- ftp [hostname]

ftp File Transfer Commands (1/2)


Command Description
put get mput mget

Syntax

Used to transfer a file from the source $ put filename terminal to the destination terminal Used to transfer a file from destination $ get filename terminal to source terminal Used to transfer multiple files from the source terminal to the destination $ mput file1 [file2] terminal Used to transfer multiple files from $ mget file1 [file2] destination terminal to source terminal

ftp File Transfer Commands (2/2)


Command
bye

Description
Used to close the FTP session

Syntax
$ bye $ close

Also used to close the FTP session System will give Good Bye close message then type bye This command displays the list of FTP commands that can be used by the user help User can also type ? symbol for the same Used to execute some commands in local machine ![Command] Shifts the control from ftp prompt to local machine
9

$ help $?

$ ![Command [arg]]

telnet
Used for connecting to UNIX Server. Usage
- telnet [hostname]

login: ajit Password: Good Evening /home/ajit>

telnet 10.122.130.17

Server

Authenticated

10

Security Features

UNIX Security - Password


The two important files that are used frequently for password authentication are
/etc/passwd /etc/shadow

These files are used whenever a user logs into the system.

12

UNIX Security - /etc/passwd


UNIX stores the user information in the passwd file.
It contains 7 fields separated by colon (:)
Username Password (Shown as character x) User-id Group-id Comments

Home Directory
Shell
13

UNIX Security - /etc/shadow


Stores the encrypted password Also called Shadow Password File Can only be viewed by the root

14

Good Passwords - Characteristics


To be a heterogeneous mix of characters and metacharacters, difficult to guess. To be easy on the users memory- no writing down To be changed frequently at least once in every 6 months To be highly secretive - never used before or in use elsewhere

15

Changing Passwords - passwd


passwd is the command used to change user password.
Syntax
passwd

The password entered would not be displayed, even in masked characters (like *,#,etc), on the screen.
Prompts for:
Current Password New Password Confirm New Password
16

File Security
Classes of users for a file.
Owner

Group
Others

Classes of Permissions for a file.


Read (r)

Write (w)
Execute (x)
17

File Security
Two modes of setting file permissions
Absolute Mode (Octal Integer) Symbolic Mode (String)

18

File Permission - Absolute Mode


Uses numbers for mentioning the permissions. File Permission Structure
Owner r 4 w 2 Group Others

x
1

r
4

w
2

x
1

r
4

w 2

x
1

rwxrwxrwx

Owner
19

Group

Others

File Permission Symbolic Mode


Uses characters & arithmetic operators for mentioning the permissions.
Operators
+ = To add permissions To remove permissions To assign permission

Symbols
r read , w - write , x execute u owner , g group , o- others, a all users

Example
u+rx Indicates the user has read and execute permissions.
20

File & Directory Permissions


System wide default permission for a file
rw-rw-rw (666) rwxrwxrwx (777)

System wide default permission for a directory

21

chmod - Setting Permissions


chmod
Used for setting file and directory permissions. Syntax.

chmod [0-7][0-7][0-7] filename (Absolute Mode) chmod [ugoa][+-=][rwx] filename (Symbolic Mode)

22

File Permissions - umask


umask
Stands for user creation mask. Sets default permissions for a newly created file and directory. Default value is 022. The value can be changed.

23

Permissions for new files


Calculation of permissions for newly created files:
System wide default permissions(666): umask value(022): Ones complement of umask value: 110 000 111 110 010 101 110 010 101

Now perform AND operation between System wide default permissions and Ones complement of umask value
110 110 110 AND 111 101 101 ----------------------------------------110 100 100 ----------------------------------------Equivalent permission set: 6(rw-) 4(r--) 4(r--)

This is resultant permissions for all newly created files.


24

Permissions for new directories


Calculation of permissions for newly created directories:
System wide default permissions(777): umask value(022): Ones complement of umask value: 111 000 111 111 010 101 111 010 101

Now perform AND operation between System wide default permissions and Ones complement of umask value
111 111 111 AND 111 101 101 ----------------------------------------111 101 101 ----------------------------------------Equivalent permission set: 7(rwx) 5(r-x) 5(r-x)

This is resultant permissions for all newly created directories.


25

Directory Permissions
Directory Permission r w x r w r x w x r w x
26

cd

ls

File creation or deletion

File accessing

X X X

X X

X X X X X

X X X

Can you answer these questions?


What are the contents of /etc/passwd file? What are the 3 classes of users in UNIX? What are the 3 types of permissions that can be given to each class of user? What is the system-wide default permissions for files and directories? Vinay has created a file named prog1.sh. He wants to give all permissions to owner, read & execute to group and only write to others. What are the ways, he can do this?
27

The find Utility

Why find Utility?


Searches a file or a group of files in the target directory
Any file can be Features of searched with the find utility help of cd in ls It can search file(s)and complete commands then directory in why should we downward direction It can be useduse perform some to find command? being searched If ls and cd commands are used,then how hierarchy of target much time is required for search operation operation on the files if path is too deep?

Syntax
find [path...] [expression]
29

find Utility Examples (1/2)


Variations/Sample Examples Search a file file1 from home directory downwards and print them Search a directory dir1 from home directory downwards and print them Syntax $ find ~ name file1 print $ find ~ type d name dir1 print

Search a file from current directory $ find . inum 46345 print downwards, which has inode value 46345

Search all files from current directory downwards, which have 2 links
Search all files from home directory downwards, which have permissions 744

$ find . links 2 print $ find ~ perm 744 print

30

find Utility Examples (2/2)


Variations/Sample Examples Search a file file1 from home directory downwards whose owner is vikas Search all files from home directory downwards, which have size 10 bytes Search a file f1 from current directory downwards and execute rm command on the result Search a file f1 from current directory downwards and execute rm command on the result after confirmation Syntax $ find ~ \( user vikas a name file1 \) print $ find ~ size 10c print

$ find . name f1 exec rm {} \;

$ find . name f1 ok rm {} \;

31

The vi Editor

Editor in UNIX
Need for editor in UNIX Types of editor
Line Editor
ed : UC Berkeley ex : Powerful than ed, Bell Systems

Full Screen Editor


vi (stands for visual) vim vi improved emacs (GNU)

33

The vi Editor (1/2)


The important features are:
Omnipresent
Works on different UNIX flavors

Fast
Various operations are very fast

Powerful UNDO features


Text in lines could be undone with very less effort

34

The vi Editor (2/2)


The limitations are:
Less user-friendly
No graphical user interface

Highly Case-sensitive
Letters in lower case have a different implementation in comparison with the same letter in upper case

Keystrokes could have more than one meaning


A letter (of the same case) has different implementation across different modes

35

vi Editor Operating Modes (1/2)


Modes of working:
Command Mode
Keys are interpreted as commands

Insert Mode
Keys are interpreted as data

Escape Mode
Keys are interpreted for saving/exiting purposes

36

vi Editor Operating Modes (2/2)


Start
$ vi i, I , o, O, a, A ..

Command mode :
Enter esc

Insert mode

Escape mode

:q

End
37

vi editor commands
To move around [Command Mode]
h, j, k, l, w, b

Inserting/Deleting text [Command Mode]


i, a, o, O, dd, dw

Changing/Replacing text [Command Mode]


cc, cw, c$, u, yy, yw, p, P

File manipulation [Escape Mode]


:w, :wq, :w!, :q, :q! , :![command]

38

Searching a pattern
Work in Command Mode
/pattern
Searches forward for first occurrence of a pattern.

?pattern
Searches backward for first occurrence of a pattern.

n
Repeats the last search.

N
Repeats the last search command in opposite direction.

39

Pattern Substitution
Work in Escape Mode
:s/ptn1/ptn2
Replaces first occurrence of ptn1 with ptn2.

: s/ptn1/ptn2/g
Replaces all occurrences in the current line.

: m, n s/ptn1/ptn2/g
Replaces all occurrences in lines m to n.

: ., $ s/ptn1/ptn2/g
Replaces all occurrences from current line to end of file.

40

Customizing vi
Work in Escape Mode
The set command
:set showmode :set nu

The abbr command


:abbr itl Infosys Technologies Ltd

The map command


:map v :wq

41

Handling multiple files


vi file1 file2 file3
Opens all files one after another

Work in Escape Mode


:n
Permits editing of next file in the buffer

: rew
Permits editing of first file in buffer

: args
Displays names of all files in the buffer

:f
Displays the name of the current file
42

vi Startup File
.exrc
vi reads .exrc before loading settings are permanent for a vi session

43

Programming with the Shell

The Shell
A program that interprets users requests and gives the response of requests to user Provides environment for user programs A command line interpreter that
Reads user inputs Executes commands

45

The Shell
Shell allows three types of commands:
An internal command An executable file that contains a sequence of shell command lines An executable file that contains object code produced by compilation

46

Shell Features
Interactive and background processing Input/output redirection Pipes Wild card matching Programmable
Shell Variables Programming language constructs Shell Scripts

47

Shell Programming
Collection of Shell commands Also called as shell scripts Variables and flow control statements could be added To run a script we need to first set the execute permission on that file Then set the PATH system variable to include the directory path which contains the file
PATH=$PATH:/home/vaniv : is the delimiter and /home/vaniv is added to the list of path

Note :Use # to interpret the rest of line as comment

48

Shell Variables
UNIX allows the use of variables at the $ prompt and within shell scripts. Variables are given names, usually meaningful ones, by the user.
Types of variables:
User defined variables System variables

Types of parameters:
Positional Parameters Special Parameters

49

User-defined Variables
User-defined variable can be assigned a value. Used extensively in shell-scripts. Used for reading data, storing and displaying it.

50

Accepting Data
read.
Accepts input from the user. Syntax : read variable_name. Example : read sname

Variable Name

51

Display Data
echo
Used to display a message or any data as required by the user. echo [Message, Variable] Example: echo Infosys Technologies Ltd. echo $sname

Variable Name

52

Local and Global Shell Variables


When a variable is defined, it is know to the shell that created it.
If a new shell is created by typing sh , the born shell is not aware of the parent shell variables. These variables are called as local variables.

The same variable names can be used in child shell.


If the child shells wants to use the parent shell variables then we can use export command provided by a shell. These variables are called as global variables.
53

export command
export
To make a variable a part of environment and also be accessible to the child process (or shell)

54

System/Environmental Variables.
Besides User-defined variables, the shell also has special variables called environmental variables. Some examples of environmental variables are the HOME, PATH, PS1 and LOGNAME variables
HOME
Contains the home directory for the user.

PATH
Contains a list of all full path-names of directories that are to be searched for executable programs. These names are separated by colon.

PS1
Contains the system prompt.

LOGNAME
Contains the users login name

55

set command
set command
Used to display all the environment variables. Shows the current value of system variables. Also allows conversion of arguments into positional parameters. Syntax : set

56

Self Study: Introduction to Shell Programming Demo

57

Can you answer these questions?


Differentiate between local and global variables What is the procedure to make any shell script executable? What is the output of following?
1. $ Num1=10 $ Num2=Num1 $ echo $Num1 $Num2 $ Num1=10 $ Num2=$Num1 $ echo $Num1 $Num2
58

2.

Summary
Communication Commands Security features The find Utility vi editor and its important features Shell Programming
Shell variables read and echo commands set command

59

Thank You

60

Anda mungkin juga menyukai