The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux...

107
The Linux System Administrators’ Guide Version 0.6.2 Lars Wirzenius [email protected] Joanna Oja [email protected]

Transcript of The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux...

Page 1: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

The Lin ux System Administrator s’Guide

Version 0.6.2

Lars [email protected]

Joanna [email protected]

Page 2: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

The Lin ux System Administrator s’ Guide: Version 0.6.2by LarsWirzenius

by JoannaOja

An introductionto systemadministrationof aLinux systemfor novices.

Copyright 1993–1998LarsWirzenius.

Trademarksareownedby theirowners.

Permissionis grantedto makeanddistributeverbatimcopiesof thismanualprovidedthecopyright noticeandthispermissionnoticearepreserved

onall copies.

Permissionis grantedto processthedocumentsourcecodethroughTeX or otherformattersandprint theresults,anddistributetheprinted

document,providedtheprinteddocumentcarriescopying permissionnoticeidenticalto thisone,includingthereferencesto wherethesource

codecanbefoundandtheofficial homepage.

Permissionis grantedto copy anddistributemodifiedversionsof this manualundertheconditionsfor verbatimcopying, providedthattheentire

resultingderivedwork is distributedunderthetermsof a permissionnoticeidenticalto this one.

Permissionis grantedto copy anddistributetranslationsof this manualinto anotherlanguage,undertheabove conditionsfor modifiedversions.

Theauthorwould appreciatea notificationof modifications,translations,andprintedversions.Thankyou.

Page 3: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Table of ContentsDedication...................................................................................................................................................7

Sourceand pre-formatted versionsavailable..........................................................................................8

1. Intr oduction............................................................................................................................................9

TheLinux DocumentationProject...................................................................................................11

2. Overview of a Linux System...............................................................................................................12

Variouspartsof anoperatingsystem...............................................................................................12Importantpartsof thekernel............................................................................................................12Major servicesin aUNIX system....................................................................................................14

init ..........................................................................................................................................14Loginsfrom terminals............................................................................................................14Syslog.....................................................................................................................................15Periodiccommandexecution:cron andat.............................................................................15Graphicaluserinterface..........................................................................................................15Networking.............................................................................................................................15Network logins.......................................................................................................................16Network file systems..............................................................................................................16Mail.........................................................................................................................................16Printing...................................................................................................................................17Thefilesystemlayout..............................................................................................................17

3. Overview of the Dir ectory Tree..........................................................................................................19

Background......................................................................................................................................19Theroot filesystem...........................................................................................................................21The/etc directory..........................................................................................................................23The/dev directory..........................................................................................................................25The/usr filesystem.........................................................................................................................26The/var filesystem.........................................................................................................................27The/proc filesystem......................................................................................................................28

4. Using Disks and Other StorageMedia...............................................................................................32

Two kindsof devices........................................................................................................................32Harddisks........................................................................................................................................33Floppies............................................................................................................................................36CD-ROM’s.......................................................................................................................................37Tapes................................................................................................................................................38Formatting........................................................................................................................................38Partitions..........................................................................................................................................41

TheMBR, bootsectorsandpartitiontable.............................................................................41

3

Page 4: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Extendedandlogicalpartitions..............................................................................................42Partition types.........................................................................................................................43Partitioninga harddisk...........................................................................................................44Devicefilesandpartitions......................................................................................................45

Filesystems.......................................................................................................................................45Whatarefilesystems?.............................................................................................................45Filesystemsgalore..................................................................................................................46Whichfilesystemshouldbeused?..........................................................................................49Creatinga filesystem..............................................................................................................49Mountingandunmounting.....................................................................................................51Checkingfilesystemintegrity with fsck.................................................................................55Checkingfor diskerrorswith badblocks...............................................................................55Fightingfragmentation...........................................................................................................56Othertoolsfor all filesystems.................................................................................................56Othertoolsfor theext2 filesystem..........................................................................................57

Diskswithout filesystems.................................................................................................................58Allocatingdiskspace.......................................................................................................................59

Partitioningschemes...............................................................................................................59Spacerequirements.................................................................................................................60Examplesof harddiskallocation...........................................................................................60Addingmorediskspacefor Linux .........................................................................................61Tips for saving diskspace.......................................................................................................61

5. Memory Management.........................................................................................................................63

Whatis virtual memory?..................................................................................................................63Creatinga swapspace......................................................................................................................63Usingaswapspace..........................................................................................................................64Sharingswapspaceswith otheroperatingsystems..........................................................................66Allocatingswapspace......................................................................................................................66Thebuffer cache...............................................................................................................................67

6. BootsAnd Shutdowns..........................................................................................................................70

An overview of bootsandshutdowns..............................................................................................70Thebootprocessin closerlook........................................................................................................71Moreaboutshutdowns.....................................................................................................................73Rebooting.........................................................................................................................................74Singleusermode..............................................................................................................................75Emergency bootfloppies..................................................................................................................75

7. init ..........................................................................................................................................................76

init comesfirst .................................................................................................................................76Configuringinit to startgetty: the/etc/inittab file ..................................................................76

4

Page 5: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Runlevels.........................................................................................................................................78Specialconfigurationin /etc/inittab.........................................................................................79Bootingin singleusermode.............................................................................................................79

8. Logging In And Out.............................................................................................................................81

Loginsvia terminals.........................................................................................................................81Loginsvia thenetwork.....................................................................................................................83What login does...............................................................................................................................84X andxdm........................................................................................................................................84Accesscontrol..................................................................................................................................84Shellstartup......................................................................................................................................85

9. Managing useraccounts......................................................................................................................86

What’sanaccount?..........................................................................................................................86Creatinga user.................................................................................................................................86

/etc/passwd andotherinformativefiles.............................................................................86Pickingnumericuserandgroupids.......................................................................................87Initial environment:/etc/skel ............................................................................................87Creatinga userby hand..........................................................................................................88

Changinguserproperties.................................................................................................................89Removing a user...............................................................................................................................89Disablinga usertemporarily............................................................................................................90

10.Backups...............................................................................................................................................92

Ontheimportanceof beingbackedup ............................................................................................92Selectingthebackupmedium..........................................................................................................93Selectingthebackuptool .................................................................................................................93Simplebackups................................................................................................................................94

Makingbackupswith tar .......................................................................................................94Restoringfiles with tar ...........................................................................................................96

Multilevel backups...........................................................................................................................97Whatto backup...............................................................................................................................99Compressedbackups........................................................................................................................99

11.KeepingTime ...................................................................................................................................101

Timezones.....................................................................................................................................101Thehardwareandsoftwareclocks.................................................................................................102Showing andsettingtime...............................................................................................................102Whentheclock is wrong................................................................................................................103

Glossary(DRAFT) .................................................................................................................................105

5

Page 6: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

List of Tables4-1.Partition types(from theLinux fdisk program).................................................................................437-1.Runlevel numbers..............................................................................................................................7810-1.Efficientbackupschemeusingmany backuplevels.........................................................................98

List of Figures2-1.Someof themoreimportantpartsof theLinux kernel......................................................................123-1.Partsof a Unix directorytree.Dashedlinesindicatepartitionlimits.................................................194-1.A schematicpictureof a harddisk.....................................................................................................344-2.A sampleharddisk partitioning.........................................................................................................424-3.Threeseparatefilesystems..................................................................................................................514-4./home and/usr havebeenmounted.................................................................................................514-5.Sampleoutputfrom dumpe2fs..........................................................................................................578-1.Loginsvia terminals:theinteractionof init , getty, login, andtheshell............................................8110-1.A samplemultilevel backupschedule..............................................................................................97

6

Page 7: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

DedicationThis placeis dedicatedto a futurededication.

7

Page 8: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Sour ce and pre-f ormatted versions availab leThesourcecodeandandothermachinereadableformatsof this bookcanbefoundon theInternetviaanonymousFTPat theLinux DocumentationProjecthomepagehttp://sunsite.unc.edu/LDP/,or at thehomepageof thisbookathttp://www.iki.fi/viu/linux/sag/.Availableareat leastPostScriptandTeX .DVIformats.

8

Page 9: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 1. Intr oduction

“In thebeginning,thefile waswithout form, andvoid; andemptinesswasuponthefaceof thebits.And theFingersof theAuthor moveduponthefaceof thekeyboard.And theAuthorsaid,Let therebewords,andtherewerewords.”

This manual,theLinux SystemAdministrators’Guide,describesthesystemadministrationaspectsofusingLinux. It is intendedfor peoplewhoknow next to nothingaboutsystemadministration(asin“what is it?”), but whohavealreadymasteredat leastthebasicsof normalusage.This manualalsodoesn’t tell youhow to install Linux; thatis describedin theInstallationandGettingStarteddocument.Seebelow for moreinformationaboutLinux manuals.

Systemadministrationis all thethingsthatonehasto do to keepacomputersystemin auseableshape.Itincludesthingslike backingupfiles (andrestoringthemif necessary),installingnew programs,creatingaccountsfor users(anddeletingthemwhenno longerneeded),makingcertainthatthefilesystemis notcorrupted,andsoon. If acomputerwere,say, a house,systemadministrationwouldbecalledmaintenance,andwould includecleaning,fixing brokenwindows,andothersuchthings.Systemadministrationis not calledmaintenance,becausethatwouldbetoo simple.1

Thestructureof this manualis suchthatmany of thechaptersshouldbeusableindependently, sothatifyou needinformationabout,say, backups,youcanreadjust thatchapter. This hopefullymakesthebookeasierto useasa referencemanual,andmakesit possibleto readjust asmallpartwhenneeded,insteadof having to readeverything.However, thismanualis first andforemosta tutorial,andareferencemanualonly asa lucky coincidence.

This manualis not intendedto beusedcompletelyby itself. Plentyof therestof theLinuxdocumentationis alsoimportantfor systemadministrators.After all, asystemadministratoris just a userwith specialprivilegesandduties.A very importantresourcearethemanualpages,whichshouldalwaysbeconsultedwhena commandis not familiar.

While this manualis targetedatLinux, a generalprinciplehasbeenthatit shouldbeusefulwith otherUNIX basedoperatingsystemsaswell. Unfortunately, sincethereis somuchvariancebetweendifferentversionsof UNIX in general,andin systemadministrationin particular, thereis little hopeto coverallvariants.Evencoveringall possibilitiesfor Linux is difficult, dueto thenatureof its development.

Thereis no oneofficial Linux distribution,sodifferentpeoplehavedifferentsetups,andmany peoplehaveasetupthey havebuilt up themselves.Thisbookis not targetedatany onedistribution,eventhoughI usetheDebianGNU/Linux systemalmostexclusively. Whenpossible,I have tried to pointoutdifferences,andexplainseveralalternatives.

I havetried to describehow thingswork, ratherthanjust listing “fi veeasysteps”for eachtask.Thismeansthatthereis muchinformationherethatis not necessaryfor everyone,but thosepartsaremarked

9

Page 10: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter1. Introduction

assuchandcanbeskippedif you usea preconfiguredsystem.Readingeverythingwill, naturally,increaseyourunderstandingof thesystemandshouldmakeusingandadministeringit morepleasant.

Likeall otherLinux relateddevelopment,thework wasdoneon a volunteerbasis:I did it becauseIthoughtit mightbefun andbecauseI felt it shouldbedone.However, likeall volunteerwork, thereis alimit to how mucheffort I havebeenableto spend,andalsoon how muchknowledgeandexperienceIhave.Thismeansthatthemanualis not necessarilyasgoodasit wouldbeif awizardhadbeenpaidhandsomelyto write it andhadspenta few yearsto perfectit. I think, of course,thatit is prettynice,butbewarned.

Oneparticularpoint whereI havecutcornersis thatI havenot coveredvery thoroughlymany thingsthatarealreadywell documentedin otherfreely availablemanuals.Thisappliesespeciallyto programspecificdocumentation,suchasall thedetailsof usingmkfs. I only describethepurposeof theprogram,andasmuchof its usageasis necessaryfor thepurposesof this manual.For furtherinformation,I referthegentlereaderto theseothermanuals.Usually, all of thereferredto documentationis partof thefullLinux documentationset.

Larshastried to make this manualasgoodaspossibleandI would like,asa currentmaintainer, to keepup thegoodwork. I would really like to hearfrom you if you haveany ideason how to make it better.Badlanguage,factualerrors,ideasfor new areasto cover, rewrittensections,informationabouthowvariousUNIX versionsdo things,I aminterestedin all of it. My contactinformationis availablevia theWorld Wide Webat http://www.iki.fi/viu/ (http://www.iki.fi/viu/).

Many peoplehavehelpedmewith this book,directlyor indirectly. I would like to especiallythankMattWelshfor inspirationandLDP leadership,Andy Oramfor gettingmeto work againwith much-valuedfeedback,Olaf Kirch for showing methatit canbedone,andAdamRichterat Yggdrasilandothersforshowing methatotherpeoplecanfind it interestingaswell.

StephenTweedie,H. PeterAnvin, RemyCard,TheodoreTs’o, andStephenTweediehave let meborrowtheirwork (andthusmake thebooklook thickerandmuchmoreimpressive):a comparisonbetweenthexia andext2 filesystems,thedevice list andadescriptionof theext2 filesystem.Thesearen’t partof thebookany more.I ammostgratefulfor this,andveryapologeticfor theearlierversionsthatsometimeslackedproperattribution.

In addition,I would like to thankMark Komarinskifor sendinghismaterialin 1993andthemany systemadministrationcolumnsin Linux Journal.They arequiteinformativeandinspirational.

Many usefulcommentshavebeensentby a largenumberof people.My miniatureblackholeof anarchivedoesn’t let mefind all their names,but someof themare,in alphabeticalorder:PaulCaprioli,AlesCepek,Marie-FranceDeclerfayt,DaveDobson,Olaf Flebbe,HelmutGeyer, Larry Greenfieldandhis father, StephenHarris,Jyrki Havia, Jim Haynes,York Lam,TimothyAndrew Lister, JimLynch,MichaelJ.Micek, JacobNavia, DanPoirier, DanielQuinlan,JouniK Seppänen,PhilippeSteindl,G.B.Stotte.My apologiesto anyoneI haveforgotten.

META needto addtypographicalconventsionsandLDP blurbhere.

10

Page 11: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter1. Introduction

The Lin ux Documentation ProjectTheLinux DocumentationProject,or LDP, is a looseteamof writers,proofreaders,andeditorswho areworking togetherto providecompletedocumentationfor theLinux operatingsystem.Theoverallcoordinatorof theprojectis Greg Hankins.

This manualis onein a setof severalbeingdistributedby theLDP, includinga Linux Users’Guide,SystemAdministrators’Guide,Network Administrators’Guide,andKernelHackers’Guide.Thesemanualsareall availablein sourceformat,.dvi format,andpostscriptoutputby anonymousFTPfromsunsite.unc.edu,in thedirectory/pub/Linux/docs/LDP.

We encourageanyonewith a penchantfor writing or editingto join usin improving Linuxdocumentation.If you haveInternete-mailaccess,youcancontactGreg Hankinsat<[email protected]>.

Notes1. Therearesomepeoplewhodo call it that,but that’s justbecausethey havenever readthis manual,

poorthings.

11

Page 12: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 2. Overview of a Lin ux System

“God lookedover everythinghehadmade,andsaw thatit wasverygood.” (Genesis1:31)

This chaptergivesanoverview of aLinux system.First, themajorservicesprovidedby theoperatingsystemaredescribed.Then,theprogramsthatimplementtheseservicesaredescribedwith aconsiderablelackof detail.Thepurposeof this chapteris to giveanunderstandingof thesystemasawhole,sothateachpartis describedin detailelsewhere.

Various par ts of an operating systemA UNIX operatingsystemconsistsof a kernelandsomesystemprograms. Therearealsosomeapplicationprogramsfor doingwork. Thekernelis theheartof theoperatingsystem.1 It keepstrackoffileson thedisk,startsprogramsandrunsthemconcurrently, assignsmemoryandotherresourcestovariousprocesses,receivespacketsfrom andsendspacketsto thenetwork, andsoon.Thekerneldoesvery little by itself, but it providestoolswith which all servicescanbebuilt. It alsopreventsanyonefromaccessingthehardwaredirectly, forcingeveryoneto usethetoolsit provides.This way thekernelprovidessomeprotectionfor usersfrom eachother. Thetoolsprovidedby thekernelareusedvia systemcalls; seemanualpagesection2 for more informationon these.

Thesystemprogramsusethetoolsprovidedby thekernelto implementthevariousservicesrequiredfrom anoperatingsystem.Systemprograms,andall otherprograms,run ‘on topof thekernel’, in whatiscalledtheusermode. Thedifferencebetweensystemandapplicationprogramsis oneof intent:applicationsareintendedfor gettingusefulthingsdone(or for playing,if it happensto bea game),whereassystemprogramsareneededto getthesystemworking.A wordprocessoris anapplication;telnet is a systemprogram.Thedifferenceis oftensomewhatblurry, however, andis importantonly tocompulsivecategorizers.

An operatingsystemcanalsocontaincompilersandtheir correspondinglibraries(GCCandtheC libraryin particularunderLinux), althoughnotall programminglanguagesneedbepartof theoperatingsystem.Documentation,andsometimesevengames,canalsobepartof it. Traditionally, theoperatingsystemhasbeendefinedby thecontentsof theinstallationtapeor disks;with Linux it is not asclearsinceit isspreadall over theFTPsitesof theworld.

Impor tant par ts of the kernelTheLinux kernelconsistsof severalimportantparts:processmanagement,memorymanagement,hardwaredevicedrivers,filesystemdrivers,network management,andvariousotherbits andpieces.

12

Page 13: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter2. Overview of a LinuxSystem

Figure2-1showssomeof them.

Figure2-1.Someof the more important parts of the Linux kernel

System call interface

Virtual filesystem

management

IDE harddisk

driver

Floppy diskIDE hard disk

Various filesystem

drivers

Floppy disk

driver

Memory

manager

Process

manager

Ethernet card

Abstract network

services (sockets)

TCP/IP protocol

drivers

Ethernet card

driver

Hardware

Kernel

Normal programs

Kernel

User level programs

Probablythemostimportantpartsof thekernel(nothingelseworkswithout them)arememorymanagementandprocessmanagement.Memorymanagementtakescareof assigningmemoryareasandswapspaceareasto processes,partsof thekernel,andfor thebuffer cache.Processmanagementcreatesprocesses,andimplementsmultitaskingby switchingtheactiveprocesson theprocessor.

At thelowestlevel, thekernelcontainsa hardwaredevicedriver for eachkind of hardwareit supports.Sincetheworld is full of differentkindsof hardware,thenumberof hardwaredevicedriversis large.

13

Page 14: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter2. Overview of a LinuxSystem

Thereareoftenmany otherwisesimilarpiecesof hardwarethatdiffer in how they arecontrolledbysoftware.Thesimilaritiesmake it possibleto havegeneralclassesof driversthatsupportsimilaroperations;eachmemberof theclasshasthesameinterfaceto therestof thekernelbut differsin whatitneedsto do to implementthem.For example,all diskdriverslook alike to therestof thekernel,i.e., theyall haveoperationslike ‘initialize thedrive’, ‘readsectorN’, and‘write sectorN’.

Somesoftwareservicesprovidedby thekernelitself havesimilar properties,andcanthereforebeabstractedinto classes.For example,thevariousnetwork protocolshavebeenabstractedinto oneprogramminginterface,theBSD socket library. Anotherexampleis thevirtual filesystem(VFS) layerthatabstractsthefilesystemoperationsaway from their implementation.Eachfilesystemtypeprovidesanimplementationof eachfilesystemoperation.Whensomeentity triesto usea filesystem,therequestgoesvia theVFS,which routestherequestto theproperfilesystemdriver.

Major services in a UNIX systemThis sectiondescribessomeof themoreimportantUNIX services,but withoutmuchdetail.They aredescribedmorethoroughlyin laterchapters.

initThesinglemostimportantservicein aUNIX systemis providedby init . init is startedasthefirst processof everyUNIX system,asthelastthing thekerneldoeswhenit boots.Wheninit starts,it continuesthebootprocessby doingvariousstartupchores(checkingandmountingfilesystems,startingdaemons,etc).

Theexactlist of thingsthat init doesdependsonwhich flavor it is; thereareseveralto choosefrom. initusuallyprovidestheconceptof singleusermode, in which noonecanlog in androotusesa shellat theconsole;theusualmodeis calledmultiusermode. Someflavorsgeneralizethis asrun levels; singleandmultiusermodesareconsideredto betwo run levels,andtherecanbeadditionalonesaswell, forexample,to run X on theconsole.

In normaloperation,init makessuregetty is working (to allow usersto log in), andto adoptorphanprocesses(processeswhoseparenthasdied;in UNIX all processesmustbein asingletree,soorphansmustbeadopted).

Whenthesystemis shutdown, it is init thatis in chargeof killing all otherprocesses,unmountingallfilesystemsandstoppingtheprocessor, alongwith anythingelseit hasbeenconfiguredto do.

Logins from terminals

14

Page 15: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter2. Overview of a LinuxSystem

Loginsfrom terminals(via seriallines)andtheconsole(whennot runningX) areprovidedby thegettyprogram.init startsaseparateinstanceof getty for eachterminalfor which loginsareto beallowed.getty readstheusernameandrunsthe login program,which readsthepassword. If theusernameandpassword arecorrect,login runstheshell.Whentheshell terminates,i.e., theuserlogsout,or whenlogin terminatedbecausetheusernameandpassword didn’t match,init noticesthisandstartsa newinstanceof getty. Thekernelhasno notionof logins,this is all handledby thesystemprograms.

SyslogThekernelandmany systemprogramsproduceerror, warning,andothermessages.It is oftenimportantthatthesemessagescanbeviewedlater, evenmuchlater, sothey shouldbewritten to afile. Theprogramdoingthis is syslog. It canbeconfiguredto sortthemessagesto differentfilesaccordingto writer ordegreeof importance.For example,kernelmessagesareoftendirectedto a separatefile from theothers,sincekernelmessagesareoftenmoreimportantandneedto bereadregularly to spotproblems.

Periodic command execution: cron and atBothusersandsystemadministratorsoftenneedto runcommandsperiodically. For example,thesystemadministratormightwantto runa commandto cleanthedirectorieswith temporaryfiles (/tmp and/var/tmp) from old files, to keepthedisksfrom filling up,sincenot all programscleanup afterthemselvescorrectly.

Thecron serviceis setup to do this.Eachuserhasacrontab file, wherehelists thecommandshewantsto executeandthetimesthey shouldbeexecuted.Thecron daemontakescareof startingthecommandswhenspecified.

Theat serviceis similar to cron, but it is onceonly: thecommandis executedat thegiventime,but it isnot repeated.

Graphical user interfaceUNIX andLinux don’t incorporatetheuserinterfaceinto thekernel;instead,they let it beimplementedby userlevel programs.Thisappliesfor bothtext modeandgraphicalenvironments.

Thisarrangementmakesthesystemmoreflexible, but hasthedisadvantagethatit is simpleto implementa differentuserinterfacefor eachprogram,makingthesystemharderto learn.

Thegraphicalenvironmentprimarily usedwith Linux is calledtheX Window System(X for short).Xalsodoesnot implementa userinterface;it only implementsa window system,i.e., toolswith whicha

15

Page 16: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter2. Overview of a LinuxSystem

graphicaluserinterfacecanbeimplemented.ThethreemostpopularuserinterfacestylesimplementedoverX areAthena,Motif, andOpenLook.

NetworkingNetworking is theactof connectingtwo or morecomputerssothatthey cancommunicatewith eachother. Theactualmethodsof connectingandcommunicatingareslightly complicated,but theendresultis veryuseful.

UNIX operatingsystemshavemany networking features.Most basicservices(filesystems,printing,backups,etc)canbedoneover thenetwork. Thiscanmakesystemadministrationeasier, sinceit allowscentralizedadministration,while still reapingin thebenefitsof microcomputinganddistributedcomputing,suchaslowercostsandbetterfault tolerance.

However, this bookmerelyglancesat networking;seetheLinuxNetworkAdministrators’ Guideformoreinformation,includingabasicdescriptionof how networksoperate.

Network loginsNetwork loginswork a little differentlythannormallogins.Thereis a separatephysicalserialline foreachterminalvia which it is possibleto log in. For eachpersonloggingin via thenetwork, thereis aseparatevirtual network connection,andtherecanbeany numberof these.2 It is thereforenot possibleto runa separategetty for eachpossiblevirtual connection.Therearealsoseveraldifferentwaysto login via anetwork, telnet andrlogin beingthemajoronesin TCP/IPnetworks.

Network loginshave, insteadof aherdof gettys,a singledaemonperwayof loggingin (telnet andrlogin haveseparatedaemons)thatlistensfor all incominglogin attempts.Whenit noticesone,it startsanew instanceof itself to handlethatsingleattempt;theoriginal instancecontinuesto listenfor otherattempts.Thenew instanceworkssimilarly to getty.

Network file systemsOneof themoreusefulthingsthatcanbedonewith networkingservicesis sharingfilesvia anetworkfilesystem. Theoneusuallyusedis calledtheNetwork File System,or NFS,developedby Sun.

With anetwork file systemany file operationsdoneby a programon onemachinearesentover thenetwork to anothercomputer. This fools theprogramto think thatall thefileson theothercomputerareactuallyon thecomputertheprogramis runningon.Thismakesinformationsharingextremelysimple,sinceit requiresnomodificationsto programs.

16

Page 17: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter2. Overview of a LinuxSystem

MailElectronicmail is usuallythemostimportantmethodfor communicatingvia computer. An electronicletteris storedin afile usinga specialformat,andspecialmail programsareusedto sendandreadtheletters.

Eachuserhasan incomingmailbox(afile in thespecialformat),whereall new mail is stored.Whensomeonesendsmail, themail programlocatesthereceiver’smailboxandappendstheletterto themailboxfile. If thereceiver’smailboxis in anothermachine,theletteris sentto theothermachine,whichdeliversit to themailboxasit bestseesfit.

Themail systemconsistsof many programs.Thedeliveryof mail to localor remotemailboxesis doneby oneprogram(themail transferagentor MTA, e.g.,sendmailor smail), while theprogramsusersusearemany andvaried(mail useragentor MUA, e.g.,pine or elm). Themailboxesareusuallystoredin/var/spool/mail.

PrintingOnly onepersoncanusea printerat onetime,but it is uneconomicalnot to shareprintersbetweenusers.Theprinteris thereforemanagedby softwarethatimplementsa print queue: all print jobsareput into aqueueandwhenevertheprinteris donewith onejob, thenext oneis sentto it automatically. This relievestheusersfrom organizingtheprint queueandfightingovercontrolof theprinter. 3

Theprint queuesoftwarealsospoolstheprintoutson disk, i.e., thetext is keptin a file while thejob is inthequeue.This allowsanapplicationprogramto spit out theprint jobsquickly to theprint queuesoftware;theapplicationdoesnot haveto wait until thejob is actuallyprintedto continue.This is reallyconvenient,sinceit allowsoneto print out oneversion,andnothave to wait for it to beprintedbeforeonecanmakeacompletelyrevisednew version.

The filesystem layoutThefilesystemis dividedinto many parts;usuallyalongthelinesof a rootfilesystemwith /bin, /lib,/etc, /dev, anda few others;a/usr filesystemwith programsandunchangingdata;a/var filesystemwith changingdata(suchaslog files); anda/home filesystemfor everyone’spersonalfiles.Dependingon thehardwareconfigurationandthedecisionsof thesystemadministrator, thedivisioncanbedifferent;it canevenbeall in onefilesystem.

Chapter3 describesthefilesystemlayoutin somedetail;theLinux FilesystemStandardcoversit insomewhatmoredetail.

17

Page 18: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter2. Overview of a LinuxSystem

Notes1. In fact,it is oftenmistakenlyconsideredto betheoperatingsystemitself, but it is not.An operating

systemprovidesmany moreservicesthana plain kernel.

2. Well, at leasttherecanbemany. Network bandwidthstill beinga scarceresource,thereis still somepracticalupperlimit to thenumberof concurrentloginsvia onenetwork connection.

3. Instead,they form a new queueat theprinter, waiting for their printouts,sinceno oneeverseemstobeableto getthequeuesoftwareto know exactly whenanyone’sprintoutis reallyfinished.This is agreatboostto intra-officesocialrelations.

18

Page 19: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 3. Overview of the Director y Tree

“ Two dayslater, therewasPooh,sitting onhis branch,danglinghis legs,andthere,besidehim, werefour potsof honey...” (A.A. Milne)

This chapterdescribestheimportantpartsof a standardLinux directorytree,basedon theFSSTNDfilesystemstandard.It outlinesthenormalwayof breakingthedirectorytreeinto separatefilesystemswith differentpurposesandgivesthemotivationbehindthisparticularsplit. Somealternativewaysofsplittingarealsodescribed.

Backgr oundThis chapteris looselybasedon theLinuxfilesystemstandard, FSSTND,version1.2(seethebibliography),which attemptsto seta standardfor how thedirectorytreein aLinux systemis organized.Sucha standardhastheadvantagethatit will beeasierto write or port softwarefor Linux, andtoadministerLinux machines,sinceeverythingwill bein their usualplaces.Thereis no authoritybehindthestandardthatforcesanyoneto complywith it, but it hasgot thesupportof most,if notall, Linuxdistributions.It is not agoodideato breakwith theFSSTNDwithout verycompellingreasons.TheFSSTNDattemptsto follow Unix traditionandcurrenttrends,makingLinux systemsfamiliar to thosewith experiencewith otherUnix systems,andviceversa.

This chapteris not asdetailedastheFSSTND.A systemadministratorshouldalsoreadtheFSSTNDfora completeunderstanding.

This chapterdoesnot explainall files in detail.Theintentionis not to describeeveryfile, but to giveanoverview of thesystemfrom a filesystempoint of view. Furtherinformationon eachfile is availableelsewherein this manualor themanualpages.

Thefull directorytreeis intendedto bebreakableinto smallerparts,eachon its own diskor partition,toaccomodateto disksizelimits andto easebackupandothersystemadministration.Themajorpartsaretheroot,/usr, /var, and/home filesystems(seeFigure3-1).Eachparthasa differentpurpose.Thedirectorytreehasbeendesignedsothatit workswell in a network of Linux machineswhich maysharesomepartsof thefilesystemsovera read-onlydevice (e.g.,a CD-ROM), or over thenetwork with NFS.

19

Page 20: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

Figure3-1.Parts of a Unix dir ectory tr ee.Dashedlines indicate partition limits.

home devbin proc lib etc varusr boot

ftp liw linus log tmprunlib spoolbin lib man tmp

/

Therolesof thedifferentpartsof thedirectorytreearedescribedbelow.

• Therootfilesystemis specificfor eachmachine(it is generallystoredona localdisk,althoughit couldbea ramdiskor network driveaswell) andcontainsthefiles thatarenecessaryfor bootingthesystemup,andto bring it up to suchastatethattheotherfilesystemsmaybemounted.Thecontentsof therootfilesystemwill thereforebesufficient for thesingleuserstate.It will alsocontaintoolsfor fixing abrokensystem,andfor recoveringlost files from backups.

• The/usr filesystemcontainsall commands,libraries,manualpages,andotherunchangingfilesneededduringnormaloperation.No files in /usr shouldbespecificfor any givenmachine,norshouldthey bemodifiedduringnormaluse.Thisallows thefiles to besharedover thenetwork, whichcanbecost-effectivesinceit savesdiskspace(therecaneasilybehundredsof megabytesin /usr),andcanmakeadministrationeasier(only themaster/usr needsto bechangedwhenupdatinganapplication,not eachmachineseparately).Evenif thefilesystemis ona localdisk, it couldbemountedread-only, to lessenthechanceof filesystemcorruptionduringacrash.

• The/var filesystemcontainsfiles thatchange,suchasspooldirectories(for mail, news,printers,etc),log files, formattedmanualpages,andtemporaryfiles.Traditionallyeverythingin /var hasbeensomewherebelow /usr, but thatmadeit impossibleto mount/usr read-only.

• The/home filesystemcontainstheusers’homedirectories,i.e.,all therealdataon thesystem.Separatinghomedirectoriesto their own directorytreeor filesystemmakesbackupseasier;theotherpartsoftendo nothave to bebackedup,or at leastnotasoften(they seldomchange).A big /home

might haveto bebrokenon severalfilesystems,which requiresaddinganextranaminglevel below

20

Page 21: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

/home, e.g.,/home/students and/home/staff.

Althoughthedifferentpartshavebeencalledfilesystemsabove,thereis norequirementthattheyactuallybeon separatefilesystems.They couldeasilybekeptin a singleoneif thesystemis asmallsingle-usersystemandtheuserwantsto keepthingssimple.Thedirectorytreemightalsobedividedintofilesystemsdifferently, dependingon how largethedisksare,andhow spaceis allocatedfor variouspurposes.Theimportantpart,though,is thatall thestandardnameswork; evenif, say, /var and/usrareactuallyon thesamepartition,thenames/usr/lib/libc.a and/var/log/messages mustwork,for exampleby moving filesbelow /var into /usr/var, andmaking/var a symlink to /usr/var.

TheUnix filesystemstructuregroupsfiles accordingto purpose,i.e.,all commandsarein oneplace,alldatafiles in another, documentationin a third, andsoon.An alternativewould beto groupfilesfilesaccordingto theprogramthey belongto, i.e.,all Emacsfileswould bein onedirectory, all TeX inanother, andsoon.Theproblemwith thelatterapproachis thatit makesit difficult to sharefiles (theprogramdirectoryoftencontainsbothstaticandshareableandchangingandnon-shareablefiles),andsometimesto evenfind thefiles (e.g.,manualpagesin a hugenumberof places,andmakingthemanualpageprogramsfind all of themis amaintenancenightmare).

The root filesystemTheroot filesystemshouldgenerallybesmall,sinceit containsverycritical filesanda small,infrequentlymodifiedfilesystemhasa betterchanceof not gettingcorrupted.A corruptedrootfilesystemwill generallymeanthatthesystembecomesunbootableexceptwith specialmeasures(e.g.,from afloppy), soyou don’t wantto risk it.

Theroot directorygenerallydoesn’t containany files,exceptperhapsthestandardbootimagefor thesystem,usuallycalled/vmlinuz. All otherfiles arein subdirectoriesin theroot filesystems:

/bin

Commandsneededduringbootupthatmight beusedby normalusers(probablyafterbootup).

/sbin

Like/bin, but thecommandsarenot intendedfor normalusers,althoughthey mayusethemifnecessaryandallowed.

/etc

21

Page 22: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

Configurationfilesspecificto themachine.

/root

Thehomedirectoryfor userroot.

/lib

Sharedlibrariesneededby theprogramson therootfilesystem.

/lib/modules

Loadablekernelmodules,especiallythosethatareneededto bootthesystemwhenrecoveringfromdisasters(e.g.,network andfilesystemdrivers).

/dev

Devicefiles.

/tmp

Temporaryfiles.Programsrunningafterbootupshoulduse/var/tmp, not/tmp, sincetheformeris probablyon a diskwith morespace.

/boot

Filesusedby thebootstraploader, e.g.,LILO. Kernelimagesareoftenkepthereinsteadof in therootdirectory. If therearemany kernelimages,thedirectorycaneasilygrow ratherbig, andit mightbebetterto keepit in aseparatefilesystem.Anotherreasonwould beto makesurethekernelimagesarewithin thefirst 1024cylindersof anIDE disk.

/mnt

Mountpoint for temporarymountsby thesystemadministrator. Programsaren’t supposedto mounton/mnt automatically. /mnt might bedividedinto subdirectories(e.g.,/mnt/dosa might bethefloppy driveusinganMS-DOSfilesystem,and/mnt/exta mightbethesamewith anext2filesystem).

22

Page 23: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

/proc, /usr, /var, /home

Mountpointsfor theotherfilesystems.

The /etc director yThe/etc directorycontainsa lot of files.Someof themaredescribedbelow. For others,youshoulddeterminewhich programthey belongto andreadthemanualpagefor thatprogram.Many networkingconfigurationfilesarein /etc aswell, andaredescribedin theNetworkingAdministrators’ Guide.

/etc/rc or /etc/rc.d or /etc/rc?.d

Scriptsor directoriesof scriptsto runat startupor whenchangingtherun level. Seethechapteroninit for furtherinformation.

/etc/passwd

Theuserdatabase,with fieldsgiving theusername,realname,homedirectory, encryptedpassword,andotherinformationabouteachuser. Theformatis documentedin thepasswdmanualpage.

/etc/fdprm

Floppy diskparametertable.Describeswhatdifferentfloppy disk formatslook like.Usedbysetfdprm. Seethesetfdprm manualpagefor moreinformation.

/etc/fstab

Lists thefilesystemsmountedautomaticallyatstartupby themount -a command(in /etc/rc orequivalentstartupfile). UnderLinux, alsocontainsinformationaboutswapareasusedautomaticallyby swapon-a. SeethesectioncalledMountingandunmountingin Chapter4 andthemountmanualpagefor moreinformation.

/etc/group

23

Page 24: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

Similar to /etc/passwd, but describesgroupsinsteadof users.Seethegroup manualpageformoreinformation.

/etc/inittab

Configurationfile for init .

/etc/issue

Outputby getty beforethelogin prompt.Usuallycontainsashortdescriptionor welcomingmessageto thesystem.Thecontentsareup to thesystemadministrator.

/etc/magic

Theconfigurationfile for file. Containsthedescriptionsof variousfile formatsbasedon whichfileguessesthetypeof thefile. Seethemagic andfile manualpagesfor moreinformation.

/etc/motd

Themessageof theday, automaticallyoutputafterasuccessfullogin. Contentsareup to thesystemadministrator. Oftenusedfor gettinginformationto everyuser, suchaswarningsaboutplanneddowntimes.

/etc/mtab

List of currentlymountedfilesystems.Initially setup by thebootupscripts,andupdatedautomaticallyby themount command.Usedwhena list of mountedfilesystemsis needed,e.g.,bythedf command.

/etc/shadow

Shadow password file on systemswith shadow passwordsoftwareinstalled.Shadow passwordsmovetheencryptedpassword from /etc/passwd into /etc/shadow; thelatteris not readablebyanyoneexceptroot.Thismakesit harderto crackpasswords.

/etc/login.defs

24

Page 25: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

Configurationfile for the login command.

/etc/printcap

Like/etc/termcap, but intendedfor printers.Differentsyntax.

/etc/profile, /etc/csh.login, /etc/csh.cshrc

Filesexecutedat login or startuptimeby theBourneor C shells.Theseallow thesystemadministratorto setglobaldefaultsfor all users.Seethemanualpagesfor therespectiveshells.

/etc/securetty

Identifiessecureterminals,i.e., theterminalsfrom which root is allowedto log in. Typically onlythevirtual consolesarelisted,sothatit becomesimpossible(or at leastharder)to gainsuperuserprivilegesby breakinginto a systemovera modemor a network.

/etc/shells

Lists trustedshells.Thechshcommandallowsusersto changetheir login shellonly to shellslistedin thisfile. ftpd , theserverprocessthatprovidesFTPservicesfor amachine,will checkthattheuser’s shellis listedin /etc/shells andwill not let peoplelog in unlestheshell is listedthere.

/etc/termcap

Theterminalcapabilitydatabase.Describesby what“escapesequences”variousterminalscanbecontrolled.Programsarewrittensothatinsteadof directly outputtinganescapesequencethatonlyworkson a particularbrandof terminal,they look up thecorrectsequenceto do whatever it is theywantto do in /etc/termcap. As a resultmostprogramswork with mostkindsof terminals.Seethetermcap, curs_termcap,andterminfo manualpagesfor moreinformation.

The /dev director y

25

Page 26: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

The/dev directorycontainsthespecialdevicefiles for all thedevices.Thedevicefilesarenamedusingspecialconventions;thesearedescribedin theDevicelist (seeXXX). Thedevicefiles arecreatedduringinstallation,andlaterwith the/dev/MAKEDEV script.The /dev/MAKEDEV .local is a scriptwrittenbythesystemadministratorthatcreateslocal-onlydevicefilesor links (i.e., thosethatarenot partof thestandardMAKEDEV , suchasdevicefiles for somenon-standarddevicedriver).

The /usr filesystemThe/usr filesystemis oftenlarge,sinceall programsareinstalledthere.All files in /usr usuallycomefrom a Linux distribution; locally installedprogramsandotherstuff goesbelow /usr/local. Thismakesit possibleto updatethesystemfrom a new versionof thedistribution,or evenacompletelynewdistribution,withouthaving to install all programsagain.Someof thesubdirectoriesof /usr arelistedbelow (someof thelessimportantdirectorieshavebeendropped;seetheFSSTNDfor moreinformation).

/usr/X11R6

TheX Window System,all files.To simplify thedevelopmentandinstallationof X, theX fileshavenot beenintegratedinto therestof thesystem.Thereis adirectorytreebelow /usr/X11R6 similarto thatbelow /usr itself.

/usr/X386

Similar to /usr/X11R6, but for X11 Release5.

/usr/bin

Almost all usercommands.Somecommandsarein /bin or in /usr/local/bin.

/usr/sbin

Systemadministrationcommandsthatarenot neededon therootfilesystem,e.g.,mostserverprograms.

/usr/man, /usr/info, /usr/doc

26

Page 27: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

Manualpages,GNU Info documents,andmiscellaneousotherdocumentationfiles,respectively.

/usr/include

Headerfiles for theC programminglanguage.Thisshouldactuallybebelow /usr/lib forconsistency, but thetraditionis overwhelminglyin supportfor this name.

/usr/lib

Unchangingdatafiles for programsandsubsystems,includingsomesite-wideconfigurationfiles.Thenamelib comesfrom library; originally librariesof programmingsubroutineswerestoredin/usr/lib.

/usr/local

Theplacefor locally installedsoftwareandotherfiles.

The /var filesystemThe/var containsdatathatis changedwhenthesystemis runningnormally. It is specificfor eachsystem,i.e.,not sharedover thenetwork with othercomputers.

/var/catman

A cachefor manpagesthatareformattedon demand.Thesourcefor manualpagesis usuallystoredin /usr/man/man*; somemanualpagesmight comewith apre-formattedversion,which is storedin /usr/man/cat*. Othermanualpagesneedto beformattedwhenthey arefirst viewed;theformattedversionis thenstoredin /var/man sothatthenext personto view thesamepagewon’thave to wait for it to beformatted.(/var/catman is oftencleanedin thesameway temporarydirectoriesarecleaned.)

/var/lib

Filesthatchangewhile thesystemis runningnormally.

27

Page 28: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

/var/local

Variabledatafor programsthatareinstalledin /usr/local (i.e.,programsthathavebeeninstalledby thesystemadministrator).Notethatevenlocally installedprogramsshouldusetheother/vardirectoriesif they areappropriate,e.g.,/var/lock.

/var/lock

Lock files.Many programsfollow aconventionto createa lock file in /var/lock to indicatethatthey areusingaparticulardeviceor file. Otherprogramswill noticethelock file andwon’t attemptto usethedeviceor file.

/var/log

Log files from variousprograms,especiallylogin (/var/log/wtmp, which logsall loginsandlogoutsinto thesystem)andsyslog(/var/log/messages, whereall kernelandsystemprogrammessageareusuallystored).Filesin /var/log canoftengrow indefinitely, andmayrequirecleaningat regularintervals.

/var/run

Filesthatcontaininformationaboutthesystemthatis valid until thesystemis next booted.Forexample,/var/run/utmp containsinformationaboutpeoplecurrentlyloggedin.

/var/spool

Directoriesfor mail, news,printerqueues,andotherqueuedwork. Eachdifferentspoolhasits ownsubdirectorybelow /var/spool, e.g.,themailboxesof theusersarein /var/spool/mail.

/var/tmp

Temporaryfiles thatarelargeor thatneedto exist for a longertime thanwhatis allowedfor /tmp.(Althoughthesystemadministratormight not allow veryold files in /var/tmp either.)

28

Page 29: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

The /proc filesystemThe/proc filesystemcontainsa illusionaryfilesystem.It doesnot exist on a disk. Instead,thekernelcreatesit in memory. It is usedto provide informationaboutthesystem(originally aboutprocesses,hencethename).Someof themoreimportantfilesanddirectoriesareexplainedbelow. The/procfilesystemis describedin moredetail in theproc manualpage.

/proc/1

A directorywith informationaboutprocessnumber1. Eachprocesshasadirectorybelow /proc

with thenamebeingits processidentificationnumber.

/proc/cpuinfo

Informationabouttheprocessor, suchasits type,make,model,andperfomance.

/proc/devices

List of devicedriversconfiguredinto thecurrentlyrunningkernel.

/proc/dma

Showswhich DMA channelsarebeingusedat themoment.

/proc/filesystems

Filesystemsconfiguredinto thekernel.

/proc/interrupts

Showswhich interruptsarein use,andhow many of eachtherehavebeen.

/proc/ioports

Which I/O portsarein useat themoment.

29

Page 30: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

/proc/kcore

An imageof thephysicalmemoryof thesystem.This is exactly thesamesizeasyourphysicalmemory, but doesnot really takeup thatmuchmemory;it is generatedon thefly asprogramsaccessit. (Remember:unlessyoucopy it elsewhere,nothingunder/proc takesupany diskspaceat all.)

/proc/kmsg

Messagesoutputby thekernel.Thesearealsoroutedto syslog.

/proc/ksyms

Symboltablefor thekernel.

/proc/loadavg

The‘load average’of thesystem;threemeaninglessindicatorsof how muchwork thesystemhastodo at themoment.

/proc/meminfo

Informationaboutmemoryusage,bothphysicalandswap.

/proc/modules

Whichkernelmodulesareloadedat themoment.

/proc/net

Statusinformationaboutnetwork protocols.

/proc/self

A symboliclink to theprocessdirectoryof theprogramthatis looking at/proc. Whentwoprocesseslook at/proc, they getdifferentlinks. This is mainlya convenienceto make it easierforprogramsto getat their processdirectory.

30

Page 31: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter3. Overview of theDirectoryTree

/proc/stat

Variousstatisticsaboutthesystem,suchasthenumberof pagefaultssincethesystemwasbooted.

/proc/uptime

Thetime thesystemhasbeenup.

/proc/version

Thekernelversion.

Notethatwhile theabovefiles tendto beeasilyreadabletext files, they cansometimesbeformattedin away thatis not easilydigestable.Therearemany commandsthatdo little morethanreadtheabovefilesandformatthemfor easierunderstanding.For example,thefr eeprogramreads/proc/meminfo andconvertstheamountsgivenin bytesto kilobytes(andaddsa little moreinformation,aswell).

31

Page 32: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 4. Using Disks and Other Stora geMedia

“On a cleardiskyou canseekforever. ”

Whenyou install or upgradeyoursystem,you needto doa fair amountof work on yourdisks.You haveto makefilesystemson yourdiskssothatfilescanbestoredon themandreservespacefor thedifferentpartsof yoursystem.

Thischapterexplainsall theseinitial activities.Usually, onceyougetyoursystemsetup,youwon’t haveto go throughthework again,exceptfor usingfloppies.You’ll needto comebackto thischapterif youadda new diskor wantto fine-tuneyourdiskusage.

Thebasictasksin administeringdisksare:

• Formatyourdisk.This doesvariousthingsto prepareit for use,suchascheckingfor badsectors.(Formattingis nowadaysnot necessaryfor mostharddisks.)

• Partition aharddisk, if youwantto useit for severalactivities thataren’t supposedto interferewithoneanother. Onereasonfor partitioningis to storedifferentoperatingsystemson thesamedisk.Anotherreasonis to keepuserfilesseparatefrom systemfiles,which simplifiesback-upsandhelpsprotectthesystemfiles from corruption.

• Makeafilesystem(of asuitabletype)on eachdiskor partition.Thediskmeansnothingto Linux untilyou makea filesystem;thenfilescanbecreatedandaccessedon it.

• Mountdifferentfilesystemsto form a singletreestructure,eitherautomatically, or manuallyasneeded.(Manuallymountedfilesystemsusuallyneedto beunmountedmanuallyaswell.)

Chapter5 containsinformationaboutvirtual memoryanddiskcaching,of which youalsoneedto beawarewhenusingdisks.

Two kinds of devicesUNIX, andthereforeLinux, recognizestwo differentkindsof device: random-accessblockdevices(suchasdisks),andcharacterdevices(suchastapesandseriallines),someof which maybeserial,andsomerandom-access.Eachsupporteddevice is representedin thefilesystemasadevicefile. Whenyou readorwrite adevicefile, thedatacomesfrom or goesto thedevice it represents.This wayno specialprograms

32

Page 33: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

(andnospecialapplicationprogrammingmethodology, suchascatchinginterruptsor polling a serialport) arenecessaryto accessdevices;for example,to senda file to theprinter, onecouldjustsay

$ cat filename�

/dev/lp1$

andthecontentsof thefile areprinted(thefile must,of course,bein a form thattheprinterunderstands).However, sinceit is not agoodideato haveseveralpeoplecattheir files to theprinterat thesametime,oneusuallyusesa specialprogramto sendthefiles to beprinted(usuallylpr ). Thisprogrammakessurethatonly onefile is beingprintedata time,andwill automaticallysendfiles to theprinterassoonasitfinisheswith thepreviousfile. Somethingsimilar is neededfor mostdevices.In fact,oneseldomneedstoworry aboutdevicefilesat all.

Sincedevicesshow up asfiles in thefilesystem(in the/dev directory),it is easyto seejustwhatdevicefilesexist, usingls or anothersuitablecommand.In theoutputof ls -l, thefirst columncontainsthetypeof thefile andits permissions.For example,inspectingaserialdevicegiveson my system

$ ls -l /dev/cua0crw-rw-rw- 1 root uucp 5, 64 Nov 30 1993 /dev/cua0

$

Thefirst characterin thefirst column,i.e., ‘c’ in crw-rw-rw- above,tellsaninformeduserthetypeofthefile, in this caseacharacterdevice.For ordinaryfiles, thefirst characteris ‘-’, for directoriesit is ‘d’,andfor block devices‘b’; seethe ls manpagefor furtherinformation.

Notethatusuallyall devicefiles exist eventhoughthedevice itself mightbenotbeinstalled.Sojustbecauseyouhaveafile /dev/sda, it doesn’t meanthatyou really do haveanSCSIharddisk.Having allthedevicefilesmakestheinstallationprogramssimpler, andmakesit easierto addnew hardware(thereis no needto find out thecorrectparametersfor andcreatethedevicefiles for thenew device).

Hard disksThis subsectionintroducesterminologyrelatedto harddisks.If youalreadyknow thetermsandconcepts,you canskip thissubsection.

SeeFigure4-1 for a schematicpictureof theimportantpartsin a harddisk.A harddiskconsistsof oneor morecircularplatters, 1 of whicheitheror bothsurfacesarecoatedwith amagneticsubstanceusedforrecordingthedata.For eachsurface,thereis a read-writeheadthatexaminesor alterstherecordeddata.Theplattersrotateon acommonaxis;a typical rotationspeedis 3600rotationsperminute,althoughhigh-performanceharddiskshavehigherspeeds.Theheadsmovealongtheradiusof theplatters;thismovementcombinedwith therotationof theplattersallows theheadto accessall partsof thesurfaces.

33

Page 34: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Theprocessor(CPU)andtheactualdisk communicatethroughadiskcontroller. This relievestherestofthecomputerfrom knowing how to usethedrive,sincethecontrollersfor differenttypesof diskscanbemadeto usethesameinterfacetowardstherestof thecomputer. Therefore,thecomputercansayjust“hey disk,gimmewhatI want”, insteadof a longandcomplex seriesof electricsignalsto movetheheadto theproperlocationandwaiting for thecorrectpositionto comeundertheheadanddoingall theotherunpleasantstuff necessary. (In reality, theinterfaceto thecontrolleris still complex, but muchlesssothanit wouldotherwisebe.)Thecontrollercanalsodo someotherstuff, suchascaching,or automaticbadsectorreplacement.

Theabove is usuallyall oneneedsto understandaboutthehardware.Thereis alsoa bunchof otherstuff,suchasthemotorthatrotatestheplattersandmovestheheads,andtheelectronicsthatcontroltheoperationof themechanicalparts,but thatis mostlynot relevantfor understandingtheworkingprincipleof a harddisk.

Thesurfacesareusuallydividedinto concentricrings,calledtracks, andthesein turn aredividedintosectors. This division is usedto specifylocationson theharddiskandto allocatediskspaceto files.Tofind a givenplaceon theharddisk,onemight say“surface3, track5, sector7”. Usuallythenumberofsectorsis thesamefor all tracks,but someharddisksput moresectorsin outertracks(all sectorsareofthesamephysicalsize,somoreof themfit in thelongeroutertracks).Typically, a sectorwill hold512bytesof data.Thedisk itself can’t handlesmalleramountsof datathanonesector.

34

Page 35: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Figure4-1.A schematicpictur eof a hard disk.

From above

From the side

Rotation

Track

Sector

Read/write head

Cylinder

Platter

Surfaces

35

Page 36: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Eachsurfaceis dividedinto tracks(andsectors)in thesameway. This meansthatwhentheheadfor onesurfaceis on a track,theheadsfor theothersurfacesarealsoon thecorrespondingtracks.All thecorrespondingtrackstakentogetherarecalledacylinder. It takestime to movetheheadsfrom onetrack(cylinder) to another, soby placingthedatathatis oftenaccessedtogether(say, afile) sothatit is withinonecylinder, it is not necessaryto movetheheadsto readall of it. This improvesperformance.It is notalwayspossibleto placefiles like this; files thatarestoredin severalplaceson thediskarecalledfragmented.

Thenumberof surfaces(or heads,which is thesamething),cylinders,andsectorsvarya lot; thespecificationof thenumberof eachis calledthegeometryof a harddisk.Thegeometryis usuallystoredin aspecial,battery-poweredmemorylocationcalledtheCMOSRAM, from wheretheoperatingsystemcanfetchit duringbootupor driver initialization.

Unfortunately, theBIOS 2 hasa designlimitation, which makesit impossibleto specifya tracknumberthatis largerthan1024in theCMOSRAM, which is too little for a largeharddisk.To overcomethis, theharddiskcontrollerliesaboutthegeometry, andtranslatestheaddressesgivenby thecomputerintosomethingthatfits reality. For example,a harddiskmight have8 heads,2048tracks,and35 sectorspertrack.3 Its controllercouldlie to thecomputerandclaim thatit has16 heads,1024tracks,and35sectorspertrack,thusnot exceedingthelimit on tracks,andtranslatestheaddressthatthecomputergivesit byhalvingtheheadnumber, anddoublingthetracknumber. Themathcanbemorecomplicatedin reality,becausethenumbersarenot asniceashere(but again,thedetailsarenot relevantfor understandingtheprinciple).This translationdistortstheoperatingsystem’sview of how thedisk is organized,thusmakingit impracticalto usetheall-data-on-one-cylindertrick to boostperformance.

Thetranslationis only a problemfor IDE disks.SCSIdisksuseasequentialsectornumber(i.e., thecontrollertranslatesa sequentialsectornumberto a head,cylinder, andsectortriplet), andacompletelydifferentmethodfor theCPUto talk with thecontroller, sothey areinsulatedfrom theproblem.Note,however, thatthecomputermightnot know therealgeometryof anSCSIdiskeither.

SinceLinux oftenwill not know therealgeometryof adisk, its filesystemsdon’t eventry to keepfileswithin a singlecylinder. Instead,it triesto assignsequentiallynumberedsectorsto files,whichalmostalwaysgivessimilarperformance.Theissueis furthercomplicatedby on-controllercaches,andautomaticprefetchesdoneby thecontroller.

Eachharddisk is representedby aseparatedevicefile. Therecan(usually)beonly two or four IDE harddisks.Theseareknown as/dev/hda, /dev/hdb, /dev/hdc, and/dev/hdd, respectively. SCSIharddisksareknown as/dev/sda, /dev/sdb, andsoon.Similar namingconventionsexist for otherharddisk types;seeXXX (device list) for moreinformation.Notethatthedevicefiles for theharddisksgiveaccessto theentiredisk,with noregardto partitions(which will bediscussedbelow), andit’s easytomessup thepartitionsor thedatain themif you aren’t careful.Thedisks’ devicefilesareusuallyusedonly to getaccessto themasterbootrecord(whichwill alsobediscussedbelow).

36

Page 37: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

FloppiesA floppy diskconsistsof aflexible membranecoveredon oneor bothsideswith similarmagneticsubstanceasaharddisk.Thefloppy disk itself doesn’t havearead-writehead,thatis includedin thedrive.A floppy correspondsto oneplatterin a harddisk,but is removableandonedrivecanbeusedtoaccessdifferentfloppies,whereastheharddisk is oneindivisibleunit.

Likea harddisk,a floppy is dividedinto tracksandsectors(andthetwo correspondingtrackson eithersideof afloppy form acylinder),but therearemany fewerof themthanon a harddisk.

A floppy drivecanusuallyuseseveraldifferenttypesof disks;for example,a3.5 inch drivecanuseboth720kB and1.44MB disks.Sincethedrivehasto operatea bit differentlyandtheoperatingsystemmustknow how big thedisk is, therearemany devicefiles for floppy drives,onepercombinationof driveanddisk type.Therefore,/dev/fd0H1440 is thefirst floppy drive(fd0), which mustbea 3.5inch drive,usinga 3.5inch,highdensitydisk (H) of size1440kB (1440),i.e.,anormal3.5 inchHD floppy. Formoreinformationon thenamingconventionsfor thefloppy devices,seeXXX (device list).

Thenamesfor floppy drivesarecomplex, however, andLinux thereforehasaspecialfloppy device typethatautomaticallydetectsthetypeof thedisk in thedrive.It worksby trying to readthefirst sectorof anewly insertedfloppy usingdifferentfloppy typesuntil it findsthecorrectone.Thisnaturallyrequiresthatthefloppy is formattedfirst. Theautomaticdevicesarecalled/dev/fd0, /dev/fd1, andsoon.

Theparameterstheautomaticdeviceusesto accessadiskcanalsobesetusingtheprogramsetfdprm.This canbeusefulif youneedto usedisksthatdo not follow any usualfloppy sizes,e.g.,if they haveanunusualnumberof sectors,or if theautodetectingfor somereasonfailsandtheproperdevicefile ismissing.

Linux canhandlemany nonstandardfloppy disk formatsin additionto all thestandardones.Someoftheserequireusingspecialformattingprograms.We’ll skip thesedisk typesfor now, but in themeantime youcanexaminethe/etc/fdprm file. It specifiesthesettingsthatsetfdprm recognizes.

Theoperatingsystemmustknow whenadisk hasbeenchangedin a floppy drive,for example,in orderto avoid usingcacheddatafrom thepreviousdisk.Unfortunately, thesignalline thatis usedfor this issometimesbroken,andworse,this won’t alwaysbenoticeablewhenusingthedrive from withinMS-DOS.If you areexperiencingweird problemsusingfloppies,this mightbethereason.Theonly wayto correctit is to repairthefloppy drive.

CD-ROM’sA CD-ROM driveusesanoptically read,plasticcoateddisk.Theinformationis recordedon thesurfaceof thedisk 4 in small‘holes’ alignedalonga spiralfrom thecenterto theedge.Thedrivedirectsa laserbeamalongthespiralto readthedisk.Whenthelaserhits a hole,thelaseris reflectedin oneway;when

37

Page 38: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

it hits smoothsurface,it is reflectedin anotherway. Thismakesit easyto codebits,andthereforeinformation.Therestis easy, meremechanics.

CD-ROM drivesareslow comparedto harddisks.Whereasa typical harddisk will haveanaverageseektime lessthan15milliseconds,a fastCD-ROM drivecanusetenthsof a secondfor seeks.Theactualdatatransferrateis fairly highat hundredsof kilobytespersecond.TheslownessmeansthatCD-ROMdrivesarenot aspleasantto useinsteadof harddisks(someLinux distributionsprovide ‘li ve’ filesystemson CD-ROM’s,makingit unnecessaryto copy thefiles to theharddisk,makinginstallationeasierandsaving a lot of harddisk space),althoughit is still possible.For installingnew software,CD-ROM’sareverygood,sinceit maximumspeedis notessentialduringinstallation.

Thereareseveralwaysto arrangedataon aCD-ROM. Themostpopularoneis specifiedby theinternationalstandardISO9660.Thisstandardspecifiesavery minimalfilesystem,which is evenmorecrudethantheoneMS-DOSuses.On theotherhand,it is sominimal thateveryoperatingsystemshouldbeableto mapit to its nativesystem.

For normalUNIX use,theISO9660filesystemis notusable,soanextensionto thestandardhasbeendeveloped,calledtheRockRidgeextension.RockRidgeallows longerfilenames,symboliclinks, andalot of othergoodies,makingaCD-ROM look moreor lesslikeany contemporaryUNIX filesystem.Evenbetter, aRockRidgefilesystemis still a valid ISO9660filesystem,makingit usableby non-UNIXsystemsaswell. Linux supportsbothISO9660andtheRockRidgeextensions;theextensionsarerecognizedandusedautomatically.

Thefilesystemis only half thebattle,however. MostCD-ROM’scontaindatathatrequiresaspecialprogramto access,andmostof theseprogramsdonot run underLinux (except,possibly, underdosemu,theLinux MS-DOSemulator).

A CD-ROM drive is accessedvia thecorrespondingdevicefile. Thereareseveralwaysto connectaCD-ROM drive to thecomputer:via SCSI,via a soundcard,or via EIDE. Thehardwarehackingneededto do this is outsidethescopeof thisbook,but thetypeof connectiondecidesthedevicefile. SeeXXX(device-list) for enlightment.

TapesA tapedriveusesa tape,similar 5 to cassettesusedfor music.A tapeis serialin nature,which meansthatin orderto getto any givenpartof it, youfirst have to go throughall thepartsin between.A disk canbeaccessedrandomly, i.e.,youcanjumpdirectly to any placeon thedisk.Theserialaccessof tapesmakesthemslow.

On theotherhand,tapesarerelatively cheapto make,sincethey do not needto befast.They canalsoeasilybemadequitelong,andcanthereforecontaina largeamountof data.Thismakestapesverysuitablefor thingslikearchiving andbackups,whichdo not requirelargespeeds,but benefitfrom low

38

Page 39: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

costsandlargestoragecapacities.

FormattingFormattingis theprocessof writing markson themagneticmediathatareusedto marktracksandsectors.Beforea disk is formatted,its magneticsurfaceis a completemessof magneticsignals.Whenitis formatted,someorderis broughtinto thechaosby essentiallydrawing lineswherethetracksgo,andwherethey aredividedinto sectors.Theactualdetailsarenot quiteexactly like this,but thatis irrelevant.Whatis importantis thata diskcannotbeusedunlessit hasbeenformatted.

Theterminologyis a bit confusinghere:in MS-DOS,theword formattingis usedto coveralsotheprocessof creatinga filesystem(whichwill bediscussedbelow). There,thetwo processesareoftencombined,especiallyfor floppies.Whenthedistinctionneedsto bemade,therealformattingis calledlow-level formatting, while makingthefilesystemis calledhigh-level formatting. In UNIX circles,thetwo arecalledformattingandmakingafilesystem,sothat’swhatis usedin this bookaswell.

For IDE andsomeSCSIdiskstheformattingis actuallydoneat thefactoryanddoesn’t needto berepeated;hencemostpeoplerarelyneedto worry aboutit. In fact,formattingaharddisk cancauseit towork lesswell, for examplebecausea diskmight needto beformattedin someveryspecialway to allowautomaticbadsectorreplacementto work.

Disksthatneedto beor canbeformattedoftenrequirea specialprogramanyway, becausetheinterfaceto theformattinglogic insidethedrive is differentfrom drive to drive.Theformattingprogramis ofteneitheron thecontrollerBIOS,or is suppliedasanMS-DOSprogram;neitherof thesecaneasilybeusedfrom within Linux.

During formattingonemightencounterbadspotson thedisk,calledbadblocksor badsectors. Thesearesometimeshandledby thedrive itself, but eventhen,if moreof themdevelop,somethingneedsto bedoneto avoid usingthosepartsof thedisk.Thelogic to do this is built into thefilesystem;how to addtheinformationinto thefilesystemis describedbelow. Alternatively, onemight createasmallpartitionthatcoversjust thebadpartof thedisk; this approachmightbea goodideaif thebadspotis very large,sincefilesystemscansometimeshave troublewith very largebadareas.

Floppiesareformattedwith fdformat. Thefloppy devicefile to useis givenastheparameter. Forexample,thefollowing commandwould formata highdensity, 3.5 inch floppy in thefirst floppy drive:

$ fdformat /dev/fd0H1440Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.

Formatting ... done

Verifying ... done

$

39

Page 40: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Notethatif youwantto useanautodetectingdevice (e.g.,/dev/fd0), youmustsettheparametersof thedevicewith setfdprm first. To achievethesameeffectasabove,onewouldhave to do thefollowing:

$ setfdprm /dev/fd0 1440/1440$ fdformat /dev/fd0Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.

Formatting ... done

Verifying ... done

$

It is usuallymoreconvenientto choosethecorrectdevicefile thatmatchesthetypeof thefloppy. Notethatit is unwiseto formatfloppiesto containmoreinformationthanwhatthey aredesignedfor.

fdformat will alsovalidatethefloppy, i.e.,checkit for badblocks.It will try abadblockseveraltimes(youcanusuallyhearthis, thedrivenoisechangesdramatically).If thefloppy is only marginally bad(dueto dirt on theread/writehead,someerrorsarefalsesignals),fdformat won’t complain,but a realerrorwill abortthevalidationprocess.Thekernelwill print log messagesfor eachI/O errorit finds;thesewill go to theconsoleor, if syslogis beingused,to thefile /usr/log/messages. fdformat itself won’ttell wheretheerroris (oneusuallydoesn’t care,floppiesarecheapenoughthatabadoneis automaticallythrown away).

$ fdformat /dev/fd0H1440Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.

Formatting ... done

Verifying ... read: Unknown error

$

Thebadblockscommandcanbeusedto searchany diskor partitionfor badblocks(includinga floppy).It doesnot formatthedisk,soit canbeusedto checkevenexisting filesystems.Theexamplebelowchecksa 3.5inch floppy with two badblocks.

$ badblocks /dev/fd0H1440 1440718

719

$

badblocksoutputstheblock numbersof thebadblocksit finds.Most filesystemscanavoid suchbadblocks.They maintaina list of known badblocks,which is initializedwhenthefilesystemis made,andcanbemodifiedlater. Theinitial searchfor badblockscanbedoneby themkfs command(whichinitializesthefilesystem),but laterchecksshouldbedonewith badblocksandthenew blocksshouldbeaddedwith fsck. We’ll describemkfs andfsck later.

Many moderndisksautomaticallynoticebadblocks,andattemptto fix themby usinga special,reservedgoodblock instead.This is invisible to theoperatingsystem.This featureshouldbedocumentedin the

40

Page 41: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

disk’smanual,if you’recuriousif it is happening.Evensuchdiskscanfail, if thenumberof badblocksgrows too large,althoughchancesarethatby thenthediskwill besorottenasto beunusable.

PartitionsA harddiskcanbedividedinto severalpartitions. Eachpartitionfunctionsasif it werea separateharddisk.Theideais thatif youhaveoneharddisk,andwantto have,say, two operatingsystemson it, youcandivide thedisk into two partitions.Eachoperatingsystemusesits partitionasit wishesanddoesn’ttouchtheotherone’s.Thisway thetwo operatingsystemscanco-exist peacefullyon thesameharddisk.Without partitionsonewouldhave to buy a harddisk for eachoperatingsystem.

Floppiesarenot partitioned.Thereis no technicalreasonagainstthis,but sincethey’resosmall,partitionswouldbeusefulonly very rarely. CD-ROM’sareusuallyalsonot partitioned,sinceit’seasierto usethemasonebig disk,andthereis seldoma needto haveseveraloperatingsystemson one.

The MBR, boot sector s and par tition tableTheinformationabouthow aharddiskhasbeenpartitionedis storedin its first sector(thatis, thefirstsectorof thefirst trackon thefirst disksurface).Thefirst sectoris themasterbootrecord (MBR) of thedisk; this is thesectorthattheBIOSreadsin andstartswhenthemachineis first booted.Themasterbootrecordcontainsasmallprogramthatreadsthepartitiontable,checkswhich partitionis active (thatis,markedbootable),andreadsthefirst sectorof thatpartition,thepartition’sbootsector(theMBR is alsoa bootsector, but it hasa specialstatusandthereforea specialname).Thisbootsectorcontainsanothersmallprogramthatreadsthefirst partof theoperatingsystemstoredon thatpartition(assumingit isbootable),andthenstartsit.

Thepartitioningschemeis not built into thehardware,or eveninto theBIOS. It is only a conventionthatmany operatingsystemsfollow. Not all operatingsystemsdo follow it, but they aretheexceptions.Someoperatingsystemssupportpartitions,but they occupy onepartitionon theharddisk,andusetheirinternalpartitioningmethodwithin thatpartition.Thelattertypeexistspeacefullywith otheroperatingsystems(includingLinux), anddoesnot requireany specialmeasures,but anoperatingsystemthatdoesn’t supportpartitionscannotco-exist on thesamediskwith any otheroperatingsystem.

As a safetyprecaution,it is agoodideato write down thepartitiontableona pieceof paper, sothatif itevercorruptsyou don’t have to loseall yourfiles. (A badpartitiontablecanbefixedwith fdisk). Therelevantinformationis givenby thefdisk -l command:

$ fdisk -l /dev/hda

Disk /dev/hda: 15 heads, 57 sectors, 790 cylinders

Units = cylinders of 855 * 512 bytes

41

Page 42: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Device Boot Begin Start End Blocks Id System

/dev/hda1 1 1 24 10231+ 82 Linux swap

/dev/hda2 25 25 48 10260 83 Linux native

/dev/hda3 49 49 408 153900 83 Linux native

/dev/hda4 409 409 790 163305 5 Extended

/dev/hda5 409 409 744 143611+ 83 Linux native

/dev/hda6 745 745 790 19636+ 83 Linux native

$

Extended and logical par titionsTheoriginalpartitioningschemefor PCharddisksallowedonly four partitions.Thisquickly turnedoutto betoo little in reallife, partly becausesomepeoplewantmorethanfour operatingsystems(Linux,MS-DOS,OS/2,Minix, FreeBSD,NetBSD,or Windows/NT, to namea few), but primarily becausesometimesit is a goodideato haveseveralpartitionsfor oneoperatingsystem.For example,swapspaceis usuallybestput in its own partitionfor Linux insteadof in themainLinux partitionfor reasonsofspeed(seebelow).

To overcomethis designproblem,extendedpartitionswereinvented.This trick allowspartitioningaprimary partition into sub-partitions.Theprimarypartitionthussubdividedis theextendedpartition; thesubpartitionsarelogical partitions. They behave like primary6 partitions,but arecreateddifferently.Thereis no speeddifferencebetweenthem.

Thepartitionstructureof a harddiskmight look like thatin Figure4-2.Thedisk is dividedinto threeprimarypartitions,thesecondof which is dividedinto two logical partitions.Part of thedisk is notpartitionedatall. Thediskasa wholeandeachprimarypartitionhasabootsector.

42

Page 43: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Figure4-2.A samplehard disk partitioning .

Logical

partition

Logical

partition

Extended

partition

Primary

partition

Primary

partition

MBRBoot sector

Data areaof partition

Boot sector

Boot sector

Unused disk space

Data area

Data area

Data area

Unused boot sector

Unused boot sector

Partition typesThepartitiontables(theonein theMBR, andtheonesfor extendedpartitions)containonebyteperpartitionthatidentifiesthetypeof thatpartition.Thisattemptsto identify theoperatingsystemthatusesthepartition,or whatit usesit for. Thepurposeis to make it possibleto avoid having two operatingsystemsaccidentallyusingthesamepartition.However, in reality, operatingsystemsdonot really careaboutthepartitiontypebyte;e.g.,Linux doesn’t careat all whatit is. Worse,someof themuseitincorrectly;e.g.,at leastsomeversionsof DR-DOSignorethemostsignificantbit of thebyte,whileothersdon’t.

Thereis no standardizationagency to specifywhateachbytevaluemeans,but somecommonlyacceptedonesareincludedin in Table4-1.Thesamelist is availablein theLinux fdisk program.

Table 4-1.Partition types(fr om the Linux fdisk program).

0 Empty 40 Venix80286 94 AmoebaBBT

43

Page 44: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

1 DOS12-bitFAT 51 Novell? a5 BSD/386

2 XENIX root 52 Microport b7 BSDI fs

3 XENIX usr 63 GNU HURD b8 BSDI swap

4 DOS16-bitf� 32M

64 Novell c7 Syrinx

5 Extended 75 PC/IX db CP/M

6 DOS16-bit� =32M

80 Old MINIX e1 DOSaccess

7 OS/2HPFS 81 Linux/MINIX e3 DOSR/O

8 AIX 82 Linux swap f2 DOSsecondary

9 AIX bootable 83 Linux native ff BBT

a OS/2BootManag

93 Amoeba

Partitioning a hard diskTherearemany programsfor creatingandremoving partitions.Most operatingsystemshavetheir own,andit canbea goodideato useeachoperatingsystem’sown, just in caseit doessomethingunusualthattheotherscan’t. Many of theprogramsarecalledfdisk, includingtheLinux one,or variationsthereof.Detailson usingtheLinux fdisk aregivenon its manpage.Thecfdisk commandis similar to fdisk, buthasanicer(full screen)userinterface.

WhenusingIDE disks,thebootpartition(thepartitionwith thebootablekernelimagefiles) mustbecompletelywithin thefirst 1024cylinders.This is becausethedisk is usedvia theBIOS duringboot(beforethesystemgoesinto protectedmode),andBIOS can’t handlemorethan1024cylinders.It issometimespossibleto useabootpartitionthatis only partlywithin thefirst 1024cylinders.Thisworksaslong asall thefiles thatarereadwith theBIOS arewithin thefirst 1024cylinders.Sincethis isdifficult to arrange,it is a verybadidea to do it; youneverknow whenakernelupdateor diskdefragmentationwill resultin anunbootablesystem.Therefore,makesureyourbootpartitioniscompletelywithin thefirst 1024cylinders.

Somenewerversionsof theBIOS andIDE diskscan,in fact,handlediskswith morethan1024cylinders.If youhavesuchasystem,youcanforgetabouttheproblem;if youaren’t quitesureof it, putit within thefirst 1024cylinders.

Eachpartitionshouldhaveanevennumberof sectors,sincetheLinux filesystemsusea 1 kilobyteblocksize,i.e., two sectors.An oddnumberof sectorswill resultin thelastsectorbeingunused.This won’t

44

Page 45: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

resultin any problems,but it is ugly, andsomeversionsof fdisk will warnaboutit.

Changinga partition’ssizeusuallyrequiresfirst backingup everythingyou wantto save from thatpartition(preferablythewholedisk, just in case),deletingthepartition,creatingnew partition,thenrestoringeverythingto thenew partition.If thepartitionis growing, youmayneedto adjustthesizes(andbackupandrestore)of theadjoiningpartitionsaswell.

Sincechangingpartitionsizesis painful, it is preferableto getthepartitionsright thefirst time,or haveaneffectiveandeasyto usebackupsystem.If you’re installingfrom amediathatdoesnot requiremuchhumanintervention(say, from CD-ROM, asopposedto floppies),it is ofteneasyto play with differentconfigurationat first. Sinceyoudon’t alreadyhavedatato backup, it is not sopainful to modify partitionsizesseveraltimes.

Thereis aprogramfor MS-DOS,calledfips, which resizesanMS-DOSpartitionwithout requiringthebackupandrestore,but for otherfilesystemsit is still necessary.

Device files and par titionsEachpartitionandextendedpartitionhasits own devicefile. Thenamingconventionfor thesefiles is thata partition’snumberis appendedafterthenameof thewholedisk,with theconventionthat1-4 areprimarypartitions(regardlessof how many primarypartitionsthereare)and5-8 arelogical partitions(regardlessof within which primarypartitionthey reside).For example,/dev/hda1 is thefirst primarypartitionon thefirst IDE harddisk,and/dev/sdb7 is thethird extendedpartitionon thesecondSCSIharddisk.Thedevice list in XXX (device list) givesmoreinformation.

Filesystems

What are filesystems?A filesystemis themethodsanddatastructuresthatanoperatingsystemusesto keeptrackof fileson adiskor partition;thatis, theway thefilesareorganizedon thedisk.Theword is alsousedto referto apartitionor disk thatis usedto storethefiles or thetypeof thefilesystem.Thus,onemight say“I havetwo filesystems”meaningonehastwo partitionson whichonestoresfiles,or thatoneis usingthe“extendedfilesystem”,meaningthetypeof thefilesystem.

Thedifferencebetweenadiskor partitionandthefilesystemit containsis important.A few programs(including,reasonablyenough,programsthatcreatefilesystems)operatedirectly on theraw sectorsof adiskor partition; if thereis anexistingfile systemthereit will bedestroyedor seriouslycorrupted.Most

45

Page 46: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

programsoperateona filesystem,andthereforewon’t work on apartitionthatdoesn’t containone(orthatcontainsoneof thewrongtype).

Beforea partitionor diskcanbeusedasafilesystem,it needsto beinitialized,andthebookkeepingdatastructuresneedto bewritten to thedisk.This processis calledmakinga filesystem.

Most UNIX filesystemtypeshavea similargeneralstructure,althoughtheexactdetailsvaryquiteabit.Thecentralconceptsaresuperblock, inode, datablock, directoryblock, andindirectionblock. Thesuperblockcontainsinformationaboutthefilesystemasa whole,suchasits size(theexactinformationheredependson thefilesystem).An inodecontainsall informationabouta file, exceptits name.Thenameis storedin thedirectory, togetherwith thenumberof theinode.A directoryentryconsistsof afilenameandthenumberof theinodewhich representsthefile. Theinodecontainsthenumbersofseveraldatablocks,which areusedto storethedatain thefile. Thereis spaceonly for a few datablocknumbersin theinode,however, andif moreareneeded,morespacefor pointersto thedatablocksisallocateddynamically. Thesedynamicallyallocatedblocksareindirectblocks;thenameindicatesthatinorderto find thedatablock,onehasto find its numberin theindirectblockfirst.

UNIX filesystemsusuallyallow oneto createa hole in afile (this is donewith lseek; checkthemanualpage),which meansthatthefilesystemjustpretendsthatata particularplacein thefile thereis just zerobytes,but no actualdisksectorsarereservedfor thatplacein thefile (this meansthatthefile will useabit lessdiskspace).Thishappensespeciallyoftenfor smallbinaries,Linux sharedlibraries,somedatabases,anda few otherspecialcases.(Holesareimplementedby storinga specialvalueastheaddressof thedatablock in theindirectblock or inode.This specialaddressmeansthatno datablock isallocatedfor thatpartof thefile, ergo, thereis aholein thefile.)

Holesaremoderatelyuseful.On theauthor’ssystem,a simplemeasurementshowedapotentialfor about4 MB of savingsthroughholesof about200MB totaluseddiskspace.Thatsystem,however, containsrelatively few programsandno databasefiles.

Filesystems galoreLinux supportsseveraltypesof filesystems.As of this writing themostimportantonesare:

minix

Theoldest,presumedto bethemostreliable,but quitelimited in features(sometimestampsaremissing,at most30 characterfilenames)andrestrictedin capabilities(atmost64MB perfilesystem).

xia

46

Page 47: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

A modifiedversionof theminix filesystemthatlifts thelimits on thefilenamesandfilesystemsizes,but doesnot otherwiseintroducenew features.It is not verypopular, but is reportedto work verywell.

ext2

Themostfeaturefulof thenativeLinux filesystems,currentlyalsothemostpopularone.It isdesignedto beeasilyupwardscompatible,sothatnew versionsof thefilesystemcodedonot requirere-makingtheexisting filesystems.

ext

An olderversionof ext2 thatwasn’t upwardscompatible.It is hardlyeverusedin new installationsany more,andmostpeoplehaveconvertedto ext2.

In addition,supportfor severalforeignfilesystemexists,to make it easierto exchangefileswith otheroperatingsystems.Theseforeignfilesystemswork just likenativeones,exceptthatthey maybelackingin someusualUNIX features,or havecuriouslimitations,or otheroddities.

msdos

Compatibilitywith MS-DOS(andOS/2andWindowsNT) FAT filesystems.

usmdos

ExtendsthemsdosfilesystemdriverunderLinux to getlong filenames,owners,permissions,links,anddevicefiles.This allowsa normalmsdosfilesystemto beusedasif it wereaLinux one,thusremoving theneedfor a separatepartitionfor Linux.

iso9660

ThestandardCD-ROM filesystem;thepopularRockRidgeextensionto theCD-ROM standardthatallows longerfile namesis supportedautomatically.

nfs

47

Page 48: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

A networkedfilesystemthatallowssharinga filesystembetweenmany computersto allow easyaccessto thefiles from all of them.

hpfs

TheOS/2filesystem.

sysv

SystemV/386,Coherent,andXenix filesystems.

Thechoiceof filesystemto usedependson thesituation.If compatibilityor otherreasonsmakeoneofthenon-nativefilesystemsnecessary, thenthatonemustbeused.If onecanchoosefreely, thenit isprobablywisestto useext2, sinceit hasall thefeaturesbut doesnot suffer from lackof performance.

Thereis alsotheprocfilesystem,usuallyaccessibleasthe/proc directory, which is not really afilesystematall, eventhoughit lookslike one.Theprocfilesystemmakesit easyto accesscertainkerneldatastructures,suchastheprocesslist (hencethename).It makesthesedatastructureslook likeafilesystem,andthatfilesystemcanbemanipulatedwith all theusualfile tools.For example,to getalisting of all processesonemightusethecommand

$ ls -l /proctotal 0

dr-xr-xr-x 4 root root 0 Jan 31 20:37 1

dr-xr-xr-x 4 liw users 0 Jan 31 20:37 63

dr-xr-xr-x 4 liw users 0 Jan 31 20:37 94

dr-xr-xr-x 4 liw users 0 Jan 31 20:37 95

dr-xr-xr-x 4 root users 0 Jan 31 20:37 98

dr-xr-xr-x 4 liw users 0 Jan 31 20:37 99

-r-r-r- 1 root root 0 Jan 31 20:37 devices

-r-r-r- 1 root root 0 Jan 31 20:37 dma

-r-r-r- 1 root root 0 Jan 31 20:37 filesystems

-r-r-r- 1 root root 0 Jan 31 20:37 interrupts

-r----- 1 root root 8654848 Jan 31 20:37 kcore

-r-r-r- 1 root root 0 Jan 31 11:50 kmsg

-r-r-r- 1 root root 0 Jan 31 20:37 ksyms

-r-r-r- 1 root root 0 Jan 31 11:51 loadavg

-r-r-r- 1 root root 0 Jan 31 20:37 meminfo

-r-r-r- 1 root root 0 Jan 31 20:37 modules

dr-xr-xr-x 2 root root 0 Jan 31 20:37 net

dr-xr-xr-x 4 root root 0 Jan 31 20:37 self

48

Page 49: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

-r-r-r- 1 root root 0 Jan 31 20:37 stat

-r-r-r- 1 root root 0 Jan 31 20:37 uptime

-r-r-r- 1 root root 0 Jan 31 20:37 version

$

(Therewill bea few extra files thatdon’t correspondto processes,though.Theaboveexamplehasbeenshortened.)

Notethateventhoughit is calleda filesystem,no partof theprocfilesystemtouchesany disk. It existsonly in thekernel’s imagination.Wheneveranyonetriesto look at any partof theprocfilesystem,thekernelmakesit look asif thepartexistedsomewhere,eventhoughit doesn’t. So,eventhoughthereis amulti-megabyte/proc/kcore file, it doesn’t takeany diskspace.

Whic h filesystem should be used?Thereis usuallylittle point in usingmany differentfilesystems.Currently, ext2fs is themostpopularone,andit is probablythewisestchoice.Dependingon theoverheadfor bookkeepingstructures,speed,(perceived)reliability, compatibility, andvariousotherreasons,it maybeadvisableto useanotherfilesystem.This needsto bedecidedon acase-by-casebasis.

Creating a filesystemFilesystemsarecreated,i.e., initialized,with themkfs command.Thereis actuallyaseparateprogramfor eachfilesystemtype.mkfs is justa front endthatrunstheappropriateprogramdependingon thedesiredfilesystemtype.Thetypeis selectedwith the-t fstype option.

Theprogramscalledby mkfs haveslightly differentcommandline interfaces.Thecommonandmostimportantoptionsaresummarizedbelow; seethemanualpagesfor more.

-t fstype

Selectthetypeof thefilesystem.

-c

Searchfor badblocksandinitialize thebadblock list accordingly.

-l filename

49

Page 50: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Readtheinitial badblock list from thenamefile.

To createanext2 filesystemona floppy, onewould give thefollowing commands:

$ fdformat -n /dev/fd0H1440Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.

Formatting ... done

$ badblocks /dev/fd0H1440 1440 $>$ bad-blocks$ mkfs -t ext2 -l bad-blocks /dev/fd0H1440mke2fs 0.5a, 5-Apr-94 for EXT2 FS 0.5, 94/03/10

360 inodes, 1440 blocks

72 blocks (5.00%) reserved for the super user

First data block=1

Block size=1024 (log=0)

Fragment size=1024 (log=0)

1 block group

8192 blocks per group, 8192 fragments per group

360 inodes per group

Writing inode tables: done

Writing superblocks and filesystem accounting information: done

$

First, thefloppy wasformatted(the-n optionpreventsvalidation,i.e.,badblockchecking).Thenbadblocksweresearchedwith badblocks, with theoutputredirectedto a file, bad-blocks. Finally, thefilesystemwascreated,with thebadblock list initializedby whateverbadblocks found.

The-c optioncouldhavebeenusedwith mkfs insteadof badblocksanda separatefile. Theexamplebelow doesthat.

$ mkfs -t ext2 -c /dev/fd0H1440mke2fs 0.5a, 5-Apr-94 for EXT2 FS 0.5, 94/03/10

360 inodes, 1440 blocks

72 blocks (5.00%) reserved for the super user

First data block=1

Block size=1024 (log=0)

Fragment size=1024 (log=0)

1 block group

8192 blocks per group, 8192 fragments per group

360 inodes per group

Checking for bad blocks (read-only test): done

Writing inode tables: done

Writing superblocks and filesystem accounting information: done

50

Page 51: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

$

The-c optionis moreconvenientthana separateuseof badblocks, but badblocks is necessaryforcheckingafterthefilesystemhasbeencreated.

Theprocessto preparefilesystemsonharddisksor partitionsis thesameasfor floppies,exceptthattheformattingisn’t needed.

Mounting and unmountingBeforeonecanusea filesystem,it hasto bemounted. Theoperatingsystemthendoesvariousbookkeepingthingsto makesurethateverythingworks.Sinceall files in UNIX arein asingledirectorytree,themountoperationwill make it look like thecontentsof thenew filesystemarethecontentsof anexisting subdirectoryin somealreadymountedfilesystem.

For example,Figure4-3 shows threeseparatefilesystems,eachwith their own rootdirectory. Whenthelasttwo filesystemsaremountedbelow /home and/usr, respectively, on thefirst filesystem,we cangeta singledirectorytree,asin Figure4-4.

Figure4-3.Thr eeseparatefilesystems.

liw bin lib

//

abc ftp etcbin dev home etc lib usr

/

51

Page 52: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Figure4-4./home and /usr havebeenmounted.

liwabc ftp bin libetc

bin dev home etc lib usr

/

Themountscouldbedoneasin thefollowing example:

$ mount /dev/hda2 /home$ mount /dev/hda3 /usr$

Themount commandtakestwo arguments.Thefirst oneis thedevicefile correspondingto thediskorpartitioncontainingthefilesystem.Thesecondoneis thedirectorybelow which it will bemounted.After thesecommandsthecontentsof thetwo filesystemslook just like thecontentsof the/home and/usr directories,respectively. Onewould thensaythat“/dev/hda2 is mountedon/home”, andsimilarly for /usr. To look at eitherfilesystem,onewould look at thecontentsof thedirectoryonwhichit hasbeenmounted,just asif it wereany otherdirectory. Notethedifferencebetweenthedevicefile,/dev/hda2, andthemounted-ondirectory, /home. Thedevicefile givesaccessto theraw contentsofthedisk, themounted-ondirectorygivesaccessto thefileson thedisk.Themounted-ondirectoryiscalledthemountpoint.

Linux supportsmany filesystemtypes.mount triesto guessthetypeof thefilesystem.Youcanalsousethe-t fstype optionto specifythetypedirectly; this is sometimesnecessary, sincetheheuristicsmount usesdo not alwayswork. For example,to mountanMS-DOSfloppy, you couldusethefollowingcommand:

$ mount -t msdos /dev/fd0 /floppy$

Themounted-ondirectoryneednot beempty, althoughit mustexist. Any files in it, however, will beinaccessibleby namewhile thefilesystemis mounted.(Any files thathavealreadybeenopenedwill stillbeaccessible.Filesthathavehardlinks from otherdirectoriescanbeaccessedusingthosenames.)Thereis no harmdonewith this,andit canevenbeuseful.For instance,somepeoplelike to have/tmp and

52

Page 53: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

/var/tmp synonymous,andmake/tmp bea symboliclink to /var/tmp. Whenthesystemis booted,beforethe/var filesystemis mounted,a/var/tmp directoryresidingon therootfilesystemis usedinstead.When/var is mounted,it will make the/var/tmp directoryon therootfilesysteminaccessible.If /var/tmp didn’t exist on theroot filesystem,it wouldbeimpossibleto usetemporaryfilesbeforemounting/var.

If you don’t intendto write anything to thefilesystem,usethe-r switchfor mount to do a readonlymount. Thiswill make thekernelstopany attemptsat writing to thefilesystem,andwill alsostopthekernelfrom updatingfile accesstimesin theinodes.Read-onlymountsarenecessaryfor unwritablemedia,e.g.,CD-ROM’s.

Thealertreaderhasalreadynoticeda slight logisticalproblem.How is thefirst filesystem(calledtheroot filesystem, becauseit containstheroot directory)mounted,sinceit obviouslycan’t bemountedonanotherfilesystem?Well, theansweris thatit is doneby magic.7 Theroot filesystemis magicallymountedatboottime,andonecanrely on it to alwaysbemounted.If theroot filesystemcan’t bemounted,thesystemdoesnot boot.Thenameof thefilesystemthatis magicallymountedasroot iseithercompiledinto thekernel,or setusingLILO or rdev.

Theroot filesystemis usuallyfirst mountedreadonly. Thestartupscriptswill thenrun fsck to verify itsvalidity, andif thereareno problems,they will re-mountit sothatwriteswill alsobeallowed.fsck mustnot berun ona mountedfilesystem,sinceany changesto thefilesystemwhile fsck is runningwill causetrouble.Sincetheroot filesystemis mountedreadonlywhile it is beingchecked,fsck canfix anyproblemswithoutworry, sincetheremountoperationwill flushany metadatathatthefilesystemkeepsinmemory.

Onmany systemsthereareotherfilesystemsthatshouldalsobemountedautomaticallyat boottime.Thesearespecifiedin the/etc/fstab file; seethefstabmanpagefor detailson theformat.Thedetailsof exactlywhentheextra filesystemsaremounteddependon many factors,andcanbeconfiguredbyeachadministratorif needbe;seeChapter6.

Whena filesystemno longerneedsto bemounted,it canbeunmountedwith umount. 8 umount takesoneargument:eitherthedevicefile or themountpoint.For example,to unmountthedirectoriesof thepreviousexample,onecouldusethecommands

$ umount /dev/hda2$ umount /usr$

Seethemanpagefor furtherinstructionson how to usethecommand.It is imperativethatyoualwaysunmounta mountedfloppy. Don’t justpopthefloppyoutof thedrive! Becauseof diskcaching,thedatais not necessarilywritten to thefloppy until youunmountit, soremoving thefloppy from thedrive tooearlymightcausethecontentsto becomegarbled.If youonly readfrom thefloppy, this is notvery likely,but if youwrite, evenaccidentally, theresultmaybecatastrophic.

53

Page 54: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Mountingandunmountingrequiressuperuserprivileges,i.e.,only root cando it. Thereasonfor this isthatif any usercanmounta floppy on any directory, thenit is rathereasyto createafloppy with, say, aTrojanhorsedisguisedas/bin/sh, or any otheroftenusedprogram.However, it is oftennecessarytoallow usersto usefloppies,andthereareseveralwaysto do this:

• Give theuserstherootpassword.This is obviouslybadsecurity, but is theeasiestsolution.It workswell if thereis no needfor securityanyway, which is thecaseonmany non-networked,personalsystems.

• Usea programsuchassudo to allow usersto usemount.This is still badsecurity, but doesn’t directlygivesuperuserprivilegesto everyone.9

• Make theusersusemtools, a packagefor manipulatingMS-DOSfilesystems,withoutmountingthem.This workswell if MS-DOSfloppiesareall thatis needed,but is ratherawkwardotherwise.

• List thefloppy devicesandtheir allowablemountpointstogetherwith thesuitableoptionsin/etc/fstab.

Thelastalternativecanbeimplementedby addinga line like thefollowing to the/etc/fstab file:

/dev/fd0 /floppy msdos user,noauto 0 0

Thecolumnsare:devicefile to mount,directoryto mounton,filesystemtype,options,backupfrequency(usedby dump), andfsck passnumber(to specifytheorderin which filesystemsshouldbecheckeduponboot;0 meansnocheck).

Thenoauto optionstopsthis mountto bedoneautomaticallywhenthesystemis started(i.e., it stopsmount -a from mountingit). Theuser optionallowsany userto mountthefilesystem,and,becauseofsecurityreasons,disallowsexecutionof programs(normalor setuid)andinterpretationof devicefilesfrom themountedfilesystem.After this,any usercanmounta floppy with anmsdosfilesystemwith thefollowing command:

$ mount /floppy$

Thefloppy can(andneedsto, of course)beunmountedwith thecorrespondingumount command.

If you wantto provideaccessto severaltypesof floppies,you needto giveseveralmountpoints.Thesettingscanbedifferentfor eachmountpoint.For example,to giveaccessto bothMS-DOSandext2floppies,youcouldhave thefollowing to linesin /etc/fstab:

/dev/fd0 /dosfloppy msdos user,noauto 0 0/dev/fd0 /ext2floppy ext2 user,noauto 0 0

For MS-DOSfilesystems(not just floppies),youprobablywantto restrictaccessto it by usingtheuid,gid, andumask filesystemoptions,describedin detailon themount manualpage.If youaren’t careful,

54

Page 55: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

mountinganMS-DOSfilesystemgiveseveryoneat leastreadaccessto thefiles in it, which is not a goodidea.

Checking filesystem integrity with fsc kFilesystemsarecomplex creatures,andassuch,they tendto besomewhaterror-prone.A filesystem’scorrectnessandvalidity canbecheckedusingthefsck command.It canbeinstructedto repairany minorproblemsit finds,andto alerttheuserif thereany unrepairableproblems.Fortunately, thecodetoimplementfilesystemsis debuggedquiteeffectively, sothereareseldomany problemsatall, andthey areusuallycausedby power failures,failing hardware,or operatorerrors;for example,by not shuttingdownthesystemproperly.

Most systemsaresetupto run fsck automaticallyatboottime,sothatany errorsaredetected(andhopefullycorrected)beforethesystemis used.Useof acorruptedfilesystemtendsto makethingsworse:if thedatastructuresaremessedup,usingthefilesystemwill probablymessthemupevenmore,resultingin moredataloss.However, fsck cantakeawhile to runon big filesystems,andsinceerrorsalmostneveroccurif thesystemhasbeenshutdown properly, a coupleof tricksareusedto avoid doingthechecksinsuchcases.Thefirst is thatif thefile /etc/fastboot exists,nochecksaremade.Thesecondis thattheext2 filesystemhasaspecialmarker in its superblockthattellswhetherthefilesystemwasunmountedproperlyafterthepreviousmount.Thisallowse2fsck(theversionof fsck for theext2 filesystem)toavoid checkingthefilesystemif theflag indicatesthattheunmountwasdone(theassumptionbeingthata properunmountindicatesno problems).Whetherthe/etc/fastboot trick workson yoursystemdependson yourstartupscripts,but theext2 trick worksevery timeyouusee2fsck. It hasto beexplicitlybypassedwith anoptionto e2fsckto beavoided.(Seethee2fsckmanpagefor detailsonhow.)

Theautomaticcheckingonly worksfor thefilesystemsthataremountedautomaticallyat boottime.Usefsck manuallyto checkotherfilesystems,e.g.,floppies.

If fsck findsunrepairableproblems,you needeitherin-depthknowlegeof how filesystemswork ingeneral,andthetypeof thecorruptfilesystemin particular, or goodbackups.Thelatteris easy(althoughsometimestedious)to arrange,theformercansometimesbearrangedvia a friend, theLinux newsgroupsandmailing lists,or someothersourceof support,if you don’t have theknow-how yourself.I’ d like totell youmoreaboutit, but my lackof educationandexperiencein this regardhindersme.Thedebugfsprogramby TheodoreT’soshouldbeuseful.

fsck mustonly berunon unmountedfilesystems,neveronmountedfilesystems(with theexceptionoftheread-onlyroot duringstartup).This is becauseit accessestheraw disk,andcanthereforemodify thefilesystemwithout theoperatingsystemrealizingit. Therewill betrouble,if theoperatingsystemisconfused.

55

Page 56: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Checking for disk errors with badb loc ksIt canbeagoodideato periodicallycheckfor badblocks.This is donewith thebadblockscommand.Itoutputsa list of thenumbersof all badblocksit canfind. This list canbefed to fsck to berecordedin thefilesystemdatastructuressothattheoperatingsystemwon’t try to usethebadblocksfor storingdata.Thefollowing examplewill show how this couldbedone.

$ badblocks /dev/fd0H1440 1440�

bad-blocks$ fsck -t ext2 -l bad-blocks /dev/fd0H1440Parallelizing fsck version 0.5a (5-Apr-94)

e2fsck 0.5a, 5-Apr-94 for EXT2 FS 0.5, 94/03/10

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Check reference counts.

Pass 5: Checking group summary information.

/dev/fd0H1440: ***** FILE SYSTEM WAS MODIFIED *****

/dev/fd0H1440: 11/360 files, 63/1440 blocks

$

If badblocksreportsa block thatwasalreadyused,e2fsckwill try to movetheblock to anotherplace.Iftheblock wasreally bad,not justmarginal, thecontentsof thefile maybecorrupted.

Fighting fragmentationWhena file is written to disk, it can’t alwaysbewritten in consecutiveblocks.A file thatis not storedinconsecutiveblocksis fragmented. It takeslongerto reada fragmentedfile, sincethedisk’s read-writeheadwill haveto movemore.It is desireableto avoid fragmentation,althoughit is lessof aproblemin asystemwith a goodbuffer cachewith read-ahead.

Theext2 filesystemattemptsto keepfragmentationata minimum,by keepingall blocksin afile closetogether, evenif they can’t bestoredin consecutivesectors.Ext2 effectively alwaysallocatesthefreeblock thatis nearestto otherblocksin a file. For ext2, it is thereforeseldomnecessaryto worry aboutfragmentation.Thereis a programfor defragmentinganext2 filesystem,seeXXX (ext2-defrag)in thebibliography.

Therearemany MS-DOSdefragmentationprogramsthatmoveblocksaroundin thefilesystemto removefragmentation.For otherfilesystems,defragmentationmustbedoneby backingup thefilesystem,re-creatingit, andrestoringthefiles from backups.Backingup a filesystembeforedefragmeningis agoodideafor all filesystems,sincemany thingscango wrongduringthedefragmentation.

56

Page 57: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Other tools for all filesystemsSomeothertoolsarealsousefulfor managingfilesystems.df shows thefreediskspaceon oneor morefilesystems;du showshow muchdiskspaceadirectoryandall its filescontain.Thesecanbeusedtohuntdown diskspacewasters.

sync forcesall unwrittenblocksin thebuffer cache(seethesectioncalledThebuffer cache in Chapter5)to bewritten to disk. It is seldomnecessaryto do thisby hand;thedaemonprocessupdatedoesthisautomatically. It canbeusefulin catastrophies,for exampleif updateor its helperprocessbdflush dies,or if youmustturn off powernowandcan’t wait for update to run.

Other tools for the ext2 filesystemIn additionto thefilesystemcreator(mke2fs) andchecker (e2fsck) accessibledirectlyor via thefilesystemtypeindependentfront ends,theext2 filesystemhassomeadditionaltoolsthatcanbeuseful.

tune2fsadjustsfilesystemparameters.Someof themoreinterestingparametersare:

• A maximalmountcount.e2fsckenforcesa checkwhenfilesystemhasbeenmountedtoo many times,evenif thecleanflag is set.For asystemthatis usedfor developingor testingthesystem,it mightbeagoodideato reducethis limit.

• A maximaltimebetweenchecks.e2fsckcanalsoenforceamaximaltimebetweentwo checks,evenifthecleanflag is set,andthefilesystemhasn’t beenmountedveryoften.Thiscanbedisabled,however.

• Numberof blocksreservedfor root.Ext2 reservessomeblocksfor rootsothatif thefilesystemfillsup, it is still possibleto do systemadministrationwithout having to deleteanything.Thereservedamountis by default5 percent,which onmostdisksisn’t enoughto bewasteful.However, for floppiesthereis no point in reservingany blocks.

Seethetune2fsmanualpagefor moreinformation.

dumpe2fsshows informationaboutanext2 filesystem,mostlyfrom thesuperblock.Figure4-5showsasampleoutput.Someof theinformationin theoutputis technicalandrequiresunderstandingof how thefilesystemworks(seeappendixXXX ext2fspaper),but muchof it is readilyunderstandableevenforlayadmins.

Figure4-5.Sampleoutput fr om dumpe2fs

dumpe2fs0.5b,11-Mar-95 for EXT2 FS0.5a,94/10/23Filesystemmagicnumber: 0xEF53Filesystemstate: cleanErrorsbehavior: Continue

57

Page 58: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

Inodecount: 360Block count: 1440Reservedblockcount: 72Freeblocks: 1133Freeinodes: 326First block: 1Block size: 1024Fragmentsize: 1024Blockspergroup: 8192Fragmentspergroup: 8192Inodespergroup: 360Lastmounttime: TueAug 8 01:52:521995Lastwrite time: TueAug 8 01:53:281995Mountcount: 3Maximummountcount: 20Lastchecked: TueAug 8 01:06:311995Checkinterval: 0Reservedblocksuid: 0 (userroot)Reservedblocksgid: 0 (grouproot)

Group0:Block bitmapat 3, Inodebitmapat 4, Inodetableat 51133freeblocks,326freeinodes,2 directoriesFreeblocks:307-1439Freeinodes:35-360

debugfs is a filesystemdebugger. It allowsdirectaccessto thefilesystemdatastructuresstoredon diskandcanthusbeusedto repairadisk thatis sobrokenthatfsck can’t fix it automatically. It hasalsobeenknown to beusedto recoverdeletedfiles.However, debugfs verymuchrequiresthatyouunderstandwhatyou’redoing;a failureto understandcandestroy all yourdata.

dump andrestorecanbeusedto backup anext2 filesystem.They areext2 specificversionsof thetraditionalUNIX backuptools.SeeChapter10 for moreinformationon backups.

Disks without filesystemsNot all disksor partitionsareusedasfilesystems.A swappartition,for example,will not haveafilesystemon it. Many floppiesareusedin a tape-driveemulatingfashion,sothata tar or otherfile is

58

Page 59: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

writtendirectly on theraw disk,without a filesystem.Linux bootfloppiesdon’t containafilesystem,only theraw kernel.

Avoidinga filesystemhastheadvantageof makingmoreof thediskusable,sinceafilesystemalwayshassomebookkeepingoverhead.It alsomakesthedisksmoreeasilycompatiblewith othersystems:forexample,thetar file formatis thesameon all systems,while filesystemsaredifferenton mostsystems.You will quickly getusedto diskswithoutfilesystemsif you needthem.BootableLinux floppiesalsodonot necessarilyhavea filesystem,althoughthatis alsopossible.

Onereasonto useraw disksis to make imagecopiesof them.For instance,if thediskcontainsapartiallydamagedfilesystem,it is a goodideato makeanexactcopy of it beforetrying to fix it, sincethenyoucanstartagainif yourfixing breaksthingsevenmore.Oneway to do this is to usedd:

$ dd if=/dev/fd0H1440 of=floppy-image2880+0 records in

2880+0 records out

$ dd if=floppy-image of=/dev/fd0H14402880+0 records in

2880+0 records out

$

Thefirst dd makesanexactimageof thefloppy to thefile floppy-image, thesecondonewritestheimageto thefloppy. (Theuserhaspresumablyswitchedthefloppy beforethesecondcommand.Otherwisethecommandpair is of doubtfulusefulness.)

Allocating disk space

Partitioning schemesIt is not easyto partitiona disk in thebestpossibleway. Worse,thereis no universallycorrectway to doit; therearetoo many factorsinvolved.

Thetraditionalway is to havea (relatively) smallroot filesystem,whichcontains/bin, /etc, /dev,/lib, /tmp, andotherstuff thatis neededto getthesystemupandrunning.Thisway, therootfilesystem(in its own partitionor on its own disk) is all thatis neededto bringup thesystem.Thereasoningis thatif therootfilesystemis smallandis not heavily used,it is lesslikely to becomecorruptwhenthesystemcrashes,andyouwill thereforefind it easierto fix any problemscausedby thecrash.Thenyoucreateseparatepartitionsor useseparatedisksfor thedirectorytreebelow /usr, theusers’homedirectories(oftenunder/home), andtheswapspace.Separatingthehomedirectories(with theusers’files) in theirown partitionmakesbackupseasier, sinceit is usuallynot necessaryto backupprograms(which residebelow /usr). In anetworkedenvironmentit is alsopossibleto share/usr amongseveralmachines(e.g.,

59

Page 60: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

by usingNFS),therebyreducingthetotal diskspacerequiredby severaltensor hundredsof megabytestimesthenumberof machines.

Theproblemwith having many partitionsis thatit splitsthetotalamountof freediskspaceinto manysmallpieces.Nowadays,whendisksand(hopefully)operatingsystemsaremorereliable,many peoplepreferto have just onepartitionthatholdsall their files.On theotherhand,it canbelesspainful to backup (andrestore)asmallpartition.

For a smallharddisk (assumingyoudon’t dokerneldevelopment),thebestway to go is probablytohave just onepartition.For largeharddisks,it is probablybetterto havea few largepartitions,just incasesomethingdoesgowrong.(Notethat‘small’ and‘large’ areusedin a relativesensehere;yourneedsfor diskspacedecidewhatthethresholdis.)

If you haveseveraldisks,youmight wish to havetheroot filesystem(including/usr) on one,andtheusers’homedirectoriesonanother.

It is agoodideato bepreparedto experimenta bit with differentpartitioningschemes(over time,notjust while first installingthesystem).This is a bit of work, sinceit essentiallyrequiresyou to install thesystemfrom scratchseveraltimes,but it is theonly way to besureyou do it right.

Space requirementsTheLinux distributionyou install will givesomeindicationof how muchdiskspaceyou needforvariousconfigurations.Programsinstalledseparatelymayalsodo thesame.Thiswill helpyouplanyourdiskspaceusage,but you shouldpreparefor thefutureandreservesomeextra spacefor thingsyouwillnoticelaterthatyou need.

Theamountyouneedfor userfiles dependson whatyouruserswish to do.Mostpeopleseemto needasmuchspacefor their filesaspossible,but theamountthey will livehappilywith variesa lot. Somepeopledoonly light text processingandwill survivenicelywith a few megabytes,othersdoheavy imageprocessingandwill needgigabytes.

By theway, whencomparingfile sizesgivenin kilobytesor megabytesanddiskspacegiveninmegabytes,it canbeimportantto know thatthetwo unitscanbedifferent.Somediskmanufacturersliketo pretendthatakilobyte is 1000bytesandamegabyteis 1000kilobytes,while all therestof thecomputingworld uses1024for bothfactors.Therefore,my 345MB harddiskwasreally a 330MB harddisk. 10

Swapspaceallocationis discussedin thesectioncalledAllocatingswapspacein Chapter5.

Examples of hard disk allocation

60

Page 61: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

I usedto havea109MB harddisk.Now I amusinga 330MB harddisk. I’ ll explainhow andwhy Ipartitionedthesedisks.

The109MB disk I partitionedin a lot of ways,whenmy needsandtheoperatingsystemsI usedchanged;I’ ll explain two typical scenarios.First, I usedto run MS-DOStogetherwith Linux. For that,Ineededabout20 MB of harddisk,or just enoughto haveMS-DOS,a C compiler, aneditor, a few otherutilities, theprogramI wasworkingon,andenoughfreediskspaceto not feel claustrophobic.For Linux,I hada 10 MB swappartition,andtherest,or 79 MB, wasasinglepartitionwith all thefiles I hadunderLinux. I experimentedwith having separateroot,/usr, and/home partitions,but therewasneverenoughfreediskspacein onepieceto do muchinteresting.

WhenI didn’t needMS-DOSanymore,I repartitionedthedisksothatI hada12 MB swappartition,andagainhadtherestasasinglefilesystem.

The330MB disk is partitionedinto severalpartitions,like this:

5 MB root filesystem

10 MB swappartition

180MB /usr filesystem

120MB /home filesystem

15 MB scratchpartition

Thescratchpartitionis for playingaroundwith thingsthatrequiretheir own partition,e.g.,tryingdifferentLinux distributions,or comparingspeedsof filesystems.Whennot neededfor anythingelse,itis usedasswapspace(I like to havea lot of openwindows).

Adding more disk space for Lin uxAddingmoredisk spacefor Linux is easy, at leastafterthehardwarehasbeenproperlyinstalled(thehardwareinstallationis outsidethescopeof this book).You formatit if necessary, thencreatethepartitionsandfilesystemasdescribedabove,andaddtheproperlinesto /etc/fstab sothatit ismountedautomatically.

Tips for saving disk spaceThebesttip for saving disk spaceis to avoid installingunnecessaryprograms.MostLinux distributionshaveanoptionto install only partof thepackagesthey contain,andby analyzingyourneedsyou mightnoticethatyoudon’t needmostof them.This will helpsave a lot of diskspace,sincemany programsarequitelarge.Evenif youdo needa particularpackageor program,youmight not needall of it. For

61

Page 62: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter4. UsingDisksandOtherStorageMedia

example,someon-linedocumentationmight beunnecessary, asmight someof theElisp files for GNUEmacs,someof thefontsfor X11, or someof thelibrariesfor programming.

If you cannotuninstallpackages,you might look into compression.Compressionprogramssuchasgzipor zip will compress(anduncompress)individualfilesor groupsof files.Thegzexesystemwillcompressanduncompressprogramsinvisibly to theuser(unusedprogramsarecompressed,thenuncompressedasthey areused).TheexperimentalDouBlesystemwill compressall files in afilesystem,invisibly to theprogramsthatusethem.(If youarefamiliarwith productssuchasStacker for MS-DOS,theprincipleis thesame.)

Notes1. Theplattersaremadeof ahardsubstance,e.g.,aluminium,whichgivestheharddisk its name.

2. TheBIOS is somebuilt-in softwarestoredon ROM chips.It takescare,amongotherthings,of theinitial stagesof booting.

3. Thenumbersarecompletelyimaginary.

4. Thatis, thesurfaceinsidethedisk,on themetaldisk insidetheplasticcoating.

5. But completelydifferent,of course.

6. Illogical?

7. For moreinformation,seethekernelsourceor theKernelHackers’Guide.

8. It shouldof coursebeunmount, but then mysteriouslydisappearedin the70’s,andhasn’t beenseensince.Pleasereturnit to Bell Labs,NJ, if youfind it.

9. It requiresseveralsecondsof hardthinking on theusers’behalf.

10. Sic transitdiscusmundi.

62

Page 63: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 5. Memor y Management

“Minnet, jaghartappatmitt minne,är jagsvenskellerfinne,kommerinte ihåg...” (BosseÖsterberg)

This sectiondescribestheLinux memorymanagementfeatures,i.e.,virtual memoryandthediskbuffercache.Thepurposeandworkingsandthethingsthesystemadministratorneedsto take intoconsiderationaredescribed.

What is vir tual memor y?Linux supportsvirtual memory, thatis, usinga diskasanextensionof RAM sothattheeffectivesizeofusablememorygrowscorrespondingly. Thekernelwill write thecontentsof a currentlyunusedblockofmemoryto theharddisksothatthememorycanbeusedfor anotherpurpose.Whentheoriginalcontentsareneededagain,they arereadbackinto memory. This is all madecompletelytransparentto theuser;programsrunningunderLinux only seethelargeramountof memoryavailableanddon’t noticethatpartsof themresideon thedisk from time to time.Of course,readingandwriting theharddisk is slower(on theorderof a thousandtimesslower) thanusingrealmemory, sotheprogramsdon’t run asfast.Thepartof theharddisk thatis usedasvirtual memoryis calledtheswapspace.

Linux canuseeitheranormalfile in thefilesystemor a separatepartitionfor swapspace.A swappartitionis faster, but it is easierto changethesizeof aswapfile (there’sno needto repartitionthewholeharddisk,andpossiblyinstall everythingfrom scratch).Whenyou know how muchswapspaceyouneed,youshouldgo for a swappartition,but if youareuncertain,youcanuseaswapfile first, usethesystemfor a while sothatyoucangeta feel for how muchswapyouneed,andthenmakeaswappartitionwhenyou’reconfidentaboutits size.

You shouldalsoknow thatLinux allowsoneto useseveralswappartitionsand/orswapfilesat thesametime.This meansthatif you only occasionallyneedanunusualamountof swapspace,youcansetup anextra swapfile at suchtimes,insteadof keepingthewholeamountallocatedall thetime.

A noteonoperatingsystemterminology:computerscienceusuallydistinguishesbetweenswapping(writing thewholeprocessout to swapspace)andpaging(writing only fixedsizeparts,usuallya fewkilobytes,at a time).Pagingis usuallymoreefficient,andthat’swhatLinux does,but traditionalLinuxterminologytalksaboutswappinganyway. 1

Creating a swap space

63

Page 64: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter5. MemoryManagement

A swapfile is anordinaryfile; it is in no wayspecialto thekernel.Theonly thing thatmattersto thekernelis thatit hasno holes,andthatit is preparedfor usewith mkswap. It mustresideon a localdisk,however; it can’t residein a filesystemthathasbeenmountedoverNFSdueto implementationreasons.

Thebit aboutholesis important.Theswapfile reservesthediskspacesothatthekernelcanquicklyswapout apagewithouthaving to go throughall thethingsthatarenecessarywhenallocatingadisksectorto afile. Thekernelmerelyusesany sectorsthathavealreadybeenallocatedto thefile. Becauseaholein afile meansthattherearenodisksectorsallocated(for thatplacein thefile), it is not goodfor thekernelto try to usethem.

Onegoodway to createtheswapfile without holesis throughthefollowing command:

$ dd if=/dev/zero of=/extra-swap bs=1024 count=10241024+0 records in

1024+0 records out

$

where/extra-swap is thenameof theswapfile andthesizeof is givenafterthecount=. It is bestforthesizeto beamultiple of 4, becausethekernelwritesout memorypages, which are4 kilobytesin size.If thesizeis not a multiple of 4, thelastcoupleof kilobytesmaybeunused.

A swappartitionis alsonot specialin any way. Youcreateit just likeany otherpartition;theonlydifferenceis thatit is usedasaraw partition,thatis, it will not containany filesystemat all. It is a goodideato markswappartitionsastype82(Linux swap);this will themakepartitionlistingsclearer, eventhoughit is not strictly necessaryto thekernel.

After youhavecreatedaswapfile or a swappartition,you needto write a signatureto its beginning;thiscontainssomeadministrative informationandis usedby thekernel.Thecommandto do this is mkswap,usedlike this:

$ mkswap /extra-swap 1024Setting up swapspace, size = 1044480 bytes

$

Notethattheswapspaceis still not in useyet: it exists,but thekerneldoesnot useit to providevirtualmemory.

You shouldbeverycarefulwhenusingmkswap, sinceit doesnotcheckthatthefile or partitionisn’tusedfor anythingelse.You caneasilyoverwriteimportantfilesandpartitionswith mkswap! Fortunately,you shouldonly needto usemkswapwhenyou install yoursystem.

TheLinux memorymanagerlimits thesizeof eachswapspaceto about127MB (for varioustechnicalreasons,theactuallimit is (4096-10)* 8 * 4096= 133890048$bytes,or 127.6875megabytes).Youcan,however, useup to 8 swapspacessimultaneously, for a totalof almost1 GB. 2

64

Page 65: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter5. MemoryManagement

Using a swap spaceAn initializedswapspaceis takeninto usewith swapon. Thiscommandtells thekernelthattheswapspacecanbeused.Thepathto theswapspaceis givenastheargument,soto startswappingonatemporaryswapfile onemight usethefollowing command.

$ swapon /extra-swap$

Swapspacescanbeusedautomaticallyby listing themin the/etc/fstab file.

/dev/hda8 none swap sw 0 0/swapfile none swap sw 0 0

Thestartupscriptswill run thecommandswapon-a, which will startswappingonall theswapspaceslistedin /etc/fstab. Therefore,theswaponcommandis usuallyusedonly whenextraswapis needed.

You canmonitortheuseof swapspaceswith fr ee. It will tell thetotalamountof swapspaceused.

$ freetotal used free shared buffers

Mem: 15152 14896 256 12404 2528

-/+ buffers: 12368 2784

Swap: 32452 6684 25768

$

Thefirst line of output(Mem:) shows thephysicalmemory. Thetotal columndoesnot show thephysicalmemoryusedby thekernel,which is usuallyabouta megabyte.Theusedcolumnshows theamountofmemoryused(thesecondline doesnot countbuffers).Thefreecolumnshowscompletelyunusedmemory. Thesharedcolumnshowstheamountof memorysharedby severalprocesses;themore,themerrier. Thebufferscolumnshows thecurrentsizeof thediskbuffer cache.

Thatlastline (Swap:) showssimilar informationfor theswapspaces.If this line is all zeroes,yourswapspaceis notactivated.

Thesameinformationis availablevia top, or usingtheprocfilesystemin file /proc/meminfo. It iscurrentlydifficult to getinformationon theuseof a specificswapspace.

A swapspacecanberemovedfrom usewith swapoff. It is usuallynot necessaryto do it, exceptfortemporaryswapspaces.Any pagesin usein theswapspaceareswappedin first; if thereis not sufficientphysicalmemoryto hold them,they will thenbeswappedout (to someotherswapspace).If thereis notenoughvirtual memoryto holdall of thepagesLinux will startto thrash;aftera longwhile it shouldrecover, but meanwhilethesystemis unusable.You shouldcheck(e.g.,with fr ee) thatthereis enoughfreememorybeforeremoving a swapspacefrom use.

65

Page 66: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter5. MemoryManagement

All theswapspacesthatareusedautomaticallywith swapon-a canberemovedfrom usewith swapoff-a; it looksat thefile /etc/fstab to find whatto remove.Any manuallyusedswapspaceswill remainin use.

Sometimesa lot of swapspacecanbein useeventhoughthereis a lot of freephysicalmemory. Thiscanhappenfor instanceif atonepoint thereis needto swap,but laterabig processthatoccupiedmuchof thephysicalmemoryterminatesandfreesthememory. Theswapped-outdatais not automaticallyswappedin until it is needed,sothephysicalmemorymayremainfreefor a long time.Thereis noneedto worryaboutthis,but it canbecomfortingto know whatis happening.

Sharing swap spaces with other operating systemsVirtual memoryis built into many operatingsystems.Sincethey eachneedit only whenthey arerunning,i.e.,neverat thesametime, theswapspacesof all but thecurrentlyrunningonearebeingwasted.It wouldbemoreefficient for themto sharea singleswapspace.This is possible,but canrequirea bit of hacking.TheTips-HOWTO containssomeadviceon how to implementthis.

Allocating swap spaceSomepeoplewill tell you thatyoushouldallocatetwice asmuchswapspaceasyou havephysicalmemory, but this is a bogusrule.Here’show to do it properly:

• Estimateyour total memoryneeds.This is thelargestamountof memoryyou’ll probablyneedat atime, thatis thesumof thememoryrequirementsof all theprogramsyouwantto runat thesametime.This canbedoneby runningat thesametimeall theprogramsyouarelikely to everberunningat thesametime.

For instance,if youwantto run X, youshouldallocateabout8 MB for it, gccwantsseveralmegabytes(somefilesneedanunusuallylargeamount,up to tensof megabytes,but usuallyaboutfour shoulddo),andsoon.Thekernelwill useabouta megabyteby itself, andtheusualshellsandothersmallutilities perhapsa few hundredkilobytes(sayamegabytetogether).Thereis noneedto try to beexact,roughestimatesarefine,but youmight wantto beon thepessimisticside.

Rememberthatif therearegoingto beseveralpeopleusingthesystemat thesametime, they areallgoingto consumememory. However, if two peoplerun thesameprogramat thesametime, thetotalmemoryconsumptionis usuallynot double,sincecodepagesandsharedlibrariesexist only once.

The fr eeandpscommandsareusefulfor estimatingthememoryneeds.

66

Page 67: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter5. MemoryManagement

• Add somesecurityto theestimatein step1. This is becauseestimatesof programsizeswill probablybewrong,becauseyou’ll probablyforgetsomeprogramsyouwantto run,andto makecertainthatyou havesomeextra spacejust in case.A coupleof megabytesshouldbefine.(It is betterto allocatetoo muchthantoo little swapspace,but there’snoneedto over-do it andallocatethewholedisk,sinceunusedswapspaceis wastedspace;seelateraboutaddingmoreswap.)Also, sinceit is nicerto dealwith evennumbers,you canroundthevalueup to thenext full megabyte.

• Basedon thecomputationsabove,you know how muchmemoryyou’ll beneedingin total.So,inorderto allocateswapspace,you just needto subtractthesizeof yourphysicalmemoryfrom thetotalmemoryneeded,andyouknow how muchswapspaceyouneed.(Onsomeversionsof UNIX, youneedto allocatespacefor animageof thephysicalmemoryaswell, sotheamountcomputedin step2is whatyouneedandyoushouldn’t do thesubtraction.)

• If yourcalculatedswapspaceis verymuchlargerthanyourphysicalmemory(morethana coupletimeslarger),youshouldprobablyinvestin morephysicalmemory, otherwiseperformancewill betoolow.

It’sa goodideato haveat leastsomeswapspace,evenif yourcalculationsindicatethatyou neednone.Linux usesswapspacesomewhataggressively, sothatasmuchphysicalmemoryaspossiblecanbekeptfree.Linux will swapout memorypagesthathavenot beenused,evenif thememoryis not yetneededfor anything.Thisavoidswaiting for swappingwhenit is needed:theswappingcanbedoneearlier,whenthedisk is otherwiseidle.

Swapspacecanbedividedamongseveraldisks.This cansometimesimproveperformance,dependingon therelativespeedsof thedisksandtheaccesspatternsof thedisks.Youmight wantto experimentwith a few schemes,but beawarethatdoingtheexperimentsproperlyis quitedifficult. You shouldnotbelieveclaimsthatany oneschemeis superiorto any other, sinceit won’t alwaysbetrue.

The buff er cacheReadingfrom adisk 3 is veryslow comparedto accessing(real)memory. In addition,it is commontoreadthesamepartof a diskseveraltimesduringrelatively shortperiodsof time.For example,onemightfirst readane-mailmessage,thenreadtheletterinto aneditorwhenreplyingto it, thenmake themailprogramreadit againwhencopying it to a folder. Or, considerhow oftenthecommandls might berunon a systemwith many users.By readingtheinformationfrom diskonly onceandthenkeepingit inmemoryuntil no longerneeded,onecanspeedup all but thefirst read.This is calleddiskbuffering, andthememoryusedfor thepurposeis calledthebuffer cache.

Sincememoryis, unfortunately, a finite, nay, scarceresource,thebuffer cacheusuallycannotbebigenough(it can’t hold all thedataoneeverwantsto use).Whenthecachefills up, thedatathathasbeenunusedfor thelongesttime is discardedandthememorythusfreedis usedfor thenew data.

67

Page 68: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter5. MemoryManagement

Disk bufferingworksfor writesaswell. On theonehand,datathatis written is oftensoonreadagain(e.g.,a sourcecodefile is savedto a file, thenreadby thecompiler),soputtingdatathatis written in thecacheis a goodidea.On theotherhand,by only puttingthedatainto thecache,not writing it to diskatonce,theprogramthatwritesrunsquicker. Thewritescanthenbedonein thebackground,withoutslowing down theotherprograms.

Most operatingsystemshavebuffer caches(althoughthey mightbecalledsomethingelse),but not all ofthemwork accordingto theaboveprinciples.Somearewrite-through: thedatais written to diskat once(it is keptin thecacheaswell, of course).Thecacheis calledwrite-back if thewritesaredoneat a latertime.Write-backis moreefficient thanwrite-through,but alsoa bit moreproneto errors:if themachinecrashes,or thepower is cutat abadmoment,or thefloppy is removedfrom thediskdrivebeforethedatain thecachewaiting to bewrittengetswritten, thechangesin thecacheareusuallylost.Thismight evenmeanthatthefilesystem(if thereis one)is not in full working order, perhapsbecausetheunwrittendataheldimportantchangesto thebookkeepinginformation.

Becauseof this,youshouldnever turn off thepowerwithoutusingapropershutdown procedure(seeChapter6), or removeafloppy from thediskdriveuntil it hasbeenunmounted(if it wasmounted)orafterwhateverprogramis usingit hassignaledthatit is finishedandthefloppy drive light doesn’t shineanymore.Thesynccommandflushesthebuffer, i.e., forcesall unwrittendatato bewritten to disk,andcanbeusedwhenonewantsto besurethateverythingis safelywritten. In traditionalUNIX systems,thereis a programcalledupdate runningin thebackgroundwhich doesa syncevery30 seconds,soit isusuallynot necessaryto usesync. Linux hasanadditionaldaemon,bdflush, whichdoesa moreimperfectsyncmorefrequentlyto avoid thesuddenfreezedueto heavy disk I/O thatsyncsometimescauses.

UnderLinux, bdflush is startedby update. Thereis usuallyno reasonto worry aboutit, but if bdflushhappensto die for somereason,thekernelwill warnaboutthis,andyoushouldstartit by hand(/sbin/update).

Thecachedoesnot actuallybuffer files,but blocks,which arethesmallestunitsof disk I/O (underLinux, they areusually1 kB). Thisway, alsodirectories,superblocks,otherfilesystembookkeepingdata,andnon-filesystemdisksarecached.

Theeffectivenessof acacheis primarily decidedby its size.A smallcacheis next to useless:it will holdsolittle datathatall cacheddatais flushedfrom thecachebeforeit is reused.Thecritical sizedependsonhow muchdatais readandwritten,andhow oftenthesamedatais accessed.Theonly way to know is toexperiment.

If thecacheis of a fixedsize,it is not verygoodto have it too big, either, becausethatmight makethefreememorytoo smallandcauseswapping(which is alsoslow). To make themostefficientuseof realmemory, Linux automaticallyusesall freeRAM for buffer cache,but alsoautomaticallymakesthecachesmallerwhenprogramsneedmorememory.

UnderLinux, you donot needto do anything to makeuseof thecache,it happenscompletelyautomatically. Exceptfor following theproperproceduresfor shutdown andremoving floppies,you do

68

Page 69: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter5. MemoryManagement

not needto worry aboutit.

Notes1. Thusquiteneedlesslyannoying a numberof computerscientistssomethinghorrible.

2. A gigabytehere,a gigabytethere,prettysoonwestarttalkingaboutrealmemory.

3. Excepta RAM disk, for obviousreasons.

69

Page 70: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 6. Boots And Shutdo wns

StartmeupAh... you’ve got to...you’ve got toNever, never never stopStartit upAh... startit up,never, never, neverYoumake a grown mancry,youmake a grown mancry

(Rolling Stones)

This sectionexplainswhatgoeson whena Linux systemis broughtupandtakendown, andhow itshouldbedoneproperly. If properproceduresarenot followed,filesmight becorruptedor lost.

An overview of boots and shutdo wnsTheactof turningon a computersystemandcausingits operatingsystemto beloaded1 is calledbooting. Thenamecomesfrom animageof thecomputerpulling itself up from its bootstraps,but theactitself slightly morerealistic.

Duringbootstrapping,thecomputerfirst loadsasmallpieceof codecalledthebootstrap loader, whichin turn loadsandstartstheoperatingsystem.Thebootstraploaderis usuallystoredin a fixedlocationona harddiskor a floppy. Thereasonfor this two stepprocessis thattheoperatingsystemis big andcomplicated,but thefirst pieceof codethatthecomputerloadsmustbeverysmall(a few hundredbytes),to avoid makingthefirmwareunnecessarilycomplicated.

Differentcomputersdo thebootstrappingdifferently. For PC’s, thecomputer(its BIOS) readsin thefirstsector(calledthebootsector) of afloppy or harddisk.Thebootstraploaderis containedwithin thissector. It loadstheoperatingsystemfrom elsewhereon thedisk (or from someotherplace).

After Linux hasbeenloaded,it initializesthehardwareanddevicedrivers,andthenrunsinit . init startsotherprocessesto allow usersto log in, anddo things.Thedetailsof this partwill bediscussedbelow.

In orderto shutdown aLinux system,first all processesaretold to terminate(thismakesthemcloseanyfilesanddo othernecessarythingsto keepthingstidy), thenfilesystemsandswapareasareunmounted,andfinally amessageis printedto theconsolethatthepowercanbeturnedoff. If theproperprocedureisnot followed,terriblethingscanandwill happen;mostimportantly, thefilesystembuffer cachemightnotbeflushed,whichmeansthatall datain it is lost andthefilesystemon disk is inconsistent,andthereforepossiblyunusable.

70

Page 71: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter6. BootsAndShutdowns

The boot process in closer lookYou canbootLinux eitherfrom a floppy or from theharddisk.Theinstallationsectionin theInstallationandGettingStartedguide(XXX citation)tells youhow to install Linux soyou canbootit thewayyouwantto.

Whena PCis booted,theBIOS will do variousteststo checkthateverythinglooksall right, 2 andwillthenstarttheactualbooting.It will chooseadiskdrive(typically thefirst floppy drive,if thereis afloppyinserted,otherwisethefirst harddisk, if oneis installedin thecomputer;theordermight beconfigurable,however)andwill thenreadits veryfirst sector. This is calledthebootsector; for aharddisk, it is alsocalledthemasterbootrecord, sincea harddiskcancontainseveralpartitions,eachwith their own bootsectors.

Thebootsectorcontainsa smallprogram(smallenoughto fit into onesector)whoseresponsibilityis toreadtheactualoperatingsystemfrom thediskandstartit. WhenbootingLinux from a floppy disk, thebootsectorcontainscodethatjust readsthefirst few hundredblocks(dependingon theactualkernelsize,of course)to a predeterminedplacein memory. Ona Linux bootfloppy, thereis no filesystem,thekernelis just storedin consecutivesectors,sincethis simplifiesthebootprocess.It is possible,however,to bootfrom a floppy with a filesystem,by usingLILO, theLInux LOader.

Whenbootingfrom theharddisk, thecodein themasterbootrecordwill examinethepartitiontable(alsoin themasterbootrecord),identify theactivepartition(thepartitionthatis markedto bebootable),readthebootsectorfrom thatpartition,andthenstartthecodein thatbootsector. Thecodein thepartition’sbootsectordoeswhata floppy disk’sbootsectordoes:it will readin thekernelfrom thepartitionandstartit. Thedetailsvary, however, sinceit is generallynot usefulto haveaseparatepartitionfor just thekernelimage,sothecodein thepartition’sbootsectorcan’t just readthedisk in sequentialorder, it hasto find thesectorswherever thefilesystemhasput them.Thereareseveralwaysaroundthisproblem,but themostcommonway is to useLILO. (Thedetailsabouthow to do this areirrelevantforthis discussion,however;seetheLILO documentationfor moreinformation;it is mostthorough.)

Whenbootingwith LILO, it will normallygo right aheadandreadin andbootthedefaultkernel.It isalsopossibleto configureLILO to beableto bootoneof severalkernels,or evenotheroperatingsystemsthanLinux, andit is possiblefor theuserto choosewhich kernelor operatingsystemis to bebootedatboottime.LILO canbeconfiguredsothatif oneholdsdown thealt, shift, or ctrl key at boottime (whenLILO is loaded),LILO will askwhatis to bebootedandnot bootthedefault right away. Alternatively,LILO canbeconfiguredsothatit will alwaysask,with anoptionaltimeoutthatwill causethedefaultkernelto bebooted.

With LILO, it is alsopossibleto givea kernelcommandline argument, afterthenameof thekerneloroperatingsystem.

Bootingfrom floppy andfrom harddiskhaveboththeir advantages,but generallybootingfrom theharddisk is nicer, sinceit avoidsthehassleof playingaroundwith floppies.It is alsofaster. However, it canbemoretroublesometo install thesystemto bootfrom theharddisk,somany peoplewill first bootfrom

71

Page 72: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter6. BootsAndShutdowns

floppy, then,whenthesystemis otherwiseinstalledandworking well, will install LILO andstartbootingfrom theharddisk.

After theLinux kernelhasbeenreadinto thememory, by whatevermeans,andis startedfor real,roughlythefollowing thingshappen:

• TheLinux kernelis installedcompressed,soit will first uncompressitself. Thebeginningof thekernelimagecontainsasmallprogramthatdoesthis.

• If youhavea super-VGA cardthatLinux recognizesandthathassomespecialtext modes(suchas100columnsby 40 rows),Linux asksyouwhichmodeyouwantto use.During thekernelcompilation,it is possibleto presetavideomode,sothatthis is neverasked.Thiscanalsobedonewith LILO or rdev.

• After this, thekernelcheckswhatotherhardwarethereis (harddisks,floppies,network adapters,etc),andconfiguressomeof its devicedriversappropriately;while it doesthis, it outputsmessagesaboutits findings.For example,whenI boot,I it lookslike this:

LILO boot:

Loading linux.

Console: colour EGA+ 80x25, 8 virtual consoles

Serial driver version 3.94 with no serial options enabled

tty00 at 0x03f8 (irq = 4) is a 16450

tty01 at 0x02f8 (irq = 3) is a 16450

lp_init: lp1 exists (0), using polling driver

Memory: 7332k/8192k available (300k kernel code, 384k reserved, 176k data)

Floppy drive(s): fd0 is 1.44M, fd1 is 1.2M

Loopback device init

Warning WD8013 board not found at i/o = 280.

Math coprocessor using irq13 error reporting.

Partition check:

hda: hda1 hda2 hda3

VFS: Mounted root (ext filesystem).

Linux version 0.99.pl9-1 (root@haven) 05/01/93 14:12:20

Theexacttextsaredifferenton differentsystems,dependingon thehardware,theversionof Linux beingused,andhow it hasbeenconfigured.

• Thenthekernelwill try to mounttherootfilesystem.Theplaceis configurableatcompilationtime,orany timewith rdev or LILO. Thefilesystemtypeis detectedautomatically. If themountingof therootfilesystemfails, for examplebecauseyou didn’t rememberto includethecorrespondingfilesystemdriver in thekernel,thekernelpanicsandhaltsthesystem(thereisn’t muchit cando,anyway).

Theroot filesystemis usuallymountedread-only(thiscanbesetin thesameway astheplace).Thismakesit possibleto checkthefilesystemwhile it is mounted;it is not a goodideato checkafilesystemthatis mountedread-write.

72

Page 73: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter6. BootsAndShutdowns

• After this, thekernelstartstheprograminit (locatedin /sbin/init) in thebackground(thiswillalwaysbecomeprocessnumber1). init doesvariousstartupchores.Theexactthingsit doesdependson how it is configured;seeChapter7 for moreinformation(not yetwritten). It will at leaststartsomeessentialbackgrounddaemons.

• init thenswitchesto multi-usermode,andstartsa getty for virtual consolesandseriallines.getty istheprogramwhich letspeoplelog in via virtual consolesandserialterminals.init mayalsostartsomeotherprograms,dependingon how it is configured.

• After this, thebootis complete,andthesystemis up andrunningnormally.

More about shutdo wnsIt is importantto follow thecorrectprocedureswhenyou shutdown aLinux system.If you fail doso,yourfilesystemsprobablywill becometrashedandthefilesprobablywill becomescrambled.This isbecauseLinux hasa diskcachethatwon’t write thingsto diskatonce,but only at intervals.This greatlyimprovesperformancebut alsomeansthatif you just turn off thepowerat a whim thecachemayhold alot of dataandthatwhatis on thediskmaynot bea fully working filesystem(becauseonly somethingshavebeenwritten to thedisk).

Anotherreasonagainstjust flipping thepowerswitchis thatin amulti-taskingsystemtherecanbelotsof thingsgoingon in thebackground,andshuttingthepowercanbequitedisastrous.By usingthepropershutdown sequence,youensurethatall backgroundprocessescansave their data.

Thecommandfor properlyshuttingdown a Linux systemis shutdown. It is usuallyusedin oneof twoways.

If you arerunningasystemwhereyou aretheonly user, theusualwayof usingshutdown is to quit allrunningprograms,log out on all virtual consoles,log in asrooton oneof them(or stayloggedin asrootif youalreadyare,but youshouldchangeto root’shomedirectoryor therootdirectory, to avoidproblemswith unmounting),thengive thecommandshutdown -h now (substitutenow with aplussignanda numberin minutesif you wanta delay, thoughyouusuallydon’t on a singleusersystem).

Alternatively, if yoursystemhasmany users,usethecommandshutdown -h +time message, wheretime is thetime in minutesuntil thesystemis halted,andmessage is ashortexplanationof why thesystemis shuttingdown.

# shutdown -h +10 ’We will install a new disk. System should> be back on-line in three hours.’#

This will warneverybodythatthesystemwill shutdown in tenminutes,andthatthey’d bettergetlost orlosedata.Thewarningis printedto every terminalon which someoneis loggedin, includingall xterms:

73

Page 74: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter6. BootsAndShutdowns

Broadcast message from root (ttyp0) Wed Aug 2 01:03:25 1995...

We will install a new disk. System should

be back on-line in three hours.

The system is going DOWN for system halt in 10 minutes !!

Thewarningis automaticallyrepeateda few timesbeforetheboot,with shorterandshorterintervalsasthetime runsout.

Whentherealshuttingdown startsafterany delays,all filesystems(excepttherootone)areunmounted,userprocesses(if anybodyis still loggedin) arekilled, daemonsareshutdown, all filesystemareunmounted,andgenerallyeverythingsettlesdown. Whenthatis done,init printsout a messagethatyoucanpowerdown themachine.Then,andonly then,shouldyou moveyourfingerstowardsthepowerswitch.

Sometimes,althoughrarelyon any goodsystem,it is impossibleto shutdown properly. For instance,ifthekernelpanicsandcrashesandburnsandgenerallymisbehaves,it mightbecompletelyimpossibletogiveany new commands,henceshuttingdown properlyis somewhatdifficult, andjustabouteverythingyou cando is hopethatnothinghasbeentoo severelydamagedandturn off thepower. If thetroublesarea bit lesssevere(say, somebodyhit yourkeyboardwith anaxe),andthekernelandtheupdateprogramstill runnormally, it is probablya goodideato wait acoupleof minutesto giveupdatea chanceto flushthebuffer cache,andonly cut thepowerafterthat.

Somepeoplelike to shutdown usingthecommandsync3 threetimes,waiting for thedisk I/O to stop,thenturnoff thepower. If thereareno runningprograms,this is aboutequivalentto usingshutdown.However, it doesnot unmountany filesystemsandthis canleadto problemswith theext2fs “cleanfilesystem”flag.Thetriple-syncmethodis not recommended.

(In caseyou’rewondering:thereasonfor threesyncsis thatin theearlydaysof UNIX, whenthecommandsweretypedseparately, thatusuallygavesufficient time for mostdisk I/O to befinished.)

RebootingRebootingmeansbootingthesystemagain.Thiscanbeaccomplishedby first shuttingit downcompletely, turningpoweroff, andthenturningit backon.A simplerway is to askshutdown to rebootthesystem,insteadof merelyhaltingit. This is accomplishedby usingthe-r optionto shutdown, forexample,by giving thecommandshutdown -r now.

Most Linux systemsrun shutdown -r now whenctrl-alt-delis pressedon thekeyboard.This rebootsthesystem.Theactionon ctrl-alt-delis configurable,however, andit mightbebetterto allow for somedelaybeforetherebooton a multiusermachine.Systemsthatarephysicallyaccessibleto anyonemight evenbeconfiguredto donothingwhenctrl-alt-del is pressed.

74

Page 75: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter6. BootsAndShutdowns

Single user modeTheshutdown commandcanalsobeusedto bring thesystemdown to singleusermode,in whichnoonecanlog in, but root canusetheconsole.This is usefulfor systemadministrationtasksthatcan’t bedonewhile thesystemis runningnormally.

Emergency boot floppiesIt is not alwayspossibleto boota computerfrom theharddisk.For example,if you makea mistake inconfiguringLILO, you mightmakeyoursystemunbootable.For thesesituations,youneedanalternativeway of bootingthatwill alwayswork (aslongasthehardwareworks).For typical PC’s, this meansbootingfrom thefloppy drive.

Most Linux distributionsallow oneto createanemergencybootfloppyduringinstallation.It is a goodideato do this.However, somesuchbootdiskscontainonly thekernel,andassumeyou will beusingtheprogramson thedistribution’s installationdisksto fix whateverproblemyouhave.Sometimesthoseprogramsaren’t enough;for example,youmight haveto restoresomefiles from backupsmadewithsoftwarenot on theinstallationdisks.

Thus,it mightbenecessaryto createa customroot floppy aswell. TheBootdiskHOWTO by GrahamChapman(XXX citation)containsinstructionsfor doingthis.Youmust,of course,rememberto keepyouremergency bootandroot floppiesup to date.

You can’t usethefloppy driveyouuseto mounttheroot floppy for anythingelse.Thiscanbeinconvenientif youonly haveonefloppy drive.However, if youhaveenoughmemory, you canconfigureyourbootfloppy to loadtherootdisk to a ramdisk(thebootfloppy’skernelneedsto bespeciallyconfiguredfor this).Oncetherootfloppy hasbeenloadedinto theramdisk,thefloppy drive is freetomountotherdisks.

Notes1. Onearlycomputers,it wasn’t enoughto merelyturnon thecomputer, youhadto manuallyloadthe

operatingsystemaswell. Thesenew-fangledthing-a-ma-jigsdo it all by themselves.

2. This is calledthepoweron selftest, or POSTfor short.

3. syncflushesthebuffer cache.

75

Page 76: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 7. init

“Uuno on numeroyksi” (Sloganfor a seriesof Finnishmovies.)

This chapterdescribesthe init process,which is thefirst userlevel processstartedby thekernel.init hasmany importantduties,suchasstartinggetty (sothatuserscanlog in), implementingrun levels,andtakingcareof orphanedprocesses.Thischapterexplainshow init is configuredandhow youcanmakeuseof thedifferentrun levels.

init comes fir stinit is oneof thoseprogramsthatareabsolutelyessentialto theoperationof aLinux system,but thatyoustill canmostlyignore.A goodLinux distributionwill comewith aconfigurationfor init thatwill workfor mostsystems,andon thesesystemsthereis nothingyou needto do aboutinit . Usually, you onlyneedto worry aboutinit if youhookup serialterminals,dial-in (not dial-out)modems,or if youwanttochangethedefault run level.

Whenthekernelhasstarteditself (hasbeenloadedinto memory, hasstartedrunning,andhasinitializedall devicedriversanddatastructuresandsuch),it finishesits own partof thebootprocessby startingauserlevel program,init . Thus,init is alwaysthefirst process(its processnumberis always1).

Thekernellooksfor init in a few locationsthathavebeenhistoricallyusedfor it, but theproperlocationfor it (onaLinux system)is /sbin/init. If thekernelcan’t find init , it triesto run/bin/sh, andif thatalsofails, thestartupof thesystemfails.

Wheninit starts,it finishesthebootprocessby doinga numberof administrative tasks,suchascheckingfilesystems,cleaningup/tmp, startingvariousservices,andstartingagetty for eachterminalandvirtualconsolewhereusersshouldbeableto log in (seeChapter8).

After thesystemis properlyup, init restartsgetty for eachterminalaftera userhasloggedout (sothatthenext usercanlog in). init alsoadoptsorphanprocesses:whenaprocessstartsachild processanddiesbeforeits child, thechild immediatelybecomesa child of init . This is importantfor varioustechnicalreasons,but it is goodto know it, sinceit makesit easierto understandprocesslistsandprocesstreegraphs.1 Therearea few variantsof init available.MostLinux distributionsusesysvinit (written byMiquel vanSmoorenburg),which is basedon theSystemV init design.TheBSDversionsof Unix havea differentinit . Theprimarydifferenceis run levels:SystemV hasthem,BSDdoesnot (at leasttraditionally).This differenceis not essential.We’ll look at sysvinit only.

76

Page 77: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter7. init

Configuring init to star t getty: the /etc/inittab fileWhenit startsup, init readsthe/etc/inittab configurationfile. While thesystemis running,it willre-readit, if senttheHUP signal;2 this featuremakesit unnecessaryto bootthesystemto makechangesto the init configurationtakeeffect.

The/etc/inittab file is abit complicated.We’ll startwith thesimplecaseof configuringgetty lines.Linesin /etc/inittab consistof four colon-delimitedfields:

id:runlevels:action:process

Thefieldsaredescribedbelow. In addition,/etc/inittab cancontainemptylines,andlinesthatbeginwith anumbersign(‘#’); thesearebothignored.

id

This identifiestheline in thefile. For getty lines,it specifiestheterminalit runson(thecharactersafter/dev/tty in thedevicefile name).For otherlines,it doesn’t matter(exceptfor lengthrestrictions),but it shouldbeunique.

runle vels

Therun levelstheline shouldbeconsideredfor. Therun levelsaregivenassingledigits,withoutdelimiters.(Runlevelsaredescribedin thenext section.)

action

Whatactionshouldbetakenby theline, e.g.,respawn to run thecommandin thenext field again,whenit exits, or once to run it justonce.

process

Thecommandto run.

To starta getty on thefirst virtual terminal(/dev/tty1), in all thenormalmulti-userrun levels(2-5),onewouldwrite thefollowing line:

1:2345:respawn:/sbin/getty 9600 tty1

77

Page 78: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter7. init

Thefirst field saysthatthis is theline for /dev/tty1. Thesecondfield saysthatit appliesto run levels2, 3, 4, and5. Thethird field meansthatthecommandshouldberunagain,afterit exits (sothatonecanlog in, log out,andthenlog in again).Thelastfield is thecommandthatrunsgetty on thefirst virtualterminal.3

If you wantedto addterminalsor dial-in modemlinesto a system,you’d addmorelinesto/etc/inittab, onefor eachterminalor dial-in line. For moredetails,seethemanualpagesinit ,inittab, andgetty.

If a commandfailswhenit starts,andinit is configuredto restart it, it will usea lot of systemresources:init startsit, it fails, init startsit, it fails, init startsit, it fails,andsoon,adinfinitum. Topreventthis, init will keeptrackof how oftenit restartsacommand,andif thefrequency growsto high,it will delayfor fiveminutesbeforerestartingagain.

Run levelsA run level is a stateof init andthewholesystemthatdefineswhatsystemservicesareoperating.Runlevelsareidentifiedby numbers,seeTable7-1.Thereis no consensusof how to usetheuserdefinedrunlevels(2 through5). Somesystemadministratorsuserun levelsto definewhich subsystemsareworking,e.g.,whetherX is running,whetherthenetwork is operational,andsoon.Othershaveall subsystemsalwaysrunningor startandstopthemindividually, withoutchangingrun levels,sincerun levelsaretoocoarsefor controllingtheir systems.You needto decidefor yourself,but it might beeasiestto follow theway yourLinux distributiondoesthings.

Table 7-1.Run level numbers

0 Halt thesystem.

1 Single-usermode(for specialadministration).

2-5 Normaloperation(userdefined).

6 Reboot.

Runlevelsareconfiguredin /etc/inittab by lineslike thefollowing:

l2:2:wait:/etc/init.d/rc 2

Thefirst field is anarbitrarylabel,thesecondonemeansthatthis appliesfor run level 2. Thethird fieldmeansthat init shouldrun thecommandin thefourthfield once,whentherun level is entered,andthatinit shouldwait for it to complete.The/etc/init.d/rc commandrunswhatevercommandsarenecessaryto startandstopservicesto enterrun level 2.

78

Page 79: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter7. init

Thecommandin thefourth field doesall thehardwork of settingupa run level. It startsservicesthataren’t alreadyrunning,andstopsservicesthatshouldn’t berunningin thenew run level any more.Exactlywhatthecommandis, andhow run levelsareconfigured,dependson theLinux distribution.

Wheninit starts,it looksfor a line in /etc/inittab thatspecifiesthedefault run level:

id:2:initdefault:

You canaskinit to go to a non-default run level at startupby giving thekernelacommandline argumentof single or emergency. Kernelcommandline argumentscanbegivenvia LILO, for example.Thisallowsyou to choosethesingleusermode(run level 1).

While thesystemis running,the telinit commandcanchangetherun level. Whentherun level ischanged,init runstherelevantcommandfrom /etc/inittab.

Special configuration in /etc/inittab

The/etc/inittab hassomespecialfeaturesthatallow init to reactto specialcircumstances.Thesespecialfeaturesaremarkedby specialkeywordsin thethird field. Someexamples:

powerwait

Allows init to shutthesystemdown, whenthepower fails.This assumestheuseof aUPS,andsoftwarethatwatchestheUPSandinforms init thatthepower is off.

ctrlaltdel

Allows init to rebootthesystem,whentheuserpressesctrl-alt-delon theconsolekeyboard.Notethatthesystemadministratorcanconfigurethereactionto ctrl-alt-delto besomethingelseinstead,e.g.,to beignored,if thesystemis in apublic location.(Or to startnethack.)

sysinit

Commandto berunwhenthesystemis booted.Thiscommandusuallycleansup/tmp, for example.

Thelist above is not exhaustive.Seeyourinittab manualpagefor all possibilities,andfor detailsonhow to usetheaboveones.

79

Page 80: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter7. init

Booting in single user modeAn importantrun level is singleusermode(run level 1), in whichonly thesystemadministratoris usingthemachineandasfew systemservices,includinglogins,aspossiblearerunning.Singleusermodeisnecessaryfor a few administrative tasks,4 suchasrunningfsck on a/usr partition,sincethis requiresthatthepartitionbeunmounted,andthatcan’t happen,unlessjust aboutall systemservicesarekilled.

A runningsystemcanbetakento singleusermodeby usingtelinit to requestrun level 1. At bootup,itcanbeenteredby giving thewordsingle or emergency on thekernelcommandline: thekernelgivesthecommandline to init aswell, andinit understandsfrom thatword thatit shouldn’t usethedefault runlevel. (Thekernelcommandline is enteredin away thatdependson how youbootthesystem.)

Bootinginto singleusermodeis sometimesnecessarysothatonecanrun fsck by hand,beforeanythingmountsor otherwisetouchesa broken/usr partition(any activity on a brokenfilesystemis likely tobreakit more,so fsck shouldberunassoonaspossible).

Thebootupscriptsinit runswill automaticallyentersingleusermode,if theautomaticfsck atbootupfails.This is anattemptto preventthesystemfrom usingafilesystemthatis sobrokenthatfsck can’t fixit automatically. Suchbreakageis relatively rare,andusuallyinvolvesa brokenharddiskor anexperimentalkernelrelease,but it’sgoodto beprepared.

As a securitymeasure,aproperlyconfiguredsystemwill askfor therootpassword beforestartingtheshell in singleusermode.Otherwise,it wouldbesimpleto just enterasuitableline to LILO to getin asroot. (Thiswill breakif /etc/passwd hasbeenbrokenby filesystemproblems,of course,andin thatcaseyou’d betterhaveabootfloppy handy.)

Notes1. init itself is not allowedto die.You can’t kill init evenwith SIGKILL.

2. Usingthecommandkill -HUP 1 asroot, for example

3. Differentversionsof getty arerun differently. Consultyourmanualpage,andmakesureit is thecorrectmanualpage.

4. It probablyshouldn’t beusedfor playingnethack.

80

Page 81: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 8. Log ging In And Out

“I don’t careto belongto a club thatacceptspeoplelike measa member.” (GrouchoMarx)

This sectiondescribeswhathappenswhenauserlogsin or out.Thevariousinteractionsof backgroundprocesses,log files,configurationfiles,andsoon aredescribedin somedetail.

Logins via terminalsFigure8-1showshow loginshappenvia terminals.First, init makessurethereis agetty programfor theterminalconnection(or console).getty listensat theterminalandwaitsfor theuserto notify thatheisreadyto login in (thisusuallymeansthattheusermusttypesomething).Whenit noticesa user, gettyoutputsawelcomemessage(storedin /etc/issue), andpromptsfor theusername,andfinally runsthelogin program.login getstheusernameasaparameter, andpromptstheuserfor thepassword. If thesematch,login startstheshellconfiguredfor theuser;elseit just exits andterminatestheprocess(perhapsaftergiving theuseranotherchanceat enteringtheusernameandpassword). init noticesthattheprocessterminated,andstartsanew getty for theterminal.

81

Page 82: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter8. Logging In AndOut

Figure8-1.Logins via terminals: the interaction of init, getty, login, and the shell.

init: fork +

exec("/sbin/getty")

getty: wait for user

do they match?

login: exec("/bin/sh")

sh: read and execute

commands

sh: exit

login: exit

login: read password

exec("/bin/login")

getty: read username,

no

yes

START

82

Page 83: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter8. Logging In AndOut

Notethattheonly new processis theonecreatedby init (usingthefork systemcall); getty andloginonly replacetheprogramrunningin theprocess(usingtheexec systemcall).

A separateprogram,for noticingtheuser, is neededfor seriallines,sinceit canbe(andtraditionallywas)complicatedto noticewhena terminalbecomesactive.getty alsoadaptsto thespeedandothersettingsof theconnection,which is importantespeciallyfor dial-in connections,wheretheseparametersmaychangefrom call to call.

Thereareseveralversionsof getty andinit in use,all with their goodandbadpoints.It is agoodideatolearnabouttheversionsonyoursystem,andalsoabouttheotherversions(youcouldusetheLinuxSoftwareMap to searchthem).If you don’t havedial-in’s,you probablydon’t have to worry aboutgetty,but init is still important.

Logins via the netw orkTwo computersin thesamenetwork areusuallylinkedvia asinglephysicalcable.Whentheycommunicateover thenetwork, theprogramsin eachcomputerthattake partin thecommunicationarelinkedvia a virtual connection, asortof imaginarycable.As far astheprogramsat eitherendof thevirtual connectionareconcerned,they haveamonopolyon theirown cable.However, sincethecableisnot real,only imaginary, theoperatingsystemsof bothcomputerscanhaveseveralvirtual connectionssharethesamephysicalcable.This way, usingjust asinglecable,severalprogramscancommunicatewithout having to know of or careabouttheothercommunications.It is evenpossibleto haveseveralcomputersusethesamecable;thevirtual connectionsexist betweentwo computers,andtheothercomputersignorethoseconnectionsthatthey don’t take partin.

That’sacomplicatedandover-abstracteddescriptionof thereality. It might,however, begoodenoughtounderstandtheimportantreasonwhy network loginsaresomewhatdifferentfrom normallogins.Thevirtual connectionsareestablishedwhentherearetwo programson differentcomputersthatwish tocommunicate.Sinceit is in principlepossibleto login from any computerin anetwork to any othercomputer, thereis ahugenumberof potentialvirtual communications.Becauseof this, it is not practicalto starta getty for eachpotentiallogin.

Thereis asingleprocessinetd(correspondingto getty) thathandlesall network logins.Whenit noticesanincomingnetwork login (i.e., it noticesthatit getsa new virtual connectionto someothercomputer),it startsanew processto handlethatsinglelogin. Theoriginalprocessremainsandcontinuesto listenfornew logins.

To makethingsa bit morecomplicated,thereis morethanonecommunicationprotocolfor networklogins.Thetwo mostimportantonesaretelnet andrlogin . In additionto logins,therearemany othervirtual connectionsthatmaybemade(for FTP, Gopher, HTTP, andothernetwork services).It would beineffectiveto haveaseparateprocesslisteningfor aparticulartypeof connection,soinsteadthereis onlyonelistenerthatcanrecognizethetypeof theconnectionandcanstartthecorrecttypeof programto

83

Page 84: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter8. Logging In AndOut

providetheservice.This singlelisteneris calledinetd; seetheLinuxNetworkAdministrators’ Guideformoreinformation.

What login doesThe login programtakescareof authenticatingtheuser(makingsurethattheusernameandpasswordmatch),andof settingup aninitial environmentfor theuserby settingpermissionsfor theserialline andstartingtheshell.

Part of theinitial setupis outputtingthecontentsof thefile /etc/motd (shortfor messageof theday)andcheckingfor electronicmail. Thesecanbedisabledby creatingafile called.hushlogin in theuser’shomedirectory.

If thefile /etc/nologin exists,loginsaredisabled.Thatfile is typically createdby shutdown andrelatives.login checksfor this file, andwill refuseto accepta login if it exists.If it doesexist, loginoutputsits contentsto theterminalbeforeit quits.

login logsall failedlogin attemptsin a systemlog file (via syslog). It alsologsall loginsby root.Bothofthesecanbeusefulwhentrackingdown intruders.

Currentlyloggedin peoplearelistedin /var/run/utmp. Thisfile is valid only until thesystemis nextrebootedor shutdown; it is clearedwhenthesystemis booted.It listseachuserandtheterminal(ornetwork connection)heis using,alongwith someotherusefulinformation.Thewho, w, andothersimilar commandslook in utmp to seewho areloggedin.

All successfulloginsarerecordedinto /var/log/wtmp. This file will grow without limit, soit mustbecleanedregularly, for exampleby having a weeklycron job to clearit. 1 The last commandbrowseswtmp.

Bothutmp andwtmp arein abinaryformat(seetheutmp manualpage);it is unfortunatelynotconvenientto examinethemwithoutspecialprograms.

X and xdmXXX X implementsloginsvia xdm;also:xterm-ls

Access contr ol

84

Page 85: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter8. Logging In AndOut

Theuserdatabaseis traditionallycontainedin the/etc/passwd file. Somesystemsuseshadowpasswords, andhavemovedthepasswordsto /etc/shadow. Siteswith many computersthatsharetheaccountsuseNIS or someothermethodto storetheuserdatabase;they might alsoautomaticallycopythedatabasefrom onecentrallocationto all othercomputers.

Theuserdatabasecontainsnot only thepasswords,but alsosomeadditionalinformationabouttheusers,suchastheir realnames,homedirectories,andlogin shells.Thisotherinformationneedsto bepublic,sothatanyonecanreadit. Thereforethepassword is storedencrypted.Thisdoeshavethedrawbackthatanyonewith accessto theencryptedpassword canusevariouscryptographicalmethodsto guessit,without trying to actuallylog into thecomputer. Shadow passwordstry to avoid this by moving thepassword into anotherfile, which only root canread(thepassword is still storedencrypted).However,installingshadow passwordslaterontoasystemthatdid not supportthemcanbedifficult.

With or withoutpasswords,it is importantto makesurethatall passwordsin asystemaregood,i.e.,noteasilyguessable.Thecrack programcanbeusedto crackpasswords;any password it canfind is bydefinitionnot a goodone.While crack canberunby intruders,it canalsoberun by thesystemadminstratorto avoid badpasswords.Goodpasswordscanalsobeenforcedby thepasswdprogram;thisis in factmoreeffective in CPUcycles,sincecrackingpasswordsrequiresquitea lot of computation.

Theusergroupdatabaseis keptin /etc/group; for systemswith shadow passwords,therecanbea/etc/shadow.group.

root usuallycan’t login via mostterminalsor thenetwork, only via terminalslistedin the/etc/securetty file. This makesit necessaryto getphysicalaccessto oneof theseterminals.It is,however, possibleto log in via any terminalasany otheruser, andusethesucommandto becomeroot.

Shell star tupWhenaninteractive login shellstarts,it automaticallyexecutesoneor morepre-definedfiles.Differentshellsexecutedifferentfiles; seethedocumentationof eachshellfor furtherinformation.

Most shellsfirst runsomeglobalfile, for example,theBourneshell(/bin/sh) andits derivativesexecute/etc/profile; in addition,they execute.profile in theuser’shomedirectory. /etc/profileallows thesystemadministratorto havesetup acommonuserenvironment,especiallyby settingthePATH to includelocal commanddirectoriesin additionto thenormalones.On theotherhand,.profile allows theuserto customizetheenvironmentto hisown tastesby overriding,if necessary, thedefault environment.

Notes1. GoodLinux distributionsdo this out of thebox.

85

Page 86: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 9. Managing user accounts

“The similaritiesof sysadminsanddrugdealers:bothmeasurestuff in K’s,andbothhave users.”(Old, tiredcomputerjoke.)

This chapterexplainshow to createnew useraccounts,how to modify thepropertiesof thoseaccounts,andhow to removetheaccounts.DifferentLinux systemshavedifferenttoolsfor doingthis.

What’s an account?Whena computeris usedby many peopleit is usuallynecessaryto differentiatebetweentheusers,forexample,sothattheir privatefilescanbekeptprivate.This is importantevenif thecomputercanonly beusedby a singlepersonata time,aswith mostmicrocomputers.1 Thus,eachuseris givena uniqueusername,andthatnameis usedto log in.

There’smoreto auserthanjust aname,however. An accountis all thefiles,resources,andinformationbelongingto oneuser. Thetermhintsat banks,andin a commercialsystemeachaccountusuallyhassomemoney attachedto it, andthatmoney vanishesat differentspeedsdependingon how muchtheuserstressesthesystem.For example,disk spacemighthaveapricepermegabyteandday, andprocessingtime mighthaveapricepersecond.

Creating a userTheLinux kernelitself treatsusersaremerenumbers.Eachuseris identifiedby auniqueinteger, theuserid or uid, becausenumbersarefasterandeasierfor acomputerto processthantextual names.Aseparatedatabaseoutsidethekernelassignsa textual name,theusername, to eachuserid. Thedatabasecontainsadditionalinformationaswell.

To createauser, youneedto addinformationabouttheuserto theuserdatabase,andcreatea homedirectoryfor him. It mayalsobenecessaryto educatetheuser, andsetup a suitableinitial environmentfor him.

Most Linux distributionscomewith a programfor creatingaccounts.Thereareseveralsuchprogramsavailable.Two commandline alternativesareadduseranduseradd; theremaybeaGUI tool aswell.Whatever theprogram,theresultis thatthereis little if any manualwork to bedone.Evenif thedetailsaremany andintricate,theseprogramsmakeeverythingseemtrivial. However, thesectioncalledCreatinga userbyhanddescribeshow to do it by hand.

86

Page 87: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter9. Managinguseraccounts

/etc/passwd and other inf ormative filesThebasicuserdatabasein a Unix systemis thetext file, /etc/passwd (calledthepassword file), whichlists all valid usernamesandtheir associatedinformation.Thefile hasoneline perusername,andisdividedinto sevencolon-delimitedfields:

• Username.

• Password, in anencryptedform.

• Numericuserid.

• Numericgroupid.

• Full nameor otherdescriptionof account.

• Homedirectory.

• Login shell(programto runat login).

Theformatis explainedin moredetailon thepasswd manualpage.

Any useron thesystemmayreadthepasswordfile, sothatthey can,for example,learnthenameofanotheruser. This meansthatthepassword (thesecondfield) is alsoavailableto everyone.Thepasswordfile encryptsthepassword,soin theorythereis no problem.However, theencryptionis breakable,especiallyif thepassword is weak(e.g.,it is shortor it canbefoundin adictionary).Thereforeit is not agoodideato have thepassword in thepassword file.

Many Linux systemshaveshadowpasswords. This is analternativewayof storingthepassword: theencryptedpassword is storedin a separatefile, /etc/shadow, whichonly rootcanread.The/etc/passwd file only containsaspecialmarker in thesecondfield. Any programthatneedsto verify auseris setuid,andcanthereforeaccesstheshadow passwordfile. Normalprograms,which only usetheotherfieldsin thepasswordfile, can’t getat thepassword. 2

Picking numeric user and group idsOnmostsystemsit doesn’t matterwhatthenumericuserandgroupids are,but if you usetheNetworkfilesystem(NFS),youneedto have thesameuid andgid on all systems.This is becauseNFSalsoidentifiesuserswith thenumericuids.If youaren’t usingNFS,youcanlet youraccountcreationtoolpick themautomatically.

If you areusingNFS,you’ll have to beinventa mechanismfor synchronizingaccountinformation.Onealternative is to theNIS system(seeXXX network-admin-guide).

However, youshouldtry to avoid re-usingnumericuid’s (andtextualusernames),becausethenew ownerof theuid (or username)maygetaccessto theold owner’sfiles (or mail, or whatever).

87

Page 88: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter9. Managinguseraccounts

Initial envir onment: /etc/skel

Whenthehomedirectoryfor anew useris created,it is initializedwith files from the/etc/skeldirectory. Thesystemadministratorcancreatefiles in /etc/skel thatwill provideanicedefaultenvironmentfor users.For example,hemightcreatea/etc/skel/.profile thatsetstheEDITORenvironmentvariableto someeditorthatis friendly towardsnew users.

However, it is usuallybestto try to keep/etc/skel assmallaspossible,sinceit will benext toimpossibleto updateexisting users’files.For example,if thenameof thefriendly editorchanges,allexisting userswould haveto edit their.profile. Thesystemadministratorcouldtry to do itautomatically, with ascript,but thatis almostcertaingoingto breaksomeone’sfile.

Wheneverpossible,it is betterto put globalconfigurationinto globalfiles,suchas/etc/profile. Thisway it is possibleto updateit withoutbreakingusers’own setups.

Creating a user by handTo createanew accountmanually, follow thesesteps:

• Edit /etc/passwd with vipw andadda new line for thenew account.Becarefulwith thesyntax.Donot edit directlywith aneditor! vipw locksthefile, sothatothercommandswon’t try to updateit atthesametime.You shouldmakethepasswordfield be‘*’, sothatit is impossibleto log in.

• Similarly, edit/etc/group with vigr , if youneedto createa new groupaswell.

• Createthehomedirectoryof theuserwith mkdir .

• Copy thefiles from /etc/skel to thenew homedirectory.

• Fix ownershipsandpermissionswith chown andchmod. The-R optionis mostuseful.Thecorrectpermissionsvarya little from onesiteto another, but usuallythefollowing commandsdo therightthing:

cd /home/newusernamechown -R username.group .chmod -R go=u,go-w .chmod go= .

• Setthepassword with passwd.

After yousetthepassword in thelaststep,theaccountwill work. You shouldn’t setit until everythingelsehasbeendone,otherwisetheusermayinadvertentlylog in while you’restill copying thefiles.

It is sometimesnecessaryto createdummyaccounts3 thatarenot usedby people.For example,to setupananonymousFTPserver (sothatanyonecandownloadfiles from it, withouthaving to getanaccount

88

Page 89: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter9. Managinguseraccounts

first), youneedto createanaccountcalledftp. In suchcases,it is usuallynot necessaryto setthepassword (laststepabove).Indeed,it is betternot to, sothatno-onecanusetheaccount,unlessthey firstbecomeroot,sincerootcanbecomeany user.

Changing user proper tiesTherearea few commandsfor changingvariouspropertiesof anaccount(i.e., therelevantfield in/etc/passwd):

chfn

Changethefull namefield.

chsh

Changethelogin shell.

pass wd

Changethepassword.

Thesuper-usermayusethesecommandsto changethepropertiesof any account.Normaluserscanonlychangethepropertiesof their own account.It maysometimesbenecessaryto disablethesecommands(with chmod) for normalusers,for examplein anenvironmentwith many noviceusers.

Othertasksneedto bedoneby hand.For example,to changetheusername,youneedto edit/etc/passwd directly (with vipw, remember).Likewise,to addor removetheuserto moregroups,youneedto edit/etc/group (with vigr ). Suchtaskstendto berare,however, andshouldbedonewithcaution:for example,if youchangetheusername,e-mailwill no longerreachtheuser, unlessyoualsocreatea mail alias.4

Removing a user

89

Page 90: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter9. Managinguseraccounts

To removeauser, youfirst removeall his files,mailboxes,mail aliases,print jobs,cron andat jobs,andall otherreferencesto theuser. Thenyou removetherelevantlinesfrom /etc/passwd and/etc/group (rememberto removetheusernamefrom all groupsit’sbeenaddedto). It maybeagoodideato first disabletheaccount(seebelow), beforeyoustartremoving stuff, to preventtheuserfromusingtheaccountwhile it is beingremoved.

Rememberthatusersmayhavefilesoutsidetheir homedirectory. Thefind commandcanfind them:

find / -user username

However, notethattheabovecommandwill takea long time, if youhave largedisks.If you mountnetwork disks,you needto becarefulsothatyouwon’t trashthenetwork or theserver.

SomeLinux distributionscomewith specialcommandsto do this; look for deluseror userdel. However,it is easyto do it by handaswell, andthecommandsmight not doeverything.

Disab ling a user temporaril yIt is sometimesnecessaryto temporarilydisableanaccount,without removing it. For example,theusermight nothavepaidhis fees,or thesystemadministratormaysuspectthatacrackerhasgot thepasswordof thataccount.

Thebestway to disableanaccountis to changeits shellinto aspecialprogramthatjustprintsamessage.This way, whoever triesto log into theaccount,will fail, andwill know why. Themessagecantell theuserto contactthesystemadministratorsothatany problemsmaybedealtwith.

It wouldalsobepossibleto changetheusernameor password to somethingelse,but thentheuserwon’tknow whatis goingon.Confusedusersmeanmorework. 5

A simpleway to createthespecialprogramsis to write ‘tail scripts’:

#!/usr/bin/tail +2This account has been closed due to a security breach.Please call 555-1234 and wait for the men in black to arrive.

Thefirst two characters(‘#!’) tell thekernelthattherestof theline is acommandthatneedsto berun tointerpretthis file. Thetail commandin this caseoutputseverythingexceptthefirst line to thestandardoutput.

If userbillg is suspectedof a securitybreach,thesystemadministratorwould dosomethinglike this:

# chsh -s /usr/local/lib/no-login/security billg# su - testerThis account has been closed due to a security breach.

90

Page 91: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter9. Managinguseraccounts

Please call 555-1234 and wait for the men in black to arrive.#

Thepurposeof thesu is to testthatthechangeworked,of course.

Tail scriptsshouldbekeptin a separatedirectory, sothattheir namesdon’t interferewith normalusercommands.

Notes1. It mightbequiteembarrassingif my sistercouldreadmy love letters.

2. Yes,this meansthatthepassword file hasall theinformationabouta userexcepthispassword.Thewonderof development.

3. Surrealusers?

4. Theuser’snamemightchangedueto marriage,for example,andhemightwantto havehisusernamereflecthis new name.

5. But they canbeso fun, if you’rea BOFH.

91

Page 92: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 10. Backups

Hardwareis indeterministicallyreliable.Softwareis deterministicallyunreliable.Peopleareindeterministicallyunreliable.Natureis deterministicallyreliable.

This chapterexplainsaboutwhy, how, andwhento makebackups,andhow to restorethingsfrombackups.

On the impor tance of being backed upYourdatais valuable.It will costyou timeandeffort re-createit, andthatcostsmoney or at leastpersonalgrief andtears;sometimesit can’t evenbere-created,e.g.,if it is theresultsof someexperiments.Sinceit is aninvestment,youshouldprotectit andtake stepsto avoid losingit.

Therearebasicallyfour reasonswhy youmight losedata:hardwarefailures,softwarebugs,humanaction,or naturaldisasters.1 Althoughmodernhardwaretendsto bequitereliable,it canstill breakseeminglyspontaneously. Themostcritical pieceof hardwarefor storingdatais theharddisk,whichrelieson tiny magneticfieldsremainingintact in a world filled with electromagneticnoise.Modernsoftwaredoesn’t eventendto bereliable;a rocksolidprogramis anexception,not a rule.Humansarequiteunreliable,they will eithermakeamistake,or they will bemaliciousanddestroy dataon purpose.Naturemightnot beevil, but it canwreakhavocevenwhenbeinggood.All in all, it is a smallmiraclethatanythingworksat all.

Backupsarea way to protecttheinvestmentin data.By having severalcopiesof thedata,it doesnotmatterasmuchif oneis destroyed(thecostis only thatof therestorationof thelost datafrom thebackup).

It is importantto do backupsproperly. Likeeverythingelsethatis relatedto thephysicalworld, backupswill fail sooneror later. Part of doingbackupswell is to makesurethey work; you don’t wantto noticethatyourbackupsdidn’t work. 2 Adding insult to injury, youmight havea badcrashjust asyou’remakingthebackup;if you haveonly onebackupmedium,it might destroyedaswell, leaving you withthesmokingashesof hardwork. 3 Or youmightnotice,whentrying to restore,thatyou forgot to backupsomethingimportant,like theuserdatabaseona 15000usersite.Bestof all, all yourbackupsmightbeworking perfectly, but thelastknown tapedrivereadingthekind of tapesyouusedwastheonethatnowhasabucketful of waterin it.

Whenit comesto backups,paranoiais in thejob description.

92

Page 93: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter10.Backups

Selecting the backup mediumThemostimportantdecisionregardingbackupsis thechoiceof backupmedium.You needto considercost,reliability, speed,availability, andusability.

Costis important,sinceyoushouldpreferablyhaveseveraltimesmorebackupstoragethanwhatyouneedfor thedata.A cheapmediumis usuallya must.

Reliability is extremelyimportant,sincea brokenbackupcanmakea grown mancry. A backupmediummustbeableto holddatawithout corruptionfor years.Thewayyouusethemediumaffectsit reliabilityasa backupmedium.A harddisk is typically very reliable,but asa backupmediumit is not veryreliable,if it is in thesamecomputerasthediskyouarebackingup.

Speedis usuallynot very important,if backupscanbedonewithout interaction.It doesn’t matterif abackuptakestwo hours,aslongasit needsnosupervision.On theotherhand,if thebackupcan’t bedonewhenthecomputerwouldotherwisebeidle, thenspeedis anissue.

Availability is obviouslynecessary, sinceyoucan’t useabackupmediumif it doesn’t exist. Lessobviousis theneedfor themediumto beavailableevenin thefuture,andon computersotherthanyourown.Otherwiseyou maynot beableto restoreyourbackupsaftera disaster.

Usability is a largefactorin how oftenbackupsaremade.Theeasierit is to makebackups,thebetter. Abackupmediummustn’t behardor boringto use.

Thetypical alternativesarefloppiesandtapes.Floppiesarevery cheap,fairly reliable,not very fast,veryavailable,but not veryusablefor largeamountsof data.Tapesarecheapto somewhatexpensive,fairlyreliable,fairly fast,quiteavailable,and,dependingon thesizeof thetape,quitecomfortable.

Thereareotheralternatives.They areusuallynot verygoodonavailability, but if thatis not a problem,they canbebetterin otherways.For example,magneto-opticaldiskscanhavegoodsidesof bothfloppies(they’rerandomaccess,makingrestorationof a singlefile quick) andtapes(containa lot of data).

Selecting the backup toolTherearemany toolsthatcanbeusedto makebackups.ThetraditionalUNIX toolsusedfor backupsaretar , cpio, anddump. In addition,therearelargenumberof third partypackages(bothfreewareandcommercial)thatcanbeused.Thechoiceof backupmediumcanaffect thechoiceof tool.

tar andcpio aresimilar, andmostlyequivalentfrom a backuppoint of view. Botharecapableof storingfileson tapes,andretrieving files from them.Botharecapableof usingalmostany media,sincethekerneldevicedriverstakecareof thelow level devicehandlingandthedevicesall tendto look alike touserlevel programs.SomeUNIX versionsof tar andcpio mayhaveproblemswith unusualfiles(symboliclinks, devicefiles,files with very longpathnames,andsoon),but theLinux versionsshouldhandleall filescorrectly.

93

Page 94: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter10.Backups

dump is differentin thatit readsthefilesystemdirectly andnot via thefilesystem.It is alsowrittenspecificallyfor backups;tar andcpio arereally for archiving files,althoughthey work for backupsaswell.

Readingthefilesystemdirectly hassomeadvantages.It makesit possibleto backfilesup withoutaffectingtheir timestamps;for tar andcpio, youwould haveto mountthefilesystemread-onlyfirst.Directly readingthefilesystemis alsomoreeffective,if everythingneedsto bebackedup,sinceit canbedonewith muchlessdiskheadmovement.Themajordisadvantageis thatit makesthebackupprogramspecificto onefilesystemtype;theLinux dump programunderstandstheext2 filesystemonly.

dump alsodirectly supportsbackuplevels(which we’ll bediscussingbelow); with tar andcpio this hasto beimplementedwith othertools.

A comparisonof thethird partybackuptoolsis beyondthescopeof this book.TheLinux SoftwareMaplists many of thefreewareones.

Simple backupsA simplebackupschemeis to backup everythingonce,thenbackup everythingthathasbeenmodifiedsincethepreviousbackup.Thefirst backupis calleda full backup, thesubsequentonesareincrementalbackups. A full backupis oftenmorelaboriusthanincrementalones,sincethereis moredatato write tothetapeanda full backupmight not fit ontoonetape(or floppy). Restoringfrom incrementalbackupscanbemany timesmorework thanfrom afull one.Restorationcanbeoptimizedsothatyou alwaysbackup everythingsincethepreviousfull backup;this way, backupsarea bit morework, but thereshouldneverbea needto restoremorethana full backupandanincrementalbackup.

If you wantto makebackupseverydayandhavesix tapes,you couldusetape1 for thefirst full backup(say, ona Friday),andtapes2 to 5 for theincrementalbackups(MondaythroughThursday).Thenyoumakea new full backupon tape6 (secondFriday),andstartdoingincrementaloneswith tapes2 to 5again.You don’t wantto overwritetape1 until you’vegota new full backup,lestsomethinghappenswhile you’remakingthefull backup.After you’vemadea full backupto tape6, you wantto keeptape1somewhereelse,sothatwhenyourotherbackuptapesaredestroyedin thefire, youstill haveat leastsomethingleft. Whenyouneedto make thenext full backup,you fetchtape1 andleave tape6 in itsplace.

If you havemorethansix tapes,youcanusetheextraonesfor full backups.Eachtime youmakea fullbackup,youusetheoldesttape.This wayyoucanhave full backupsfrom severalpreviousweeks,whichis goodif youwantto find anold, now deletedfile, or anold versionof afile.

Making backups with tar

94

Page 95: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter10.Backups

A full backupcaneasilybemadewith tar :

# tar -create -file /dev/ftape /usr/srctar: Removing leading / from absolute path names in the archive

#

TheexampleaboveusestheGNU versionof tar andits longoptionnames.Thetraditionalversionof taronly understandssinglecharacteroptions.TheGNU versioncanalsohandlebackupsthatdon’t fit ononetapeor floppy, andalsovery longpaths;not all traditionalversionscando thesethings.(Linux onlyusesGNU tar .)

If yourbackupdoesn’t fit on onetape,youneedto usethe-multi-volume (-M) option:

# tar -cMf /dev/fd0H1440 /usr/srctar: Removing leading / from absolute path names in the archive

Prepare volume #2 for /dev/fd0H1440 and hit return:

#

Notethatyoushouldformatthefloppiesbeforeyoubegin thebackup,or elseuseanotherwindow orvirtual terminalanddo it whentar asksfor a new floppy.

After you’vemadeabackup,youshouldcheckthatit is OK, usingthe-compare (-d) option:

# tar -compare -verbose -f /dev/ftapeusr/src/

usr/src/linux

usr/src/linux-1.2.10-includes/

....

#

Failing to checkabackupmeansthatyouwill not noticethatyourbackupsaren’t workinguntil afteryou’velost theoriginaldata.

An incrementalbackupcanbedonewith tar usingthe-newer (-N) option:

# tar -create -newer ’8 Sep 1995’ -file /dev/ftape /usr/src -verbosetar: Removing leading / from absolute path names in the archive

usr/src/

usr/src/linux-1.2.10-includes/

usr/src/linux-1.2.10-includes/include/

usr/src/linux-1.2.10-includes/include/linux/

usr/src/linux-1.2.10-includes/include/linux/modules/

usr/src/linux-1.2.10-includes/include/asm-generic/

usr/src/linux-1.2.10-includes/include/asm-i386/

usr/src/linux-1.2.10-includes/include/asm-mips/

usr/src/linux-1.2.10-includes/include/asm-alpha/

95

Page 96: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter10.Backups

usr/src/linux-1.2.10-includes/include/asm-m68k/

usr/src/linux-1.2.10-includes/include/asm-sparc/

usr/src/patch-1.2.11.gz

#

Unfortunately, tar can’t noticewhena file’s inodeinformationhaschanged,for example,thatit’spermissionbits havebeenchanged,or whenits namehasbeenchanged.Thiscanbeworkedaroundusingfind andcomparingcurrentfilesystemstatewith lists of files thathavebeenpreviouslybackedup.Scriptsandprogramsfor doingthis canbefoundon Linux ftp sites.

Restoring files with tarThe-extract (-x) optionfor tar extractsfiles:

# tar -extract -same-permissions -verbose -file /dev/fd0H1440usr/src/

usr/src/linux

usr/src/linux-1.2.10-includes/

usr/src/linux-1.2.10-includes/include/

usr/src/linux-1.2.10-includes/include/linux/

usr/src/linux-1.2.10-includes/include/linux/hdreg.h

usr/src/linux-1.2.10-includes/include/linux/kernel.h

...

#

You alsoextractonly specificfilesor directories(which includesall their filesandsubdirectories)bynamingon thecommandline:

# tar xpvf /dev/fd0H1440 usr/src/linux-1.2.10-includes/include/linux/hdreg.husr/src/linux-1.2.10-includes/include/linux/hdreg.h

#

Usethe-list (-t) option,if you just wantto seewhatfilesareon abackupvolume:

# tar -list -file /dev/fd0H1440usr/src/

usr/src/linux

usr/src/linux-1.2.10-includes/

usr/src/linux-1.2.10-includes/include/

usr/src/linux-1.2.10-includes/include/linux/

usr/src/linux-1.2.10-includes/include/linux/hdreg.h

usr/src/linux-1.2.10-includes/include/linux/kernel.h

...

96

Page 97: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter10.Backups

#

Notethat tar alwaysreadsthebackupvolumesequentially, sofor largevolumesit is ratherslow. It is notpossible,however, to userandomaccessdatabasetechniqueswhenusinga tapedriveor someothersequentialmedium.

tar doesn’t handledeletedfilesproperly. If youneedto restoreafilesystemfrom a full andanincrementalbackup,andyouhavedeleteda file betweenthetwo backups,it will exist againafteryouhavedonetherestore.Thiscanbea big problem,if thefile hassensitivedatathatshouldno longerbeavailable.

Multile vel backupsThesimplebackupmethodoutlinedin theprevioussectionis oftenquiteadequatefor personaluseorsmallsites.For moreheavy dutyuse,multilevel backupsaremoreappropriate.

Thesimplemethodhastwo backuplevels:full andincrementalbackups.Thiscanbegeneralizedto anynumberof levels.A full backupwouldbelevel 0, andthedifferentlevelsof incrementalbackupslevels1, 2, 3, etc.At eachincrementalbackuplevel you backup everythingthathaschangedsincethepreviousbackupat thesameor a previouslevel.

Thepurposefor doingthis is thatit allowsa longerbackuphistorycheaply. In theexamplein theprevioussection,thebackuphistorywentbackto thepreviousfull backup.Thiscouldbeextendedbyhaving moretapes,but only aweekpernew tape,whichmightbetoo expensive.A longerbackuphistoryis useful,sincedeletedor corruptedfiles areoftennot noticedfor a long time.Evena versionof a filethatis not veryup to dateis betterthanno file at all.

With multiple levelsthebackuphistorycanbeextendedmorecheaply. For example,if we buy tentapes,we couldusetapes1 and2 for monthlybackups(first Fridayeachmonth),tapes3 to 6 for weeklybackups(otherFridays;notethattherecanbefiveFridaysin onemonth,soweneedfour moretapes),andtapes7 to 10 for daily backups(Mondayto Thursday).With only four moretapes,we’vebeenableto extendthebackuphistoryfrom two weeks(afterall daily tapeshavebeenused)to two months.It istruethatwe can’t restoreeveryversionof eachfile duringthosetwo months,but whatwecanrestoreisoftengoodenough.

Figure10-1showswhich backuplevel is usedeachday, andwhichbackupscanberestoredfrom at theendof themonth.

97

Page 98: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter10.Backups

Figure10-1.A samplemultilevel backup schedule.

MonMonMonMonMon MonMonMonMon

1 1 1 12 2 2 2 2 22 2 2 22 2

3129221581

2 22 1 1 12 2 2 2 2 22 2 2 22 2 Backup level

31 Day of month2922158

2 22 2 22

1

201 3 4 5 6 2

03 4 5

Restorable

Tape number

Backuplevelscanalsobeusedto keepfilesystemrestorationtime to a minimum.If you havemanyincrementalbackupswith monotonouslygrowing level numbers,youneedto restoreall of themif youneedto rebuild thewholefilesystem.Insteadyoucanuselevel numbersthataren’t monotonous,andkeepdown thenumberof backupsto restore.

To minimizethenumberof tapesneededto restore,youcouldusea smallerlevel for eachincrementaltape.However, thenthetime to make thebackupsincreases(eachbackupcopieseverythingsincethepreviousfull backup).A betterschemeis suggestedby thedump manualpageanddescribedby thetableXX (efficient-backup-levels).Usethefollowing successionof backuplevels:3, 2, 5, 4, 7, 6, 9, 8, 9, etc.This keepsboththebackupandrestoretimeslow. Themostyou haveto backupis two day’sworthofwork. Thenumberof tapesfor a restoredependsonhow long youkeepbetweenfull backups,but it islessthanin thesimpleschemes.

Table 10-1.Efficient backup schemeusingmany backup levels

Tape Level Backup (days) Restore tapes

1 0 n/a 1

2 3 1 1, 2

3 2 2 1, 3

4 5 1 1, 2, 4

5 4 2 1, 2, 5

6 7 1 1, 2, 5, 6

7 6 2 1, 2, 5, 7

8 9 1 1, 2, 5, 7, 8

9 8 2 1, 2, 5, 7, 9

10 9 1 1, 2, 5, 7, 9, 10

11 9 1 1, 2, 5, 7, 9, 10,11

98

Page 99: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter10.Backups

Tape Level Backup (days) Restore tapes

... 9 1 1, 2, 5, 7, 9, 10,11, ...

A fancy schemecanreducetheamountof laborneeded,but it doesmeantherearemorethingsto keeptrackof. Youmustdecideif it is worth it.

dump hasbuilt-in supportfor backuplevels.For tar andcpio it mustbeimplementedwith shellscripts.

What to back upYouwantto backupasmuchaspossible.Themajorexceptionis softwarethatcanbeeasilyreinstalled,4

but eventhey mayhaveconfigurationfiles thatit is importantto backup, lestyou needto doall theworkto configurethemall overagain.Anothermajorexceptionis the/proc filesystem;sincethatonlycontainsdatathatthekernelalwaysgeneratesautomatically, it is neveragoodideato backit up.Expeciallythe/proc/kcore file is unnecessary, sinceit is just animageof yourcurrentphysicalmemory;it’sprettylargeaswell.

Grayareasincludethenewsspool,log files,andmany otherthingsin /var. You mustdecidewhatyouconsiderimportant.

Theobviousthingsto backup areuserfiles (/home) andsystemconfigurationfiles (/etc, but possiblyotherthingsscatteredall over thefilesystem).

Compressed backupsBackupstakea lot of space,which cancostquitea lot of money. To reducethespaceneeded,thebackupscanbecompressed.Thereareseveralwaysof doingthis.Someprogramshavesupportfor forcompressionbuilt in; for example,the-gzip (-z) optionfor GNU tar pipesthewholebackupthroughthegzip compressionprogram,beforewriting it to thebackupmedium.

Unfortunately, compressedbackupscancausetrouble.Dueto thenatureof how compressionworks,if asinglebit is wrong,all therestof thecompresseddatawill beunusable.Somebackupprogramshavesomebuilt in errorcorrection,but no methodcanhandlea largenumberof errors.This meansthatif thebackupis compressedthewayGNU tar doesit, with thewholeoutputcompressedasaunit, a singleerrormakesall therestof thebackuplost.Backupsmustbereliable,andthis methodof compressionisnot a goodidea.

An alternativeway is to compresseachfile separately. Thisstill meansthattheonefile is lost,but allotherfilesareunharmed.Thelost file wouldhavebeencorruptedanyway, sothis situationis not muchworsethannot usingcompressionat all. Theafio program(avariantof cpio) cando this.

99

Page 100: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter10.Backups

Compressiontakessometime,which maymake thebackupprogramunableto write datafastenoughfora tapedrive.5 This canbeavoidedby buffering theoutput(eitherinternally, if thebackupprogramifsmartenough,or by usinganotherprogram),but eventhatmightnot work well enough.This shouldonlybea problemon slow computers.

Notes1. Thefifth reasonis “somethingelse”.

2. Don’t laugh.Thishashappenedto severalpeople.

3. Beenthere,donethat...

4. You getto decidewhat’seasy. Somepeopleconsiderinstallingfrom dozensof floppieseasy.

5. If a tapedrivedoesn’t datafastenough,it hasto stop;this makesbackupsevenslower, andcanbebadfor thetapeandthedrive.

100

Page 101: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter 11. Keeping Time

“Time is anillusion. Lunchtimedoubleso.” (DouglasAdams.)

This chapterexplainshow aLinux systemkeepstime,andwhatyouneedto do to avoid causingtrouble.Usually, youdon’t needto do anythingabouttime,but it is goodto understandit.

Time zonesTimemeasurementis basedonmostlyregularnaturalphenomena,suchasalternatinglight anddarkperiodscausedby therotationof theplanet.Thetotal time takenby two successiveperiodsis constant,but thelengthsof thelight anddarkperiodvary. Onesimpleconstantis noon.

Noonis thetimeof thedaywhentheSunis at its highestposition.SincetheEarthis round,1 noonhappensat differenttimesin differentplaces.This leadsto theconceptof local time. Humansmeasuretime in many units,mostof which aretied to naturalphenomenalike noon.As long asyoustayin thesameplace,it doesn’t matterthatlocal timesdiffer.

As soonasyouneedto communicatewith distantplaces,you’ll noticetheneedfor acommontime. Inmoderntimes,mostof theplacesin theworld communicatewith mostotherplacesin theworld, soaglobalstandardfor measuringtimehasbeendefined.This time is calleduniversal time(UT or UTC,formerly known asGreenwichMeanTimeor GMT, sinceit usedto belocal time in Greenwich,England).Whenpeoplewith differentlocal timesneedto communicate,they canexpresstimesinuniversaltime,sothatthereis no confusionaboutwhenthingsshouldhappen.

Eachlocal time is calleda timezone.While geographywouldallow all placesthathavenoonat thesametime havethesametimezone,politics makesit difficult. For variousreasons,many countriesusedaylightsavingstime, thatis, they movetheir clocksto havemorenaturallight while they work, andthenmovetheclocksbackduringwinter. Othercountriesdo not do this.Thosethatdo,do not agreewhentheclocksshouldbemoved,andthey changetherulesfrom yearto year. Thismakestime zoneconversionsdefinitelynon-trivial.

Timezonesarebestnamedby thelocationor by telling thedifferencebetweenlocalanduniversaltime.In theUS andsomeothercountries,thelocal timezoneshaveanameanda threeletterabbreviation.Theabbreviationsarenot unique,however, andshouldnot beusedunlessthecountryis alsonamed.It isbetterto talk aboutthelocal time in, say, Helsinki, thanaboutEastEuropeantime,sincenot all countriesin EasternEuropefollow thesamerules.

Linux hasa time zonepackagethatknowsaboutall existing time zones,andthatcaneasilybeupdatedwhentheruleschange.All thesystemadministratorneedsto do is to selecttheappropriatetimezone.Also, eachusercansethisown timezone;this is importantsincemany peoplework with computersin

101

Page 102: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter11.KeepingTime

differentcountriesover theInternet.Whentherulesfor daylightsavingstimechangein your local timezone,makesureyou’ll upgradeat leastthatpartof yourLinux system.Otherthansettingthesystemtime zoneandupgradingthetime zonedatafiles, thereis little needto botherabouttime.

The hardware and software cloc ksA personalcomputerhasabatterydrivenhardwareclock.Thebatteryensuresthattheclockwill workevenif therestof thecomputeris withoutelectricity. Thehardwareclock canbesetfrom theBIOS setupscreenor from whateveroperatingsystemis running.

TheLinux kernelkeepstrackof time independentlyfrom thehardwareclock.During theboot,Linuxsetsits own clock to thesametimeasthehardwareclock.After this,bothclocksrun independently.Linux maintainsits own clockbecauselookingat thehardwareis slow andcomplicated.

Thekernelclockalwaysshowsuniversaltime.Thisway, thekerneldoesnot needto know abouttimezonesat all. Thesimplicity resultsin higherreliability andmakesit easierto updatethetimezoneinformation.Eachprocesshandlestimezoneconversionsitself (usingstandardtoolsthatarepartof thetime zonepackage).

Thehardwareclockcanbein local timeor in universaltime. It is usuallybetterto have it in universaltime,becausethenyoudon’t needto changethehardwareclock whendaylightsavingstime beginsorends(UTC doesnothaveDST).Unfortunately, somePCoperatingsystems,includingMS-DOS,Windows,andOS/2,assumethehardwareclockshows local time.Linux canhandleeither, but if thehardwareclock shows local time, thenit mustbemodifiedwhendaylightsavingstimebeginsor ends(otherwiseit wouldn’t show local time).

Showing and setting timeIn theDebiansystem,thesystemtimezoneis determinedby thesymboliclink /etc/localtime. Thislink pointsat a time zonedatafile thatdescribesthelocal time zone.Thetimezonedatafilesarestoredin /usr/lib/zoneinfo. OtherLinux distributionsmaydo this differently.

A usercanchangehis privatetimezoneby settingtheTZ environmentvariable.If it is unset,thesystemtime zoneis assumed.Thesyntaxof theTZ variableis describedin thetzset manualpage.

Thedatecommandshowsthecurrentdateandtime. 2 For example:

$ dateSun Jul 14 21:53:41 EET DST 1996

$

102

Page 103: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter11.KeepingTime

Thattime is Sunday, 14thof July, 1996,at abouttenbeforetenat theevening,in thetimezonecalled“EET DST” (whichmightbeEastEuropeanDaylightSavingsTime).datecanalsoshow theuniveraltime:

$ date -uSun Jul 14 18:53:42 UTC 1996Sun Jul 14 18:53:42 UTC 1996

$

date is alsousedto setthekernel’ssoftwareclock:

# date 07142157Sun Jul 14 21:57:00 EET DST 1996

# dateSun Jul 14 21:57:02 EET DST 1996

#

Seethedatemanualpagefor moredetails;thesyntaxis a bit arcane.Only root cansetthetime.Whileeachusercanhavehisown timezone,theclock is thesamefor everyone.

dateonly showsor setsthesoftwareclock.Theclock commandssyncronizesthehardwareandsoftwareclocks.It is usedwhenthesystemboots,to readthehardwareclock andsetthesoftwareclock. If youneedto setbothclocks,youfirst setthesoftwareclock with date, andthenthehardwareclock withclock -w.

The-u optionto clock tells it thatthehardwareclock is in universaltime.You mustusethe-u optioncorrectly. If you don’t, yourcomputerwill bequiteconfusedaboutwhatthetime is.

Theclocksshouldbechangedwith care.Many partsof a Unix systemrequiretheclocksto workcorrectly. For example,thecron daemonrunscommandsperiodically. If you changetheclock, it canbeconfusedof whetherit needsto run thecommandsor not.OnoneearlyUnix system,someonesettheclock twentyyearsinto thefuture,andcron wantedto runall theperiodiccommandsfor twentyyearsallat once.Currentversionsof cron canhandlethis correctly, but youshouldstill becareful.Big jumpsorbackwardjumpsaremoredangeoursthansmalleror forwardones.

When the cloc k is wrongTheLinux softwareclock is not alwaysaccurate.It is keptrunningby aperiodictimer interruptgeneratedby PChardware.If thesystemhastoo many processesrunning,it maytake too long to servicethetimer interrupt,andthesoftwareclockstartsslippingbehind.Thehardwareclock runsindependentlyandis usuallymoreaccurate.If youbootyourcomputeroften(asis thecasefor mostsystemsthataren’tservers),it will usuallykeepfairly accuratetime.

103

Page 104: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Chapter11.KeepingTime

If you needto adjustthehardwareclock, it is usuallysimplestto reboot,go into theBIOS setupscreen,anddo it from there.This avoidsall troublethatchangingsystemtime mightcause.If doingit via BIOSis not anoption,setthenew time with dateandclock (in thatorder),but bepreparedto reboot,if somepartof thesystemstartsactingfunny.

A networkedcomputer(evenif just over themodem)cancheckits own clockautomatically, bycomparingit to someothercomputer’stime. If theothercomputeris known to keepveryaccuratetime,thenbothcomputerswill keepaccuratetime.Thiscanbedoneby usingtherdate andnetdatecommands.Bothcheckthetime of a remotecomputer(netdatecanhandleseveralremotecomputers),andsetthelocal computer’s time to that.By runningonethesecommandsregularly, yourcomputerwillkeepasaccuratetime astheremotecomputer.

XXX saysomethingintelligentaboutNTP

Notes1. Accordingto recentresearch.

2. Bewareof the time command,which doesnot show thecurrenttime.

104

Page 105: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Glossar y (DRAFT)

“The Librarianof theUnseenUniversityhadunilaterallydecidedto aidcomprehensionby producinganOrang-utan/HumanDictionary. He’d beenworking on it for threemonths.It wasn’t easy. He’d gotasfar as‘Oook.’” (TerryPratchett,“Men At Arms”)

This is ashortlist of word definitionsfor conceptsrelatingto Linux andsystemadministration.

ambition

Theactof writing funny sentencesin thehopeof gettingtheminto theLinux cookiefile.

application program

Softwarethatdoessomethinguseful.Theresultsof usinganapplicationprogramis whatthecomputerwasboughtfor. Seealsosystemprogram,operatingsystem.

daemon

A processlurking in thebackground,usuallyunnoticed,until somethingtriggersit into action.Forexample,theupdatedaemonwakesup every thirty secondsor soto flushthebuffer cache,andthesendmaildaemonawakeswheneversomeonesendsmail.

file system

Themethodsanddatastructuresthatanoperatingsystemusesto keeptrackof fileson a diskorpartition;theway thefilesareorganizedon thedisk.Also usedabouta partitionor disk thatis usedto storethefilesor thetypeof thefilesystem.

glossar y

A list of wordsandexplanationsof whatthey do.Not to beconfusedwith adictionary, which is alsoa list of wordsandexplanations.

kernel

105

Page 106: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of

Glossary(DRAFT)

Part of anoperatingsystemthatimplementstheinteractionwith hardwareandthesharingofresources.Seealsosystemprogram.

operating system

Softwarethatsharesa computersystem’s resources(processor, memory, diskspace,networkbandwidth,andsoon)betweenusersandtheapplicationprogramsthey run.Controlsaccessto thesystemto providesecurity. Seealsokernel,systemprogram,applicationprogram.

system call

Theservicesprovidedby thekernelto applicationprograms,andtheway in which they areinvoked.Seesection2 of themanualpages.

system program

Programsthatimplementhigh level functionalityof anoperatingsystem,i.e., thingsthataren’tdirectly dependenton thehardware.May sometimesrequirespecialprivilegesto run (e.g.,fordeliveringelectronicmail), but oftenjust commonlythoughtof aspartof thesystem(e.g.,acompiler).Seealsoapplicationprogram,kernel,operatingsystem.

106

Page 107: The Linux System Administrators’ Guide - WordPress.com · 2009. 3. 18. · This manual, the Linux System Administrators’ Guide, describes the system administration aspects of