You are on page 1of 203

KUVEMPU UNIVERSITY

INFORMATION TECHNOLOGY PROGRAMMES


Bachelor of Science in Information Technology - B.Sc. (IT) Master of Science in Information Technology - M.Sc. (IT)

M.Sc.(IT) - 3rd Semester

MSIT - 3A

Linux Internals

Directorate of Distrance Education

Kuvempu University
Shankaraghatta, Shimoga District, Karnataka

In collabora tion with

Universal Education Trust


Bangalore

PDF created with pdfFactory Pro trial version www.pdffactory.com

Titles in this Volume : MSIT - 3A Linux Internals

Prepared by UNIVERSAL EDUCATION TRUST (UET) Bangalore

First Edition : May 2005

Copyright by UNIVERSAL EDUCATION TRUST, Bangalore All rights reserved

No Part of this Book may be reproduced in any form or by any means without the written permission from Universal Education Trust, Bangalore.

All Product names and company names mentioned herein are the property of their respective owners.

NOT FOR SALE For personal use of Kuvempu University IT - Programme Students only.

Corrections & Suggestions for Improvement of Study material are invited by Universal Education Trust, Bangalore. E-mail : info@uetb.org

Printed at :

Mayur Print Ads


Bangalore - 20 Ph : 94480 - 42724

PDF created with pdfFactory Pro trial version www.pdffactory.com

LINUX INTERNALS
(MSIT - 3A)

: Contributing Authors :

Sri. M.V. Panduranga Rao


Research Scholar, National Institute of Technology, Karnataka Formerly KREC, Suratkal, Mangalore. Lecturer, Dept. of Information Science & Engg. JNNCE, Shimoga - 577204. &

Smt. K. Roopa
Lecturer, Dept. of C.Sc. & Engg. JNNCE, Shimoga - 577204

Under the guidance of

Smt. K. Roopa Dr. K. Chandrashekar Shet


Professor Department of Computer Engineering National Institute of Technology, Karnataka Surathkal, Mangalore, INDIA P.O. Srinivasnagar - 575025

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

Contents

Chapter 1 INTRODUCTION 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 Introduction and Objectives........................................................ Unix and Linux: Features ......................................................... Unix System Architecture ........................................................ Unix Philosophy ...................................................................... What is Linux? ........................................................................ Using a Linux System: Login .................................................... Linux Command Line .............................................................. Logging Out ........................................................................... Command Syntax .................................................................... Files ...................................................................................... Linux Directories...................................................................... File System Organization............................................................ Creating Files with cat............................................................... Displaying Files Contents with cat.............................................. Deleting Files with rm .............................................................. Unix Command Feedback .......................................................... Copying and Renaming Files with cp and mv .............................. Filename Completion ................................................................. Command History .................................................................... Summary.................................................................................. Check Your Progress................................................................. 1 1 2 3 4 4 5 5 6 6 6 7 7 10 10 11 11 11 12 12 12 13

PDF created with pdfFactory Pro trial version www.pdffactory.com

VI
Chapter 2 LINUX COMMANDS 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10 2.11 2.12

Chapter 1 - Introduction

18 18 19 21 22 24 27 27 28 28 30 30 31 31

Introduction and objectives ........................................................ Basic Linux Commands............................................................. Help commands........................................................................ General commands.................................................................... File management commands...................................................... Finding files.............................................................................. Linux Files and File Permission................................................... File names and permission characters.......................................... Examples on files...................................................................... Setting Keystrokes.................................................................... Keys and terminal configuration ................................................. Summary.................................................................................. Check Your Progress.................................................................

Chapter 3 PROCESSES 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 Introduction and objectives......................................................... Looking at Processes................................................................. Process IDs............................................................................. Viewing Active Processes.......................................................... ps Output Formats..................................................................... Killing a Process....................................................................... Creating Processes................................................................... Creating Using system()............................................................ Creating Using system calls fork, exec..................................... Creating by Calling fork............................................................. Creating by Using the exec Family.............................................. Creating by Using fork and exec Together................................... Process Scheduling.................................................................... Signals..................................................................................... Some Linux daemons................................................................. popular daemons....................................................................... Zombie Proces.......................................................................... Threads.................................................................................... 37 37 38 38 39 39 41 42 42 43 43 44 45 45 46 47 48 50 51

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals 3.18 Processes Vs. Threads.............................................................. 3.19 Summary.................................................................................. 3.20 Check Your Progress................................................................. Chapter 4 SHELL PROGRAMMING 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 Introduction and Objectives........................................................ Creating a shell script................................................................ Meta characters........................................................................ Positional Parameters and Shell Variables.................................... Built-in shell commands and variables.......................................... Iteration, control and if statements.............................................. Shell operators ........................................................................ The test Command.................................................................... An example script..................................................................... Environment Variables .............................................................. Summary.................................................................................. Check Your Progress.................................................................

VII
52 53 54

60 60 61 62 63 64 66 68 69 71 72 73 74

Chapter 5 FILE SYSTEM 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 5.12 5.13 5.14 Introduction and Objectives ....................................................... Filesystem Overview ................................................................ Types of Files........................................................................... Directory pointer....................................................................... Directory Hierarchy ................................................................. Pathnames .............................................................................. Current Directory ..................................................................... Dot (.) and DotDot(..) .............................................................. Moving and Copying Files ......................................................... Removing Files ........................................................................ Operations on Directories ......................................................... Inodes .................................................................................... Options of Inodes ................................................................... Links ...................................................................................... Hard links ............................................................................... 79 79 80 80 83 83 84 85 85 86 86 87 87 88 88 89

PDF created with pdfFactory Pro trial version www.pdffactory.com

VIII
5.15 5.16 5.17 5.18 5.19 5.20 5.21 5.22 5.23 5.24 5.25 5.26 5.27 5.28 5.29 5.30

Chapter 1 - Introduction

Soft links ................................................................................. Access Control and UID .......................................................... Categories of Access Control .................................................... Access Control Example ........................................................ Changing Access Previleges: hmod............................................ chmod symbolically .................................................................. chmod numerically ................................................................... umask Masking Privileges ......................................................... Filesystem Structure ................................................................. /etc/fstab - Example ................................................................ Special Files - /dev ................................................................ Special Files - /proc ............................................................... Mounting Additional Volumes .................................................... Mounting shared filesystems ..................................................... Summary ................................................................................ Check Your Progress................................................................

89 90 91 91 91 92 92 92 93 93 93 94 94 95 95 96

Chapter 6 FILE SYSTEM ADMINISTRATION 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 6.12 6.13 6.14 6.15 6.16 6.17 Introduction and objectives ........................................................ Inode in depth ......................................................................... Inode table entry ...................................................................... Links: relationship with inode ..................................................... Hard links ............................................................................... Soft links ................................................................................. Non-native filesystems ............................................................ Disk checking and recovery (fsck) ............................................. Check a Linux ext2 filesystem (e2fsck)....................................... Disk free space (df).................................................................. Disk usage (du)......................................................................... Disk partitioning concepts .......................................................... A Partitioned Disk ................................................................... Making and changing partitions .................................................. How many partitions? ............................................................... What size partitions? ................................................................. BIOS problems with LILO and partitions .................................... Disk partitioning tools ................................................................ 102 102 103 103 104 105 105 106 106 107 107 108 108 109 109 110 110 110 111

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals 6.18 6.19 6.20 6.21 6.22 6.23 6.24 6.25 Using fdisk()............................................................................. The fdisk interface ................................................................... Interactive commands in fdisk..................................................... Making Linux filesystems (mke2fs) ............................................ Block devices ........................................................................... Character devices ..................................................................... Summary ................................................................................. Check Your Progress.................................................................

IX
111 112 112 113 113 114 114 115

Chapter 7 SYSTEM ADMINISTRATION 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10 Introduction and objectives......................................................... The superuser account............................................................... User accounts /etc/passwd......................................................... Groups..................................................................................... Managing Linux users................................................................ User reference files................................................................... User modifications.................................................................... Examples of making changes to permissions................................ Some administration commands.................................................. Summary.................................................................................. Check Your Progress................................................................. 123 123 124 124 125 127 128 128 129 130 132 132

Chapter 8 EDITORS AND TOOLS 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 8.10 Introduction and objectives......................................................... Linux VI editor......................................................................... The emacs editor...................................................................... System and network configuration............................................... X Configuration........................................................................ Library and kernel dependency management................................ General Diagnostic.................................................................... To remotely use Xwindows from Ms Windows............................. Network application commands.................................................. Summary.................................................................................. Check Your Progress................................................................. 135 135 136 138 140 140 141 141 142 143 144 145

PDF created with pdfFactory Pro trial version www.pdffactory.com

X
Chapter 9 ISTALLATION OF LINUX 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 9.10 9.11 9.12 9.13 9.14 9.15 9.16 9.17 9.18

Chapter 1 - Introduction

148 148 149 150 150 151 151 152 153 154 154 155 155 156 156 158 159 162 164 164

Introduction and objectives......................................................... Devices of installation................................................................ Partitioning hard disk................................................................. Partition sizes........................................................................... Mount points............................................................................. Mounting partitions at startup..................................................... The fstab file............................................................................ Know your hardware................................................................. Creating the Linux boot disk....................................................... Making a diskette under MS-DOS............................................... Making a diskette under a Linux-Like OS..................................... Beginning the installation of Linux .............................................. Installation options - class and method ........................................ Manual configurations................................................................ Package installations.................................................................. Standard linux partition filesystem types....................................... Network administration tools....................................................... Summary.................................................................................. Check Your Progress.................................................................

Chapter 10 GLOSSARY 169

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 1

Introduction

1.0 INTRODUCTION AND OBJECTIVES

inux is an operating system that was first created at the University of Helsinki in Finland by a young student named Linus Torvalds. At this time the student was working on a UNIX system that was running on an expensive platform. Because of his low budget, and his need to work at home, he decided to create a copy of the UNIX system in order to run it on a less expensive platform, such as an IBM PC. He began his work in 1991 when he released version 0.02 and worked steadily until 1994 when version 1.0 of the Linux Kernel was released. There are no royalty or licensing fees for using Linux, and the source code can be modified to fit your needs. Because it comes with source code to the kernel, it is quite portable. Linux runs on more CPUs and platforms than any other computer operating system. Linux is a true multi-tasking operating system similar to its brother, UNIX. It uses sophisticated, stateof-the-art memory management to control all system processes. That means that if a program crashes you can kill it and continue working with confidence. Another benefit is that Linux is practically immunized against all kinds of viruses that we find in other operating systems.

Objectives
At the end of this unit, You would be able to know important features of Linux Unix system Architecture Significance of using a Linux System

Chapter 1 - Introduction

11

PDF created with pdfFactory Pro trial version www.pdffactory.com

12
Linux command Line

Chapter 1 - Introduction

Exhibit login, logoff, Issue basic commands & switching to another account Linux directories and file system organization Know commands related to file retrievals

1.1 Unix and Linux: features


Linux is based on Unix. Unix philosophy. Unix commands. Unix standards and conventions. There is some variation between Unix operating systems. Especially regarding system administration. Often Linux-specific things in these areas. Important features of Linux Full multitasking and 32-bit support. Linux, like all other versions of UNIX, is a real multitasking system, allowing multiple users to run many programs on the same system at once. Linux is also a full 32-bit operating system, utilizing the special protected-mode features of Intel 80386 and later processors and their work-alikes. Linux inherited all the features of UNIX operating system, henceforth we have used the name Linux or UNIX for learning the concepts. The X Window System. The X Window System is the de facto industry-standard graphics system for UNIX machines. A complete version of the X Window system, known as XFree86, is available for Linux. The X Window System is a very powerful graphics interface, supporting many applications. TCP/IP (Transmission Control Protocol/Internet Protocol) support. This is the set of protocols that links millions of university and business computers into a worldwide network known as the Internet. With an Ethernet connection, you can have access to the Internet or to a local area network from your Linux system. Using SLIP (Serial Line Internet Protocol) or PPP (Point to Point Protocol), you can access the Internet over phone lines with a modem. Virtual memory and shared libraries. Linux can use a portion of your hard drive as virtual memory, expanding your total amount of available RAM. Linux also implements shared libraries, allowing programs that use standard subroutines to find the code for these subroutines in the libraries at runtime. This saves a large amount of space on your system; each application doesnt store its own copy of these common routines.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

13

The Linux kernel uses no code from AT&T or any other proprietary source. Much of the software available for Linux is free. In fact, a large number of utilities in Linux are developed by the GNU project at the Free Software Foundation in Cambridge, Massachusetts. However, Linux enthusiasts, hackers, programmers, and recently even commercial companies from all over the world have contributed to the growing pool of Linux software. Linux supports (almost) all of the features of commercial versions of UNIX. In fact, some of the features found in Linux may not be available on other proprietary UNIX systems. GNU software support. Linux supports a wide range of free software written by the GNU Project, including utilities such as the GNU C and C++ compiler, gawk, groff, and so on. Many of the essential system utilities used by Linux are GNU software. Linux is compatible with the IEEE POSIX.1 standard. Linux has been developed with software portability in mind, thus supporting many important features of other UNIX standards. Virtual memory support. Linux utilizes all of your systems memory, without memory limits or segmentation through the use of a virtual memory manager. Built-in support for networking, multitasking, and other features. Youll see this touted as New Technology in systems such as Windows NT. In fact, UNIX (and now, Linux) has implemented this new technology for more than 15 years. Linux is cheaper to get than most commercially available UNIX systems and UNIX clones. If you have the patience and access to the Internet, the only price you pay for Linux is your time. Linux is freely available on the Internet.

1.2 UNIX SYSTEM ARCHITECTURE


The shell and the window environment are programs. Programs only access to hardware is via the kernel. Kernel understands only system calls, but not user commands. Shell converts user commands to system calls and pass on to the kernel. Kernel is the core part of Linux, hence it is also called the operating system.

PDF created with pdfFactory Pro trial version www.pdffactory.com

14 1.3 UNIX PHILOSOPHY

Chapter 1 - Introduction

Fig. 1.1 Unix Architecture

Multi-user. A user needs an account to use a computer. Each user must log in. Complete separation of different users files and configuration settings.

Small components. Each component should perform a single task. Multiple components can be combined and chained together for more complex tasks. An individual component can be substituted for another, without affecting other components.

1.4 WHAT IS LINUX?


Linux kernel. Developed by Linus Torvalds. Strictly speaking, Linux is just the kernel. Associated utilities. Standard tools found on (nearly) all Linux systems. Many important parts come from the GNU project. Free Software Foundations project to make a free Unix. Some claim the OS as a whole should be GNU/Linux.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

15

Linux distributions. Kernel plus utilities plus other tools, packaged up for end users. Generally with installation program. Distributors include: Red Hat, Debian, SuSE, Mandrake.

1.5 USING A LINUX SYSTEM: LOGIN


Login prompt displayed. When Linux first loads after booting the computer. After another user has logged out. Need to enter a username and password. The login prompt may be graphical or simple text. If text, logging in will present a shell. If graphical, logging in will present a desktop. Some combination of mouse key and keystrokes will make a terminal window appear. A shell runs in the terminal window.

1.6 LINUX COMMAND LINE


The shell is where commands are invoked. A command is typed at a shell prompt. Prompt usually ends in a dollar sign ($)[bash Bourne again shell or sh Bourne shell]. After typing a command press Enter to invoke it. The shell will try to obey the command. Another prompt will appear. Example: $ date Thu Jun 14 12:28:05 BST 2001 $ The dollar represents the prompt in this course do not type it.

PDF created with pdfFactory Pro trial version www.pdffactory.com

16 1.7 LOGGING OUT


To exit from the shell, use the exit command. Pressing Ctrl+D at the shell prompt will also quit the shell. Quitting all programs should log you out.

Chapter 1 - Introduction

If in a text-only single-shell environment, exiting the shell should be sufficient. In a window environment, the window manager should have a log out command for this purpose. After logging out, a new login prompt should be displayed.

1.8 COMMAND SYNTAX


Most commands take parameters. Some commands require them. Parameters are also known as arguments. For example, echo simply displays its arguments: $ echo $ echo Hello there Hello there Commands are case-sensitive. Usually lower-case. $ echo whisper whisper $ ECHO SHOUT bash: ECHO: command not found.

1.9 FILES
Data can be stored in a file. Each file has a filename. A label referring to a particular file.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

17

Permitted characters include letters, digits, hyphens (-), underscores (_), and dots (.) Case-sensitive NewsCrew.mov is a different file from NewScrew.mov. The functions usually performed on a file are as follows: v Opening a file for processing. v Reading data from a file for processing. v Writing data to a file after processing. v Closing a file after all the necessary processing has been done.

1.10 LINUX DIRECTORIES


As you probably already know from working in graphics mode, in Linux the directories use the slash (/) as a separator (Windows uses backslash (\)). In other words it works just like websites or ftp servers. Any directory which starts with a slash, such as /usr/bin, means it is an absolute name - the name specifies the entire sequence of directories from the root directory (/) up to the specific directory being requested (bin). Thus, it doesnt matter which directory is the current directory when you specify that name, it will always point to the /usr/bin directory. On the other hand a directory which does not start with a slash is relative to the current directory. For example the directory bin will point to different directories depending on whether you are in the root directory (in which case it will point to /bin). It may reside in the /usr directory (in which case it will point to /usr/bin) or in the /usr/local directory (in which case it will point to /usr/local/bin). The same applies to files - if you specify file.txt it is assumed to be in the current directory, while if you specify /tmp/file.txt it will always point to file.txt in the temporary directory. Two special directory names are the current directory, represented by a single period (.) and the parent directory, represented by a double period (..). Thus, if you are in the /home/ sandbox directory and type in ls .. , it will list the contents of the parent directory, which is /home.

1.11 FILE SYSTEM ORGANIZATION


In this section, you will learn about the ways LINUX provides for organizing files so that you can easily locate and use them. LINUX has provided the directory as a way of organizing files. The directory is a special file under which you can have files or more directories (also

PDF created with pdfFactory Pro trial version www.pdffactory.com

18

Chapter 1 - Introduction

referred to as subdirectories). You can visualize the LINUX file structure as a bottom-up tree with the root at the top. Thus, the top-level directory is called the root directory and is denoted by a single / (forward slash). All the directories and files belong to the root directory. Below is a list of some common directories that are found in Linux and Unix systems, and what they are used for. / v This is the root directory, inside which all other directories reside v This is similar to the root directory of a drive in Windows (C:\), except that in Linux even different hard disks reside within this root. /bin v This stands for binary, and contains program (executable) files. This (and other bin directories) is where commands such as ls can be found. v In Windows, the c:\windows\commands holds some of command-line programs, but others are scattered in various other directories. /dev v This stands for devices. It contains a number of special pseudo-files that are used to access the physical hardware that make up, or are connected to, your computer. For example the parallel-port would be a file called lp0 in this directory, while the hard disk would be hda, and its first partition would be hda0. v Windows / DOS uses a similar method, however in Windows these are not in any particular directory. Devices have names like LPT1, COM1 or CON - any time you try to access a file with that name from any directory, you will get the parallel printer, serial port or console, respectively. /etc v This is where (almost) all system-wide configuration information is stored. Almost all configuration information is stored in text files, so you can go into this directory and have a look around with a text viewer if you like. Some of the files are quite cryptic though. v There is no equivalent in Windows, where configuration data can be stored anywhere, including the registry, INI files and other data files in various directories. /home v This is where users home directories are usually found. Thus, if you created a user called sandbox, there will be a directory with the same name in this directory, which will be that users home directory.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

19

v The nearest equivalent in Windows is c:\windows\profiles, where some user-specific data is held, together with c:\My Documents, where user-created documents go. However other data can be written in many other directories. /lib v This is where the library files are found. Libraries are files containing reusable functions and routines for programs to use. v There is no equivalent in Windows / DOS. /mnt v This is where storage devices other than the hard disks are mounted. This directory usually contains subdirectories called cdrom, floppy, etc., which - when these devices are mounted - show the contents of the CD-ROM or floppy disk respectively. Your Windows drives may also be automatically mounted in this directory. v There is no equivalent in Windows / DOS. /opt v This is where optional components of the system are installed. Products such as KDE, Gnome and Oracle may be installed into this directory. v The nearest thing in Windows is the c:\Program Files directory. /tmp v This is a temporary directory. All files placed in here will automatically be deleted eventually. v The equivalent in Windows / DOS is c:\windows\temp. /usr v Contains a copy of most of the directories in the root. For example, there is a bin directory containing programs, a lib directory containing libraries, etc. Usually, core Linux files are contained in the root directories, while non-core files are in the /usr subdirectories. v There is no equivalent in Windows / DOS. /var v Stands for various. Among the files stored here are the system log files, spool files and other data files. v There is no equivalent in Windows / DOS.

PDF created with pdfFactory Pro trial version www.pdffactory.com

20 1.12 CREATING FILES WITH CAT


There are many ways of creating a file. One of the simplest is with the cat command: $ cat > shopping_list cucumber bread milk fish fingers

Chapter 1 - Introduction

Note the greater-than sign (>) this is necessary to create the file. The text typed is written to a file with the specified name. Press Ctrl+D after a line-break to denote the end of the file. The next shell prompt is displayed.

ls demonstrates the existence of the new file.

1.13 DISPLAYING FILES CONTENTS WITH CAT


There are many ways of viewing the contents of a file. One of the simplest is with the cat command: $ cat shopping_list cucumber bread milk fish fingers Note that no greater-than sign is used. The text in the file is displayed immediately: Starting on the line after the command. Before the next shell prompt.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

21

1.14 DELETING FILES WITH RM


To delete a file, use the rm (remove) command. Simply pass the name of the file to be deleted as an argument: $ rm shopping_list The file and its contents are removed. There is no recycle bin. There is no unrm command. The ls command can be used to confirm the deletion.

1.15 UNIX COMMAND FEEDBACK


Typically, successful commands do not give any output. Messages are displayed in the case of errors. The rm command is typical. If it manages to delete the specified file, it does so silently. There is no File shopping_list has been removed message. But if the command fails for whatever reason, a message is displayed. The silence can be be off-putting for beginners. It is standard behaviour, and doesnt take long to get used to.

1.16 COPYING AND RENAMING FILES WITH CP AND MV


To copy the contents of a file into another file, use the cp command: $ cp CV.pdf old-CV.pdf To rename a file use the mv (move) command: $ mv commitee_minutes.txt committee_minutes.txt Similar to using cp then rm. For both commands, the existing name is specified as the first argument and the new name as the second. If a file with the new name already exists, it is overwritten.

PDF created with pdfFactory Pro trial version www.pdffactory.com

22 1.17 FILENAME COMPLETION


The shell can make typing filenames easier.

Chapter 1 - Introduction

Once an unambiguous prefix has been typed, pressing Tab will automatically type the rest. For example, after typing this: $ rm sho pressing Tab key (key board) may turn it into this: $ rm shopping_list

This also works with command names. For example, da may be completed to date if no other commands start da.

1.18 COMMAND HISTORY


Often it is desired to repeat a previously executed command. The shell keeps a command history for this purpose. Use the Up and Down cursor keys to scroll through the list of previous commands. Press Enter to execute the displayed command. Commands can also be edited before being run. Particularly useful for fixing a typo in the previous command. The Left and Right cursor keys navigate across a command. Extra characters can be typed at any point. Backspace deletes characters to the left of the cursor. Del and Ctrl+D delete characters to the right. Take care not to log out by holding down Ctrl+D too long.

1.19 SUMMARY
An operating system is an important part of a computer system. You can view a computer system as being built from three general components: the hardware, the operating system, and the applications. The hardware includes pieces such as a central processing unit (CPU), a keyboard, a hard drive, and a printer. You can think of these as the parts you are able to touch physically. Applications are why you use computers; they use the rest of the system to perform the desired task (for example, play a game, edit a memo, send electronic mail). The operating system is the component that on one side manages and controls the hardware and on the other manages the applications.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

23

The operating system controls all the resources of the computer system and that control is given to the user. The user runs his applications to achieve the goal.

1.20 CHECK YOUR PROGRESS


I. Exercises
1. a. Log in. b. Log out. c. Log in again. Open a terminal window, to start a shell. d. Exit from the shell; the terminal window will close. e. Start another shell. Enter each of the following commands in turn. date whoami hostname uname uptime 2. a. Use the ls command to see if you have any files. b. Create a new file using the cat command as follows: $ cat > hello.txt Hello world! This is a text file. Press Enter at the end of the last line, then Ctrl+D to denote the end of the file. c. Use ls again to verify that the new file exists. d. Display the contents of the file. e. Display the file again, but use the cursor keys to execute the same command again without having to retype it. 3. a. Create a second file. Call it secret-of-the-universe, and put in whatever content you deem appropriate. b. Check its creation with ls.

PDF created with pdfFactory Pro trial version www.pdffactory.com

24

Chapter 1 - Introduction

c. Display the contents of this file. Minimise the typing needed to do this: Scroll back through the command history to the command you used to create the file. Change that command to display secret-of-the-universe instead of creating it. 4. After each of the following steps, use ls and cat to verify what has happened. a. Copy secret-of-the-universe to a new file called answer.txt. Use Tab to avoid typing the existing files name in full. b. Now copy hello.txt to answer.txt. Whats happened now? c. Delete the original file, hello.txt. d. Rename answer.txt to message. e. Try asking rm to delete a file called missing. What happens? f. Try copying secret-of-the-universe again, but dont specify a filename to which to copy. What happens now?

II. Choose the correct answer: 1. Linux is invented by_________.


a) Linus Torvalds b) Dennis Ritchie c) James Gosling d)None of these. 2. Operating system is a collection of _______________. a) S/w programs b) system programs c) Application programs d) all of these. 3. Linux components include the __________. a) Kernel b) shell c) file system & user programs d) all of these. 4. NFS means _________________________. a) Network File System b) Network FAT System c) NetBios System d) None of the above 5. Device Drivers control the interaction between the _________________. a) I/p device & operating system b) Operating system & o/p device

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

25

c) Operating system & hardware device d) All of the above 6. The standard way of mounting file systems onto the _________ directory. a) dev 7. b) mnt c) bin d) all of these

Linux supports ___________ file systems. a) Single b) NFS c) multiple d) all of these

8.

Linux support __________. a) multitasking only b) 32-bit c) Networking d) All of the above.

9.

In Linux the directories uses the ____ as a separator. a) / b) \ c) // d) all of these

10.

shell is a ___________. a) command interpreter b) operating system c) system call d) All of the above

III. Say True or False 1. Linux is a 16-bit operating system.


2. 3. 4. 5. Linux is a multi-processor operating system. Linux supports many different file systems. Block size for floppy device is 1024 bytes. KERNEL accepts instructions from the shell and carries them out

True/False True/False True/False True/False True/False

PDF created with pdfFactory Pro trial version www.pdffactory.com

26
IV. Essay Type Questions 1) What is Linux? Write brief features regarding Linux.
2) 3) 4) 5) 6) 7) 8) Explain the architecture of Unix. Explain the concept of filesystem. Elucidate the functions usually performed on a file. Explain how the directories are manipulated in Linux. Describe how the file systems are arranged? Give the types.

Chapter 1 - Introduction

Exhibit the commands for login, logoff & switching to another account. Enumerate the commands related to file retrivals.[ Ex: cat, rm, cp, mv ]

V. Further readings and other activities 1. The student shall get more information using man or info or any other help command for pwd, mkdir, cd, rmdir, sh, csh, ksh
Ex: man pwd OR info ls OR help cd OR apropos cp 2. 3. 4. Get new commands from the list given at the end of any man command information at the place see also. Using man intro, get the information about different kinds of commands present in your kind of linux system. For further readings you can refer the following books Title: Linux internals Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.pandurangarao.i8.com/doc/linux/linuxbook.html http://www.raomvp.bravepages.com/doc/linux/linuxbook.html A complete reference for you to get more information. You can also extract solutions for exercises. Title: Linux in a Nutshell Author: Jessica Perry Hekman and the staff of OReilly & Associates; ISBN 1-56592167-4; 1997. A complete reference for Linux. Title: Harley Hahns Student Guide to UNIX, second edition; McGraw- Hill; ISBN 007-025492-3; 1996. Not just for students, this is a complete and very readable guide to LINUX and networking.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

27

VI. Solutions II. Choose the correct answer: II. 1. a 2. b 3. d 4. a III. Say True or False III 1. false 2. true
5. d 6. b 7. d 8. d 9. a 10. a

3. true

4. false

5. true

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 2

Linux Commands

2.0 INTRODUCTION AND OBJECTIVES

inux is a complete multitasking, multi-user operating system that behaves like the UNIX operating system in terms of kernel behavior and peripheral support. Linux has all the features of UNIX, plus several recent extensions that add new versatility to Linux. All source code for Linux and its utilities is freely available. The Linux kernel was originally developed for the Intel 80386 CPUs protected mode. The 80386 was designed with multitasking in mind (despite the fact that most of the Intel CPUs are used with singletasking DOS), and Linux makes good use of the advanced features built into the CPUs instruction set. Memory management is especially strong with the 80386 (compared to earlier CPUs). A floating-point emulation routine allows Linux to function on machines that do not have math coprocessors (such as the SX series of Intel CPUs). Linux allows shared executables so that if more than one copy of a particular application is loaded (either by one user running several identical tasks, or several users running the same task), all the tasks can share the same memory. This process, called copy-on-write pages, makes for much more efficient use of RAM. The Linux kernel also supports demand paging, which means that only sections of a program that are necessary are read into RAM. To further optimize memory usage, Linux uses a unified memory pool. This pool enables all free memory on the system to be used as disk cache, effectively speeding up access to frequently used programs and data. As memory usage increases, the amount of cache is automatically adjusted. To support large memory requirements when only small amounts of physical RAM are available, Linux supports swap space. Swap space enables pages of memory to be written to a reserved area of a

28

Chapter 2 - Linux Commands

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

29

disk and treated as an extension of physical memory. By moving pages back and forth between the swap space and RAM, Linux can effectively behave as if it had more physical RAM than it does, albeit at the cost of some speed due to the hard drives slower access. Linux uses dynamically shared libraries extensively. Dynamically shared libraries use a common library section for many different applications, effectively reducing the size of each application. Linux does allow full library linking (called statically linked libraries) for portability to machines that may not have the dynamic libraries. To make Linux widely acceptable, it supports a number of different filesystems, including those compatible with DOS and OS/2. Linuxs own primary filesystem, called ext2fs, is designed for optimal use of the disk. Linux is ideally suited for application development and experimentation with new languages. Several different compilers, including C, C++, Fortran, Pascal, Modula-2, LISP, Ada, Basic, and Smalltalk, come with the distribution software. Many of the Linux compilers, tools, debuggers, and editors are from the Free Software Foundations GNU project. After completing this module you should be able to understand and utilize these features of the Linux commands: Basic Linux commands Help commands General commands File management commands Finding files Linux files and file Permission File names and permission characters Example to extract information from files Setting keystrokes Keys and terminal configuration

2.1 BASIC LINUX COMMANDS


A UNIX command is a series of characters that we type. To invoke a command, simply type the command name, followed by arguments (if any), to indicate to the shell that we are done typing and are ready for the command to be executed, press Enter.

PDF created with pdfFactory Pro trial version www.pdffactory.com

30

Chapter 2 - Linux Commands

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

31

2.2 HELP COMMANDS


any_command help | more Display a brief help on a command (works with most commands). For example, try cp help |more help works similar to DOS /h switch. The more pipe is needed when the output is longer than one screen. man topic Display the contents of the system manual pages (help) on the topic. Press q to quit the viewer. Try man man if we need any advanced options. The command info topic works similar to man topic, yet it may contain more up-to-date information. Manual pages can be hard to readthey were written for UNIX programmers. Try any_command help for a brief, easier to digest help on a command. Some programs also come with README or

PDF created with pdfFactory Pro trial version www.pdffactory.com

32

Chapter 2 - Linux Commands

other info fileshave a look to the directory /usr/share/doc. To display manual page from a specific section, We may use something like: man 3 exit (this displays an info on the command exit from section 3 of the manual pages) or man -a exit (this displays man pages for exit from all sections). The man sections are: Section 1-User Commands, Section 2-System Calls, Section 3-Subroutines, Section 4-Devices, Section 5-File Formats, Section 6-Games, Section 7-Miscellaneous, Section 8-System Administration, Section 9, Section n-New. info topic Display the contents of the info on a particular command. info is a replacement for man pages so it contains the most recent updates to the system documentation. Use <Space> and <BkSpace> to move around or we may get confused. Press q to quit. A replacement for the somewhat confusing info browsing system might be pinfo - try if we like it any better. apropos topic Give me the list of the commands that have something to do with my topic. If does not work run $ updatedb as root. whatis topic Give me a short list of commands matching my topic. whatis is similar to apropos (see the command above)they both use the same database. But whatis searches keywords, while apropos also searches the descriptions of the keywords. help command Display brief info on a bash (shell) built-in command. Using help with no command prints the list of all bash built-in commands. The shortest list of bash built-in commands would probably include: alias, bg, cd, echo, exit, export, fg, help, history, jobs, kill, logout, pwd, set, source, ulimit, umask, unalias, unset. kdehelpcenter & (in X-terminal). Browse the whole system help using the graphical KDE help navigator. Normally, KDE help is invoked by pressing the appropriate icon on the KDE control panel. Use gnome-help-browser for the GNOME equivalent. (The older KDE versions used kdehelp& instead of kdehelpcenter&).

2.3 GENERAL COMMANDS


pwd Print working directory, i.e., display the name of my current directory on the screen.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

33

hostname Print the name of the local host (the machine on which We are working). Use netconf (as root) to change the name of the machine.

whoami Print my login name.

id username Print user id (uid) and his/her group id (gid), effective id (if different than the real id) and the supplementary groups.

who Determine the users logged on the machine.

last Show listing of users last logged-in on the system. Really good idea to check it from time to time as a security measure on our system.

history | more Show the last (1000 or so) commands executed from the command line on the current account. The | more causes the display to stop after each screen. To see what another user was doing on the system, login as root and inspect his/her history. The history is kept in the file .bash_history in the user home directory.

uname -a (= Unix name with option all) Info on our (local) server. We can also use guname (in Xwindow terminal) to display the info more nicely.

free Memory info (in kilobytes). Shared memory is the memory that can be shared between processes (e.g., executable code is shared). Buffered and cashed memory is the part that keeps parts of recently accessed filesit can be shrunk if more memory is needed by processes.

set Show the current user environment (in full).

echo $PATH Show the content of the environment variable PATH. This command can be used to show other environment variables as well. Use set to see the full environment.

PDF created with pdfFactory Pro trial version www.pdffactory.com

34
ls or dir

Chapter 2 - Linux Commands

List the contents of the current directory. The command dir is an alias to ls so these two commands do exactly the same thing. The file listing is normally color-coded: dark blue= directories, light grey = regular files, green = executable files, magenta = graphics files, red = compressed (zipped) files, light blue = symbolic links, yellow = device files, brown = FIFO (First-In First-Out named pipes). ls -al |more List the content of the current directory, all files (also those starting with a dot), and in a long form. Pipe the output through the more command, so that the display pauses after each screenful. The ls command has several very useful options. Some of these may have shortcuts (aliases) to avoid clumsy typing. Try ll (=long ls, an alias to ls -l). Another option We use quite often is ls-ad (list all the subdirectories in my current directory, but dont list their contents). cd directory Change directory. Using cd without the directory name will take us to our home directory. cd - will take us to our previous directory and is a convenient way to toggle between two directories. cd .. will take me one directory up (very useful). shutdown -h now (as root) Shut down the system to a halt. Mostly used for a remote shutdown. Use <Ctrl><Alt><Del> for a shutdown at the console (which can be done by any user). halt or reboot or init 6 (as root, three commands) Halt or reboot the machine. Used for remote shutdown, simpler to type than the previous command. Also great if the computer hangs (if we lose control over the keyboard)We can telnet to it from another machine on the network and remotely reboot it. We use <Ctrl><Alt><Del> for normal shutdown at the console of a local computer.

2.4 FILE MANAGEMENT COMMANDS


rm files Remove (delete) files. We must own the file in order to be able to remove it (or be root). On many systems, We will be asked for a confirmation of deletion; if We dont want this, use the - f (=force) option, e.g., rm -f * will remove all files in my current working directory, no questions asked. mkdir directory Make a new directory.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

35

rmdir directory Remove an empty directory. rm -r files (recursive remove) Remove files, directories, and their subdirectories. Careful with this command as rootWe can easily remove all files on the system with such a command executed on the top of our directory tree, and there is no undelete in Linux (yet). But if We really wanted to do it (reconsider), here is how (as root): rm -rf /*

rm -rf files (recursive force remove). As above, but skip the prompt for confirmation, if one is set on our system. Careful with this command particularly as rootsee the command above.

cat filename | more View the content of a text file called filename, one page a time. The | is the pipe symbol (on many American keyboards it shares the key with \). more makes the output stop after each screenful. For long files, it is sometimes convenient to use the commands head and tail that display just the beginning and the end of the file, or less that enables scrolling up and down. If we happened to use cat a binary file and our terminal displays funny characters afterwards, we can restore it with the command reset.

cat filename | less, less filename (two commands, use either) Scroll a content of a text file. Press q when done. less is roughly an equivalent to more , the command we know from DOS, but often less is more convenient than more because it lets me scroll both up and down.

head filename Print first 10 lines of the (long) text file. tail filename Print last 10 lines of a long or growing text file. Use tail -f filename for tail to follow the file as it growsreally handy for continuing inspection of log files.

ispell filename Spell check an ASCII text file. AbiWord, WordPerfect, StarOffice and other word processors come with as-we-type spellchecking, so we really dont have to worry about the simple ispell unless we need it.

touch filename Change the date/time stamp of the file filename to the current time. Create an empty file if the file does not exist. You can change the stamp to any date using touch -t 200201311759.30 (year 2002 January day 31 time 17:59:30).

PDF created with pdfFactory Pro trial version www.pdffactory.com

36

Chapter 2 - Linux Commands

There are three date/time values associated with every file on an ext2 filesystem: the time of last access to the file (atime) the time of last modification to the file (mtime) the time of last change to the files inode (ctime). Touch will change the first two to the value specified, and the last one always to the current system time. They can all be read using the stat command. stat filename Print general info about a file (the contents of the so-called inode). strings filename | more Display the strings contained in the binary file called filename. strings could, for example, be a useful first step to a close examination of an unknown executable. od (=octal dump). Display contents as octal numbers. This can be useful when the output contains non-printable characters. For example, a filename may contain non-printable characters and be a real pain. This can also be handy to view binary files. Examples: dir | od -c | more sort -f filename Arrange the lines in filename according to the ascii order. The option -f tells sort to ignore the upper and lower character case. uniq (=unique) Eliminate duplicate lines in sorted input. Example: sort myfile | uniq fold -w 30 -s my_file.txt > new_file.txt Wrap the lines in the text file my_file.txt so that there is 30 characters per line. Break the lines on spaces. Output goes to new_file.txt. rev filename > filename1 Print the file filename, each line in reversed order. In the example above, the output is directed to the file filename1. paste file1 file2 > file3 Merge two or more text files on lines using <Tab> as delimiter (use option d= to specify our own delimiter(s).

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

37

join file1 file2 > file3 Join lines of two files on a common field. join parallels the database operation join tables, but works on text tables. The default is to join on the first field of the first table, and the default delimiter is white space.

2.5 FINDING FILES


find / -name filename Find the file called filename on our filesystem starting the search from the root directory / . The filename may contain wildcards (*,?). locate filename Find the file name which contains the string filename. Easier and faster than the previous command but depends on a database that normally rebuilds at night, so we cannot find a file that was just saved to the filesystem. To force the immediate update of the database, We may do (as root): updatedb&. which executable_name Show me the full path to the executable that would run if we just typed its name on the command line. For example, this command: whereis command Print the locations for the binary, source, and manual page files of the command command.

2.6 LINUX FILES AND FILE PERMISSION


Linux files are setup so access to them is controlled. There are three types of access: 1. read 2. write 3. execute Each file belongs to a specific user and group. Access to the files is controlled by user, group, and what is called other. The term, other, is used to refer to someone who is not the user (owner) of the file, nor is the person a member of the group the file belongs to. When talking about setting permissions for other users to use, it is commonly referred to as setting the world execute, read, or write bit since anyone in the world will be able to perform the operation if the permission is set in the other category.

PDF created with pdfFactory Pro trial version www.pdffactory.com

38

Chapter 2 - Linux Commands

2.7 FILE NAMES AND PERMISSION CHARACTERS


File names can be up to 256 characters long with -, _, and . characters along with letters and numbers. When a long file listing is done , there are 10 characters that are shown on the left that indicate type and permissions of the file. File permissions are shown according to the following syntax example: drwxrwxrwx

There are a total of 10 characters in this example for all Linux / Unix files. The first character indicates the type of file, and the next three indicate read, write, and execute permission for each of the three user types, user, group and other. Since there are three types of permission for three users, there are a total of nine permission bits. The table below shows the syntax: v Character 1 is the type of file: - is ordinary, d is directory, l is link. v Characters 2-4 show owner permissions. Character 2 indicates read permission, character 3 indicates write permission, and character 4 indicates execute permission. v Characters 5-7 show group permissions. Character 5=read, 6=write, 7=execute v Characters 8-10 show permissions for all other users. Character 8=read, 9=write, 10=execute

There are 5 possible characters in the permission fields. They are: r = read - This is only found in the read field. w = write - This is only found in the write field. x = execute - This is only found in the execute field. s = setuid - This is only found in the execute field. If there is a - in a particular location, there is no permission. This may be found in any field of read, write, or execute.

2.8 EXAMPLES ON FILES


Type ls -l and a listing like the following is displayed:

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

39

total 10 drwxrwxrwx -rw-rw-rw-rw-rw-rw

4 1 1

george team1 122 george team1 1873 george team1 1234

Dec 12 18:02 Aug 23 08:34 Sep 12 11:13

Projects test datafile

Which means the following:

The fields are as follows: 1. Type field: The first character in the field indicates a file type of one of the following: d = directory l = symbolic link s = socket p = named pipe - = regular file c= character (un buffered) device file special b=block (buffered) device file special

2. 3. 4. 5. 6. 7. 8.

Permissions are explained above. Links: The number of directory entries that refer to the file. In our example, there are four. The files owner in our example is George. The group the file belongs to. In our example, the group is team1. The size of the file in bytes The last modification date. If the file is recent, the date and time is shown. If the file is not in the current year, the year is shown rather than time. The name of the file.

PDF created with pdfFactory Pro trial version www.pdffactory.com

40 2.9 SETTING KEYSTROKES

Chapter 2 - Linux Commands

The stty command - Used to set up keystrokes on terminals [try $ stty a]. The command <stty erase ^H> will bind the Backspace key(^H) to the erase function. Characters that are specified: v eof - End of file v eol - End of line v eol2 - Alternate character for ending a line v erase - Erase the last character typed v intr - The specified character will send an interrupt signal v kill - Erase the current line. v quit - Sends a quit signal. v start - Restart output after having stopped it. v stop - Stops the output. v susp - Sends a terminal stop signal v switch - Switch to a different shell layer v werase - erase the last word typed. setterm foreground white background blue setterm store Sets white foreground, and blue background Stores the present terminal values as default

2.10 KEYS AND TERMINAL CONFIGURATION


Special command line keys: v CTRL U - Delete the line v CTRL C - Abort command v CTRL Z - Suspend command, Type fg to put it back into the foreground v <TAB> - Auto complete command v <ALT>F1 through <ALT>F6 - Select a different virtual terminal

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

41

v <CRTL><ALT>F7 - Toggle to the first X terminal, if one is running. v <CRTL><ALT>F1 - Toggle to the first text terminal from an X session. v CTRL D - End of file, exits the shell v CTRL W - Delete a word backwards v CTRL / - Quit, weaker than CTRL C but does a core dump. v CTRL S - Stop scrolling, may use scroll lock for this function. v CTRL Q - Resume scrolling

2.11 SUMMARY
A UNIX command is a series of characters that we type. These characters consist of words that are separated by whitespace. Whitespace is the result of typing one or more Space or Tab keys. The first word is the name of the command. The rest of the words are called the commands arguments. The arguments give the command information that it might need, or specify varying behavior of the command. You can use command options to fine-tune the actions of a Linux command. Quite often, a Linux command will do almost, but not quite what you want it to do. Instead of making you learn a second command, Linux lets you modify the basic, or default, actions of the command by using options. Most Linux commands are very flexible. When you enter a Linux command, there are several ways to tailor the basic command to your specific needs. We will look at the two main ways used to modify the effect of a command: v Specifying or redirecting a commands input and output v Using command options Standard LINUX commands makes shell scripts more versatile. The power of scripts comes from coupling system commands and shell directives with simple programming constructs.

2.12 CHECK YOUR PROGRESS


I. Exercises Manipulating files
In this exercise, well create, rename and delete files. Find out if the system has one or more printers as well as the appropriate command to use for printing.

PDF created with pdfFactory Pro trial version www.pdffactory.com

42
Go to home directory. Copy distant file to working directory. Create new directory. List working directory. Move file to new directory. Change working directory. Copy file to working directory. Print the file. List filenames with wildcard. Remove files. Go up to parent directory. Remove directory. Verify that directory was removed. Enter cd

Chapter 2 - Linux Commands

Enter cp /etc/passwd myfile Enter mkdir temp Enter ls F Enter mv myfile temp Enter cd temp Enter cp myfile myfile.two Enter the printer command and the filename Enter ls -l myfile* Enter rm myfile* Enter cd .. Enter rmdir temp Enter ls F

II. Choose the correct answer: 1) tac _______________________.


a) concatenates 2 files b) listing a file backwards from its end c) reverses each line of file d) none of the above 2) rmdir remove directory iff _____________. a) Directory must be empty of all files b) Directory maynt be empty c) Both a) & b) d) None of the above

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

43

3)

touch command is used for ____________. a) Updating access/modification times of a file to current system time b) Creating a new file c) Both a) & b) d) None of the above

4)

Diff reports ____________________ a) Differences between two files b) At what point the files differ c) Removes duplicate lines d) None of the above.

5)

egrep an acronym for __________________________. a) evaluation global regular expression print b) extensible global regular expression print c) extended global regular expression print d) none of the above.

6)

traceroute works within ______ a) LAN b) WAN c) Over the internet d) All of the above

7)

If finger is used without an argument ___________________________ a) it gives information concerning users currently logged in b) it gives information about a particular user on a network c) it gives information about all users d) none of the above

PDF created with pdfFactory Pro trial version www.pdffactory.com

44
8) Protocol used for connecting to a remote host _______. a) ftp b) telnet c) tcp/ip d) none of the above 9) df command is used for__________ purpose. a) Communication b) System & administrative c) Networking d) File & archiving 10) Command used for shutting down the system ______ a) halt b) exit c) quit d) logout 11) The following are Network related Commands _______ a) Wall b) ftp c) host d) none of the above

Chapter 2 - Linux Commands

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

45

III. Say True or False 1. mv is equivalent to a combination of cp & rm


2. 3. 4. 5. 6. 7. mkdir p creates a new directory with necessary parent directories. expr concatenates and evaluates the arguments according to the given operation join pastes together only those lines with a common numerical label.

True/False True/False True/False True/False

tr a-z \* < filename changes all the lowercase letters in filename to asterisks True/False exec shell builtin replaces the present process with a specified command. kill command resumes a process. True/False True/False

IV. Essay Type Questions 1. Write a short notes on the following with relevant options.
a. cat & tac b. chmod c. touch d. sort e. grep 2. 3. 4. 5. Explain any three help, file and text Processing Commands. Write in detial about tar command with the relvant options. Give the differentiation between locate, find & slocate. Give a short note on the following communication commands. a. cat b. rm 6. List the differences between the following. a. mv & cp b. tac & rev c. expr & let d. cmp & diff

PDF created with pdfFactory Pro trial version www.pdffactory.com

46

Chapter 2 - Linux Commands

V. Further readings and other activities 1. Get more information using man or info or any help command for find, date, uniq, stty, diff, cmp, ls
EX: 1. $ man du 2. $ apropos bash

Check the amount of space used, amount of space free for different drives and different directories. 2. For further readings you can refer the following books Title: Linux internals Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.pandurangarao.i8.com/doc/linux/linuxbook.html http://www.raomvp.bravepages.com/doc/linux/linuxbook.html A complete reference for you to get more information. You can also extract solutions for exercises. Title: principles of unix programming Auihors: Sumitabha Das Work and expertise all the commands given in the chapter with different options and by the virtue of which know, which command is to be used for different situations of jobs.

VI. Solutions II. Choose the correct answer: 1.b 2.a 3.c 4.a III. Say True or False 1.True 2.True
5.c 6.d 7.b 8.b 9.b 10.a 11.d

3.True

4.True

5.True

6.True

7.False

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 3

Processes

3.0 INTRODUCTION AND OBJECTIVES


running instance of a program is called a PROCESS. If we have two terminal windows showing on our screen, then we are probably running the same terminal program twicewe have two terminal processes. Each terminal window is probably running a shell; each running shell is another process. When we invoke a command from a shell, the corresponding program is executed in a new process; the shell process resumes when that process completes. Advanced programmers often use multiple cooperating processes in a single application to enable the application to do more than one thing at once, to increase application robustness, and to make use of already-existing programs. Most of the process manipulation functions described in this chapter are similar to those on other UNIX systems. Most are declared in the header file <unistd.h>; check the man page for each function to be sure.

Objectives
After completing this module you should be able to understand and utilize these features of the Linux processes: Looking at processes Process IDs Analysis of active processes Different output formats of ps

MSIT 3A Linux Internals

47

PDF created with pdfFactory Pro trial version www.pdffactory.com

48
Killing a process Processes creation mechanisms Using system, system calls - fork, exec Calling fork, using the exec Family Using fork and exec Together Process scheduling Signals and Linux daemons Most useful daemons Zombie process and threads Differentiating processes and threads

Chapter 3 - Processes

3.1 LOOKING AT PROCESSES


Even as we sit down at our computer, there are processes running. Every executing program uses one or more processes. Lets start by taking a look at the processes already running on the computer.

3.2 PROCESS IDS


Each process in a Linux system is identified by its unique process ID, sometimes referred to as pid. Process IDs are 16-bit numbers that are assigned sequentially by Linux as new processes are created. Every process also has a parent process. Thus, we can think of the processes on a Linux system as arranged in a tree, with the init process at its root. The parent process ID, or ppid, is simply the process ID of the processs parent. When referring to process IDs in a C or C++ program, always use the pid_t typedef, which is defined in <sys/types.h>.A program can obtain the process ID of the process its running in with the getpid() system call, and it can obtain the process ID of its parent process with the getppid() system call. For instance, the program in Listing below prints its process ID and its parents process ID. Listing ( print-pid.c) Printing the Process ID #include <stdio.h> #include <unistd.h> int main ()

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

49

{ printf (The process ID is %d\n, (int) getpid ()); printf (The parent process ID is %d\n, (int) getppid ()); return 0; } Observe that if we invoke this program several times, a different process ID is reported because each invocation is in a new process. However, if we invoke it every time from the same shell, the parent process ID (that is, the process ID of the shell process) is the same.

3.3 VIEWING ACTIVE PROCESSES


The ps command displays the processes that are running on our system. The Linux version of ps has lots of options because it tries to be compatible with versions of ps on several other UNIX variants. These options control which processes are listed and what information about each is shown. By default, invoking ps displays the processes controlled by the terminal or terminal window in which ps is invoked. For example: $ ps PID TTY TIME CMD 21693 pts/8 00:00:00 bash 21694 pts/8 00:00:00 ps This invocation of ps shows two processes. The first, bash, is the shell running on this terminal. The second is the running instance of the ps program itself. The first column, labeled PID, displays the process ID of each. For a more detailed look at whats running on our Linux system, invoke this: $ ps -e -o pid,ppid,command The -e option instructs ps to display all processes running on the system. The -o pid,ppid,command option tells ps what information to show about each process in this case, the process ID, the parent process ID, and the command running in this process.

3.4 PS OUTPUT FORMATS


With the -o option to the ps command, we specify the information about processes that we

PDF created with pdfFactory Pro trial version www.pdffactory.com

50

Chapter 3 - Processes want in the output as a comma-separated list. For example, ps -o pid,user,start_time,command displays the process ID, the name of the user owning the process, the wall clock time at which the process started, and the command running in the process. See the man page for ps for the full list of field codes. We can use the -f (full listing), -l (long listing), or -j (jobs listing) options instead to get three different preset listing formats. Here are the first few line and last few lines of output from this command on the system. We may see different output, depending on whats running on our system. $ ps -e -o pid,ppid,command PID 1 2 3 ... 21725 21727 21728 PPID 0 1 1 21693 21725 21727 COMMAND init [5] [kflushd] [kupdate] xterm bash ps -e -o pid,ppid,command

Note that the parent process ID of the ps command, 21727, is the process ID of bash, the shell from which we invoked ps. The parent process ID of bash is in turn 21725, the process ID of the xterm program in which the shell is running. Using ps provides us a snapshot of the systems active processes. The following are the fields from the output of the ps command that are important in terms of performance tuning: Description Flags that indicate the processs current state and are calculated by adding each of the hexadecimal values: Process has terminated System process, always in memory Process is being traced by its parent Process is being traced by parent, and is stopped Process cannot be awakened by a signal Process is in memory and locked, pending an event Process cannot be swapped The current state of the process, as indicated by one of the following letters:

Field F 00 01 02 04 08 10 20 S

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

51

O S R I Z T X

Process is currently running on the processor Process is sleeping, waiting for an I/O event (including terminal I/O) to complete Process is ready to run Process is idle Process is a zombie process (it has terminated, and the parent is not waiting but is still in the process table) Process is stopped because of parent tracing it Process is waiting for more memory

3.5 KILLING A PROCESS


While logged in as root, type ps -ax | more or ps -aux | more. We will get a list of all processes running on our computer. We will see the process id (PID), process status (STAT) various statistics, and the command name. We can kill a process by typing kill and the PID number right afterwards similar to the line below. $ kill 1721 We can also stop and restart processes by sending them various signals as in the below examples: Stops (suspends) process 1721 by sending the STOP signal to the process. This process will still be on the task list. The process cant catch or ignore the STOP signal. Continue process 1721 causing it to resume. The CONT signal is sent to the process. Terminates process 1721 by sending the TERM signal to the process. This process will no longer show up on the task list if it is actually terminated. Process terminated cannot be continued. The TERM signal can be caught so TERM is not guaranteed to kill the process. Stops, then restarts process 1721. This is usually done when a process is not working properly or the configuration files for that process have been changed. This command sends the HUP signal to the process which means hangup. This signal can be caught by the process.

kill STOP 1721

kill CONT 1721 kill TERM 1721

kill HUP 1721

killall HUP myprint Restarts any process with the name myprint. kill TERM myprint Terminates any process with the name myprint.

PDF created with pdfFactory Pro trial version www.pdffactory.com

52 3.6 CREATING PROCESSES

Chapter 3 - Processes

Two common techniques are used for creating a new process. The first is relatively simple but should be used sparingly because it is inefficient and has considerably security risks. The second technique is more complex but provides greater flexibility, speed, and security.

3.7 CREATING USING SYSTEM()


The system function in the standard C library provides an easy way to execute a command from within a program, much as if the command had been typed into a shell. In fact, system creates a sub process running the standard Bourne shell (/bin/sh) and hands the command to that shell forexecution. For example, this program in Listing invokes the ls command to display the contents of the root directory, as if we typed ls -l / into a shell. Listing (system.c) Using the system Call #include <stdlib.h> int main () { int return_value; return_value = system (ls -l /); return return_value; } The system function returns the exit status of the shell command. If the shell itself cannot be run, system returns 127; if another error occurs, system returns 1. Because the system function uses a shell to invoke our command, its subject to the features, limitations, and security flaws of the systems shell. We cant rely on the availability of any particular version of the Bourne shell. On many UNIX systems, /bin/sh is a symbolic link to another shell. For instance, on most Linux systems, /bin/sh points to bash (the Bourne-Again Shell), and different Linux distributions use different versions of bash. Invoking a program with root privilege with the system function, for instance, can have different results on different Linux systems. Therefore, its preferable to use the fork and exec method for creating processes.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

53

3.8 CREATING USING SYSTEM CALLS FORK, EXEC


The DOS and Windows API contain the spawn family of functions. These functions take as an argument the name of a program to run and create a new process instance of that program. Linux doesnt contain a single function that does all this in one step. Instead, Linux provides one function, fork, which makes a child process that is an exact copy of its parent process. Linux provides another set of functions, the exec family, which causes a particular process to cease being an instance of one program and to instead become an instance of another program. To spawn a new process, we first use fork to make a copy of the current process. Then we use exec to transform one of these processes into an instance of the program we want to spawn.

3.9 CREATING BY CALLING FORK


When a program calls fork, a duplicate process, called the child process, is created. The parent process continues executing the program from the point that fork was called. The child process, too, executes the same program from the same place. So how do the two processes differ? First, the child process is a new process and therefore has a new process ID, distinct from its parents process ID. One way for a program to distinguish whether its in the parent process or the child process is to call getpid. However, the fork function provides different return values to the parent and child processesone process goes in to the fork call, and two processes come out, with different return values. The return value in the parent process is the process ID of the child. The return value in the child process is zero. Because no process ever has a process ID of zero, this makes it easy for the program whether it is now running as the parent or the child process. Listing below is an example of using fork to duplicate a programs process. Note that the first block of the if statement is executed only in the parent process, while the else clause is executed in the child process. Listing ( fork.c) Using fork to Duplicate a Programs Process #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main ()

PDF created with pdfFactory Pro trial version www.pdffactory.com

54
{ pid_t child_pid; printf (the main program process ID is %d\n, (int) getpid ()); child_pid = fork (); if (child_pid != 0) { printf (this is the parent process, with id %d\n, (int) getpid ()); printf (the childs process ID is %d\n, (int) child_pid); } else printf (this is the child process, with id %d\n, (int) getpid ()); return 0; }

Chapter 3 - Processes

3.10 CREATING BY USING THE EXEC FAMILY


The exec functions replace the program running in a process with another program. When a program calls an exec function, that process immediately ceases executing that program and begins executing a new program from the beginning, assuming that the exec call doesnt encounter an error. Within the exec family, there are functions that vary slightly in their capabilities and how they are called. Functions that contain the letter p in their names (execvp and execlp) accept a program name and search for a program by that name in the current execution path; functions that dont contain the p must be given the full path of the program to be executed. Functions that contain the letter v in their names (execv, execvp, and execve) accept the argument list for the new program as a NULL-terminated array of pointers to strings. Functions that contain the letter l (execl, execlp, and execle) accept the argument list using the C language varargs mechanism. Functions that contain the letter e in their names (execve and execle) accept an additional argument, an array of environment variables. The argument should be a NULL-terminated array of pointers to character strings. Each character string should be of the form VARIABLE=value.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

55

Because exec replaces the calling program with another one, it never returns unless an error occurs.

3.11 CREATING BY USING FORK AND EXEC TOGETHER


A common pattern to run a subprogram within a program is first to fork the process and then exec the subprogram. This allows the calling program to continue execution in the parent process while the calling program is replaced by the subprogram in the child process.

3.12 PROCESS SCHEDULING


Linux schedules the parent and child processes independently; theres no guarantee of which one will run first, or how long it will run before Linux interrupts it and lets the other process run. In particular, none, part, or all of the ls command may run in the child process before the parent completes. Linux promises that each process will run eventually no process will be completely starved of execution resources. We may specify that a process is less important and should be given a lower priority by assigning it a higher niceness value. By default, every process has a niceness of zero. (use nice command) A higher niceness value means that the process is given a lesser execution priority; conversely, a process with a lower (that is, negative) niceness gets more execution time. To run a program with a nonzero niceness, use the nice command, specifying the niceness value with the -n option. For example, this is how we might invoke the command sort input.txt > output.txt, a long sorting operation, with a reduced priority so that it doesnt slow down the system too much: Example: $ nice -n 10 sort input.txt > output.txt We can use the renice command to change the niceness of a running process from the command line. To change the niceness of a running process programmatically, use the nice function. ITS ARGUMENT IS AN INCREMENT VALUE, WHICH IS ADDED TO THE NICENESS VALUE OF THE PROCESS THAT CALLS IT. REMEMBER THAT A POSITIVE VALUE RAISES THE NICENESS VALUE AND THUS REDUCES THE PROCESSS EXECUTION PRIORITY.

PDF created with pdfFactory Pro trial version www.pdffactory.com

56

Chapter 3 - Processes

Note that only a process with root privilege can run a process with a negative niceness value or reduce the niceness value of a running process. This means that we may specify negative values to the nice and renice commands only when logged in as root, and only a process running as root can pass a negative value to the nice function. This prevents ordinary users from grabbing execution priority away from others using the system.

3.13 SIGNALS
Signals are mechanisms for communicating with and manipulating processes in Linux. The topic of signals is a large one; here we discuss some of the most important signals and techniques that are used for controlling processes. A signal is a special message sent to a process. Signals are asynchronous; when a process receives a signal, it processes the signal immediately, without finishing the current function or even the current line of code. There are several dozen different signals, each with a different meaning. Each signal type is specified by its signal number, but in programs, we usually refer to a signal by its name. In Linux, these are defined in /usr/include/bits/signum.h. (We shouldnt include this header file directly in our programs; instead, use <signal.h>.) When a process receives a signal, it may do one of several things, depending on the signals disposition (address space). For each signal, there is a default disposition, which determines what happens to the process if the program does not specify some other behavior. For most signal types, a program may specify some other behavioreither to ignore the signal or to call a special signal-handler function to respond to the signal. If a signal handler is used, the currently executing program is paused, the signal handler is executed from other address space. When the signal handler returns, the program resumes. The Linux system sends signals to processes in response to specific conditions. For instance, SIGBUS (bus error), SIGSEGV (segmentation violation), and SIGFPE (floating point exception) may be sent to a process that attempts to perform an illegal operation. The default disposition [action] for these signals it to terminate the process and produce a core file. A process may also send a signal to another process. One common use of this mechanism is to end another process by sending it a SIGTERM or SIGKILL signal. Another common use is to send a command to a running program. Two userdefined signals are reserved for this purpose: SIGUSR1 and SIGUSR2. The SIGHUP signal is sometimes used for this purpose as well, commonly to wake up an idle program or cause a program to reread its configuration files.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

57

The sigaction function can be used to set a signal disposition. The first parameter is the signal number. The next two parameters are pointers to sigaction structures; the first of these contains the desired disposition for that signal number, while the second receives the previous disposition. The most important field in the first or second sigaction structure is sa_handler. It can take one of three values: SIG_DFL, which specifies the default disposition for the signal. SIG_IGN, which specifies that the signal should be ignored. A pointer to a signal-handler function. The function should take one parameter, the signal number, and return void. Because signals are asynchronous, the main program may be in a very fragile state when a signal is processed and thus while a signal handler function executes. Therefore, we should avoid performing any I/O operations or calling most library and system functions from signal handlers. A signal handler should perform the minimum work necessary to respond to the signal, and then return control to the main program (or terminate the program). In most cases, this consists simply of recording the fact that a signal occurred. The main program then checks periodically whether a signal has occurred and reacts accordingly. It is possible for a signal handler to be interrupted by the delivery of another signal. While this may sound like a rare occurrence, if it does occur, it will be very difficult to diagnose and debug the problem. Therefore, we should be very careful about what our program does in a signal handler. Even assigning a value to a global variable can be dangerous because the assignment may actually be carried out in two or more machine instructions, and a second signal may occur between them, leaving the variable in a corrupted state. If we use a global variable to flag a signal from a signal-handler function, it should be of the special type sig_atomic_t. Linux guarantees that assignments to variables of this type are performed in a single instruction and therefore cannot be interrupted midway. In Linux, sig_atomic_t is an ordinary int; in fact, assignments to integer types the size of int or smaller, or to pointers, are atomic. If we want to write a program thats portable to any standard UNIX system, though, use sig_atomic_t for these global variables.

3.14 SOME LINUX DAEMONS


Daemons are resident programs that periodically wake up, check our system and may perform certain functions. They do not take any input and dont normally produce any output. Some daemons normally start when system boots up. They are long running processes, having high [long] life. They do good jobs related to system and users on the system. Rarely they harm also.

PDF created with pdfFactory Pro trial version www.pdffactory.com

58

Chapter 3 - Processes

Our Linux system is likely set to run quite a number of daemons. Most of them can be (dis)selected by running the program ntsysv (RedHat) as root and checking the appropriate box. The short description of each daemon is available under netsysv by pressing <F1>. If the daemon we need is not listed in ntsysv, we need to insert our RedHat installation CD and install the appropriate package. The alternative to ntsysv may be tksysv (type as root, in X terminal), Which is perhaps more flexible, but way more complicated (it lets us set up the list of daemons to run in each runlevel). Another, simpler and even more powerful flexible difficult to use tool is /sbin/ chkconfig.

3.15 POPULAR DAEMONS


Here is a short list of popular daemons with a brief description: v anacron - checks cron jobs that were left out due to down time and executes them. Useful if we have cron jobs scheduled but dont run our machine all the timeanacron will detect that during bootup. v arpwatch - keeps watch for ethernet/ip address pairings. v autofs - control the operation of automount daemons (competition to amd). v bootparamd - server process that provides information to diskless clients necessary for booting. v crond - automatic task scheduler. Manages the execution of tasks that are executed at regular but infrequent intervals, such as rotating log files, cleaning up /tmp directories, etc. v cupsd - the Common UNIX Printing System (CUPS) daemon. CUPS is an advanced printer spooling system which allows setting of printer options and automatic availability of a printer configured on one server in the whole network. The default printing system of Linux Mandrake. v dhcpd - implements the Dynamic Host Configuration Protocol (DHCP) and the Internet Bootstrap Protocol (BOOTP). v gpm - useful mouse server for applications running on the Linux text console. v httpd - daemon for the Apache webserver. v inetd - listens for service requests on network connections, particularly dial-in services.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

59

This daemon can automatically load and unload other daemons (ftpd, telnetd, etc.), thereby economizing on system resources. Newer systems use xinetd instead. v kudzu - detects and configures new or changed hardware during boot. v linuxconf - the linuxconf configuration tool. The automated part is run if we want linuxconf to perform various tasks at boottime to maintain the system configuration. v lpd - printing daemon. v named - the Internet Domain Name Server (DNS) daemon. v netfsd - network filesystem mounter. Used for mounting nfs, smb and ncp shares on boot. v networkd -activates all network interfaces at boot time by calling scripts in /etc/sysconfig network-scripts. v nfsd - used for exporting nfs shares when requested by remote systems. v portmap - needed for Remote Procedure Calls. Most likely, we need it for running network. v routed - daemon that manages routing tables. v rstatd - kernel statistics server. v rusersd, rwalld - identification of users and wall messaging services for remote users. v rwhod - server which maintains the database used by the rwho(1) and ruptime(1) programs. Its operation depends on the ability to broadcast messages on a network. v sendmaild - mail transfer agent. This is the agent that comes with Red Hat. v smbd - the SAMBA (or smb) daemon, a network connectivity services to MS Windows computers on our network (hard drive sharing, printers, etc). v squid - An http proxy with caching. Proxies relay requests from clients to the outside world, and return the results. We would use this particular proxy if we wanted to use our linux computer as a gateway to the Internet for other computer on our network. Another (and probably safer at home) way to do it, is to set up masquarading. v syslogd - manages system activity logging. The configuration file is /etc/syslog.conf. v smtpd - Simple Mail Transfer Protocol, designed for the exchange of electronic mail messages. Several daemons that support SMTP are available, including sendmail, smtpd, rsmtpd, qmail, zmail, etc. v ypbind - NIS binder. Needed if computer is part of Network Information Service domain.

PDF created with pdfFactory Pro trial version www.pdffactory.com

60 3.16 ZOMBIE PROCESS


v v

Chapter 3 - Processes

If a child process terminates while its parent is calling a wait function, the child process vanishes and its termination status is passed to its parent via the wait call. But what happens when a child process terminates and the parent is not calling wait? Does it simply vanish? No, because then information about its terminationsuch as whether it exited normally and, if so, what its exit status iswould be lost. Instead, when a child process terminates, is becomes a zombie process. A zombie process is a process that has terminated but has not been cleaned up yet. It is the responsibility of the parent process to clean up its zombie children. The wait functions do this, too, so its not necessary to track whether our child process is still executing before waiting for it. Suppose, for instance, that a program forks a child process, performs some other computations, and then calls wait. If the child process has not terminated at that point, the parent process will block in the wait call until the child process finishes. If the child process finishes before the parent process calls wait, the child process becomes a zombie. When the parent process calls wait, the zombie childs termination status is extracted, the child process is deleted, and the wait call returns immediately. Orphan process is a parent less child. What happens if the parent does not clean up its children? They stay around in the system, as zombie processes. The program in Listing below forks a child process, which terminates immediately and then goes to sleep for a minute, without ever cleaning up the child process. Listing (zombie.c) Making a Zombie Process #include <stdlib.h> #include <sys/types.h> #include <unistd.h> int main () { pid_t child_pid; /* Create a child process. */ child_pid = fork (); if (child_pid > 0) { /* This is the parent process. Sleep for a minute. */ sleep (60);

v v

v v

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

61

} else { /* This is the child process. Exit immediately. */ exit (0); } return 0; } Try compiling this file to an executable named make-zombie. Run it, and while its still running, list the processes on the system by invoking the following command in another window: $ ps -e -o pid,ppid,stat,cmd This lists the process ID, parent process ID, process status, and process command line. Observe that, in addition to the parent make-zombie process, there is another make-zombie process listed. Its the child process; note that its parent process ID is the process ID of the main make-zombie process. The child process is marked as <defunct>, and its status code is Z, for zombie. What happens when the main make-zombie program ends when the parent process exits, without ever calling wait? Does the zombie process stay around? Notry running ps again, and note that both of the make-zombie processes are gone. When a program exits, its children are inherited by a special process, the init program, which always runs with process ID of 1 (its the first process started when Linux boots).The init process automatically cleans up any zombie child processes that it inherits.

3.17 THREADS
Threads, like processes, are a mechanism to allow a program to do more than one thing at a time. As with processes, threads appear to run concurrently; the Linux kernel schedules them asynchronously, interrupting each thread from time to time to give others a chance to execute. Conceptually, a thread exists within a process. Threads are a finer-grained unit of execution than processes. When we invoke a program, Linux creates a new process and in that process creates a single thread, which runs the program sequentially. That thread can create additional threads; all these threads run the same program in the same process, but each thread may be executing a different part of the program at any given time.

PDF created with pdfFactory Pro trial version www.pdffactory.com

62

Chapter 3 - Processes

Weve seen how a program can fork a child process. The child process is initially running its parents program, with its parents virtual memory, file descriptors, and so on copied. The child process can modify its memory, close file descriptors, and the like without affecting its parent, and vice versa. When a program creates another thread, though, nothing is copied. The creating and the created thread share the same memory space, file descriptors, and other system resources as the original. If one thread changes the value of a variable, for instance, the other thread subsequently will see the modified value. Similarly, if one thread closes a file descriptor, other threads may not read from or write to that file descriptor. Because a process and all its threads can be executing only one program at a time, if any thread inside a process calls one of the exec functions, all the other threads are ended (the new program may, of course, create new threads).

3.18 PROCESSES VS. THREADS


For some programs that benefit from concurrency, the decision whether to use processes or threads can be difficult. Here are some guidelines to help us decide which concurrency model best suits our program: 1. All threads in a program must run the same executable. A child process, on the other hand, may run a different executable by calling an exec function. 2. An errant thread can harm other threads in the same process because threads share the same virtual memory space and other resources. For instance, a wild memory write through an un initialized pointer in one thread can corrupt memory visible to another thread. An errant process, on the other hand, cannot do so because each process has a copy of the programs memory space. 3. Copying memory for a new process adds an additional performance overhead relative to creating a new thread. However, the copy is performed only when the memory is changed, so the penalty is minimal if the child process only reads memory. 4. Threads should be used for programs that need fine-grained parallelism. For example, if a problem can be broken into multiple, nearly identical tasks, threads may be a good choice. Processes should be used for programs that need coarser parallelism. 5. Sharing data among threads is trivial because threads share the same memory.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

63

3.19 SUMMARY
Process defined as: A process is program (command given by user) to perform specific Job. In Linux when you start process, it gives a number to process (called PID or process-id), PID starts from 0 to 65535. Process & PID defined as: An instance of running command is called process and the number printed by shell is called process-id (PID), this PID can be use to refer specific running process. TOOLS FOR WORKING WITH PROCESSES v accton - Turns process accounting on and off. Uses the file /var/log/pacct. To turn it on type accton /var/log/pacct. Use the command with no arguments to turn it off. v kill - Kill a process by number v killall - Send a signal to a process by name v lastcomm (1) - Display information about previous commands in reverse order. Works only if process accounting is on. v nice - Set process priority of new processes. v ps(1) - Used to report the status of one or more processes. v pstree(1) - Display the tree of running processes. v renice(8) - Can be used to change the process priority of a currently running process. v sa(8) - Generates a summary of information about users processes that are stored in the / var/log/pacct file. v skill - Report process status. v snice - Report process status. v top - Displays the processes that are using the most CPU resources.

PDF created with pdfFactory Pro trial version www.pdffactory.com

64 3.20 CHECK YOUR PROGRESS

Chapter 3 - Processes

I. Exercises v You are working on your Linux workstation (like sending mails, typing letter), while doing this work you have started to play MP3 files on your workstation. Regarding this situation, answer the following question:
1) Is it example of Multitasking? 2) How you will find out the both running process (MP3 Playing & Letter typing)? 3) Currently only two Process are running in your Linux / PC environment, Is it True or False?, And how you will verify this? 4) You dont want to listen music (MP3 Files) but want to continue with other work on PC, you will take any of the following action: 1. Turn off Speakers 2. Turn off Computer / Shutdown Linux Os 3. Kill the MP3 playing process 4. None of the above

II. Choose the correct answer


1. An example of system call is ______. a. fork b. exec c. wait d. all of the above. 2. process is a ________ entity. a. passive b. dynamic c. static d. none of the above.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

65

3.

To run a command in the background, add the & character ________of the command line. a. At the end b. At the beginning c. In the middle d. Any where

4.

We can kill a process by typing ______ and the PID number right afterwards. a. pid b. kill c. signal d. none of the above

5.

Common techniques used for creating a new process is using ______. a. system(). b. fork() c. fork(), exec() and wait() d. All of the above

6.

________ command is used to set priority for the process. a. more b. less c. nice d. all of the above.

7.

_____ are long running processes, having high [long] life. a. Daemons b. Threads c. Zombie d. Orphan

PDF created with pdfFactory Pro trial version www.pdffactory.com

66
III. Say True or False 1. Each signal type is specified by its signal number.
2. 3. 4. 5. Threads, like processes, are a mechanism to allow a program to do more than one thing at a time

Chapter 3 - Processes

True / False True / False

Process IDs are 16-bit numbers that are assigned sequentially by Linux as new processes are created. True / False A signal is program (command given by user) to perform specific Job. pstree() - Display the tree of running processes. True / False

IV. Essay type questions 1. What is meant by process ? Explain its states briefly.
2. 3. 4. 5. 6. 7. 8. 9. Explain how a process can be created. What is meant by background processing ? illustrate with example. Differentiate a process and thread in detail. What is a daemon? Describe the most popular daemons with their functions. Describe the output formats of ps command with respect to performance analysis. Elucidate the techniques of creating the processes with examples. Write a note on zombie, thread, daemon and Orphan. Explain the different types of Linux processes.

V. Further readings and other activities 1. Get more information using man or info or any help command for init, getty, daemon, telnetd, SIGQUIT, SIGABRT, ps, top, nice.
2. Work on processes by executing small programs.: Following table lists most commonly used command(s) with process:

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

67
Use this Command ps kill {PID} killall {Process-name} ps ag kill 0 Examples* $ ps $ kill 1012 $ killall httpd $ ps ag $ kill 0

For this purpose To see currently running process To stop any process by PID i.e. to kill process To stop processes by name i.e. to kill process To get information about all running process To stop all process except your shell For background processing (With &, use to put particular command and program in background) To display the owner of the processes along with the processes To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command To see currently running processes and other information like memory and CPU usage with real time updates. To display a tree of processes

linux-command &

$ ls / -R | wc -l &

ps aux

$ ps aux For e.g. you want to see server process is runningor not then give command $ ps ax | grep httpd $ top

ps ax | grep processU-want-to see

top

Note that to exit from top command press q. $ pstree

pstree

* To run some of this command you need to be root or equivalent user. 3. Books for Reference Title: Linux internals

PDF created with pdfFactory Pro trial version www.pdffactory.com

68
Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.pandurangarao.i8.com/doc/linux/linuxbook.html http://www. geocities.com/raomvp/doc/linux/linuxbook.html A complete reference for you to get more information. You can also extract solutions for exercises.

Chapter 3 - Processes

Title: Linux in a Nutshell - a good overview of all the most common Linux commands, process including a comprehensive section on the bash shell.

VI. Solutions I. Exercises 1) Is it example of Multitasking? Ans.: Yes, since we are running two process simultaneously.
2) 3) How you will find out the both running process (MP3 Playing & Letter typing)? Ans.: Try $ ps -aux or $ ps -ax | grep process-you-want-to-search Currently only two Process are running in your Linux/PC environment, Is it True or False?, And how you will verify this? Ans.: No its not true, when you start Linux Os, various process start in background for different purpose. To verify this simply use top or ps -aux command. You dont want to listen music (MP3 Files) but want to continue with other work on PC, you will take any of the following action: 1. Turn off Speakers 2. Turn off Computer / Shutdown Linux Os 3. Kill the MP3 playing process 4. None of the above Ans.: Use action no. 3 i.e. kill the MP3 process. Tip: First find the PID of MP3 playing process by issuing command: $ ps -ax | grep mp3-process-name Then in the first column you will get PID of process. Kill this PID to end the process as: $ kill PID Or you can try killall command to kill process by name as follows: $ killall mp3-process-name

4)

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

69

II. Choose appropriate answer 1.d 2.b 3.a 4.b III. Say True or False 1. True 2. True 3. True

5.d

6.c

7.a

4. False

5. True

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 4

Shell Programming

4.0 INTRODUCTION AND OBJECTIVES

shell script is a series of shell commands, similar to the ones given above, contained in a file, which can be given to the bash program to be executed in sequence. Technically, it is a program with bash as the language.

There are several shells available on the Linux system (if we installed them): bash (Bourne Again shell), sh (Bourne shell, standard on many UNIX systems), csh (C shell, with a syntax akin to the c programming language, available on most UNIX systems), pdksh (public domain Korn shell), tcsh (tiny C shell, often used on small systems), sash (stand-alone shell, could be used when libraries are not available), ash, zsh, and perhaps a couple more.

Objectives
At the end of this unit, You would be able to Understand the analogy of shell programming Creation of a shell script Meta characters Positional Parameters and Shell Variables Built-in shell commands and variables Iteration, control and if statements Shell operators

70

Chapter 4 - Shell Programming

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

71

The test Command An example script Environment Variables

4.1 CREATING A SHELL SCRIPT

We can use any text editor to create the shell script - simply choose one, that we are comfortable with. If we are using KDE, We can use the Advanced Editor (kate or kwrite), or kedit. Do not use a word processing program - these are not text editors, and insert additional formatting information into the text. If, however, we know how to use our word processor to save plain-text file, go ahead and use it. Start with a new file firstshellscript.sh, and type the following lines. Example: $ vi firstshellscipt.sh #!/bin/bash echo I am about to list the files present in home directory # here is the actual listing: ls $HOME # HOME = /rao/laptop echo Done!

The first line has a special meaning. It tells the current shell which program should be used to interpret this file. Here, we have given the filename of the bash program. This is so that, if this script is invoked from within a different shell, or from a file-management program such as Konqueror and Nautilus, it will still know that this script requires the bash shell to run it. The second line is our first command. We use the echo command to display a simple line of information. The third line is a comment. Bash simply ignores it, but it can be useful to a person reading the program in the future to understand what the code is trying to do. While comments are not very useful in such a short, simple script, they are vital in longer and more cryptic ones. This is followed by two more commands - the ls command which takes the directory name as a parameter, and finally another echo to display our final declaration of triumph! When we save this file, it shows up as an ordinary file, which cannot be executed. In order to run it, we must make it executable. To do this, we use the chmod command. In the following example, we have used firstshellscript.sh as the script name - we should change that to whichever name we used. $ chmod u+x firstshellscript.sh

PDF created with pdfFactory Pro trial version www.pdffactory.com

72

Chapter 4 - Shell Programming

Thats it! We can now execute the new script. To do this, enter the following command, again replacing firstshellscript.sh with the name we used if different. rao@laptop $ ./firstshellscript.sh I am about to list the home directory KDesktop firstshellscript.sh public_html snapshot1.png usr-listing.txt Done!

Note the ./ at the beginning of the file - this is actually a directory name (a single period means the current directory). We can avoid this by placing the new script, and any others we create, in a directory within the PATH environment variable. Then we can enter the command from wherever we are.

4.2 META CHARACTERS

The following table provides a list of characters or combination characters called as meta characters [wildcards]. They have special predefined meaning to the shell. By using one or more special symbols, the shell will find those files, which match a pattern, and place them on the command line instead of the pattern itself. v Meaning of some special characters. Character $ | # & ? * $# $* $? $$ Meaning Indicates the beginning of a shell variable name. For example, $var will look for a shell variable named var. Pipes standard output to next command. Start a comment. Executes a process in the background. Matches one character. Matches none or more characters. Number of arguments passed to a shell script. Arguments passed to a shell script. Returns code from the previous executed command. Process identification number.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

73

> <

Output redirection operator. Input redirection operator.

(backquote) Command substitution. >> [] . filename : Output redirection operator (to append to a file). Lists a range of characters. [a-z] means all characters a through z. [a,z] means characters a or z. Executes the file filename Directory name separator in the path.

4.3 POSITIONAL PARAMETERS AND SHELL VARIABLES


The shell has knowledge of a special kind of variable called a positional parameter. Positional parameters are used to refer to the parameters that were passed to a shell program on the command line or a shell function by the shell script that invoked the function. When we run a shell program that requires or supports a number of command-line options, each of these options is stored into a positional parameter. The first parameter is stored into a variable named 1, the second parameter is stored into a variable named 2, and so forth. The shell reserves these variable names so that we cant use them as variables we define. To access the values stored in these variables, we must precede the variable name with a dollar sign ($) just as we do with variables we define. The following shell program expects to be invoked with two parameters. The program takes the two parameters and prints the second parameter that was typed on the command line first and the first parameter that was typed on the command line second. $ vi reverse.sh #program reverse.sh, prints the command line parameters out in reverse #order echo $2 $1 If we invoked this program by entering $ sh reverse.sh hello there the program would return the following output: there hello

PDF created with pdfFactory Pro trial version www.pdffactory.com

74

Chapter 4 - Shell Programming

4.4 BUILT-IN SHELL COMMANDS AND VARIABLES


Several other built-in shell commands and variables are important to know about when we are doing a lot of shell programming. Table below lists these commands and variables and gives a brief description of what each is used for. v BUILT-IN SHELL COMMANDS. Command alias bg break breaksw case cd chdir continue default dirs echo eval exec exit fg foreach glob goto hashstat history if jobs kill Description Defines or lists a command alias Switches a job to background execution Breaks out of a loop Exits from a switch statement Begins a case in switch Changes directory Changes directory Begins the next loop iteration immediately Specifies the default case in switch Lists the directory stack Echoes arguments to standard output Rescans a line for substitutions Replaces the current process with a new process Exits from the current shell Switches a job to foreground execution Specifies a looping control statement Echoes arguments to standard output Alters the order of command execution Prints hash table statistics Lists the command history Specifies conditional execution Lists active jobs Signals a process

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

75

limit login logout newgrp nice nohup notify onintr popd pushd rehash repeat set setenv shift source stop suspend switch time umask unalias unhash unlimit unset unsetenv wait while %job @

Respecifies maximum resource limits Invokes the system logon procedure Exits from a logon shell Changes our Group ID Controls background process dispatch priority Prevents termination on logout Requests notification of background job status changes Processes an interrupt within a shell script Returns to a previous directory Changes directory with pushdown stack Rehashes the directory search path Executes a command repeatedly Displays or changes a shell variable Sets environment variable Shifts parameters Interprets a script in the current shell Stops a background job Stops the current shell Specifies conditional execution Times a command Displays or sets the process file-creation mask Deletes a command alias Disables use of the hash table Cancels a previous limit command Deletes shell variables Deletes environment variables Waits for background jobs to finish Specifies a looping control Specifies foreground execution Specifies expression evaluation

PDF created with pdfFactory Pro trial version www.pdffactory.com

76
v Built-in shell variables Variable $# $? $0 $* $@ Use

Chapter 4 - Shell Programming

Stores the number of command-line arguments that were passed to the shell program. Stores the exit value of the last command that was executed. Stores the first word of the entered command (the name of the shell program). Stores all the arguments that were entered on the command line ($1 $2 ...). Stores all the arguments that were entered on the command line, individually quoted ($1 $2 ...).

4.5 ITERATION, CONTROL AND IF STATEMENTS


if - Used to execute one or more statements on a condition. An example: if [ ! -d /mnt ] # be sure the directory /mnt exists then mkdir /mnt fi case - Used to execute specific commands based on the value of a variable. An example: case $NUM 1) echo The number is 1 ;; 2) echo The number is 2 ;; *) echo The number is not 1 or 2 ;; esac for - Used to loop for all cases of a condition. In the example below, it is used to copy all files found in /mnt/floppy to the /etc directory. The lines were numbered for reference with descriptions: 1 The for loop statement will loop until all files have been found. 2 A test to be sure the file is a normal file and not a directory.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

77

3 A comment line. 4 This line extracts the name of the file from its full path pointed to by the variable $i and puts it in the variable $filename. The method used here is called parameter expansion and is documented in the bash man page. For more information on parameter expansion read the Linux Programmers Guide. 5 This line sends a statement to the standard output, telling what file is being copied. 6 This line performs the copy command using the -p option to preserve file attributes. Note: Much ability to perform script programming is couched in the ability to know and use the various commands, programs and tools available in Linux rather than a strict understanding of syntax. This is obvious to anyone who reads the system startup script files in /etc/rc.d and associated directories. 7 This line ends the if statement. 8 This line ends the for statement. 1. for i in /mnt/floppy/*; do 2. if [ -f $i ]; then 3. # if the file is there 4. filename=${i#/mnt/floppy/} 5. echo copying $i to /etc/$filename 6. cp -p $i /etc/$filename 7. fi 8. done until - Cycles through a loop until some condition is met. The syntax for the command is shown below: until [ expression ] do statements done while - Cycles through a loop while some condition is met. The below example will cycle through a loop forever: while [ 1 ] do statement(s) done

PDF created with pdfFactory Pro trial version www.pdffactory.com

78 4.6 SHELL OPERATORS


For Mathematics, use following operator in Shell Script: NOTE: == is equal, ! = is not equal.

Chapter 4 - Shell Programming

Mathematical Operator in Shell Script

Meaning

Normal Arithmetical/ Mathematical Statements

But in Shell For test statement with if command For [ expr ] statement with if command if [ 5 -eq 6 ] if [ 5 -ne 6 ] if [ 5 -lt 6 ] if [ 5 -le 6 ] if [ 5 -gt 6 ] if [ 5 -ge 6 ]

-eq -ne -lt -le -gt -ge

is equal to is not equal to is less than is less than or equal to is greater than is greater than or equal to

5 == 6 5 != 6 5<6 5 <= 6 5>6 5 >= 6

if test 5 - eq 6 if test 5 - ne 6 if test 5 - lt 6 if test 5 - le 6 if test 5 - gt 6 if test 5 - ge 6

For string Comparisons use:

Operator string1 = string2 string1 != string2 string1 -n string1 -z string1

Meaning string1 is equal to string2 string1 is NOT equal to string2 string1 is NOT NULL or not defined string1 is NOT NULL and does exist string1 is NULL and does exist

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

79

Shell also test for file and directory types:

Test -s file -f file -d dir -w file -r file -x file


Logical Operators :

Meaning Non empty file Is File exist or normal file and not a directory Is Directory exist and not a file Is writeable file Is read -only file Is file is executable

LOGICAL OPERATORS ARE USED TO COMBINE TWO OR MORE CONDITION AT A TIME

Operator ! expression expression1 -a expression2 expression1 -o expression2

Meaning Logical NOT Logical AND Logical OR

4.7 THE TEST COMMAND


In bash and sh, a command called test is used to evaluate conditional expressions. We would typically use the test command to evaluate a condition that is used in a conditional statement or to evaluate the entrance or exit criteria for an iteration statement. The test command has the following syntax: test expression or [ expression ] Several built-in operators can be used with the test command. These operators can be classified into four groups: integer operators, string operators, file operators, and logical operators. The shell integer operators perform similar functions to the string operators except that they act on integer arguments. Table below lists the test commands integer operators.

PDF created with pdfFactory Pro trial version www.pdffactory.com

80
THE TEST COMMANDS INTEGER OPERATORS : Operator int1 eq int2 int1 ge int2 int1 gt int2 int1 le int2 int1 lt int2 int1 ne int2 Meaning Returns True if int1 is equal to int2. Returns True if int1 is greater than or equal to int2. Returns True if int1 is greater than int2. Returns True if int1 is less than or equal to int2. Returns True if int1 is less than int2. Returns True if int1 is not equal to int2.

Chapter 4 - Shell Programming

The string operators are used to evaluate string expressions. Table lists the string operators that are supported by the three shell programming languages.

THE TEST COMMANDS STRING OPERATORS: Operator str1 = str2 str1 != str2 str -n str -z str Meaning Returns True if str1 is identical to str2. Returns True if str1 is not identical to str2. Returns True if str is not null. Returns True if the length of str is greater than zero. Returns True if the length of str is equal to zero. The test commands file operators are used to perform functions such as checking to see if a file exists and checking to see what kind of file is passed as an argument to the test command. Table below lists the test commands file operators.

The test commands file operators: Operator -d filename -f filename -r filename -s filename -w filename -x filename Meaning Returns True if file, filename is a directory. Returns True if file, filename is an ordinary file. Returns True if file, filename can be read by the process. Returns True if file, filename has a nonzero length. Returns True if file, filename can be written by the process. Returns True if file, filename is executable.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

81

The test commands logical operators are used to combine two or more of the integer, string, or file operators or to negate a single integer, string, or file operator. Table below lists the test commands logical operators.

THE TEST COMMANDS LOGICAL OPERATORS: Command ! expr expr1 -a expr2 expr1 -o expr2 Meaning Returns True if expr is not true. Returns True if expr1 and expr2 are true. Returns True if expr1 or expr2 is true.

4.8 AN EXAMPLE SCRIPT


The file below is an example file, which demonstrates some of the testing as talked above along with several looping and control statements. #! /bin/bash # Use the bash shell to run the script # This is an example file to take entries from the user # entries Version 1.0 jan 29, 2005 DONE=no ENTRIES=hello bye ls 1" while [ $DONE = no ] do echo Valid entries are: $ENTRIES read ENTRY case $ENTRY in 1) pwd ;; hello) echo How are you? ;; bye) # Read the variable ENTRY from the user

PDF created with pdfFactory Pro trial version www.pdffactory.com

82
echo exiting... DONE=yes ;; ls) ls -al |more ;; *) echo $ENTRY is an unrecognized command. ;; esac done

Chapter 4 - Shell Programming

4.9 ENVIRONMENT VARIABLES


Environment variables are variables that the shell or any other program can access to get information unique to a specific user. Any program can use environment variables. The vi editor, for example, checks the variable EXINIT for any standard options we want to set every time we run vi. Several environment variables are created and used by the Bourne shell. We can type $ env or $ set to see a listing of all current environment variables. The command used to add or modify environment variables is the export built-in command provided by the bash shell. HOME: This variable is initialized when the interactive shell is executed by the login program. It contains the value of the users home directory. If the cd command is executed without any arguments, the effect is cd $HOME. IFS: This variable sets characters to be internal field separators, or the characters that separate words on a command line. By default, the internal field separators are the space, tab, and newline characters. Setting the IFS adds separators, but space, tab, and the newline character always separate fields. MAIL: This variable specifies the complete pathname of the users mailbox file. MAILCHECK: This variable specifies in seconds how often the mailbox should be checked for incoming mail. MAILPATH: This variable is a colon-separated list of mailbox files to be checked.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

83

PATH: This variable, usually set in our .profile, contains a list of directories that are searched for executables. If an executable, Which is any utility, program, or shell program, is not in the PATH variable, it can only be executed by giving the full pathname of the executable file. The directories are searched in the order in which they appear in the PATH variable. PS1: The value of the interactive shell prompt. The default value in the Bourne Shell is $. [set $ ps1= [pwd] > and then invoke $ echo $PS1 ] PS2: The value of the secondary shell prompt. The default value is >. TERM: This variable is not automatically created on all UNIX systems but is used by so many programs that it is considered a standard environment variable. TERM usually contains the type of terminal that we are using, such as ansi or vt100. SHELL: The current shell program that is being executed. Default is /bin/bash USER - The name of the current user.

4.10 SUMMARY
A shell script is simply a text file containing shell commands. What makes shell scripts especially handy is the capability to execute the commands in the file simply by typing the files name as if it were a command. Generally, well want to write a shell script when we recognize either of two situations: v We find ourselves repeating a lengthy series of commands over and over to accomplish one general task. Any time we need to accomplish a task on a fairly frequent basis (daily, weekly, or maybe several times a day), and the task requires more than one UNIX command, the task is a good candidate for packaging in a shell script. v A repeatable procedure needs to be established for a formal activity. Printing a weekly customer invoicing report, for example, might require a complex procedureextracting billing information from a master file, computing the invoice data, setting up the printer, and actually generating the print file. When variables are used they are referred to with the $ symbol in front of them. There are several useful variables available in the shell program. Here are a few: $$ = The PID number of the process executing the shell. $? = Exit status variable. $0 = The name of the command we used to call a program. $1 = The first argument on the command line.

PDF created with pdfFactory Pro trial version www.pdffactory.com

84
$2 = The second argument on the command line. $n = The nth argument on the command line. $* = All the arguments on the command line. $# The number of command line arguments.

Chapter 4 - Shell Programming

The shift command can be used to shift command line arguments to the left, ie $1 becomes the value of $2, $3 shifts into $2, etc. The command, shift 2 will shift 2 places meaning the new value of $1 will be the old value of $3 and so forth.

4.11 CHECK YOUR PROGRESS


I. Exercises Work on the following list of shell programming concepts.
1) Write a simple shell script program for displaying current date, all logged-in users and process status using switch-case construct. 2) Write a shell script program to count the number of vowels, consonants and digits in a given text. 3) Write a shell script program to check whether a given string is palindrome or not. 4) Explain all the test operators related to files with a example.

II. Choose the correct answer 1) Shell is a __________.


a) Interpreter b) Compiler c) Assembler d) Linker 2) Shell scripts are not useful when applications are__________ a) complex b) portable c) secured

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

85

d) all of the above 3) All LINUX systems have the following _________ a) stdin b) stdout c) stderr d) all of the above 4) < redirects the _________ a) stdin b) stdout c) stderr d) none of the above 5) chmod 777 scriptname gives _________ permission. a) Read / execute b) Read / write / execute c) Read / write d) Write / execute 6) The symbol used for command separator ____. a) ; 7) b) ;; c) , d) .

Terminator in a case option ___ . a) ; b) , c) d) ;;

8)

________ runs job in background. a) Sleep 100 b) Sleep 100 & c) & sleep 100 d) sleep & 100

PDF created with pdfFactory Pro trial version www.pdffactory.com

86
9) ____ is the name of the script. a) $1 10) b) $2 c) $0 d) none of the above

Chapter 4 - Shell Programming

shift command

_____________________________________.

a) reassigns the positional parameters, in effect shifting them to the right one notch. b) reassigns the positional parameters, in effect shifting them to the left one notch. c) reassigns the positional parameters, in effect shifting them to the both right & left one notch. d) None of the above. 11) Pipelines are used to ________ commands. a) reverse 12) b) connect c) both a) & b) d) none of these

_____ is a synonym for test. a) ] b) [ c) both a) & b) d) none of these

13)

[ $a ne $b ] is ________________. a) Arithmetic comparison b) String comparison c) Both a) & b) d) Neither a) nor b)

14)

Following are the internal variables _______. a) $HOME b) $PATH c) all of the above d) none of the above

15)

case test line ends with ______. a) ) b) ( c) )) d) none of the above.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

87

III. Say True or False 1. Exit status 0 results false.


2. 3. 4. Bash an acronym for Bourne-Again Shell. The backquotes around the command illustrate the use of command replacement. Environment variables are variables that the shell or any other program can access to get information unique to a specific user.

True/False True/False True/False

True/False

IV. Essay type questions 1) What is shell? Why shell programming is required.
2) 3) 4) 5) 6) 7) 8) 9) When shell scripts are nt useful? Breifly explain the access permissions related to different types of users. Elaborate all the looping control constructs. Explain all the string-related operators. What are meta characters [wildcards]? Give examples. What are positional parameters? Give examples. Elucidate built-in shell commands and variables. Explain the test command with all options.

V. Further readings and other activities 1. Get more information using man or info or any help command for ksh, sh, bash, tcsh, csh, ksh, ls, vi. EX: 1. $ man sh 2. $ apropos shell Work on all of the example explained in the unit. Some exercises also are given there itself relating to the particular topic.
2. For further readings you can refer the following books Title: Linux internals Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.pandurangarao.i8.com/doc/linux/linuxbook.html http://www.raomvp.bravepages.com/doc/linux/linuxbook.html

PDF created with pdfFactory Pro trial version www.pdffactory.com

88
A complete reference for you to get more information. You can also extract solutions for exercises. Title: UNIX Shell Programming

Chapter 4 - Shell Programming

Author: Stephen G. Kochan and Patrick H. Wood; Howard Sams; ISBN 0-672-48448-X; 1990. An excellent introduction to Bourne and Korn shell programming with lots of illustrative examples. Title: Unix Shell Programming Author: Yashawant Kanitkar Publication: BPB

VI. Solutions I. Choose the correct answer


1.a 2.d 3.d 4.a 5.b 6.a 7.a 8.b 9.c 10.b 11.b 12.b 13.a 14.c 15.a II. Say True or False 1.False 2.True 3.False 4.True

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 5

File System

5.0 INTRODUCTION AND OBJECTIVES

inux is inherently multi-user. Your personal settings (and all other personal files) are in your home directory which is /home/your_user_login_name. Many settings are kept in files with names starting with a dot .

LINUX IS CASE-SENSITIVE. Your user login name and password are also case sensitive. Filenames can be up to 256 characters long and can contain letters, numbers, . (dots), _ (underscores), - (dashes), plus some other non-recommended characters. Files with names starting with . are normally not shown by the ls (list) or dir command. Think of these dot files as hidden. Use ls -a (list with the option all) to see these files. / is an equivalent to DOS \ (root directory, meaning the parent of all other directories, or a separator between a directory name and a subdirectory or filename). Under Linux, all directories appear under a single directory tree (there are no DOS-style drive letters). This means directories and files from all physical devices are merged into this single-view tree. Under Linux, as in any multiuser operating system, directories and files have an owner and set of permissions. You will typically be allowed to write only to your home directory which is /home/ your_user_login_name.

Objectives
After completing this section, you will be able to:

Understand a typical Linux filesystem

MSIT 3A Linux Internals

89

PDF created with pdfFactory Pro trial version www.pdffactory.com

90

Basic types of files Navigate the file hierarchy Manipulate files and directories Operations on directories Options of inodes Links- hard links and soft links Handle access control Changing access privileges: chmod chmod symbolically and numerically umask masking Privileges Deal with special files and links Mounting volumes and filesystems

Chapter 5 - File System

5.1 FILE SYSTEM OVERVIEW


Linux uses ext2 as its native filesystem. It also supports many other types. All data stored on a Linux system is a file. Ext2 file names can be 1 to 255 characters long. Only / and nul are disallowed. Nonnative filesystems have different features. Ext2 sees only two basic types of files: directories and files.

5.2 TYPES OF FILES


Files come in 8 flavors: Normal Files Directories Hard Links Symbolic links Sockets Named Pipes Character Devices Block Devices

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

91

Normal Files: These are the files you use the most. They can be either text or binary files; however, their internal structure is irrelevant from a System Administrator standpoint. An ls -l on a normal file will look something like this. -rw------------- 1 rao admin 42 May 12 13:09 hello

Directories: These are a special kind of file that contains a list of other files. Although there is a one-toone mapping of inode to disk blocks, there can be a many-to-one mapping from directory entry to inode. When viewing a directory listing using the ls -l command, you can identify directories by their permissions starting with the d character. drwx------------2 rao admin 512 May 12 13:08 public_html

Hard Links:
A hard link is actually a normal directory entry except instead of pointing to a unique file, it points to an already existing file. This gives the illusion that there are two identical files when you do a directory listing. Because a hard link shares an inode, it cannot exist across file systems. Hard links are created with the ln command. For example, given this directory listing using ls -l, we see: -rw------------- 1 rao admin 42 May 12 13:04 hello

When you type $ ln hello goodbye and then perform another directory listing using ls -l, you see: -rw------------- 2 rao admin -rw------------- 2 rao admin 42 May 12 13:04 goodbye 42 May 12 13:04 hello

Notice how this appears to be two separate files that just happen to have the same file lengths. Also note that the link count (second column) has increased from one to two. How can you tell they actually are the same file? Use $ ls -il. Observe: 13180 -rw------------- 2 rao admin 13180 -rw------------2 rao admin 42 May 12 13:04 goodbye 42 May 12 13:04 hello

You can see that both point to the same inode, 13180. Symbolic Links: A symbolic link (sometimes referred to as a symlink) differs from a hard link because it doesnt point to another inode but to another filename. This allows symbolic links to exist

PDF created with pdfFactory Pro trial version www.pdffactory.com

92

Chapter 5 - File System

across file systems as well as be recognized as a special file to the operating system. Symbolic links are created using the ln -s command. A common thing people do is creating a symbolic link to a directory that has moved. For example, if you are accustomed to accessing the directory for your home page in the subdirectory www, but at the new site you work at, home pages are kept in the public_html directory, you can create a symbolic link from www to public_html using the command ln -s public_html www. Performing an ls -l on the result shows the link. drwx------------- 2 rao admin lrwx------------- 1 rao admin Sockets: Sockets are the means for UNIX to network with other machines. Typically, this is done using network ports; however, the file system has a provision to allow for interprocess communication through socket files. If you need to remove a socket file, use the rm command. Socket files are identified by their permission settings beginning with an s character. An ls l on a socket file looks something like this: srwxrwxrwx 1 root Named Pipes: Similar to sockets, named pipes enable programs to communicate with one another through the file system. You can use the mknod command to create a named pipe. Named pipes are recognizable by their permission settings beginning with the p character. An ls -l on a named pipe looks something like this: prw------------- 1 rao admin Character Devices: These special files are typically found in the /dev directory and provide a mechanism for communicating with system device drivers through the file system one character at a time. They are easily noticed by their permission bits starting with the c character. Each character file contains two special numbers, the major and minor. These two numbers identify which device driver that file communicate. An ls -l on a character device looks something like this: crw-rw-rw- 1 root Block Devices: Block devices also share many characteristics with character devices in that they exist in the /dev directory. They are used to communicate with device drivers. They have major and minor numbers. The key difference is that block devices typically transfer large blocks of data at a time versus one character at a time (A hard disk is a block device, whereas a wheel 21, 4 May 12 13:40 ptyp4 0 May 12 22:02 mypipe admin 0 May 10 14:38 X0 512 May 12 13:08 public_html 11 May 12 13:08 www -> public_html

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

93

terminal is a character device). Block devices are identified by their permission bits starting with the b character. An ls -l on a block device looks something like this: brw------------- 2 root staff 16, 2 Jul 29 1992 fd0c

5.3 DIRECTORY POINTER

Directories are files that list other files. It can be normal files or directories. Also enables a hierarchy to be built. Each directory entry consists of two parts: a file name and an inode number. (An inode is roughly a pointer to a file, see table) The topmost directory is always called /. i.e called the filesystem root. Directory information can only be changed by Linux itself. It also ensures a proper structure to be maintained.

5.4 DIRECTORY HIERARCHY

PDF created with pdfFactory Pro trial version www.pdffactory.com

94
By tradition several directories have specialized roles

Chapter 5 - File System

User-installed programs typically go under the /usr/local hierarchy /mnt is not always present, it is merely a convenience to place all mounted devices under one place

5.5 PATHNAMES

Fig. 5.1 Pathname

Files can be referred to by relative or absolute pathnames $ cd /usr/sbin/httpd $ cd usr/local/bin/safe-mysqld Absolute pathnames begin with /

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

95

The absolute pathname refers to one file only.[ reference by root / ] A relative pathname does not begin with / and describes the path from the current directory to find a file, [cd .. or .] Example: $ cd sbin/httpd ; $ cd bin/safe ; $ cd ..

5.6 CURRENT DIRECTORY


When you log in your shell is placed in your home directory. Typically /home/usrname. Superuser typically has /root for a home directory ~ is a synonym for /home/usrname. cd changes your current directory, Typing cd path changes your current directory to path. path can be absolute or relative. Without arguments cd changes to your home directory. pwd tells you the current directory.

5.7 DOT (.) AND DOTDOT(..)

Directories always contain two entries . and .. Used for relative pathnames and navigation. Example:

This last row above shows forced execution of a particular file. If we had simply typed a.out then our PATH environment variable would be used to search for the file.

PDF created with pdfFactory Pro trial version www.pdffactory.com

96

Chapter 5 - File System

It may execute another a.out instead of the one we actually want.

5.8 MOVING AND COPYING FILES


The mv command is used to move files: $ mv oldname newname $ mv somefile .. The cp command is used to copy files: $ cp thisfile newfile $ cp file1 file2 file3 /tmp Full details of applicable options and usage can be found by typing man mv or man cp.

5.9 REMOVING FILES


Files are removed using the rm command: $ rm thisfile thatfile rm: remove thisfile? y rm: remove thatfile? y $ Most notable among the options are :

Example: $ rm r somedir Again, full details are available by typing man rm. Removing a file is not considered an operation on the file, It is an operation on the directory. Filenames are merely links.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

97

5.10 OPERATIONS ON DIRECTORIES

These commands can take many arguments. mkdir can be told to create the whole pathname of directories if they dont exist. Example: $ mkdir p /home/lee/new1/test/directory Will create the directories /home/lee/new1 and /home/lee/new1/test as well as /home/lee/new1/test/directory if they dont already exist. ls arguments control what information is shown and how its sorted, Some are explained later, consult $man ls for full details.

5.11 INODES
Each file is represented by an inode. An inode contains information about: File type (ordinary, directory, FIFO, block device etc.) Owner ID (user the file belongs to) Size (in bytes) Access, creation, and modification times Group ID (group the file belongs to) File permissions Mapping of the file contents (data sectors) Inode layout and location varies with filesystem type The term inode was invented by Dennis M. Ritchie of AT&T.

PDF created with pdfFactory Pro trial version www.pdffactory.com

98 5.12 OPTIONS OF INODES


Example: $ ls -i displays inode numbers of entries.

Chapter 5 - File System

stat prints the inode contents for files including permissions, size, links, access times etc.

5.13 LINKS
More than one filename can refer to an inode, These file names are links to the file. ln creates links to files. Creates hard links by default. ln -s creates symbolic or soft links. Erasing a file just removes its directory entry, The file is only lost when all entries or it have been removed.

Crucially : A filename is not the file The inode is the file All names are simply links (references) to the inode Vague resemblance to Windows shortcuts

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

99

5.14 HARD LINKS


A hard link is merely a directory entry with the relevant inode number Consider the following

Start with: We create a hard link:

Note: Hard links cannot cross filesystems. Inode numbers are filesystem specific.

5.15 SOFT LINKS


Soft links store the pathname of the linked file. This means they can cross filesystems.

PDF created with pdfFactory Pro trial version www.pdffactory.com

100
Adding a soft link :

Chapter 5 - File System

If we replace the test file with another then the symbolic link still works, but the hard one still points to the old file!

5.16 ACCESS CONTROL AND UID


File access can be limited to specific users. Super user(s) can override access control. Access control is set by user and group ID. Each user has a user-id (UID) and one or more group-ids(GIDs) Processes have an associated UID and GID, Inherited from the user who created the process. They can however can be changed: Processes are known as set-user ID (setuid) if they set their own user ID. or set-group ID (setgid) if they set their own group ID.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

101

5.17 CATEGORIES OF ACCESS CONTROL


There are three categories of access

These may be specified for three sets of users: User Group Everyone

5.18 ACCESS CONTROL EXAMPLE


Example: $ ls -l shows the access permissions.

5.19 CHANGING ACCESS PREVILEGES: CHMOD


Only the owner of a file (or the super-user) may alter its access permissions. chmod (change mode) changes access permissions. Works in two ways, symbolically or numerically. Symbolically is easier to remember (for most).

PDF created with pdfFactory Pro trial version www.pdffactory.com

102 5.20 CHMOD SYMBOLICALLY


Chapter 5 - File System

Select who you want to change permissions for (u=user, g=group, o=others, a=all}. Decide whether you want to grant a permission (+), remove(-), or set(=) it. Take the permission that you want to change (r=read, w=write, x=execute). Example: $ chmod gu+w filename Adds write permission for user and group. You can make several changes by separating the settings with commas. Example: $ chmod a-w,gu=rw filename Removes write permission for all, then grants it for the user and group.

5.21 CHMOD NUMERICALLY


Once you know this it is often quicker, A number represents each permission type. 4 read permission 2 write permission 1 execute permission Add up the permission numbers you want for each user group (owner, group, all) and supply these to chmod. Example: $ chmod 755 filename grants all permissions to the owner (4+2+1), and read and execute (4+1) to group and all others.

5.22 UMASK MASKING PRIVILEGES


Files begin with a default access setting, Specified by a users umask setting. This only works numerically, Unlike chmod, specified permissions are turned off With a umask setting of 000 files are created with permissions rw-rw-rw- (666). Default umask is 022 which means files are typically created rw-rr (644). Example:

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

103

5.23 FILE SYSTEM STRUCTURE


Multi-Volume Filesystems of linux. The filesystem can be held on several devices. Large disks can be divided into partitions, This creates several logical devices. A basic Linux system must be present on /. Other parts of the fs may be mounted at any time. The main ones are mounted at boot time. This is controlled by the /etc/fstab file which says what volumes are mounted where. [/etc/ mtab also gives volume information]

5.24 /ETC/FSTAB EXAMPLE

5.25 SPECIAL FILES - /DEV


Files under /dev typically represent devices attached to your computer. Programs can open and close them and read from and write to them - as with regular files. Kernel code exactly handles and extracts work from these. Two types: Block Special- Disk drives, tape drives, CDROMs. Character Special- Printers, modems, etc

PDF created with pdfFactory Pro trial version www.pdffactory.com

104 5.26 SPECIAL FILES - /PROC


Chapter 5 - File System

The section of the filesystem called /proc doesnt contain real files. It contains system status information.

For example:

Table 5.1 : System Information from /proc

5.27 MOUNTING ADDITIONAL VOLUMES


To mount a filesystem use mount command. For example: $ mount /dev/cdrom /mnt/cdrom Mounts the filesystem /dev/cdrom in the directory /mnt/cdrom. $ cd /mnt/cdrom changes directory to the root of the CDROMs filesystem. To unmount use $ umount name where name is either the filesystem name or the mount point: $ umount /dev/cdrom $ umount /mnt/cdrom

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

105

A filesystem can only be unmounted when it is no longer in use. In use includes: Having any file on that filesystem open. Having a shell in a directory on that filesystem.

5.28 MOUNTING SHARED FILESYSTEMS


NFS filesystems can be mounted with Example: $ mount t nfs hostname:path mount-point $ mount t nfs landlord:/backup /mnt/backup Share files from MS-Windows machines using SAMBA. This is a free implementation of the Windows file-sharing protocols. Example: $ mount t smbfs \\ntbox\c /mnt/ntbox Linux does not use the drive letter concept at all. Drives and shares integrate seamlessly into the filename tree.

5.29 SUMMARY
The primary Linux filesystem is Ext2. It has a tree-like hierarchy of directories. Directories merely contain pointers to files(inodes). inodes contain all the information about a file. Also can have multiple links to the same file. Read/Write access is controlled per file. Creation/Deletion of files is controlled by permissions of the directory. Several filesystems can be mounted to create the directory hierarchy. This chapter described the basic overview of how to use a command shell. There is a lot more, which would be very useful especially when writing shell scripts or performing advanced tasks. However the above should be serve as a good beginning to understand what is going on in the system, as well as a starting point from which you can learn more details about Linux shell programs. Remember that the bash shell has its own man page, as well as longer tutorials and documentation available at the Linux Documentation Project website.

PDF created with pdfFactory Pro trial version www.pdffactory.com

106 5.30 CHECK YOUR PROGRESS


I. Exercises 1. Basic navigation

Chapter 5 - File System

(a) Log in and use pwd to discover what the full path of your home directory is. (b) Change directory to /bin and then /tmp. Use pwd to check you got there each time. (c) When in /tmp type cd .. and use pwd to find out where you end up. (d) What is the parent directory of the root of the filesystem? Why is this so? (e) Move back to your home directory. Think of three ways you can do this. 2. Directories (a) Start in your home directory and create a directory called new. (b) Change to the new directory and create a directory called newer. (c) Go to you home directory. Now create a directory under newer called newerstill. There are two ways to do this what are they? (Hint: You dont have to change directories to solve this.) (d) Remove all the directories that youve just created, there are several ways to do this. (e) Create the same directory structure with one command. 3. Links (a) (b) (c) (d) (e) 4. Create a file called test in your home directory (Typing echo 123> test should do this). Now create a hard link to test called h_test and a symbolic link to test called s_test. Find out the inode number of the files. Check you understand why they are what they are. Remove the original file called test. Can you still get at the contents of the original file? What happens if you try cat s_test. Make sure you understand the distinction between h_test, and s_test. Try to make a hard link to your home directory. Why does this fail?

/proc (a) (b) Use the files in /proc to find out how much memory your system has and what processor it is running on. Find out what PCI devices are attached to your machine.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

107

(c) (d)

Find out what environment variables are set for your currently running shell using the information in /proc. Hint you can get the process-id of your shell using $$. Whether or not your machine is doing IP forwarding is stored in the file /proc/sys/net/ipv4/ ip_forword. You can cat this file, a value of 1 means that IP forwarding is turned on. Find out whether or not your machine will forward IP.

(e)

Find out how many files are currently open on your system.

II. Choose appropriate answer 1. File contains _____.


a. digitally encoded pictures b. program c. sound d. all of these. 2. In case of ___________ pathname ,the path starts from root. a. absolute b. relative 3. c. both of these d. none of these

_______ command used to change the permissions of your files & directories. a. ls b. chmod c. more d. ls -F

4.

chmod 555 dirname is used ____________________________. a. To protect the files in a directory and all its subdirectories from everyone else on your system. b. To keep yourself from accidentally removing files in a directory. c. To let other people on the system see whats in a directory - and read or edit the files if the file permissions let them - but not rename, remove, or add files. d. none of the above.

5.

chmod 644 filename is used to________________________. a. To let non group users read but not edit the file b. To let anyone read or edit the file c. To make a private file that only you can edit d. none of the above.

6.

For using FTP ____________________________. a. Both computers dont need to be running LINUX

PDF created with pdfFactory Pro trial version www.pdffactory.com

108
b. Both computers need to be running LINUX

Chapter 5 - File System

c. one computer must be running LINUX & other any operating system d. none of the above. 7. The acronym for EXT2 is ________________________. a. Second Extended File system b. Extended File system c. Expanded File system d. all of the above 8. mount command contains the ____________. a. physical block device that contains the file system b. name of the file system c. both of above d. none of these

III. Say True or False 1. A directory is actually a special kind of file.


2. 3. 4. 5. 6. In case of absolute pathname, the path starts from your working directory. ls -l puts a / (slash) at the end of each directory name. chmod 755 dirname gives full access to everyone on the system. chmod 664 filename allows nongroup users to read but not edit the file Filenames can have any character including /.

True/False True/False True/False True/False True/False True/False

IV. Essay type Questions 1. Briefly explain the concept of LINUX file system..
2. Explain the following: i. PATH ii. Absolute pathname and relative pathname. iii. Files in the Directory Tree.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

109

iv. Directory Access Permission v. File Access Permission 3. 4. 5. 6. Differentiate among chmod & umask. Explain all the attributes related to ls al . Describe the concept of File Management. Write a note on the following filesystems i /dev ii /poc 7. 8. 9. what is uid? does it relate with access control mechanism of files. How to mount a Shared File system. What is a link? Justify your answer by giving proper examples for its types.

V. Further readings and other activities 1. Get more information using man or info or any help command for sh, bash, csh, ksh, inode, fs, env, set, mount, mknd, id, dev, stat.
2. For further readings you can refer the following books Title: Linux internals Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.pandurangarao.i8.com/doc/linux/linuxbook.html http://www.raomvp.bravepages.com/doc/linux/linuxbook.html A complete reference for you to get more information. You can also extract solutions for exercises. More information on filesystem can be obtained from text book title: Introduction to the Shell programming Author: Yashwanth Kanetkar

VI. Solutions I. Exercises 1. Basic Navigation


(a) You will probably see $ pwd

PDF created with pdfFactory Pro trial version www.pdffactory.com

110
/home/username where username is the name you log in with. (b) Check that the output of pwd is /bin and /tmp. (c) You should end up in /. This is the root of the filesystem.

Chapter 5 - File System

(d) The parent of / is itself. You can use ls -lia to show that the inode numbers for . and .. are the same when you are in /. (e) You can move back to your home directory by using any of the following: i. cd ii. cd ~ iii. cd /home/username (Provided your home directory lives under /home) 2. Directories (a) $ cd ; $ mkdir new (c) $ cd ; then either: i. $ cd new/newer ; $ mkdir newerstill ii. $ mkdir new/newer/newerstill (d) Any of the following will work : i. $ cd ; $ cd new/newer ; $ rmdir newerstill ; $ cd .. ; $ rmdir newer ; $ cd .. ; $ rmdir new ii. $ cd ; $ rmdir p new/newer/newerstill ; iii. $ cd ; $ rm fr new/ (e) $ cd ; $ mkdir p new/newer/newerstill 3. Links (a) This is achieved as follows: $ ln test h_test $ ln s test s_test (b) ls -li should show that the inode number of the original file and h_test are identical, h_test is another name for the original file. The inode number for s_test will be different. It is a separate file that contains information about the location of the file it is a link to. (c) After you created the hard link the original file had two names test and h_test. You have removed test but until all names for a file have been removed it is still accessible. In this case you can do cat h_test to see the contents of the file. (b) $ cd new ; $ mkdir newer

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

111

(d) This should fail with a No such file or directory message. s_test contained a pointer to the file test not the inode number. There is no longer a file named test so this cannot work. Hard links reference a file by its inode number, symbolic links reference it by its name. (e) This is not allowed, as it could stop the filesystem being strictly hierarchical. 4. /proc (a) cat /proc/meminfo should show you memory usage. (b) cat /proc/pci gives a list of all PCI devices. (c) cat /proc/$$/environ will give a list of the environment of your current shell. Each variable is delimited with the nul character (decimal 0). The following will show the output with one variable per line : $ cat /proc/$$/environ | tr \verb|\|000 \verb|\|n (d) See The Exercise (e) $ cat /proc/sys/fs/file-nr

II. Choose appropriate answer 1.d 2. a 3.b 4.b 5.d 6.a 7.a 8. c III. Say True or False 1. True 2. False 3. True 4. False 5. True 6. False

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 6

File System Administration

6.0 INTRODUCTION AND OBJECTIVES

resented in this chapter are the fundamentals and many useful specifics of the Bourne again shell, currently the most popular of the Linux shells for execution of application programs. Also described are the steps to organize file system and script shell commands, a detailed coverage on partitioning methods of hard disk. Creation and analysis of links, inodes are justified. Disk recovery techniques, maintenance and and space information also enumerated.

OBJECTIVES
After completing this module you should be able to understand and utilize these features of the Linux filesystem: Inode parameters Links creation and analysis Links: relationship with inode Non-native filesystem types Native filesystems types Filesystem checking and recovery Disk usage and free space Disk partitioning utilities Chapter 6 - File System Administration

112

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

113

Problems with LILO and partitions Filesystem creation A detailed look at fdisk() Block, character and raw Devices

6.1 INODE IN DEPTH


Every file/directory is specified by an inode. A single inode can have many filenames (links). An inode contains information about: File type (ordinary, directory, FIFO, block device etc.). Owner ID (user the file belongs to). Size (in bytes). Access, creation, and modification times. Group ID (to which group does the file belong). File permissions. Mapping of the file contents (data sectors). Knowing inode numbers can be very useful if you want to restore files on a disk with bad blocks. Inode layout and location varies with fs type.

6.2 INODE TABLE ENTRY


An inode maintains information about each file. Depending on the type of file system, the inode can contain upwards of 40+ pieces of information. Most of it, however, is only useful to the kernel and doesnt concern us. The fields that do concern us are mode link count user ID group ID The permission mask and type of file. The number of directories that contain an entry with this inode number. The ID of the files owner. The ID of the files group.

PDF created with pdfFactory Pro trial version www.pdffactory.com

114
size access time mod time inode time block list indirect list Number of bytes in this file. The time at which the file was last accessed. The time at which the file was last modified.

Chapter 6 - File System Administration

The time at which this inode structure was last modified. A list of disk block numbers which contain the first segment of the file. A list of other block lists.

The modes, link count, user ID, group ID, size and access time are used when generating file listings. Note that the inode does not contain the files name. That information is held in the directory file (see below for details). Example: ls i displays inode numbers of filenames.

stat prints the inode contents for files including permissions, size, links, access times etc.

6.3 LINKS: RELATIONSHIP WITH INODE


More than one filename may refer to an inode. These file names are links to the file. ln creates links to files.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals Creates hard links by default. ln s creates symbolic or soft links.

115

Erasing a file simply removes its directory entry. Only when all entries for a file have been removed is the file lost. Crucially : A filename is not the file, The inode is the file. All names are simply links (references) to the inode

6.4 HARD LINKS


A hard link is merely a directory entry with the relevant inode number. Consider the following, We start with : $ ls li

We create a hard link :

Note: Hard links cannot cross filesystems. Inode numbers are filesystem specific.

6.5 SOFT LINKS


Soft links store the pathname of the linked file. This means they can cross filesystems. Adding a soft link :

PDF created with pdfFactory Pro trial version www.pdffactory.com

116

Chapter 6 - File System Administration

If we replace the test file with another then the symbolic link still works, the hard one still points to the old file!

6.6 NON-NATIVE FILESYSTEMS


Besides Ext2, Linux supports most well known filesystems. Example: MS-DOS (FAT16), VFAT, FAT32, ISO9660 (CD-ROM), NTFS (Windows NT), SMB / CIFS (MS Windows file sharing). Most can be mounted, read & written. Specific tools can create some non-native filesystems (e.g. DOS/CD-ROM). Some non-native filesystems can even be checked and repaired (e.g. Minix). Not wise to automate checking and repair on non-native systems.

6.7 DISK CHECKING AND RECOVERY (FSCK)


fsck checks and repairs Linux filesystems. GENERIC SYNTAX:

Actually, just a front-end for specific filesystem checkers , e.g. e2fsck The filesystem-specific checker is searched for in these places in this order: /sbin. /etc/fs and /etc. Directories in the PATH environment variable.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

117

Common options: -A uses /etc/fstab to ID and check all filesystems in one run. -R skips the root filesystem when the -A option is operative.

6.8 CHECK A LINUX EXT2 FILESYSTEM (E2FSCK)


e2fsck is the application called by fsck to check ext2 filesystems. SYNTAX Main options: $ e2fsck [options] device Devices specified as /dev/hdXX, /dev/sdXX, etc.,

6.9 DISK FREE SPACE (DF)


df shows free space on filesystems. SYNTAX: $ df [options] [filesystem list] Defaults to all currently mounted filesystems. Displays in 1K blocks by default; environment variable POSIXLY_CORRECT sets 512-byte GNU df cant show space on unmounted filesystems. Main options:

PDF created with pdfFactory Pro trial version www.pdffactory.com

118

Chapter 6 - File System Administration

6.10 DISK USAGE (DU)


du reports disk space used by a directory, including its sub-directories or a file. Useful for summarizing file/directory sizes. SYNTAX: $ du [options][pathlist] GNU du defaults to 1K blocks, unless set POSIXLY_CORRECT (512-byte).

6.11 DISK PARTITIONING CONCEPTS


Partitioning enables efficient use of large drives by dividing them into smaller sections. A Partition table at the start of each disk points to the beginning or end of each partition. Max 4 Primary Partitions on standard disks. More possible inside Extended Partitions.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

119

An Extended Partition has its own partition table; pointing to sub-divisions within it. Sub-divisions called Logical partitions (drives).

6.12 A PARTITIONED DISK

Figure 6.1 shows a partitioned disk

6.13 MAKING AND CHANGING PARTITIONS


Linux usually benefits from putting parts of its filesystem on different drives or partitions. Example: access different parts simultaneously. Issues: How many partitions? Size of partitions for specific directories. Partitioning an empty drive. Destructive re-partitioning. Non-destructive re-partitioning.

PDF created with pdfFactory Pro trial version www.pdffactory.com

120 6.14 HOW MANY PARTITIONS?


How many partitions you need depends on: How you intend to use a system. What resources you have (e.g. physical disks).

Chapter 6 - File System Administration

A common single-disk server might have separate partitions for: Swapping. The / filesystem/directory The /boot filesystem/directory The /home filesystem/directory The /usr filesystem/directory The /var filesystem/directory The /usr/doc filesystem/directory

6.15 WHAT SIZE PARTITIONS?


What size partitions you need depends entirely upon how you intend to use the system. Filesystems needing large partitions are usually: /home for users file space. /var for server files (news, web, mail, logs etc). The following are usually made just big enough: the swap partition (rarely more than 127 Mb). /boot (enough for a few alternative kernels).

6.16 BIOS PROBLEMS WITH LILO AND PARTITIONS


BIOSes on most Intel-type machines cant access data beyond cylinder 1023. To start Linux, LILO uses data in /boot. The /boot partition must always be located entirely below cylinder 1023.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

121

Multiple IDE drive machines always work /boot is on the first primary controller. On mixed IDE/SCSI systems /boot must be on the first IDE primary controller or SCSI ID 0. On multiple SCSI drive systems /boot must be on ID 0 or ID 1.

6.17 DISK PARTITIONING TOOLS


Tool fdisk Notes Standard on all Linux and much UNIX. Very Flexible. Tricky character-based interface, Fairly reliable Non-destructive re-sizing of partitions Difficult CLI. Some distrust it. Red Hat install-time tool. Friendlier character-based interface. Set growable partitions. Set mount points Used in Caldera X-based installation. Allows non-destructive repartitioning on the fly. Easy and reliable. Proprietory. Not always available As above, but the full commercial product and price

fipa Disk Druid

Partition Magic Lite

Partition Magic

Common partitioning tools:

6.18 USING FDISK()


fdisk basically writes partition tables. Very dangerous, but not that difficult. SYNTAX: $ fdisk [options][disk-device-name] One useful CLI option: -l lists available partition tables then exits. Usually super user-only and needs explicit path. 2 tips: v Never use w (write/save changes) unless you really know what you are doing. v Use q (quit) to exit without saving changes.

PDF created with pdfFactory Pro trial version www.pdffactory.com

122 6.19 THE FDISK INTERFACE


Chapter 6 - File System Administration

The only helpful thing about fdisk screens is that they all prompt with (m for help). Launched without arguments on IDE: [root@jnnce]# /sbin/fdisk using /dev/hda as default device! Command (m for help): v p prints the partition table for current hard disk.

6.20 INTERACTIVE COMMANDS IN FDISK


A selection of fdisk interactive commands:

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

123

6.21 MAKING LINUX FILESYSTEMS (MKE2FS)


Normally, you can only write a file to a device with a filesystem on it. Native Linux filesystems are made using mke2fs. Utilities for other types: mkdosfs, mkisofs, etc., SYNTAX: $ mke2fs [options] device Filesystems are made on un mounted devices. If omitted, mke2fs auto-calculates fs size. Lots of options, few widely used:

6.22 BLOCK DEVICES


Typically, block devices are disks and tapes. Strictly, I/O devices with these characteristics: Seen by kernel as a range of blocks 0 to n-1. Where n = number of blocks on the device. Can have a filesystem mounted on it. Ability to perform random access reads. A specific block size. Handling only one data block at a time. Only accepts actions on whole data blocks. Kernel buffers its I/O.

PDF created with pdfFactory Pro trial version www.pdffactory.com

124 6.23 CHARACTER DEVICES


Any device which is not a block device. Typically: printers terminals modems

Chapter 6 - File System Administration

Drivers determine how a program reads from and writes to it. For example, a terminal device driver lets programs read typed info in two ways: In raw mode (i.e without driver interpretation). A line at a time with the driver removing erase and kill chars (typos and corrections), so: v The program reads everything on a line. v The number of characters on a line can vary.

Kernel doesnt buffer character devices

6.24 SUMMARY
Commands used to manage file systems
badblocks(8) - Search a device for badblocks. The command badblocks /dev/hda will search the first partition of the first IDE hard drive for badblocks. cfdisk(8) - A partition table manipulator used to create or delete disk partitions. dosfsck(8) - Used to check a msdos filesystem. dumpe2fs(8) - Lists the superblock and blocks group information on the device listed. Use with a command like dumpe2fs /dev/hda2. The filesystem on the device must be a Linux filesystem for this to work. fdformat(8) - Performs s lowlevel format on a floppy disk. Ex: fdformat /dev/fd0H1440. fdisk(8) - Used to add or remove partitions on a disk device. It modifies the partition table entries. fsck(8) - Used to check and/or repair a Linux filesystem. This should only be used on systems that are not mounted.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

125

hdparm(8) - Used to get or set the hard disk parameters. mkdosfs(8) - Used to create a msdos filesystem. mke2fs(8) - Create a Linux native filesystem which is called a second extended filesystem. This creates the current version of the Linux filesystem. mkfs(8) - Used to make a Linux filesystem on a device. The command mkfs /dev/hdb1 will create a Linux filesystem on the first partition of the second IDE drive. mkswap(8) - Creates a Linux swap area on a device. mount(8) - Used to mount a filesystem. It supports many types of filesystems. stat(1u) - Used to print out inode information on a file. Usage: stat filename swapoff(8) - Used to de-activate a swap partition. swapon(8) - Used to activate a swap partition. tune2fs(8) - Used to adjust filesystem parameters that are tunable on a Linux second extended filesystem. The filesystem must not be mounted write when this operation is performed. Can adjust maximum mount counts between filesystem checks, the time between filesystem checks, the amount of reserved blocks, and other parameters. umount(8) - Unmount a filesystem.

6.25 CHECK YOUR PROGRESS


I. Exercises 1. Inodes and Linking.
(a) Use stat to find out the number of hard links to your / directory. (b) Use the following to locate the files with inode numbers 1 and 3: $ find / -inum inode-number (c) Create a file called test in your home directory (Typing echo > test should do this). Now create a hard link to test called h_test and a symbolic link to test called s_test (d) Find out the inode number of the files. Check you understand why they are what they are. (e) Remove the original file called test. Can you still get at the contents of the original file?

PDF created with pdfFactory Pro trial version www.pdffactory.com

126

Chapter 6 - File System Administration

(f) What happens if you try cat s_ test. Make sure you understand the destinction between h_test and s-_ test. (g) Try to make a hard link to your home directory. Why does this fail? 2. Creating Filesystems and Formatting. (a) Identify and use the command string needed to create the following on a floppy disk: i. An Ext2 filesystem ii. A DOS filesystem iii. An IS09660 CD-ROM filesystem (b) To test your DOS filesystem, cp a long filename file to it. What happens to the filename? (c) Mount the ISO9660 formatted disk under /mnt/floppy to prove it is readable. (d) Identify and use the command string needed to do the following to a floppy disk: i. Low-level format the disk. ii. Add a DOS fs to a low-level formatted disk. (e) Try using tar to write to a floppy disk without a filesystem on it (you may have to low-level format the disk to remove an existing fs). 3. Checking and Repairing Filesystems. (a) Find the appropriate man pages and, hence commands to: i. Locate bad blocks on an Ext2 device. ii. Find and mark the bad blocks on DOS/FAT floppies. (b) Use e2fsck to force checking and automatically repair an Ext2 formatted floppy. 4. Creating, Modifying and Deleting Partitions.

It is not wise to practice disk partitioning on important filesystems. It can be done, but mistakes may be unrecoverable. So we ask you to do the following partitioning questions on a floppy. Most people wont be able to make mountable filesystems on a floppy (you need to make a block device file first), but you should get some safe practice on fdisk. You may only be able to do these floppy-based questions using recent versions of fdisk. (a) (b) Delete all existing partitions from your floppy. Create 2 new primary partitions: i. 1 with an Ext2 system ID ii. 1 with a Win95 FAT32 system ID.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

127

(c) (d) (e)

Save the new partition table and exit fdisk. Re-enter fdisk and check the floppys partition table contains the correct partitions. Delete the FAT32 partition and replace it with two FAT32 logical drives.

II. Choose the correct answer: 1. Every file / directory is specified by an _____.
a) Link b) inode c) name d) all of these. 2. ln creates links to ___________. a) files b) system programs c) Application programs d) all of these. 3. ln -s creates _____ links. a) Kernel b) shell c) symbolic or soft d) all of these. 4. Ext2 is _________________________. a) Network File System b) Native File System c) NetBios System d) None of the above 5. ____ checks and repairs Linux filesystems. a) fips b) diskdruid c) fsck d) All of the above

III. Say True or False


1. 2. 3. 4. e2fsck is the application called by fsck to check ext2 filesystems. df shows free space on filesystems. Native Linux filesystems are made using mke2fs. character devices are disks and tapes True/False True/False True/False True/False

PDF created with pdfFactory Pro trial version www.pdffactory.com

128
IV. Essay Type Questions
1) 2) 3) 4) 5) 6) 7) 8)

Chapter 6 - File System Administration

What are block, character and raw devices? Write brief information. Explain with options e2fsck. Explain the changes in filesystem when du and df commands are executed. Explain how the recovery of Linux filesystems are made. Describe how the Native Linux filesystems are made? Give the types. Desctibe the inode table entries in detail. Elucidate common disk partitioning tools. Mention all fdisk interactive commands.

V. Further readings and other activities 1. Get more information using man or info or any help command for
e2fsck, du, df, tty, passwd, group, ln EX: 1. $ man du 2. $ apropos bash Check the amount of space used, amount of space free for different drives and different directories. 2. For further readings you can refer the following books Title: Linux internals Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.geocities.com/raomvp/doc/linux/linuxbook.html A complete reference for you to get more information. You can also extract solutions for exercises. Title: principles of unix programming Auihors: Sumitabha Das

VI. Solutions I. Exercises 1. Inodes and Linking.


(a) The answer depends on your system. In recent Red Hat distributions / is inode number 2.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

129

(b) The answer depends on your system. In recent Red Hat distributions inode number 1 is / proc and number 3 is /proc/uptime. (c) This is achieved as follows: $ ln test h_test $ ln s test s_test (d) ls -li should show that the inode number of the original file and h_test are identical h_test is another name for the original file. The inode number for s_test will be different. It is a seperate file that contains information about the location of the file it is a link to. (e) After you created the hard link the original file had two names test and h_test. You have removed test but until all names for a file have been removed it is still accessible. In this case you can do cat h_test to see the contents of the file. (f) This should fail with a no such file or directory message. s_test contained a pointer to the file test not the inode number. There is no longer a file named test so this cannot work. Hard links reference a file by its inode number, symbolic links reference it by its name. (g) This is not allowed, as it could stop the filesystem being strictly hierarchical. 2. Creating Filesystems and Formatting. (a) These are typical solutions, but there are others: i. $ mke2fs /dev/fd0 ii. $ mkdosfs /dev/fd0 iii. $ mkdosfs /dev/fd0 (b) The filename should be shortened to 8 chars, without warning . . . could be tricky! (c) Yes, you can mount a floppy drive on Linux with a CD-ROM format. Indeed, you can actually mount an ISO9660 formatted disk image (i.e. a single file) of an entire Linux directory tree. Trust me, it isnt as daft as it sounds! (d) Something like this: i. $ fdformat /dev/fd0h1440 ii. Add a DOS fs to a low-level formatted disk. (e) Use a command something like this: $ tar c filenames > /dev/fd0

PDF created with pdfFactory Pro trial version www.pdffactory.com

130
3. Checking and Repairing Filesystems (a) apropos should yield: i. badblocks (8). ii. mbadblocks (1). (b) e2fsck p f /dev/fd0

Chapter 6 - File System Administration

Assuming that your floppy is actually OK, you will hardly get any feedback on stdout. 4. Creating, Modifying and Deleting Partitions. You probably cant do these floppy-based questions on old versions of fdisk. (a) Use $ fdisk /dev/fd0 followed by the d interactive command. (b) Use the n interactive command twice, followed by: i. command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 83 ii. command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): b (c) Use the w interactive command followed by the q interactive command. (d) $ fdisk /dev/fd0 followed by the p interactive command (e) Your screen should look something like this:

command (m for help): d partition number (1-4): 2 command (m for help): n

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

131

command action e extended p primary partition (1-4) e command (m for help): p Disk /dev/fd0: 2 heads. 18 sectors, 80 cylinders units = cylinders of 36 * 512 bytes

Using default value 41 Last cylinder or +size or +sizeM or +sizeK (41-80, default 80): 60 command (m for help): n command action l logical (5 or over) p primary partition (1-4) l

command (m for help): p Disk /dev/fd0: 2 heads. 18 sectors, 80 cylinders units = cylinders of 36 * 512 bytes

PDF created with pdfFactory Pro trial version www.pdffactory.com

132

Chapter 6 - File System Administration

command (m for help): t partition number (1-6): 5 Hex code (type L to list codes): b Changed system type of partition 5 to b (Win95 FAT32) command (m for help): t partition number (1-6): 6 Hex code (type L to list codes): b Changed system type of partition 6 to b (Win95 FAT32) command (m for help): p Disk /dev/fd0: 2 heads. 18 sectors, 80 cylinders units = cylinders of 36 * 512 bytes

II. Choose the correct answer: 1. b 2. a 3. c 4. b III. Say True or False 1. True 2. True 3. True

5. d

4. False

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 7

System Administration

7.0 INTRODUCTION AND OBJECTIVES

o far in this book, youve seen how to use Linux for many different tasks. However, there are some issues we havent dealt with because they are used rarely or only by a single administrator (who may be the only user).

This chapter covers the most important things that you need to know about system administration under Linux in sufficient detail to start using the system comfortably. In order to keep the chapter manageable, it covers just the basics and omits many important details. The general actions a system administrator must perform to keep filesystems performing smoothly are the following:v v v v v v v Check filesystems for corrupt sectors Check filesystems for integrity and correct i-node tables Check file permissions and ownerships to ensure proper access Make filesystems (local and remote) available to users as necessary Check the Linux systems disk space Management of users with respect to security and data Perform regular backups for data security

MSIT 3A Linux Internals

133

PDF created with pdfFactory Pro trial version www.pdffactory.com

134 Objectives

Chapter 7 - System Administration

At the end of this unit, You would be able to know and expertise the following concepts of system administration. The superuser account User accounts /etc/passwd Groups Managing Linux users User reference files User modifications Examples of making changes to permissions Some administration commands

7.1 THE SUPERUSER ACCOUNT


When the Linux software is installed, one master login is created automatically. This login, called root, is known as the superuser because there is nothing the login cant access or do. While most user accounts on a Linux system are set to prevent the user from accidentally destroying all the system files, for example, the root login can blow away the entire Linux operating system with one simple command. Essentially, the root login has no limitations.

7.2 USER ACCOUNTS /ETC/PASSWD


All the information about user accounts is kept in the file /etc/passwd. The /etc/passwd file should be owned only by root and have the group ID set to zero (usually root or system group, as defined in the /etc/group file). The permissions of the /etc/passwd file should be set to allow write access only by root, but all others can have read access. (We deal with groups and permissions later in this section.) The lines in the /etc/passwd file are divided into a strict format: v username:password:userID:groupID:comment:home directory:login command This format can best be seen by looking at a sample /etc/passwd file. The /etc/passwd file created when a Linux system is newly installed is shown below.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

135

$ vi /etc/passwd root::0:0:root:/root:/bin/bash
bin:*:1:1:bin:/bin: daemon:*:2:2:daemon:/sbin: adm:*:3:4:adm:/var/adm: lp:*:4:7:lp:/var/spool/lpd: sync:*:5:0:sync:/sbin:/bin/sync shutdown:*:6:0:shutdown:/sbin:/sbin/shutdown halt:*:7:0:halt:/sbin:/sbin/halt mail:*:8:12:mail:/var/spool/mail: news:*:9:13:news:/usr/lib/news: uucp:*:10:14:uucp:/var/spool/uucppublic: operator:*:11:0:operator:/root:/bin/bash nobody:*:-1:100:nobody:/dev/null: Each line in the /etc/passwd file is composed of seven fields, separated by a full colon. If there is nothing to be entered in a field, the field is left blank, but the colons are retained to make sure each line has seven fields (which also means each line will have six colons). The seven fields (from left to right on each line) are: A unique identifier for the user. The users password (encrypted). A unique number that identifies the user to the operating system. A unique number that identifies the users group (for file permissions). Usually the users real name, but sometimes phone numbers, departments, and so on. The directory in which users are placed when they log in. The command executed when the user logs in, normally a shell.

user name password user ID (UID) group ID (GID) comment home directory login command

7.3 GROUPS
Group information is maintained in the file /etc/group, which is similar in layout to the /etc/ passwd file. The default /etc/group file from a newly installed Linux system is shown below.

PDF created with pdfFactory Pro trial version www.pdffactory.com

136
$vi /etc/group root::0:root bin::1:root,bin,daemon daemon::2:root,bin,daemon sys::3:root,bin,adm adm::4:root,adm,daemon tty::5: disk::6:root,adm lp::7:lp mem::8: kmem::9: wheel::10:root floppy::11:root mail::12:mail news::13:news uucp::14:uucp man::15:man users::100:games nogroup::-1:

Chapter 7 - System Administration

Each line in the file has four fields separated by colons. Two colons together mean that the field is empty and has no value specified. Each line in the file follows this format: v group name:group password:group ID:users

Each group has a line of its own in the file. The fields in the /etc/group file (from left to right) are listed as follows: v group name- A unique name usually of eight characters or less (usually standard alphanumeric characters only). v password- Usually left as an asterisk or blank, but a password can be assigned that a user must enter to join the group. Not all versions of Linux or UNIX use this field, and it is left in the file for backward compatibility reasons. v group ID (GID)- A unique number for each group, used by the operating system. v users- A list of all user IDs that belong to that group.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

137

Every Linux system has a number of default groups which belong to the operating system, usually called bin, mail, uucp, sys, and so on. You can see the system-dependent groups in the default /etc/group file as shown by vi editor. In that file, all but the last two entries are system groups. You should never allow users to belong to one of these groups because it gives them access permissions that can be the same as roots. Only system logins should have access to these operating system groups.

7.4 MANAGING LINUX USERS


Linux Commands for managing users are: v adduser(8) - Command used to add user accounts. v chage (1) - Used to change the time the users password will expire. v chfn(1) - Change a users finger information vchsh(1) - Change a users shell. vchgrp (1) - Changes the group ownership of files. vchown (1) - Change the owner of file(s ) to another user. v gpasswd (1) - Used to administer the /etc/group file. v groupadd (8) - Create a new group. v grpconv (8) - Creates /etc/gshadow from the file /etc/group which converts to shadow passwords. v grpunconv (8)- Uses the files /etc/passwd and /etc/shadow to create /etc/passwd, then deletes /etc/shadow which converts from shadow passwords. v groupdel (8) - Delete a group v groupmod (8) - Modify a group v groups (1) - print the groups a user is in v grpck (8) - Verify the integrity of group files. v id(1) - Print group or user ID numbers for the specified user. v newgrp(1) - Allows a user to log in to a new group. v newusers (8) - Update and create new users in batch form. v nologin (5) - Prevent non-root users from logging onto the system.

PDF created with pdfFactory Pro trial version www.pdffactory.com

138

Chapter 7 - System Administration

v passwd (1) - Used to update a users password. The command passwd username will set the password for the given user. v pwconv (8) - Used to create the file /etc/shadow from the file /etc/passwd to convert to shadow passwords. v pwunconv (8) - Uses the files /etc/passwd and /etc/shadow to create /etc/passwd, then deletes /etc/shadow to convert from shadow passwords. v su (1) - run a shell with substitute user and group IDs v useradd (8) - Create a new user or update default new user information v userdel (8) - Delete a user account and their files from the system. The command userdel -r newuser will remove the user and deletes their home directory. v usermod (8) - Modify a user account. Other useful commands/examples find / -user username -ls chown -R myuser /home/myuser chmod +s filename Gives a list of all files owned by username. Changes ownership of all files in mysuer home directory to myuser. SETS THE UID

7.5 USER REFERENCE FILES


v v v v v /etc/passwd - Where the users name, and other pertinent information are stored. This includes the password unless your system is using shadow passwords. /etc/shadow - Where the users password is stored if you are using shadow passwords. /etc/group - Where group names are stored. /etc/aliases - Where the users name is matched to a nickname for e-mail. /etc/sudoers - A list of users with special privileges along with the commands they can execute.

7.6 USER MODIFICATIONS


To add a user, while logged in as root, type adduser username. To set a user password, while logged in as root or the user that will be changed, type passwd username. If passwd is typed, the password will be changed for the user, you are logged in as.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

139

To remove a user, while logged in as root, type userdel -r username. This will remove the users home directory. You can delete the user without the -r option and delete the users home directory manually. If the group the user was in, is no longer needed, you may delete it by editing the /etc/group file. The file /etc/aliases is where the users real name is matched to their user name for e-mail accounts. Add users to this file in the form: Firstname,Lastname:username

Then update the database by typing newaliases.

7.7 EXAMPLES OF MAKING CHANGES TO PERMISSIONS


chmod u+x myfile chmod +x myfile chmod ugo+x myfile chmod 400 myfile Gives the user execute permission on myfile. Gives everyone execute permission on myfile. Same as the above command, but specifically specifies user, group and other. Gives the user read permission, and removes all other permission. These permissions are specified in octal, the first char is for the user, second for the group and the third is for other. The high bit (4) is for read access, the middle bit (2) os for write access, and the low bit (1) is for execute access. Gives user full access, group read and write access, and other read access. Gives user full access, group read and execute permission, and other, execute permission. Set the setuid bit. Remove read and execute permissions for the group and other.

chmod 764 myfile chmod 751 myfile chmod +s myfile chmod go=rx myfile

Below are examples of making changes to owner and group: chown mark test1 chgrp mark test1 Changes the owner of the file test1 to the user Mark. Changes the file test1 to belong to the group mark.

PDF created with pdfFactory Pro trial version www.pdffactory.com

140

Chapter 7 - System Administration

7.8 SOME ADMINISTRATION COMMANDS


su (=substitute user id) Assume the superuser (=root) identity (you will be prompted for the password). Type exit to return you to your previous login. The root account is for administration and the su command is to ease your access to the administration account when you require it. setup

(as root) Configure mouse, soundcard, keyboard, X-windows, and system services. There are many distibution-specific configuration utilities, setup is the default on RedHat. Mandrake 7.0 offers very nice DrakConf. linuxconf

(as root, either in text mode or in the X terminal). You can access and change hundreds of networks setting from here. ReadHats network configuration utility netconf is a subset of linuxconf, therefore it is simpler and sometimes easier to use. mouseconf

(as root). Simple tool to configure your mouse (after the initial installation). Mandrake includes also an alternative mousedrake. kudzu

(as root). Automatically determines and configures your hardware. If having mysterious problems with your mouse (or other serial hardware), you may want to disable kudzu, so it does not run on the system startup. You can run it manually when you need it. timeconfig

(as root) Set the timezone for your system. It is important to keep the timezone correct. setclock

(as root). Set your computer hardware clock from the current linux system time. Use the command date first to set up the linux system time. E.g., We could change the date and time to 2005-12-31 23:57 using this command: date 123123572005. dateconfig&

(in X-terminal, as root else you will be asked for the root password). An excellent GUI utility to set our operating system and hardware clock and timezone, and tell our BIOS to keep time in UTC. We ignore the previous two commands.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

141

xvidtune

(in X-terminal). Adjust the settings for your monitor display for all resolutions so as to eliminate black bands, shift the display right/left/up/down, etc. (First use the knobs on your monitor to fit your text mode correctly on the screen). Then use xvidtune to adjust the monitor frequencies for each resolution so it fits well in your screen. To make the changes permanent, display the frequencies on the screen and then transfer them to the setup file /etc/X11/XF86Config. On newer monitors, you may really prefer to adjust your monitor using the built-in monitor settingsxvidtune is for older monitors which do not have the capability to remember their settings. SuperProbe

(as root). A utility to determine the type of the video card and the amount of its memory. lspci

Show info on your motherboard and what cards are inserted into the pci extension slots. Our older computer has ISA slots (or EISA) slots, no pci. lsdev

Display info about your hardware (DMA, IRQ, IO ports). chkconfig list | more

To check the current status of services, We may use: service status-all

To start a service right now, We may use something like (starts an proxy server): service squid start To re-start samba networking, We may use: service smb restart fsck -t ext2 /dev/hda2

(=file system check, as root) Check and repair a filesystem, e.g., after an unclean shutdown due to a power failure. The above example performs the check on the partition hda2, filesystem type ext2. You definitely want to unmount the partitions or boot Linux in the single mode to perform this (type linux single at the LILO prompt or use init 1 as root to enter the single user mode). If errors are found during the filesystem checkup, We accept the defaults for repair. The problems with unclean shutdown can be avoided if you use a journalizing file system (e.g., ext3) on your system (highly recommended). mkbootdisk device /dev/fd0 2.4.2-3

Make an emergency boot floppy. You are typically asked if you would like to make a boot disk during the system installation. The above command shows how to make it after install, on the first floppy drive (/ dev/fd0). Your kernel name (needed in the command, here 2.4.2-3) can be determined either by running uname -a or ls /lib/modules.

PDF created with pdfFactory Pro trial version www.pdffactory.com

142 7.9 SUMMARY

Chapter 7 - System Administration

All access to a Linux system is through a user account. Every user must be set up by the system administrator, with the sole exception of the root account (and some system accounts that users seldom, if ever, use). While many Linux systems only have one user, that user should not use the root account for dailyaccess. Most systems allow several users to gain access, either through multiple users on the main console, through a modem or network, or over hard-wired terminals. Knowing how to set up and manage user accounts and their associated directories and files is an important aspect of Linux system administration.

7.10 CHECK YOUR PROGRESS


I. Exercises Work on all the commands described in the unit and also login as root in your system and carry out the jobs of an System Administrator. II. Choose appropriate answer 1. For creating a new user account we require _______________.
a. b. c. d. 2. create the account provide an alias to their e-mail address both a & b none of the above

All Unix passwords, regardless of length (up to a maximum of 8 characters), are stored in the password file as encrypted strings of _________. a. b. c. 13 characters 16 characters 8 characters

3.

user account is removed using ________ command. a. b. rm userdelete

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

143

c. 4.

userdel

Unix systems keeps user account information, including one-way encrypted passwords, in a text file called ________. a. b. c. /etc/password /etc/passwd /etc/pwd

5.

The /etc/shadow file contents are read _____________. a. b. c. only by the current account only by the root account both a & b

6.

df command gives the information about ______ space. a. b. c. free space used space none of the above

7.

The / file-system, mounted on /dev/hda1, contains the_____. a. b. c. d. Linux kernel device drivers Other directories All of the above.

III. Say True or False 1. Passwd command is used for changing the password.
2. 3. 4. The root account has security restrictions. The username is at atleast 8 characters long. Change the password using su command.

True/False True/False True/False True/False

PDF created with pdfFactory Pro trial version www.pdffactory.com

144
IV. Essay type Questions 1. Explain the different steps of creating user account.
2. 3. Describe the total responsibility of a system administrator. Write a note on the following. i. ii. disabling user accounts removing user accounts

Chapter 7 - System Administration

iii. changing user passwords iv. starting & stopping processes 4. 5. 6. Elaborate the commands used for system administration. Explain the technique of managing /etc/passwd and /etc/group files. Explain the technique of managing and changing permissions using atleast two commands.

V. Further readings and other activities 1. Get more information using man or info or any help command for sh, bash, csh, ksh, inode, fs, env, set, mount, mknd, id, dev, stat.
2. For further readings you can refer the following books Title: Linux internals Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.geocities.com/raomvp/doc/linux/linuxbook.html A complete reference for you to get more information. You can also extract solutions for exercises. More information on filesystem can be obtained from text book title: Introduction to the Shell programming Author: Yashwanth Kanetkar

VI. Solutions I. Choose appropriate answer 1.c 2.a 3.c 4.b 5.b II. Say True or False 1.true 2.false 3.false 4.false
6.a 7.d

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 8

Editors and Tools

8.0 INTRODUCTION AND OBJECTIVES

here are many text editors available for the Linux system. Two of the most popular are vi (which is actually an alias to the elvis editor) and emacs. Both provide basic editing functions such as inserting and deleting text, reading and writing of external files, text searching, and copying and moving text. vi is a full-screen editor that has two modes: command mode and text mode. emacs is an extendible and powerful editor that is highly configurable to suit a variety of editing tasks (such as programming, document writing, and changing user or system files).

Objectives
At the end of this unit, You would be able to understand and expertise the following concets. Linux VI editor The emacs editor System and network configuration commands X Configuration utilities Library and kernel dependency management tools General Diagnostic function calls To remotely use Xwindows from Ms Windows Network application commands

MSIT 3A Linux Internals

145

PDF created with pdfFactory Pro trial version www.pdffactory.com

146 8.1 LINUX VI EDITOR


i a /word n w e b 3b dd 3dd D dw x o O CTRL-w u U . vi is a visual editor. It is also called as the king of all editors.

Chapter 8 - Editor and Tools

The following is a summary of the more essential commands of vi. You should consult the vi man page for more details on the many other vi commands. A SUMMARY OF COMMANDS insert mode, (ESC to exit insert mode) allows text to be entered on the screen Append to right mode Move to the occurrence of word Locate the next occurance Advance to the next word Advance to the next end of a word Move to the previous word Move backward 3 words delete line delete 3 lines Delete remainder of a line Delete word Delete character Open space for new line below the cursor line Open a line above the cursor Move back a word in append mode Undo last Undo all changes to current line In command mode, repeat the last text changing the command on the current line

:w newfilename save the file to newfilename from the command mode :wq :q! save and quit quit without saving

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

147

r J s cw c cc h H L M G 0 (zero)

replace then typa a character ot be replaced with r then return to break up a line join 2 lines substitute (sentence) typed text over a character, ESC when done change word change part of a line from the cursor to the end of the line substitute new text for aline, ESC when done move the cursor back one space move the cursor to the highest line on the screen move the cursor to the lowest line on the screen position the cursor at the midpoint on the screen last line in the file Move the cursor to the beginning of the line it is on

view filename Open a file for viewing only set number set nonumber Options: number autoindent (ai) showmatch tabstop=4 wrapmargine(wm) :2 copy 4 :1,4 :n when line is wrapped (80-value) Place a copy of line 2 after line 4. copy 7 (lines 1-4) Access the next file for editing ctrl-d to move to the left match brackets and parenthesis Turn on line numbers Turn off line numbers

PDF created with pdfFactory Pro trial version www.pdffactory.com

148 8.2 THE EMACS EDITOR

Chapter 8 - Editor and Tools

emacs has become the editor of choice for many users because of its online help facility and its extensive collection of editing commands. emacs is advanced and sophisticated text editor. For programmers, emacs is especially attractive because it can be configured to format source code for a variety of languages such as C, C++, and Lisp. emacs is somewhat easier to learn than vi, but it also features a much larger set of commands. There are likely 3 versions of emacs installed on your system: (1) text-only: type emacs in a text (not X-windows) terminal (2) graphical-mode: type emacs in an X-windows terminal and (3) X-windows mode: type xemacs in an X-windows terminal. STARTING EMACS emacs is invoked from the command line by entering emacs. To start emacs with a file to be edited, enter emacs filename. If you start emacs with a file, the screen will display the contents starting from the first line. Note the two lines at the bottom of the screen. The first of these lines, known as the mode line, displays the name of the file being edited and which part of the file that you are looking at (for example, TOP, 20%, BOT). The last line on the screen is the echo line, which emacs uses to display system messages and as a prompt for more input. A SUMMARY OF COMMANDS The following table is a summary of the strictly essential commands that you will need for basic editing in emacs. The emacs man page should be consulted for a more comprehensive description of the full emacs command set. Files are loaded into the following types of buffers: Fundamental - For text files C mode Shell mode

Ctrl v Alt v Ctrl a Ctrl e Ctrl h

move forward a page move back a page move cursor to beginning of the current line move cursor to the end of the current line Get a list of help options, Ctrl h again describes what they are. Ctrl h, then t tutorial, i -info reader

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

149

Ctrl a,Ctrl space Set the mark Ctrl w Ctrl y Alt w Ctrl k Alt y Ctrl x x a Ctrl s Ctrl r Alt Ctrl s Alt % Alt x string Deletes the text in the region (between the mark and the cursor) Paste the text in the buffer at the current cursor location Copy the text in the region kill text Replace yanked text with the previous block from the kill ring.(the ring of buffers where deleted and copied text is stored) Copy the region into register a Search for text Search backward Search for something like jo.*n matching john, joan, etc replace a string, space to replace, delete to skip, period to stop. replace all the string without prompts

Ctrl x - make the following commands available (also allows macros): (Ctrl x e - to perform the macro) Exit the editor Save the current file Find another file to edit Switch to another buffer Present a list of available buffers Switch between two windows Moves back one character. Deletes the current character. Moves forward one character. Cancels the current command. Enters emacs online help. Moves forward to the next line.

Ctrx x ...commands... Ctrlx Ctrl c Ctrl s Ctrl f b Ctrl b o C-b C-d C-f C-g C-h C-n

PDF created with pdfFactory Pro trial version www.pdffactory.com

150
C-p C-s C-v M-v C-x u C-x C-c C-x C-s Moves back to the previous line. Searches forward for a string. Scrolls forward one screen. Scrolls backward one screen. Undoes the last edit. Exits emacs. Saves the buffer to a file.

Chapter 8 - Editor and Tools

8.3 SYSTEM AND NETWORK CONFIGURATION


v v v v v v v v v linuxconf - A GUI interactive interface available on Redhat 6.0 or later which includes netconf configuration. netconf - A GUI interactive interface available on Redhat 6.0 and later. kbdconf - A Redhat Linux tool which configures the /etc/sysconfig/keyboard file which specifies the location of the keyboard map file. This is a GUI based tool. mouseconfig - A Redhat Linux tool used to configure the /etc/sysconfig.mouse file. This is a GUI tool. timeconfig - A Redhat Linux tool used to configure the /etc/sysconfig/clock file. This is a GUI tool used to set timezone and whether or not the clock is set to GMT time. kernelcfg - A Redhat kernel configuration utility to be started from X. stty - Used to configure and print the console devices. setterm - Set terminal attributes. vmstat - Report statistics on virtual memory.

8.4 X CONFIGURATION
v v XF86Setup - A newer X configuration program with a GUI interface which modifies the / etc/X11/XF86Config configuration file. xf86config - An older X configuration program with a text based interface. It also modifies the /etc/X11/XF86Config configuration file.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

151

v v v

Xconfigurator - The Redhat tool used during system setup to configure X. SuperProbe - A program that probes the video card to determine its type for use with setting up X. xvidtune - This program will test video modes on the fly without modification to your X configuration. Read the usr/X11R6/lib/X11/doc/VideoModes.doc file before running this program.

8.5 LIBRARY AND KERNEL DEPENDENCY MANAGEMENT


Library management: v ldd - Used to determine shared libraries used by binary files. Type ldd /bin/ls to see the shared libraries used by the ls command. v ldconfig - Used to update links and cache for system use of the most recent runtime shared libraries. Kernel Management: v lsmod - List currently installed kernel modules. v depmod - Creates a dependency file, modules.dep in the directory /lib/modules/x.x.x, later used by modprobe to automatically load the relevant modules. v insmod - Installs a loadable kernel module into the running kernel. v rmmod - Unloads modules, Ex: rmmod ftape v modprobe - Used to load a module or set of modules. Loads all modules specified in the file modules.dep.

8.6 GENERAL DIAGNOSTIC


System resources v free - Show system memory availability and usage v df - Show the amount of disk free space on each mounted filesystem. v du - Show disk usage v lspci - List PCI devices v pnpdump - Lists ISA PNP device resource information. v vmstat - Reports virtual memory statistics.

PDF created with pdfFactory Pro trial version www.pdffactory.com

152
Other: v env - List the current environment variables. v printenv - Print a copy of the environment.

Chapter 8 - Editor and Tools

v set - Shows how the environment is set up. This command can be very useful when debugging the environment. v runlevel - List the current and previous runlevel. v uname - Print system information. In my case, it prints Linux. v dmesg - Show the last kernel messages printed during the last boot.

8.7 TO REMOTELY USE XWINDOWS FROM MS WINDOWS


This section describes how to set this up for the MI/X server program to run on windows. We have not been able to get this server to run with KDE or gnome, and many graphic images do not seem to work with it. See the section on using X remotely for a better solution. This section is still here for anyone who wants to experiment with it. v Get a copy of an X windows server program for windows such as MI/X for windows from MicroImages at http://www.microimages.com. v Install the package according to instructions on your windows box. v Set up a shortcut to MI/X to run XS.EXE on the windows box. v Invoke the shortcut to begin the Xwindows server program. v Telnet to your linux box from your windows box v Type fvwm display mark:0 & where mark is the name of your windows box Tips v To exit X windows quickly or if the video is working improperly, use the <CTRL><ALT><BACKSPACE> key combination. v To increase or decrease graphic mode use <CTRL><ALT><+> or <-> respectively. This will toggle through video modes on the modes line in the screen section of the XF86Config file. v The command X -showconfig lists the video chip names known to the X server.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

153

v The command X -probeonly > /tmp/test.out 2>&1 lists supported chipsets and other useful information. v The command X > /tmp/test.out 2>&1 runs X bare. Use Ctrl-Alt-BS key combination, then examine the contents of the output file to help debug X startup problems.

8.8 NETWORK APPLICATION COMMANDS


lynx file.html View an html file or browse the net from the text mode. Although lynxs look or convenience of use is not as great as GUI-based browser, it is light-weight, almost always works, and does not require any configuration, as long as your networks is functional. konqueror &

(in X terminal) File manager and web browser in one. Very nice, in many very comptetitive to mozilla. Comes with KDE. pine

A good, old-fashioned, text-mode mail reader. Another old-fashioned and standard one is elm. Your mozilla mail will read the mail from your Internet account. pine will let you read the local mail, e.g. the mail your son or a cron process sends to you from a computer on your home network. The command mail could also be used for reading/composing mail, but it would be inconvenientit is meant to be used in scripts for automation. mutt

A really basic but extremally useful and fast mail reader. mail

A basic operating system tool for e-mail. Look at the previous commands for a better e-mail reader. mail is good if you wanted to send an e-mail from a shell script. kmail &

(in X-terminal) Nice, GUI mail program. We use kmail, it is much better than netscape mail. We can have multiple accounts and retrieve mail from the smtp (local) server and pop3 servers (internet service provider) to the same mailbox. Simple and elegant. Supports digital signatures. licq &

(in X terminal) An icq instant messaging client. Another good one is kxicq. Older distributions dont have an icq client installed, you may have to do download one and install it.

PDF created with pdfFactory Pro trial version www.pdffactory.com

154
talk username1

Chapter 8 - Editor and Tools

Talk to another user currently logged on your machine (or use talkusername1@machinename to talk to a user on a different computer) . To accept the invitation to the conversation, type the command talk username2. If somebody is trying to talk to you and it disrupts your work, your may use the command mesg n to refuse accepting messages. You may want to use who or rwho to determine the users who are currently logged-in. talk is one of the old-fashioned standard UNIX tools, yet it still can be cool and useful in some situations. telnet server

Connect to another machine using the TELNET protocol. Use a remote machine name or IP address. You will be prompted for your login name and passwordyou must have an account on the remote machine to login. Telnet will connect you to another machine and let you operate on it as if you were sitting at its keyboard (almost). Telnet is not very secureeverything you type moves through the networks in open text, even your password! A competent system administrator on a computer on-route can read what you type. Use ssh (requires some setup) for encrypted transmission. ftp server Ftp another machine. (There is also ncftp which adds extra features and gftp for GUI .) Ftp is good for copying files to/from a remote machine. Try user anonymous if you dont have an account on the remote server. After connection, use ? to see the list of available ftp commands. The essential ftp commands are: ls (see the files on the remote system), ASCII,binary (set the file transfer mode to either text or binary, important that you select the proper one ), get (copy a file from the remote system to the local system), mget (get many files at once), put (copy a file from the local system to the remote system), mput (put many files at once), bye (disconnect). For automation in a script, you may want to use ncftpput and ncftpget, for example:

8.9 SUMMARY
There are many other editors also present. pico filename Edit a text file using the simple and standard text editor called pico. Use <Ctrl>x to exit. There are many text editors for Linux, including several GUI-based. A brand new clone of pico (GPLed) is nano. kwrite (in X terminal) Very nice, advanced text editor. Supports vertical text selection!

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

155

kate, kedit, gedit (in X terminal). Simple yet nice text editors (GUI based).

gxedit (in X terminal) Another multi-purpose, feature packed text editor. This one even has timed backup.

latte (in X terminal) Code editor, i.e., plain text editor meant for writing programs.

nedit (in X terminal) Another programmer editor. Very nice and loaded.

bluefish (in X terminal) html editor (source with syntax highlighting and many tools and options).

8.10 CHECK YOUR PROGRESS


I. Exercises Work on all the commands described in the unit and also login as root in your system and carry out the jobs of an Network Administrator and have a hands-on experience with all editors. II. Choose the correct answer 1. Linux uses _______ slash.
a. b. c. d. 2. Normal Back Double None of the above

The following are the Linux commands _____ a. b. c. d. ATTRIB BACKUP RESTORE None of the above

PDF created with pdfFactory Pro trial version www.pdffactory.com

156
3. The equivalent for COPY in Linux is _____ a. b. c. d. mv & rm cp both a & b none of the above.

Chapter 8 - Editor and Tools

4. The example if an editor is _____. a. b. c. d. vi emacs ex & ed all of the above.

II. Say True or False 1. In Linux operating system filenames are limited to 8 characters. True/False
2. 3. 4. Linux operating system is case sensitive. ifconfig is used for netwoks ip address setting. free - Show system memory availability and usage True/False True/False True/False

III. Essay type questions 1. List out the comparisons between DOS-to-LINUX.
2. 3. 4. 5. 6. Explian different modes of vi-editor and describe emacs editor. Describe the network configuration commands. Explain the X Configuration utilities Explain the Network application commands Describe how to remotely use Xwindows from Ms Windows.

IV. Further readings and other activities 1. Get more information using help commands aboutVi, ed, red, gEdit, vim, Emacs, ex, sed, awk, mtools

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

157

2.

Install from Linux source cd, The dos emulation package by name DOSEMU and work on all dos emulation commands.

3. For further readings you can refer the following books Title: Linux internals Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.geocities.com/raomvp/doc/linux/linuxbook.html A complete reference for you to get more information. You can also extract solutions for exercises. Title: Power Tools, second edition, Author: Jerry Peek, Tim OReilly, Mike Loukides, and others; OReilly & Associates; ISBN 156592-260-3; 1997. A huge collection of tips, techniques, and concepts for making intermediate users into advanced users. Title: Learning the vi Editor Author: Linda Lamb; OReilly & Associates; ISBN 0-937175-67-6; 1992. A complete introduction to vi, structured like LINUX in a Nutshell.

VI. Solutions I. Choose the correct answer 1.a 2.d 3.a 4.d II. Say True or False 1.False 2.True 3.True 4.True

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 9

Installation of Linux

9.0 INTRODUCTION AND OBJECTIVES


e have prepared and structured this chapter in a manner that follows the original installation of the Red Hat Linux operating system from CD-ROM. Each section below refers to, and will guide you through, the different screens that appear during the setup of your system after booting from the Red Hat boot diskette. We promise that it will be interesting to have the machine you want to install Linux on ready and near you when you follow the steps described below. You will see that through the beginning of the installation of Linux, there are many options, parameters, and hacks that you can set before the system logs in for the first time. Unlike some versions of Linux, Red Hat gives you a number of methods of installing its operating system that offer versatility and ease of use. For most people, installation will be a very simple task with most of the process automated by the Red Hat installation routines. Users with some special hardware or disk-layout problems will have to perform a more typical manual installation of Linux. This chapter covers all the steps you need to install Linux manually, as well as the brief instructions you need to allow an automated installation. If you want to try an automated installation of Red Hat Linux, there are two ways to go. One is called floppyless, as it uses a routine on the CD-ROM to install itself. This step simply saves you the trouble of making floppy diskettes; the rest of the installation process is exactly the same. The alternative to floppyless installation is to create boot and root floppies and use those to start up the system and access the CD-ROM. The following sections discuss both floppyless and floppy-based methods in more detail.

158

Chapter 9 - Installation of Linux

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

159

OBJECTIVES
At the end of this unit, You would be able to install and configure the Linux operating system and know and expertise the following concepts of system installation. Devices of installation Partitioning hard disk Partition sizes Mount points Mounting partitions at startup The fstab file Know your hardware Creating the Linux boot disk Making a bootable diskette under MS-DOS Making a diskette under a Linux-Like OS Beginning the installation of Linux Installation options - class and method Manual configurations Package installations Standard linux partition filesystem types Network administration tools

9.1 DEVICES OF INSTALLATION


The first partition on a IDE hard drive is called partition 1, and is called /dev/hda1 if the drive is the primary IDE master. /dev/fd0 /dev/hda1 /dev/hdb3 /dev/sda1 First Floppy disk [fd1 = Second] IDE Hard drive 1, partition 1 IDE Hard drive 2, partition 3 First SCSI interface (fast hard drive), device id 1

PDF created with pdfFactory Pro trial version www.pdffactory.com

160
/dev/sdc3 First SCSI interface, device id 3

Chapter 9 - Installation of Linux

/dev/cdrom CD ROM drive /dev/mouse Mouse device, sometimes a pointer to another device such as /dev/psaux, a ps/2 mouse driver. primary primary secondary secondary IDE master IDE slave IDE master IDE slave /dev/had /dev/hdb /dev/hdc /dev/hdd

9.2 PARTITIONING HARD DISK


This is the most important work for installation of Linux. Many distributions use disk, cfdisk, or disk druid to set up partitions for Linux. You will need a Linux swap partition and at least 1 Linux native partition. You can install Linux with other operating systems such as DOS, windows95, 98, and/or NT/Win2000. If you will install with NT or Windows 2000, you must use extra care since NTs boot loader may conflict with LILO, the boot loader used by Linux. We have installed Linux with Windows98, and Windows 2000. What we recommend is setting your partitions in advance by using partition magic 4.0 [partitioning utility]. If you cant do it this way use fdisk, then you may have to format any non-Linux drives with the appropriate programs.

9.3 PARTITION SIZES


Swap partition The swap partition, we usually make 128Mb. The recommendation is that the swap partition be twice as large as RAM memory or greater. In the past Linux could not use swap partitions larger than 128MB but the size limitation depends on your systems architecture. On most Intel based systems, a swap partition of up to 2GB [2048MB] can be used. You can also make multiple swap partitions. Native Partition(s)

If you are setting up a computer you will be learning Linux on or installing several operating systems, you may want one Linux native partition [3 to 5 GB total] per Linux install. If, however, you are setting up a dedicated high performance server, you may want to set up several Linux native partitions and install various parts of the operating system on separate ones similar to the following recommendation or some modification thereof:

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

161

1. 2. 3.

/ - Root size of 200MB. /usr - Holds local software. Minimum of 700MB, Recommended 1.7 GB or more. /var - Holds mail, spool, and log files. Recommended size of 400MB

4. /home -The rest of the disk, Used for user storage and in a multi user system, is where most data will be stored. In systems that we will use at home or learn on, We normally use one linux native partition per Linux install. If we are installing Redhat and Slackware we might make two 3GB partitions. We like to make the size of our Linux native [ext2] partitions about 3GB depending on the amount of hard drive space we have and the number of systems we are installing. This is because Linux takes about 0.8G to 1.3G for a full install and some room is left over for expansion. If installing only one Linux system, we like to have 4-6GB. One thing we like to do when installing a system to be shared with Windows, which can make life convenient and save your data, is to make a large partition on a large hard drive that is FAT32. Since Linux can mount this and most if not all Windows operating systems can see this, your data can be available in a common filesystem on this partition.

9.4 MOUNT POINTS


A mount point is a directory which a device or partition is attached to. During your install you may need to choose mount points for specific filesystems. You will need to set the mountpoint of / for your root filesystem on the disk partition you are installing your system to. For example if you are installing your system on the second partition of an IDE drive, you will select the /dev/hda2 drive mount point to be /. If you want to mount other drives or partitions, you may, for example, mount your second hard drive or partition to a directory called /data. Once your install is complete you will need to be sure this mount point exists and possibly create it with the mkdir /data command. Then the next time you boot your system, this partition should be available for use (or you can use the command mount -a to mount all filesystems in the /etc/fstab file.

9.5 MOUNTING PARTITIONS AT STARTUP


To mount other partitions at startup modify the /etc/fstab file. Each line in the file refers to a different filesystem. Fields are separated by whitespace. The primary filesystems must be mounted first, so they must be in correct order. Your native partition for the system you are running should be listed first. The fields are as follows:

PDF created with pdfFactory Pro trial version www.pdffactory.com

162
The name of the device such as /dev/hda1

Chapter 9 - Installation of Linux

The mount point. Use / for root. Other typical mount points are /dos for DOS, swap or none for the swap partition, and /mnt/floppy for /dev/fd0 (the floppy drive). The type of filesystem. They are: mini, ext, ext2(linux native), xiafs, msdos, hpfs, ntfs, fat32, iso9660(CD-ROM), nfs, swap (for swap space). The mount options for use with the filesystem. Each filesystem type has different mount options. Read the mount man page to see possible options. ro= read only, user- allows normal users to mount the device. The frequency the filesystem needs to be dumped (backed up) by the dump command. For ext2, normally make it 1, for others make it 0. 0 or nothing means it is not dumped. If 1, it is backed up during a system backup. A number telling the order in which the filesystems should be checked at reboot time by the fsck program. Your root should be 1, others are in ascending order or 0 to not be checked. To determine your hard drives partitions and see what each partition holds which operating system, you may use the fdisk program. Use p option to see a list of current partitions, then you can add them to your fstab file.

9.6 THE FSTAB FILE


A typical /etc/fstab file: / /data /dos /slackw swap /mnt/cdrom /mnt/floppy /proc /dev/pts ext2 auto vfat ext2 swap iso9660 ext2 proc devpts defaults defaults defaults defaults defaults noauto,owner,ro noauto,owner defaults gid=5,mode=620 11 00 00 00 00 00 00 00 00 /dev/hda2 /dev/hdb1 /dev/hda1 /dev/hda3 /dev/hda4 /dev/cdrom /dev/fd0 none none

These are the options: v defaults - Use the default options of rw, suid, dev,exec,auto, nouser, and async. v auto - Auto detect the type of filesystem.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

163

v noauto - The -a option will not mount the filesystem. v owner v ro - Read only v rw - Read and write allowed v user - Users have permission to mount this filesystem v users - Allows a user to mount and another user to unmount the filesystem The /proc directory is required for tracking processes in memory (RAM). The directories /data, /dos, and /slackw in this example must exist or their mounts will fail.

9.7 KNOW YOUR HARDWARE


Understanding the hardware of your computer is essential for a successful installation of Linux. Therefore, you should take a moment and familiarize yourself with your computer hardware. Be prepared to answer the following questions: v How many hard drives do you have? v What size is each hard drive (eg, 15GB)? v If you have more than one hard drive, which is the primary one? v What kind of hard drive do you have (eg, IDE ATA/66, SCSI)? v How much RAM do you have (eg, 256MB RAM)? v Do you have a SCSI adapter? If so, who made it and what model is it? v Do you have a RAID system? If so, who made it and what model is it? v What type of mouse do you have (eg, PS/2, Microsoft, Logitech)? v How many buttons does your mouse have (2/3)? v If you have a serial mouse, what COM port is it connected to (eg, COM1)? v What is the make and model of your video card? How much video RAM do you have (eg, 8MB)? v What kind of monitor do you have (make and model)? v Will you be connected to a network? If so, what will be the details of following entities.

PDF created with pdfFactory Pro trial version www.pdffactory.com

164

Chapter 9 - Installation of Linux

* IP address * netmask * gateway address * domain name servers IP address * domain name * hostname * types of network(s) card(s) (makes and model) * number of card(s) (makes and model).

9.8 CREATING THE LINUX BOOT DISK


The first thing to do is to create an installation diskette, also known as a boot disk. If you have purchased the official Red Hat Linux CD-ROM, you will find a floppy disk named Boot Diskette in the Red Hat Linux box so you dont need to create it. Sometimes, you may find that the installation will fail using the standard diskette image that comes with the official Red Hat Linux CD-ROM. If this happens, a revised diskette is required in order for the installation to work properly. There are two methods to create the installation Boot Disk, the first method is to use an existing icrosoft Windows computer and the second using an existing Linux computer.

9.9 MAKING A DISKETTE UNDER MS-DOS


Before you make the boot disk, insert the Official Red Hat Linux CD-ROM Disk1 in your computer that runs the Windows operating system. When the program asks for the filename, enter boot.img for the boot disk. To make the floppies under MS-DOS, you need to use these commands (assuming your CD-ROM is drive D: and contain the Official Red Hat Linux CDROM). Open the Command Prompt under Windows: Start | Programs | Command Prompt C:\> d: D:\> cd \dosutils D:\DOSUTILS> RAWRITE Enter disk image source file name: ..\images\boot.img Enter target diskette drive: a: Please insert a formatted diskette into drive A: and press -ENTER- : D:\dosutils> The rawrite.exe program asks for the filename of the disk image: Enter boot.img and insert a blank floppy into drive A. It will then ask for a disk to write to: Enter a:, and when complete, label the disk Red Hat boot disk, for example.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

165

9.10 MAKING A DISKETTE UNDER A LINUX-LIKE OS


To make a diskette under Linux or any other variant of Linux-Like operating system, you must have permission to write to the device representing the floppy drive (known as /dev/fd0H1440 under Linux). This permission is granted when you log in the system as the super-user root. Once you have logged as root, insert a blank formatted diskette into the diskette drive of your computer without issuing a mount command on it. Now its time to mount the Red Hat Linux CD-ROM on Linux and change to the directory containing the desired image file to create the boot disk. Insert a blank formatted diskette into the diskette drive. Insert the Red Hat Linux CD Part 1 into the CD-ROM drive. [root@rao /]# mount /dev/cdrom /mnt/cdrom [root@rao /]# cd /mnt/cdrom/images/ [root@rao images]# dd if=boot.img of=/dev/fd0H1440 bs=1440k 1+0 records in 1+0 records out [root@rao images]# cd / [root@rao /]# umount /mnt/cdrom Dont forget to label the diskette Red Hat boot disk.

9.11 BEGINNING THE INSTALLATION OF LINUX


Now that we have made the boot disk, it is time to begin the installation of Linux. Since wed start the installation directly off the CD-ROM, boot with the boot disk. Insert the boot diskette you create into the drive A: on the computer where you want to install Linux and reboot the computer. At the boot: prompt, press Enter to continue booting and follow the three simple steps below: Step 1 The first step is to choose what language should be used during the installation process. In our example we choose the English language. Step 2 After that, the system allows you to choose your keyboard type, layout type for the keyboard, and the possibility to enable or disable Dead Keys.

PDF created with pdfFactory Pro trial version www.pdffactory.com

166
Step 3

Chapter 9 - Installation of Linux

Finally, we choose the kind of mouse type we use and if this mouse has two or three buttons. If you have a mouse with just two buttons, you can select the option named Emulate 3 Buttons and click both mouse buttons at the same time to act as the middle mouse button. Once we have completed the above three steps, we are ready to begin the installation of Red Hat Linux.

9.12 INSTALLATION OPTIONS - CLASS AND METHOD


Red Hat Linux includes four different classes, or type of installation. They are: v Workstation v Server System v Laptop v Custom System The first two classes (Workstation, and Server System) give you the option of simplifying the installation process with a significant loss of configuration flexibility that we dont want to lose.

9.13 MANUAL CONFIGURATIONS


Now that we know exactly what partitions we need to create for our new Linux machine, it is time to choose the partitioning software we will use to make these partitions on the machine. With Red Hat Linux two programs exist to assist you during this step. During setup, the installation will give you two choices, which are: Manually partition with Disk druid Manually partition with fdisk [experts preferred] Disk Druid is the new software used by default in Red Hat Linux to partition your disk drive, this is an easy to use program, which allows you to work through a graphical interface to create your partitions tables. fdisk was the first partitioning program available on Linux. It is more powerful then Disk Druid and allows you to create your partition table in exactly the way you want it (if you want to put your swap partition near the beginning of your drive, then you will need to use fdisk).

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

167

On the next screen you will see the LILO Configuration screen. LILO, the LInux LOader, is software that can be used to start Linux on your computer. From this screen, you will see different configurable options related to LILO. The first option is:- Create boot disk The Create boot disk option is checked by default. If you do not want to create a boot disk, you should deselect this option. Also, this option must be checked if you decide to not install LILO on the MBR (the Master Boot Record) or if you are not installing LILO at all.

The second option is:- Do not install LILO This option allows you to skip installing LILO if you use a boot disk rather than LILO to start your system.

The third option (the one that we will chose) installs LILO in your Linux system and gives you the choice to install LILO boot record on: Master Boot Record (MBR) First Sector of Boot Partition Usually, if Linux is the only Operating System on your machine (and this must be the case in a custum installation), you should choose the Master Boot Record (MBR) option.

Network Configuration After that, you need to configure your network. If you have multiple Ethernet devices, each device will have its own configuration screen.

Firewall Configuration The latest release of Red Hat Linux now offers the possibility to configure a Firewall during installation. This is OK for the average end user but NOT for serious Firewall security. From the next screen that appears, you will see three different security levels available, choose the No firewall option and click Next.

Language Support Selection From here the installation will ask you to choose the default language that will be used on your Linux system. Time Zone Selection On the next screen, you will have the opportunity to set your time zone. Once selected click Next.

PDF created with pdfFactory Pro trial version www.pdffactory.com

168
Account Configuration

Chapter 9 - Installation of Linux

After the clock has been configured, you need to give your system a root password account. Authentication Configuration Finally, the last stage is the authentication configuration. For Authentication Configuration dont forget to select: Enable MD5 passwords Enable Shadow passwords Enable MD5 passwords - allows a long password to be used (up to 256 characters), instead of the Unix standard eight letters or less. Enable shadow passwords - provides a very secure method of retaining passwords for you. All passwords are stored in a file named shadow, which is readable only by the super-user root. Enable NIS, LDAP, and Kerberos doesnt need to be selected. Selecting Package Groups After your partitions have been configured and selected for formatting and configurations have been set for your specific system, you are ready to select packages for installation. By default, continue by pressing next key till you get message linux is installed successfully.

9.14 PACKAGE INSTALLATIONS


tar.gz SOURCE PACKAGE GENERAL INSTRUCTIONS 1 2 3 Read the install documentation associated with the installation Put the packed file in the directory you want it to be installed under. Examine the package with the command tar tvzpf <packagename>.tar.gz to see where it will install. 4 Unpack the tar.gz file in one step with tar xvzpf <packagename>.tar.gz or in two steps with gunzip <packagename>.tar.gz and tar xpf <packagename>.tar It will create a directory like

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

169

packagename-2.3.4 and place the extracted information here. 5 6 7 Typically the following outlines the rest of the steps but they will vary from package to package. Go into the created subdirectory and type ./configure. Type make all or ./make Type make install or ./make install

Installing Redhat Packages The RedHat package manager installation utility is called rpm. rpm is supposed to be an intelligent software package manager. To get help Queries the database of installed packages to see if this package is installed or what version of it is installed. Installs mypack. Upgrades mypack. To query with details about the package. Gives a list of files this package installs. Removes the package mypack-1.2.2.

rpm help rpm -q mypack-1.0.1.rpm rpm ivh mypack-1.0-1.rpm rpm uvh mypack-2.0-1.rpm rpm qip mypack-1.2.2.rpm rpm qlp mypack-1.2.2.rpm rpm -e mypack-1.2.2

9.15 STANDARD LINUX PARTITION FILESYSTEM TYPES


As a quick reference, here is a brief summary of the standard linux partition types (filesystems) with a short description. [We can have a look at the source code files at /usr/src/linux/fs]. minix

It is the filesystem used in the Minix operating system, the first to run under Linux. It has a number of shortcomings: a 64MB partition size limit, short filenames, a single time stamp, etc. It remains useful for floppies and RAM disks. ext

It is an elaborate extension of the minix filesystem. It has been completely superseded by the second version of the extended filesystem (ext2) and will eventually be removed from the kernel. ext2

It is the high performance disk filesystem used by Linux for fixed disks as well as removable media. The second extended filesystem was designed as an extension of the extended file system (ext). ext2

PDF created with pdfFactory Pro trial version www.pdffactory.com

170

Chapter 9 - Installation of Linux

offers the best performance (in terms of speed and CPU usage) of the filesystems supported under Linux. In short, ext2 is the main (default, typical) Linux filesystem. ext3

It is an extension of the ext2 filesystem with journalizing. It is backward and forward compatible with ext2. It means that ext2 can be converted into ext3 without reformatting or data loss (just re-mounting the partition is required). ext3 can be changed back to ext2, also without data loss. It is highly recommended that you use this filesystem. xiafs

It was designed and implemented to be a stable, safe filesystem by extending the Minix filesystem code. It provides the basic most requested features without undue complexity. The xia filesystem is no longer actively developed or maintained. It is used infrequently. msdos

It is the filesystem used by DOS, Windows, and some OS/2 computers. msdos filenames can be no longer than 8 characters followed by an optional period and 3 character extension. umsdos

It is an extended DOS filesystem used by Linux. It adds capability for long filenames, UID/GID, POSIX permissions, and special files (devices, named pipes, etc.) under the DOS filesystem, without sacrificing compatibility with DOS. vfat

It is an extended DOS filesystem used by Microsoft Windows95 and Windows NT. VFAT adds capability for long filenames under the MSDOS filesystem. proc

It is a pseudo-filesystem which is used as an interface to kernel data structures rather than reading and interpreting /dev/kmem. In particular, its files do not take up disk space. See $ man 5 proc. iso9660

It is a CD-ROM filesystem type conforming to the ISO 9660 standard. Two extensions (listed below) are automatically supported. v High Sierra Linux supports High Sierra, the precursor to the ISO 9660 standard for CDROM filesystems. It is automatically recognized within the iso9660 filesystem support under Linux. v Rock Ridge Linux also supports the System Use Sharing Protocol records specified by the Rock Ridge Interchange Protocol. They are used to further describe the files in the

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

171

iso9660 filesystem to a UNIX host, and provide information such as long filenames, UID/ GID, POSIX permissions, and devices. It is automatically recognized within the iso9660 filesystem support under Linux. hpfs

It is the High Performance filesystem, used in OS/2. This filesystem is read-only under Linux due to the lack of available documentation. sysv

It is an implementation of the System V / Coherent filesystem for Linux. It implements all of Xenix FS, System V / 386 FS, and Coherent FS. nfs

It is the network filesystem used to access disks located on remote computers. smb

It is a network filesystem that supports the SMB protocol, used by MS Windows for Workgroups, Windows NT, and Lan Manager. To use smb fs, you need a special mount program. Standard linux command smbmount will do. ncpfs

It is a network filesystem that supports the NCP protocol, used by Novell NetWare. devpts

It is a pseudo file system, traditionally mounted on /dev/pts. In order to acquire a pseudo terminal, a process opens /dev/ptmx; the number of the pseudo terminal is then made available to the process and the pseudo terminal slave can be accessed as /dev/pts/<number>. fat

It is not a separate filesystem, but a common part of the msdos, umsdos and vfat filesystems. UFS

It is a file system widely used in different operating systems. swap

It is a special partition type used for swapping data from memory to hard drive. raiserfs

It is a brand new journalizing filesystem available as standard with Linux kernel version 2.4.1 up.

PDF created with pdfFactory Pro trial version www.pdffactory.com

172
hfs

Chapter 9 - Installation of Linux

(=hierarchical files system)MacIntosh filesystem. It is a late beta version., i.e., not recommended for use with critical data, unless read-only. ntfs

MS Windows NT filesytem. It is still experimental under Linux, i.e. not recommended for production machines, unless read-only.

9.16 NETWORK ADMINISTRATION TOOLS


netconf (as root) A very good menu-driven setup for your network. ping machine_name

Check if you can contact another machine (give the machines name or IP), press <Ctrl>C when done (without <Ctrl>c, the command keeps going). As all Linux commands, ping has options, including the ping of death attack, when it seems you can ping some servers so they dietry the options -f and -s. host host_to_find, nslookup host_to_find, dig ip_to_find

(Three commands, use any.) Query your default domain name server (DNS) for an Internet name (or IP number) host_to_find. This way you can check if your DNS works. You can also find out the name of the host of which you only know the IP number. traceroute host_to_trace

Have a look how your messages trace to host_to_trace (which is either a host name or IP number). mtr host_to_trace

(as root) A powerful and nice tool that combines the functionality of the older ping and traceroute. firewall-config (as root, in Xterm). A GUI for building your custom firewall. ifconfig

(as root) Display info on the network interfaces currently active (ethernet, ppp, etc). Your first ethernet should show up as eth0, second as eth1, etc, first ppp over modem as ppp0, second as ppp1, etc. The lo is the loopback only interface which should be always active. Use the options (see ifconfig help) to configure the interfaces.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

173

ifup interface_name

(/sbin/ifup to run as a user) Startup a network interface. Example: ifup eth0, ifup ppp0, ifup ppp1 Users can start up or shutdown the ppp interface only when the permission is given in the ppp setup (using netconf ). To start a ppp interface (dial-up connection), We normally use kppp available under the KDE K menu (or by typing kppp in an X-terminal). /etc/rc.d/init.d/network restart

Restart the network using its normal initialization script (the same which is used during bootup). Useful if you just have manually made changes to your network configuration. Any other service listed in init.d can be stopped, started, or restarted in a similar way (call the script with an options stop, start or restart). ifdown interface_name (/sbin/ifdown to run it as a user). Shut down the network interface. Example: ifdown ppp0. netstat | more

Displays a lot information on the status of your network. /usr/sbin/mtr gtk

(as root, in X windows if you wish the nice gtk-based interface). Network diagnostic tool combining the capabilities of traceroute and ping. nmap ip_number

Map the ports on the machine with ip_number. REALLY useful to establish the security of your network configuration as you can see the opened ports. ethereal

(as root, in Xterminal) Network analyzerview the network trafic going through your computer. tcpdump -i ppp0 -a -x

(as root) Print all the network traffic going through the first over-the-phone interface (ppp0) as ascii and hexadecimal.

PDF created with pdfFactory Pro trial version www.pdffactory.com

174 9.17 SUMMARY

Chapter 9 - Installation of Linux

The process of installing Red Hat Linux is straightforward, although there are lots of little problems scattered throughout the process that can cause hassles. Red Hat Linux is much easier to install than many other versions of Linux, so you will find the installation goes quite easily. Common errors that you might make are handled nicely by the Red Hat installation program, and the system is versatile enough to handle most configuration requirements. The essential steps for installing Red Hat Linux are as follows: 1. Create boot and root disks. 2. Boot Linux from floppies. 3. Partition the hard disk. 4. Create a swap file. 5. Create a Linux file system. 6. Install the Linux software. 7. Configure the kernel. 8. Set the boot process. 9. Reboot into Linux from your hard disk.

The process is similar whether you are installing from a CD-ROM or from a diskette. Because the CD-ROM is the most common form of installation, it is used as the example in this chapter. Much of this chapter will be familiar to you if you have installed Linux before, although some users really dont know what goes on during the automated installation script. Knowing the process, and staying on top of it, helps prevent problems with the Linux installation.

9.18 CHECK YOUR PROGRESS


I. Exercises Work on all the installation commands described in the unit by logging in as root.
Start installing a Redhat Linux. This is available with magazines such as pcquest. Make multiple partitions to install dual boot or triple boot of operating systems.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

175

Try installing updation packages for the system you have installed. The student has to concentrate for Creation of an Installation Diskette, Partitioning Hard Drive(s) and choosing Desired Packages required to Install, Multi-boot with Other Operating Systems. Give an example of space works of partitions for installing Linux and windows operating systems in one hard disk size of 4.3 Gb. You may use 2 Gb for windows 98 operating system. Install first on a separate hard disk (min 300 MB) Linux OS in text mode only. Install the linux OS including X windows on a separate hard disk (min 1.2 GB). Install the linux OS including X windows on a hard disk (min 4.3 GB) with multi-boot concept along with windows 95/98 also windows NT. Try out creation of Linux boot diskette with different commands. Partition a hard disk with different sizes using any partition command such as diskdruid or fdisk.

II. Choose the correct answer 1. Creation of linux boot disk can be done through
a. rawrite 2. b. mkbootdisk c. diskcopy d. all of these.

The example of a compressed file used for any package installation is a. *.tar.gz b. *.rpm c. *.zip d. all of these.

3.

Linuxconf can be used to a. mount a drive b. configure a network c. administer users d. all of these

4.

LILO is used for a. Creation of boot diskette b. Managing linux network c. Multiboot d. Linux configuration

5.

A Linux system must contain a. Swap partition b. root password c. linux native partition d. all of these

6.

Setup command helps to configure a. Only X windows b. Mouse c. Sound card d. all of these

PDF created with pdfFactory Pro trial version www.pdffactory.com

176

Chapter 9 - Installation of Linux

III. Say True or False 1. Linear mode option must be used for multi boot option with size of first partition more than 1023 cylinders.
2. Formatting of all the partitions or mount points is a must for Linux installation

True/False True/False True/False True/False True/False

3. /dev mount point contains all user files and information. 4. fdisk command is used to create boot diskette. 4. Windows NT operating system can not be installed as dual boot for linux.

IV. Essay type questions 1. Explain how to create an installation or boot diskette.
2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. Illustrate the significance of /etc/fstab file with respect to installation. Elucidate the different types of partition sizes required for linux installation. What is partitioning of hard disk drive? Why it is important explain. Describe the possible benefits of partitioning of hard disk drive. Describe the network administration tools and commands with options. Explain the usage of /, swap, /home, /usr mount points with respect to linux installation. What is LILO? How it is useful in Linux. Enumerate the full hardware details of a computer involving all parts. Explain how the updations of packages can be done in Linux. Brief the function of the command linuxconf. What is a tar file? Explain its options in detail. What is a rpm utility? Explain its options in detail. Explain the Partitioning utility fdisk in detail with its different options. Describe the standard linux filesystem types with its significance. Write a detail note on Linux installation.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

177

V. Further readings and other activities Get more information using man or info or any help command for linuxconf, fs, lilo, setup, Xconfigurator, fdisk, sndconfig, mouseconfig, netconf, neconfig, userconf, ipcalc, fstab, disk, sfdisk, tar, rpm. EX: 1. $ man sfdisk 2. $ apropos nysysv 3. info mtools
For further readings you can refer the following books 1. Title: Linux internals Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga http://www.pandurangarao.i8.com/doc/linux/linuxbook.html http://www.raomvp.bravepages.com/doc/linux/linuxbook.html http://www.geocities.com/raomvp/doc/linux/linuxbook.html A complete reference for you to get more information. Title: The Complete Reference Linux Foutth Edition Author: Richard Petersen Publication: Tata McGraw-Hill Title: Red Hat Linux Starter kit in 24 hours Author: Judith Samson etal Publication: Sams Techmedia Title: Unix Shell Programming Author: Yashawant Kanitkar Publication: BPB Title: Linux Kernel Internals Author: M. Beck etal Publication: Addison-Wesley, Second Edition Title: Unix Power Tools Author: Jerry Peek etal Publication: BPB Title: Unix System Programming Author: Richard Stevens Title: Linux System Administration Handbook - for detail information on problems of linux installation.

2.

3.

4.

5.

6.

7. 8.

PDF created with pdfFactory Pro trial version www.pdffactory.com

178
VI. Solutions I. Choose the correct answer 1.d 2.d 3.d 4.c
5.d 6.d

Chapter 9 - Installation of Linux

II. Say True or False 1.True 2.True 3.False 4.False 5.False

PDF created with pdfFactory Pro trial version www.pdffactory.com

Chapter 10

Glossary

GLOSSARY OF TERMS
$HOME Environment variable that points to your login directory. $PATH The shell environment variable that contains a set of directories to be searched for LINUX commands. /dev/null file The place to send output that you are not interesting in seeing; also the place to get input from when you have none (but the program or command requires something). This is also known as the bit bucket, which is where old bits go to die. /etc/cshrc file The file containing shell environment characteristics common to all users that use the C Shell. /etc/group file This file contains information about groups, the users they contain, and passwords required for access by other users. The password may actually be in another filethe shadow group fileto protect it from attacks. /etc/inittab file The file that contains a list of active terminal ports for which LINUX will issue the login prompt. This also contains a list of background processes for LINUX to initialize. Some versions of LINUX use other files like /etc/tty.

MSIT 3A Linux Internals

179

PDF created with pdfFactory Pro trial version www.pdffactory.com

180
/etc/motd file

Chapter 10 - Glossary

Message Of The Day file usually contains information the system administrator feels is important for you to know. This file is displayed when the user signs on the system. /etc/passwd file Contains user information and password. The password may actually be in another filethe shadow password fileto protect it from attacks. /etc/profile The file containing shell environment characteristics common to all users that use the Bourne and Korn shells. abbreviation (vi) User-defined character sequences that are expanded into the defined text string when typed during insert mode. absolute pathname The means used to represent the location of a file in a directory by specifying the exact location including all directories in the chain including the root. API (Application Program Interface) The specific method prescribed by a computer operating system, application, or third-party tool by which a programmer writing an application program can make requests of the operating system. arguments See parameters. ARPA See DARPA. ASCII (American Standard Code for Information Interchange) Used to represent characters in memory for most computers. AT&T UNIX Original version of UNIX developed at AT&T Bell Labs, which was later known as UNIX Systems Laboratories. Many current versions of UNIX are descendants; even BSD UNIX was derived from early AT&T UNIX.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

181

attribute The means of describing objects. The attributes for a ball might be: rubber, red, 3 cm in diameter. The behavior of the ball might be how high it bounces when thrown. Attribute is another name for the data contained within an object (class). AWK Programming language developed by A.V. Aho, P.J. Weinberger, and Brian W. Kernighan. The language is built on C syntax, includes the regular expression search facilities of grep, and adds in the advanced string and array handling features that are missing from the C language. nawk, gawk, and POSIX awk are versions of this language. background Processes usually running at a lower priority and with their input disconnected from the interactive session. Any input and output are usually directed to a file or other process. background process An autonomous process that runs under LINUX without requiring user interaction. backup The process of storing the LINUX system, applications, and data files on removable media for future retrieval. bash BASH stands for GNU Bourne Again Shell, and is based on the Bourne shell, sh, the original command interpreter. beep Usually referred to in LINUX documentation as the bell (see bell). bell The character sent by a program to a terminal to indicate some kind of error condition; for example, in vi pressing Esc to exit insert mode when you are already in command mode; actually the ^G character which rather than displaying on the terminal instead causes it to sound an alarm, which on ancient teletype terminals was implemented as a bell. Different terminals produce different sounds for their bells including one old video terminal that sounded like someone shifting gears without benefit of clutch. binding (emacs) The assignment of a shift-key sequence to an Emacs editing command.

PDF created with pdfFactory Pro trial version www.pdffactory.com

182
block-special

Chapter 10 - Glossary

A device file that is used to communicate with a block oriented I/O device. Disk and tape drives are examples of block devices. The block-special file refers to the entire device. You should not use this file unless you want to ignore the directory structure of the device (that is, if you are coding a device driver). boot or boot up The process of starting the operating system (LINUX). Bourne Shell The original standard user interface to UNIX that supported limited programming capability. BSD UNIX Version of UNIX developed by Berkeley Software Distribution and written at UC Berkeley buffer (vi) The working version of the file you are editing is usually called the buffer; the buffer is actually an image of the file kept in random access memory during editing; changes are made in this image and only written out to disk upon user command (or when the vi autowrite setting is in effect); see also named buffer and undo buffer. buffer list (emacs) A special window which shows all of the buffers currently open; allows you to manipulate buffers using buffer list commands. C Programming language developed by Brian W. Kernighan and Dennis M. Ritchie. The C language is highly portable and available on many platforms including mainframes, PCs, and, of course, UNIX systems. C Shell A user interface for UNIX written by Bill Joy at Berkeley. It also features C programming-like syntax. CD-ROM (Compact Disk-Read Only Memory) Computer-readable data stored on the same physical form as a musical CD. Large capacity, inexpensive, slower than a hard disk, and limited to reading. There are versions that are writable (CD-R, CD Recordable) and other formats that can be written to once or many times. CGI (Common Gateway Interface) A means of transmitting data between Web pages and programs or scripts executing on the server. Those programs can then process the data and send the results back to the users browser through dynamically creating HTML.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

183

character-special A device file that is used to communicate with character-oriented I/O devices such as terminals, printers, or network communications lines. All I/O access is treated as a series of bytes (characters). characters 1. alphabetic The letters A through Z and a through z. 2. alphanumeric The letters A through Z and a through z, and the numbers 0 through 9. 3. control

Any non-printable characters. The characters are used to control devices, separate records, and eject pages on printers. 4. numeric The numbers 0 through 9. 5. special

Any of the punctuation characters or printable characters that are not alphanumeric. Include the space, comma, period, and many others. child-process See sub-process. child-shell See sub-shell. class A model of objects that have attributes (data) and behavior (code or functions). It is also viewed as a collection of objects in their abstracted form. command line (1) The shell command line from which the current vi or Emacs session was started; (2) the ex command line, where ex commands are entered. command line editing LINUX shells support the ability to recall a previously entered command, modify it, and then execute the new version. The command history can remain between sessions (the commands you did yesterday can be available for you when you log in today). Some shells support a command line editing mode that uses a subset of the vi, emacs, or gmacs editor commands for command recall and modification.

PDF created with pdfFactory Pro trial version www.pdffactory.com

184
command line history See command line editing. command line parameters

Chapter 10 - Glossary

Used to specify parameters to pass to the execute program or procedure. Also known as command line arguments. command prompt See shell prompt. completion (emacs) The automatic provision of the rest of a command or a file name; when the command or file name cannot be resolved to a single entity, a menu of choices is provided (type a few characters of the name and press TAB; Emacs will either complete the name or give you a menu of choices). configuration files Collections of information used to initialize and set up the environment for specific commands and programs. Shell configuration files set up the users environment. configuration files, shell For Bourne shell: /etc/profile and $HOME/.profile. For Korn shell: /etc/profile, $HOME/.profile, and ENV= file. For C shell: /etc/.login, /etc/cshrc, $HOME/.login, $HOME/.cshrc, and $HOME/.logout. Older versions may not support the first two files listed. For BASH: /etc/profile/, $HOME/.bash_profile, $HOME/.bash_login, $HOME/.profile, $HOME/ .bashrc, ~/.bash_logout. control keys These are keys that cause some function to be performed instead of displaying a character. These functions have names, for instance, the end-of-file key tells the LINUX that there is no more input. The typical end-of-file key is the <^D> (control-d) key. CPU (Central Processing Unit) The primary brain of the computer; the calculation engine and logic controller. current macro (emacs) The most recently recorded macro; it is executed by the call-last-kbd-macro function.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

185

cursor The specific point on the screen where the next editing action will take place; the cursor is usually indicated on the screen by some sort of highlighting, such as a underscore or a solid block, which may or may not be blinking. daemon A system related background process that often runs with the permissions of root and services requests from other processes. DARPA (U.S. Department of Defense Advanced Research Projects Agency) Funded development of TCP/IP and ARPAnet (predecessor of the Internet). database server See server, database. default settings Most tools and systems are governed by a number of settings; those that are in effect when the tool is started is known as the default. vi is governed by a number of settings; the default settings are those in effect when vi is first started and no automatic overrides of settings are in effect through .exrc files or EXINIT environment variables. device file File used to implement access to a physical device. This provides a consistent approach to access of storage media under LINUXdata files and devices (such as tapes and communication facilities) are implemented as files. To the programmer, there is no real difference. directory A means of organizing and collecting files together. The directory itself is a file that consists of a list of files contained within it. The root (/) directory is the top level and every other directory is contained in it (directly or indirectly). A directory might contain other directories, which are known as sub-directories. directory navigation The process of moving through directories is known as navigation. Your current directory is known as the current working directory. Your login directory is known as the default or home directory. Using the cd command, you can move up and down through the tree structure of directories. DNS (Domain Name Server) Used to convert the name of a machine on the Internet (name.domain.com) to the numeric address (123.45.111.123).

PDF created with pdfFactory Pro trial version www.pdffactory.com

186
DOS (Disk Operating System)

Chapter 10 - Glossary

Operating system that is based on the use of disks for the storage of commands. It is also a generic name for MS-DOS and PC-DOS on the Personal Computer. MS-DOS is the version Microsoft sells and PC-DOS is the version IBM sells. Both are based on Microsoft code. EBCDIC (Extended Binary Coded Decimal Interchange Code) The code used to represent characters in memory for mainframe computers. echo The display on the screen of characters you type is sometimes called the echo of characters; it is called this because usually your terminal is set up not to display the characters directly as typed, but rather to wait for them to be sent to the computer, which then echoes (sends) them back to your terminal. ed A common tool used for line-oriented text editing. e-mail Messages sent through an electronic medium instead of through the local postal service. There are many proprietary e-mail systems that are designed to handle mail within a LAN environment; most of these are also able to send over the Internet. Most Internet (open) e-mail systems make use of MIME to handle attached data (which can be binary). emacs A freely available editor now part of the GNU software distribution. Originally written by Richard M. Stallman at MIT in the late 1970s, it is available for many platforms. It is extremely extensible and has its own programming language; the name stands for Editing with MACroS.encapsulation. The process of combining data (attributes) and functions (behavior in the form of code) into an object. The data and functions are closely coupled within an object. Instead of every programmer being able to access the data in a structure his own way, programmers have to use the code connected with that data. This promotes code reuse and standardized methods of working with the data. environment variables See variables, environmental. escape (1) (vi) The Esc key, used to terminate insert mode, or an incomplete vi command; (2) To prevent a character from having its normal interpretation by a program by preceding it with the escape character (usually \, the backslash); for example in a regular expression, to search for a literal character that has a special meaning in a regular expression, it must be escaped; as a specific example, to search for a period

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

187

(.), you must type it escaped as \. ethernet A networking method where the systems are connected to a single shared bus and all traffic is available to every machine. The data packets contain an identifier of the recipient, which is the only machine that should process that packet. expression A constant, variable, or operands and operators combined. Used to set a value, perform a calculation, or set the pattern for a comparison (regular expressions). fifo First In, First Out. See named pipe. file Collection of bytes stored on a device (typically a disk or tape). Can be source code, executable binaries or scripts, or data. 1. indexed

A file based on a file structure where data can be retrieved based on specific keys (name, employee number, and so on) or sequentially. The keys are stored in an index. This is not directly supported by the LINUX operating system; usually implemented by the programmer or by using tools from an ISV. A typical form is known as ISAM. 2. line sequential

See file, text. 3. sequential

a. A file that can only be accessed sequentially (not randomly). b. A file without record separators. Typically fixed length but LINUX does not know what that length is and does not care. 4. text

A file with record separators. May be fixed or variable length; LINUX tools can handle these files because it can tell when the record ends (by the separator). file compression The process of applying mathematical formula to data typically resulting in a form of the data that

PDF created with pdfFactory Pro trial version www.pdffactory.com

188

Chapter 10 - Glossary

occupies less space. A compressed file can be uncompressed (lossless) resulting in the original file. When the compression/uncompress process results in exactly the same file as was originally compressed, it is known as lossless. If information about the original file is lost, the compression method is know as lossy. Data and programs need lossless compression; images and sounds can stand lossy compression. filename The name used to identify a collection of data (a file). Without a pathname, it is assumed to be in the current directory. filename generation The process of the shell interpreting meta-characters (wild cards) to produce a list of matching files. This is referred to as filename expansion or globbing. filename, fully qualified The name used to identify a collection of data (a file) and its location. It includes both the path and name of the file; typically, the pathname is fully specified (absolute). See also pathname and pathname, absolute. filesystem A collection of disk storage that is connected (mounted) to the directory structure at some point (sometimes at the root). Filesystems are stored in a disk partition and are also referred to as disk partitions. firewall A system used to provide a controlled entry point to the internal network from the outside (usually the Internet). This is used to prevent outside or unauthorized systems from accessing systems on your internal network. The capability depends on the individual software package, but the features typically include: filter packets, filter datagrams, provide system (name or IP address) aliasing, and rejecting packets from certain IP addresses. It can also prevent internal systems from accessing the Internet on the outside. In theory, it provides protection from malicious programs or people on the outside. The name comes from the physical barrier between connected buildings or within a single building that is supposed to prevent fire from spreading from one to another. flags See options. foreground Programs running while connected to the interactive session. fseek

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

189

Internal function used by LINUX to locate data inside a file or filesystem. ANSI standard fseek accepts a parameter that can hold a value of +2 to -2 billion. This function, used by the operating system, system tools, and application programs, is the cause of the 2 GB file and filesystem size limitation on most systems. With 64 bit operating systems, this limit is going away. FTP (File Transfer Protocol, or File Transfer Program) A system-independent means of transferring files between systems connected via TCP/IP. Ensures that the file is transferred correctly, even if there are errors during transmission. Can usually handle character set conversions (ASCII/EBCDIC) and record terminator resolution (<lf> for LINUX, <cr> and <lf> for MS/PC-DOS). gateway A combination of hardware, software, and network connections that provides a link between one architecture and another. Typically, a gateway is used to connect a LAN or LINUX server with a mainframe (that uses SNA for networking resulting in the name: SNA gateway). A gateway can also be the connection between the internal and external network (often referred to as a firewall). See also firewall. globbing See filename generation. GNU GNU stands for GNUs Not Unix, and is the name of free useful software packages commonly found in UNIX environments that are being distributed by the GNU project at MIT, largely through the efforts of Richard Stallman. grep A common tool used to search a file for a pattern. egrep and fgrep are newer versions. Egrep allows the use of extended (hence the e prefix) regular expressions, fgrep uses limited expressions for a faster (hence the f prefix) searches. here document The << redirection operator, known as here document, allows keyboard input (stdin) for the program to be included in the script. HTML (HyperText Markup Language) Describes World Wide Web pages. It is the document language that is used to define the pages available on the Internet through the use of tags. A browser interprets the HTML to display the desired information.

PDF created with pdfFactory Pro trial version www.pdffactory.com

190
i-node

Chapter 10 - Glossary

Used to describes a file and its storage. The directory contains a cross reference between the i-node and pathname/filename combination. Also known as inode. I-Phone (Internet Phone) A method of transmitting speech long distances over the Internet in near real-time allowing the participants to avoid paying long distance telephone charges. They still pay for the call to their ISP and the ISPs service charges. ICMP (Internet Control Message Protocol) Part of TCP/IP that provides network layer management and control. inheritance A method of object-oriented software reuse in which new classes are developed based on existing ones by using the existing attributes and behavior and adding on to them. For example, if the base object is automobile with attributes of an engine, four wheels, and tires, and behavior of acceleration, turning, and deceleration, then a sports car would modify the attributes so the engine would be larger or have more horsepower than the default, the four wheels would include alloy wheels and high speed rated tires, and the behavior would also be modified for faster acceleration, tighter turning radius, and faster deceleration. inode See i-node. Internet A collection of different networks that provide the ability to move data between them. It is built on the TCP/IP communications protocol. Originally developed by DARPA, it was taken over by NSF, and has now been released from governmental control. Internet Service Provider The people who connect you to the Internet. IRC (Internet Relay Chat) A server-based application that allows groups of people to communicate simultaneously through textbased conversations. IRC is similar to Citizen Band radio or the chat rooms on some bulletin boards. Some chats can be private (between invited people only) or public (where anyone can join in). IRC now also supports sound files as well as text it can also be useful for file exchange.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

191

ISAM (Indexed Sequential Access Method) On LINUX and other systems, ISAM refers to a method for accessing data in a keyed or sequential way. The LINUX operating system does not directly support ISAM files; they are typically add on products. ISP See Internet Service Provider. ISV (Independent Software Vendor) Generic name for software vendors other than your hardware vendor. kernel The core of the operating system that handles tasks such as memory allocation, device input and output, process allocation, security, and user access. LINUX tends to have a small kernel when compared to other operating systems. keyboard macros A feature which allows a special key sequence to stand for another, usually more complex sequence; in vi, keyboard macros are implemented via the :map command. kill ring (emacs) A set of buffers where killed text is kept; the buffers are arranged in a circular pattern. When commands that automatically move from one buffer to the next get to the end of the set, the next movement will be to the first buffer in the ring. Korn Shell A user interface for LINUX with extensive scripting (programming) support. Written by David G. Korn. The shell features command line editing and will also accept scripts written for the Bourne Shell. LAN (Local Area Network) A collection of networking hardware, software, desktop computers, servers, and hosts all connected together within a defined local area. A LAN could be an entire college campus. limits See quota. line address (vi and ex) The way a selected set of lines is indicated in ex mode is through a line address. A line address can be an absolute line number, relative line number, or special symbols which refer to the beginning or of the file.

PDF created with pdfFactory Pro trial version www.pdffactory.com

192
link 1. hard

Chapter 10 - Glossary

Directory entry that provides an alias to another file that is in the same filesystem. Multiple entries appear in the directory (or other directories) for one physical file without replication of the contents. 2. soft

See symbolic link. 3. symbolic

Directory entry that provides an alias to another file that can be in another filesystem. Multiple entries appear in the directory for one physical file without replication of the contents. Implemented through link files; see also link file. 4. file

File used to implement a symbolic link producing an alias on one filesystem for a file on another. The file only contains the fully qualified filename of the original (linked-to) file. lisp A programming language used in artificial intelligence. The name stands for LISt Processing. It is the programming language that Emacs is written in and also refers to three major modes within it. literal text string An exact character text string, with no wildcards. login The process that a user gains access to a LINUX system. This can also refer to the user id that is typed at the login prompt. macro A recorded series of keystrokes which can be played back to accomplish the same task repetitively. major mode (emacs) A named set of behavioral characteristics; a buffer can be in only one major mode at a time. For examples, text mode for writing a letter; c mode for writing c source code. man page On-line reference tool under LINUX that contains the documentation for the systemthe actual pages from the printed manuals. It is stored in a searchable form for improved capability to locate information.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

193

manual page See man page. mappings (vi) User-defined character sequences (which may include control keys) that are interpreted as a command sequence (which may also include control keys). memory 1. real The amount of storage that is being used within the system (silicon; it used to be magnetic cores). 2. virtual Memory that exists but you cannot see. Secondary storage (disk) is used to allow the operating system to allow programs to use more memory than is physically available. Part of a disk is used as a paging file and portions of programs and their data are moved between it and real memory. To the program, it is in real memory. The hardware and operating system performs translation between the memory address the program thinks it is using the where it is actually stored. meta-character A printing character that has special meaning to the shell or another command. It is converted into something else by the shell or command - the asterisk <*> is converted by the shell to a list of all files in the current directory. MIME (Multipurpose Internet Mail Extensions) A set of protocols or methods of attaching binary data (executable programs, images, sound files, and so on) or additional text to e-mail messages. mini-buffer (emacs) The last line on the screen, where commands are entered. minor mode (emacs) A particular characteristic which can be independently toggled on or off. For example, autofill mode for easing the creation of document text. mode Many programs offer only subsets of their functions at any given time, because only certain functions are relevant within an immediate context; further, the same keystroke may invoke different commands in these different contexts; such a context is referred to as a mode. Major modes in vi are insert mode (for adding new text into the buffer), and command mode (for most other editing actions).

PDF created with pdfFactory Pro trial version www.pdffactory.com

194
MPTN (MultiProtocol Transport Network) IBM networking protocol to connect mainframe to TCP/IP network. named buffer

Chapter 10 - Glossary

(vi)A memory location where text objects can be stored during a single vi session; named buffers persist when you switch from one file to another during a session and are the primary way of moving and copying text between files. named pipe An expanded function of a regular pipe (redirecting the output of one program to become the input of another). Instead of connecting stdout to stdin, the output of one program is sent to the named pipe and another program reads data from the same file. This is implemented through a special file known as a pipe file or fifo. The operating system ensures the proper sequencing of the data. Little or no data is actually stored in the pipe file; it just acts as a connection between the two. Netnews This is a loosely controlled collection of discussion groups. A message (similar to an e-mail) is posted in a specific area and then people can comment on it, publicly replying to the same place (posting a response) for others to see. A collection of messages along the same theme is referred to as a thread. Some of the groups are moderated, which means that nothing is posted without the approval of the owner. Most are not and the title of the group is no guarantee that the discussion will be related. The official term for this is Usenet News. NFS (Network File System) Means of connecting disks that are mounted to a remote system to the local system as if they were physically connected. NIS (Network Information Service) A service that provides information necessary to all machines on a network, such as NFS support for hosts and clients, password verification, and so on. NNTP (Net News Transport Protocol) Used to transmit Netnews or usenet messages over top of TCP/IP. See Netnews for more information on the messages transmitted. null statement A program step that performs no operation but to hold space and fulfill syntactical requirements of the programming language. Also known as a NO-OP for no-operation performed.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

195

numeric setting A setting which takes a numeric value, rather than an enabled or disabled state. Applies to many tools including vi and the different shells. object An object in the truest sense of the word is something that has physical properties, such as automobiles, rubber balls, and clouds. These things have attributes and behavior. They can be abstracted into data (attribute) and code (behavior). Instead of just writing functions to work on data, they are encapsulated into a package that is known as an object. open mode The visual mode of the ex editor. operator Meta-character that performs a function on values or variables. The plus sign <+> is an operator that adds two integers. options Program- or command-specific indicators that control behavior of that program. Sometimes called flags. The -a option to the ls command shows the files that begin with a . (such as .profile, .kshrc, and so on.) Without it, these files would not be shown, no matter what wildcards were used. These are used on the command line. See also parameters. package (emacs) A feature set which can be added to the editor. Major modes and many functions are implemented via packages. Numerous packages are built in to standard Emacs; many others are freely or otherwise available. parameters Data passed to a command or program through the command line. These can be options (see options) that control the command or arguments that the command works on. Some have special meaning based on their position on the command line. parent process identifier Shown in the heading of the ps command as PPID. The process identifier of the parent-process. See also parent-process. parent-process Process that controls another often referred to as the child- or sub-process. See process.

PDF created with pdfFactory Pro trial version www.pdffactory.com

196
parent-shell

Chapter 10 - Glossary

Shell (typically the login shell) that controls another, often referred to as the child- or sub-shell. See shell. password The secure code that is used in combination with a user id to gain access to a LINUX system. pathname The means used to represent the location of a file in the directory structure. If you do not specify a pathname, it defaults to the current directory. Also see absolute pathname and relative pathname. PDP (Personal Data Processor) Computers manufactured by Digital Equipment Corporation. LINUX was originally written for a PDP7 and gained popularity on the PDP-11. The entire series were inexpensive mini-computers popular with educational institutions and small businesses. Perl (Practical Extraction and Report Language) Programming language developed by Larry Wall. (Perl stands for Practical Extraction and Report Language or Pathologically Eclectic Rubbish Language, both are equally valid). The language provides all of the capabilities of awk and sed, plus many of the features of the shells and C. permissions When applied to files, they are the attributes that control access to a file. There are three levels of access: owner (the file creator), group (people belonging to a related group as determined by the system administrator), and other (everyone else). The permissions may be r for read, w for write, and x for execute. The execute permissions flag is also used to control who may search a directory. pipe A method of sending the output of one program (redirecting) to become the input of another. The pipe character <|> tells the shell to perform the redirection. pipe file See named pipe. polymorphism Allows code to be written in a general fashion to handle existing and future related classes. Properly developed, the same behavior can act differently depending on the derived object it acts on. With an automobile, the acceleration behavior might be different for a station wagon and a dragster which are

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

197

subclasses of the superclass automobile. The function would still be accelerate(), but the version would vary (this may sound confusing, but the compiler keeps track and figures it all out). POSIX POSIX stands for Portable Operating System Interface, UNIX. It is the name for a family of open system standards based on UNIX. The name has been credited to Richard Stallman. The POSIX Shell and Utilities standard developed by IEEE Working Group 1003.2 (POSIX.2) concentrates on the command interpreter interface and utility programs. PPP (Point-to-Point Protocol) Internet protocol over serial link (modem). process A discrete running program under LINUX. The users interactive session is a process. A process can invoke (run) and control another program that is then referred to as a sub-process. Ultimately, everything a user does is a subprocess of the operating system. process identifier Shown in the heading of the ps command as PID. The unique number assigned to every process running in the system. quota General description of a system-imposed limitation on a user or process. It can apply to disk space, memory usage, CPU usage, maximum number of open files, and many other resources. quoting The use of single and double quotes to negate the normal command interpretation and concatenate all words and whitespace within the quotes as a single piece of text. range (vi, ed, and ex) a line address which indicates one or more lines from a starting line to an ending line; indicated as start,end where both start and end are individual line addresses. recursive edit (emacs) A feature that allows a query-replace operation to be temporarily suspended while other editing is done. redirection The process of directing a data flow from the default. Input can be redirected to get data from a file or

PDF created with pdfFactory Pro trial version www.pdffactory.com

198

Chapter 10 - Glossary

the output of another program. Normal output can be sent to another program or a file. Errors can be sent to another program or a file. regular expression A way of specifying and matching strings for shells (filename wildcarding), grep (file searches), sed, and awk. relative pathname The means used to represent the location of a file in a directory other than the current by navigating up and down through other directories using the current directory as a base. reserved word A set of characters recognized by LINUX and related to a specific program, function, or command. RFC (Request For Comment) Document used for creation of Internet and TCP/IP related standards. rlogin (Remote Login) Gives the same functionality of telnet, with the added functionality of not requiring a password from trusted clients, which can also create security concerns. See telnet. root 1) The user who owns the operating system and controls the computer. 2) The processes of the operating system run as though a user, root, signed on and started them. The root user is all powerful and can do anything he or she wants. For this reason, the root user is often referred to as a super-user. It is also the very top of the directory tree structure. routing The process of moving network traffic between two different physical networks; also decides which path to take when there are multiple connections between the two machines. It may also send traffic around transmission interruptions. RPC (Remote Procedural Call) Provides the capability to call functions or subroutines that run on a remote system from the local one. scripts A program written for a LINUX utility including shells, AWK, perl, sed, and others. Also see shell scripts.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

199

sed A common tool used for stream text editing, having ed-like syntax. server, database A system designated to run database software (typically a relational database like Oracle, SQL Server, Sybase, or others). Other systems connect to this one to get the data (client applications). settings vi is governed by a number of internal variable called settings; these control how certain actions take place. Shell The part of LINUX that handles user input and invokes other programs to run commands. Includes a programming language. See also Bourne Shell, C Shell, Korn Shell, tcsh, and BASH. shell environment The shell program (Bourne, Korn, C, tcsh, or BASH), invocation options, and preset variables that define the characteristics, features and functionality of the LINUX command line and program execution interface. shell buffer (emacs) A buffer in which an interactive LINUX shell session has been started. shell scripts A program written using a shell programming language like those supported by Bourne, Korn, or C shells. shift-key sequence (emacs) To perform a shift-key sequence, hold down the designated shift key (for example, Shift, Ctrl, Alt, or Meta), then press the second designated key, then release both keys. When typing several consecutive shift-key sequences that use the same shift key, you can keep holding down the shift key for the duration. signal A special flag or interrupts that is used to communicate special events to programs by the operating system and other programs. SLIP (Serial Line Internet Protocol)

PDF created with pdfFactory Pro trial version www.pdffactory.com

200

Chapter 10 - Glossary

Internet over a serial link (modem). The protocol frames and controls the transmission of TCP/IP packets of the line. SNA(System Network Architecture) IBM networking architecture. special keys See control keys. stderr The normal error output for a program that is sent to the screen by default. Can be redirected to a file. stdin The normal input for a program, taken from the keyboard by default. Can be redirected to get input from a file or the output of another program. stdout The normal output for a program that is sent to the screen by default. Can be redirected to a file or to the input of another program. sticky bit One of the status flags on a file that tells LINUX to load a copy of the file into the page file the first time it is executed. This is done for programs that are commonly used so the bytes are available quickly. When the sticky bit is used on frequently used directories, they are cached in memory. stream A sequential collection of data. All files are streams to the LINUX operating system. To it, there is no structure to a filethat is something imposed by application programs or special tools (ISAM packages or relational databases). sub-directory See directory. sub-process Process running under the control of another, often referred to as the parent-process. See process. sub-shell Shell running under the control of another, often referred to as the parent-shell (typically the login shell). See shell.

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

201

subnet A portion of a network that shares a common IP address component. Used for security and performance reasons. super-user See root. system administrator The person who takes care of the operating system and user administrative issues on LINUX systems. Also called a system manager although that term is much more common in DEC VAX installations. system manager See system administrator. system programmer See system administrator. TCP/IP (Transport Control Protocol/Internet Protocol) The pair of protocols and also generic name for suite of tools and protocols. that forms the basis for the Internet. Originally developed to connect systems to the ARPANET. tcsh A C shell-like user interface featuring command-line editing. Telnet Protocol for interactive (character user interface) terminal access to remote systems. The terminal emulator that uses the telnet protocol is often known as telnet or tnvt100. Terminal A hardware device, normally containing a cathode ray tube (screen) and keyboard for human interaction with a computer system. text object (vi) A text object is the portion of text in the buffer that would be traversed by a specific movement command; for example w refers to the next small word. text processing languages A way of developing documents in text editors with embedded commands that handle formatting. The

PDF created with pdfFactory Pro trial version www.pdffactory.com

202

Chapter 10 - Glossary

file is fed through a processor that executes the embedded commands producing a formatted document. These include roff, nroff, troff, RUNOFF, TeX, LaTeX, and even the mainframe SCRIPT. TFTP (Trivial File Transfer Protocol or Trivial File Transfer Program) A system-independent means of transferring files between systems connected via TCP/IP. It is different from FTP in that it does not ensure that the file is transferred correctly, does not authenticate users, and is missing a lot of functionality (like the ls command). toggle A mode that is alternately turned on and off by successive entry of its command. toggle setting (vi) A setting which is either enabled or disabled; for example, for the fictitious setting named option, you would enable the setting by entering the command :set option; you would disable the setting by entering the command :set nooption. top A common tool used to display information about the top processes on the system. typewriter key The subset of a terminal keyboard that is on a standard typewriter; generally the alphanumeric keys, but not the function, cursor control, or numeric pad keys. UDP (User Datagram Protocol) Part of TCP/IP used for control messages and data transmission where the delivery acknowledgment is not needed. The application program must ensure data transmission in this case. undo buffer (vi) A location in memory where the most recent deleted text object is saved, either for later undoing of the deletion, or for copying of the object to another location. URL (Uniform Resource Locator) The method of specifying the protocol, format, login (usually omitted), and location of materials on the Internet. Usenet See Netnews. UUCP (UNIX-to-UNIX-Copy-Program)

PDF created with pdfFactory Pro trial version www.pdffactory.com

MSIT 3A Linux Internals

203

Used to build an early, informal network for the transmission of files, e-mail, and Netnews. variables, attributes The modifiers that set the variable type. A variable can be string or integer, left or right justified, readonly or changeable, and other attributes. variables, environmental A place to store data and values (strings and integers) in the area controlled by the shell so they are available to the current and sub-processes. They can just be local to the current shell or available to a subshell (exported). variables, substitution The process of interpreting an environmental variable to get its value. viewport The portion of the buffer that appears in a window on your screen; one way to think of moving through the buffer is to think of the viewport as sliding back and forth through the buffer. Web See World Wide Web. whitespace Blanks, space and tabs that are normally interpreted to delineate commands and filenames unless quoted. wildcard Means of specifying filename(s) where the operating system determines some of the characters. Multiple files may match and will be available to the tool. window The portion of your screen which is displaying a viewport into a buffer. World Wide Web A collection of servers and services on the Internet that run software that communicate using a common protocol (HTTP). Instead of having to remember the location of these resources, links are provided from one Web page to another through the use of URLs (Uniform Resource Locators). WWW See World Wide Web.

PDF created with pdfFactory Pro trial version www.pdffactory.com

204
X Window System

Chapter 10 - Glossary

A windowing and graphics system developed by MIT, to be used in client/server environments. X See X Window System. X11 See X Window System. X-windows The wrong term for the X Window System. See X Window System.

PDF created with pdfFactory Pro trial version www.pdffactory.com

You might also like