linux hack - droppdf1.droppdf.com/files/fvg5o/linux-hack-ajay-kumar-tiwari.pdf · linux hack ajay...

Post on 04-Jun-2020

1 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

LinuxHack

LinuxHackAjayKumarTiwari

TableofContentsIntroductionForewordVersionChapter1:PowerfulCDCommandHacksUseCDPATHtodefinethebasedirectoryforcdcommandUsecdaliastonavigateupthedirectoryeffectivelyPerformmkdirandcdusingasinglecommand

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

DebugSSHClientSessionToggleSSHSessionusingSSHEscapeCharacterSSHSessionStatisticsusingSSHEscapeCharacterChapter4:EssentialLinuxCommandsGrepCommand2

LinuxHackAjayKumarTiwariFindCommandSuppressStandardOutputandErrorMessageJoinCommandChangetheCaseXargsCommandSortCommandUniqCommandCutCommandStatCommandDiffCommand

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

ChangebackgroundcolorofthepromptDisplaymultiplecolorsinthepromptChangethepromptcolorusingtputCreateyourownpromptusingtheavailablecodesforPS1variableUsebashshellfunctioninsidePS1variableUseshellscriptinsidePS1variableChapter7:ArchiveandCompression3

LinuxHackAjayKumarTiwariZipcommandbasicsAdvancedcompressionusingzipcommand.PasswordProtectionofZipfilesValidateaziparchiveTarCommandBasicsCombinegzip,bzip2withtarChapter8:CommandLineHistoryDisplayTIMESTAMPinhistoryusingHISTTIMEFORMATSearchthehistoryusingControl+RRepeatpreviouscommandquicklyusing4differentmethods

ExecuteaspecificcommandfromhistoryExecutepreviouscommandthatstartswithaspecificwordControlthetotalnumberoflinesinthehistoryusingHISTSIZEChangethehistoryfilenameusingHISTFILEEliminatethecontinuousrepeatedentryfromhistoryusingHISTCONTROLEraseduplicatesacrossthewholehistoryusingHISTCONTROLForcehistorynottorememberaparticularcommandusingHISTCONTROLClearalltheprevioushistoryusingoption-c

SubstitutewordsfromhistorycommandsSubstituteaspecificargumentforaspecificcommandDisabletheusageofhistoryusingHISTSIZEIgnorespecificcommandsfromthehistoryusingHISTIGNOREChapter9:SystemAdministrationTasksPartitionusingfdiskFormatapartitionusingmke2fskMountthepartition4

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

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

ReaddatafilefieldsinsideashellscriptChapter12:SystemMonitoringandPerformanceFreecommandTopCommandPsCommandDfCommandKillCommandDuCommand5

LinuxHackAjayKumarTiwariSarCommandvmstatCommandNetstatCommandSysctlCommandNiceCommandReniceCommand6

lsofcommands.

LinuxHackAjayKumarTiwari

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

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

Storage(EMC).Copyright©2015–AjayKumarTiwari7

LinuxHackAjayKumarTiwari

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

VersionVersion1.0Date25-Mar-2015RevisionsFirstEdition8

LinuxHackAjayKumarTiwari

Chapter1:PowerfulCDCommandHackscdisoneofthemostfrequentlyusedcommandsduringaUNIXsession.The6cdcommandhacksmentionedinthischapterwillboostyourproductivityinstantlyandmakeiteasiertonavigatethedirectorystructurefromcommandline.Hack1.UseCDPATHtodefinethebasedirectoryforcdcommandIfyouarefrequentlyperformingcdtosubdirectoriesofaspecificparentdirectory,youcansettheCDPATHtotheparentdirectoryandperformcdto

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

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

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

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

#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

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

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

/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

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

#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.

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

/tmp/very/long/directory/structure12

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

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

#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

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

#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.

14

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

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

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

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

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

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

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

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]

17

LinuxHackAjayKumarTiwari

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

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"

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

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

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

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'

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

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

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

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"

ThuFeb2808:27:41PST200821

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

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"

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

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

LinuxHackAjayKumarTiwari

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

$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

%ssh-keygen-Fpublickey.pub24

LinuxHackAjayKumarTiwarionthekeyfile.Areyousureyouwanttocontinueconnecting(yes/no)Hostkeysavedto/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pubhostkeyforremotehost.example.com,acceptedbyjsmithMonMay26200816:06:50-0700jsmith@remotehost.example.compassword:remotehost.example.com$TheSecondtimewhenyoulogintotheremotehostfromthelocalhost,itwillpromptonlyforthepasswordastheremotehostkeyisalreadyaddedtotheknownhostslistofthesshclient.

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

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

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

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:31-0700jsmith@remotehost.example.com’spassword:remotehost$Hack14.DebugSSHClientSessionSometimesitisnecessarytoviewdebugmessagestotroubleshootanySSHconnectionissues.pass-v(lowercasev)optiontothesshasshownbelowto

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

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:

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

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

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

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

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

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:

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

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

LinuxHackAjayKumarTiwari

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

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

textJohnin/etc/passwdfile.31

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

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

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

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

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

daysagounderthecurrentdirectory.33

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

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

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

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

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

35

LinuxHackAjayKumarTiwarithedatafromthesetwofilesusingemployee-idasshownbelow.$catemployee.txt100200300400JasonSmithJohnDoeSanjayGuptaAshokSharma

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

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

SanjayGuptaAshokSharma$ tra-zA-Z<employee.txt100JASONSMITH200JOHNDOE300SANJAYGUPTA36

LinuxHack400ASHOKSHARMAAjayKumarTiwariConvertafiletoalllower-case$catdepartment.txt100200300400FINANCEMARKETINGPRODUCTDEVELOPMENT

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

file$ cut-c1-8names.txtEmmaThoAlexJasMadisonSanjayGNishaSi42

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

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

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

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

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

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

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

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

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

1.080.993.394.506.10ConnecttimeforalltheusersTodisplayconnecttimeforalltheusersuse–passhownbelow.Pleasenotethatthisindicatesthecumulativeconnecttimefortheindividualusers.$ ac-pjohnmadisonsanjaynisha

3.640.0688.17105.9245

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

Dec24Dec29Todaytotaltotaltotaltotaltotaltotaltotal12.855.051.035.37

8.151.422.9546

LinuxHackAjayKumarTiwari

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

-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

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

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

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

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

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

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

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

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

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

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

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

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

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

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"

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

LinuxHackAjayKumarTiwari

Chapter6:ColorfulandFunctionalShellPromptUsingPS1Hack34.Displayusername,hostnameandbasenameofdirectoryinthepromptThePS1inthisexampledisplaysfollowingthreeinformationintheprompt:ooo

\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)]>"

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

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

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>

54

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.

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

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

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"

[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

56

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

\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\]”

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+‘}

57

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

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

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

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

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

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

oo59

LinuxHackooooooo\hthehostnameuptothefirstpart\HthehostnameAjayKumarTiwari\jthenumberofjobscurrentlymanagedbytheshell

\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

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

ooooooooooo

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

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

61

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

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

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%)

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%)

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

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.

# 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---------------

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

34cc03a1---

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

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

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

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

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

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:

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

testing:var/log/anaconda.log66OKOK

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

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

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

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

#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.

68

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

LinuxHackAjayKumarTiwari

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

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

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

history.Whenyou’vealreadyexecutedaverylongcommand,youcansimply70

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

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

71

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

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

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

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

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

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

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

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

58history|tail-474

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

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

erased]75

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

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

# history-c76

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

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

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

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

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

80servicehttpdstop81history[Note:Historydidnotdisplaypwdandls]79

LinuxHackAjayKumarTiwari

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

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

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

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

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

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

with:81

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

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

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

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=

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

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

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

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

84

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

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

Firstinode:Inodesize:Defaultdirectoryhash:DirectoryHashSeed:ccccc123292bAjayKumarTiwariContinueLinux109491214013899401684848110149690

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

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)

85

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

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

LinuxHackAjayKumarTiwariHack70.CreateanewuserAddanewuser–BasicmethodSpecifyonlytheusername.#useraddjsmithAddanewuserwithadditionalParameterYoucanalsospecifythefollowingparametertotheuseradd

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.

NewUNIXpassword:BADPASSWORD:itisbasedonadictionarywordRetypenewUNIXpassword:passwd:allauthenticationtokensupdatedsuccessfully.87

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

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

88

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

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-

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

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:

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.

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

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

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

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

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

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

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

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

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

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

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

94

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

‘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.

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

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

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

ooo96

LinuxHackAjayKumarTiwari

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

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

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

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

#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

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

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

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

infonoticewarnerrorcritalertemerg99

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

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)

authn_file_module(shared)authn_alias_module(shared)SyntaxOK100

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

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

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

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.

# 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

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

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

-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”

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.

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

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

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

LinuxHackAjayKumarTiwari

Chapter11:BashScriptingHack84.ExecutionSequenceof.bash_*filesWhatisthesequenceinwhichthefollowingfilesareexecuted?oooooo

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

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

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

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

0–32767asshownbelow.$echo$RANDOM106

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

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

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

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

++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

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

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

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

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

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

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

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

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

MadisonRandyispartofProductDevelopmentdepartmentSanjayGuptaispartofSupportdepartmentNishaSinghispartofSalesdepartment111

LinuxHackAjayKumarTiwari

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

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

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

LinuxHacktotal101019832994used9830983free271983

2011shared0AjayKumarTiwaribuffers170cached601Mem:Swap:Total:Hack90.TopCommandtopcommanddisplaysrealtimeinformationaboutvariousperformance

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

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

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

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

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

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

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

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

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

# 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

immediately.Forclaritypurpose,fewcolumnshavebeencut-offintheoutputbelow.# psaxuf116

LinuxHackAjayKumarTiwarirootrootrootrootrootrootrootrootroot511511Oct14

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

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

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

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

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

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

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

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

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

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

# kill3118631187119

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

}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

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

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

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

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

ooCOMMANDprocessname.PIDprocessIDUSERUsername121

LinuxHackooooooFDfiledescriptorTYPEnodetypeofthefileDEVICEdevicenumberSIZEfilesizeNODEnodenumber

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

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

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

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

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

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

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

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

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

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

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

# 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

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

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

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

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

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

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

020csussyidwa000100001007359001000

LinuxHack00000282120134108579701202821201341085797012000000AjayKumarTiwari

0111757700100001007366001000VmstatprocsSectionoorfield:Totalnumberofrunnableprocessbfield:TotalnumberofblockedprocessMemorysectionooooSwpdfield:Usedswapspace

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

SystemSectionooInfield:Numberofinterruptspersecond.Csfield:Numberofcontextswitchespersecond.127

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

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

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

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

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*

0eth0default192.168.1.10eth0Genmask255.255.255.0255.255.0.00.0.0.0UFlagsMSS00UUG0000

DisplayRAWnetworkstatistics# netstat--statistics--rawIp:129

LinuxHackAjayKumarTiwari11080343totalpacketsreceived0forwarded1withunknownprotocol0incomingpacketsdiscarded11037744incomingpacketsdelivered11199763requestssentoutIcmp:577135ICMPmessagesreceived64inputICMPmessagefailed.ICMPinputhistogram:

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

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

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

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

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

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

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)

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

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

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.

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

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

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.]

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

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

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

LinuxHackAjayKumarTiwaLinuxHack

top related