Sie sind auf Seite 1von 49

Unix

Unix Utilities & Development Tools.

Welcome to this course on Unix Operating System, Day 3.

Road Map
Recap of Day 2 To understand Unix Utilities. To understand Program development tools. To understand communication commands. To understand the security features.

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

To understand Unix Utilities. To understand file compression utilities. To understand Unix Program development tools To understand C Program Development Tool make SCCS To understand Unix Communication Tools. To understand commands like write, wall, etc. To understand security features Passwords File Permissions Directory Permissions SU Command

Unix Utilities.
gzip Utility for compression of files. Files have an extension of .gz. Syntax gzip file_name To restore the file back, we need to use gzip d file_name or gunzip file_name

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

gzip gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. Example $> ls -l abcd1 -rw-rw-r-# For finding the details of file abcd1 1 ajit ajit 255 Mar 17 15:45 abcd1 # Size of the file is 255 bytes. #Zipping the file. File would have an

$> gzip abcd1 extension .gz $> ls -l abcd1*

-rw-rw-r-- 1 ajit ajit 51 Mar 17 15:45 abcd1.gz Size of the zipped file is 51 bytes. To unzip $> gzip -d abcd1

Unix Utilities.
tar Tape Archive.
Copies files to or restores files from tape. Syntax tar [options] files.

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

tar If any of the files are directories, tar acts on the entire subtree. Options need not be preceded by -. Function Options (choose one) c r t u x files) Example To backup 'trng' dir, create an archive file 'trng.tar : $> tar -cvf trng.tar trng Note : c - create, v - verbose mode, f - archive to file. Instead of 'f', you can mention a device name too, like tape device. To extract it back from the archive file: $> tar -xvf trng.tar Note : The archive file can have any or no extension. All that 'tar' does is, concatenates all the files into archive file and it doesn't compress the archive file (like WinZip & other utilities). So, it's a good idea to use 'gzip' to compress the archive file before actually backing it up. create a new tape Append filesto tape Print the names of files if they are stored on tape Add files if not on tape or if modified Extract files from tape (if files not specified, extract all

Unix Program Development Tools.


Ease of program development. Some programming languages available on Unix are:

Program

Primarily used for

awk String processing, report generation, file editing C General purpose and system programs; fast executing programs efl Writing structured FORTRAN programs Fortran 77 Number-crunching; engineering, statistical and mathematical applications; fast executing programs sh General purpose programs; file manipulation; process control sno Pattern matching; string manipulation (SNOBOL)
ER/CORP/CRS/OS31/003 Version No: 2.00

Copyright 2004, Infosys Technologies Ltd

One of the primary goals of the Unix operating system design was ease of application program development. As such the Unix system offers a wide assortment of programming languages and development tools. The nature of the application would determine the choice of a language. In most cases there is a trade-off between development time versus execution speed. It is not uncommon for the shell itself to either control the execution of other programs, or do a lot of processing itself. A shell script is shorter and takes less time to develop than an equivalent C program but runs more slowly because it is interpreted rather than compiled.

Unix
C Program Development

C Program Development.

cc C language Compiler
Phases in creation of executable program.

Source Code

.c

Object Code

.o

Executable code

a.out
Copyright 2004, Infosys Technologies Ltd 7 ER/CORP/CRS/OS31/003 Version No: 2.00

Example $> cc file1.c creates an executable file by the name a.out Compiles the file file1.c. $> a.out Executes the file #Compiles and

cc - Important options
-c
Compiles the given C file. No linking would be done. Syntax
cc c <filename.c>

-o
Specify a given name for the final executable file. Syntax
cc o <file1> <filename.o>

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

cc -c Compiles the given file and creates an object code. Examples $> cat demo1.c main() { printf( \n Hello \n"); } $> cc -c demo1.c Creates an object code by name demo1.o cc o Allows to create an executable file by the name requested by the user. Examples $> cc -o demo1 demo1.c name demo1 $> demo1 #Execute the file demo1 Hello #Output #Creates an executable file by the

Development Tools.
Debuggers
adb - assembly debugger ctrace - tracing execution statement by statement sdb - symbolic debugger

Beautifier
cb - Automatic formatting of C programs

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

Development Tools.
cflow
Generating a flow graph of external references

lint
Checking (that may span many files) for bugs or non-portable uses of the language

prof
Obtains performance statistics

Copyright 2004, Infosys Technologies Ltd

10

ER/CORP/CRS/OS31/003 Version No: 2.00

10

Development Tools - make.


make
Regenerates a program system by automatically tracking files that have changed since the system was last made. Useful in program systems that comprise of more than one source file.

Copyright 2004, Infosys Technologies Ltd

11

ER/CORP/CRS/OS31/003 Version No: 2.00

11

make
How it works?
Works on concept of Dependency Maintenance. Picks up a file containing dependency relation among all the files, often called as makefile. Keeps automatic tracking of the source files that may been changed since the last compilation and causes their recompilation when necessary. automatically re-links the object modules to update the final executable.

Copyright 2004, Infosys Technologies Ltd

12

ER/CORP/CRS/OS31/003 Version No: 2.00

12

make.
Benefits.
Lots of compilation time is saved. Picks up the latest version of the file. Avoids ambiguity over decision of which file to compile if a file content in a project system is changed.

Copyright 2004, Infosys Technologies Ltd

13

ER/CORP/CRS/OS31/003 Version No: 2.00

If the project contains multiple files, make utility avoids compiling all the files together, thereby reducing the time spent on compiling. It only recompiles the file on which changes were made.

13

makefile
When a make command is issued, by default picks-up a file known as the makefile or Makefile. It contains the dependency relation for the different files in the system. The user could also customize the name of the makefile as per choice.
make f mymakefile

Copyright 2004, Infosys Technologies Ltd

14

ER/CORP/CRS/OS31/003 Version No: 2.00

Note : make can also be invoked without any arguments.

14

Makefile Contents.
Names of the source files that make up the program system. Name of the destination file. Information on how to regenerate the program system.

Copyright 2004, Infosys Technologies Ltd

15

ER/CORP/CRS/OS31/003 Version No: 2.00

Example $> cat makefile OBJECTS = try.o add.o sub.o #Three object files make this system : try.o, add.o and sub.o final: $(OBJECTS) # The executable would be called final. cc $(OBJECTS) -lm -o final try.o: try.c common.h # For creating try.o, we require 2 files .i.e try.c and common.h add.o: add.c common.h # For creating add.o, we require 2 files .i.e add.c and common.h sub.o: sub.c common.h # For creating sub.o, we require 2 files .i.e sub.c and common.h $>

15

SCCS.
SCCS Source Code Control Systems. SCCS is a package of programs
to help manage the development and maintenance of large programs. to automatically track changes between versions of a program. to maintain a list of these changes which has the ability to quickly recreate a particular version on request. that does not waste much of disk space.

Copyright 2004, Infosys Technologies Ltd

16

ER/CORP/CRS/OS31/003 Version No: 2.00

SCCS is a Version Control System. Version Control System is required to keep track of the different changes made in the system, so that as per business requirement, the user could revert to the earlier version of the system.

16

Placing a file under SCCS.


admin -n -ifile s.sccsname
places file under SCCS, giving it the name s.sccsname

all SCCS files must begin with s. admin command must be run separately for each file that is to be placed under SCCS.

Copyright 2004, Infosys Technologies Ltd

17

ER/CORP/CRS/OS31/003 Version No: 2.00

Example $ admin -n -i main.c s.main.c -n means new -i means install for the first time places main.c under SCCS calling it s.main.c To place many files under SCCS Use the for statement in a shell script for f in *.c do admin done After placing the files under SCCS, the original file must be removed to ensure the consistency of the contents of the version controlled file. -n -i$f s.$f

17

Extracting files from SCCS.


Removes original files. Any access to source files must be through SCCS system only get command is used for extracting from SCCS source files

Copyright 2004, Infosys Technologies Ltd

18

ER/CORP/CRS/OS31/003 Version No: 2.00

Examples. Display latest version with info: $ get -p s.main.c Extract for viewing but not editing $ get s.main.c Extract for editing $ get -e s.main.c

18

Updating files in SCCS


Inserting file back into SCCS after editing (a new version) delta command automatically updates the current level number
prompts the user for an optional comment removes the file that was edited.

Syntax
$ delta s.main.c

Copyright 2004, Infosys Technologies Ltd

19

ER/CORP/CRS/OS31/003 Version No: 2.00

19

Basic SCCS Commands


Command admin get delta prs Used to creates new SCCS files extract SCCS files make changes to a SCCS file print information about an SCCS file

Copyright 2004, Infosys Technologies Ltd

20

ER/CORP/CRS/OS31/003 Version No: 2.00

20

Make & SCCS


The SCCS and make utilities work well in conjunction with each other. If make does not find a particular file f, it will automatically look for file s.f . If it finds it, it will automatically assume it as an SCCS file and will issue the necessary get command to extract it. After make is finished with the extracted SCCS file, it will automatically remove it.

Copyright 2004, Infosys Technologies Ltd

21

ER/CORP/CRS/OS31/003 Version No: 2.00

21

Unix
Communication Commands

Unix Communication Commands.

22

Communication Commands.
write mesg wall ftp telnet

Copyright 2004, Infosys Technologies Ltd

23

ER/CORP/CRS/OS31/003 Version No: 2.00

Unix Operating System has several Commands by which one user can communicate to others.

23

write Command
Writes to another user logged in. Can be used by any user. Usage
$ write userid [tty]

Copyright 2004, Infosys Technologies Ltd

24

ER/CORP/CRS/OS31/003 Version No: 2.00

Example $> whoami pract1 $> write pract2 This is a demo. user pract2

# Senders name # Message to be displayed to the

24

mesg command
To change permission for receiving messages. Usage
mesg [y/n/ ]

This command is ineffective for messages from the administrator/root.

Copyright 2004, Infosys Technologies Ltd

25

ER/CORP/CRS/OS31/003 Version No: 2.00

Mesg mesg y -- gives permission mesg n -- denies permission mesg -- reports current status Example: $> mesg is y $> mesg n from normal users. $> mesg is n Note : Even if a user chooses option n, then too he would receive the messages from the root. #Disables receiving of messages #Displays the status

25

wall command
writes to all users currently logged in Usage
- wall [-g group] message

Can be used by any user

Copyright 2004, Infosys Technologies Ltd

26

ER/CORP/CRS/OS31/003 Version No: 2.00

Example $> wall This is a demo" $> Broadcast message from ajit (pts/0) (Sun Jul 11 15:32:51 2004): This is a demo

26

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]

Mode of transfer of data


Ascii Binary

Copyright 2004, Infosys Technologies Ltd

27

ER/CORP/CRS/OS31/003 Version No: 2.00

27

ftp File Transfer Commands


put
This command is used to transfer file from the source terminal to the destination terminal. put filename

get
This command is used to transfer a file from destination terminal to source terminal. get filename

Copyright 2004, Infosys Technologies Ltd

28

ER/CORP/CRS/OS31/003 Version No: 2.00

Multiple files can be selected in one command line and for that mput and mget commands are used. $> mput a* #Transfers all filenames starting with a from the source machine to the destination machine. $> mget b* #Transfers all filenames starting with b from the destination machine to host machine.

28

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

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

telnet 192.168.158.11

Server

Authenticated

Copyright 2004, Infosys Technologies Ltd

29

ER/CORP/CRS/OS31/003 Version No: 2.00

The character typed on keyboard will first go to UNIX server and then return to the user terminal to be displayed on the telnet window.

29

Unix
Security Features

30

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 logins in the system.

Copyright 2004, Infosys Technologies Ltd

31

ER/CORP/CRS/OS31/003 Version No: 2.00

/etc/passwd can be viewed by a normal user. /etc/shadow cannot be viewed by a normal user.

31

Unix Security - /etc/passwd


Unix stores the user information in the passwd file. It contains 7 fields separated by colon (:)
Username Encrypted Password User-id Group-id Comments Home Directory Shell

Copyright 2004, Infosys Technologies Ltd

32

ER/CORP/CRS/OS31/003 Version No: 2.00

/home/ajit>cat /etc/passwd root:x:0:4:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin trng39:x:542:504::/home/trng39:/bin/bash eugene:x:554:504::/home/eugene:/bin/bash trng90:x:594:504::/home/trng90:/bin/bash trng91:x:595:504::/home/trng91:/bin/bash trng92:x:596:504::/home/trng92:/bin/bash

32

Unix Security - /etc/shadow.


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

Copyright 2004, Infosys Technologies Ltd

33

ER/CORP/CRS/OS31/003 Version No: 2.00

$> cat /etc/shadow root:$1$lduGqK0k$fVDdj4J7zPvrcGefrXOf5/:12487::99999:::: bin:*:12227:0:99999:7::: daemon:*:12227:0:99999:7::: trng90:$1$22RPkY9l$SLMEVhOc1JoRMIOdBRj9D.:12332:0:99999:7::: trng91:$1$Z6f2.MP7$jhFco/./JuMBzDkkErAgo/:12332:0:99999:7::: trng92:$1$kbD4fCFI$3mlc37uMa4eAOFJL4sfFy1:12332:0:99999:7::: trng93:$1$wvhu.RmA$46MpC2jOYZbdOzt6ejiA41:12332:0:99999:7:::

33

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

Copyright 2004, Infosys Technologies Ltd

34

ER/CORP/CRS/OS31/003 Version No: 2.00

34

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.

Copyright 2004, Infosys Technologies Ltd

35

ER/CORP/CRS/OS31/003 Version No: 2.00

Example $>passwd Changing password for user ajit. Changing password for ajit (current) UNIX password: New password: Retype new password: passwd: all authentication tokens updated successfully.

35

File Security.
Classes of users for a file.
Owner Group Others

Classes of Permissions for a file.


Read (r) Write (w) Execute (x)

Copyright 2004, Infosys Technologies Ltd

36

ER/CORP/CRS/OS31/003 Version No: 2.00

Each type of user has 3 different permissions for a file. Hence for every file there are 9 composite permissions, which is identified by the 9-bit field for every file.

36

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

Copyright 2004, Infosys Technologies Ltd

37

ER/CORP/CRS/OS31/003 Version No: 2.00

37

File Permission - Absolute Mode.


Uses numbers for mentioning the permissions.
Owner r 4 w 2 x 1 r 4 Group w 2 x 1 r 4 Others w 2 x 1

rwxrwxrwx File Permission Structure


Owner Group Others

Copyright 2004, Infosys Technologies Ltd

38

ER/CORP/CRS/OS31/003 Version No: 2.00

The read (r) permission is assigned the value 4. The write (w) permission is assigned the value 2. The execute (x) permission is assigned the value 1. - indicates no permission has been set. Example rwxrw-r-- indicates the owner has read, write and execute permissions; the group has read and write permissions; others have only read permission.

38

File Permission Symbolic Mode.


Uses characters & arithmetic operators for mentioning the permissions. Example
u+rx Indicates the user has read and execute permissions.

Copyright 2004, Infosys Technologies Ltd

39

ER/CORP/CRS/OS31/003 Version No: 2.00

39

File & Directory Permissions.


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

System wide default permission for a directory


rwxrwxrwx (777)

Copyright 2004, Infosys Technologies Ltd

40

ER/CORP/CRS/OS31/003 Version No: 2.00

40

chmod - Setting Permissions


chmod
Used for setting file and directory permissions. Syntax. chmod [0-7][0-7][0-7] filename chmod [ugo][+-][rwx] filename (Absolute Mode) (Symbolic Mode)

Copyright 2004, Infosys Technologies Ltd

41

ER/CORP/CRS/OS31/003 Version No: 2.00

chmod Example $> chmod 777 com1 $> ls -l com1 -rwxrwxrwx $> ls -l com1 -rwxrwxrwx 1 ajit ajit 87 Jan 19 14:33 com1 1 ajit ajit 87 Jan 19 14:33 com1 #Symbolic Mode $> chmod ugo+rwx com1 #Absolute Mode

41

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.

Copyright 2004, Infosys Technologies Ltd

42

ER/CORP/CRS/OS31/003 Version No: 2.00

42

Default File Permissions.


Calculate system default file permissions 6 6 6 -0 2 2 6 4 4 - System wide default permissions - Denial mask set by UMASK - Resultant permissions that will be set on all files created (-rw-rr--)

Copyright 2004, Infosys Technologies Ltd

43

ER/CORP/CRS/OS31/003 Version No: 2.00

43

Default Directory Permissions.


Calculate system default directory permissions 7 7 7 -0 2 2 7 5 5 - System wide default permissions - Denial mask set by UMASK - Resultant permissions that will be set on all directories created (drwxr-xr-x)

Copyright 2004, Infosys Technologies Ltd

44

ER/CORP/CRS/OS31/003 Version No: 2.00

Directory Permissions. r permission only Can only list the contents of the directory, nothing else is possible. w permission only No listing files, no cd, nothing can be done on that directory! x permission only Can enter into the directory, but nothing can be done on that directory w and x permissions only Everything is possible, except listing of files. r and x permissions only Cant create a new file, can edit or delete an existing one either. It makes sense to deny write permission on a directory to others. r and w permission only Nothing is possible because cant enter into the directory, same as giving write permission alone. So, the ideal directory permission would be: drwxr-xr-(Value 754 octal) Full powers for the owner. No modification or creation of files by group, only execution and listing. Others get just listing of directory only.

44

Changing Ownership.
chown
Changing ownership for a file. Can be done only by the root or administrator. Syntax chown username file.

Copyright 2004, Infosys Technologies Ltd

45

ER/CORP/CRS/OS31/003 Version No: 2.00

chown Examples $> ls -l demo -rw-rw-r-1 ajit_nair ajit_nair 11 Jan 17 14:02 demo #The owner name of file demo is ajit_nair

$> chown sujith demo #Converting the owner name from ajit_nair to sujith $> ls -l demo -rw-rw-r-1 sujith ajit_nair 11 Jan 17 14:02 demo #The owner name is sujith

45

Changing Group.
chgrp
Changing group for a file. Can be changed by the owner of the file. Syntax chgrp groupname file.

Copyright 2004, Infosys Technologies Ltd

46

ER/CORP/CRS/OS31/003 Version No: 2.00

chgrp Examples $> ls -l demo1 -rw-rw-r-1 ajit_nair ajit_nair 11 Jan 17 14:02 demo1 #The group name of file demo1 is ajit_nair

$> chgrp sureesh demo1 #Converting the group name from ajit_nair to sureesh $> ls -l demo1 -rw-rw-r-1 ajit_nair sureesh 11 Jan 17 14:02 demo1 #The group name is sureesh

46

su command.
su Substitute user-id. An user can login as super-user without the need of logging off from the system.

Copyright 2004, Infosys Technologies Ltd

47

ER/CORP/CRS/OS31/003 Version No: 2.00

The su (Substitute User-id) command switches a user to Super User without having to logout and is equivalent to logging in as root. It requires that the user should know the password of the root. $> su

47

Summary
Compression utilities. Different Development Tools. make. SCCS. Communication Tools. File Security.

Copyright 2004, Infosys Technologies Ltd

48

ER/CORP/CRS/OS31/003 Version No: 2.00

48

Thank You!
Copyright 2004, Infosys Technologies Ltd 49 ER/CORP/CRS/OS31/003 Version No: 2.00

49

Das könnte Ihnen auch gefallen