431

Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 2: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack

Page 3: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

TableofContentsIntroductionForewordVersionChapter1:PowerfulCDCommandHacksUseCDPATHtodefinethebasedirectoryforcdcommandUsecdaliastonavigateupthedirectoryeffectivelyPerformmkdirandcdusingasinglecommand

Page 4: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Use“cd-”totogglebetweenthelasttwodirectoriesUsedirs,pushdandpopdtomanipulatedirectorystackUse“shopt-scdspell”toautomaticallycorrectmistypeddirectorynamesoncdChapter2:DateManipulationSetSystemDateandTimeSetHardwareDateandTimeDisplayCurrentDateandTimeinaSpecificFormatDisplayPastDateandTimeDisplayFutureDateandTimeChapter3:SSHClientCommandsIdentifySSHClientVersionLogintoRemoteHostusingSSH

Page 5: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

DebugSSHClientSessionToggleSSHSessionusingSSHEscapeCharacterSSHSessionStatisticsusingSSHEscapeCharacterChapter4:EssentialLinuxCommandsGrepCommand2

Page 6: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariFindCommandSuppressStandardOutputandErrorMessageJoinCommandChangetheCaseXargsCommandSortCommandUniqCommandCutCommandStatCommandDiffCommand

Page 7: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

DisplaytotalconnecttimeofusersChapter5:PS1,PS2,PS3,PS4andPROMPT_COMMANDPS1-DefaultInteractionPromptPS2-ContinuationInteractivePromptPS3-Promptusedby“select”insideshellscriptPS4-Usedby“set-x”toprefixtracingoutputPROMPT_COMMANDChapter6:ColorfulandFunctionalShellPromptUsingPS1Displayusername,hostnameandbasenameofdirectoryinthepromptDisplaycurrenttimeinthepromptDisplayoutputofanycommandinthepromptChangeforegroundcoloroftheprompt

Page 8: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ChangebackgroundcolorofthepromptDisplaymultiplecolorsinthepromptChangethepromptcolorusingtputCreateyourownpromptusingtheavailablecodesforPS1variableUsebashshellfunctioninsidePS1variableUseshellscriptinsidePS1variableChapter7:ArchiveandCompression3

Page 9: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariZipcommandbasicsAdvancedcompressionusingzipcommand.PasswordProtectionofZipfilesValidateaziparchiveTarCommandBasicsCombinegzip,bzip2withtarChapter8:CommandLineHistoryDisplayTIMESTAMPinhistoryusingHISTTIMEFORMATSearchthehistoryusingControl+RRepeatpreviouscommandquicklyusing4differentmethods

Page 10: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ExecuteaspecificcommandfromhistoryExecutepreviouscommandthatstartswithaspecificwordControlthetotalnumberoflinesinthehistoryusingHISTSIZEChangethehistoryfilenameusingHISTFILEEliminatethecontinuousrepeatedentryfromhistoryusingHISTCONTROLEraseduplicatesacrossthewholehistoryusingHISTCONTROLForcehistorynottorememberaparticularcommandusingHISTCONTROLClearalltheprevioushistoryusingoption-c

Page 11: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SubstitutewordsfromhistorycommandsSubstituteaspecificargumentforaspecificcommandDisabletheusageofhistoryusingHISTSIZEIgnorespecificcommandsfromthehistoryusingHISTIGNOREChapter9:SystemAdministrationTasksPartitionusingfdiskFormatapartitionusingmke2fskMountthepartition4

Page 12: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariFinetunethepartitionusingtune2fsCreateaswapfilesystem.CreateanewuserCreateanewgroupandassigntoanuserSetupSSHpasswordlesslogininOpenSSHUsessh-copy-idalongwithssh-agentCrontabSafeRebootOfLinuxUsingMagicSysRqKeyChapter10:ApachectlandHttpdExamplesPassdifferenthttpd.conffilenametoapachectl

Page 13: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

UseatemporaryDocumentRootwithoutmodifyinghttpd.confIncreasetheLogLeveltemporarilyDisplaythemodulesinsideApacheShowallaccepteddirectivesinsidehttpd.confValidatethehttpd.confaftermakingchangesDisplaythehttpdbuildparametersLoadaspecificmoduleonlyondemandChapter11:BashScriptingExecutionSequenceof.bash_*filesHowtogeneraterandomnumberinbashshellDebugashellscriptQuoting

Page 14: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ReaddatafilefieldsinsideashellscriptChapter12:SystemMonitoringandPerformanceFreecommandTopCommandPsCommandDfCommandKillCommandDuCommand5

Page 15: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariSarCommandvmstatCommandNetstatCommandSysctlCommandNiceCommandReniceCommand6

Page 16: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

lsofcommands.

Page 17: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 18: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Introduction”Thereareonly10typesofpeopleintheworld—thosewhounderstandbinary,thosewhodon’t,andthosewhounderstand—Geek

Page 19: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

I’mAjayKumarTiwari,authorofBookIhavedoneintensiveprogrammingonseverallanguagesandCismyfavorite.IhavedonelotofworkontheinfrastructuresideincludingLinuxsystemadministration,DBA,Networking,Hardwareand

Page 20: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Storage(EMC).Copyright©2015–AjayKumarTiwari7

Page 21: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

ForewordAnothercollectionofhacks?Yes!Ifyouhavejustcompletedyourfirstadmincourseorlookingforbetterwaystogetthejobdonethe"LinuxHack"eBookisagoodpointtostart.Theseusefultipsareconcise,wellwrittenandeasytoread.Welldone-IwillrecommendthiseBooktomystudents.--Prof.Dr.FritzMehner,FHSüdwestfalen,Germany(AuthorofseveralVimplugins,

Page 22: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

VersionVersion1.0Date25-Mar-2015RevisionsFirstEdition8

Page 23: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 24: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter1:PowerfulCDCommandHackscdisoneofthemostfrequentlyusedcommandsduringaUNIXsession.The6cdcommandhacksmentionedinthischapterwillboostyourproductivityinstantlyandmakeiteasiertonavigatethedirectorystructurefromcommandline.Hack1.UseCDPATHtodefinethebasedirectoryforcdcommandIfyouarefrequentlyperformingcdtosubdirectoriesofaspecificparentdirectory,youcansettheCDPATHtotheparentdirectoryandperformcdto

Page 25: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

thesubdirectorieswithoutgivingtheparentdirectorypathasexplainedbelow.[ramesh@dev-db~]# pwd/home/ramesh[ramesh@dev-db~]# cdmail-bash:cd:mail:Nosuchfileordirectory[Note:Thisislookingformaildirectoryundercurrentdirectory][ramesh@dev-db~]# exportCDPATH=/etc[ramesh@dev-db~]# cdmail/etc/mail[Note:Thisislookingformailunder/etcandnotundercurrentdirectory][ramesh@dev-db/etc/mail]# pwd

Page 26: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

/etc/mailTomakethischangepermanent,addexportCDPATH=/etctoyour~/.bash_profile9

Page 27: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariSimilartothePATHvariable,youcanaddmorethanonedirectoryentryintheCDPATHvariable,separatingthemwith:,asshownbelow.exportCDPATH=.:~:/etc:/varThishackcanbeveryhelpfulunderthefollowingsituations:oOracleDBAsfrequentlyworkingunder$ORACLE_HOME,cansettheCDPATHvariabletotheoraclehomeUnixsysadminsfrequentlyworkingunder/etc,cansettheCDPATHvariableto/etc

Page 28: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Developersfrequentlyworkingunderprojectdirectory/home/projects,cansettheCDPATHvariableto/home/projectsEnd-usersfrequentlyaccessingthesubdirectoriesundertheirhomedirectory,cansettheCDPATHvariableto~(homedirectory)oooHack2.UsecdaliastonavigateupthedirectoryeffectivelyWhenyouarenavigatingupaverylongdirectorystructure,youmaybeusingcd..\..\withmultiple..\’sdependingonhowmanydirectoriesyouwanttogoupasshownbelow.

Page 29: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#mkdir-p/tmp/very/long/directory/structure/that/is/too/deep#cd/tmp/very/long/directory/structure/that/is/too/deep#pwd/tmp/very/long/directory/structure/that/is/too/deep#cd../../../../10

Page 30: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack#pwd/tmp/very/long/directory/structureAjayKumarTiwariInsteadofexecutingcd../../../..tonavigatefourlevelsup,useoneofthefollowingthreealiasmethods:Method1:Navigateupthedirectoryusing“..n”Intheexamplebelow,..4isusedtogoup4directorylevel,..3togoup3directorylevel,..2togoup2directorylevel.Addthefollowingaliastoyour~/.bash_profileandre-login.alias

Page 31: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

aliasaliasaliasalias..="cd.."..2="cd../.."..3="cd../../.."..4="cd../../../.."..5="cd../../../../.."#cd/tmp/very/long/directory/structure/that/is/too/deep#..4[Note:use..4togoup4directorylevel]#pwd

Page 32: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

/tmp/very/long/directory/structure/Method2:NavigateupthedirectoryusingonlydotsIntheexamplebelow,…..(fivedots)isusedtogoup4directorylevel.Typing5dotstogoup4directorystructureisreallyeasytoremember,aswhenyoutypethefirsttwodots,youarethinking“goinguponedirectory”,afterthateveryadditionaldot,istogoonelevelup.So,use….(fourdots)togoup3directoryleveland..(twodots)togoup1directorylevel.Addthefollowingaliastoyour~/.bash_profileandre-loginforthe…..(fivedots)toworkproperly.alias..="cd.."11

Page 33: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackaliasaliasaliasalias...="cd../.."....="cd../../..".....="cd../../../.."......="cd../../../../.."AjayKumarTiwari#cd/tmp/very/long/directory/structure/that/is/too/deep#.....[Note:use.....(fivedots)togoup4directorylevel]

Page 34: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#pwd/tmp/very/long/directory/structure/

Method3:NavigateupthedirectoryusingcdfollowedbyconsecutivedotsIntheexamplebelow,cd…..(cdfollowedbyfivedots)isusedtogoup4directorylevel.Makingit5dotstogoup4directorystructureisreallyeasytoremember,aswhenyoutypethefirsttwodots,youarethinking“goinguponedirectory”,afterthateveryadditionaldot,istogoonelevelup.So,usecd….(cdfollowedbyfourdots)togoup3directorylevelandcd…(cdfollowedbythreedots)togoup2directorylevel.Addthefollowingaliastoyour~/.bash_profileandre-loginfortheabovecd…..(fivedots)toworkproperly.

Page 35: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

aliasaliasaliasaliasaliascd..="cd.."cd...="cd../.."cd....="cd../../.."cd.....="cd../../../.."cd......="cd../../../../.."#cd/tmp/very/long/directory/structure/that/is/too/deep#cd.....[Note:usecd.....togoup4directorylevel]#pwd

Page 36: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

/tmp/very/long/directory/structure12

Page 37: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariMethod5:NavigateupthedirectoryusingcdfollowedbynumberIntheexamplebelow,cd4(cdfollowedbynumber4)isusedtogoup4directorylevel.aliasaliasaliasaliasaliascd1="cd

Page 38: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

cd2="cdcd3="cdcd4="cdcd5="cd.."../.."../../.."../../../.."../../../../.."Hack3.PerformmkdirandcdusingasinglecommandSometimeswhenyoucreateanewdirectory,youmaycdtothenewdirectoryimmediatelytoperformsomeworkasshownbelow.

Page 39: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#mkdir-p/tmp/subdir1/subdir2/subdir3#cd/tmp/subdir1/subdir2/subdir3#pwd/tmp/subdir1/subdir2/subdir3Wouldn’titbenicetocombinebothmkdirandcdinasinglecommand?Addthefollowingtothe.bash_profileandre-login.$vi.bash_profilefunctionmkdircd(){mkdir-p"$@"&&evalcd"\"\$$#\"";}Now,performbothmkdirandcdatthesametimeusingasinglecommandasshownbelow:13

Page 40: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack#mkdircd/tmp/subdir1/subdir2/subdir3AjayKumarTiwari[Note:Thiscreatesthedirectoryandcdtoitautomatically]#pwd/tmp/subdir1/subdir2/subdir3Hack4.Use“cd-”totogglebetweenthelasttwodirectoriesYoucantogglebetweenthelasttwocurrentdirectoriesusingcd-asshownbelow.#cd/tmp/very/long/directory/structure/that/is/too/deep

Page 41: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#cd/tmp/subdir1/subdir2/subdir3#cd-#pwd/tmp/very/long/directory/structure/that/is/too/deep# cd-#pwd/tmp/subdir1/subdir2/subdir3# cd-#pwd/tmp/very/long/directory/structure/that/is/too/deepHack5.Usedirs,pushdandpopdtomanipulatedirectorystackYoucanusedirectorystacktopushdirectoriesintoitandlaterpopdirectoryfromthestack.Followingthreecommandsareusedinthisexample.

Page 42: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

14

Page 43: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackooodirs:Displaythedirectorystackpushd:PushdirectoryintothestackAjayKumarTiwaripopd:PopdirectoryfromthestackandcdtoitDirswillalwaysprintthecurrentdirectoryfollowedbythecontentofthestack.Evenwhenthedirectorystackisempty,dirscommandwillstillprintonlythecurrentdirectoryasshownbelow.#popd

Page 44: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

-bash:popd:directorystackempty#dirs~#pwd/home/rameshHowtousepushdandpopd?Letusfirstcreatesometemporarydirectoriesandpushthemtothedirectorystackasshownbelow.####mkdirmkdirmkdir

Page 45: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

mkdir/tmp/dir1/tmp/dir2/tmp/dir3/tmp/dir4#cd/tmp/dir1#pushd.#cd/tmp/dir2#pushd.#cd/tmp/dir3#pushd.#cd/tmp/dir4#pushd.15

Page 46: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari#dirs/tmp/dir4/tmp/dir4/tmp/dir3/tmp/dir2/tmp/dir1[Note:Thefirstdirectory(/tmp/dir4)ofthedircommandoutputisalwaysthecurrentdirectoryandnotthecontentfromthestack.]Atthisstage,thedirectorystackcontainsthefollowingdirectories:/tmp/dir4/tmp/dir3/tmp/dir2/tmp/dir1

Page 47: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Thelastdirectorythatwaspushedtothestackwillbeatthetop.Whenyouperformpopd,itwillcdtothetopdirectoryentryinthestackandremoveitfromthestack.Asshownabove,thelastdirectorythatwaspushedintothestackis/tmp/dir4.So,whenwedoapopd,itwillcdtothe/tmp/dir4andremoveitfromthedirectorystackasshownbelow.#popd#pwd/tmp/dir4[Note:Aftertheabovepopd,directoryStackContains:/tmp/dir3/tmp/dir2/tmp/dir1]#popd#pwd

Page 48: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

/tmp/dir3[Note:Aftertheabovepopd,directoryStackContains:/tmp/dir2/tmp/dir1]#popd16

Page 49: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack#pwd/tmp/dir2AjayKumarTiwari[Note:Aftertheabovepopd,directoryStackContains:/tmp/dir1]#popd#pwd/tmp/dir1[Note:Aftertheabovepopd,directoryStackisempty!]#popd-bash:popd:directorystackempty

Page 50: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Hack6.Use“shopt-scdspell”toautomaticallycorrectmistypeddirectorynamesoncdUseshopt-scdspelltocorrectthetyposinthecdcommandautomaticallyasshownbelow.Ifyouarenotgoodattypingandmakelotofmistakes,thiswillbeveryhelpful.#cd/etc/mall-bash:cd:/etc/mall:Nosuchfileordirectory# shopt-scdspell#cd/etc/mall#pwd/etc/mail[Note:Bymistake,whenItypedmallinsteadofmail,cdcorrecteditautomatically]

Page 51: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

17

Page 52: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter2:DateManipulationHack7.SetSystemDateandTimeTochangethesystemdateuse:#date{mmddhhmiyyyy.ss}ooooo

Page 53: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

omm–Monthdd–Datehh–24hourformatmi–Minutesyyyy–Yearss–secondsForexample,tosetsystemdatetoJan31#date013122192009.53Youcanalsochangesystemdateusingsetargumentasshownbelow.#date013122192009.53#date+%Y%m%d-s"20090131"#date-s"01/31/200922:19:53"#date-s"31JAN200922:19:53"

Page 54: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#dateset="31JAN200922:19:53"Tosetthetimeonly:#date+%T-s"22:19:53"18

Page 55: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack#date+%T%p-s"10:19:53PM"AjayKumarTiwariHack8.SetHardwareDateandTimeBeforesettingthehardwaredateandtime,makesuretheOSdateandtimeissetappropriatelyasshowninthehack#7.Setthehardwaredateandtimebasedonthesystemdateasshownbelow:#hwclock–systohc#hwclock--systohc–utcUsehwclockwithoutanyparameter,toviewthecurrenthardwaredateandtime:#hwclock

Page 56: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ChecktheclockfiletoverifywhetherthesystemissetforUTC:#cat/etc/sysconfig/clockZONE="America/Los_Angeles"UTC=falseARC=falseHack9.DisplayCurrentDateandTimeinaSpecificFormatFollowingaredifferentwaysofdisplayingthecurrentdateandtimeinvariousformats:19

Page 57: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 58: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack$dateThuJanAjayKumarTiwari108:19:23PST2009$date--date="now"ThuJan108:20:05PST2009$date--date="today"ThuJan108:20:12PST2009$date--date='1970-01-0100:00:01UTC+5hours'+%s18001$date'+CurrentDate:%m/%d/%y%nCurrentTime:%H:%M:%S'

Page 59: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

CurrentDate:01/01/09CurrentTime:08:21:41$date+"%d-%m-%Y"01-01-2009$date+"%d/%m/%Y"01/01/2009$date+"%A,%B%d%Y"Thursday,January012009Followingarethedifferentformatoptionsyoucanpasstothedatecommand:oooo

Page 60: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

oo%Ddate(mm/dd/yy)%ddayofmonth(01..31)%mmonth(01..12)%ylasttwodigitsofyear(00..99)%alocale’sabbreviatedweekdayname(Sun..Sat)%Alocale’sfullweekdayname,variablelength(Sunday..Saturday)%blocale’sabbreviatedmonthname(Jan..Dec)o20

Page 61: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackoAjayKumarTiwari%Blocale’sfullmonthname,variablelength(January..December)%Hhour(00..23)%Ihour(01..12)%Yyear(1970…)oooHack10.DisplayPastDateandTime

Page 62: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Followingarevariouswaystodisplayapastdateandtime:$date--date='3secondsago'ThuJan108:27:00PST2009$date--date="1dayago"WedDec3108:27:13PST2008$date--date="1daysago"WedDec3108:27:18PST2008$date--date="1monthago"MonDec108:27:23PST2008$date--date="1yearago"TueJan108:27:28PST2008$date--date="yesterday"WedDec3108:27:34PST2008$date--date="10months2dayago"

Page 63: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ThuFeb2808:27:41PST200821

Page 64: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack11.DisplayFutureDateandTimeFollowingexamplesshowshowtodisplayafuturedateandtime.$dateThuJan108:30:07PST2009$date--date='3seconds'ThuJan108:30:12PST2009$date--date='4hours'ThuJan112:30:17PST2009$date--date='tomorrow'

Page 65: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

FriJan208:30:25PST2009$date--date="1day"FriJan208:30:31PST2009$date--date="1days"FriJan208:30:38PST2009$date--date="2days"SatJan308:30:43PST2009$date--date='1month'SunFeb108:30:48PST2009$date--date='1week'ThuJan808:30:53PST2009$date--date="2months"SunMar108:30:58PST2009$date--date="2years"

Page 66: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SatJan108:31:03PST2011$date--date="nextday"FriJan208:31:10PST200922

Page 67: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack$date--date="-1daysago"FriJan208:31:15PST2009$date--date="thisWednesday"WedJan700:00:00PST2009AjayKumarTiwari23

Page 68: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 69: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 70: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter3:SSHClientCommandsHack12.IdentifySSHClientVersionSometimesitmaybenecessarytoidentifytheSSHclientthatyouarecurrentlyrunningandit’scorrespondingversionnumber.Usessh–Vtoidentifytheversionnumber.PleasenotethatLinuxcomeswithOpenSSH.ThefollowingexampleindicatesthatthisparticularsystemisusingOpenSSH:$ssh-VOpenSSH_3.9p1,OpenSSL0.9.7aFeb192003ThefollowingexampleindicatesthatthisparticularsystemisusingSSH2:

Page 71: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

$ssh-Vssh:SSHSecureShell3.2.9.1(non-commercialversion)oni686-pc-linux-gnuHack13.LogintoRemoteHostusingSSHTheFirsttimewhenyoulogintoaremotehostfromalocalhost,itwilldisplaythehostkeynotfoundmessageandyoucangive“yes”tocontinue.Thehostkeyoftheremotehostwillbeaddedunder.ssh2/hostkeysdirectoryofyourhomedirectory,asshownbelow.localhost$ ssh-ljsmithremotehost.example.comHostkeynotfoundfromdatabase.Keyfingerprint:xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxumYoucangetapublickey’sfingerprintbyrunning

Page 72: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

%ssh-keygen-Fpublickey.pub24

Page 73: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwarionthekeyfile.Areyousureyouwanttocontinueconnecting(yes/no)Hostkeysavedto/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pubhostkeyforremotehost.example.com,acceptedbyjsmithMonMay26200816:06:[email protected]:remotehost.example.com$TheSecondtimewhenyoulogintotheremotehostfromthelocalhost,itwillpromptonlyforthepasswordastheremotehostkeyisalreadyaddedtotheknownhostslistofthesshclient.

Page 74: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

localhost$ ssh-ljsmithremotehost.example.comjsmith@remotehost.example.compassword:remotehost.example.com$Forsomereason,ifthehostkeyoftheremotehostischangedafteryouloggedinforthefirsttime,youmaygetawarningmessageasshownbelow.Thiscouldbebecauseofvariousreasonssuchas:ooSysadminupgraded/reinstalledtheSSHserverontheremotehostSomeoneisdoingmaliciousactivityetc.,Thebestpossibleactiontotakebeforesaying“yes”tothemessagebelow,istocallyoursysadminandidentifywhyyougotthehostkeychangedmessageandverifywhetheritisthecorrecthostkeyornot.localhost$ ssh-ljsmithremotehost.example.com

Page 75: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@WARNING:HOSTIDENTIFICATIONHASCHANGED!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ITISPOSSIBLETHATSOMEONEISDOINGSOMETHINGNASTY!Someonecouldbeeavesdroppingonyourightnow(man-in-the-25

Page 76: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwarimiddleattack)!Itisalsopossiblethatthehostkeyhasjustbeenchanged.Pleasecontactyoursystemadministrator.Addcorrecthostkeyto“/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub”togetridofthismessage.Receivedserverkey’sfingerprint:xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-tuxumYoucangetapublickey’sfingerprintbyrunning%ssh-keygen-Fpublickey.pub

Page 77: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

onthekeyfile.Agentforwardingisdisabledtoavoidattacksbycorruptedservers.Areyousureyouwanttocontinueconnecting(yes/no)Doyouwanttochangethehostkeyondisk(yes/no)Agentforwardingre-enabled.Hostkeysavedto/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pubhostkeyforremotehost.example.com,acceptedbyjsmithMonMay26200816:17:[email protected]’spassword:remotehost$Hack14.DebugSSHClientSessionSometimesitisnecessarytoviewdebugmessagestotroubleshootanySSHconnectionissues.pass-v(lowercasev)optiontothesshasshownbelowto

Page 78: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

viewthesshdebugmessages.ExamplewithoutSSHclientdebugmessage:localhost$ ssh-ljsmithremotehost.example.comwarning:Connectingtoremotehost.example.comfailed:Noaddressassociatedtothename26

Page 79: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariExamplewithSSHclientdebugmessage:locaclhost$ ssh-v-ljsmithremotehost.example.comdebug:SshConfig/sshconfig.c:2838/ssh2_parse_config_ext:Metaconfigparsingstoppedatline3.debug:SshConfig/sshconfig.c:637/ssh_config_set_param_verbose:Settingvariable‘VerboseMode’to‘FALSE’.debug:

Page 80: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SshConfig/sshconfig.c:3130/ssh_config_read_file_ext:Read17paramsfromconfigfile.debug:Ssh2/ssh2.c:1707/main:Userconfigfilenotfound,usingdefaults.(Lookedfor‘/home/jsmith/.ssh2/ssh2_config’)debug:Connectingtoremotehost.example.com,port22…(SOCKSnotused)warning:Connectingtoremotehost.example.comfailed:NoaddressassociatedtothenameHack15.ToggleSSHSessionusingSSHEscapeCharacterWhenyou’veloggedontotheremotehostusingsshfromthelocalhost,youmaywanttocomebacktothelocalhosttoperformsomeactivityandgoback

Page 81: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

toremotehostagain.Inthiscase,youdon’tneedtodisconnectthesshsessiontotheremotehost.Instead,followthestepsbelow.1.Logintoremotehostfromlocalhost:localhost$ssh-ljsmithremotehost27

Page 82: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari2.Nowyouareconnectedtotheremotehost:remotehost$3.Tocomebacktothelocalhosttemporarily,typetheescapecharacter~andControl-Z.Whenyoutype~youwillnotseethatimmediatelyonthescreenuntilyoupress<Control-Z>andpressenter.So,ontheremotehostinanewlineenterthefollowingkeystrokesforthebelowtowork:~<Control-Z>remotehost$ ~^Z[1]+Stoppedssh-ljsmithremotehostlocalhost$

Page 83: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

4.NowyouarebacktothelocalhostandthesshremotehostclientsessionrunsasatypicalUNIXbackgroundjob,whichyoucancheckasshownbelow:localhost$ jobs[1]+Stoppedssh-ljsmithremotehost5.Youcangobacktotheremotehostsshwithoutenteringthepasswordagainbybringingthebackgroundsshremotehostsessionjobtoforegroundonthelocalhost.localhost$ fg%1ssh-ljsmithremotehostremotehost$28

Page 84: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 85: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack16.SSHSessionStatisticsusingSSHEscapeCharacterTogetsomeusefulstatisticsaboutthecurrentsshsession,dothefollowing.ThisworksonlyonSSH2client.1.Logintoremotehostfromlocalhost.localhost$ssh-ljsmithremotehost2.Ontheremotehost,typesshescapecharacter~followedbysasshownbelow.ThiswilldisplaylotofusefulstatisticsaboutthecurrentSSHconnection.remotehost$[Note:The~sisnotvisibleonthe

Page 86: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

commandlinewhenyoutype.]remotehost:remotehostlocalhost:localhostremoteversion:SSH-1.99-OpenSSH_3.9p1localversion:SSH-2.0-3.2.9.1SSHSecureShell(non-commercial)compressedbytesin:1506uncompressedbytesin:1622compressedbytesout:4997uncompressedbytesout:5118packetsin:15packetsout:24rekeys:0Algorithms:

Page 87: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Chosenkeyexchangealgorithm:diffie-hellman-group1-sha1Chosenhostkeyalgorithm:ssh-dssCommonhostkeyalgorithms:ssh-dss,ssh-rsaAlgorithmsclienttoserver:Cipher:aes128-cbcMAC:hmac-sha1Compression:zlib29

Page 88: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAlgorithmsservertoclient:Cipher:aes128-cbcMAC:hmac-sha1Compression:zliblocalhost$AjayKumarTiwariAdditionalSSHInfoOnasidenote,tosetupSSHkeybasedauthentication,referSSH2tutorials.30

Page 89: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 90: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 91: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter4:EssentialLinuxCommandsHack17.GrepCommandgrepcommandisusedtosearchfilesforaspecifictext.Thisisincrediblypowerfulcommandwithlotofoptions.Syntax:grep[options]pattern[files]HowcanIfindalllinesmatchingaspecifickeywordonafile?Inthisexample,greplooksforthetextJohninside/etc/passwdfileand

Page 92: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

displaysallthematchinglines.#grepJohn/etc/passwdjsmith:x:1082:1082:JohnSmith:/home/jsmith:/bin/bashjdoe:x:1083:1083:JohnDoe:/home/jdoe:/bin/bashOption-v,willdisplayallthelinesexceptthematch.Intheexamplebelow,itdisplaysalltherecordsfrom/etc/passwordthatdoesn'tmatchJohn.Note: Thereareseverallinesinthe/etc/passwordthatdoesn’tcontainthewordJohn.Onlythefirstlineoftheoutputisshownbelow.#grep-vJohn/etc/passwdjbourne:x:1084:1084:JasonBourne:/home/jbourne:/bin/bash

Howmanylinesmatchedthetextpatterninaparticularfile?Intheexamplebelow,itdisplaysthetotalnumberoflinesthatcontainsthe

Page 93: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

textJohnin/etc/passwdfile.31

Page 94: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari#grep-cJohn/etc/passwd2Youcanalsogetthetotalnumberoflinesthatdidnotmatchthespecificpatternbypassingoption-cv.#grep-cvJohn/etc/passwd39Howtosearchatextbyignoringthecase?Passtheoption-i(ignorecase),whichwillignorethecasewhilesearching.#grep-ijohn/etc/passwd

Page 95: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

jsmith:x:1082:1082:JohnSmith:/home/jsmith:/bin/bashjdoe:x:1083:1083:JohnDoe:/home/jdoe:/bin/bashHowdoIsearchallsubdirectoriesforatextmatchingaspecificpattern?Useoption-r(recursive)forthispurpose.Intheexamplebelow,itwillsearchforthetext"John"byignoringthecaseinsideallthesubdirectoriesunder/home/users.Thiswilldisplaytheoutputintheformatof"filename:linethatmatchingthepattern".Youcanalsopasstheoption-l,whichwilldisplayonlythenameofthefilethatmatchesthepattern.#grep-rijohn/home/users/home/users/subdir1/letter.txt:John,Thanksforyour

Page 96: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

contribution./home/users/name_list.txt:JohnSmith32

Page 97: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack/home/users/name_list.txt:JohnDoe#grep-riljohn/root/home/users/subdir1/letter.txt/home/users/name_list.txtAjayKumarTiwariHack18.FindCommandfindisfrequentlyusedcommandtofindfilesintheUNIXfilesystembasedonnumerousconditions.Letusreviewsomepracticeexamplesoffindcommand.Syntax:find[pathnames][conditions]Howtofindfilescontainingaspecificwordinits

Page 98: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

name?Thefollowingcommandlooksforallthefilesunder/etcdirectorywithmailinthefilename.#find/etc-name"*mail*"Howtofindallthefilesgreaterthancertainsize?Thefollowingcommandwilllistallthefilesinthesystemgreaterthan100MB.#find/-typef-size+100MHowtofindfilesthatarenotmodifiedinthelastxnumberofdays?Thefollowingcommandwilllistallthefilesthatweremodifiedmorethan60

Page 99: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

daysagounderthecurrentdirectory.33

Page 100: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack#find.-mtime+60AjayKumarTiwariHowtofindfilesthataremodifiedinthelastxnumberofdays?Thefollowingcommandwilllistallthefilesthatweremodifiedinthelasttwodaysunderthecurrentdirectory.#find.–mtime-2Howtodeleteallthearchivefileswithextension*.tar.gzand

Page 101: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

greaterthan100MB?Pleasebecarefulwhileexecutingthefollowingcommandasyoudon’twanttodeletethefilesbymistake.Thebestpracticeistoexecutethesamecommandwithls–ltomakesureyouknowwhichfileswillgetdeletedwhenyouexecutethecommandwithrm.#find/-typef-name*.tar.gz-size+100M-execls-l{}\;#find/-typef-name*.tar.gz-size+100M-execrm-f{}\;

Howtoarchiveallthefilesthatarenotmodifiedinthelastxnumberofdays?Thefollowingcommandfindsallthefilesnotmodifiedinthelast60daysunder/home/jsmithdirectoryandcreatesanarchivefilesunder/tmpinthe

Page 102: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

formatofddmmyyyy_archive.tar.#find/home/jsmith-typef-mtime+60|xargstar-cvf/tmp/`date'+%d%m%Y'_archive.tar`Onasidenote,youcanperformlotoffilerelatedactivities(includingfindingfiles)usingmidnightcommanderGUI,Unix.34

Page 103: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack19.SuppressStandardOutputandErrorMessageSometimewhiledebuggingashellscript,youmaynotwanttoseeeitherthestandardoutputorstandarderrormessage.Use/dev/nullasshownbelowforsuppressingtheoutput.Suppressstandardoutputusing>/dev/nullThiswillbeveryhelpfulwhenyouaredebuggingshellscripts,whereyoudon’twanttodisplaytheechostatementandinterestedinonlylookingat

Page 104: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

theerrormessages.#catfile.txt>/dev/null#./shell-script.sh>/dev/nullSuppressstandarderrorusing2>/dev/nullThisisalsohelpfulwhenyouareinterestedinviewingonlythestandardoutputanddon’twanttoviewtheerrormessages.#catinvalid-file-name.txt2>/dev/null#./shell-script.sh2>/dev/nullHack20.JoinCommandJoincommandcombineslinesfromtwofilesbasedonacommonfield.Intheexamplebelow,wehavetwofiles–employee.txtandsalary.txt.Bothhaveemployee-idascommonfield.So,wecanusejoincommandtocombine

Page 105: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

35

Page 106: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwarithedatafromthesetwofilesusingemployee-idasshownbelow.$catemployee.txt100200300400JasonSmithJohnDoeSanjayGuptaAshokSharma

Page 107: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

$catbonus.txt100200300400$5,000$500$3,000$1,250$ joinemployee.txtbonus.txt100200300400

Page 108: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

JasonSmith$5,000JohnDoe$500SanjayGupta$3,000AshokSharma$1,250Hack21.ChangetheCaseConvertafiletoallupper-case$catemployee.txt100200300400JasonSmithJohnDoe

Page 109: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SanjayGuptaAshokSharma$ tra-zA-Z<employee.txt100JASONSMITH200JOHNDOE300SANJAYGUPTA36

Page 110: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack400ASHOKSHARMAAjayKumarTiwariConvertafiletoalllower-case$catdepartment.txt100200300400FINANCEMARKETINGPRODUCTDEVELOPMENT

Page 111: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SALES$ trA-Za-z<department.txt100200300400financemarketingproductdevelopmentsalesHack22.XargsCommandxargsisaverypowerfulcommandthattakesoutputofacommandandpassitasargumentofanothercommand.Followingaresomepracticalexampleson

Page 112: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

howtousexargseffectively.1.Whenyouaretryingtodeletetoomanyfilesusingrm,youmaygeterrormessage:/bin/rmArgumentlisttoolong–Linux.Usexargstoavoidthisproblem.find~-name‘*.log’-print0|xargs-0rm-f2.Getalistofallthe*.conffileunder/etc/.Therearedifferentwaystogetthesameresult.Followingexampleisonlytodemonstratetheuseofxargs.Theoutputofthefindcommandinthisexampleispassedtothels–lonebyoneusingxargs.37

Page 113: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari#find/etc-name"*.conf"|xargsls–l3.IfyouhaveafilewithlistofURLsthatyouwouldliketodownload,youcanusexargsasshownbelow.#caturl-list.txt|xargswget–c4.Findoutallthejpgimagesandarchiveit.#find/-name*.jpg-typef-print|xargstar-cvzfimages.tar.gz5.Copyalltheimagestoanexternalhard-drive.#ls*.jpg|xargs-n1-icp{}/external-hard-drive/directory

Page 114: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Hack23.SortCommandSortcommandsortsthelinesofatextfile.Followingareseveralpracticalexamplesonhowtousethesortcommandbasedonthefollowingsampletextfilethathasemployeeinformationintheformat:employee_name:employee_id:department_name.$ catnames.txtEmmaThomas:100:MarketingAlexJason:200:SalesMadisonRandy:300:ProductDevelopmentSanjayGupta:400:SupportNishaSingh:500:Sales38

Page 115: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 116: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariSortatextfileinascendingorder$ sortnames.txtAlexJason:200:SalesEmmaThomas:100:MarketingMadisonRandy:300:ProductDevelopmentNishaSingh:500:SalesSanjayGupta:400:SupportSortatextfileindescendingorder$ sort-rnames.txtSanjayGupta:400:Support

Page 117: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

NishaSingh:500:SalesMadisonRandy:300:ProductDevelopmentEmmaThomas:100:MarketingAlexJason:200:SalesSortacolondelimitedtextfileon2(employee_id)$ sort-t:-k2names.txtEmmaThomas:100:MarketingAlexJason:200:SalesMadisonRandy:300:ProductDevelopmentSanjayGupta:400:SupportNishaSingh:500:SalesSortatabdelimitedtextfileon3rdfield(department_name)

Page 118: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

andsuppressduplicates$ sort-t:-u-k3names.txt39

Page 119: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackEmmaThomas:100:MarketingMadisonRandy:300:ProductDevelopmentAlexJason:200:SalesSanjayGupta:400:SupportAjayKumarTiwariSortthepasswdfilebythe3userid)$ sort-t:-k3n/etc/passwd|moreroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin

Page 120: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

adm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinSort/etc/hostsfilebyip-addres$ sort-t.-k1,1n-k2,2n-k3,3n-k4,4n/etc/hosts127.0.0.1localhost.localdomainlocalhost192.168.100.101dev-db.thegeekstuff.comdev-db192.168.100.102prod-db.thegeekstuff.comprod-db192.168.101.20dev-web.thegeekstuff.comdev-web192.168.101.21prod-web.thegeekstuff.comprod-webCombinesortwithothercommandsoo

Page 121: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ps–ef|sort :Sorttheoutputofprocesslistls-al|sort+4n :Listthefilesintheascendingorderofthefile-size.i.esortedby5thfiledanddisplayingsmallestfilesfirst.ls-al|sort+4nr :Listthefilesinthedescendingorderofthefile-size.i.esortedby5thfiledanddisplayinglargestfilesfirst.o40

Page 122: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack24.UniqCommandUniqcommandismostlyusedincombinationwithsortcommand,asuniqremovesduplicatesonlyfromasortedfile.i.eInorderforuniqtowork,alltheduplicateentriesshouldbeintheadjacentlines.Followingaresomecommonexamples.1.Whenyouhaveanemployeefilewithduplicateentries,youcandothefollowingtoremoveduplicates.$sortnamesd.txt|uniq$sort–unamesd.txt2.Ifyouwanttoknowhowmanylinesareduplicates,dothefollowing.The

Page 123: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

firstfieldinthefollowingexamplesindicateshowmanyduplicateswherefoundforthatparticularline.So,inthisexamplethelinesbeginningwithAlexandEmmawerefoundtwiceinthenamesd.txtfile.$ sortnamesd.txt|uniq–c22111AlexJason:200:SalesEmmaThomas:100:MarketingMadisonRandy:300:ProductDevelopmentNishaSingh:500:SalesSanjayGupta:400:Support

Page 124: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

3.Thefollowingdisplaysonlytheentriesthatareduplicates.$ sortnamesd.txt|uniq–cd2AlexJason:200:Sales2EmmaThomas:100:Marketing41

Page 125: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack25.CutCommandCutcommandcanbeusedtodisplayonlyspecificcolumnsfromatextfileorothercommandoutputs.Followingaresomeoftheexamples.Displaythe1stfield(employeename)fromacolondelimitedfile$ cut-d:-f1names.txtEmmaThomasAlexJason

Page 126: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

MadisonRandySanjayGuptaNishaSinghDisplay1stand3rdfieldfromacolondelimitedfile$ cut-d:-f1,3names.txtEmmaThomas:MarketingAlexJason:SalesMadisonRandy:ProductDevelopmentSanjayGupta:SupportNishaSingh:SalesDisplayonlythefirst8charactersofeverylineina

Page 127: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

file$ cut-c1-8names.txtEmmaThoAlexJasMadisonSanjayGNishaSi42

Page 128: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariMiscCutcommandexamplesocut-d:-f1/etc/passwd Displaystheunixloginnamesforalltheusersinthesystem.free|tr-s''|sed'/^Mem/!d'|cut-d""-fmemoryavailableonthesystem.oHack26.StatCommandStatcommandcanbeusedeithertocheckthestatus/propertiesofasinglefileorthefilesystem.

Page 129: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Displaystatisticsofafileordirectory.$ stat/etc/my.cnfFile:Size:Device:Access:(0/Access:Modify:Change:`/etc/my.cnf'346Blocks:16IOBlock:4096regularfile

Page 130: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

801h/2049dInode:279856Links:1(0644/-rw-r--r--)Uid:(0/root)Gid:root)2009-01-0102:58:30.000000000-08002006-06-0120:42:27.000000000-07002007-02-0214:17:27.000000000-0800$ stat/home/rameshFile:`/home/ramesh'Size:4096Blocks:8IOBlock:4096directoryDevice:803h/2051dInode:5521409Links:7Access:(0755/drwxr-xr-x)Uid:(401/ramesh)Gid:(401/ramesh)Access:2009-01-0112:17:42.000000000-0800

Page 131: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Modify:2009-01-0112:07:33.000000000-0800Change:2009-01-0912:07:33.000000000-080043

Page 132: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariDisplaythestatusofthefilesystemusingoption–f$ stat-f/File:ID:Blocks:1876998Inodes:"/"0Namelen:255Type:ext2/ext3

Page 133: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Total:2579457Free:2008027Available:Size:4096Total:1310720Free:1215892Hack27.DiffCommanddiffcommandcomparestwodifferentfilesandreportsthedifference.Theoutputisverycrypticandnotstraightforwardtoread.Syntax:diff[options]file1file2Whatwasmodifiedinmynewfilewhencomparetomyoldfile?Theoption-winthediffcommandwillignorethewhitespacewhileperformingthecomparison.Inthefollowingdiffoutput:o

Page 134: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Thelinesabove---,indicatesthechangeshappenedinfirstfileinthediffcommand(i.ename_list.txt).Thelinesbelow---,indicatesthechangeshappenedtothesecondfileinthediffcommand(i.ename_list_new.txt).Thelinesthatbelongtothefirstfilestartswith<andthelinesofsecondfilestartswith>.o# diff-wname_list.txtname_list_new.txt44

Page 135: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack2c2,3<JohnDoe--->JohnMDoe>JasonBourneAjayKumarTiwariHack28.DisplaytotalconnecttimeofusersAccommandwilldisplaythestatisticsabouttheuser’sconnecttime.Connecttimeforthecurrentloggedinuser

Page 136: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Withtheoption–d,itwillbreakdowntheoutputfortheindividualdays.Inthisexample,I’vebeenloggedintothesystemformorethan6hourstoday.OnDec1st,Iwasloggedinforabout1hour.$ ac–dDec1Dec2Dec3Dec4Todaytotaltotaltotaltotaltotal

Page 137: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

1.080.993.394.506.10ConnecttimeforalltheusersTodisplayconnecttimeforalltheusersuse–passhownbelow.Pleasenotethatthisindicatesthecumulativeconnecttimefortheindividualusers.$ ac-pjohnmadisonsanjaynisha

Page 138: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

3.640.0688.17105.9245

Page 139: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackrameshtotal309.21AjayKumarTiwari111.42ConnecttimeforaspecificuserTogetaconnecttimereportforaspecificuser,executethefollowing:$ ac-dsanjayJul2Aug25Sep3Sep4

Page 140: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Dec24Dec29Todaytotaltotaltotaltotaltotaltotaltotal12.855.051.035.37

Page 141: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

8.151.422.9546

Page 142: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter5:PS1,PS2,PS3,PS4andPROMPT_COMMANDHack29.PS1-DefaultInteractionPromptThedefaultinteractivepromptonyourLinuxcanbemodifiedasshownbelowtosomethingusefulandinformative.Inthefollowingexample,thedefaultPS1was“\s-\v\$”,whichdisplaystheshellnameandtheversionnumber.Letuschangethisdefaultbehaviortodisplaytheusername,hostnameandcurrentworkingdirectorynameasshownbelow.

Page 143: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

-bash-3.2$exportPS1="\u@\h\w>"ramesh@dev-db~>cd/etc/mailramesh@dev-db/etc/mail>[Note:Promptchangedto"username@hostnamecurrent-dir>"format]FollowingPS1codesareusedinthisexample:ooo\u–Username\h–Hostname\w-Fullpathnameofcurrentdirectory.Pleasenotethatwhenyouareinthehomedirectory,thiswilldisplayonly~asshown

Page 144: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

aboveNotethatthereisaspaceattheendinthevalueofPS1.Personally,Ipreferaspaceattheendofthepromptforbetterreadability.MakethissettingpermanentbyaddingexportPS1=”\u@\h\w>”toeither.bash_profile(or).bashrcasshownbelow.47

Page 145: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariramesh@dev-db~>vi~/.bash_profileramesh@dev-db~>vi~/.bashrc[Note:AddexportPS1="\u@\h\w>"tooneoftheabovefiles]RefertothenextchapterforseveralpracticalexamplesofPS1usageindetail.Hack30.PS2-ContinuationInteractivePromptAverylongcommandcanbebrokendowntomultiplelinesbygiving\attheendoftheline.Thedefaultinteractivepromptforamulti-linecommandis“>“.Letuschangethisdefaultbehaviortodisplay“continue->”byusingPS2

Page 146: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

environmentvariableasshownbelow.ramesh@dev-db~> myisamchk--silent--force--fast--update-state\> --key_buffer_size=512M--sort_buffer_size=512M\> --read_buffer_size=4M--write_buffer_size=4M\> /var/lib/mysql/bugs/*.MYI[Note:Thisusesthedefault">"forcontinuationprompt]ramesh@dev-db~> exportPS2="continue->"ramesh@dev-db~> myisamchk--silent--force--fast--update-state\continue-> --key_buffer_size=512M--sort_buffer_size=512M\continue-> --read_buffer_size=4M--write_buffer_size=4M

Page 147: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

\continue-> /var/lib/mysql/bugs/*.MYI[Note:Thisusesthemodified"continue->"for48

Page 148: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackcontinuationprompt]AjayKumarTiwariIfounditveryhelpfulandeasytoread,whenIbreakmylongcommandsintomultiplelinesusing\.Ihavealsoseenotherswhodon’tliketobreak-uplongcommands.Hack31.PS3-Promptusedby“select”insideshellscriptYoucandefineacustompromptfortheselectloopinsideashellscript,usingthePS3environmentvariable,asexplainedbelow.ShellscriptandoutputWITHOUT

Page 149: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

PS3:ramesh@dev-db~> catps3.shselectiinmontuewedexitdocase$iinmon)echo"Monday";;tue)echo"Tuesday";;wed)echo"Wednesday";;exit)exit;;esacdoneramesh@dev-db~> ./ps3.sh1)mon

Page 150: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

2)tue3)wed4)exit#?1Monday#?4[Note:Thisdisplaysthedefault"#?"forselectcommandprompt]49

Page 151: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariShellscriptandoutputWITHPS3:ramesh@dev-db~> catps3.shPS3="Selectaday(1-4):"selectiinmontuewedexitdocase$iinmon)echo"Monday";;tue)echo"Tuesday";;wed)echo"Wednesday";;

Page 152: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

exit)exit;;esacdoneramesh@dev-db~> ./ps3.sh1)mon2)tue3)wed4)exitSelectaday(1-4):1MondaySelectaday(1-4):4[Note:Thisdisplaysthemodified"Selectaday(1-4):"forselectcommandprompt]

Page 153: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Hack32.PS4-Usedby“set-x”toprefixtracingoutputThePS4shellvariabledefinesthepromptthatgetsdisplayed,whenyouexecuteashellscriptindebugmodeasshownbelow.ShellscriptandoutputWITHOUTPS4:50

Page 154: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackramesh@dev-db~> catps4.shset-xecho"PS4demoscript"ls-l/etc/|wc-ldu-sh~ramesh@dev-db~> ./ps4.sh ++echo'PS4demoscript'PS4demoscript++ls-l/etc/++wc-l243

Page 155: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

++du-sh/home/ramesh48K/home/rameshAjayKumarTiwari[Note:Thisdisplaysthedefault"++"whiletracingtheoutputusingset-x]ShellscriptandoutputWITHPS4:ThePS4definedbelowintheps4.shhasthefollowingtwocodes:oo$0-indicatesthenameofscript$LINENO-displaysthecurrentlinenumberwithinthescriptramesh@dev-db~> catps4.sh

Page 156: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

exportPS4='$0.$LINENO+'set-xecho"PS4demoscript"ls-l/etc/|wc-ldu-sh~ramesh@dev-db~> ./ps4.sh../ps4.sh.3+echo'PS4demoscript'PS4demoscript../ps4.sh.4+ls-l/etc/51

Page 157: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack../ps4.sh.4+wc-l243../ps4.sh.5+du-sh/home/ramesh48K/home/rameshAjayKumarTiwari[Note:Thisdisplaysthemodified"{script-name}.{line-number}+"whiletracingtheoutputusingset-x]Hack33.PROMPT_COMMANDBashshellexecutesthecontentofthePROMPT_COMMANDjustbeforedisplayingthePS1variable.ramesh@dev-db~> exportPROMPT_COMMAND="date+%k:%m:%S"

Page 158: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

22:08:42ramesh@dev-db~>[Note:ThisdisplaysthePROMPT_COMMANDandPS1outputondifferentlines]IfyouwanttodisplaythevalueofPROMPT_COMMANDinthesamelineasthePS1,usetheecho-nasshownbelow.ramesh@dev-db~> exportPROMPT_COMMAND="echo-n[$(date+%k:%m:%S)]"[22:08:51]ramesh@dev-db~>[Note:ThisdisplaysthePROMPT_COMMANDandPS1outputonthesameline]52

Page 159: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 160: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter6:ColorfulandFunctionalShellPromptUsingPS1Hack34.Displayusername,hostnameandbasenameofdirectoryinthepromptThePS1inthisexampledisplaysfollowingthreeinformationintheprompt:ooo

Page 161: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

\u–Username\h–Hostname\W–Basenameofthecurrentworkingdirectory-bash-3.2$ exportPS1="\u@\h\W>"ramesh@dev-db~>cd/etc/mailramesh@dev-dbmail>Hack35.DisplaycurrenttimeinthepromptInthePS1environmentvariable,youcandirectlyexecuteanyLinuxcommand,byspecifyingintheformat$(linux_command).Inthefollowingexample,thecommand$(date)isexecutedtodisplaythecurrenttimeinsidetheprompt.ramesh@dev-db~> exportPS1="\u@\h[\$(date+%k:%m:%S)]>"

Page 162: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ramesh@dev-db[11:09:56]>53

Page 163: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariYoucanalsouse\ttodisplaythecurrenttimeinthehh:mm:ssformatasshownbelow:ramesh@dev-db~> exportPS1="\u@\h[\t]>"ramesh@dev-db[12:42:55]>Youcanalsouse\@todisplaythecurrenttimein12-houram/pmformatasshownbelow:ramesh@dev-db~> exportPS1="[\@]\u@\h>"[04:12PM]ramesh@dev-db>Hack36.Displayoutputofanycommandintheprompt

Page 164: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

YoucandisplayoutputofanyLinuxcommandintheprompt.Thefollowingexampledisplaysthreeitemsseparatedby|(pipe)inthecommandprompt:ooo\!:Thehistorynumberofthecommand\h:hostname$kernel_version:Theoutputoftheuname-rcommandfrom$kernel_versionvariable\$?:Statusofthelastcommandoramesh@dev-db~> kernel_version=$(uname-r)ramesh@dev-db~> exportPS1="\!|\h|$kernel_version|\$?>"473|dev-db|2.6.25-14.fc9.i686|0>

Page 165: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

54

Page 166: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack37.ChangeforegroundcolorofthepromptDisplaypromptinbluecolor,alongwithusername,hostandcurrentdirectoryinformation$ exportPS1="\e[0;34m\u@\h\w>\e[m"[Note:Thisisforlightblueprompt]$ exportPS1="\e[1;34m\u@\h\w>\e[m"[Note:Thisisfordarkblueprompt]\e[-Indicatesthebeginningofcolorpromptx;ym-Indicatescolorcode.Usethecolorcodevaluesmentionedbelow.

Page 167: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

\e[m-indicatestheendofcolorpromptoooColorCodeTable:Black0;30Blue0;34Green0;32Cyan0;36Red0;31Purple0;35Brown0;33[Note:Replace0with1fordarkcolor]

Page 168: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Makethecolorchangepermanentbyaddingthefollowinglinesyour~/.bash_profileor~/.bashrc$vi~/.bash_profile55

Page 169: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariSTARTCOLOR='\e[0;34m';ENDCOLOR="\e[0m"exportPS1="$STARTCOLOR\u@\h\w>$ENDCOLOR"Hack38.ChangebackgroundcolorofthepromptChangethebackgroundcolorbyspecifying\e[{code}minthePS1promptasshownbelow.$ exportPS1="\e[47m\u@\h\w>\e[m"[Note:ThisisforLightGraybackground]Combinationofbackgroundandforeground.$ exportPS1="\e[0;34m\e[47m\u@\h\w>\e[m"

Page 170: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

[Note:ThisisforLightBlueforegroundandLightGraybackground]Addthefollowingtoyour~/.bash_profileor~/.bashrctomaketheabovebackgroundandforegroundcolorpermanent.$vi~/.bash_profileSTARTFGCOLOR='\e[0;34m';STARTBGCOLOR="\e[47m"ENDCOLOR="\e[0m"exportPS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h\w>$ENDCOLOR"Playaroundbyusingthefollowingbackgroundcolorandchoosetheonethatmatchyourtaste:o\e[40m

Page 171: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

56

Page 172: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackooooooo\e[41m\e[42m\e[43m\e[44m

Page 173: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

\e[45m\e[46m\e[47mAjayKumarTiwariHack39.DisplaymultiplecolorsinthepromptYoucanalsodisplaymultiplecolorsinthesameprompt.Addthefollowingfunctiontoyour~/.bash_profilefunctionprompt{localBLUE="\[\033[0;34m\]"localDARK_BLUE="\[\033[1;34m\]”localRED=”\[\033[0;31m\]”localDARK_RED=”\[\033[1;31m\]”localNO_COLOR=”\[\033[0m\]”

Page 174: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

case$TERMinxterm*|rxvt*)TITLEBAR=’\[\033]0;\u@\h:\w\007\]’;;*)TITLEBAR=”";;esacPS1=”\u@\h[\t]>”PS1=”${TITLEBAR}\$BLUE\u@\h$RED[\t]>$NO_COLOR”PS2=’continue->‘PS4=’$0.$LINENO+‘}

Page 175: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

57

Page 176: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariYoucanre-loginforthechangestotakeeffectorsourcethe.bash_profileasshownbelow.$../.bash_profile$ promptramesh@dev-db[13:02:13]>Hack40.ChangethepromptcolorusingtputYoucanalsochangecolorofthePS1promptusingtputasshownbelow:$exportPS1="\[$(tputbold)$(tputsetb4)$(tputsetaf7)\]\u@\h:\w$\[$(tputsgr0)\]“tputColorCapabilities:

Page 177: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ooootputsetab[1-7]-SetabackgroundcolorusingANSIescapetputsetb[1-7]-Setabackgroundcolortputsetaf[1-7]-SetaforegroundcolorusingANSIescapetputsetf[1-7]-SetaforegroundcolortputTextModeCapabilities:ooooo

Page 178: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

tputbold-Setboldmodetputdim-turnonhalf-brightmodetputsmul-beginunderlinemodetputrmul-exitunderlinemodetputrev-Turnonreversemode58

Page 179: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackoooAjayKumarTiwaritputsmso-Enterstandoutmode(boldonrxvt)tputrmso-Exitstandoutmodetputsgr0-TurnoffallattributesColorCodefortput:ooo

Page 180: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ooooo0–Black1–Red2–Green3–Yellow4–Blue5–Magenta6–Cyan7-White

Page 181: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Hack41.CreateyourownpromptusingtheavailablecodesforPS1variableUsethefollowingcodesandcreateyourownpersonalPS1Linuxpromptthatisfunctionalandsuitesyourtaste.oo\aanASCIIbellcharacter(07)\dthedatein“WeekdayMonthDate”format(e.g.,“TueMay26″)\D{format}-theformatispassedtostrftime(3)andtheresultisinsertedintothepromptstring;anemptyformatresultsinalocale-specifictimerepresentation.Thebracesarerequired\eanASCIIescapecharacter(033)

Page 182: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

oo59

Page 183: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackooooooo\hthehostnameuptothefirstpart\HthehostnameAjayKumarTiwari\jthenumberofjobscurrentlymanagedbytheshell

Page 184: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

\lthebasenameoftheshell’sterminaldevicename\nnewline\rcarriagereturn\sthenameoftheshell,thebasenameof$0(theportionfollowingthefinalslash)\tthecurrenttimein24-hourHH:MM:SSformat\Tthecurrenttimein12-hourHH:MM:SSformat\@thecurrenttimein12-houram/pmformat\Athecurrenttimein24-hourHH:MMformat\utheusernameofthecurrentuser\vtheversionofbash(e.g.,2.00)\Vthereleaseofbash,version+patchlevel(e.g.,2.00.0)\wthecurrentworkingdirectory,with$HOMEabbreviatedwithatilde

Page 185: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

\Wthebasenameofthecurrentworkingdirectory,with$HOMEabbreviatedwithatilde\!thehistorynumberofthiscommand\#thecommandnumberofthiscommand\$iftheeffectiveUIDis0,a#,otherwisea$\nnnthecharactercorrespondingtotheoctalnumbernnn\\abackslash\[beginasequenceofnon-printingcharacters,whichcouldbeusedtoembedaterminalcontrolsequenceintotheprompt60oooo

Page 186: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ooooooooooo

Page 187: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackoAjayKumarTiwari\]endasequenceofnon-printingcharacterHack42.UsebashshellfunctioninsidePS1variableYoucanalsoinvokeabashshellfunctioninthePS1asshownbelow.ramesh@dev-db~>functionhttpdcount{>psaux|grephttpd|grep-vgrep|wc-l>}ramesh@dev-db~> exportPS1="\u@\h[`httpdcount`]>"ramesh@dev-db[12]>[Note:Thisdisplaysthetotalnumberofrunninghttpd

Page 188: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

processes]Youcanaddthefollowinglinetoyour~/.bash_profileor~/.bashrctomakethischangepermanent:$vi.bash_profilefunctionhttpdcount{psaux|grephttpd|grep-vgrep|wc-l}exportPS1='\u@\h[`httpdcount`]>'Hack43.UseshellscriptinsidePS1variableYoucanalsoinvokeashellscriptinsidethePS1variable.Intheexamplebelow,the~/bin/totalfilesize.sh,whichcalculatesthetotalfilesizeofthecurrentdirectory,isinvokedinsidethePS1variable.ramesh@dev-db~>cat~/bin/totalfilesize.sh

Page 189: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

61

Page 190: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariforfilesizein$(ls-l.|grep"^-"|awk'{print$5}')dolettotalsize=$totalsize+$filesizedoneecho-n"$totalsize"ramesh@dev-db~>exportPATH=$PATH:~/binramesh@dev-db~> exportPS1="\u@\h[\$(totalfilesize.sh)bytes]>"ramesh@dev-db[534bytes]>cd/etc/mail

Page 191: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ramesh@dev-db[167997bytes]>[Note:Thisexecutesthetotalfilesize.shtodisplaythetotalfilesizeofthecurrentdirectoryinthePS1prompt]62

Page 192: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 193: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter7:ArchiveandCompressionHack44.ZipcommandbasicsHowtozipmultiplefiles?syntax:zip{.zipfile-name}{file-names}#zipvar-log-files.zip/var/log/*adding:var/log/acpid(deflated81%)adding:var/log/anaconda.log(deflated79%)adding:var/log/anaconda.syslog(deflated73%)adding:var/log/anaconda.xlog(deflated82%)

Page 194: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

adding:var/log/audit/(stored0%)adding:var/log/boot.log(stored0%)adding:var/log/boot.log.1(deflated40%)adding:var/log/boot.log.2(deflated42%)adding:var/log/boot.log.3(deflated40%)adding:var/log/boot.log.4(deflated40%)Howtozipadirectoryandit’sfilesrecursively?#zip-rvar-log-dir.zip/var/log/updating:var/log/(stored0%)adding:var/log/wtmp(deflated78%)adding:var/log/scrollkeeper.log(deflated94%)adding:var/log/rpmpkgs.3(deflated68%)

Page 195: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

adding:var/log/spooler(stored0%)adding:var/log/cron.2(deflated90%)adding:var/log/spooler.1(stored0%)adding:var/log/spooler.4(stored0%)adding:var/log/httpd/(stored0%)adding:var/log/rpmpkgs.1(deflated68%)adding:var/log/anaconda.log(deflated79%)adding:var/log/secure.2(deflated93%)63

Page 196: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackHowtounzipa*.zipcompressedfile?#unzipvar-log.zipArchive:var-log.zipinflating:var/log/acpidinflating:var/log/anaconda.loginflating:var/log/anaconda.sysloginflating:var/log/anaconda.xlogcreating:var/log/audit/AjayKumarTiwariToseeadetailedoutputduringunzippassthe–voptionasshownbelow.

Page 197: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

# unzip-vvar-log.zipArchive:var-log.zipLengthMethodSizeRatioDateTimeName-----------------------------------1916Defl:N36981%02-08-0814:27var/log/acpid13546Defl:N290079%02-02-0714:25var/log/anaconda.logskip..7680Defl:N411var/log/wtmp.140981Defl:N7395var/log/Xorg.0.log---------------

Page 198: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

----414069912809229files95%82%---93%12-30-0810:5502-08-0814:28fe876ee96386a95e---56CRC-32------e2ffdc0c

Page 199: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

34cc03a1---

Howtolistacontentofzipfilewithuncompressingit?# unzip-lvar-log.zipArchive:var-log.zipLengthDateTime----------------191602-08-0814:27Name----var/log/acpid64

Page 200: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack13546..skip..4098140981--------4140699102-08-0814:2802-08-0714:5602-02-0714:25AjayKumarTiwarivar/log/anaconda.log

Page 201: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

var/log/Xorg.0.logvar/log/Xorg.0.log.old-------56filesHack45.Advancedcompressionusingzipcommand.Thereare10levelsofcompressionprovidedbyzipcommand.oLevel0isthelowestlevel,whereitjustarchivesthefilewithoutanycompression.Level1willperformlittlecompression.But,willbeveryfast.Level6isthedefaultlevelofcompression.Level9isthemaximumcompression.Thiswillbeslowerwhencomparedtodefaultlevel.Inmyopinion,unlessyouare

Page 202: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

compressingahugefile,youshouldalwaysuselevel9.oooIntheexamplebelow,IusedLevel0,defaultLevel6,andLevel9compressiononasamedirectory.Seethecompressedfilesizeyourself.#zipvar-log-files-default.zip/var/log/*#zip-0var-log-files-0.zip/var/log/*#zip-9var-log-files-9.zip/var/log/*#ls-ltr-rw-r--r--1rootrootvar-log-files-default.zip-rw-r--r--1rootrootvar-log-files-0.zip

Page 203: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

-rw-r--r--1rootrootvar-log-files-9.zip2817248Jan41415301Jan2582610Jan113:05113:05113:0665

Page 204: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack46.PasswordProtectionofZipfilesPasstheoption–Ptothezipcommandtoassignapasswordtothezipfile.#zip-Pmysecurepwdvar-log-protected.zip/var/log/*Theaboveoptionisgoodifyouareusingthecommandinsideashell-scriptforbackgroundjobs.However,whenyouareperformingthecompressioninteractivelyonthecommand-line,youdon’twantthepasswordtobevisibleinthehistory.So,usetheoption–easshownbelowtoassignthepassword.#zip-evar-log-protected.zip/var/log/*Enterpassword:Verifypassword:

Page 205: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

updating:var/log/acpid(deflated81%)updating:var/log/anaconda.log(deflated79%)Whenyouareuncompressingapasswordprotectedfile,itwillaskforthepasswordasshownbelow.#unzipvar-log-protected.zipArchive:var-log-protected.zip[var-log-protected.zip]var/log/acpidpassword:Hack47.ValidateaziparchiveSometimeyoumaywanttovalidateaziparchivewithoutextractingit.Totestthevalidityofthezipfile,passoption–tasshownbelow.# unzip-tvar-log.zipArchive:var-log.ziptesting:var/log/acpid

Page 206: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

testing:var/log/anaconda.log66OKOK

Page 207: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHacktesting:var/log/anaconda.syslogskip...testing:var/log/wtmptesting:var/log/wtmp.1testing:var/log/Xorg.0.logAjayKumarTiwariOKOKOKOKNoerrorsdetectedincompresseddataofvar-log.zip.

Page 208: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Hack48.TarCommandBasicstarcommand(tapearchive)isusedtoconvertagroupoffilesintoanarchive.Syntax:tar[options][tar-archive-name][other-file-names]HowcanIcreateasinglebackupfileofallfilesandsubdirectoriesundermyhomedirectory?Thefollowingcommandcreatesasinglearchivebackupfilecalledmy_home_directory.tarunder/tmp.Thisarchivewillcontainallthefilesandsubdirectoriesunder/home/jsmith.o

Page 209: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

oOptionc,standsforcreateanarchive.Optionvstandsforverbosemode,displaysadditionalinformationwhileexecutingthecommand.Optionfindicatesthearchivefilenamementionedinthecommand.o#tarcvf/tmp/my_home_directory.tar/home/jsmith67

Page 210: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHowdoIviewallthefilesinsidethetararchive?Optiontwilldisplayallthefilesfromthetararchive.#tartvf/tmp/my_home_directory.tarHowdoIextractallthefilesfromatararchive?Optionxwillextractthefilesfromthetararchiveasshownbelow.Thiswillextractthecontenttothecurrentdirectorylocationfromwherethecommandisexecuted.

Page 211: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#tarxvf/tmp/my_home_directory.tarHowdoIextracttar.gzfilestoaspecificdirectory?#tarxvfz/tmp/my_home_directory.tar.gz–C/home/rameshHack49.Combinegzip,bzip2withtarHowtousegzipwithtar?Add optionz tothetarcommandwhendealingwithtar.gzcompressedfile.#tarcvfz/tmp/my_home_directory.tar.gz/home/jsmith#tarxvfz/tmp/my_home_directory.tar.gz#tartvfz/tmp/my_home_directory.tar.gzNote: Usinggzipisfasterwhencomparedtobzip2.

Page 212: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

68

Page 213: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHowtousebzip2withtar?Add optionj tothetarcommandwhendealingwithtar.bz2compressedfile.#tarcvfj/tmp/my_home_directory.tar.bz2/home/jsmith#tarxvfj/tmp/my_home_directory.tar.bz2#tartvfj/tmp/my_home_directory.tar.bz2Note: Usingbizp2giveshigherlevelofcompressionwhencomparedtogzip.69

Page 214: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 215: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 216: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter8:CommandLineHistoryWhenyouareusingLinuxcommandlinefrequently,usingthehistoryeffectivelycanbeamajorproductivityboost.Infact,onceyouhavemasteredthe15examplesthatI’veprovidedhere,you’llfindusingcommandlinemoreenjoyableandfun.Hack50.DisplayTIMESTAMPinhistoryusingHISTTIMEFORMATTypicallywhenyoutypehistoryfromcommandline,itdisplaysthecommand#andthecommand.Forauditingpurpose,itmaybebeneficialto

Page 217: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

displaythetimestampalongwiththecommandasshownbelow.# exportHISTTIMEFORMAT=’%F%T‘#history1234|more2008-08-052008-08-052008-08-052008-08-0519:02:3919:02:39

Page 218: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

19:02:3919:02:39servicenetworkrestartexitidcat/etc/redhat-releaseNote: Youcanalsosetupthefollowingaliastoviewtherecenthistorycommands.aliash1='history10'aliash2='history20'aliash3='history30'Hack51.SearchthehistoryusingControl+RIstronglybelievethatthismaybeyourmostfrequentlyusedfeatureof

Page 219: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

history.Whenyou’vealreadyexecutedaverylongcommand,youcansimply70

Page 220: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwarisearchhistoryusingakeywordandre-executethesamecommandwithouthavingtotypeitfully.PressControl+Randtypethekeyword.Inthefollowingexample,Isearchedforred,whichdisplayedthepreviouscommand“cat/etc/redhat-release”inthehistorythatcontainedthewordred.#[Note:PressCtrl+Rfromthecommandprompt,whichwilldisplaythereverse-i-searchpromptasshownbelow](reverse-i-search)`red‘:cat/etc/redhat-release[Note:Pressenterwhenyouseeyourcommand,which

Page 221: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

willexecutethecommandfromthehistory]# cat/etc/redhat-releaseFedorarelease9(Sulphur)Sometimesyouwanttoeditacommandfromhistorybeforeexecutingit.Fore.g.youcansearchforhttpd,whichwilldisplayservicehttpdstopfromthecommandhistory,selectthiscommandandchangethestoptostartandre-executeitagainasshownbelow.#[Note:PressCtrl+Rfromthecommandprompt,whichwilldisplaythereverse-i-searchprompt](reverse-i-search)`httpd‘:servicehttpdstop[Note:Presseitherleftarroworrightarrowkeywhenyouseeyourcommand,whichwilldisplaythecommandforyoutoedit,beforeexecutingit]#servicehttpdstart

Page 222: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

71

Page 223: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack52.Repeatpreviouscommandquicklyusing4differentmethodsSometimeyoumayenduprepeatingthepreviouscommandsforvariousreasons.Followingarethe4differentwaystorepeatthelastexecutedcommand.1.Usethe uparrow toviewthepreviouscommandandpressentertoexecuteit.2. Type!! andpressenterfromthecommandline3. Type!-1 andpressenterfromthecommandline.4. PressControl+P willdisplaythepreviouscommand,pressenter

Page 224: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

toexecuteitHack53.ExecuteaspecificcommandfromhistoryInthefollowingexample,Ifyouwanttorepeatthecommand#4,execute!4asshownbelow.#history1234|moreservicenetworkrestartexitid

Page 225: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

cat/etc/redhat-release# !4cat/etc/redhat-releaseFedorarelease9(Sulphur)72

Page 226: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack54.ExecutepreviouscommandthatstartswithaspecificwordType!followedbythestartingfewlettersofthecommandthatyouwouldliketore-execute.Inthefollowingexample,typing!psandenter,executedthepreviouscommandstartingwithps,whichis‘psaux|grepyp’.# !pspsaux|grepyproot169470.0Sl13:100:00ypbindroot175030.0

Page 227: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

S+19:190:00grepyp0.10.03651641241264?740pts/0Hack55.ControlthetotalnumberoflinesinthehistoryusingHISTSIZEAppendthefollowingtwolinestothe.bash_profileandrelogintothebashshellagaintoseethechange.Inthisexample,only450commandwillbestoredinthebashhistory.#vi~/.bash_profile

Page 228: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

HISTSIZE=450HISTFILESIZE=450Hack56.ChangethehistoryfilenameusingHISTFILEBydefault,historyisstoredin~/.bash_historyfile.Addthefollowinglinetothe.bash_profileandrelogintothebashshell,tostorethehistorycommandin.commandline_warriorfileinsteadof.bash_historyfile.I’myettofigureoutapracticaluseforthis.Icanseethisgettingusedwhenyouwanttotrackcommandsexecutedfromdifferentterminalsusingdifferenthistoryfilename.73

Page 229: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari#vi~/.bash_profileHISTFILE=/root/.commandline_warriorHack57.EliminatethecontinuousrepeatedentryfromhistoryusingHISTCONTROLInthefollowingexamplepwdwastypedthreetimes,whenyoudohistory,youcanseeallthe3continuousoccurrencesofit.Toeliminateduplicates,setHISTCONTROLtoignoredupsasshownbelow.#pwd#pwd#pwd

Page 230: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#history|tail-444pwd45pwd46pwd47history|tail-4[Note:Therearethreepwdcommandsinhistory,afterexecutingpwd3timesasshownabove]# exportHISTCONTROL=ignoredups#pwd#pwd#pwd#history|tail-356exportHISTCONTROL=ignoredups57pwd

Page 231: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

58history|tail-474

Page 232: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari[Note:Thereisonlyonepwdcommandinthehistory,evenafterexecutingpwd3timesasshownabove]Hack58.EraseduplicatesacrossthewholehistoryusingHISTCONTROLTheignoredupsshownaboveremovesduplicatesonlyiftheyareconsecutivecommands.Toeliminateduplicatesacrossthewholehistory,settheHISTCONTROLtoerasedupsasshownbelow.# exportHISTCONTROL=erasedups#pwd#servicehttpdstop

Page 233: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#history|tail-338pwd39servicehttpdstop40history|tail-3#ls-ltr#servicehttpdstop#history|tail-635exportHISTCONTROL=erasedups36pwd37history|tail-338ls–ltr39servicehttpdstop40history|tail-6[Note:Thepreviousservicehttpdstopafterpwdgot

Page 234: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

erased]75

Page 235: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack59.ForcehistorynottorememberaparticularcommandusingHISTCONTROLWhenyouexecuteacommand,youcaninstructhistorytoignorethecommandbysettingHISTCONTROLtoignorespaceANDtypingaspaceinfrontofthecommandasshownbelow.Icanseelotofjuniorsysadminsgettingexcitedaboutthis,astheycanhideacommandfromthehistory.Itisgoodtounderstandhowignorespaceworks.But,asabestpractice,don’thidepurposefullyanythingfromhistory.# exportHISTCONTROL=ignorespace

Page 236: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#ls–ltr#pwd#servicehttpdstop[Note:Thereisaspaceatthebeginningofservice,toignorethiscommandfromhistory]#history|tail-367ls–ltr68pwd69history|tail-3Hack60.Clearalltheprevioushistoryusingoption-cSometimeyoumaywanttoclearalltheprevioushistory.Howeveryoumaystillwanttokeepthehistorymovingforward.

Page 237: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

# history-c76

Page 238: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack61.SubstitutewordsfromhistorycommandsWhenyouaresearchingthroughhistory,youmaywanttoexecuteadifferentcommandbutusethesameparameterfromthecommandthatyou’vejustsearched.Intheexamplebelow,the!!:$nexttothevicommandgetstheargumentfromthepreviouscommandtothecurrentcommand.#lsanaconda-ks.cfganaconda-ks.cfg#vi!!:$vianaconda-ks.cfg

Page 239: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Intheexamplebelow,the!^nexttothevicommandgetsthefirstargumentfromthepreviouscommand(i.ecpcommand)tothecurrentcommand(i.evicommand).#cpanaconda-ks.cfganaconda-ks.cfg.bakanaconda-ks.cfg#vi!^vianaconda-ks.cfgHack62.SubstituteaspecificargumentforaspecificcommandIntheexamplebelow,!cp:2searchesforthepreviouscommandinhistorythatstartswithcpandtakesthesecondargumentofcpandsubstitutesitforthels-lcommandasshownbelow.77

Page 240: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 241: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari#cp~/longname.txt/really/a/very/long/path/long-filename.txt#ls-l!cp:2ls-l/really/a/very/long/path/long-filename.txtIntheexamplebelow,!cp:$searchesforthepreviouscommandinhistorythatstartswithcpandtakesthelastargument(inthiscase,whichisalsothesecondargumentasshownabove)ofcpandsubstitutesitforthels-lcommandasshownbelow.#ls-l!cp:$ls-l/really/a/very/long/path/long-filename.txt

Page 242: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Hack63.DisabletheusageofhistoryusingHISTSIZEIfyouwanttodisablehistoryalltogetheranddon’twantbashshelltorememberthecommandsyou’vetyped,settheHISTSIZEto0asshownbelow.# exportHISTSIZE=0#history#[Note:Historydidnotdisplayanything]Hack64.IgnorespecificcommandsfromthehistoryusingHISTIGNORESometimesyoumaynotwanttoclutteryourhistorywithbasiccommandssuchaspwdandls.UseHISTIGNOREtospecifyallthecommandsthatyouwanttoignorefromthehistory.78

Page 243: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 244: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariPleasenotethataddinglstotheHISTIGNOREignoresonlylsandnotls-l.So,youhavetoprovidetheexactcommandthatyouwouldliketoignorefromthehistory.# exportHISTIGNORE=”pwd:ls:ls–ltr:”#pwd#ls#ls-ltr#servicehttpdstop#history|tail-379exportHISTIGNORE=”pwd:ls:ls-ltr:”

Page 245: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

80servicehttpdstop81history[Note:Historydidnotdisplaypwdandls]79

Page 246: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 247: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter9:SystemAdministrationTasksHack65.PartitionusingfdiskAfteryou’veinstalledbrandnewdisksonyourserver,youhavetousetoolslikefdisktopartitionitaccordingly.Followingarethe5typicalactions(commands)thatyoucanexecuteinsidefdisk.ooo

Page 248: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

oon–NewPartitioncreationd–Deleteanexistingpartitionp-PrintPartitionTablew–Writethechangestothepartitiontable.i.esave.q–QuitthefdiskutilityCreateapartitionInthefollowingexample,Icreateda/dev/sda1primarypartition.# fdisk/dev/sdaDevicecontainsneitheravalidDOSpartitiontable,norSun,SGIorOSFdisklabelBuildinganewDOSdisklabel.Changeswillremaininmemoryonly,

Page 249: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

untilyoudecidetowritethem.Afterthat,ofcourse,thepreviouscontentwon'tberecoverable.Thenumberofcylindersforthisdiskissetto34893.Thereisnothingwrongwiththat,butthisislargerthan1024,andcouldincertainsetupscauseproblemswith:80

Page 250: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari1)softwarethatrunsatboottime(e.g.,oldversionsofLILO)2)bootingandpartitioningsoftwarefromotherOSs(e.g.,DOSFDISK,OS/2FDISK)Warning:invalidflag0x0000ofpartitiontable4willbecorrectedbyw(rite)Command(mforhelp): pDisk/dev/sda:287.0GB,287005343744bytes255heads,63sectors/track,34893cylindersUnits=cylindersof16065*512=8225280bytes

Page 251: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

DeviceBootStartEndBlocksIdSystemCommand(mforhelp): nCommandactioneextendedpprimarypartition(1-4)pPartitionnumber(1-4): 1Firstcylinder(1-34893,default1):Usingdefaultvalue1

Page 252: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Lastcylinderor+sizeor+sizeMor+sizeK(1-34893,default34893):Usingdefaultvalue34893Command(mforhelp): wThepartitiontablehasbeenaltered!Callingioctl()tore-readpartitiontable.Syncingdisks.Verifythatthepartitiongotcreatedsuccessfully# fdisk/dev/sdaThenumberofcylindersforthisdiskissetto34893.Thereisnothingwrongwiththat,butthisislargerthan1024,andcouldincertainsetupscauseproblems

Page 253: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

with:81

Page 254: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari1)softwarethatrunsatboottime(e.g.,oldversionsofLILO)2)bootingandpartitioningsoftwarefromotherOSs(e.g.,DOSFDISK,OS/2FDISK)Command(mforhelp):pDisk/dev/sda:287.0GB,287005343744bytes255heads,63sectors/track,34893cylindersUnits=cylindersof16065*512=8225280bytesDeviceBoot/dev/sda1

Page 255: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Start1End34893Blocks280277991Id83SystemLinuxCommand(mforhelp): qHack66.Formatapartitionusingmke2fskAfterpartitioningthedisks,itisstillnotreadyforusage,asweneedto

Page 256: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

formatthedisk.Atthisstage,ifyoutrytoviewthediskinformation,itwillgivethefollowingerrormessageindicatingthatnovalidsuperblockispresent.# tune2fs-l/dev/sda1tune2fs1.35(28-Feb-2004)tune2fs:Badmagicnumberinsuper-blockwhiletryingtoopen/dev/sda1Couldn'tfindvalidfilesystemsuperblock.Toformatthedisk,usemke2fsasshownbelow.# mke2fs/dev/sda1Youcanalsopassthefollowingoptionalparametertothemke2fs.82

Page 257: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackoAjayKumarTiwari-m0:reserved-blocks-percentage–Thisindicatesthepercentageofthefilesystemblocksreservedfortherootuser.Defaultis5%.Inthefollowingexample,itissetto0.-b4096:block-sizespecifiedinbytes.Validvaluesare1024,2048and4096bytesperblock.o# mke2fs-m0-b4096/dev/sda1mke2fs1.35(28-Feb-2004)Filesystemlabel=

Page 258: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

OStype:LinuxBlocksize=4096(log=2)Fragmentsize=4096(log=2)205344inodes,70069497blocks0blocks(0.00%)reservedforthesuperuserFirstdatablock=0Maximumfilesystemblocks=713031682139blockgroups32768blockspergroup,32768fragmentspergroup96inodespergroupSuperblockbackupsstoredonblocks:32768,98304,163840,229376,294912,819200,884736,1605632,2654208,4096000,7962624,11239424,20480000,23887872

Page 259: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Writinginodetables:doneWritingsuperblocksandfilesystemaccountinginformation:doneThisfilesystemwillbeautomaticallycheckedevery32mountsor180days,whichevercomesfirst.Usetune2fs-cor-itooverride.Theabovecommandwillcreateanext2filesystem.Tocreateanext3filesystemdothefollowing:# mkfs.ext3/dev/sda1# mke2fs–j/dev/sda183

Page 260: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack67.MountthepartitionAftercreatingapartitionandformatting,youcanmountittoamountpoint.Firstcreateadirectorywherethepartitionshouldbemounted.#mkdir/home/databaseMountthefilesystem.# mount/dev/sda1/home/databaseToautomaticallymountthefilesystemafterthereboot,addthefollowingentrytothe/etc/fstab/dev/sdaa/home/databaseext3defaults02Hack68.Finetunethepartitionusingtune2fs

Page 261: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Usethetune2fs–l/dev/sda1toviewthefilesysteminformationasshownbelow.# tune2fs-l/dev/sda1tune2fs1.35(28-Feb-2004)Filesystemvolumename:/home/databaseLastmountedon:<notavailable>FilesystemUUID:f1234556-e123-1234-abcd-bbbbaaaaae11Filesystemmagicnumber:0xEF44Filesystemrevision#:1(dynamic)Filesystemfeatures:resize_inodefiletypesparse_superDefaultmountoptions:(none)Filesystemstate:notclean

Page 262: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

84

Page 263: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackErrorsbehavior:FilesystemOStype:Inodecount:Blockcount:Reservedblockcount:Freeblocks:Freeinodes:Firstblock:Blocksize:Fragmentsize:ReservedGDTblocks:

Page 264: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Blockspergroup:Fragmentspergroup:Inodespergroup:Inodeblockspergroup:Filesystemcreated:Lastmounttime:Lastwritetime:Mountcount:Maximummountcount:Lastchecked:Checkinterval:Nextcheckafter:Reservedblocksuid:Reservedblocksgid:

Page 265: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Firstinode:Inodesize:Defaultdirectoryhash:DirectoryHashSeed:ccccc123292bAjayKumarTiwariContinueLinux109491214013899401684848110149690

Page 266: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

2048204851216384163841288TueJul100:06:032008ThuAug2105:58:252008FriJan215:40:362009220TueJul100:06:03200815552000(6months)

Page 267: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SatDec2723:06:0320080(userroot)0(grouproot)11128tea12345829-1236-4123-9aaa-Youcanalsousethetune2fstotunetheex2/ext3filesystemparameter.Forexample,ifyouwanttochangetheFilesystemvolumename,youcandoitasshownbelow.# tune2fs-l/dev/sda1|grepvolumeFilesystemvolumename:/home/database# tune2fs-Ldatabase-home/dev/emcpowera1tune2fs1.35(28-Feb-2004)

Page 268: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

85

Page 269: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari# tune2fs-l/dev/sda1|grepvolumeFilesystemvolumename:database-homeHack69.Createaswapfilesystem.Createafileforswapusageasshownbelow.# ddif=/dev/zeroof=/home/swap-fsbs=1Mcount=512512+0recordsin512+0recordsout#ls-l/home/swap-fs-rw-r--r--1rootroot536870912Jan/home/swap-fs

Page 270: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

223:13UsemkswaptosetupaLinuxswapareainthe/home/swap-fsfilethatwascreatedabove.# mkswap/home/swap-fsSettingupswapspaceversion1,size=536866kBOncethefileiscreatedandhasbeensetupforLinuxswaparea,itistimetoenabletheswapusingswaponasshownbelow.# swapon/home/swap-fsAddthefollowinglineto/etc/fstabandrebootthesystemfortheswaptotakeintoeffect./home/swap-fsswapswapdefaults0086

Page 271: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 272: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack70.CreateanewuserAddanewuser–BasicmethodSpecifyonlytheusername.#useraddjsmithAddanewuserwithadditionalParameterYoucanalsospecifythefollowingparametertotheuseradd

Page 273: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

oo-c:Descriptionabouttheuser.-e:expirydateoftheuserinmm/dd/yyformat# adduser-c"JohnSmith-OracleDeveloper"-e12/31/09jsmithVerifythattheusergotaddedsuccessfully.#grepjsmith/etc/passwdjsmith:x:510:510:JohnSmith-OracleDeveloper:/home/jsmith:/bin/bashChangetheuserpassword.# passwdjsmithChangingpasswordforuserjsmith.

Page 274: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

NewUNIXpassword:BADPASSWORD:itisbasedonadictionarywordRetypenewUNIXpassword:passwd:allauthenticationtokensupdatedsuccessfully.87

Page 275: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariNote: Makesuretofollowthesebestpracticesfortheuser.Howtoidentifythedefaultvaluesusedbyuseradd?Followingarethedefaultvaluesthatwillbeusedwhenanuseriscreated.# useradd–DGROUP=100HOME=/homeINACTIVE=-1EXPIRE=

Page 276: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SHELL=/bin/bashSKEL=/etc/skelHack71.CreateanewgroupandassigntoanuserCreateanewdevelopergroup.#groupadddevelopersValidatethatthegroupwascreatedsuccessfully.#grepdeveloper/etc/groupdevelopers:x:511:Addanusertoanexistinggroup.Youcannotuseuseraddtomodifyanexistinguser,asyou’llgetthefollowingerrormessage.

Page 277: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

88

Page 278: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack#useradd-Gdevelopersjsmithuseradd:userjsmithexists#usermod-gdevelopersjsmithAjayKumarTiwariValidatetheusersgroupwasmodifiedsuccessfully.#grepjsmith/etc/passwdjsmith:x:510:511:OracleDeveloper:/home/jsmith:/bin/bash# idjsmithuid=510(jsmith)gid=511(developers)

Page 279: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

groups=511(developers)#grepjsmith/etc/groupjsmith:x:510:developers:x:511:jsmithHack72.SetupSSHpasswordlesslogininOpenSSHYoucanlogintoaremoteLinuxserverwithoutenteringpasswordin3simplestepsusingssky-keygenandssh-copy-idasexplainedinthisexample.ssh-keygencreatesthepublicandprivatekeys.ssh-copy-idcopiesthelocal-host’spublickeytotheremote-host’sauthorized_keysfile.ssh-copy-idalsoassignsproperpermissiontotheremote-host’shome,~/.ssh,and~/.ssh/authorized_keys.Step1:Createpublicandprivatekeysusingssh-key-genonlocal-

Page 280: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

hostjsmith@local-host$ ssh-keygenGeneratingpublic/privatersakeypair.89

Page 281: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariEnterfileinwhichtosavethekey(/home/jsmith/.ssh/id_rsa):[Enterkey]Enterpassphrase(emptyfornopassphrase):[Pressenterkey]Entersamepassphraseagain:[Pessenterkey]Youridentificationhasbeensavedin/home/jsmith/.ssh/id_rsa.Yourpublickeyhasbeensavedin/home/jsmith/.ssh/id_rsa.pub.Thekeyfingerprintis:

Page 282: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9jsmith@local-hostStep2:Copythepublickeytoremote-hostusingssh-copy-idjsmith@local-host$ ssh-copy-id-i~/.ssh/id_rsa.pubremote-hostjsmith@remote-host’spassword:Nowtryloggingintothemachine,with“ssh‘remote-host’”,andcheckin:.ssh/authorized_keystomakesurewehaven’taddedextrakeysthatyouweren’texpecting.Note: ssh-copy-idappendsthekeystotheremote-host’s.ssh/authorized_key.

Page 283: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Step3:Logintoremote-hostwithoutenteringthepasswordjsmith@local-host$ sshremote-hostLastlogin:SunNov1617:22:332008from192.168.1.2[Note:SSHdidnotaskforpassword.]jsmith@remote-host$[Note:Youareonremote-hosthere]90

Page 284: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack73.Usessh-copy-idalongwithssh-agentUsingssh-copy-idalongwiththessh-add/ssh-agentWhennovalueispassedfortheoption-iandIf~/.ssh/identity.pubisnotavailable,ssh-copy-idwilldisplaythefollowingerrormessage.jsmith@local-host$ssh-copy-id-iremote-host/usr/bin/ssh-copy-id:ERROR:NoidentitiesfoundIfyouhaveloadedkeystothessh-agentusingthessh-add,thenssh-copy-idwillgetthekeysfromthessh-agenttocopytotheremote-host.i.e,itcopies

Page 285: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

thekeysprovidedbyssh-add-Lcommandtotheremote-host,whenyoudon’tpassoption-itothessh-copy-id.jsmith@local-host$ssh-agent$SHELLjsmith@local-host$ssh-add-LTheagenthasnoidentities.jsmith@local-host$ssh-addIdentityadded:/home/jsmith/.ssh/id_rsa(/home/jsmith/.ssh/id_rsa)jsmith@local-host$ssh-add-Lssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79DaHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow==/home/jsmith/.ssh/id_rsajsmith@local-host$ssh-copy-id-iremote-host

Page 286: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

jsmith@remote-host’spassword:Nowtryloggingintothemachine,with“ssh‘remote-host’”,andcheckin:.ssh/authorized_keystomakesurewehaven’taddedextrakeysthatyouweren’texpecting.[Note:Thishasaddedthekeydisplayedbyssh-add-L]91

Page 287: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackThreeMinorAnnoyancesofssh-copy-idAjayKumarTiwariFollowingarefewminorannoyancesofthessh-copy-id.1.Defaultpublickey:ssh-copy-iduses~/.ssh/identity.pubasthedefaultpublickeyfile(i.ewhennovalueispassedtooption-i).Instead,Iwishitusesid_dsa.pub,orid_rsa.pub,oridentity.pubasdefaultkeys.i.eIfanyoneofthemexist,itshouldcopythattotheremote-host.Iftwoorthreeofthemexist,itshouldcopyidentity.pubasdefault.2.Theagenthasnoidentities:Whenthessh-agentisrunningand

Page 288: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

thessh-add-Lreturns“Theagenthasnoidentities”(i.enokeysareaddedtothessh-agent),thessh-copy-idwillstillcopythemessage“Theagenthasnoidentities”totheremote-host’sauthorized_keysentry.3.Duplicateentryinauthorized_keys:Iwishssh-copy-idvalidatesduplicateentryontheremote-host’sauthorized_keys.Ifyouexecutessh-copy-idmultipletimesonthelocal-host,itwillkeepappendingthesamekeyontheremote-host’sauthorized_keysfilewithoutcheckingforduplicates.Evenwithduplicateentrieseverythingworksasexpected.But,Iwouldliketohavemyauthorized_keysfileclutterfree.Hack74.CrontabUsingcronyoucanexecuteashell-scriptorLinuxcommandsataspecific

Page 289: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

timeanddate.Forexampleasysadmincanscheduleabackupjobthatcanruneveryday.Howtoaddajobtothecron?#crontab–e05***/root/bin/backup.sh92

Page 290: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariThiswillexecute/root/bin/backup.shat5a.meveryday.DescriptionofCronfields.Followingistheformatofthecrontabfile.{minute}{hour}{day-of-month}{month}{day-of-week}{full-path-to-shell-script}ooooominute:Allowedrange0–59

Page 291: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

hour:Allowedrange0–23day-of-month:Allowedrange0–31month:Allowedrange1–12.1=January.12=December.Day-of-week:Allowedrange0–7.Sundayiseither0or7.Crontabexamples1.Runat12:01a.m.1minuteaftermidnighteveryday.Thisisagoodtimetorunbackupwhenthesystemisnotunderload.10***/root/bin/backup.sh2.Runbackupeveryweekday(Mon–Fri)at11:59p.m.5911**1,2,3,4,5/root/bin/backup.shFollowingwillalsodothesame.5911**1-5/root/bin/backup.sh93

Page 292: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 293: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari3.Executethecommandevery5minutes.*/5****/root/bin/check-status.sh4.Executeat1:10p.mon1stofeverymonth10131**/root/bin/full-backup.sh5.Execute11p.monweekdays.023**1-5/root/bin/incremental-backup.shCrontabOptionsFollowingaretheavailableoptionswithcrontab:ocrontab–e:Editthecrontabfile.Thiswillcreateacrontab,ifit

Page 294: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

doesn’texistcrontab–l:Displaythecrontabfile.crontab-r:Removethecrontabfile.crontab-ir:Thiswillpromptuserbeforedeletingacrontab.oooHack75.SafeRebootOfLinuxUsingMagicSysRqKeyThemagicSysRqkeyisakeycombinationintheLinuxkernelwhichallowstheusertoperformvariouslowlevelcommandsregardlessofthesystem’sstate.Itisoftenusedtorecoverfromfreezes,ortorebootacomputerwithoutcorruptingthefilesystem.Thekeycombinationconsistsof

Page 295: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

94

Page 296: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariAlt+SysRq+commandkey.InmanysystemstheSysRqkeyistheprintscreenkey.First,youneedtoenabletheSysRqkey,asshownbelow.echo"1">/proc/sys/kernel/sysrqListofSysRqCommandKeysFollowingarethecommandkeysavailableforAlt+SysRq+commandkey.ooo

Page 297: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

‘k’–Killsalltheprocessrunningonthecurrentvirtualconsole.’s’–Thiswillattempttosyncallthemountedfilesystem.‘b’-Immediatelyrebootthesystem,withoutunmountingpartitionsorsyncing.‘e’–SendsSIGTERMtoallprocessexceptinit.‘m’–Outputcurrentmemoryinformationtotheconsole.‘i’-SendtheSIGKILLsignaltoallprocessesexceptinit‘r’-Switchthekeyboardfromrawmode(themodeusedbyprogramssuchasX11),toXLATEmode.’s’–syncallmountedfilesystem.‘t’-Outputalistofcurrenttasksandtheirinformationtotheconsole.‘u’-Remountallmountedfilesystemsinreadonlymode.‘o’–Shutdownthesystemimmediately.

Page 298: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

‘p’–Printthecurrentregistersandflagstotheconsole.‘0-9′-Setstheconsoleloglevel,controllingwhichkernelmessageswillbeprintedtoyourconsole.oooooooooo95

Page 299: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackooAjayKumarTiwari‘f’-Willcalloom_killtokillprocesswhichtakesmorememory.‘h’–Usedtodisplaythehelp.Butanyotherkeysthantheabovelistedwillprinthelp.Wecanalsodothisbyechoingthekeystothe/proc/sysrq-triggerfile.Forexample,tore-bootasystemyoucanperformthefollowing.echo"b">/proc/sysrq-triggerPerformaSaferebootofLinuxusingMagicSysRqKey

Page 300: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ToperformasaferebootofaLinuxcomputerwhichhangsup,dothefollowing.Thiswillavoidthefsckduringthenextre-booting.i.ePressAlt+SysRq+letterhighlightedbelow.oounRaw(takecontrolofkeyboardbackfromX11,tErminate(sendSIGTERMtoallprocesses,allowingthemtoterminategracefully),kIll (sendSIGILLtoallprocesses,forcingthemtoterminateimmediately),Sync (flushdatatodisk),Unmount (remountallfilesystemsread-only),reBoot.o

Page 301: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ooo96

Page 302: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter10:ApachectlandHttpdExamplesAfteryouhaveinstalledApache2,ifyouwanttouseapachectlandhttpdtoit’smaximumpotential,youshouldgobeyondusingstart,stopandrestart.The9practicalexamplesprovidedinthischapterwillhelpyoutouseapachectlandhttpdveryeffectively.ApachectlactsasSysVinitscript,takingargumentslikestart,stop,restartandstatus.Italsoactsasfront-endtohttpdcommand,bysimplypassingthe

Page 303: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

commandlineargumentstohttpd.So,allthecommandsyouexecuteusingapachectl,canalsobeexecuteddirectlybycallinghttpd.Ifyoudon’thaveApache,refertothetutorials:installLAMPstackusingyum.Hack76.Passdifferenthttpd.conffilenametoapachectlTypicallyyou’llmodifytheoriginalhttpd.conftotryoutdifferentApachedirectives.Ifsomethingdoesn’tworkout,you’llrevertbackthechanges.Insteadofplayingaroundwiththeoriginalhttpd.conf,copyittoanewhttpd.conf.debugandusethisnewhttpd.conf.debugfilewithApachefortestingpurposeasshownbelowusingoption-f.# apachectl-fconf/httpd.conf.debug# httpd-kstart-fconf/httpd.conf.debug

Page 304: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

[Note:youcanuseeitherapachectlorhttpdasshownabove]#ps-ef|grephttproot250801023:2600:00:00/usr/sbin/httpd-f97

Page 305: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariconf/httpd.conf.debugapache2509925080023:2800:00:00/usr/sbin/httpd-fconf/httpd.conf.debug[Note:psshowsthehttpdrunningwithhttpd.conf.debugfile]OnceyouaresatisfiedwiththechangesandApacherunswithoutanyproblemwithhttpd.conf.debug,youcancopythechangestohttpd.confandstarttheApachenormallyasshownbelow.#cphttpd.conf.debughttpd.conf#apachectlstop

Page 306: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

#apachectlstart#ps-ef|grephttpdroot251141023:2800:00:00/usr/sbin/httpd-kstartdaemon2511525114023:2800:00:00/usr/sbin/httpd-kstart[Note:psindicatesthatthehttpdisrunningusingthedefaultconfigfile]Hack77.UseatemporaryDocumentRootwithoutmodifyinghttpd.confThisisveryhelpful,whenyouaretryingoutdifferentlayoutforyourwebsiteanddon’twanttomodifytheoriginalfilesunderthedefaultDocumentRoot.TakeacopyofyouroriginalDocumentRootdirectory(/var/www/html)toa

Page 307: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

newtemporaryDocumentRootdirectory(/var/www/html_debug).MakeallyourchangesunderthistemporaryDocumentRootdirectory(/var/www/html_debug)andstarttheApachewiththistemporarydirectoryasshownbelowusingoption-c.98

Page 308: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari# httpd-kstart-c“DocumentRoot/var/www/html_debug/”IfyouwanttogobacktooriginalconfigurationusingthedefaultDocumentRoot(/var/www/html),simplyrestarttheApacheasshownbelow.#httpd-kstop#apachectlstartHack78.IncreasetheLogLeveltemporarilyWhileyouaredebugginganissue,youcanchangetheLogLeveloftheApachetemporarily,withoutmodifyingtheLogLeveldirectiveinthehttpd.confasshownbelowusingoption-e.Inthisexample,theLogLevelissettodebug.# httpd-kstart-edebug

Page 309: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

[SunAug1713:53:062008][debug]mod_so.c(246):loadedmoduleauth_basic_module[SunAug1713:53:062008][debug]mod_so.c(246):loadedmoduleauth_digest_modulePossiblevaluesyoucanpasstooption-eare:oooooooodebug

Page 310: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

infonoticewarnerrorcritalertemerg99

Page 311: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack79.DisplaythemodulesinsideApacheDisplaythemodulescompiledinsideApache# httpd-lCompiledinmodules:core.cprefork.chttp_core.cmod_so.c

Page 312: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

DisplaybothstaticanddynamicmoduleloadedbyApacheWhenyoupassoption-l,tohttpd,itwilldisplayonlythestaticmodules.Passingoption-M,willdisplaybothstaticandsharedmodulesasshownbelow.# httpd–MLoadedModules:core_module(static)mpm_prefork_module(static)http_module(static)so_module(static)auth_basic_module(shared)auth_digest_module(shared)

Page 313: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

authn_file_module(shared)authn_alias_module(shared)SyntaxOK100

Page 314: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack80.Showallaccepteddirectivesinsidehttpd.confThisislikeanextendedhelpforhttpd,whichwilldisplayallthehttpd.confdirectivesandtheplaceswheretheyarevalid.Foraspecificdirective,ittellsallthepossiblevaluesandwhereitcanbeusedinsidethehttpd.conf.Thiscanbeveryhelpful,whenyouwanttoquicklyknowaboutaparticularApachedirective.# httpd–LHostnameLookups(core.c)“on”toenable,“off”todisablereverseDNSlookups,

Page 315: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

or“double”toenabledouble-reverseDNSlookupsAllowedin*.confanywhereServerLimit(prefork.c)MaximumvalueofMaxClientsforthisrunofApacheAllowedin*.confonlyoutside<Directory>,<Files>or<Location>KeepAlive(http_core.c)WhetherpersistentconnectionsshouldbeOnorOffAllowedin*.confonlyoutside<Directory>,<Files>or<Location>LoadModule(mod_so.c)amodulenameandthenameofasharedobjectfiletoloaditfromAllowedin*.confonlyoutside<Directory>,<Files>or

Page 316: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

<Location>Hack81.Validatethehttpd.confaftermakingchangesUseoption-ttovalidatewhetherthereareanyissueswithaspecificApacheconfigurationfile.Intheexampleshownbelow,itdisplaysthatthereisa101

Page 317: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariproblematline148inthehttpd.conf.debug.mod_auth_basicsoismissinga.(period)beforetheso.# httpd-t-fconf/httpd.conf.debughttpd:Syntaxerroronline148of/etc/httpd/conf/httpd.conf.debug:Cannotload/etc/httpd/modules/mod_auth_basicsointoserver:/etc/httpd/modules/mod_auth_basicso:cannotopensharedobjectfile:NosuchfileordirectoryOnceyoufixtheissue,itwilldisplaySyntaxOK.

Page 318: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

# httpd-t-fconf/httpd.conf.debugSyntaxOKHack82.DisplaythehttpdbuildparametersUseoption-V(upper-caseV),todisplayApacheversionnumberandalltheparametersthatareusedwhilebuildingtheApache.# httpd–VServerversion:Apache/2.2.9(Unix)Serverbuilt:Jul14200815:36:56Server’sModuleMagicNumber:20051115:15Serverloaded:APR1.2.12,APR-Util1.2.12Compiledusing:APR1.2.12,APR-Util1.2.12Architecture:32-bitServerMPM:Prefork

Page 319: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

threaded:noforked:yes(variableprocesscount)Servercompiledwith….-DAPACHE_MPM_DIR=”server/mpm/prefork”-DAPR_HAS_SENDFILE-DAPR_HAS_MMAP102

Page 320: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack-D-D-D-D-D-D-D-D-D-D-D

Page 321: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

-D-D-D-DAjayKumarTiwariAPR_HAVE_IPV6(IPv4-mappedaddressesenabled)APR_USE_SYSVSEM_SERIALIZEAPR_USE_PTHREAD_SERIALIZESINGLE_LISTEN_UNSERIALIZED_ACCEPTAPR_HAS_OTHER_CHILDAP_HAVE_RELIABLE_PIPED_LOGSDYNAMIC_MODULE_LIMIT=128HTTPD_ROOT=”/etc/httpd”SUEXEC_BIN=”/usr/sbin/suexec”

Page 322: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

DEFAULT_PIDLOG=”logs/httpd.pid”DEFAULT_SCOREBOARD=”logs/apache_runtime_status”DEFAULT_LOCKFILE=”logs/accept.lock”DEFAULT_ERRORLOG=”logs/error_log”AP_TYPES_CONFIG_FILE=”conf/mime.types”SERVER_CONFIG_FILE=”conf/httpd.conf”IfyouwantdisplayonlytheApacheversionnumber,usetheoption-v(lower-casev)asshownbelow.# httpd-vServerversion:Apache/2.2.9(Unix)Serverbuilt:Jul14200815:36:56Hack83.LoadaspecificmoduleonlyondemandSometimesyoumaynotwanttoloadallthemodulesintheApache.Fore.g.

Page 323: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

YoumaywanttoloadldaprelatedmodulestoApache,onlywhenyouaretestingLDAP.Thiscanbeachievedasshownbelow.Modifythehttpd.confandaddIfDefinedirectivecalledload-ldap(youcannamethisanythingyouwant).<IfDefineload-ldap>LoadModuleldap_modulemodules/mod_ldap.soLoadModuleauthnz_ldap_module103

Page 324: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackmodules/mod_authnz_ldap.so</IfDefine>AjayKumarTiwariWhenyouaretestingldapandwouldliketoLoadtheldaprelatedmodules,passtheload-ldaptoOption-D,asshownbelow:#httpd-kstart-edebug-Dload-ldap-f/etc/httpd/conf/httpd.conf.debug[SunAug1714:14:582008][debug]mod_so.c(246):loadedmoduleldap_module[SunAug1714:14:582008][debug]mod_so.c(246):loadedmoduleauthnz_ldap_module

Page 325: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

[Note:Pass-Dload-ldap,toloadtheldapmodulesintoApache]#apachectlstart[Note:StarttheApachenormally,ifyoudon'twanttoloadtheldapmodules.104

Page 326: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 327: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter11:BashScriptingHack84.ExecutionSequenceof.bash_*filesWhatisthesequenceinwhichthefollowingfilesareexecuted?oooooo

Page 328: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

/etc/profile~/.bash_profile~/.bashrc~/.bash_login~/.profile~/.bash_logoutExecutionsequenceforinteractiveloginshellFollowingpseudocodeexplainsthesequenceofexecutionofthesefiles.execute/etc/profileIF~/.bash_profileexistsTHENexecute~/.bash_profileELSE

Page 329: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

IF~/.bash_loginexistTHENexecute~/.bash_loginELSEIF~/.profileexistTHENexecute~/.profileENDIFENDIFENDIF105

Page 330: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariWhenyoulogoutoftheinteractiveshell,followingisthesequenceofexecution:IF~/.bash_logoutexistsTHENexecute~/.bash_logoutENDIFPleasenotethat/etc/bashrcisexecutedby~/.bashrcasshownbelow:#cat~/.bashrcif[-f/etc/bashrc];then./etc/bashrcFi

Page 331: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Executionsequenceforinteractivenon-loginshellWhilelaunchinganon-logininteractiveshell,followingisthesequenceofexecution:IF~/.bashrcexistsTHENexecute~/.bashrcENDIFNote: Whenanon-interactiveshellstartsup,itlooksforENVenvironmentvariable,andexecutesthefile-namevaluementionedintheENVvariable.Hack85.HowtogeneraterandomnumberinbashshellUsethe$RANDOMbashbuilt-infunctiontogeneraterandomnumberbetween

Page 332: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

0–32767asshownbelow.$echo$RANDOM106

Page 333: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack22543$echo$RANDOM25387$echo$RANDOM647AjayKumarTiwariHack86.DebugashellscriptTodebugashellscriptuseset–xvinsidetheshellscriptatthetop.Shellscriptwithnodebugcommand:

Page 334: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

$catfilesize.sh#!/bin/bashforfilesizein$(ls-l.|grep"^-"|awk'{print$5}')dolettotalsize=$totalsize+$filesizedoneecho"Totalfilesizeincurrentdirectory:$totalsize"OutputofShellscriptwithnodebugcommand:$./filesize.shTotalfilesizeincurrentdirectory:652ShellscriptwithDebugcommand

Page 335: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

inside:Addset–xvinsidetheshellscriptnowtodebugtheoutputasshownbelow.$catfilesize.sh#!/bin/bashset-xvforfilesizein$(ls-l.|grep"^-"|awk'{print107

Page 336: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari$5}')dolettotalsize=$totalsize+$filesizedoneecho"Totalfilesizeincurrentdirectory:$totalsize"OutputofShellscriptwithDebugcommandinside:$./fs.sh++ls-l.++grep'^-'

Page 337: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

++awk'{print$5}'+forfilesizein'$(ls-l'\''{print$5}'\'')'+lettotalsize=+178+forfilesizein'$(ls-l'\''{print$5}'\'')'+lettotalsize=178+285+forfilesizein'$(ls-l'\''{print$5}'\'')'+lettotalsize=463+189+echo'TotalfilesizeinTotalfilesizeincurrent.|grep"^-"|awk.|grep"^-"|awk

Page 338: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

.|grep"^-"|awkcurrentdirectory:652'directory:652ExecuteShellscriptwithdebugoption:Insteadofgivingtheset–xvinsidetheshellscript,youcanalsoprovidethatwhileexecutingtheshellscriptasshownbelow.$bash-xvfilesize.shHack87.Quotingechostatementwithoutanyspecialcharacter.$echoTheGeekStuff108

Page 339: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackTheGeekStuffAjayKumarTiwariEchostatementwithaspecialcharacter;.semi-colonisacommandterminatorinbash.Inthefollowingexample,“TheGeek”worksfortheechoand“Stuff”istreatedasaseparateLinuxcommandandgivescommandnotfound.$echoTheGeek;StuffTheGeek-bash:Stuff:commandnotfoundToavoidthisyoucanadda\infrontofsemi-colon,whichwillremovethespecialmeaningofsemi-colonandjustprintitasshownbelow.

Page 340: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

$echoTheGeek\;StuffTheGeek;StuffSingleQuoteUsesinglequotewhenyouwanttoliterallyprinteverythinginsidethesinglequote.Eventhespecialvariablessuchas$HOSTNAMEwillbeprintas$HOSTNAMEinsteadofprintingthenameoftheLinuxhost.$echo'Hostname=$HOSTNAME;Message=\$isUSD'Hostname=$HOSTNAME;Message=\$isUSDCurrentUser=`whoami`;CurrentUser=`whoami`;DoubleQuote

Page 341: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Usedoublequoteswhenyouwanttodisplaytherealmeaningofspecialvariables.$echo"Hostname=$HOSTNAME;Message=\$isUSD"CurrentUser=`whoami`;109

Page 342: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHostname=dev-db;CurrentUser=ramesh;Message=$isUSDDoublequoteswillremovethespecialmeaningofallcharactersexceptthefollowing:oooooo

Page 343: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

$ParameterSubstitution.`Backquotes\$LiteralDollarSign.\´LiteralBackquote.\"EmbeddedDoublequote.\\EmbeddedBackslashes.Hack88.ReaddatafilefieldsinsideashellscriptThisexampleshowshowtoreadaparticularfieldfromadata-fileandmanipulateitinsideashell-script.Forexample,letusassumetheemployees.txtfileisintheformatof{employee-name}:{employee-id}:{department-name},withcolondelimitedfileasshownbelow.$catemployees.txtEmmaThomas:100:Marketing

Page 344: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

AlexJason:200:SalesMadisonRandy:300:ProductDevelopmentSanjayGupta:400:SupportNishaSingh:500:SalesThefollowingshellscriptexplainshowtoreadspecificfieldsfromthisemployee.txtfile.$viread-employees.sh#!/bin/bashIFS=:echo"EmployeeNames:"echo"---------------"whilereadnameempiddept110

Page 345: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwaridoecho"$nameispartof$deptdepartment"done<~/employees.txtAssignexecuteprivilegetotheshellscriptandexecuteit.$chmodu+xread-employees.sh$./read-employees.shEmployeeNames:---------------EmmaThomasispartofMarketingdepartmentAlexJasonispartofSalesdepartment

Page 346: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

MadisonRandyispartofProductDevelopmentdepartmentSanjayGuptaispartofSupportdepartmentNishaSinghispartofSalesdepartment111

Page 347: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 348: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari

Chapter12:SystemMonitoringandPerformanceHack89.Freecommandfreecommanddisplaysallthenecessaryinformationaboutsystemphysical(RAM)andswapmemory.Syntax:free[options]WhatisthetotalRAMonmy

Page 349: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

system?Intheexamplebelow,thetotalphysicalmemoryonthissystemis1GB.ThevaluesdisplayedbelowareinKB.#freetotalusedMem:10346241006696-/+buffers/cache:Swap:2031608freesharedbuffers27928017413621666881795602031608cached

Page 350: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

615892WhatisthetotalmemoryonmysystemincludingRAMandSwap?Inthefollowingcommand:oooptionmdisplaysthevaluesinMBoptiontdisplaysthe“Total”line,whichissumofphysicalandswapmemoryvaluesoptionoistohidethebuffers/cachelinefromtheaboveexample.o# free–mto112

Page 351: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHacktotal101019832994used9830983free271983

Page 352: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

2011shared0AjayKumarTiwaribuffers170cached601Mem:Swap:Total:Hack90.TopCommandtopcommanddisplaysrealtimeinformationaboutvariousperformance

Page 353: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

metricsofthesystemsuchasCPULoad,MemoryUsage,Processeslistetc.Syntax:top[options]HowtoviewmycurrentsystemstatusincludingCPUusage?Executetopwithoutanyoptionfromthecommandline,whichwilldisplaytheoutputshownbelow.Thetopcommandoutputwillkeepdisplayingthereal-timevalues,untilyoupress“Control+c”orqtoexitfromthecommandoutput.# toptop-13:10:13up171days,20:21,3users,loadaverage:0.01,0.05,0.00Tasks:194total,1running,193sleeping,0stopped,0zombieCpu(s):0.6%us,0.7%sy,0.0%ni,98.7%id,0.0%wa,0.0%hi,0.0%siMem:1034624ktotal,1007420kused,27204kfree,174540kbuffers

Page 354: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Swap:2031608ktotal,0kused,2031608kfree,615904kcachedPIDUSERPRNIVIRTRES11912apache1503182819299oracle190279m11398jsmith160107mSHRS%CPU%MEMTIME+COMMAND13m3916S10.20:46.35httpd18m17mS10.20:00.03oracle28m6404S00.40:03.07perl113

Page 355: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHowtoreadtheoutputofthetopcommandshownabove?oLine1“top”,indicatesthatthesystemhasbeenupandrunningfor171days.Line2“Tasks”,displaysthetotalnumberofprocessesalongwithabreakdownofrunning,sleeping,stoppedandzombieprocessescount.Line3“Cpu(s)”displaysthecurrentCPUutilizationofthesystem.Inthisexample,CPUis98.7%idle

Page 356: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Line4“Mem”andline5“Swap”providesthememoryinformation.Thisisthesameinformationfromthefreecommand.Therestofthelinesdisplayalltheactiveprocessesonthesystem,sorteddefaultbyCPUusage(%CPUcolumn).i.ethemostCPUintensiveprocesseswillbedisplayedonthetopbydefault.ooooThereareseveralcommandlineoptionsandinteractiveoptionsavailablefortopcommands.Letusreviewcoupleofessentialoptionsfortopcommand.Howtoidentifythemostmemoryintensive

Page 357: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

processes?Whiletheoutputofthetopcommanddisplayed,pressF,whichwilldisplaythefollowingmessageandshowallfieldsavailableforsorting,pressn(whichisforsortingtheprocessesbyMemory)andpressenter.Thiswilldisplaytheprocessesinthetopoutputsortedbymemoryusage.CurrentSortField:Kforwindow1:DefSelectsortfieldviafieldletter,typeanyotherkeytoreturnHowtoaddadditionalfields(fore.g.CPUTime)tothetopoutput?114

Page 358: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariWhilethetopcommandisrunning,pressf,whichwilldisplaythefollowingmessageandshowallfieldsavailablefordisplay,pressl,whichwilladdtheCPUTimetothedisplaycolumnsinthetopoutput.CurrentFields:AEHIOQTWKNMbcdfgjplrsuvyzXforwindow1:DefTogglefieldsviafieldletter,typeanyotherkeytoreturnHowtogetthefullpathnameandparametersoftherunningprocesses?

Page 359: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Whilethetopcommandisrunning,pressc,whichwilldisplayfullpathnameofrunningprocessesasshownbelowinthecommandcolumn.i.eInsteadofhttpd,itdisplays/usr/local/apache2/bin/httpd.PIDUSERPRNIVIRTRESSHRS%CPU%MEM1TIME+COMMAND0.20:46.3511912apache1503182813m3916S/usr/local/apache2/bin/httpdHowtoviewtheindividualCPUsinthetopcommand?Whilethetopcommandisrunning,press1(numberone),whichwilldisplay

Page 360: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

theperformancedataoftheindividualCPUsonthatmachineasshownbelow.top-13:10:13up171days,20:21,3users,loadaverage:0.01,0.05,0.00Tasks:194total,1running,193sleeping,0stopped,0zombieCpu0:10.2%us,2.6%sy,0.0%ni,86.8%id,0.3%wa,0.0%hi,0.0%siCpu1:9.6%us,8.0%sy,0.0%ni,82.4%id,0.0%wa,0.0%hi,0.0%siCpu2:1.3%us,1.3%sy,0.0%ni,95.0%id,2.3%wa,0.0%hi,0.0%siCpu3:0.0%us,0.0%sy,0.0%ni,100.0%id,0.0%wa,0.0%hi,0.0%si115

Page 361: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariMem:1034624ktotal,1007420kused,27204kfree,174540kbuffersSwap:2031608ktotal,0kused,2031608kfree,615904kcachedHack91.PsCommandpscommand(processstatus)willdisplaysnapshotinformationofallactiveprocesses.Syntax:ps[options]Howtodisplayalltheprocessesrunninginthesystem?Use"psaux",asshownbelow.

Page 362: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

# psaux|moreUSERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMANDroot10.00.02044588?SsJun270:00init[5]apache311860.01.62373617556?SJul260:40/usr/local/apache2/bin/httpdapache311870.01.32064014444?SJul260:37/usr/local/apache2/bin/httpdYoucanalsouse"ps-ef|more",togetasimilaroutputPrinttheProcessTreeYoucanuseeither psaxuf or ps–ejThetreestructurewillhelptovisualizetheprocessandit’sparentprocess

Page 363: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

immediately.Forclaritypurpose,fewcolumnshavebeencut-offintheoutputbelow.# psaxuf116

Page 364: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwarirootrootrootrootrootrootrootrootroot511511Oct14

Page 365: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Oct14Oct14Oct14Oct14Oct14Dec03Dec2223:3523:350:00/opt/VRTSralus/bin/beremote0:00\_/opt/VRTSralus/bin/beremote0:00\_/opt/VRTSralus/bin/beremote0:00\_/opt/VRTSralus/bin/beremote0:01\_/opt/VRTSralus/bin/beremote0:00\_/opt/VRTSralus/bin/beremote0:01/usr/local/sbin/sshd

Page 366: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

1:08/usr/local/sbin/sshd0:00\_/usr/local/sbin/sshd0:00\_-bash\_psaxufNote:Youcanalsousepstreecommandtodisplayprocessintreestructure.ViewProcessesOwnedbyaParticularUserThefollowingcommanddisplaysalltheprocessownedbyLinuxuser-name:oracle.$ psUoraclePIDTTY5014?7124?

Page 367: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

8206?8852?8854?8911?STATSsSsSsSsSsSsTIMECOMMAND0:01/oracle/bin/tnslsnr0:00ora_q002_med

Page 368: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

0:00ora_cjq0_med0:01ora_pmon_med0:00ora_psp0_med0:02oraclemed(LOCAL=NO)ViewProcessesOwnedbyCurrentUserFollowingcommanddisplaysalltheprocessownedbythecurrentuser.$ psU$USERPIDTTY10329?10330pts/1STATS

Page 369: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SsTIMECOMMAND0:00sshd:ramesh@pts/1,pts/20:00-bash117

Page 370: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack10354pts/210530pts/1Ss+R+0:00-bash0:00psUrameshAjayKumarTiwariHack92.DfCommanddfcommand(diskfree)displaystheamountoftotalandfreediskspaceavailableonthemountedfilesystems.Syntax:df[options][name]

Page 371: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

HowmuchGBofdiskspaceisfreeonmysystem?Usedf-hasshownbelow.Option-hdisplaysthevaluesinhumanreadableformat(forexample:KforKb,MforMbandGforGb).Inthesampleoutputbelow,/filesystemhas17GBofdiskspaceavailableand/home/userfilesystemhas70GBavailable.# df–hFilesystem/dev/sda1/dev/sdb1Size64G137G

Page 372: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

UsedAvailUse%Mountedon44G17G73%/67G70G49%/home/userWhattypeoffilesystemdoIhaveonmysystem?Option-TwilldisplaytheinformationaboutthefilesystemType.Inthisexample/and/home/userfilesystemsareext2.Option-awilldisplayallthefilesystems,includingthe0sizespecialfilesystemusedbythesystem.# df-ThaFilesystemTypeSizeUsedAvailUse%Mountedon118

Page 373: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari/dev/sda1ext264G44G17G73%//dev/sdb1ext2137G67G70G49%/home/usernoneproc000-/procnonesysfs000-/sysnonedevpts000-/dev/ptsnonetmpfs2.0G02.0G0%/dev/shmHack93.KillCommandkillcommandcanbeusedtoterminatearunningprocess.Typicallythiscommandisusedtokillprocessesthatarehangingandnotresponding.Syntax:kill[options][pids|commands]

Page 374: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Howtokillahangingprocess?First,identifytheprocessidoftheparticularprocessthatyouwouldliketokillusingthepscommand.Onceyouknowtheprocessid,passitasaparametertothekillcommand.Theexamplebelowshowshowtokillthehangingapachehttpdprocess.Pleasenotethattypicallyyoushoulduse“apachectlstop”tostopapache.# psaux|grephttpdUSERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMANDapache311860.01.62373617556?SJul260:40/usr/local/apache2/bin/httpdapache311870.01.32064014444?SJul260:37/usr/local/apache2/bin/httpd

Page 375: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

# kill3118631187119

Page 376: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariPleasenotethattheabovecommandtriestoterminatetheprocessgraciouslybysendingasignalcalledSIGTERM.Iftheprocessdoesnotgetterminated,youcanforcefullyterminatetheprocessbypassingasignalcalledSIGKILL,usingtheoption-9asshownbelow.Youshouldeitherbetheowneroftheprocessoraprivilegedusertokillaprocess.# kill-93118631187Anotherwaytokillmultipleprocesseseasilyisbyaddingthefollowingtwofunctionstothe.bash_profile.functionpsgrep(){psaux|grep"$1"|grep-v'grep'

Page 377: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

}functionpsterm(){[${#}-eq0]&&echo"usage:$FUNCNAMESTRING"&&return0localpidpid=$(psax|grep"$1"|grep-vgrep|awk'{print$1}')echo-e"terminating'$1'/process(es):\n$pid"kill-SIGTERM$pid}Nowdothefollowing,toidentifyandkillallhttpdprocesses.# psgrephttpUSERCOMMANDapacheJul26apache

Page 378: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Jul26PID%CPU%MEM311860:40311870:37VSZRSSTTYSTATSTARTTIMESS0.01.62373617556?/usr/local/apache2/bin/httpd0.01.32064014444?/usr/local/apache2/bin/httpd

Page 379: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

# pstermhttpdterminating'httpd'/process(es):31186120

Page 380: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack31187AjayKumarTiwariHack94.DuCommandducommand(diskusage)willprintthefilespaceusageforaparticulardirectoryanditssubdirectories.Howmuchspaceistakenbymyhomedirectoryandallitssubdirectories?Inthefollowingexample,option-sstandsforsummaryonly.i.eitdisplaysonlythetotalsizeof/home/jsmithandnottheindividualsizesofallthe

Page 381: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

subdirectoriesinsidethe/home/jsmith.Option-hdisplaystheinformationinahumanreadableformat.i.eKforKB,MforMBandGforGB.The~indicatestheuserhomedirectory.Thiscommandissameas“du-sh/home/jsmith”# du-sh~320M/home/jsmithTogetthesubdirectoriesunder/home/jsmithlisted,executetheabovecommandwithoutthesoption.Hack95.lsofcommands.Lsofstandsforlsopenfiles,whichwilllistalltheopenfilesinthesystem.Theopenfilesincludenetworkconnection,devicesanddirectories.Theoutputofthelsofcommandwillhavethefollowingcolumns:o

Page 382: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

ooCOMMANDprocessname.PIDprocessIDUSERUsername121

Page 383: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackooooooFDfiledescriptorTYPEnodetypeofthefileDEVICEdevicenumberSIZEfilesizeNODEnodenumber

Page 384: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

NAMEfullpathofthefilename.AjayKumarTiwariViewallopenfilesofthesystemExecutethelsofcommandwithoutanyparameterasshownbelow.# lsof|moreCOMMANDPIDUSERFDTYPEDEVICESIZENODENAMEinit1rootcwdDIR8,140962/init1rootrtdDIR8,140962/init1roottxtREG8,132684983101/sbin/initinit1rootmemREG8,1106397166798/lib/ld-2.3.4.soinit1rootmemREG8,11454802166799

Page 385: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

/lib/tls/libc-2.3.4.soinit1rootmemREG8,153736163964/lib/libsepol.so.1init1rootmemREG8,156328166811/lib/libselinux.so.1init1root10uFIFO0,13972/dev/initctlmigration2rootcwdDIR8,140962/skipped…Thelsofcommandbyitselfwithoutmayreturnlotofrecordsasoutput,whichmaynotbeverymeaningfulexcepttogiveyouaroughideaabouthow122

Page 386: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwarimanyfilesareopeninthesystematanygivenpointofviewasshownbelow.# lsof|wc-l3093ViewopenfilesbyaspecificuserUselsof–uoptiontodisplayallthefilesopenedbyaspecificuser.# lsof–urameshvi7190ramesh475196/bin/vitxtREG

Page 387: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

8,1474608sshd7163ramesh3uIPv615088263TCPdev-db:ssh->abc-12-12-12-12.socal.res.rr.com:2631(ESTABLISHED)Asystemadministratorcanusethiscommandtogetsomeideaonwhatusersareexecutingonthesystem.ListUsersofaparticularfileIfyouliketoviewalltheuserswhoareusingaparticularfile,uselsofasshownbelow.Inthisexample,itdisplaysalluserswhoarecurrentlyusingvi.#lsof/bin/viCOMMANDPIDvi7258vi7300

Page 388: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

USERFDroottxtrameshtxtTYPEDEVICESIZENODENAMEREG8,1474608475196/bin/viREG8,1474608475196/bin/vi123

Page 389: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack96.SarCommandSarcommandscomeswiththesysstatpackage.Makesuresysstatisinstalled.Ifyoudon’thavesarinstalledonyoursystem,getitfromSarisanexcellentmonitoringtoolthatdisplaysperformancedataofprettymucheveryresourceofthesystemincludingCPU,memory,IO,paging,networking,interruptsetc.,SarCollects,Reports(displays)andSavestheperformancedata.LetuslookatallthethreeaspectsseparatelySadc-Systemactivitydata

Page 390: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

collector/usr/lib/sadc(Systemactivitydatacollector)commandcollectsthesystemdataataspecifiedtimeinterval.Thisusesthedailyactivitydatafilethatislocatedunder/va/log/sa/sa[dd],whereddisthecurrentday.Sa1shell-script/usr/lib/sa1in-turncallsthe/usr/lib/sadcs.sa1isinvokedfromthecrontabasshownbelow.Runthisevery5minutesor15minutesdependingonyourneed.Iprefertoscheduleitforevery5minutesinthecrontabasshownbelow.*/5****root/usr/lib/sa/sa111Sa2shell-script/usr/lib/sa2isashellscriptthatwillwriteadailyreportinthe/var/log/sa/sa[dd]file,whereddisthecurrentday.Invokethesa2fromthe

Page 391: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

crontabonceadayatmidnight.#5923***root/usr/lib/sa/sa2–A124

Page 392: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariNote: /etc/cron.d/sysstatfilescomeswiththesysstatpackagethatincludessomedefaultvalueforthesa1andsa2,whichyoucanchangeaccordingly.DisplayCPUStatisticsusingSarCommand# sar–uLinux2.6.9-42.ELsmp(dev-db)12:00:01AMCPU%user%nice12:05:01AMall3.700.0012:10:01AMall4.590.0012:15:01AMall3.900.00

Page 393: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

12:20:01AMall4.060.0012:25:01AMall3.890.0012:30:01AMall3.890.00Skipped..Average:all4.560.001.000.1594.2901/01/2009%system%iowait0.850.001.190.060.950.041.000.01

Page 394: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

0.870.000.870.00%idle95.4594.1695.1194.9395.2395.23Note: IfyouneedabreakdownoftheperformancedatafortheindividualCPU’s,executethefollowingcommand.#sar-u-PALLDisplayDiskIOStatisticsusingsarcommand

Page 395: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

# sar–dLinux2.6.9-42.ELsmp(dev-db)12:00:01AMDEV12:05:01AMdev2-012:10:01AMdev8-1Skipped..Average:Average:dev2-0dev8-14.661.89120.773.1769.458.02

Page 396: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

01/01/2009tpsrd_sec/s1.651.284.088.11wr_sec/s45.4321.81125

Page 397: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariDisplaynetworkingStatisticsusingsarcommand# sar-nDEV|moreLinux2.6.9-42.ELsmp(dev-db)12:00:01AMIFACErxpck/srxcmp/stxcmp/srxmcst/s12:05:01AMlo0.170.000.000.0012:10:01AMeth052.92

Page 398: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

0.000.000.0001/01/2009txpck/srxbyt/stxbyt/s0.1625.3123.3353.6410169.7412178.57# sar-nSOCK|moreLinux2.6.9-42.ELsmp(dev-db)12:00:01AMtotscktcpsck12:05:01AM501312:10:01AM5013

Page 399: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

12:15:01AM531301/01/2009udpsckrawsck304050ip-frag000

Hack97.vmstatCommandForatypicalperformancemonitoringallyouneedisonlyvmstatcommand.Thisdisplaymemory,swap,IO,systemandcpuperformanceinformation.Thefollowingcommandexecutesvmstatevery1secondfor100times.# vmstat1100

Page 400: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

procs-----------memory-------------swap-------io------system------cpu----rbswpdfreebuffcachesiso0000028212013410857970120282120134108579701212600bi00boin0

Page 401: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

020csussyidwa000100001007359001000

Page 402: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack00000282120134108579701202821201341085797012000000AjayKumarTiwari

Page 403: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

0111757700100001007366001000VmstatprocsSectionoorfield:Totalnumberofrunnableprocessbfield:TotalnumberofblockedprocessMemorysectionooooSwpdfield:Usedswapspace

Page 404: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Freefield:AvailablefreeRAMBufffield:RAMusedforbuffersCachefield:RAMusedforfilesystemcacheSwapSectionooSifield:AmountofmemoryswappedfromdiskpersecondSofield:AmountofmemoryswappedtodiskpersecondIOSectionooBifield:BlocksreceivedfromdiskBofield:Blockssenttodisk.

Page 405: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SystemSectionooInfield:Numberofinterruptspersecond.Csfield:Numberofcontextswitchespersecond.127

Page 406: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariCPUSectionooooUsfield:Timespendrunningusercode.(non-kernelcode)Syfield:Timespentrunningkernelcode.Idfield:Idletime.Wafield:TimespentwaitingfortheIOHack98.NetstatCommand

Page 407: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Netstatcommanddisplaysthenetworkrelatedinformationsuchasnetworkconnections,routingtables,interfacestatistics.Followingarefewexamplesonhowtousenetstatcommand.DisplayActiveInternetConnectionsanddomainsocketsusingnetstat# netstat–anActiveInternetconnections(serversandestablished)ProtoRecv-QSend-QLocalAddressForeignAddressStatetcp000.0.0.0:56660.0.0.0:*LISTENtcp000.0.0.0:1110.0.0.0:*LISTEN

Page 408: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

tcp000.0.0.0:40860.0.0.0:*LISTENskipped..ActiveUNIXdomainsockets(serversandestablished)ProtoRefCntFlagsTypeStateI-NodePathunix2[ACC]STREAMLISTENING7894/tmp/.font-unix/fs7100unix2[ACC]STREAMLISTENING9662/tmp/.gdm_socketunix2[ACC]STREAMLISTENING10897@/tmp/fam-root-128

Page 409: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariDisplayActiveConnectionswithProcessIDandProgramNameThiscouldbeveryhelpfultoidentifywhichprogramhasinitiatedaspecificnetworkconnection.# netstat-tapActiveInternetconnections(serversandestablished)ProtoRecv-QSend-QLocalAddressForeignAddressStatePID/Programnametcp00*:nrpe*:*LISTEN16277/xinetd

Page 410: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

tcp00localhost.localdomain:smtp*:*LISTEN7263/sendmail:accetcp340localhost.localdomain:54221localhost.localdomain:4089CLOSE_WAIT29881/httpdtcp03216dev-db:sshcpe-76-94-215-154.soca:4682ESTABLISHED11717/sshd:rameshDisplayRoutingTable# netstat–-routeKernelIProutingtableDestinationGatewayWindowirttIface192.168.1.0*0eth0162.244.0.0*

Page 411: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

0eth0default192.168.1.10eth0Genmask255.255.255.0255.255.0.00.0.0.0UFlagsMSS00UUG0000

Page 412: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

DisplayRAWnetworkstatistics# netstat--statistics--rawIp:129

Page 413: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari11080343totalpacketsreceived0forwarded1withunknownprotocol0incomingpacketsdiscarded11037744incomingpacketsdelivered11199763requestssentoutIcmp:577135ICMPmessagesreceived64inputICMPmessagefailed.ICMPinputhistogram:

Page 414: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

destinationunreachable:537timeoutintransit:65sourcequenches:2echorequests:576476echoreplies:12timestamprequest:3addressmaskrequest:3581558ICMPmessagessent0ICMPmessagesfailedICMPoutputhistogram:destinationunreachable:5079echoreplies:576476timestampreplies:3

Page 415: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

MiscNetstatCommandso#netstat–-tcp–-numeric ListofTCPconnectiontoandfromthemachine.#netstat--tcp--listening–-programserverislisteningonalongwiththeprogramthatislisteningonthatparticularport.#netstat–rnC DisplaytheroutingcacheooHack99.SysctlCommandLinuxkernelparametercanbechangedontheflyusingsysctlcommand.130

Page 416: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariSysctlhelpstoconfiguretheLinuxkernelparametersduringruntime.# sysctl–adev.cdrom.autoclose=1fs.quota.writes=0kernel.ctrl-alt-del=0kernel.domainname=(none)kernel.exec-shield=1net.core.somaxconn=128net.ipv4.tcp_window_scaling=1net.ipv4.tcp_wmem=409616384

Page 417: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

net.ipv6.route.mtu_expires=600sunrpc.udp_slot_table_entries=16vm.block_dump=0131072ModifyKernelparameterin/etc/sysctl.confforpermanentchangeAftermodifyingthekernelparameterinthe/etc/sysctl.conf,executesysctl–ptocommitthechanges.Thechangeswillstillbethereafterthereboot.#vi/etc/sysctl.conf# sysctl–pModifykernelparametertemporarily

Page 418: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Totemporarilymodifyakernelparameter,executethefollowingcommand.Pleasenotethatafterrebootthesechangeswillbelost.#sysctl–w{variable-name=value}131

Page 419: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwariHack100.NiceCommandKerneldecideshowmuchprocessortimeisrequiredforaprocessbasedonthenicevalue.Possiblenicevaluerangeis:-20to20.Aprocessthathasanicevalueof-20isveryhighpriority.Theprocessthathasanicevalueof20isverylowpriority.Usepsaxltodisplaythenicevalueofallrunningprocessasshownbelow.# psaxlFUIDPIDPPIDPRINIVSZRSSWCHANSTATTTYTIMECOMMAND40101602172552-S?0:17init[5]

Page 420: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

1031341900ksoftiSN?3:18[ksoftirqd/0]101015-1000workerS<?0:01[events/0]405145125103212418592-SNs?0:08/usr/bin/python/usr/bin/rhn-applet-gui--sm-client-iddefault440514751421603528604-S?0:00/sbin/pam_timestamp_check-droot1503175524180160142083920-S?0:01/home/www/apache2/bin/httpd-f/home/www/apache2/conf/httpd.conf-kstart

Howtoassignalowprioritytoashell-script?(highernicevalue)

Page 421: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Intheexamplebelow,whenIstartedthenice-test.shscriptinthebackground,ittookthenicevalueof0.$./nice-test.sh&[3]13009$psaxl|grepnice-test050913009128631704652972waitS132

Page 422: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackpts/1AjayKumarTiwari0:00/bin/bash./nice-test.sh[Note:6thcolumnwithvalue0isthenice.]Now,letusexecutethesameshellscriptwithadifferentnicevalueasshownbelow.$ nice-10./nice-test.sh&[1]13016$psaxl|grepnice-test0509130161286330104236968waitpts/10:00/bin/bash./nice-test.sh

Page 423: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

SN[Note:6thcolumnwithvalue10isthenicevaluefortheshell-script.]Howtoassignahighprioritytoashell-script?(Lowernicevalue)Inthefollowingexample,letusassignanicevalueof-10(minus10)tothenice-test.shshellscript.$ nice--10./nice-test.sh&[1]13021$nice:cannotsetpriority:PermissiondeniedNote: Onlyrootusercansetanegativenicevalue.Loginasrootandtrythesame.Pleasenotethatthereisadoubledashbeforethe10inthenicecommandbelow.

Page 424: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

# nice--10./nice-test.sh&[1]13060#psaxl|grepnice-test40130601302410-105388964waitpts/10:00/bin/bash./nice-test.sh133S<

Page 425: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwari[Note:6thcolumnwithvalue-10isthenicevalueoftheshell-script.]Hack101.ReniceCommandRenicealterstheschedulingpriorityofarunningprocess.Howtodecreasethepriorityofarunningprocess?(Increasenice)Intheexamplebelow,anexistingshell-scriptisrunningatnicevalueof10.(6thcolumninthepsoutput)$psaxl|grepnice-test

Page 426: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

0509132451321630105244968waitpts/10:00/bin/bash./nice-test.shSNToincreasethenicevalue(thusreducingthepriority),executetherenicecommandasshownbelow.$ renice16-p1324513245:oldpriority10,newpriority16$psaxl|grepnice-test0509132451321636165244968waitpts/10:00/bin/bash./nice-test.shSN[Note:Now,the6thcolumnofthenice-test.sh(PID13245)showsthenewnicevalueof16.]

Page 427: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Howtoincreasethepriorityofarunningprocess?(Decreasenice)Intheexamplebelow,anexistingshell-scriptisrunningatanicevalueof10.134

Page 428: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHack(6thcolumninthepsoutput)AjayKumarTiwari$psaxl|grepnice-test0509132541321630104412968waitpts/10:00/bin/bash./nice-test.shSNInincreasethepriority,givealowernicevalueasshownbelow.However,onlyrootcanincreasethepriorityofarunningprocess,elseyou’llgetthefollowingerrormessage.$ renice5-p13254renice:13254:setpriority:Permissiondenied

Page 429: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

Loginasroottoincreasethepriorityofarunningprocess$su-# renice5-p1325413254:oldpriority10,newpriority5#psaxl|grepnice-test050913254132162554412968waitpts/10:00/bin/bash./nice-test.shSN[Note:The6thcolumnnowshowsalowernicevalueof5(increasedpriority)]135

Page 430: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently
Page 431: Linux Hack - DropPDF1.droppdf.com/files/fVG5O/linux-hack-ajay-kumar-tiwari.pdf · Linux Hack Ajay Kumar Tiwari Chapter 1: Powerful CD Command Hacks cd is one of the most frequently

LinuxHackAjayKumarTiwaLinuxHack