You are on page 1of 49

Unix

Unix Utilities & Development Tools.

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

1

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

2

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

2

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

3

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

#

3

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

Copyright © 2004, Infosys Technologies Ltd

4

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

4

In most cases there is a trade-off between development time versus execution speed. Ease of program development. report generation. file editing C General purpose and system programs. fast executing programs sh General purpose programs. The nature of the application would determine the choice of a language. fast executing programs efl Writing structured FORTRAN programs Fortran 77 Number-crunching. Some programming languages available on Unix are: Program Primarily used for awk String processing. file manipulation. or do a lot of processing itself.Unix Program Development Tools. 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. statistical and mathematical applications. As such the Unix system offers a wide assortment of programming languages and development tools. engineering. 5 . Infosys Technologies Ltd 5 One of the primary goals of the Unix operating system design was ease of application program development. It is not uncommon for the shell itself to either control the execution of other programs. string manipulation (SNOBOL) ER/CORP/CRS/OS31/003 Version No: 2. process control sno Pattern matching.00 Copyright © 2004.

6 .Unix C Program Development C Program Development.

c creates an executable file by the name a.00 Example $> cc file1.o Executable code a.c Object Code .out Compiles the file file1.cc – C language Compiler Phases in creation of executable program. $> a. Infosys Technologies Ltd 7 ER/CORP/CRS/OS31/003 Version No: 2.c.out Copyright © 2004.out Executes the file #Compiles and 7 . Source Code .

o cc –o Allows to create an executable file by the name requested by the user. Infosys Technologies Ltd 8 ER/CORP/CRS/OS31/003 Version No: 2. } $> cc -c demo1. – Syntax • cc –o <file1> <filename. – No linking would be done.o> Copyright © 2004.00 cc -c Compiles the given file and creates an object code. Examples $> cc -o demo1 demo1. – Syntax • cc –c <filename.c> -o – Specify a given name for the final executable file.Important options -c – Compiles the given C file. Examples $> cat demo1.c main() { printf(“ \n Hello \n").c name demo1 $> demo1 #Execute the file demo1 Hello #Output #Creates an executable file by the 8 .c Creates an object code by name demo1.cc .

00 9 .assembly debugger – ctrace . Infosys Technologies Ltd 9 ER/CORP/CRS/OS31/003 Version No: 2.symbolic debugger Beautifier – cb .tracing execution statement by statement – sdb . Debuggers – adb .Development Tools.Automatic formatting of C programs Copyright © 2004.

00 10 . 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.Development Tools. Infosys Technologies Ltd 10 ER/CORP/CRS/OS31/003 Version No: 2.

– Useful in program systems that comprise of more than one source file.make. Copyright © 2004. Infosys Technologies Ltd 11 ER/CORP/CRS/OS31/003 Version No: 2.00 11 .Development Tools . make – Regenerates a program system by automatically tracking files that have changed since the system was last made.

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. – Picks up a file containing dependency relation among all the files.make How it works? – Works on concept of Dependency Maintenance.00 12 .

Infosys Technologies Ltd 13 ER/CORP/CRS/OS31/003 Version No: 2. thereby reducing the time spent on compiling. make utility avoids compiling all the files together. Copyright © 2004. Benefits. – Lots of compilation time is saved. 13 .00 If the project contains multiple files.make. It only recompiles the file on which changes were made. – 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.

– make –f mymakefile Copyright © 2004.00 Note : make can also be invoked without any arguments.makefile When a make command is issued. The user could also customize the name of the makefile as per choice. by default picks-up a file known as the makefile or Makefile. Infosys Technologies Ltd 14 ER/CORP/CRS/OS31/003 Version No: 2. 14 . It contains the dependency relation for the different files in the system.

c and common. Name of the destination file.o: sub.o. cc $(OBJECTS) -lm -o final try.c common.Makefile – Contents.c common.i.h # For creating add.o #Three object files make this system : try.i.o final: $(OBJECTS) # The executable would be called final.c common.e add. Copyright © 2004.o.h add.o: try.c and common.o.o sub. Infosys Technologies Ltd 15 ER/CORP/CRS/OS31/003 Version No: 2.o: add.h # For creating sub.o and sub. add. Names of the source files that make up the program system.00 Example $> cat makefile OBJECTS = try.e sub.c and common. we require 2 files .o.h # For creating try.h sub. we require 2 files .h $> 15 .o add. we require 2 files .i. Information on how to regenerate the program system.e try.

Copyright © 2004. – to automatically track changes between versions of a program.SCCS. so that as per business requirement. Infosys Technologies Ltd 16 ER/CORP/CRS/OS31/003 Version No: 2. Version Control System is required to keep track of the different changes made in the system. – to maintain a list of these changes – which has the ability to quickly recreate a particular version on request. the user could revert to the earlier version of the system. SCCS is a package of programs – to help manage the development and maintenance of large programs. – that does not waste much of disk space. 16 .00 SCCS is a Version Control System. SCCS – Source Code Control Systems.

$f 17 . admin command must be run separately for each file that is to be placed under SCCS. Infosys Technologies Ltd 17 ER/CORP/CRS/OS31/003 Version No: 2.Placing a file under SCCS. giving it the name s.c -n means new -i means install for the first time places main.sccsname – – places file under SCCS.main.00 Example $ admin -n -i main. admin -n -ifile s. Copyright © 2004. the original file must be removed to ensure the consistency of the contents of the version controlled file.main.c s. -n -i$f s.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.sccsname all SCCS files must begin with s.c under SCCS calling it s.

main.Extracting files from SCCS.main.00 Examples. Display latest version with info: $ get -p s.c Extract for editing $ get -e s. Any access to source files must be through SCCS system only get command is used for extracting from SCCS source files Copyright © 2004.main. Removes original files.c Extract for viewing but not editing $ get s. Infosys Technologies Ltd 18 ER/CORP/CRS/OS31/003 Version No: 2.c 18 .

Syntax – $ delta s.main.c Copyright © 2004.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.00 19 . Infosys Technologies Ltd 19 ER/CORP/CRS/OS31/003 Version No: 2.

00 20 .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.

it will automatically remove it. it will automatically assume it as an SCCS file and will issue the necessary get command to extract it.00 21 .Make & SCCS The ‘SCCS’ and ‘make’ utilities work well in conjunction with each other. Copyright © 2004. After ‘make’ is finished with the extracted SCCS file. If it finds it. it will automatically look for file s.f . Infosys Technologies Ltd 21 ER/CORP/CRS/OS31/003 Version No: 2. If ‘make’ does not find a particular file f.

Unix Communication Commands Unix Communication Commands. 22 .

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. write mesg wall ftp telnet Copyright © 2004. 23 .Communication Commands.

user pract2 # Sender’s name # Message to be displayed to the 24 . Infosys Technologies Ltd 24 ER/CORP/CRS/OS31/003 Version No: 2.00 Example $> whoami pract1 $> write pract2 This is a demo.write Command Writes to another user logged in. Usage – $ write userid [tty] Copyright © 2004. Can be used by any user.

mesg command To change permission for receiving messages. Infosys Technologies Ltd 25 ER/CORP/CRS/OS31/003 Version No: 2. Copyright © 2004.denies permission mesg -. $> mesg is n Note : Even if a user chooses option “n”.gives permission mesg n -.00 Mesg mesg y -. #Disables receiving of messages #Displays the status 25 .reports current status Example: $> mesg is y $> mesg n from normal users. Usage mesg [y/n/ ] This command is ineffective for messages from the administrator/root. then too he would receive the messages from the root.

wall command writes to all users currently logged in Usage .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 . Infosys Technologies Ltd 26 ER/CORP/CRS/OS31/003 Version No: 2.wall [-g group] message Can be used by any user Copyright © 2004.

ftp [hostname] Mode of transfer of data – Ascii – Binary Copyright © 2004. Syntax .ftp – File Transfer Protocol. Infosys Technologies Ltd 27 ER/CORP/CRS/OS31/003 Version No: 2. Allows the user to work with files on remote machine.00 27 . Transfer files to and from a remote machine.

– put filename get – This command is used to transfer a file from destination terminal to source terminal. Infosys Technologies Ltd 28 ER/CORP/CRS/OS31/003 Version No: 2. 28 .00 Multiple files can be selected in one command line and for that mput and mget commands are used. – get filename Copyright © 2004.ftp – File Transfer Commands put – This command is used to transfer file from the source terminal to the destination terminal. $> mget b* #Transfers all filenames starting with “b” from the destination machine to host machine. $> mput a* #Transfers all filenames starting with “a” from the source machine to the destination machine.

11 Server Authenticated Copyright © 2004. Infosys Technologies Ltd 29 ER/CORP/CRS/OS31/003 Version No: 2. Usage .158.168. Used for connecting to Unix Server. 29 .telnet [hostname] login: ajit Password: Good Evening /home/ajit> telnet 192.telnet.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.

Unix Security Features 30 .

Password. /etc/shadow cannot be viewed by a normal user. Copyright © 2004.Unix Security .00 /etc/passwd can be viewed by a normal user. Infosys Technologies Ltd 31 ER/CORP/CRS/OS31/003 Version No: 2. 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. 31 .

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 . 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./etc/passwd Unix stores the user information in the passwd file.Unix Security .

Can only be viewed by the root.:12332:0:99999:7::: trng91:$1$Z6f2. Stores the encrypted password. Infosys Technologies Ltd 33 ER/CORP/CRS/OS31/003 Version No: 2.Unix Security ./etc/shadow. Also called “Shadow Password File”. Copyright © 2004.00 $> cat /etc/shadow root:$1$lduGqK0k$fVDdj4J7zPvrcGefrXOf5/:12487::99999:::: bin:*:12227:0:99999:7::: daemon:*:12227:0:99999:7::: trng90:$1$22RPkY9l$SLMEVhOc1JoRMIOdBRj9D.MP7$jhFco/.RmA$46MpC2jOYZbdOzt6ejiA41:12332:0:99999:7::: 33 ./JuMBzDkkErAgo/:12332:0:99999:7::: trng92:$1$kbD4fCFI$3mlc37uMa4eAOFJL4sfFy1:12332:0:99999:7::: trng93:$1$wvhu.

no writing down To be changed frequently – at least once in every 6 months To be highly secretive .Good Passwords .Characteristics To be a heterogeneous mix of characters and metacharacters. Infosys Technologies Ltd 34 ER/CORP/CRS/OS31/003 Version No: 2.never used before or in use elsewhere Copyright © 2004.00 34 . To be easy on the user’s memory. difficult to guess.

etc).Changing Passwords -passwd. Changing password for ajit (current) UNIX password: New password: Retype new password: passwd: all authentication tokens updated successfully.00 Example $>passwd Changing password for user ajit. Prompts for current password. Copyright © 2004.”#”. passwd is the command used to change user password. even in masked characters (like “*”. on the screen. 35 . Infosys Technologies Ltd 35 ER/CORP/CRS/OS31/003 Version No: 2. Syntax – passwd The password entered would not be displayed.

– Read (r) – Write (w) – Execute (x) Copyright © 2004. which is identified by the 9-bit field for every file.File Security.00 Each type of user has 3 different permissions for a file. Hence for every file there are 9 composite permissions. – Owner – Group – Others Classes of Permissions for a file. Classes of users for a file. Infosys Technologies Ltd 36 ER/CORP/CRS/OS31/003 Version No: 2. 36 .

Two modes of setting file permissions – Absolute Mode (Octal Integer) – Symbolic Mode (String) Copyright © 2004.File Security.00 37 . Infosys Technologies Ltd 37 ER/CORP/CRS/OS31/003 Version No: 2.

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. 38 . Example rwxrw-r-. others have only read permission. The write (w) permission is assigned the value 2.File Permission . Uses numbers for mentioning the permissions. Infosys Technologies Ltd 38 ER/CORP/CRS/OS31/003 Version No: 2.indicates the owner has read. write and execute permissions. the group has read and write permissions. “-” indicates no permission has been set. The execute (x) permission is assigned the value 1.00 The read (r) permission is assigned the value 4.Absolute Mode.

00 39 . Copyright © 2004. Example – u+rx – Indicates the user has read and execute permissions. Infosys Technologies Ltd 39 ER/CORP/CRS/OS31/003 Version No: 2. Uses characters & arithmetic operators for mentioning the permissions.File Permission – Symbolic Mode.

Infosys Technologies Ltd 40 ER/CORP/CRS/OS31/003 Version No: 2.00 40 .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.

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 .chmod . – Syntax.Setting Permissions chmod – Used for setting file and directory permissions. Infosys Technologies Ltd 41 ER/CORP/CRS/OS31/003 Version No: 2. • chmod [0-7][0-7][0-7] filename • chmod [ugo][+-][rwx] filename (Absolute Mode) (Symbolic Mode) Copyright © 2004.

File Permissions . Copyright © 2004. – The value can be changed. Infosys Technologies Ltd 42 ER/CORP/CRS/OS31/003 Version No: 2. – Default value is 022.umask umask – Stands for user creation mask.00 42 . – Sets default permissions for a newly created file and directory.

Denial ‘mask’ set by UMASK .Default File Permissions.Resultant permissions that will be set on all files created (-rw-r—r--) Copyright © 2004. Infosys Technologies Ltd 43 ER/CORP/CRS/OS31/003 Version No: 2.00 43 . Calculate system default file permissions 6 6 6 -0 2 2 6 4 4 .System wide default permissions .

‘r’ and ‘w’ permission only Nothing is possible because can’t enter into the directory. 44 . nothing can be done on that directory! ‘x’ permission only Can enter into the directory. No modification or creation of files by group. except listing of files. no cd. So. nothing else is possible.System wide default permissions .Default Directory Permissions. Infosys Technologies Ltd 44 ER/CORP/CRS/OS31/003 Version No: 2. ‘r’ permission only Can only list the contents of the directory. only execution and listing. Others get just listing of directory only. same as giving write permission alone. ‘w’ permission only No listing files. can edit or delete an existing one either.Denial ‘mask’ set by UMASK . the ideal directory permission would be: drwxr-xr-(Value 754 octal) Full powers for the owner. but nothing can be done on that directory ‘w’ and ‘x’ permissions only Everything is possible. It makes sense to deny write permission on a directory to others. Calculate system default directory permissions 7 7 7 -0 2 2 7 5 5 .Resultant permissions that will be set on all directories created (drwxr-xr-x) Copyright © 2004.00 Directory Permissions. ‘r and ‘x’ permissions only Can’t create a new file.

– Can be done only by the root or administrator. – Syntax • chown username file. Copyright © 2004.Changing Ownership.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 . Infosys Technologies Ltd 45 ER/CORP/CRS/OS31/003 Version No: 2. chown – Changing ownership for a file.

Changing Group. – Can be changed by the owner of the file. 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 . Copyright © 2004. chgrp – Changing group for a file. – Syntax • chgrp groupname file.

It requires that the user should know the password of the root. An user can login as super-user without the need of logging off from the system. su – Substitute user-id. Copyright © 2004. $> su 47 .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.su command. Infosys Technologies Ltd 47 ER/CORP/CRS/OS31/003 Version No: 2.

Summary Compression utilities. File Security. SCCS. make.00 48 . Different Development Tools. Communication Tools. Infosys Technologies Ltd 48 ER/CORP/CRS/OS31/003 Version No: 2. Copyright © 2004.

00 49 . Infosys Technologies Ltd 49 ER/CORP/CRS/OS31/003 Version No: 2.Thank You! Copyright © 2004.