•
•
•
•
•
•
TableofContents
Index
Reviews
ReaderReviews
Errata
Academic
BSDHacks
ByDruLavigne
Publisher :O'Reilly
PubDate :May2004
ISBN :0-596-00679-9
Pages :300
Lookingforauniquesetofpracticaltips,
tricks,andtoolsforadministratorsandpower
usersofBSDsystems?Fromhacksto
customizetheuserenvironmentto
networking,securingthesystem,and
optimization,BSDHackstakesacreative
approachtosavingtimeandaccomplishing
morewithfewerresources.Ifyouwantmore
thantheaverageBSDuser--toexploreand
experiment,unearthshortcuts,createuseful
tools--thisbookisamust-have.
•
•
•
•
•
•
TableofContents
Index
Reviews
ReaderReviews
Errata
Academic
BSDHacks
ByDruLavigne
Publisher :O'Reilly
PubDate :May2004
ISBN :0-596-00679-9
Pages :300
Credits
AbouttheAuthor
Contributors
Acknowledgments
Preface
WhyBSDHacks?
HowtoUsethisBook
HowThisBookIsOrganized
UsingCodeExamples
ConventionsUsedinThisBook
We'dLiketoHearfromYou
Chapter1.CustomizingtheUserEnvironment
Section0.Introduction
Section1.GettheMostOutoftheDefaultShell
Section2.UsefultcshShellConfigurationFileOptions
Section3.CreateShellBindings
Section5.UsetheMouseataTerminal
Section7.LocktheScreen
Section9.CustomizeUserConfigurations
Section11.UseanInteractiveShell
Section4.UseTerminalandXBindings
Section6.GetYourDailyDoseofTrivia
Section8.CreateaTrashDirectory
Section10.MaintainYourEnvironmentonMultipleSystems
Section12.UseMultipleScreensonOneTerminal
Chapter2.DealingwithFilesandFilesystems
Section12.Introduction
Section13.FindThings
Section14.GettheMostOutofgrep
Section16.FormatTextattheCommandLine
Section18.DOSFloppyManipulation
Section20.DealwithDiskHogs
Section22.RecreateaDirectoryStructureUsingmtree
Section15.ManipulateFileswithsed
Section17.DelimiterDilemma
Section19.AccessWindowsSharesWithoutaServer
Section21.ManageTemporaryFilesandSwapSpace
Section23.GhostingSystems
Chapter3.TheBootandLoginEnvironments
Introduction
Section24.CustomizetheDefaultBootMenu
Section25.ProtecttheBootProcess
Section27.LogaHeadlessServerRemotely
Section29.ProtectingPasswordsWithBlowfishHashes
Section31.CreateanEffective,ReusablePasswordPolicy
Section33.UseOneTimePasswords
Section26.RunaHeadlessSystem
Section28.RemovetheTerminalLoginBanner
Section30.MonitorPasswordPolicyCompliance
Section32.AutomateMemorablePasswordGeneration
Section34.RestrictLogins
Chapter4.BackingUp
Introduction
Section35.BackUpFreeBSDwithSMBFS
Section37.InteractiveCopy
Section39.AutomateRemoteBackups
Section41.PerformClient-ServerCross-PlatformBackupswithBacula
Section36.CreatePortablePOSIXArchives
Section38.SecureBackupsOveraNetwork
Section40.AutomateDataDumpsforPostgreSQLDatabases
Chapter5.NetworkingHacks
Introduction
Section42.SeeConsoleMessagesOveraRemoteLogin
Section43.SpoofaMACAddress
Section45.SurviveCatastrophicInternetLoss
Section47.UnderstandDNSRecordsandTools
Section49.WhyDoINeedsendmail?
Section51.GettheMostOutofFTP
Section53.InteractiveRemoteAdministration
Section44.UseMultipleWirelessNICConfigurations
Section46.HumanizetcpdumpOutput
Section48.SendandReceiveEmailWithoutaMailClient
Section50.HoldEmailforLaterDelivery
Section52.DistributedCommandExecution
Chapter6.SecuringtheSystem
Introduction
Section54.StriptheKernel
Section55.FreeBSDAccessControlLists
Section57.TightenSecuritywithMandatoryAccessControl
Section59.IntrusionDetectionwithSnort,ACID,MySQL,andFreeBSD
Section61.SudoGotchas
Section63.RestrictanSSHserver
Section65.SecureaWirelessNetworkUsingPF
Section67.AutomateSecurityPatches
Section56.ProtectFileswithFlags
Section58.UsemtreeasaBuilt-inTripwire
Section60.EncryptYourHardDisk
Section62.sudoscript
Section64.ScriptIPFilterRulesets
Section66.AutomaticallyGenerateFirewallRules
Section68.ScanaNetworkofWindowsComputersforViruses
Chapter7.GoingBeyondtheBasics
Introduction
Section70.TrafficShapingonFreeBSD
Section72.UsetheFreeBSDRecoveryProcess
Section74.ConsolidateWebServerLogs
Section76.CreateaTradeShowDemo
Section69.TuneFreeBSDforDifferentApplications
Section71.CreateanEmergencyRepairKit
Section73.UsetheGNUDebuggertoAnalyzeaBufferOverflow
Section75.ScriptUserInteraction
Chapter8.KeepingUp-to-Date
Introduction
Section77.AutomatedInstall
Section78.FreeBSDfromScratch
Section80.AutomateUpdates
Section82.BuildaPortWithoutthePortsTree
Section84.NavigatethePortsSystem
Section86.CreateYourOwnStartupScripts
Section88.EasilyInstallUnixApplicationsonMacOSX
Section79.SafelyMergeChangesto/etc
Section81.CreateaPackageRepository
Section83.KeepPortsUp-to-DatewithCTM
Section85.DowngradeaPort
Section87.AutomateNetBSDPackageBuilds
Chapter9.GrokkingBSD
Introduction
Section89.How'dHeKnowThat?
Section90.CreateYourOwnManpages
Section92.Apply,Understand,andCreatePatches
Section94.DetermineWhoIsontheSystem
Section96.LeaveonTime
Section98.RotateYourSignature
Section9.13.FunwithX
Section91.GettheMostOutofManpages
Section93.DisplayHardwareInformation
Section95.SpellingBee
Section97.RunNativeJavaApplications
Section99.UsefulOne-Liners
Index
Credits
AbouttheAuthor
Contributors
Acknowledgments
AbouttheAuthor
DruLavigneistheauthorofONLamp.com'sFreeBSDBasics
columnandhasbeenanavidBSDusersinceFreeBSD2.2.1.As
anITinstructor,shespecializesinnetworking,routing,and
security.SheisalsoresponsibleforISECOM'sProtocol
Database,whichcanbefoundat.
Contributors
Thefollowingpeoplecontributedtheirhacks,writing,and
inspirationtothisbook:
JohnRichard,knownlocallyasJR,isasystemadministrator
inKingston,Ontario,Canada.Histrademarkinthefieldis
hisinsistenceonaFreeBSDboxastheprimaryfirewallona
network.Hehasenjoyedworkingwiththeauthorinthe
pastataprivatecollegeinKingston.Inhissparetime,he
experimentswithFreeBSDandrideshisHarley-Davidson.
[Hack#64]
JoeWarnerisaTechnicalAnalystforSiemensMedical
SolutionsHealthServicesCorporationandhasbeenusing
FreeBSDasaserveranddesktopsinceOctoberof2000.
JoehaslivedinSaltLakeCity,Utahformostofhislifeand
enjoys*BSD,computing,history,andTheMatrix.
[Hacks#35and#59]
DanLangille(runsaconsulting
groupinOttawa,Canada.Hehasfondmemoriesofhis
yearsinNewZealand,wheretheclimateismuchmore
conducivetoyear-roundmountainbiking.Helivesina
houseruledbyfelines.
[Hack#41]
RobertBernier'sprofessionalcareerhasincluded
engineering,accidentinvestigation,andOlympictrials.In
the1980s,hisinterestreturnedtoITwhenherealizedhe
wouldn'thavetouseapunchcardanymore.Eventuallyhe
discoveredLinuxandbythemid-1990shaddevelopeda
passionforallthingsopensource.Today,Robertteachesat
thelocalcommunitycollegeandwritesforanumberofIT
publicationsbasedinNorthAmericaandEurope.
[Hack#12]
KirkRussell()isakerneltesteratQNX
SoftwareSystems( />[Hack#36]
KarlVogelisasystemadministratorfortheC-17Program
Office.He'sworkedatWright-PattersonAirForceBasefor
22yearsandhasaBSinMechanical&Aerospace
EngineeringfromCornellUniversity.
[Hack#32]
HowardOwendiscoveredcomputersbyreadingabout
Conway's"Life"inLifemagazine.Ittookmanyyearsfrom
thatdiscoverytothetimehecouldactuallymakealiving
withthegodforsakenthings.Oncethathappened,however,
Howardturnedintoa"majorgeek."Hehasworkedasa
sysadmin,systemsengineer,andsystemsarchitect.Heis
currentlyemployedbyIBMinSiliconValleysupporting
Linux,buthestillrunsFreeBSDandOpenBSDathome.
[Hacks#61and#62]
DanielHarrisisastudentandoccasionalconsultantinWest
Virginia.Heisinterestedincomputernetworking,
documentation,andsecurity;healsoenjoyswriting,
armchairpolitics,andamateurradio.
[Hack#55]
AndrewGould,CPA,performsfinancialandclinicaldata
analysisforahospitalinTexas.Hisprimarytoolfordata
integrationisaPostgreSQLdatabaseserverrunningon
FreeBSD.AndrewhasbeenusingFreeBSDatbothworkand
homeforfouryears.AndrewhasaBSinEducationanda
BBAinAccountingfromtheUniversityofTexasatAustin.
[Hacks#172.6,#40,#44,and#68]
JimMockisaFreeBSDadminanddeveloperturnedMacOS
Xuseranddeveloper.He'saFreeBSDcommitter,aswellas
anOpenDarwincommitter,andhecurrentlymaintains50+
DarwinPorts.JimisalsoamemberoftheDarwinPortsPort
Managerteam.Hecanbereachedator
throughhispersonalsiteat />[Hack#88]
AvleenVigisasystemsadministratoratEarthLink
(wherehemaintainsthe
company'sweb,mail,news,andotherInternetservicesfor
over8millionusers.Hespendshissparetimewithhis
newbornson,contributingtothevariousInternetandUnix
communities,andenjoyinglife.Afterseizingthedayin
2001andmovingtoLAfromLondon,he'swaitingtosee
wherelifewilltakehimnext.
[Hack#69]
AlexandruPopaisaCCNAstudyingforaCCNP,andis
activelyinvolvedintheFreeBSDcommunityinhisspare
time.Atthetimeofthiswriting,hewasstudyingComputer
ScienceatthePolitechnicaUniversityofBucharest.Healso
maintainscvsup.ro.freebsd.orgoutofabasementina
desertedbuilding,usingalargehamsterarrayforpower.He
canbecontactedat
[Hack#70]
JensSchweikhardtisaGermansoftwareengineerand
Internetwizardwhoisconstantlylookingforinteresting
thingstodo.Asaseven-timeIOCCCwinner,heiswellknownfortakingCcompilerstotheirlimits.Hecontributes
toUnixstandardizationand,ofcourse,toGod'sOwn
OperatingSystem.Whennothacking,Jenshasbeencaught
writingromanticpoetryandridinghisItalianMotoGuzzi
aroundtheSwabianhillsandvalleys.Ifheweregivenone
modestwish,itwouldbeclearskieswhenhegoes
stargazingwithhistelescope.
[Hack#78]
MatthewSeamanis38yearsoldandaformerscientistand
academic(OxfordUniversitypostgraduate).Heisnowa
specialistincomputersystemadministration,network
architecture,andinfrastructuredesign.
[Hacks#49,#50,and#97]
NathanRosenquistfirsttriedFreeBSDin1996,andhas
beenusingUnixeversince.Duringtheday,hecanbefound
developingPerl-basedwebapplicationsandbusiness
automationsoftware.HelivesinShadowHills,California
withhisgirlfriendCarrieandtheirdogNutmeg.
[Hack#39]
AdrianMayo(hasworkedwith
computersfor20years,specializinginthedesignofsafety
andmission-criticalsoftwarefortheaerospaceandmedical
industries.HehasgainedexposuretoBSDUnixthrough
Apple'sMacOSXoperatingsystem.HeisEditorforthe
newsandsupportsite,writingmost
ofthetechnicalcontent,includingtheUnixtutorialsand
DailyUnixtips.
[Hacks#14,#15,and#16]
SebastianStark()worksasasystem
administratorattheMaxPlanckInstituteforBiological
CyberneticsinGermany.Hemanagesabunchof
workstations,aswellasacomputerclusterthatisusedfor
machine-learningresearch.
[Hack#52]
MarlonBerlin()studieslinguistics,
comparativeliterature,andmathematicsinBerlin.Heworks
forDNS:NET,aGermanISP,asasystemsdeveloper.
[Hack#52]
DavidMaxwell()isaNetBSDDeveloper
andmemberoftheNetBSDSecurity-Officerteam.He
attendedUnixUnanimousinTorontosincethefirstmeeting
intheearly`80s,andstillvisitswhenhecan.Hewasan
avidAmigauser,andrelishesagood(orbad)punwhenhe
canmusterone.DavidcurrentlyworksatIntegratedDevice
Technology,Inc.(IDT).
[Hacks#10,#53,#73,#75,and#76]
JulioMerinoVidalisstudyingInformaticsEngineeringatthe
UPCUniversityofBarcelona,Spain.HehasbeenaNetBSD
developersinceNovember2002,workingontheNetBSD
PackagesCollection(and
translatingthewebsitetoSpanish.Healsomaintainshis
ownfreesoftwareprojects,includingBuildtool
(Youcancontacthimat
[Hacks#27and#87]
JanL.Peterson()isaprofessional
systemadministratorwith16yearsofexperienceworking
withmultipleUnixversions(andtheoccasionalWindows
machine).Laidofffromhislastjobwhenthecompanywas
acquiredbyadirectcompetitor,hehasspentthelastcouple
ofyearsasaconsultant.MoreaboutJancanbefoundat
/>[Hack#74]
MichaelVincewasbornin1977.Hisinitialinterestin
computerswasvideogames,buthesoonventuredinto
manyotherareas,suchasprogramming,Unix,theWeb,
andnetworks.HavingcompletedaDiplomainComputer
SystemsandaCCNA,heisanITadministratorforsoftware
companiesandhasbeeninvolvedinlargesoftwareprojects
thatputhisdevelopmentskillstogooduse.Atechnews
junkie,heisalwaysinterestedinthefutureofcomputing.
Healsoenjoysstayinguplatesolvingdifficultproblemsthat
requirecomplexregularexpressionsinPerl,goingtothe
gym,andhangingoutincafes.Heiscurrentlyworkingona
softwareproductcalledEzmin.
[Hack#64]
DanielCarosonehasbeeninvolvedwithNetBSDasauser,
advocate,anddeveloperforover10years.Heisamember
oftheNetBSDSecurityOfficerteam,whichprovides
leadershipforsecuritymatterswithintheprojectand
coordinatesresponsestopublicincidentsand
vulnerabilities.HeisChiefTechnologistfore-Secure,
specializinginsecurityconsultingandmanagementservices
tofinancial,government,andtelecommunications
organizations.Hepromotessecurityawarenessthrough
conferencepresentationsanduniversitylectures.Helivesin
Melbourne,Australia,andwhennotworkingtoohardenjoys
hiking,driving,andastronomy.
[Hack#60]
AaronCrandall,BSEE,hasusedOpenBSDsince2.7.He
currentlyworksfortheOregonGraduateInstituterunning
computersasapart-timeMaster'sstudent.He'sbuiltand
givenawaymoreOpenBSDfirewallsthanhecancount.
Contacthimat
[Hack#45]
chromaticistheTechnicalEditoroftheO'ReillyNetwork.In
practice,thatmeansheeditsONLamp.com(opensource
administrationanddevelopment)and,occasionally,books
likethisone.Outsideofwork,heenjoyscookingand
somehowproducesawholeslewofweirdsoftwarehacks
likeSDLParrot,tinymailtools,andthatPerl6thing.Wade
throughthedisarrayofhiswebsiteat
/>[Hack#92]
BrettWarden,BSEE,specializesinPerlprogrammingand
embeddedsystems.HelivesintheNorthwestwithhiswife,
son,andtwoantisocialcats.He'scurrentlykeepinganeye
outforcontractingandpermanentpositions.Youcanfinda
collectionofoddprojectsat />[Hack#65]
Acknowledgments
IwouldliketothankthemanyBSDandopensourceuserswho
sowillinglysharedtheirexperiences,ideas,andsupport.You
serveasaconstantreminderthatBSDismorethanan
operatingsystemitisacommunity.
Iwouldalsoliketothankallofmystudentsandthereadersof
theFreeBSDBasicscolumn.Yourquestionsandfeedbackfuel
mycuriosity;maythisbookreturnthatfavor.
ThankstoDavidLentsandRobFlickengerforreviewsand
advice.SpecialthankstoJacekArtymiakforhisinvaluableinput
fromtheOpenBSDandNetBSDperspectives.Andfinally,
specialthankstochromatic.Awritercouldn'thaveaskedfora
bettereditor.
Preface
"WhatwasitaboutUNIXthatwonmyheart?...UNIXis
mysteriouswhenyoufirstapproach.Alittleintimidating,
too.Butdespiteanunadornedandoftenplain
presentation,thediscerningsuitorcantellthere'slot
goingonunderthesurface."
ThomasScoville,
/>Whentheabove-mentionedarticlewasfirstpublished,Iwas
stillverymuchaBSDnewbie.Mysparehourswerespent
strugglingwithkernelrecompiles,PPPconnectivity(orlack
thereof),rmandchmoddisasters,andreadingandrereading
everybitofthethenavailabledocumentation.Yet,thatarticle
gavevoicetomyexperience,for,likethequotedauthor,Ihad
stumbleduponoperatingsystemlove.Inotherwords,Iwas
discoveringhowtohackonBSD.
Sincethen,I'velearnedthatthereisanunspokencommonality
betweenthenoviceUnixuserandtheseasonedguru.Itdoesn't
matterwhetheryou'vejustsurvivedyourfirstsuccessful
installationoryou'vejustexecutedacomplexscriptthatwill
saveyourcompanytimeandmoney,thefeelingisthesame.
It'stheexcitementofventuringintounknownterritoryand
discoveringsomethingnewandwonderful.It'sthatsenseof
accomplishmentthatcomeswithfiguringsomethingoutfor
yourself,withfindingyourownsolutiontotheproblemathand.
Thisbookcontains100hackswrittenbyuserswholovehacking
withBSD.You'llfindhackssuitedtoboththenoviceuserand
theseasonedveteran,aswellaseveryoneinbetween.Read
theminanyorderthatsuitsyourpurpose,butkeepthe"onion
principle"inmind.Whileeachhackdoespresentatleastone
practicalsolutiontoaproblem,that'sjusttheouterlayer.Use
yourimaginationtopeelawaydeeperlayers,exposingnew
solutionsasyoudoso.
WhyBSDHacks?
Thetermhackinghasanunfortunatereputationinthepopular
press,whereitoftenreferstosomeonewhobreaksinto
systemsorwreakshavocwithcomputers.Amongenthusiasts,
ontheotherhand,thetermhackreferstoa"quick-n-dirty"
solutiontoaproblemoracleverwaytodosomething.The
termhackerisverymuchacompliment,praisingsomeonefor
beingcreativeandhavingthetechnicalchopstogetthings
done.O'Reilly'sHacksseriesisanattempttoreclaimtheword,
documentthewayspeoplearehacking(inagoodway),and
passthehackerethicofcreativeparticipationontoanew
generationofhackers.Seeinghowothersapproachsystems
andproblemsisoftenthequickestwaytolearnaboutanew
technology.
BSDHacksisallaboutmakingthemostofyourBSDsystem.
TheBSDsoftodayhaveaproudlineage,tracingbacktosome
oftheoriginalhackerspeoplewhobuiltUnixandtheInternetas
weknowittoday.Asyou'dexpect,theyfacedmanyproblems
andsolvedproblemsbothquicklyandelegantly.We'vecollected
someofthatwisdom,bothclassicandmodern,aboutusingthe
commandline,securingsystems,keepingtrackofyourfiles,
makingbackups,and,mostimportantly,howtobecomeyour
ownBSDgurualongtheway.
HowtoUsethisBook
OneofthebeautiesofUnixisthatyoucanbeveryproductive
withsurprisinglylittleknowledge.Evenbetter,eachnewtrick
youlearncanshaveminutesoffofyourday.We'vearranged
thechaptersinthisbookbysubjectarea,notbyanysuggested
orderoflearning.Skiparoundtowhatinterestsyoumostor
solvesyourcurrentproblem.Ifthecurrenthackdependson
informationinanotherhack,we'llincludealinkforyouto
follow.
Furthermore,the"SeeAlso"sectionsattheendofindividual
hacksoftenincludereferencessuchasmanfortune.These
refertothemanualpagesinstalledonyourmachine.Ifyou're
notfamiliarwiththesemanpages,startwith[Hack#89].
HowThisBookIsOrganized
TomasterBSD,you'llhavetounderstandseveraltopics.We've
arrangedthehackslooselyintochapters.Theyare:
Chapter1CustomizingtheUserEnvironment
ThoughmodernBSDshavemyriadgraphicalapplications
andutilities,thecombinedwisdomof35yearsof
command-lineprogramsisjustashellaway.Thischapter
demonstrateshowtomakethemostofthecommandline,
customizingittoyourneedsandpreferences.
Chapter2DealingwithFilesandFilesystems
WhatgoodisknowingUnixcommandsifyouhavenofiles?
Youhavetoslice,dice,andstoredatasomewhere.This
chapterexplainstechniquesforfindingandprocessing
information,whetherit'sonyourmachineoronaserver
elsewhere.
Chapter3TheBootandLoginEnvironments
Thebest-laidsecurityplansofadministratorsoftengoout
thewindowwhenusersenterthepicture.Keepingthebad
guysoffofsensitivemachinesrequiresatwo-pronged
approach:protectingnormaluseraccountsthroughgood
passwordpoliciesandprotectingtheboxesphysically.This
chapterexploresseveraloptionsforcustomizingand
securingthebootandloginprocesses.
Chapter4BackingUp
Afteryoustartcreatingfiles,you'reboundtorunacross
datayoucan'taffordtolose.That'swherebackupscomein.
Thischapteroffersseveralideasforvariousmethodsof
ensuringthatyourpreciousdatawillpersistinthefaceof
tragedy.
Chapter5NetworkingHacks
Unlessyou'readie-hardindividualist,you'relikely
connectedtoanetwork.Thatfactpresentsseveralnew
opportunitiesforcleverhacksaswellasmystifyingfailures.
Thischapterilluminateswaystotakeadvantageofyour
networkconnection.
Chapter6SecuringtheSystem
Securityisasmuchamindsetasitisaprocess.Knowing
thetoolsatyourdisposalwillhelp.Thischapterdelvesinto
multipletoolsandideasforincreasingthesecurityofyour
systems,whetherkeepingoutthebadguysorstayingon
topofupdates.
Chapter7GoingBeyondtheBasics
Withyearsandyearsofrefinement,theBSDsprovide
powerfulandmaintainableenvironments.Areyoutaking
fulladvantageofeverythingyoursystemhastooffer?This
chapterpushestheenvelopeofwhatyoucanaccomplish.
Chapter8KeepingUp-to-Date
NobraggingaboutBSDiscompletewithoutmentioningthe
portsorpackagessystemthatkeepsthousandsof
applicationsrightatyourfingertips.Keepingup-to-date
couldneverbeeasier,couldit?Thischaptertacklesthe
subjectofinstallingandupdatingsoftware,includingthe
coresystem.
Chapter9GrokkingBSD
YoucannotbeatrueBSDmasteruntilyougroktheUnix
mindset.Howdidthegurusbecomegurus?Isthetruepath
stillopen?Thischapterrevealssomesecretsofthemasters
andhasalittlefunalongtheway.
ConventionsUsedinThisBook
Thisbookusesthefollowingtypographicalconventions:
Italic
Indicatesnewterms,URLs,emailaddresses,filenames,
pathnames,anddirectories.
Constantwidth
Indicatescommands,options,switches,variables,
attributes,functions,userandgroupnames,thecontentsof
files,andtheoutputfromcommands.
Constantwidthbold
Incodeexamples,showscommandsorothertextthat
shouldbetypedliterallybytheuser.
Constantwidthitalic
Showstextthatshouldbereplacedwithuser-supplied
values.
Color
Thesecondcolorisusedtoindicateacross-referencewithin
thetext.
Thisiconsignifiesatip,suggestion,orgeneralnote.
Thisiconindicatesawarningorcaution.
Thethermometericons,foundnexttoeachhack,indicatethe
relativecomplexityofthehack:
beginner
moderate
expert