194

Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

  • Upload
    others

  • View
    22

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you
Page 2: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack

Page 3: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

TableofContentsIntroduction

Foreword

Version

Chapter1:PowerfulCDCommandHacksUseCDPATHtodefinethebasedirectoryforcdcommand

Usecdaliastonavigateupthedirectoryeffectively

Performmkdirandcdusingasinglecommand

Use“cd-”totogglebetweenthelasttwodirectories

Usedirs,pushdandpopdtomanipulatedirectorystack

Use“shopt-scdspell”toautomaticallycorrectmistyped

directorynamesoncd

Chapter2:DateManipulationSetSystemDateandTime

SetHardwareDateandTime

DisplayCurrentDateandTimeinaSpecificFormat

DisplayPastDateandTime

DisplayFutureDateandTime

Chapter3:SSHClientCommandsIdentifySSHClientVersion

LogintoRemoteHostusingSSH

DebugSSHClientSession

ToggleSSHSessionusingSSHEscapeCharacter

SSHSessionStatisticsusingSSHEscapeCharacter

Chapter4:EssentialLinuxCommandsGrepCommand

2

Page 4: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

FindCommand

SuppressStandardOutputandErrorMessage

JoinCommand

ChangetheCase

XargsCommand

SortCommand

UniqCommand

CutCommand

StatCommand

DiffCommand

Displaytotalconnecttimeofusers

Chapter5:PS1,PS2,PS3,PS4andPROMPT_COMMANDPS1-DefaultInteractionPrompt

PS2-ContinuationInteractivePrompt

PS3-Promptusedby“select”insideshellscript

PS4-Usedby“set-x”toprefixtracingoutput

PROMPT_COMMAND

Chapter6:ColorfulandFunctionalShellPromptUsingPS1Displayusername,hostnameandbasenameofdirectoryin

theprompt

Displaycurrenttimeintheprompt

Displayoutputofanycommandintheprompt

Changeforegroundcoloroftheprompt

Changebackgroundcoloroftheprompt

Displaymultiplecolorsintheprompt

Changethepromptcolorusingtput

CreateyourownpromptusingtheavailablecodesforPS1

variable

UsebashshellfunctioninsidePS1variable

Page 5: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

UseshellscriptinsidePS1variable

Chapter7:ArchiveandCompression3

Page 6: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Zipcommandbasics

Advancedcompressionusingzipcommand.

PasswordProtectionofZipfiles

Validateaziparchive

TarCommandBasics

Combinegzip,bzip2withtar

Chapter8:CommandLineHistoryDisplayTIMESTAMPinhistoryusingHISTTIMEFORMAT

SearchthehistoryusingControl+R

Repeatpreviouscommandquicklyusing4differentmethods

Executeaspecificcommandfromhistory

Executepreviouscommandthatstartswithaspecificword

Controlthetotalnumberoflinesinthehistoryusing

HISTSIZE

ChangethehistoryfilenameusingHISTFILE

Eliminatethecontinuousrepeatedentryfromhistoryusing

HISTCONTROL

Eraseduplicatesacrossthewholehistoryusing

HISTCONTROL

Forcehistorynottorememberaparticularcommandusing

HISTCONTROL

Clearalltheprevioushistoryusingoption-c

Substitutewordsfromhistorycommands

Substituteaspecificargumentforaspecificcommand

DisabletheusageofhistoryusingHISTSIZE

IgnorespecificcommandsfromthehistoryusingHISTIGNORE

Page 7: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

Chapter9:SystemAdministrationTasksPartitionusingfdisk

Formatapartitionusingmke2fsk

Mountthepartition

4

Page 8: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Finetunethepartitionusingtune2fs

Createaswapfilesystem.

Createanewuser

Createanewgroupandassigntoanuser

SetupSSHpasswordlesslogininOpenSSH

Usessh-copy-idalongwithssh-agent

Crontab

SafeRebootOfLinuxUsingMagicSysRqKey

Chapter10:ApachectlandHttpdExamplesPassdifferenthttpd.conffilenametoapachectl

UseatemporaryDocumentRootwithoutmodifying

httpd.conf

IncreasetheLogLeveltemporarily

DisplaythemodulesinsideApache

Showallaccepteddirectivesinsidehttpd.conf

Validatethehttpd.confaftermakingchanges

Displaythehttpdbuildparameters

Loadaspecificmoduleonlyondemand

Chapter11:BashScriptingExecutionSequenceof.bash_*files

Howtogeneraterandomnumberinbashshell

Debugashellscript

Quoting

Readdatafilefieldsinsideashellscript

Chapter12:SystemMonitoringandPerformanceFreecommand

TopCommand

PsCommand

DfCommand

Page 9: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

KillCommand

DuCommand

5

Page 10: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

SarCommand

vmstatCommand

NetstatCommand

SysctlCommand

NiceCommand

ReniceCommand

6

lsofcommands.

Page 11: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Introduction”Thereareonly10typesofpeopleintheworld—thosewhounderstand

binary,thosewhodon’t,andthosewhounderstandgraycode”

—Geek

I’mAjayKumarTiwari,authorofthisBook

Ihavedoneintensiveprogrammingonseveral

languagesandCismyfavorite.Ihavedonelotofwork

ontheinfrastructuresideincludingLinuxsystem

administration,DBA,Networking,Hardwareand

Storage(EMC).

Copyright©2015–AjayKumarTiwari

7

Page 12: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

ForewordAnothercollectionofhacks?Yes!Ifyouhavejustcompletedyourfirstadmin

courseorlookingforbetterwaystogetthejobdonethe“LinuxHack”

eBookisagoodpointtostart.Theseusefultipsareconcise,wellwrittenand

easytoread.

Welldone-IwillrecommendthiseBooktomystudents.

—Prof.Dr.FritzMehner,FHSüdwestfalen,Germany

(AuthorofseveralVimplugins,includingbash-supportvimplugin)

VersionVersion

1.0

Date

25-Mar-2015

Revisions

FirstEdition

8

Page 13: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter1:PowerfulCDCommandHackscdisoneofthemostfrequentlyusedcommandsduringaUNIXsession.The6

cdcommandhacksmentionedinthischapterwillboostyourproductivity

instantlyandmakeiteasiertonavigatethedirectorystructurefrom

commandline.

Hack1.UseCDPATHtodefinethebasedirectoryforcdcommandIfyouarefrequentlyperformingcdtosubdirectoriesofaspecificparent

directory,youcansettheCDPATHtotheparentdirectoryandperformcdto

thesubdirectorieswithoutgivingtheparentdirectorypathasexplained

below.

[ramesh@dev-db~]# pwd

/home/ramesh

[ramesh@dev-db~]# cdmail

-bash:cd:mail:Nosuchfileordirectory

[Note:Thisislookingformaildirectoryundercurrent

directory]

[ramesh@dev-db~]# exportCDPATH=/etc

[ramesh@dev-db~]# cdmail

/etc/mail

[Note:Thisislookingformailunder/etcandnot

undercurrentdirectory]

[ramesh@dev-db/etc/mail]# pwd

/etc/mail

Tomakethischangepermanent,addexportCDPATH=/etctoyour

~/.bash_profile

9

Page 14: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

SimilartothePATHvariable,youcanaddmorethanonedirectoryentryin

theCDPATHvariable,separatingthemwith:,asshownbelow.

exportCDPATH=.:~:/etc:/var

Thishackcanbeveryhelpfulunderthefollowingsituations:

o

OracleDBAsfrequentlyworkingunder$ORACLE_HOME,canset

theCDPATHvariabletotheoraclehome

Unixsysadminsfrequentlyworkingunder/etc,cansetthe

CDPATHvariableto/etc

Developersfrequentlyworkingunderprojectdirectory

/home/projects,cansettheCDPATHvariableto/home/projects

End-usersfrequentlyaccessingthesubdirectoriesundertheir

homedirectory,cansettheCDPATHvariableto~(home

directory)

o

o

o

Hack2.UsecdaliastonavigateupthedirectoryeffectivelyWhenyouarenavigatingupaverylongdirectorystructure,youmaybeusing

cd....\withmultiple..\’sdependingonhowmanydirectoriesyouwanttogo

upasshownbelow.#mkdir-p

/tmp/very/long/directory/structure/that/is/too/deep

#cd/tmp/very/long/directory/structure/that/is/too/deep

#pwd

/tmp/very/long/directory/structure/that/is/too/deep

#cd../../../../

10

Page 15: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack#pwd

/tmp/very/long/directory/structure

AjayKumarTiwari

Insteadofexecutingcd../../../..tonavigatefourlevelsup,useoneofthe

followingthreealiasmethods:

Method1:Navigateupthedirectoryusing“..n”Intheexamplebelow,..4isusedtogoup4directorylevel,..3togoup3

directorylevel,..2togoup2directorylevel.Addthefollowingaliastoyour

~/.bash_profileandre-login.

alias

alias

alias

alias

alias

..=“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,as

whenyoutypethefirsttwodots,youarethinking“goinguponedirectory”,

afterthateveryadditionaldot,istogoonelevelup.So,use….(fourdots)to

Page 16: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

goup3directoryleveland..(twodots)togoup1directorylevel.Addthe

followingaliastoyour~/.bash_profileandre-loginforthe…..(fivedots)to

workproperly.

alias..=“cd..”

11

Page 17: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackalias

alias

alias

alias

…=“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:Navigateupthedirectoryusingcdfollowedby

consecutivedotsIntheexamplebelow,cd…..(cdfollowedbyfivedots)isusedtogoup4

directorylevel.Makingit5dotstogoup4directorystructureisreallyeasyto

remember,aswhenyoutypethefirsttwodots,youarethinking“goingup

onedirectory”,afterthateveryadditionaldot,istogoonelevelup.So,use

cd….(cdfollowedbyfourdots)togoup3directorylevelandcd…(cd

followedbythreedots)togoup2directorylevel.Addthefollowingaliasto

your~/.bash_profileandre-loginfortheabovecd…..(fivedots)towork

properly.

alias

alias

alias

alias

alias

cd..=“cd..”

cd…=“cd../..”

Page 18: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

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/structure

12

Page 19: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Method5:NavigateupthedirectoryusingcdfollowedbynumberIntheexamplebelow,cd4(cdfollowedbynumber4)isusedtogoup4

directorylevel.

alias

alias

alias

alias

alias

cd1=“cd

cd2=“cd

cd3=“cd

cd4=“cd

cd5=“cd

..”

../..”

../../..”

../../../..”

../../../../..”

Hack3.PerformmkdirandcdusingasinglecommandSometimeswhenyoucreateanewdirectory,youmaycdtothenewdirectory

immediatelytoperformsomeworkasshownbelow.

#mkdir-p/tmp/subdir1/subdir2/subdir3

#cd/tmp/subdir1/subdir2/subdir3

#pwd

/tmp/subdir1/subdir2/subdir3

Wouldn’titbenicetocombinebothmkdirandcdinasinglecommand?Add

thefollowingtothe.bash_profileandre-login.

Page 20: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

$vi.bash_profile

functionmkdircd(){mkdir-p“$@”&&evalcd

“\”\$$#\””;}

Now,performbothmkdirandcdatthesametimeusingasinglecommandas

shownbelow:

13

Page 21: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack#mkdircd/tmp/subdir1/subdir2/subdir3

AjayKumarTiwari

[Note:Thiscreatesthedirectoryandcdtoit

automatically]

#pwd

/tmp/subdir1/subdir2/subdir3

Hack4.Use“cd-”totogglebetweenthelasttwodirectoriesYoucantogglebetweenthelasttwocurrentdirectoriesusingcd-asshown

below.#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/deep

Hack5.Usedirs,pushdandpopdtomanipulatedirectorystackYoucanusedirectorystacktopushdirectoriesintoitandlaterpopdirectory

fromthestack.Followingthreecommandsareusedinthisexample.

14

Page 22: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

o

o

dirs:Displaythedirectorystack

pushd:Pushdirectoryintothestack

AjayKumarTiwari

popd:Popdirectoryfromthestackandcdtoit

Dirswillalwaysprintthecurrentdirectoryfollowedbythecontentofthe

stack.Evenwhenthedirectorystackisempty,dirscommandwillstillprint

onlythecurrentdirectoryasshownbelow.

#popd

-bash:popd:directorystackempty

#dirs

~

#pwd

/home/ramesh

Howtousepushdandpopd?Letusfirstcreatesometemporarydirectories

andpushthemtothedirectorystackasshownbelow.

#

#

#

#

mkdir

mkdir

mkdir

mkdir

/tmp/dir1

/tmp/dir2

/tmp/dir3

/tmp/dir4

Page 23: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

#cd/tmp/dir1

#pushd.

#cd/tmp/dir2

#pushd.

#cd/tmp/dir3

#pushd.

#cd/tmp/dir4

#pushd.

15

Page 24: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

#dirs

/tmp/dir4/tmp/dir4/tmp/dir3/tmp/dir2/tmp/dir1

[Note:Thefirstdirectory(/tmp/dir4)ofthedir

commandoutputisalwaysthecurrentdirectoryandnot

thecontentfromthestack.]

Atthisstage,thedirectorystackcontainsthefollowingdirectories:

/tmp/dir4

/tmp/dir3

/tmp/dir2

/tmp/dir1

Thelastdirectorythatwaspushedtothestackwillbeatthetop.Whenyou

performpopd,itwillcdtothetopdirectoryentryinthestackandremoveit

fromthestack.Asshownabove,thelastdirectorythatwaspushedintothe

stackis/tmp/dir4.So,whenwedoapopd,itwillcdtothe/tmp/dir4and

removeitfromthedirectorystackasshownbelow.

#popd

#pwd

/tmp/dir4

[Note:Aftertheabovepopd,directoryStackContains:

/tmp/dir3

/tmp/dir2

/tmp/dir1]

#popd

#pwd

/tmp/dir3

[Note:Aftertheabovepopd,directoryStackContains:

/tmp/dir2

/tmp/dir1]

#popd

Page 25: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

16

Page 26: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack#pwd

/tmp/dir2

AjayKumarTiwari

[Note:Aftertheabovepopd,directoryStackContains:

/tmp/dir1]

#popd

#pwd

/tmp/dir1

[Note:Aftertheabovepopd,directoryStackisempty!]

#popd

-bash:popd:directorystackempty

Hack6.Use“shopt-scdspell”toautomaticallycorrectmistypeddirectorynamesoncdUseshopt-scdspelltocorrectthetyposinthecdcommandautomaticallyas

shownbelow.Ifyouarenotgoodattypingandmakelotofmistakes,thiswill

beveryhelpful.

#cd/etc/mall

-bash:cd:/etc/mall:Nosuchfileordirectory

# shopt-scdspell

#cd/etc/mall

#pwd

/etc/mail

[Note:Bymistake,whenItypedmallinsteadofmail,

cdcorrecteditautomatically]

17

Page 27: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter2:DateManipulationHack7.SetSystemDateandTimeTochangethesystemdateuse:

#date{mmddhhmiyyyy.ss}

o

o

o

o

o

o

mm–Month

dd–Date

hh–24hourformat

mi–Minutes

yyyy–Year

ss–seconds

Forexample,tosetsystemdatetoJan31st2008,10:19p.m,53seconds

#date013122192009.53

Youcanalsochangesystemdateusingsetargumentasshownbelow.

#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

Page 28: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack#date+%T%p-s“10:19:53PM”

AjayKumarTiwari

Hack8.SetHardwareDateandTimeBeforesettingthehardwaredateandtime,makesuretheOSdateandtime

issetappropriatelyasshowninthehack#7.

Setthehardwaredateandtimebasedonthesystemdateasshownbelow:

#hwclock–systohc

#hwclock—systohc–utc

Usehwclockwithoutanyparameter,toviewthecurrenthardwaredateand

time:

#hwclock

ChecktheclockfiletoverifywhetherthesystemissetforUTC:

#cat/etc/sysconfig/clock

ZONE=“America/Los_Angeles”

UTC=false

ARC=false

Hack9.DisplayCurrentDateandTimeinaSpecificFormatFollowingaredifferentwaysofdisplayingthecurrentdateandtimein

variousformats:

19

Page 29: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack$date

ThuJan

AjayKumarTiwari

108:19:23PST2009

$date—date=“now”

ThuJan108:20:05PST2009

$date—date=“today”

ThuJan108:20:12PST2009

$date—date=‘1970-01-0100:00:01UTC+5hours’+%s

18001

$date‘+CurrentDate:%m/%d/%y%nCurrentTime:%H:%M:%S’

CurrentDate:01/01/09

CurrentTime:08:21:41

$date+”%d-%m-%Y”

01-01-2009

$date+”%d/%m/%Y”

01/01/2009

$date+”%A,%B%d%Y”

Thursday,January012009

Followingarethedifferentformatoptionsyoucanpasstothedate

command:

o

o

o

o

o

o

%Ddate(mm/dd/yy)

%ddayofmonth(01..31)

%mmonth(01..12)

Page 30: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

%ylasttwodigitsofyear(00..99)

%alocale’sabbreviatedweekdayname(Sun..Sat)

%Alocale’sfullweekdayname,variablelength

(Sunday..Saturday)

%blocale’sabbreviatedmonthname(Jan..Dec)

o

20

Page 31: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

AjayKumarTiwari

%Blocale’sfullmonthname,variablelength

(January..December)

%Hhour(00..23)

%Ihour(01..12)

%Yyear(1970…)

o

o

o

Hack10.DisplayPastDateandTimeFollowingarevariouswaystodisplayapastdateandtime:

$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:41PST2008

21

Page 32: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack11.DisplayFutureDateandTimeFollowingexamplesshowshowtodisplayafuturedateandtime.

$date

ThuJan

108: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:10PST2009

22

Page 33: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack$date—date=”-1daysago”

FriJan208:31:15PST2009

$date—date=“thisWednesday”

WedJan700:00:00PST2009

AjayKumarTiwari

23

Page 34: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter3:SSHClientCommandsHack12.IdentifySSHClientVersionSometimesitmaybenecessarytoidentifytheSSHclientthatyouare

currentlyrunningandit’scorrespondingversionnumber.Usessh–Vto

identifytheversionnumber.PleasenotethatLinuxcomeswithOpenSSH.

ThefollowingexampleindicatesthatthisparticularsystemisusingOpenSSH:

$ssh-V

OpenSSH_3.9p1,OpenSSL0.9.7aFeb192003

ThefollowingexampleindicatesthatthisparticularsystemisusingSSH2:

$ssh-V

ssh:SSHSecureShell3.2.9.1(non-commercialversion)

oni686-pc-linux-gnu

Hack13.LogintoRemoteHostusingSSHTheFirsttimewhenyoulogintoaremotehostfromalocalhost,itwilldisplay

thehostkeynotfoundmessageandyoucangive“yes”tocontinue.Thehost

keyoftheremotehostwillbeaddedunder.ssh2/hostkeysdirectoryofyour

homedirectory,asshownbelow.

localhost$ ssh-ljsmithremotehost.example.comHostkeynotfoundfromdatabase.

Keyfingerprint:

xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-

tuxum

Youcangetapublickey’sfingerprintbyrunning

%ssh-keygen-Fpublickey.pub

24

Page 35: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwarionthekeyfile.

Areyousureyouwanttocontinueconnecting(yes/no)? Yes

Hostkeysavedto

/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub

hostkeyforremotehost.example.com,acceptedbyjsmithMon

May26200816:06:50-0700

[email protected]:

remotehost.example.com$

TheSecondtimewhenyoulogintotheremotehostfromthelocalhost,itwill

promptonlyforthepasswordastheremotehostkeyisalreadyaddedtothe

knownhostslistofthesshclient.

localhost$ ssh-ljsmithremotehost.example.com

[email protected]:

remotehost.example.com$

Forsomereason,ifthehostkeyoftheremotehostischangedafteryou

loggedinforthefirsttime,youmaygetawarningmessageasshownbelow.

Thiscouldbebecauseofvariousreasonssuchas:

o

o

Sysadminupgraded/reinstalledtheSSHserverontheremotehost

Someoneisdoingmaliciousactivityetc.,

Thebestpossibleactiontotakebeforesaying“yes”tothemessagebelow,is

tocallyoursysadminandidentifywhyyougotthehostkeychangedmessage

andverifywhetheritisthecorrecthostkeyornot.

localhost$ ssh-ljsmithremotehost.example.com@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@WARNING:HOSTIDENTIFICATIONHASCHANGED!@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

ITISPOSSIBLETHATSOMEONEISDOINGSOMETHINGNASTY!

Someonecouldbeeavesdroppingonyourightnow(man-in-the-

25

Page 36: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwarimiddleattack)!

Itisalsopossiblethatthehostkeyhasjustbeenchanged.

Pleasecontactyoursystemadministrator.

Addcorrecthostkeyto

“/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pu

b”

togetridofthismessage.

Receivedserverkey’sfingerprint:

xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-arde-

tuxum

Youcangetapublickey’sfingerprintbyrunning

%ssh-keygen-Fpublickey.pub

onthekeyfile.

Agentforwardingisdisabledtoavoidattacksbycorrupted

servers.

Areyousureyouwanttocontinueconnecting(yes/no)? yes

Doyouwanttochangethehostkeyondisk(yes/no)? yes

Agentforwardingre-enabled.

Hostkeysavedto

/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub

hostkeyforremotehost.example.com,acceptedbyjsmithMon

May26200816:17:31-0700

[email protected]’spassword:

remotehost$

Hack14.DebugSSHClientSessionSometimesitisnecessarytoviewdebugmessagestotroubleshootanySSH

connectionissues.pass-v(lowercasev)optiontothesshasshownbelowto

viewthesshdebugmessages.

ExamplewithoutSSHclientdebugmessage:localhost$ ssh-ljsmithremotehost.example.com

warning:Connectingtoremotehost.example.comfailed:

Noaddressassociatedtothename

Page 37: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

26

Page 38: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

ExamplewithSSHclientdebugmessage:locaclhost$ ssh-v-ljsmithremotehost.example.com

debug:

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:Userconfigfilenot

found,usingdefaults.(Lookedfor

‘/home/jsmith/.ssh2/ssh2_config’)

debug:Connectingtoremotehost.example.com,port22…

(SOCKSnotused)

warning:Connectingtoremotehost.example.comfailed:

Noaddressassociatedtothename

Hack15.ToggleSSHSessionusingSSHEscapeCharacterWhenyou’veloggedontotheremotehostusingsshfromthelocalhost,you

maywanttocomebacktothelocalhosttoperformsomeactivityandgoback

toremotehostagain.Inthiscase,youdon’tneedtodisconnectthessh

sessiontotheremotehost.Instead,followthestepsbelow.

1.Logintoremotehostfromlocalhost:

localhost$ssh-ljsmithremotehost

27

Page 39: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

2.Nowyouareconnectedtotheremotehost:

remotehost$

3.Tocomebacktothelocalhosttemporarily,typetheescapecharacter~

andControl-Z.

Whenyoutype~youwillnotseethatimmediatelyonthescreenuntilyou

press<Control-Z>andpressenter.So,ontheremotehostinanewlineenter

thefollowingkeystrokesforthebelowtowork:~<Control-Z>

remotehost$ ~^Z

[1]+Stoppedssh-ljsmithremotehost

localhost$

4.Nowyouarebacktothelocalhostandthesshremotehostclientsession

runsasatypicalUNIXbackgroundjob,whichyoucancheckasshownbelow:

localhost$ jobs

[1]+Stoppedssh-ljsmithremotehost

5.Youcangobacktotheremotehostsshwithoutenteringthepassword

againbybringingthebackgroundsshremotehostsessionjobtoforegroundon

thelocalhost.

localhost$ fg%1

ssh-ljsmithremotehost

remotehost$

28

Page 40: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack16.SSHSessionStatisticsusingSSHEscapeCharacterTogetsomeusefulstatisticsaboutthecurrentsshsession,dothefollowing.

ThisworksonlyonSSH2client.

1.Logintoremotehostfromlocalhost.

localhost$ssh-ljsmithremotehost

2.Ontheremotehost,typesshescapecharacter~followedbysasshown

below.ThiswilldisplaylotofusefulstatisticsaboutthecurrentSSH

connection.

remotehost$[Note:The~sisnotvisibleonthe

commandlinewhenyoutype.]

remotehost:remotehost

localhost:localhost

remoteversion:SSH-1.99-OpenSSH_3.9p1

localversion:SSH-2.0-3.2.9.1SSHSecure

Shell(non-commercial)

compressedbytesin:1506

uncompressedbytesin:1622

compressedbytesout:4997

uncompressedbytesout:5118

packetsin:15

packetsout:24

rekeys:0

Algorithms:

Chosenkeyexchangealgorithm:diffie-hellman-

group1-sha1

Chosenhostkeyalgorithm:ssh-dss

Commonhostkeyalgorithms:ssh-dss,ssh-rsa

Algorithmsclienttoserver:

Page 41: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

Cipher:aes128-cbc

MAC:hmac-sha1

Compression:zlib

29

Page 42: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAlgorithmsservertoclient:

Cipher:aes128-cbc

MAC:hmac-sha1

Compression:zlib

localhost$

AjayKumarTiwari

AdditionalSSHInfoOnasidenote,tosetupSSHkeybasedauthentication,referopenSSHand

SSH2tutorials.

30

Page 43: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter4:EssentialLinuxCommandsHack17.GrepCommandgrepcommandisusedtosearchfilesforaspecifictext.Thisisincredibly

powerfulcommandwithlotofoptions.

Syntax:grep[options]pattern[files]

HowcanIfindalllinesmatchingaspecifickeywordonafile?Inthisexample,greplooksforthetextJohninside/etc/passwdfileand

displaysallthematchinglines.

#grepJohn/etc/passwd

jsmith:x:1082:1082:JohnSmith:/home/jsmith:/bin/bash

jdoe:x:1083:1083:JohnDoe:/home/jdoe:/bin/bash

Option-v,willdisplayallthelinesexceptthematch.Intheexamplebelow,

itdisplaysalltherecordsfrom/etc/passwordthatdoesn’tmatchJohn.

Note: Thereareseverallinesinthe/etc/passwordthatdoesn’tcontainthe

wordJohn.Onlythefirstlineoftheoutputisshownbelow.

#grep-vJohn/etc/passwdjbourne:x:1084:1084:JasonBourne:/home/jbourne:/bin/bash

Howmanylinesmatchedthetextpatterninaparticularfile?Intheexamplebelow,itdisplaysthetotalnumberoflinesthatcontainsthe

textJohnin/etc/passwdfile.

31

Page 44: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

#grep-cJohn/etc/passwd

2

Youcanalsogetthetotalnumberoflinesthatdidnotmatchthespecific

patternbypassingoption-cv.

#grep-cvJohn/etc/passwd

39

Howtosearchatextbyignoringthecase?Passtheoption-i(ignorecase),whichwillignorethecasewhilesearching.

#grep-ijohn/etc/passwd

jsmith:x:1082:1082:JohnSmith:/home/jsmith:/bin/bash

jdoe:x:1083:1083:JohnDoe:/home/jdoe:/bin/bash

HowdoIsearchallsubdirectoriesforatextmatchingaspecific

pattern?Useoption-r(recursive)forthispurpose.Intheexamplebelow,itwillsearch

forthetext“John”byignoringthecaseinsideallthesubdirectoriesunder

/home/users.

Thiswilldisplaytheoutputintheformatof“filename:linethatmatchingthe

pattern”.Youcanalsopasstheoption-l,whichwilldisplayonlythenameof

thefilethatmatchesthepattern.

#grep-rijohn/home/users

/home/users/subdir1/letter.txt:John,Thanksforyour

contribution.

/home/users/name_list.txt:JohnSmith

32

Page 45: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack/home/users/name_list.txt:JohnDoe

#grep-riljohn/root

/home/users/subdir1/letter.txt

/home/users/name_list.txt

AjayKumarTiwari

Hack18.FindCommandfindisfrequentlyusedcommandtofindfilesintheUNIXfilesystembasedon

numerousconditions.Letusreviewsomepracticeexamplesoffindcommand.

Syntax:find[pathnames][conditions]

Howtofindfilescontainingaspecificwordinitsname?Thefollowingcommandlooksforallthefilesunder/etcdirectorywithmail

inthefilename.

#find/etc-name“*mail*”

Howtofindallthefilesgreaterthancertainsize?Thefollowingcommandwilllistallthefilesinthesystemgreaterthan

100MB.

#find/-typef-size+100M

Howtofindfilesthatarenotmodifiedinthelastxnumberofdays?Thefollowingcommandwilllistallthefilesthatweremodifiedmorethan60

daysagounderthecurrentdirectory.

33

Page 46: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack#find.-mtime+60

AjayKumarTiwari

Howtofindfilesthataremodifiedinthelastxnumberofdays?Thefollowingcommandwilllistallthefilesthatweremodifiedinthelast

twodaysunderthecurrentdirectory.

#find.–mtime-2

Howtodeleteallthearchivefileswithextension*.tar.gzand

greaterthan100MB?Pleasebecarefulwhileexecutingthefollowingcommandasyoudon’twant

todeletethefilesbymistake.Thebestpracticeistoexecutethesame

commandwithls–ltomakesureyouknowwhichfileswillgetdeletedwhen

youexecutethecommandwithrm.#find/-typef-name*.tar.gz-size+100M-execls-l{}\;

#find/-typef-name*.tar.gz-size+100M-execrm-f{}\;

Howtoarchiveallthefilesthatarenotmodifiedinthelastx

numberofdays?Thefollowingcommandfindsallthefilesnotmodifiedinthelast60days

under/home/jsmithdirectoryandcreatesanarchivefilesunder/tmpinthe

formatofddmmyyyy_archive.tar.

#find/home/jsmith-typef-mtime+60|xargstar-cvf

/tmp/`date‘+%d%m%Y’_archive.tar`

Onasidenote,youcanperformlotoffilerelatedactivities(includingfinding

files)usingmidnightcommanderGUI,apowerfultextbasedfilemanagerfor

Unix.

34

Page 47: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack19.SuppressStandardOutputandErrorMessageSometimewhiledebuggingashellscript,youmaynotwanttoseeeitherthe

standardoutputorstandarderrormessage.Use/dev/nullasshownbelowfor

suppressingtheoutput.

Suppressstandardoutputusing>/dev/nullThiswillbeveryhelpfulwhenyouaredebuggingshellscripts,whereyou

don’twanttodisplaytheechostatementandinterestedinonlylookingat

theerrormessages.

#catfile.txt>/dev/null

#./shell-script.sh>/dev/null

Suppressstandarderrorusing2>/dev/nullThisisalsohelpfulwhenyouareinterestedinviewingonlythestandard

outputanddon’twanttoviewtheerrormessages.

#catinvalid-file-name.txt2>/dev/null

#./shell-script.sh2>/dev/null

Hack20.JoinCommandJoincommandcombineslinesfromtwofilesbasedonacommonfield.

Intheexamplebelow,wehavetwofiles–employee.txtandsalary.txt.Both

haveemployee-idascommonfield.So,wecanusejoincommandtocombine

35

Page 48: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

thedatafromthesetwofilesusingemployee-idasshownbelow.

$catemployee.txt

100

200

300

400

JasonSmith

JohnDoe

SanjayGupta

AshokSharma

$catbonus.txt

100

200

300

400

$5,000

$500

$3,000

$1,250

$ joinemployee.txtbonus.txt

100

200

300

400

JasonSmith$5,000

JohnDoe$500

SanjayGupta$3,000

AshokSharma$1,250

Hack21.ChangetheCase

Page 49: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

Convertafiletoallupper-case$catemployee.txt

100

200

300

400

JasonSmith

JohnDoe

SanjayGupta

AshokSharma

$ tra-zA-Z<employee.txt

100JASONSMITH

200JOHNDOE

300SANJAYGUPTA

36

Page 50: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack400ASHOKSHARMA

AjayKumarTiwari

Convertafiletoalllower-case$catdepartment.txt

100

200

300

400

FINANCE

MARKETING

PRODUCTDEVELOPMENT

SALES

$ trA-Za-z<department.txt

100

200

300

400

finance

marketing

productdevelopment

sales

Hack22.XargsCommandxargsisaverypowerfulcommandthattakesoutputofacommandandpassit

asargumentofanothercommand.Followingaresomepracticalexampleson

howtousexargseffectively.

1.Whenyouaretryingtodeletetoomanyfilesusingrm,youmaygeterror

message:/bin/rmArgumentlisttoolong–Linux.Usexargstoavoidthis

problem.

find~-name‘*.log’-print0|xargs-0rm-f

2.Getalistofallthe*.conffileunder/etc/.Therearedifferentwaystoget

Page 51: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

thesameresult.Followingexampleisonlytodemonstratetheuseofxargs.

Theoutputofthefindcommandinthisexampleispassedtothels–loneby

oneusingxargs.

37

Page 52: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

#find/etc-name“*.conf”|xargsls–l

3.IfyouhaveafilewithlistofURLsthatyouwouldliketodownload,youcan

usexargsasshownbelow.

#caturl-list.txt|xargswget–c

4.Findoutallthejpgimagesandarchiveit.

#find/-name*.jpg-typef-print|xargstar-cvzf

images.tar.gz

5.Copyalltheimagestoanexternalhard-drive.

#ls*.jpg|xargs-n1-icp{}/external-hard-

drive/directory

Hack23.SortCommandSortcommandsortsthelinesofatextfile.Followingareseveralpractical

examplesonhowtousethesortcommandbasedonthefollowingsampletext

filethathasemployeeinformationintheformat:

employee_name:employee_id:department_name.

$ catnames.txt

EmmaThomas:100:Marketing

AlexJason:200:Sales

MadisonRandy:300:ProductDevelopment

SanjayGupta:400:Support

NishaSingh:500:Sales

38

Page 53: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Sortatextfileinascendingorder$ sortnames.txt

AlexJason:200:Sales

EmmaThomas:100:Marketing

MadisonRandy:300:ProductDevelopment

NishaSingh:500:Sales

SanjayGupta:400:Support

Sortatextfileindescendingorder$ sort-rnames.txt

SanjayGupta:400:Support

NishaSingh:500:Sales

MadisonRandy:300:ProductDevelopment

EmmaThomas:100:Marketing

AlexJason:200:Sales

Sortacolondelimitedtextfileon2ndfield(employee_id)$ sort-t:-k2names.txt

EmmaThomas:100:Marketing

AlexJason:200:Sales

MadisonRandy:300:ProductDevelopment

SanjayGupta:400:Support

NishaSingh:500:Sales

Sortatabdelimitedtextfileon3rdfield(department_name)and

suppressduplicates$ sort-t:-u-k3names.txt

39

Page 54: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackEmmaThomas:100:Marketing

MadisonRandy:300:ProductDevelopment

AlexJason:200:Sales

SanjayGupta:400:Support

AjayKumarTiwari

Sortthepasswdfilebythe3rdfield(numericuserid)$ sort-t:-k3n/etc/passwd|more

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Sort/etc/hostsfilebyip-addres$ sort-t.-k1,1n-k2,2n-k3,3n-k4,4n/etc/hosts

127.0.0.1localhost.localdomainlocalhost

192.168.100.101dev-db.thegeekstuff.comdev-db

192.168.100.102prod-db.thegeekstuff.comprod-db

192.168.101.20dev-web.thegeekstuff.comdev-web

192.168.101.21prod-web.thegeekstuff.comprod-web

Combinesortwithothercommandso

o

ps–ef|sort :Sorttheoutputofprocesslist

ls-al|sort+4n :Listthefilesintheascendingorderofthefile-

size.i.esortedby5thfiledanddisplayingsmallestfilesfirst.

ls-al|sort+4nr :Listthefilesinthedescendingorderofthe

file-size.i.esortedby5thfiledanddisplayinglargestfilesfirst.

o

40

Page 55: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack24.UniqCommandUniqcommandismostlyusedincombinationwithsortcommand,asuniq

removesduplicatesonlyfromasortedfile.i.eInorderforuniqtowork,all

theduplicateentriesshouldbeintheadjacentlines.Followingaresome

commonexamples.

1.Whenyouhaveanemployeefilewithduplicateentries,youcandothe

followingtoremoveduplicates.

$sortnamesd.txt|uniq

$sort–unamesd.txt

2.Ifyouwanttoknowhowmanylinesareduplicates,dothefollowing.The

firstfieldinthefollowingexamplesindicateshowmanyduplicateswhere

foundforthatparticularline.So,inthisexamplethelinesbeginningwith

AlexandEmmawerefoundtwiceinthenamesd.txtfile.

$ sortnamesd.txt|uniq–c

2

2

1

1

1

AlexJason:200:Sales

EmmaThomas:100:Marketing

MadisonRandy:300:ProductDevelopment

NishaSingh:500:Sales

SanjayGupta:400:Support

3.Thefollowingdisplaysonlytheentriesthatareduplicates.

$ sortnamesd.txt|uniq–cd

2AlexJason:200:Sales

2EmmaThomas:100:Marketing

41

Page 56: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack25.CutCommandCutcommandcanbeusedtodisplayonlyspecificcolumnsfromatextfileor

othercommandoutputs.

Followingaresomeoftheexamples.

Displaythe1stfield(employeename)fromacolondelimitedfile$ cut-d:-f1names.txt

EmmaThomas

AlexJason

MadisonRandy

SanjayGupta

NishaSingh

Display1stand3rdfieldfromacolondelimitedfile$ cut-d:-f1,3names.txt

EmmaThomas:Marketing

AlexJason:Sales

MadisonRandy:ProductDevelopment

SanjayGupta:Support

NishaSingh:Sales

Displayonlythefirst8charactersofeverylineinafile$ cut-c1-8names.txt

EmmaTho

AlexJas

Madison

SanjayG

NishaSi

42

Page 57: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

MiscCutcommandexampleso

cut-d:-f1/etc/passwd Displaystheunixloginnamesforalltheusers

inthesystem.

free|tr-s‘‘|sed‘/^Mem/!d’|cut-d””-f2 Displaysthetotal

memoryavailableonthesystem.

o

Hack26.StatCommandStatcommandcanbeusedeithertocheckthestatus/propertiesofasingle

fileorthefilesystem.

Displaystatisticsofafileordirectory.$ stat/etc/my.cnf

File:

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

2006-06-0120:42:27.000000000-0700

2007-02-0214:17:27.000000000-0800

$ stat/home/ramesh

Page 58: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

File:`/home/ramesh’

Size:4096Blocks:8IOBlock:

4096directory

Device:803h/2051dInode:5521409Links:7

Access:(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-0800

Change:2009-01-0912:07:33.000000000-0800

43

Page 59: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Displaythestatusofthefilesystemusingoption–f$ stat-f/

File:

ID:

Blocks:

1876998

Inodes:

“/”

0Namelen:255Type:ext2/ext3

Total:2579457Free:2008027Available:

Size:4096

Total:1310720Free:1215892

Hack27.DiffCommanddiffcommandcomparestwodifferentfilesandreportsthedifference.The

outputisverycrypticandnotstraightforwardtoread.

Syntax:diff[options]file1file2

Whatwasmodifiedinmynewfilewhencomparetomyoldfile?Theoption-winthediffcommandwillignorethewhitespacewhile

performingthecomparison.

Inthefollowingdiffoutput:

o

Thelinesabove–,indicatesthechangeshappenedinfirstfileinthe

diffcommand(i.ename_list.txt).

Thelinesbelow–,indicatesthechangeshappenedtothesecond

fileinthediffcommand(i.ename_list_new.txt).Thelinesthat

belongtothefirstfilestartswith<andthelinesofsecondfilestarts

with>.

o

# diff-wname_list.txtname_list_new.txt

Page 60: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

44

Page 61: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack2c2,3

<JohnDoe

>JohnMDoe

>JasonBourne

AjayKumarTiwari

Hack28.DisplaytotalconnecttimeofusersAccommandwilldisplaythestatisticsabouttheuser’sconnecttime.

ConnecttimeforthecurrentloggedinuserWiththeoption–d,itwillbreakdowntheoutputfortheindividualdays.In

thisexample,I’vebeenloggedintothesystemformorethan6hourstoday.

OnDec1st,Iwasloggedinforabout1hour.

$ ac–d

Dec1

Dec2

Dec3

Dec4

Today

total

total

total

total

total

1.08

0.99

3.39

4.50

6.10

ConnecttimeforalltheusersTodisplayconnecttimeforalltheusersuse–passhownbelow.Pleasenote

Page 62: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

thatthisindicatesthecumulativeconnecttimefortheindividualusers.

$ ac-p

john

madison

sanjay

nisha

3.64

0.06

88.17

105.92

45

Page 63: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackramesh

total309.21

AjayKumarTiwari

111.42

ConnecttimeforaspecificuserTogetaconnecttimereportforaspecificuser,executethefollowing:

$ ac-dsanjay

Jul2

Aug25

Sep3

Sep4

Dec24

Dec29

Today

total

total

total

total

total

total

total

12.85

5.05

1.03

5.37

8.15

1.42

2.95

46

Page 64: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter5:PS1,PS2,PS3,PS4andPROMPT_COMMANDHack29.PS1-DefaultInteractionPromptThedefaultinteractivepromptonyourLinuxcanbemodifiedasshownbelow

tosomethingusefulandinformative.Inthefollowingexample,thedefault

PS1was“\s-\v\$”,whichdisplaystheshellnameandtheversionnumber.Let

uschangethisdefaultbehaviortodisplaytheusername,hostnameand

currentworkingdirectorynameasshownbelow.

-bash-3.2$

exportPS1=”\u@\h\w>“

ramesh@dev-db~>cd/etc/mail

ramesh@dev-db/etc/mail>

[Note:Promptchangedto“username@hostnamecurrent-

dir>”format]

FollowingPS1codesareusedinthisexample:

o

o

o

\u–Username

\h–Hostname

\w-Fullpathnameofcurrentdirectory.Pleasenotethatwhen

youareinthehomedirectory,thiswilldisplayonly~asshown

above

NotethatthereisaspaceattheendinthevalueofPS1.Personally,Iprefer

aspaceattheendofthepromptforbetterreadability.

MakethissettingpermanentbyaddingexportPS1=”\u@\h\w>”toeither

.bash_profile(or).bashrcasshownbelow.

47

Page 65: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

ramesh@dev-db~>vi~/.bash_profile

ramesh@dev-db~>vi~/.bashrc

[Note:AddexportPS1=”\u@\h\w>”tooneoftheabove

files]

RefertothenextchapterforseveralpracticalexamplesofPS1usagein

detail.

Hack30.PS2-ContinuationInteractivePromptAverylongcommandcanbebrokendowntomultiplelinesbygiving\atthe

endoftheline.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“>”forcontinuation

prompt]

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

48

Page 66: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackcontinuationprompt]

AjayKumarTiwari

Ifounditveryhelpfulandeasytoread,whenIbreakmylongcommandsinto

multiplelinesusing.Ihavealsoseenotherswhodon’tliketobreak-uplong

commands.

Hack31.PS3-Promptusedby“select”insideshellscriptYoucandefineacustompromptfortheselectloopinsideashellscript,using

thePS3environmentvariable,asexplainedbelow.

ShellscriptandoutputWITHOUTPS3:ramesh@dev-db~> catps3.sh

selectiinmontuewedexit

do

case$iin

mon)echo“Monday”;;

tue)echo“Tuesday”;;

wed)echo“Wednesday”;;

exit)exit;;

esac

done

ramesh@dev-db~> ./ps3.sh

1)mon

2)tue

3)wed

4)exit

#?1

Monday

#?4

[Note:Thisdisplaysthedefault“#?”forselect

Page 67: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

commandprompt]

49

Page 68: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

ShellscriptandoutputWITHPS3:ramesh@dev-db~> catps3.sh

PS3=“Selectaday(1-4):“

selectiinmontuewedexit

do

case$iin

mon)echo“Monday”;;

tue)echo“Tuesday”;;

wed)echo“Wednesday”;;

exit)exit;;

esac

done

ramesh@dev-db~> ./ps3.sh

1)mon

2)tue

3)wed

4)exit

Selectaday(1-4):1

Monday

Selectaday(1-4):4

[Note:Thisdisplaysthemodified“Selectaday(1-4):”

forselectcommandprompt]

Hack32.PS4-Usedby“set-x”toprefixtracingoutputThePS4shellvariabledefinesthepromptthatgetsdisplayed,whenyou

executeashellscriptindebugmodeasshownbelow.

ShellscriptandoutputWITHOUTPS4:50

Page 69: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackramesh@dev-db~> catps4.sh

set-x

echo“PS4demoscript”

ls-l/etc/|wc-l

du-sh~

ramesh@dev-db~> ./ps4.sh

++echo‘PS4demoscript’

PS4demoscript

++ls-l/etc/

++wc-l

243

++du-sh/home/ramesh

48K/home/ramesh

AjayKumarTiwari

[Note:Thisdisplaysthedefault“++”whiletracingthe

outputusingset-x]

ShellscriptandoutputWITHPS4:ThePS4definedbelowintheps4.shhasthefollowingtwocodes:

o

o

$0-indicatesthenameofscript

$LINENO-displaysthecurrentlinenumberwithinthescript

ramesh@dev-db~> catps4.sh

exportPS4=’$0.$LINENO+‘

set-x

echo“PS4demoscript”

ls-l/etc/|wc-l

du-sh~

ramesh@dev-db~> ./ps4.sh

../ps4.sh.3+echo‘PS4demoscript’

Page 70: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

PS4demoscript

../ps4.sh.4+ls-l/etc/

51

Page 71: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack../ps4.sh.4+wc-l

243

../ps4.sh.5+du-sh/home/ramesh

48K/home/ramesh

AjayKumarTiwari

[Note:Thisdisplaysthemodified“{script-name}.{line-

number}+”whiletracingtheoutputusingset-x]

Hack33.PROMPT_COMMANDBashshellexecutesthecontentofthePROMPT_COMMANDjustbefore

displayingthePS1variable.

ramesh@dev-db~> exportPROMPT_COMMAND=“date+%k:%m:%S”

22:08:42

ramesh@dev-db~>

[Note:ThisdisplaysthePROMPT_COMMANDandPS1output

ondifferentlines]

IfyouwanttodisplaythevalueofPROMPT_COMMANDinthesamelineasthe

PS1,usetheecho-nasshownbelow.

ramesh@dev-db~> exportPROMPT_COMMAND=“echo-n[$(date

+%k:%m:%S)]”

[22:08:51]ramesh@dev-db~>

[Note:ThisdisplaysthePROMPT_COMMANDandPS1output

onthesameline]

52

Page 72: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter6:ColorfulandFunctionalShellPromptUsingPS1Hack34.Displayusername,hostnameandbasenameofdirectoryinthepromptThePS1inthisexampledisplaysfollowingthreeinformationintheprompt:

o

o

o

\u–Username

\h–Hostname

\W–Basenameofthecurrentworkingdirectory

-bash-3.2$ exportPS1=”\u@\h\W>“

ramesh@dev-db~>cd/etc/mail

ramesh@dev-dbmail>

Hack35.DisplaycurrenttimeinthepromptInthePS1environmentvariable,youcandirectlyexecuteanyLinux

command,byspecifyingintheformat$(linux_command).Inthefollowing

example,thecommand$(date)isexecutedtodisplaythecurrenttimeinside

theprompt.

ramesh@dev-db~> exportPS1=”\u@\h[\$(date

+%k:%m:%S)]>“

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

53

Page 73: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Youcanalsouse\ttodisplaythecurrenttimeinthehh:mm:ssformatas

shownbelow:

ramesh@dev-db~> exportPS1=”\u@\h[\t]>“

ramesh@dev-db[12:42:55]>

Youcanalsouse\@todisplaythecurrenttimein12-houram/pmformatas

shownbelow:

ramesh@dev-db~> exportPS1=”[\@]\u@\h>“

[04:12PM]ramesh@dev-db>

Hack36.DisplayoutputofanycommandinthepromptYoucandisplayoutputofanyLinuxcommandintheprompt.Thefollowing

exampledisplaysthreeitemsseparatedby|(pipe)inthecommandprompt:

o

o

o

\!:Thehistorynumberofthecommand

\h:hostname

$kernel_version:Theoutputoftheuname-rcommandfrom

$kernel_versionvariable

\$?:Statusofthelastcommand

o

ramesh@dev-db~> kernel_version=$(uname-r)ramesh@dev-db~> exportPS1=”\!|\h|$kernel_version|\$?>“

473|dev-db|2.6.25-14.fc9.i686|0>

54

Page 74: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack37.ChangeforegroundcolorofthepromptDisplaypromptinbluecolor,alongwithusername,hostandcurrentdirectory

information

$ exportPS1=”\e[0;34m\u@\h\w>\e[m“

[Note:Thisisforlightblueprompt]

$ exportPS1=”\e[1;34m\u@\h\w>\e[m“

[Note:Thisisfordarkblueprompt]

\e[-Indicatesthebeginningofcolorprompt

x;ym-Indicatescolorcode.Usethecolorcodevaluesmentioned

below.

\e[m-indicatestheendofcolorprompt

o

o

o

ColorCodeTable:Black0;30

Blue0;34

Green0;32

Cyan0;36

Red0;31

Purple0;35

Brown0;33

[Note:Replace0with1fordarkcolor]

Makethecolorchangepermanentbyaddingthefollowinglinesyour

~/.bash_profileor~/.bashrc

$vi~/.bash_profile

55

Page 75: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

STARTCOLOR=’\e[0;34m’;

ENDCOLOR=”\e[0m”

exportPS1=”$STARTCOLOR\u@\h\w>$ENDCOLOR”

Hack38.ChangebackgroundcolorofthepromptChangethebackgroundcolorbyspecifying\e[{code}minthePS1promptas

shownbelow.

$ exportPS1=”\e[47m\u@\h\w>\e[m“

[Note:ThisisforLightGraybackground]

Combinationofbackgroundandforeground.

$ exportPS1=”\e[0;34m\e[47m\u@\h\w>\e[m“

[Note:ThisisforLightBlueforegroundandLightGray

background]

Addthefollowingtoyour~/.bash_profileor~/.bashrctomaketheabove

backgroundandforegroundcolorpermanent.

$vi~/.bash_profile

STARTFGCOLOR=’\e[0;34m’;

STARTBGCOLOR=”\e[47m”

ENDCOLOR=”\e[0m”

exportPS1=”$STARTFGCOLOR$STARTBGCOLOR\u@\h\w>

$ENDCOLOR”

Playaroundbyusingthefollowingbackgroundcolorandchoosetheonethat

matchyourtaste:

o

\e[40m

56

Page 76: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

o

o

o

o

o

o

\e[41m

\e[42m

\e[43m

\e[44m

\e[45m

\e[46m

\e[47m

AjayKumarTiwari

Hack39.DisplaymultiplecolorsinthepromptYoucanalsodisplaymultiplecolorsinthesameprompt.Addthefollowing

functiontoyour~/.bash_profile

functionprompt{

localBLUE=”\[\033[0;34m\]”

localDARK_BLUE=”\[\033[1;34m\]”

localRED=”\[\033[0;31m\]”

localDARK_RED=”\[\033[1;31m\]”

localNO_COLOR=”\[\033[0m\]”

case$TERMin

xterm*|rxvt*)

TITLEBAR=’\[\033]0;\u@\h:\w\007\]’

;;

*)

TITLEBAR=””

Page 77: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

;;

esac

PS1=”\u@\h[\t]>”

PS1=”${TITLEBAR}\

$BLUE\u@\h$RED[\t]>$NO_COLOR”

PS2=’continue->‘

PS4=’$0.$LINENO+‘

}

57

Page 78: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Youcanre-loginforthechangestotakeeffectorsourcethe.bash_profileas

shownbelow.

$../.bash_profile

$ prompt

ramesh@dev-db[13:02:13]>

Hack40.ChangethepromptcolorusingtputYoucanalsochangecolorofthePS1promptusingtputasshownbelow:

$exportPS1=”\[$(tputbold)$(tputsetb4)$(tputsetaf

7)\]\u@\h:\w$\[$(tputsgr0)\]“

tputColorCapabilities:

o

o

o

o

tputsetab[1-7]-SetabackgroundcolorusingANSIescape

tputsetb[1-7]-Setabackgroundcolor

tputsetaf[1-7]-SetaforegroundcolorusingANSIescape

tputsetf[1-7]-Setaforegroundcolor

tputTextModeCapabilities:

o

o

o

o

o

tputbold-Setboldmode

tputdim-turnonhalf-brightmode

tputsmul-beginunderlinemode

tputrmul-exitunderlinemode

tputrev-Turnonreversemode

Page 79: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

58

Page 80: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

o

o

AjayKumarTiwari

tputsmso-Enterstandoutmode(boldonrxvt)

tputrmso-Exitstandoutmode

tputsgr0-Turnoffallattributes

ColorCodefortput:

o

o

o

o

o

o

o

o

0–Black

1–Red

2–Green

3–Yellow

4–Blue

5–Magenta

6–Cyan

7-White

Hack41.CreateyourownpromptusingtheavailablecodesforPS1variableUsethefollowingcodesandcreateyourownpersonalPS1Linuxpromptthat

isfunctionalandsuitesyourtaste.

o

o

Page 81: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

\aanASCIIbellcharacter(07)

\dthedatein“WeekdayMonthDate”format(e.g.,“TueMay

26″)

\D{format}-theformatispassedtostrftime(3)andtheresultis

insertedintothepromptstring;anemptyformatresultsina

locale-specifictimerepresentation.Thebracesarerequired

\eanASCIIescapecharacter(033)

o

o

59

Page 82: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

o

o

o

o

o

o

\hthehostnameuptothefirstpart

\Hthehostname

AjayKumarTiwari

\jthenumberofjobscurrentlymanagedbytheshell

\lthebasenameoftheshell’sterminaldevicename

\nnewline

\rcarriagereturn

\sthenameoftheshell,thebasenameof$0(theportion

followingthefinalslash)

\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$HOMEabbreviatedwitha

tilde

\Wthebasenameofthecurrentworkingdirectory,with$HOME

abbreviatedwithatilde

\!thehistorynumberofthiscommand

\#thecommandnumberofthiscommand

\$iftheeffectiveUIDis0,a#,otherwisea$

Page 83: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

\nnnthecharactercorrespondingtotheoctalnumbernnn

\abackslash

\[beginasequenceofnon-printingcharacters,whichcouldbe

usedtoembedaterminalcontrolsequenceintotheprompt

60

o

o

o

o

o

o

o

o

o

o

o

o

o

o

o

Page 84: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

AjayKumarTiwari

\]endasequenceofnon-printingcharacter

Hack42.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~/.bashrctomake

thischangepermanent:

$vi.bash_profile

functionhttpdcount{

psaux|grephttpd|grep-vgrep|wc-l

}

exportPS1=’\u@\h[`httpdcount`]>‘

Hack43.UseshellscriptinsidePS1variableYoucanalsoinvokeashellscriptinsidethePS1variable.Intheexample

below,the~/bin/totalfilesize.sh,whichcalculatesthetotalfilesizeofthe

currentdirectory,isinvokedinsidethePS1variable.

ramesh@dev-db~>cat~/bin/totalfilesize.sh

61

Page 85: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

forfilesizein$(ls-l.|grep“^-”|awk‘{print

$5}’)

do

lettotalsize=$totalsize+$filesize

done

echo-n“$totalsize”

ramesh@dev-db~>exportPATH=$PATH:~/bin

ramesh@dev-db~> exportPS1=”\u@\h

[\$(totalfilesize.sh)bytes]>“

ramesh@dev-db[534bytes]>cd/etc/mail

ramesh@dev-db[167997bytes]>

[Note:Thisexecutesthetotalfilesize.shtodisplay

thetotalfilesizeofthecurrentdirectoryinthePS1

prompt]

62

Page 86: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

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

Page 87: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

adding:var/log/secure.2(deflated93%)

63

Page 88: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack

Howtounzipa*.zipcompressedfile?#unzipvar-log.zip

Archive:var-log.zip

inflating:var/log/acpid

inflating:var/log/anaconda.log

inflating:var/log/anaconda.syslog

inflating:var/log/anaconda.xlog

creating:var/log/audit/

AjayKumarTiwari

Toseeadetailedoutputduringunzippassthe–voptionasshownbelow.# unzip-vvar-log.zip

Archive:var-log.zip

LengthMethodSizeRatioDateTime

Name

––—––––-––––-

-

1916Defl:N36981%02-08-0814:27

var/log/acpid

13546Defl:N290079%02-02-0714:25

var/log/anaconda.log

skip..

7680Defl:N411

var/log/wtmp.1

40981Defl:N7395

var/log/Xorg.0.log

–––––

–-

414069912809229

files

95%

82%

93%

12-30-0810:55

Page 89: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

02-08-0814:28

fe876ee9

6386a95e

56

CRC-32

––

e2ffdc0c

34cc03a1

Howtolistacontentofzipfilewithuncompressingit?# unzip-lvar-log.zip

Archive:var-log.zip

LengthDateTime

–––––-

191602-08-0814:27

Name

–-

var/log/acpid

64

Page 90: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack13546

..skip..

40981

40981

––—

41406991

02-08-0814:28

02-08-0714:56

02-02-0714:25

AjayKumarTiwari

var/log/anaconda.log

var/log/Xorg.0.log

var/log/Xorg.0.log.old

––-

56files

Hack45.Advancedcompressionusingzipcommand.Thereare10levelsofcompressionprovidedbyzipcommand.

o

Level0isthelowestlevel,whereitjustarchivesthefilewithout

anycompression.

Level1willperformlittlecompression.But,willbeveryfast.

Level6isthedefaultlevelofcompression.

Level9isthemaximumcompression.Thiswillbeslowerwhen

comparedtodefaultlevel.Inmyopinion,unlessyouare

compressingahugefile,youshouldalwaysuselevel9.

o

o

o

Intheexamplebelow,IusedLevel0,defaultLevel6,andLevel9

compressiononasamedirectory.Seethecompressedfilesizeyourself.

Page 91: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

#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—1rootroot

var-log-files-default.zip

-rw-r—r—1rootroot

var-log-files-0.zip

-rw-r—r—1rootroot

var-log-files-9.zip

2817248Jan

41415301Jan

2582610Jan

113:05

113:05

113:06

65

Page 92: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack46.PasswordProtectionofZipfilesPasstheoption–Ptothezipcommandtoassignapasswordtothezipfile.

#zip-Pmysecurepwdvar-log-protected.zip/var/log/*

Theaboveoptionisgoodifyouareusingthecommandinsideashell-script

forbackgroundjobs.However,whenyouareperformingthecompression

interactivelyonthecommand-line,youdon’twantthepasswordtobevisible

inthehistory.So,usetheoption–easshownbelowtoassignthepassword.

#zip-evar-log-protected.zip/var/log/*

Enterpassword:

Verifypassword:

updating:var/log/acpid(deflated81%)

updating:var/log/anaconda.log(deflated79%)

Whenyouareuncompressingapasswordprotectedfile,itwillaskforthe

passwordasshownbelow.

#unzipvar-log-protected.zip

Archive:var-log-protected.zip

[var-log-protected.zip]var/log/acpidpassword:

Hack47.ValidateaziparchiveSometimeyoumaywanttovalidateaziparchivewithoutextractingit.To

testthevalidityofthezipfile,passoption–tasshownbelow.

# unzip-tvar-log.zip

Archive:var-log.zip

testing:var/log/acpid

testing:var/log/anaconda.log

66

OK

OK

Page 93: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacktesting:var/log/anaconda.syslog

skip…

testing:var/log/wtmp

testing:var/log/wtmp.1

testing:var/log/Xorg.0.log

AjayKumarTiwari

OK

OK

OK

OK

Noerrorsdetectedincompresseddataofvar-log.zip.

Hack48.TarCommandBasicstarcommand(tapearchive)isusedtoconvertagroupoffilesintoan

archive.

Syntax:tar[options][tar-archive-name][other-file-

names]

HowcanIcreateasinglebackupfileofallfilesandsubdirectories

undermyhomedirectory?Thefollowingcommandcreatesasinglearchivebackupfilecalled

my_home_directory.tarunder/tmp.Thisarchivewillcontainallthefilesand

subdirectoriesunder/home/jsmith.

o

o

Optionc,standsforcreateanarchive.

Optionvstandsforverbosemode,displaysadditionalinformation

whileexecutingthecommand.

Optionfindicatesthearchivefilenamementionedinthecommand.

o

#tarcvf/tmp/my_home_directory.tar/home/jsmith

67

Page 94: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

HowdoIviewallthefilesinsidethetararchive?Optiontwilldisplayallthefilesfromthetararchive.

#tartvf/tmp/my_home_directory.tar

HowdoIextractallthefilesfromatararchive?Optionxwillextractthefilesfromthetararchiveasshownbelow.Thiswill

extractthecontenttothecurrentdirectorylocationfromwherethe

commandisexecuted.

#tarxvf/tmp/my_home_directory.tar

HowdoIextracttar.gzfilestoaspecificdirectory?#tarxvfz/tmp/my_home_directory.tar.gz–C

/home/ramesh

Hack49.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.gz

Note: Usinggzipisfasterwhencomparedtobzip2.

68

Page 95: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Howtousebzip2withtar?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.bz2

Note: Usingbizp2giveshigherlevelofcompressionwhencomparedtogzip.

69

Page 96: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter8:CommandLineHistoryWhenyouareusingLinuxcommandlinefrequently,usingthehistory

effectivelycanbeamajorproductivityboost.Infact,onceyouhave

masteredthe15examplesthatI’veprovidedhere,you’llfindusingcommand

linemoreenjoyableandfun.

Hack50.DisplayTIMESTAMPinhistoryusingHISTTIMEFORMATTypicallywhenyoutypehistoryfromcommandline,itdisplaysthe

command#andthecommand.Forauditingpurpose,itmaybebeneficialto

displaythetimestampalongwiththecommandasshownbelow.

# exportHISTTIMEFORMAT=’%F%T‘

#history

1

2

3

4

|more

2008-08-05

2008-08-05

2008-08-05

2008-08-05

19:02:39

19:02:39

19:02:39

19:02:39

servicenetworkrestart

exit

id

Page 97: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

cat/etc/redhat-release

Note: Youcanalsosetupthefollowingaliastoviewtherecenthistory

commands.

aliash1=‘history10’

aliash2=‘history20’

aliash3=‘history30’

Hack51.SearchthehistoryusingControl+RIstronglybelievethatthismaybeyourmostfrequentlyusedfeatureof

history.Whenyou’vealreadyexecutedaverylongcommand,youcansimply

70

Page 98: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

searchhistoryusingakeywordandre-executethesamecommandwithout

havingtotypeitfully.PressControl+Randtypethekeyword.

Inthefollowingexample,Isearchedforred,whichdisplayedtheprevious

command“cat/etc/redhat-release”inthehistorythatcontainedtheword

red.

#[Note:PressCtrl+Rfromthecommandprompt,which

willdisplaythereverse-i-searchpromptasshown

below]

(reverse-i-search)`red‘:cat/etc/redhat-release

[Note:Pressenterwhenyouseeyourcommand,which

willexecutethecommandfromthehistory]

# cat/etc/redhat-release

Fedorarelease9(Sulphur)

Sometimesyouwanttoeditacommandfromhistorybeforeexecutingit.For

e.g.youcansearchforhttpd,whichwilldisplayservicehttpdstopfromthe

commandhistory,selectthiscommandandchangethestoptostartandre-

executeitagainasshownbelow.

#[Note:PressCtrl+Rfromthecommandprompt,which

willdisplaythereverse-i-searchprompt]

(reverse-i-search)`httpd‘:servicehttpdstop

[Note:Presseitherleftarroworrightarrowkeywhen

youseeyourcommand,whichwilldisplaythecommand

foryoutoedit,beforeexecutingit]

#servicehttpdstart

71

Page 99: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack52.Repeatpreviouscommandquicklyusing4differentmethodsSometimeyoumayenduprepeatingthepreviouscommandsforvarious

reasons.Followingarethe4differentwaystorepeatthelastexecuted

command.

1.Usethe uparrow toviewthepreviouscommandandpressenter

toexecuteit.

2. Type!! andpressenterfromthecommandline

3. Type!-1 andpressenterfromthecommandline.

4. PressControl+P willdisplaythepreviouscommand,pressenter

toexecuteit

Hack53.ExecuteaspecificcommandfromhistoryInthefollowingexample,Ifyouwanttorepeatthecommand#4,execute!4

asshownbelow.

#history

1

2

3

4

|more

servicenetworkrestart

exit

id

cat/etc/redhat-release

# !4

cat/etc/redhat-release

Fedorarelease9(Sulphur)

72

Page 100: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack54.ExecutepreviouscommandthatstartswithaspecificwordType!followedbythestartingfewlettersofthecommandthatyouwould

liketore-execute.Inthefollowingexample,typing!psandenter,executed

thepreviouscommandstartingwithps,whichis‘psaux|grepyp’.

# !ps

psaux|grepyp

root169470.0

Sl13:100:00ypbind

root175030.0

S+19:190:00grepyp

0.1

0.0

36516

4124

1264?

740pts/0

Hack55.ControlthetotalnumberoflinesinthehistoryusingHISTSIZEAppendthefollowingtwolinestothe.bash_profileandrelogintothebash

shellagaintoseethechange.Inthisexample,only450commandwillbe

storedinthebashhistory.

#vi~/.bash_profile

HISTSIZE=450

HISTFILESIZE=450

Hack56.ChangethehistoryfilenameusingHISTFILEBydefault,historyisstoredin~/.bash_historyfile.Addthefollowinglineto

the.bash_profileandrelogintothebashshell,tostorethehistorycommand

Page 101: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

in.commandline_warriorfileinsteadof.bash_historyfile.I’myettofigure

outapracticaluseforthis.Icanseethisgettingusedwhenyouwanttotrack

commandsexecutedfromdifferentterminalsusingdifferenthistoryfile

name.

73

Page 102: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

#vi~/.bash_profile

HISTFILE=/root/.commandline_warrior

Hack57.EliminatethecontinuousrepeatedentryfromhistoryusingHISTCONTROLInthefollowingexamplepwdwastypedthreetimes,whenyoudohistory,

youcanseeallthe3continuousoccurrencesofit.Toeliminateduplicates,

setHISTCONTROLtoignoredupsasshownbelow.

#pwd

#pwd

#pwd

#history|tail-4

44pwd

45pwd

46pwd

47history|tail-4

[Note:Therearethreepwdcommandsinhistory,after

executingpwd3timesasshownabove]

# exportHISTCONTROL=ignoredups

#pwd

#pwd

#pwd

#history|tail-3

56exportHISTCONTROL=ignoredups

57pwd

58history|tail-4

74

Page 103: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

[Note:Thereisonlyonepwdcommandinthehistory,

evenafterexecutingpwd3timesasshownabove]

Hack58.EraseduplicatesacrossthewholehistoryusingHISTCONTROLTheignoredupsshownaboveremovesduplicatesonlyiftheyareconsecutive

commands.Toeliminateduplicatesacrossthewholehistory,setthe

HISTCONTROLtoerasedupsasshownbelow.

# exportHISTCONTROL=erasedups

#pwd

#servicehttpdstop

#history|tail-3

38pwd

39servicehttpdstop

40history|tail-3

#ls-ltr

#servicehttpdstop

#history|tail-6

35exportHISTCONTROL=erasedups

36pwd

37history|tail-3

38ls–ltr

39servicehttpdstop

40history|tail-6

[Note:Thepreviousservicehttpdstopafterpwdgot

erased]

75

Page 104: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack59.ForcehistorynottorememberaparticularcommandusingHISTCONTROLWhenyouexecuteacommand,youcaninstructhistorytoignorethe

commandbysettingHISTCONTROLtoignorespaceANDtypingaspaceinfront

ofthecommandasshownbelow.Icanseelotofjuniorsysadminsgetting

excitedaboutthis,astheycanhideacommandfromthehistory.

Itisgoodtounderstandhowignorespaceworks.But,asabestpractice,don’t

hidepurposefullyanythingfromhistory.

# exportHISTCONTROL=ignorespace

#ls–ltr

#pwd

#

servicehttpdstop

[Note:Thereisaspaceatthebeginningofservice,

toignorethiscommandfromhistory]

#history|tail-3

67ls–ltr

68pwd

69history|tail-3

Hack60.Clearalltheprevioushistoryusingoption-cSometimeyoumaywanttoclearalltheprevioushistory.Howeveryoumay

stillwanttokeepthehistorymovingforward.

# history-c

76

Page 105: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack61.SubstitutewordsfromhistorycommandsWhenyouaresearchingthroughhistory,youmaywanttoexecuteadifferent

commandbutusethesameparameterfromthecommandthatyou’vejust

searched.

Intheexamplebelow,the!!:$nexttothevicommandgetstheargument

fromthepreviouscommandtothecurrentcommand.

#lsanaconda-ks.cfg

anaconda-ks.cfg

#vi!!:$

vianaconda-ks.cfg

Intheexamplebelow,the!^nexttothevicommandgetsthefirstargument

fromthepreviouscommand(i.ecpcommand)tothecurrentcommand(i.evi

command).

#cpanaconda-ks.cfganaconda-ks.cfg.bak

anaconda-ks.cfg

#vi!^

vianaconda-ks.cfg

Hack62.SubstituteaspecificargumentforaspecificcommandIntheexamplebelow,!cp:2searchesforthepreviouscommandinhistory

thatstartswithcpandtakesthesecondargumentofcpandsubstitutesitfor

thels-lcommandasshownbelow.

77

Page 106: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

#cp~/longname.txt/really/a/very/long/path/long-

filename.txt

#ls-l!cp:2

ls-l/really/a/very/long/path/long-filename.txt

Intheexamplebelow,!cp:$searchesforthepreviouscommandinhistory

thatstartswithcpandtakesthelastargument(inthiscase,whichisalsothe

secondargumentasshownabove)ofcpandsubstitutesitforthels-l

commandasshownbelow.

#ls-l!cp:$

ls-l/really/a/very/long/path/long-filename.txt

Hack63.DisabletheusageofhistoryusingHISTSIZEIfyouwanttodisablehistoryalltogetheranddon’twantbashshellto

rememberthecommandsyou’vetyped,settheHISTSIZEto0asshownbelow.

# exportHISTSIZE=0

#history

#[Note:

Historydidnotdisplayanything]

Hack64.IgnorespecificcommandsfromthehistoryusingHISTIGNORESometimesyoumaynotwanttoclutteryourhistorywithbasiccommands

suchaspwdandls.UseHISTIGNOREtospecifyallthecommandsthatyou

wanttoignorefromthehistory.

78

Page 107: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

PleasenotethataddinglstotheHISTIGNOREignoresonlylsandnotls-l.So,

youhavetoprovidetheexactcommandthatyouwouldliketoignorefrom

thehistory.

# exportHISTIGNORE=”pwd:ls:ls–ltr:”

#pwd

#ls

#ls-ltr

#servicehttpdstop

#history|tail-3

79exportHISTIGNORE=”pwd:ls:ls-ltr:”

80servicehttpdstop

81history

[Note:Historydidnotdisplaypwdandls]

79

Page 108: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter9:SystemAdministrationTasksHack65.PartitionusingfdiskAfteryou’veinstalledbrandnewdisksonyourserver,youhavetousetools

likefdisktopartitionitaccordingly.

Followingarethe5typicalactions(commands)thatyoucanexecuteinside

fdisk.

o

o

o

o

o

n–NewPartitioncreation

d–Deleteanexistingpartition

p-PrintPartitionTable

w–Writethechangestothepartitiontable.i.esave.

q–Quitthefdiskutility

CreateapartitionInthefollowingexample,Icreateda/dev/sda1primarypartition.

# fdisk/dev/sda

DevicecontainsneitheravalidDOSpartitiontable,

norSun,SGIorOSFdisklabelBuildinganewDOS

disklabel.Changeswillremaininmemoryonly,

untilyoudecidetowritethem.Afterthat,ofcourse,

thepreviouscontentwon’tberecoverable.

Thenumberofcylindersforthisdiskissetto34893.

Thereisnothingwrongwiththat,butthisislarger

than1024,andcouldincertainsetupscauseproblems

with:

Page 109: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

80

Page 110: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

1)softwarethatrunsatboottime(e.g.,oldversions

ofLILO)

2)bootingandpartitioningsoftwarefromotherOSs

(e.g.,DOSFDISK,OS/2FDISK)

Warning:invalidflag0x0000ofpartitiontable4will

becorrectedbyw(rite)

Command(mforhelp): p

Disk/dev/sda:287.0GB,287005343744bytes

255heads,63sectors/track,34893cylinders

Units=cylindersof16065*512=8225280bytes

DeviceBoot

Start

End

Blocks

Id

System

Command(mforhelp): n

Commandaction

eextended

pprimarypartition(1-4)

p

Partitionnumber(1-4): 1

Firstcylinder(1-34893,default1):

Usingdefaultvalue1

Lastcylinderor+sizeor+sizeMor+sizeK(1-34893,

default34893):

Usingdefaultvalue34893

Command(mforhelp): w

Thepartitiontablehasbeenaltered!

Page 111: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

Callingioctl()tore-readpartitiontable.

Syncingdisks.

Verifythatthepartitiongotcreatedsuccessfully# fdisk/dev/sda

Thenumberofcylindersforthisdiskissetto34893.

Thereisnothingwrongwiththat,butthisislarger

than1024,andcouldincertainsetupscauseproblems

with:

81

Page 112: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

1)softwarethatrunsatboottime(e.g.,oldversions

ofLILO)

2)bootingandpartitioningsoftwarefromotherOSs

(e.g.,DOSFDISK,OS/2FDISK)

Command(mforhelp):p

Disk/dev/sda:287.0GB,287005343744bytes

255heads,63sectors/track,34893cylinders

Units=cylindersof16065*512=8225280bytes

DeviceBoot

/dev/sda1

Start

1

End

34893

Blocks

280277991

Id

83

System

Linux

Command(mforhelp): q

Hack66.Formatapartitionusingmke2fskAfterpartitioningthedisks,itisstillnotreadyforusage,asweneedto

formatthedisk.Atthisstage,ifyoutrytoviewthediskinformation,itwill

givethefollowingerrormessageindicatingthatnovalidsuperblockis

present.

# tune2fs-l/dev/sda1

tune2fs1.35(28-Feb-2004)

tune2fs:Badmagicnumberinsuper-blockwhiletrying

Page 113: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

toopen/dev/sda1

Couldn’tfindvalidfilesystemsuperblock.

Toformatthedisk,usemke2fsasshownbelow.

# mke2fs/dev/sda1

Youcanalsopassthefollowingoptionalparametertothemke2fs.

82

Page 114: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

AjayKumarTiwari

-m0:reserved-blocks-percentage–Thisindicatesthe

percentageofthefilesystemblocksreservedfortherootuser.

Defaultis5%.Inthefollowingexample,itissetto0.

-b4096:block-sizespecifiedinbytes.Validvaluesare1024,

2048and4096bytesperblock.

o

# mke2fs-m0-b4096/dev/sda1

mke2fs1.35(28-Feb-2004)

Filesystemlabel=

OStype:Linux

Blocksize=4096(log=2)

Fragmentsize=4096(log=2)

205344inodes,70069497blocks

0blocks(0.00%)reservedforthesuperuser

Firstdatablock=0

Maximumfilesystemblocks=71303168

2139blockgroups

32768blockspergroup,32768fragmentspergroup

96inodespergroup

Superblockbackupsstoredonblocks:

32768,98304,163840,229376,294912,819200,884736,

1605632,2654208,4096000,7962624,11239424,20480000,

23887872

Writinginodetables:done

Writingsuperblocksandfilesystemaccounting

information:done

Thisfilesystemwillbeautomaticallycheckedevery32

mountsor180days,whichevercomesfirst.Usetune2fs

Page 115: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

-cor-itooverride.

Theabovecommandwillcreateanext2filesystem.Tocreateanext3file

systemdothefollowing:

# mkfs.ext3/dev/sda1

# mke2fs–j/dev/sda1

83

Page 116: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack67.MountthepartitionAftercreatingapartitionandformatting,youcanmountittoamountpoint.

Firstcreateadirectorywherethepartitionshouldbemounted.

#mkdir/home/database

Mountthefilesystem.

# mount/dev/sda1/home/database

Toautomaticallymountthefilesystemafterthereboot,addthefollowing

entrytothe/etc/fstab

/dev/sdaa/home/databaseext3defaults02

Hack68.Finetunethepartitionusingtune2fsUsethetune2fs–l/dev/sda1toviewthefilesysteminformationasshown

below.

# tune2fs-l/dev/sda1

tune2fs1.35(28-Feb-2004)

Filesystemvolumename:/home/database

Lastmountedon:<notavailable>

FilesystemUUID:f1234556-e123-1234-abcd-

bbbbaaaaae11

Filesystemmagicnumber:0xEF44

Filesystemrevision#:1(dynamic)

Filesystemfeatures:resize_inodefiletype

sparse_super

Defaultmountoptions:(none)

Filesystemstate:notclean

84

Page 117: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

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:

ccccc123292b

Page 118: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

AjayKumarTiwari

Continue

Linux

1094912

140138994

0

16848481

1014969

0

2048

2048

512

16384

16384

128

8

TueJul100:06:032008

ThuAug2105:58:252008

FriJan215:40:362009

2

20

TueJul100:06:032008

15552000(6months)

SatDec2723:06:032008

0(userroot)

0(grouproot)

11

128

tea

12345829-1236-4123-9aaa-

Youcanalsousethetune2fstotunetheex2/ext3filesystemparameter.For

example,ifyouwanttochangetheFilesystemvolumename,youcandoitas

Page 119: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

shownbelow.

# tune2fs-l/dev/sda1|grepvolume

Filesystemvolumename:/home/database

# tune2fs-Ldatabase-home/dev/emcpowera1

tune2fs1.35(28-Feb-2004)

85

Page 120: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

# tune2fs-l/dev/sda1|grepvolume

Filesystemvolumename:database-home

Hack69.Createaswapfilesystem.Createafileforswapusageasshownbelow.

# ddif=/dev/zeroof=/home/swap-fsbs=1Mcount=512

512+0recordsin

512+0recordsout

#ls-l/home/swap-fs

-rw-r—r—1rootroot536870912Jan

/home/swap-fs

223:13

UsemkswaptosetupaLinuxswapareainthe/home/swap-fsfilethatwas

createdabove.

# mkswap/home/swap-fs

Settingupswapspaceversion1,size=536866kB

OncethefileiscreatedandhasbeensetupforLinuxswaparea,itistimeto

enabletheswapusingswaponasshownbelow.

# swapon/home/swap-fs

Addthefollowinglineto/etc/fstabandrebootthesystemfortheswapto

takeintoeffect.

/home/swap-fsswapswapdefaults00

86

Page 121: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack70.CreateanewuserAddanewuser–BasicmethodSpecifyonlytheusername.

#useraddjsmith

AddanewuserwithadditionalParameterYoucanalsospecifythefollowingparametertotheuseradd

o

o

-c:Descriptionabouttheuser.

-e:expirydateoftheuserinmm/dd/yyformat

# adduser-c“JohnSmith-OracleDeveloper”-e

12/31/09jsmith

Verifythattheusergotaddedsuccessfully.

#grepjsmith/etc/passwd

jsmith:x:510:510:JohnSmith-Oracle

Developer:/home/jsmith:/bin/bash

Changetheuserpassword.# passwdjsmith

Changingpasswordforuserjsmith.

NewUNIXpassword:

BADPASSWORD:itisbasedonadictionaryword

RetypenewUNIXpassword:

passwd:allauthenticationtokensupdatedsuccessfully.

87

Page 122: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Note: Makesuretofollowthesebestpracticestocreateastrongpassword

fortheuser.

Howtoidentifythedefaultvaluesusedbyuseradd?Followingarethedefaultvaluesthatwillbeusedwhenanuseriscreated.

# useradd–D

GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/bash

SKEL=/etc/skel

Hack71.CreateanewgroupandassigntoanuserCreateanewdevelopergroup.#groupadddevelopers

Validatethatthegroupwascreatedsuccessfully.#grepdeveloper/etc/group

developers:x:511:

Addanusertoanexistinggroup.Youcannotuseuseraddtomodifyanexistinguser,asyou’llgetthefollowing

errormessage.

88

Page 123: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack#useradd-Gdevelopersjsmith

useradd:userjsmithexists

#usermod-gdevelopersjsmith

AjayKumarTiwari

Validatetheusersgroupwasmodifiedsuccessfully.#grepjsmith/etc/passwd

jsmith:x:510:511:Oracle

Developer:/home/jsmith:/bin/bash

# idjsmith

uid=510(jsmith)gid=511(developers)

groups=511(developers)

#grepjsmith/etc/group

jsmith:x:510:

developers:x:511:jsmith

Hack72.SetupSSHpasswordlesslogininOpenSSHYoucanlogintoaremoteLinuxserverwithoutenteringpasswordin3simple

stepsusingssky-keygenandssh-copy-idasexplainedinthisexample.

ssh-keygencreatesthepublicandprivatekeys.ssh-copy-idcopiesthelocal-

host’spublickeytotheremote-host’sauthorized_keysfile.ssh-copy-idalso

assignsproperpermissiontotheremote-host’shome,~/.ssh,and

~/.ssh/authorized_keys.

Step1:Createpublicandprivatekeysusingssh-key-genonlocal-

hostjsmith@local-host$ ssh-keygen

Generatingpublic/privatersakeypair.

89

Page 124: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Enterfileinwhichtosavethekey

(/home/jsmith/.ssh/id_rsa):[Enterkey]

Enterpassphrase(emptyfornopassphrase):[Press

enterkey]

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:f9

jsmith@local-host

Step2:Copythepublickeytoremote-hostusingssh-copy-idjsmith@local-host$ ssh-copy-id-i~/.ssh/id_rsa.pub

remote-host

jsmith@remote-host’spassword:

Nowtryloggingintothemachine,with“ssh‘remote-

host’”,andcheckin:

.ssh/authorized_keystomakesurewehaven’tadded

extrakeysthatyouweren’texpecting.

Note: ssh-copy-idappendsthekeystotheremote-host’s

.ssh/authorized_key.

Step3:Logintoremote-hostwithoutenteringthepasswordjsmith@local-host$ sshremote-host

Lastlogin:SunNov1617:22:332008from192.168.1.2

[Note:SSHdidnotaskforpassword.]

jsmith@remote-host$[Note:Youareonremote-hosthere]

90

Page 125: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack73.Usessh-copy-idalongwithssh-agentUsingssh-copy-idalongwiththessh-add/ssh-agentWhennovalueispassedfortheoption-iandIf~/.ssh/identity.pubisnot

available,ssh-copy-idwilldisplaythefollowingerrormessage.

jsmith@local-host$ssh-copy-id-iremote-host

/usr/bin/ssh-copy-id:ERROR:Noidentitiesfound

Ifyouhaveloadedkeystothessh-agentusingthessh-add,thenssh-copy-id

willgetthekeysfromthessh-agenttocopytotheremote-host.i.e,itcopies

thekeysprovidedbyssh-add-Lcommandtotheremote-host,whenyoudon’t

passoption-itothessh-copy-id.

jsmith@local-host$ssh-agent$SHELL

jsmith@local-host$ssh-add-L

Theagenthasnoidentities.

jsmith@local-host$ssh-add

Identityadded:/home/jsmith/.ssh/id_rsa

(/home/jsmith/.ssh/id_rsa)

jsmith@local-host$ssh-add-L

ssh-rsa

AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79D

aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow==

/home/jsmith/.ssh/id_rsa

jsmith@local-host$ssh-copy-id-iremote-host

jsmith@remote-host’spassword:

Nowtryloggingintothemachine,with“ssh‘remote-

host’”,andcheckin:.ssh/authorized_keystomakesure

wehaven’taddedextrakeysthatyouweren’texpecting.

[Note:Thishasaddedthekeydisplayedbyssh-add-L]

91

Page 126: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack

ThreeMinorAnnoyancesofssh-copy-idAjayKumarTiwari

Followingarefewminorannoyancesofthessh-copy-id.

1.Defaultpublickey:ssh-copy-iduses~/.ssh/identity.pubasthe

defaultpublickeyfile(i.ewhennovalueispassedtooption-i).

Instead,Iwishitusesid_dsa.pub,orid_rsa.pub,oridentity.pub

asdefaultkeys.i.eIfanyoneofthemexist,itshouldcopythat

totheremote-host.Iftwoorthreeofthemexist,itshouldcopy

identity.pubasdefault.

2.Theagenthasnoidentities:Whenthessh-agentisrunningand

thessh-add-Lreturns“Theagenthasnoidentities”(i.enokeys

areaddedtothessh-agent),thessh-copy-idwillstillcopythe

message“Theagenthasnoidentities”totheremote-host’s

authorized_keysentry.

3.Duplicateentryinauthorized_keys:Iwishssh-copy-idvalidates

duplicateentryontheremote-host’sauthorized_keys.Ifyou

executessh-copy-idmultipletimesonthelocal-host,itwillkeep

appendingthesamekeyontheremote-host’sauthorized_keys

filewithoutcheckingforduplicates.Evenwithduplicateentries

everythingworksasexpected.But,Iwouldliketohavemy

authorized_keysfileclutterfree.

Hack74.CrontabUsingcronyoucanexecuteashell-scriptorLinuxcommandsataspecific

timeanddate.Forexampleasysadmincanscheduleabackupjobthatcan

runeveryday.

Howtoaddajobtothecron?#crontab–e

05***/root/bin/backup.sh

92

Page 127: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Thiswillexecute/root/bin/backup.shat5a.meveryday.

DescriptionofCronfields.Followingistheformatofthecrontabfile.{minute}{hour}{day-of-month}{month}{day-of-week}{full-path-to-shell-script}

o

o

o

o

o

minute:Allowedrange0–59

hour:Allowedrange0–23

day-of-month:Allowedrange0–31

month:Allowedrange1–12.1=January.12=December.

Day-of-week:Allowedrange0–7.Sundayiseither0or7.

Crontabexamples1.Runat12:01a.m.1minuteaftermidnighteveryday.Thisisagoodtimeto

runbackupwhenthesystemisnotunderload.

10***/root/bin/backup.sh

2.Runbackupeveryweekday(Mon–Fri)at11:59p.m.

5911**1,2,3,4,5/root/bin/backup.sh

Followingwillalsodothesame.

5911**1-5/root/bin/backup.sh

93

Page 128: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

3.Executethecommandevery5minutes.

*/5****/root/bin/check-status.sh

4.Executeat1:10p.mon1stofeverymonth

10131**/root/bin/full-backup.sh

5.Execute11p.monweekdays.

023**1-5/root/bin/incremental-backup.sh

CrontabOptionsFollowingaretheavailableoptionswithcrontab:

o

crontab–e:Editthecrontabfile.Thiswillcreateacrontab,ifit

doesn’texist

crontab–l:Displaythecrontabfile.

crontab-r:Removethecrontabfile.

crontab-ir:Thiswillpromptuserbeforedeletingacrontab.

o

o

o

Hack75.SafeRebootOfLinuxUsingMagicSysRqKeyThemagicSysRqkeyisakeycombinationintheLinuxkernelwhichallows

theusertoperformvariouslowlevelcommandsregardlessofthesystem’s

state.

Itisoftenusedtorecoverfromfreezes,ortorebootacomputerwithout

corruptingthefilesystem.Thekeycombinationconsistsof

94

Page 129: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Alt+SysRq+commandkey.InmanysystemstheSysRqkeyistheprintscreen

key.

First,youneedtoenabletheSysRqkey,asshownbelow.

echo“1”>/proc/sys/kernel/sysrq

ListofSysRqCommandKeysFollowingarethecommandkeysavailableforAlt+SysRq+commandkey.

o

o

o

‘k’–Killsalltheprocessrunningonthecurrentvirtualconsole.

’s’–Thiswillattempttosyncallthemountedfilesystem.

‘b’-Immediatelyrebootthesystem,withoutunmountingpartitions

orsyncing.

‘e’–SendsSIGTERMtoallprocessexceptinit.

‘m’–Outputcurrentmemoryinformationtotheconsole.

‘i’-SendtheSIGKILLsignaltoallprocessesexceptinit

‘r’-Switchthekeyboardfromrawmode(themodeusedbyprograms

suchasX11),toXLATEmode.

’s’–syncallmountedfilesystem.

‘t’-Outputalistofcurrenttasksandtheirinformationtothe

console.

‘u’-Remountallmountedfilesystemsinreadonlymode.

‘o’–Shutdownthesystemimmediately.

‘p’–Printthecurrentregistersandflagstotheconsole.

‘0-9′-Setstheconsoleloglevel,controllingwhichkernelmessages

willbeprintedtoyourconsole.

o

o

o

Page 130: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

o

o

o

o

o

o

o

95

Page 131: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

o

AjayKumarTiwari

‘f’-Willcalloom_killtokillprocesswhichtakesmorememory.

‘h’–Usedtodisplaythehelp.Butanyotherkeysthantheabove

listedwillprinthelp.

Wecanalsodothisbyechoingthekeystothe/proc/sysrq-triggerfile.For

example,tore-bootasystemyoucanperformthefollowing.

echo“b”>/proc/sysrq-trigger

PerformaSaferebootofLinuxusingMagicSysRqKeyToperformasaferebootofaLinuxcomputerwhichhangsup,dothe

following.Thiswillavoidthefsckduringthenextre-booting.i.ePress

Alt+SysRq+letterhighlightedbelow.

o

o

unRaw(takecontrolofkeyboardbackfromX11,

tErminate(sendSIGTERMtoallprocesses,allowingthemtoterminate

gracefully),

kIll (sendSIGILLtoallprocesses,forcingthemtoterminate

immediately),

Sync (flushdatatodisk),

Unmount (remountallfilesystemsread-only),

reBoot.

o

o

o

o

96

Page 132: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter10:ApachectlandHttpdExamplesAfteryouhaveinstalledApache2,ifyouwanttouseapachectlandhttpdto

it’smaximumpotential,youshouldgobeyondusingstart,stopandrestart.

The9practicalexamplesprovidedinthischapterwillhelpyoutouse

apachectlandhttpdveryeffectively.

ApachectlactsasSysVinitscript,takingargumentslikestart,stop,restart

andstatus.Italsoactsasfront-endtohttpdcommand,bysimplypassingthe

commandlineargumentstohttpd.So,allthecommandsyouexecuteusing

apachectl,canalsobeexecuteddirectlybycallinghttpd.

Ifyoudon’thaveApache,refertothetutorials:installapachefromsourceor

installLAMPstackusingyum.

Hack76.Passdifferenthttpd.conffilenametoapachectlTypicallyyou’llmodifytheoriginalhttpd.conftotryoutdifferentApache

directives.Ifsomethingdoesn’tworkout,you’llrevertbackthechanges.

Insteadofplayingaroundwiththeoriginalhttpd.conf,copyittoanew

httpd.conf.debugandusethisnewhttpd.conf.debugfilewithApachefor

testingpurposeasshownbelowusingoption-f.

# apachectl-fconf/httpd.conf.debug

# httpd-kstart-fconf/httpd.conf.debug

[Note:youcanuseeitherapachectlorhttpdasshown

above]

#ps-ef|grephttp

root250801023:2600:00:00/usr/sbin/httpd-f

97

Page 133: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

conf/httpd.conf.debug

apache2509925080023:2800:00:00/usr/sbin/httpd-f

conf/httpd.conf.debug

[Note:psshowsthehttpdrunningwithhttpd.conf.debug

file]

OnceyouaresatisfiedwiththechangesandApacherunswithoutanyproblem

withhttpd.conf.debug,youcancopythechangestohttpd.confandstartthe

Apachenormallyasshownbelow.

#cphttpd.conf.debughttpd.conf

#apachectlstop

#apachectlstart

#ps-ef|grephttpd

root251141023:2800:00:00/usr/sbin/httpd

-kstart

daemon2511525114023:2800:00:00/usr/sbin/httpd

-kstart

[Note:psindicatesthatthehttpdisrunningusingthe

defaultconfigfile]

Hack77.UseatemporaryDocumentRootwithoutmodifyinghttpd.confThisisveryhelpful,whenyouaretryingoutdifferentlayoutforyourwebsite

anddon’twanttomodifytheoriginalfilesunderthedefaultDocumentRoot.

TakeacopyofyouroriginalDocumentRootdirectory(/var/www/html)toa

newtemporaryDocumentRootdirectory(/var/www/html_debug).Makeall

yourchangesunderthistemporaryDocumentRootdirectory

(/var/www/html_debug)andstarttheApachewiththistemporarydirectory

asshownbelowusingoption-c.

98

Page 134: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

# httpd-kstart-c“DocumentRoot/var/www/html_debug/”

Ifyouwanttogobacktooriginalconfigurationusingthedefault

DocumentRoot(/var/www/html),simplyrestarttheApacheasshownbelow.

#httpd-kstop

#apachectlstart

Hack78.IncreasetheLogLeveltemporarilyWhileyouaredebugginganissue,youcanchangetheLogLeveloftheApache

temporarily,withoutmodifyingtheLogLeveldirectiveinthehttpd.confas

shownbelowusingoption-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_module

Possiblevaluesyoucanpasstooption-eare:

o

o

o

o

o

o

o

o

debug

info

notice

warn

error

crit

Page 135: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

alert

emerg

99

Page 136: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack79.DisplaythemodulesinsideApacheDisplaythemodulescompiledinsideApache# httpd-l

Compiledinmodules:

core.c

prefork.c

http_core.c

mod_so.c

DisplaybothstaticanddynamicmoduleloadedbyApacheWhenyoupassoption-l,tohttpd,itwilldisplayonlythestaticmodules.

Passingoption-M,willdisplaybothstaticandsharedmodulesasshown

below.

# httpd–M

LoadedModules:

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)

SyntaxOK

100

Page 137: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack80.Showallaccepteddirectivesinsidehttpd.confThisislikeanextendedhelpforhttpd,whichwilldisplayallthehttpd.conf

directivesandtheplaceswheretheyarevalid.Foraspecificdirective,it

tellsallthepossiblevaluesandwhereitcanbeusedinsidethehttpd.conf.

Thiscanbeveryhelpful,whenyouwanttoquicklyknowaboutaparticular

Apachedirective.

# httpd–L

HostnameLookups(core.c)

“on”toenable,“off”todisablereverseDNSlookups,

or“double”toenabledouble-reverseDNSlookups

Allowedin*.confanywhere

ServerLimit(prefork.c)

MaximumvalueofMaxClientsforthisrunofApache

Allowedin*.confonlyoutside<Directory>,<Files>or

<Location>

KeepAlive(http_core.c)

WhetherpersistentconnectionsshouldbeOnorOff

Allowedin*.confonlyoutside<Directory>,<Files>or

<Location>

LoadModule(mod_so.c)

amodulenameandthenameofasharedobjectfileto

loaditfrom

Allowedin*.confonlyoutside<Directory>,<Files>or

<Location>

Hack81.Validatethehttpd.confaftermakingchangesUseoption-ttovalidatewhetherthereareanyissueswithaspecificApache

Page 138: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

configurationfile.Intheexampleshownbelow,itdisplaysthatthereisa

101

Page 139: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

problematline148inthehttpd.conf.debug.mod_auth_basicsoismissinga.

(period)beforetheso.

# httpd-t-fconf/httpd.conf.debug

httpd:Syntaxerroronline148of

/etc/httpd/conf/httpd.conf.debug:

Cannotload/etc/httpd/modules/mod_auth_basicsointo

server:

/etc/httpd/modules/mod_auth_basicso:cannotopenshared

objectfile:Nosuchfileordirectory

Onceyoufixtheissue,itwilldisplaySyntaxOK.

# httpd-t-fconf/httpd.conf.debug

SyntaxOK

Hack82.DisplaythehttpdbuildparametersUseoption-V(upper-caseV),todisplayApacheversionnumberandallthe

parametersthatareusedwhilebuildingtheApache.

# httpd–V

Serverversion:Apache/2.2.9(Unix)

Serverbuilt:Jul14200815:36:56

Server’sModuleMagicNumber:20051115:15

Serverloaded:APR1.2.12,APR-Util1.2.12

Compiledusing:APR1.2.12,APR-Util1.2.12

Architecture:32-bit

ServerMPM:Prefork

threaded:no

forked:yes(variableprocesscount)

Servercompiledwith….

-DAPACHE_MPM_DIR=”server/mpm/prefork”

-DAPR_HAS_SENDFILE

-DAPR_HAS_MMAP

Page 140: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

102

Page 141: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack-D

-D

-D

-D

-D

-D

-D

-D

-D

-D

-D

-D

-D

-D

-D

AjayKumarTiwari

APR_HAVE_IPV6(IPv4-mappedaddressesenabled)

APR_USE_SYSVSEM_SERIALIZE

APR_USE_PTHREAD_SERIALIZE

SINGLE_LISTEN_UNSERIALIZED_ACCEPT

APR_HAS_OTHER_CHILD

AP_HAVE_RELIABLE_PIPED_LOGS

DYNAMIC_MODULE_LIMIT=128

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

Page 142: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

SERVER_CONFIG_FILE=”conf/httpd.conf”

IfyouwantdisplayonlytheApacheversionnumber,usetheoption-v(lower-

casev)asshownbelow.

# httpd-v

Serverversion:Apache/2.2.9(Unix)

Serverbuilt:Jul14200815:36:56

Hack83.LoadaspecificmoduleonlyondemandSometimesyoumaynotwanttoloadallthemodulesintheApache.Fore.g.

YoumaywanttoloadldaprelatedmodulestoApache,onlywhenyouare

testingLDAP.Thiscanbeachievedasshownbelow.

Modifythehttpd.confandaddIfDefinedirectivecalledload-ldap(youcan

namethisanythingyouwant).

<IfDefineload-ldap>

LoadModuleldap_modulemodules/mod_ldap.so

LoadModuleauthnz_ldap_module

103

Page 143: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackmodules/mod_authnz_ldap.so

</IfDefine>

AjayKumarTiwari

WhenyouaretestingldapandwouldliketoLoadtheldaprelatedmodules,

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,toloadtheldapmodulesinto

Apache]

#apachectlstart

[Note:StarttheApachenormally,ifyoudon’twantto

loadtheldapmodules.

104

Page 144: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter11:BashScriptingHack84.ExecutionSequenceof.bash_*filesWhatisthesequenceinwhichthefollowingfilesareexecuted?

o

o

o

o

o

o

/etc/profile

~/.bash_profile

~/.bashrc

~/.bash_login

~/.profile

~/.bash_logout

ExecutionsequenceforinteractiveloginshellFollowingpseudocodeexplainsthesequenceofexecutionofthesefiles.

execute/etc/profile

IF~/.bash_profileexistsTHEN

execute~/.bash_profile

ELSE

IF~/.bash_loginexistTHEN

execute~/.bash_login

ELSE

IF~/.profileexistTHEN

execute~/.profile

ENDIF

ENDIF

Page 145: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

ENDIF

105

Page 146: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Whenyoulogoutoftheinteractiveshell,followingisthesequenceof

execution:

IF~/.bash_logoutexistsTHEN

execute~/.bash_logout

ENDIF

Pleasenotethat/etc/bashrcisexecutedby~/.bashrcasshownbelow:

#cat~/.bashrc

if[-f/etc/bashrc];then

./etc/bashrc

Fi

Executionsequenceforinteractivenon-loginshellWhilelaunchinganon-logininteractiveshell,followingisthesequenceof

execution:

IF~/.bashrcexistsTHEN

execute~/.bashrc

ENDIF

Note: Whenanon-interactiveshellstartsup,itlooksforENVenvironment

variable,andexecutesthefile-namevaluementionedintheENVvariable.

Hack85.HowtogeneraterandomnumberinbashshellUsethe$RANDOMbashbuilt-infunctiontogeneraterandomnumberbetween

0–32767asshownbelow.

$echo$RANDOM

106

Page 147: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack22543

$echo$RANDOM

25387

$echo$RANDOM

647

AjayKumarTiwari

Hack86.DebugashellscriptTodebugashellscriptuseset–xvinsidetheshellscriptatthetop.

Shellscriptwithnodebugcommand:$catfilesize.sh

#!/bin/bash

forfilesizein$(ls-l.|grep“^-”|awk‘{print

$5}’)

do

lettotalsize=$totalsize+$filesize

done

echo“Totalfilesizeincurrentdirectory:$totalsize”

OutputofShellscriptwithnodebugcommand:$./filesize.sh

Totalfilesizeincurrentdirectory:652

ShellscriptwithDebugcommandinside:Addset–xvinsidetheshellscriptnowtodebugtheoutputasshownbelow.

$catfilesize.sh

#!/bin/bash

set-xv

forfilesizein$(ls-l.|grep“^-”|awk‘{print

107

Page 148: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

$5}’)

do

lettotalsize=$totalsize+$filesize

done

echo“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‘Totalfilesizein

Totalfilesizeincurrent

.|grep“^-”|awk

.|grep“^-”|awk

.|grep“^-”|awk

currentdirectory:652’

directory:652

ExecuteShellscriptwithdebugoption:Insteadofgivingtheset–xvinsidetheshellscript,youcanalsoprovidethat

whileexecutingtheshellscriptasshownbelow.

Page 149: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

$bash-xvfilesize.sh

Hack87.Quotingechostatementwithoutanyspecialcharacter.

$echoTheGeekStuff

108

Page 150: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackTheGeekStuff

AjayKumarTiwari

Echostatementwithaspecialcharacter;.semi-colonisacommand

terminatorinbash.Inthefollowingexample,“TheGeek”worksfortheecho

and“Stuff”istreatedasaseparateLinuxcommandandgivescommandnot

found.

$echoTheGeek;Stuff

TheGeek

-bash:Stuff:commandnotfound

Toavoidthisyoucanadda\infrontofsemi-colon,whichwillremovethe

specialmeaningofsemi-colonandjustprintitasshownbelow.

$echoTheGeek\;Stuff

TheGeek;Stuff

SingleQuoteUsesinglequotewhenyouwanttoliterallyprinteverythinginsidethesingle

quote.Eventhespecialvariablessuchas$HOSTNAMEwillbeprintas

$HOSTNAMEinsteadofprintingthenameoftheLinuxhost.

$echo‘Hostname=$HOSTNAME;

Message=\$isUSD’

Hostname=$HOSTNAME;

Message=\$isUSD

CurrentUser=`whoami`;

CurrentUser=`whoami`;

DoubleQuoteUsedoublequoteswhenyouwanttodisplaytherealmeaningofspecial

variables.

$echo“Hostname=$HOSTNAME;

Message=\$isUSD”

CurrentUser=`whoami`;

109

Page 151: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hostname=dev-db;CurrentUser=ramesh;Message=$is

USD

Doublequoteswillremovethespecialmeaningofallcharactersexceptthe

following:

o

o

o

o

o

o

$ParameterSubstitution.

`Backquotes

\$LiteralDollarSign.

\´LiteralBackquote.

\”EmbeddedDoublequote.

\EmbeddedBackslashes.

Hack88.ReaddatafilefieldsinsideashellscriptThisexampleshowshowtoreadaparticularfieldfromadata-fileand

manipulateitinsideashell-script.Forexample,letusassumethe

employees.txtfileisintheformatof{employee-name}:{employee-

id}:{department-name},withcolondelimitedfileasshownbelow.

$catemployees.txt

EmmaThomas:100:Marketing

AlexJason:200:Sales

MadisonRandy:300:ProductDevelopment

SanjayGupta:400:Support

NishaSingh:500:Sales

Thefollowingshellscriptexplainshowtoreadspecificfieldsfromthis

employee.txtfile.

Page 152: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

$viread-employees.sh

#!/bin/bash

IFS=:

echo“EmployeeNames:”

echo“–––––”

whilereadnameempiddept

110

Page 153: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

do

echo“$nameispartof$deptdepartment”

done<~/employees.txt

Assignexecuteprivilegetotheshellscriptandexecuteit.

$chmodu+xread-employees.sh

$./read-employees.sh

EmployeeNames:

–––––

EmmaThomasispartofMarketingdepartment

AlexJasonispartofSalesdepartment

MadisonRandyispartofProductDevelopmentdepartment

SanjayGuptaispartofSupportdepartment

NishaSinghispartofSalesdepartment

111

Page 154: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Chapter12:SystemMonitoringandPerformanceHack89.Freecommandfreecommanddisplaysallthenecessaryinformationaboutsystemphysical

(RAM)andswapmemory.

Syntax:free[options]

WhatisthetotalRAMonmysystem?Intheexamplebelow,thetotalphysicalmemoryonthissystemis1GB.The

valuesdisplayedbelowareinKB.

#free

totalused

Mem:10346241006696

-/+buffers/cache:

Swap:2031608

freesharedbuffers

279280174136

216668817956

02031608

cached

615892

WhatisthetotalmemoryonmysystemincludingRAMandSwap?Inthefollowingcommand:

o

o

optionmdisplaysthevaluesinMB

optiontdisplaysthe“Total”line,whichissumofphysicalandswap

memoryvalues

Page 155: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

optionoistohidethebuffers/cachelinefromtheaboveexample.

o

# free–mto

112

Page 156: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacktotal

1010

1983

2994

used

983

0

983

free

27

1983

2011

shared

0

AjayKumarTiwari

buffers

170

cached

601

Mem:

Swap:

Total:

Hack90.TopCommandtopcommanddisplaysrealtimeinformationaboutvariousperformance

metricsofthesystemsuchasCPULoad,MemoryUsage,Processeslistetc.

Syntax:top[options]

HowtoviewmycurrentsystemstatusincludingCPUusage?Executetopwithoutanyoptionfromthecommandline,whichwilldisplay

theoutputshownbelow.Thetopcommandoutputwillkeepdisplayingthe

real-timevalues,untilyoupress“Control+c”orqtoexitfromthecommand

Page 157: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

output.

# top

top-13:10:13up171days,20:21,3users,loadaverage:0.01,0.05,0.00

Tasks:194total,1running,193sleeping,0stopped,0zombie

Cpu(s):0.6%us,0.7%sy,0.0%ni,98.7%id,0.0%wa,0.0%hi,0.0%si

Mem:1034624ktotal,1007420kused,27204kfree,174540kbuffers

Swap:2031608ktotal,0kused,2031608kfree,615904kcached

PIDUSERPRNIVIRTRES

11912apache15031828

19299oracle190279m

11398jsmith160107m

SHRS%CPU%MEMTIME+COMMAND

13m3916S10.20:46.35httpd

18m17mS10.20:00.03oracle

28m6404S00.40:03.07perl

113

Page 158: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Howtoreadtheoutputofthetopcommandshownabove?o

Line1“top”,indicatesthatthesystemhasbeenupandrunning

for171days.

Line2“Tasks”,displaysthetotalnumberofprocessesalongwith

abreakdownofrunning,sleeping,stoppedandzombieprocesses

count.

Line3“Cpu(s)”displaysthecurrentCPUutilizationofthe

system.Inthisexample,CPUis98.7%idle

Line4“Mem”andline5“Swap”providesthememory

information.Thisisthesameinformationfromthefree

command.

Therestofthelinesdisplayalltheactiveprocessesonthe

system,sorteddefaultbyCPUusage(%CPUcolumn).i.ethemost

CPUintensiveprocesseswillbedisplayedonthetopbydefault.

o

o

o

o

Thereareseveralcommandlineoptionsandinteractiveoptionsavailablefor

topcommands.Letusreviewcoupleofessentialoptionsfortopcommand.

Howtoidentifythemostmemoryintensiveprocesses?Whiletheoutputofthetopcommanddisplayed,pressF,whichwilldisplay

thefollowingmessageandshowallfieldsavailableforsorting,pressn(which

isforsortingtheprocessesbyMemory)andpressenter.Thiswilldisplaythe

processesinthetopoutputsortedbymemoryusage.

CurrentSortField:Kforwindow1:Def

Selectsortfieldviafieldletter,typeanyotherkeytoreturn

Howtoaddadditionalfields(fore.g.CPUTime)tothetopoutput?

Page 159: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

114

Page 160: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Whilethetopcommandisrunning,pressf,whichwilldisplaythefollowing

messageandshowallfieldsavailablefordisplay,pressl,whichwilladdthe

CPUTimetothedisplaycolumnsinthetopoutput.

CurrentFields:AEHIOQTWKNMbcdfgjplrsuvyzXforwindow1:Def

Togglefieldsviafieldletter,typeanyotherkeytoreturn

Howtogetthefullpathnameandparametersoftherunning

processes?Whilethetopcommandisrunning,pressc,whichwilldisplayfullpathname

ofrunningprocessesasshownbelowinthecommandcolumn.i.eInsteadof

httpd,itdisplays/usr/local/apache2/bin/httpd.

PIDUSER

PRNIVIRTRESSHRS%CPU%MEM

1

TIME+COMMAND

0.20:46.35

11912apache1503182813m3916S

/usr/local/apache2/bin/httpd

HowtoviewtheindividualCPUsinthetopcommand?Whilethetopcommandisrunning,press1(numberone),whichwilldisplay

theperformancedataoftheindividualCPUsonthatmachineasshown

below.

top-13:10:13up171days,20:21,3users,loadaverage:0.01,0.05,0.00

Tasks:194total,1running,193sleeping,0stopped,0zombie

Cpu0:10.2%us,2.6%sy,0.0%ni,86.8%id,0.3%wa,0.0%hi,0.0%si

Cpu1:9.6%us,8.0%sy,0.0%ni,82.4%id,0.0%wa,0.0%hi,0.0%si

Cpu2:1.3%us,1.3%sy,0.0%ni,95.0%id,2.3%wa,0.0%hi,0.0%si

Cpu3:0.0%us,0.0%sy,0.0%ni,100.0%id,0.0%wa,0.0%hi,0.0%si

115

Page 161: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Mem:1034624ktotal,1007420kused,27204kfree,174540kbuffers

Swap:2031608ktotal,0kused,2031608kfree,615904kcached

Hack91.PsCommandpscommand(processstatus)willdisplaysnapshotinformationofallactive

processes.

Syntax:ps[options]

Howtodisplayalltheprocessesrunninginthesystem?Use“psaux”,asshownbelow.

# psaux|more

USERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMAND

root10.00.02044588?SsJun27

0:00init[5]

apache311860.01.62373617556?SJul26

0:40/usr/local/apache2/bin/httpd

apache311870.01.32064014444?SJul26

0:37/usr/local/apache2/bin/httpd

Youcanalsouse“ps-ef|more”,togetasimilaroutput

PrinttheProcessTreeYoucanuseeither psaxuf or ps–ejH todisplayprocessesinatreeformat.

Thetreestructurewillhelptovisualizetheprocessandit’sparentprocess

immediately.Forclaritypurpose,fewcolumnshavebeencut-offinthe

outputbelow.# psaxuf

116

Page 162: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwariroot

root

root

root

root

root

root

root

root

511

511

Oct14

Oct14

Oct14

Oct14

Oct14

Oct14

Dec03

Dec22

23:35

23:35

0:00/opt/VRTSralus/bin/beremote

0:00\_/opt/VRTSralus/bin/beremote

0:00\_/opt/VRTSralus/bin/beremote

0:00\_/opt/VRTSralus/bin/beremote

0:01\_/opt/VRTSralus/bin/beremote

0:00\_/opt/VRTSralus/bin/beremote

0:01/usr/local/sbin/sshd

1:08/usr/local/sbin/sshd

0:00\_/usr/local/sbin/sshd

0:00\_-bash

\_psaxuf

Note:Youcanalsousepstreecommandtodisplayprocessintreestructure.

ViewProcessesOwnedbyaParticularUser

Page 163: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

ThefollowingcommanddisplaysalltheprocessownedbyLinuxuser-name:

oracle.

$ psUoracle

PIDTTY

5014?

7124?

8206?

8852?

8854?

8911?

STAT

Ss

Ss

Ss

Ss

Ss

Ss

TIMECOMMAND

0:01/oracle/bin/tnslsnr

0:00ora_q002_med

0:00ora_cjq0_med

0:01ora_pmon_med

0:00ora_psp0_med

0:02oraclemed(LOCAL=NO)

ViewProcessesOwnedbyCurrentUserFollowingcommanddisplaysalltheprocessownedbythecurrentuser.

$ psU$USER

PIDTTY

10329?

10330pts/1

STAT

S

Page 164: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

Ss

TIMECOMMAND

0:00sshd:ramesh@pts/1,pts/2

0:00-bash

117

Page 165: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack10354pts/2

10530pts/1

Ss+

R+

0:00-bash

0:00psUramesh

AjayKumarTiwari

Hack92.DfCommanddfcommand(diskfree)displaystheamountoftotalandfreediskspace

availableonthemountedfilesystems.

Syntax:df[options][name]

HowmuchGBofdiskspaceisfreeonmysystem?Usedf-hasshownbelow.Option-hdisplaysthevaluesinhumanreadable

format(forexample:KforKb,MforMbandGforGb).Inthesampleoutput

below,/filesystemhas17GBofdiskspaceavailableand/home/user

filesystemhas70GBavailable.

# df–h

Filesystem

/dev/sda1

/dev/sdb1

Size

64G

137G

UsedAvailUse%Mountedon

44G17G73%/

67G70G49%/home/user

WhattypeoffilesystemdoIhaveonmysystem?Option-TwilldisplaytheinformationaboutthefilesystemType.Inthis

example/and/home/userfilesystemsareext2.Option-awilldisplayallthe

filesystems,includingthe0sizespecialfilesystemusedbythesystem.

Page 166: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

# df-Tha

Filesystem

Type

SizeUsedAvailUse%Mountedon

118

Page 167: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

/dev/sda1ext264G44G17G73%/

/dev/sdb1ext2137G67G70G49%/home/user

noneproc000-/proc

nonesysfs000-/sys

nonedevpts000-/dev/pts

nonetmpfs2.0G02.0G0%/dev/shm

Hack93.KillCommandkillcommandcanbeusedtoterminatearunningprocess.Typicallythis

commandisusedtokillprocessesthatarehangingandnotresponding.

Syntax:kill[options][pids|commands]

Howtokillahangingprocess?First,identifytheprocessidoftheparticularprocessthatyouwouldliketo

killusingthepscommand.Onceyouknowtheprocessid,passitasa

parametertothekillcommand.Theexamplebelowshowshowtokillthe

hangingapachehttpdprocess.Pleasenotethattypicallyyoushoulduse

“apachectlstop”tostopapache.

# psaux|grephttpd

USERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMAND

apache311860.01.62373617556?SJul26

0:40/usr/local/apache2/bin/httpd

apache311870.01.32064014444?SJul26

0:37/usr/local/apache2/bin/httpd

# kill3118631187

119

Page 168: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Pleasenotethattheabovecommandtriestoterminatetheprocessgraciously

bysendingasignalcalledSIGTERM.Iftheprocessdoesnotgetterminated,

youcanforcefullyterminatetheprocessbypassingasignalcalledSIGKILL,

usingtheoption-9asshownbelow.Youshouldeitherbetheownerofthe

processoraprivilegedusertokillaprocess.

# kill-93118631187

Anotherwaytokillmultipleprocesseseasilyisbyaddingthefollowingtwo

functionstothe.bash_profile.functionpsgrep()

{

psaux|grep“$1”|grep-v‘grep’

}

functionpsterm()

{

[${#}-eq0]&&echo“usage:$FUNCNAMESTRING”&&return0

localpid

pid=$(psax|grep“$1”|grep-vgrep|awk‘{print$1}’)

echo-e“terminating‘$1’/process(es):\n$pid”

kill-SIGTERM$pid

}

Nowdothefollowing,toidentifyandkillallhttpdprocesses.# psgrephttp

USER

COMMAND

apache

Jul26

apache

Jul26

PID%CPU%MEM

31186

0:40

31187

0:37

Page 169: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

VSZ

RSSTTY

STATSTART

TIME

S

S

0.01.62373617556?

/usr/local/apache2/bin/httpd

0.01.32064014444?

/usr/local/apache2/bin/httpd

# pstermhttpd

terminating‘httpd’/process(es):

31186

120

Page 170: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack31187

AjayKumarTiwari

Hack94.DuCommandducommand(diskusage)willprintthefilespaceusageforaparticular

directoryanditssubdirectories.

Howmuchspaceistakenbymyhomedirectoryandallits

subdirectories?Inthefollowingexample,option-sstandsforsummaryonly.i.eitdisplays

onlythetotalsizeof/home/jsmithandnottheindividualsizesofallthe

subdirectoriesinsidethe/home/jsmith.Option-hdisplaystheinformationin

ahumanreadableformat.i.eKforKB,MforMBandGforGB.The~

indicatestheuserhomedirectory.Thiscommandissameas“du-sh

/home/jsmith”

# du-sh~

320M/home/jsmith

Togetthesubdirectoriesunder/home/jsmithlisted,executetheabove

commandwithoutthesoption.

Hack95.lsofcommands.Lsofstandsforlsopenfiles,whichwilllistalltheopenfilesinthesystem.

Theopenfilesincludenetworkconnection,devicesanddirectories.The

outputofthelsofcommandwillhavethefollowingcolumns:

o

o

o

COMMANDprocessname.

PIDprocessID

USERUsername

121

Page 171: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHacko

o

o

o

o

o

FDfiledescriptor

TYPEnodetypeofthefile

DEVICEdevicenumber

SIZEfilesize

NODEnodenumber

NAMEfullpathofthefilename.

AjayKumarTiwari

ViewallopenfilesofthesystemExecutethelsofcommandwithoutanyparameterasshownbelow.

# lsof|more

COMMANDPIDUSERFDTYPEDEVICESIZENODE

NAME

init1rootcwdDIR8,140962/

init1rootrtdDIR8,140962/

init1roottxtREG8,132684983101/sbin/init

init1rootmemREG8,1106397166798/lib/ld-

2.3.4.so

init1rootmemREG8,11454802166799

/lib/tls/libc-2.3.4.so

init1rootmemREG8,153736163964

/lib/libsepol.so.1

init1rootmemREG8,156328166811

/lib/libselinux.so.1

init1root10uFIFO0,13972/dev/initctl

Page 172: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

migration2rootcwdDIR8,140962/

skipped…

Thelsofcommandbyitselfwithoutmayreturnlotofrecordsasoutput,

whichmaynotbeverymeaningfulexcepttogiveyouaroughideaabouthow

122

Page 173: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

manyfilesareopeninthesystematanygivenpointofviewasshownbelow.

# lsof|wc-l

3093

ViewopenfilesbyaspecificuserUselsof–uoptiontodisplayallthefilesopenedbyaspecificuser.

# lsof–uramesh

vi7190ramesh

475196/bin/vi

txt

REG

8,1

474608

sshd7163ramesh3uIPv615088263

TCPdev-db:ssh->abc-12-12-12-12.socal.res.rr.com:2631

(ESTABLISHED)

Asystemadministratorcanusethiscommandtogetsomeideaonwhatusers

areexecutingonthesystem.

ListUsersofaparticularfileIfyouliketoviewalltheuserswhoareusingaparticularfile,uselsofas

shownbelow.Inthisexample,itdisplaysalluserswhoarecurrentlyusingvi.#lsof/bin/vi

COMMANDPID

vi7258

vi7300

USERFD

roottxt

rameshtxt

TYPEDEVICESIZENODENAME

REG8,1474608475196/bin/vi

REG8,1474608475196/bin/vi

Page 174: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

123

Page 175: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack96.SarCommandSarcommandscomeswiththesysstatpackage.Makesuresysstatisinstalled.

Ifyoudon’thavesarinstalledonyoursystem,getitfromSysstatproject.

Sarisanexcellentmonitoringtoolthatdisplaysperformancedataofpretty

mucheveryresourceofthesystemincludingCPU,memory,IO,paging,

networking,interruptsetc.,

SarCollects,Reports(displays)andSavestheperformancedata.Letuslook

atallthethreeaspectsseparately

Sadc-Systemactivitydatacollector/usr/lib/sadc(Systemactivitydatacollector)commandcollectsthesystem

dataataspecifiedtimeinterval.Thisusesthedailyactivitydatafilethatis

locatedunder/va/log/sa/sa[dd],whereddisthecurrentday.

Sa1shell-script/usr/lib/sa1in-turncallsthe/usr/lib/sadcs.sa1isinvokedfromthecrontab

asshownbelow.Runthisevery5minutesor15minutesdependingonyour

need.Iprefertoscheduleitforevery5minutesinthecrontabasshown

below.

*/5****root/usr/lib/sa/sa111

Sa2shell-script/usr/lib/sa2isashellscriptthatwillwriteadailyreportinthe

/var/log/sa/sa[dd]file,whereddisthecurrentday.Invokethesa2fromthe

crontabonceadayatmidnight.

#5923***root/usr/lib/sa/sa2–A

124

Page 176: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Note: /etc/cron.d/sysstatfilescomeswiththesysstatpackagethatincludes

somedefaultvalueforthesa1andsa2,whichyoucanchangeaccordingly.

DisplayCPUStatisticsusingSarCommand# sar–u

Linux2.6.9-42.ELsmp(dev-db)

12:00:01AMCPU%user%nice

12:05:01AMall3.700.00

12:10:01AMall4.590.00

12:15:01AMall3.900.00

12:20:01AMall4.060.00

12:25:01AMall3.890.00

12:30:01AMall3.890.00

Skipped..

Average:all

4.56

0.00

1.00

0.15

94.29

01/01/2009

%system%iowait

0.850.00

1.190.06

0.950.04

1.000.01

0.870.00

0.870.00

%idle

95.45

94.16

95.11

94.93

95.23

95.23

Page 177: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

Note: Ifyouneedabreakdownoftheperformancedatafortheindividual

CPU’s,executethefollowingcommand.

#sar-u-PALL

DisplayDiskIOStatisticsusingsarcommand# sar–d

Linux2.6.9-42.ELsmp(dev-db)

12:00:01AMDEV

12:05:01AMdev2-0

12:10:01AMdev8-1

Skipped..

Average:

Average:

dev2-0

dev8-1

4.66

1.89

120.77

3.17

69.45

8.02

01/01/2009

tpsrd_sec/s

1.651.28

4.088.11

wr_sec/s

45.43

21.81

125

Page 178: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

DisplaynetworkingStatisticsusingsarcommand# sar-nDEV|more

Linux2.6.9-42.ELsmp(dev-db)

12:00:01AMIFACErxpck/s

rxcmp/stxcmp/

srxmcst/s

12:05:01AMlo0.17

0.000.0

00.00

12:10:01AMeth052.92

0.000.0

00.00

01/01/2009

txpck/srxbyt/s

txbyt/s

0.16

25.31

23.33

53.64

10169.74

12178.57

# sar-nSOCK|more

Linux2.6.9-42.ELsmp(dev-db)

12:00:01AMtotscktcpsck

12:05:01AM5013

12:10:01AM5013

12:15:01AM5313

01/01/2009

udpsckrawsck

30

40

50

ip-frag

0

Page 179: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

0

0

Hack97.vmstatCommandForatypicalperformancemonitoringallyouneedisonlyvmstatcommand.

Thisdisplaymemory,swap,IO,systemandcpuperformanceinformation.

Thefollowingcommandexecutesvmstatevery1secondfor100times.

# vmstat1100

procs–––—memory–––-–swap—–—io–-—system—–-cpu–-

rbswpdfreebuffcachesiso

00

00

02821201341085797012

02821201341085797012

126

0

0

bi

0

0

boin

0

0

2

0

csussyidwa

0001000

01007359001000

Page 180: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack00

00

02821201341085797012

02821201341085797012

0

0

0

0

0

0

AjayKumarTiwari

01117577001000

01007366001000

VmstatprocsSectiono

o

rfield:Totalnumberofrunnableprocess

bfield:Totalnumberofblockedprocess

Memorysectiono

o

o

o

Swpdfield:Usedswapspace

Freefield:AvailablefreeRAM

Bufffield:RAMusedforbuffers

Cachefield:RAMusedforfilesystemcache

SwapSectiono

o

Page 181: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

Sifield:Amountofmemoryswappedfromdiskpersecond

Sofield:Amountofmemoryswappedtodiskpersecond

IOSectiono

o

Bifield:Blocksreceivedfromdisk

Bofield:Blockssenttodisk.

SystemSectiono

o

Infield:Numberofinterruptspersecond.

Csfield:Numberofcontextswitchespersecond.

127

Page 182: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

CPUSectiono

o

o

o

Usfield:Timespendrunningusercode.(non-kernelcode)

Syfield:Timespentrunningkernelcode.

Idfield:Idletime.

Wafield:TimespentwaitingfortheIO

Hack98.NetstatCommandNetstatcommanddisplaysthenetworkrelatedinformationsuchasnetwork

connections,routingtables,interfacestatistics.Followingarefewexamples

onhowtousenetstatcommand.

DisplayActiveInternetConnectionsanddomainsocketsusing

netstat# netstat–an

ActiveInternetconnections(serversandestablished)

ProtoRecv-QSend-QLocalAddressForeign

AddressState

tcp000.0.0.0:56660.0.0.0:*

LISTEN

tcp000.0.0.0:1110.0.0.0:*

LISTEN

tcp000.0.0.0:40860.0.0.0:*

LISTEN

skipped..

ActiveUNIXdomainsockets(serversandestablished)

ProtoRefCntFlagsTypeStateI-NodePath

unix2[ACC]STREAMLISTENING7894

/tmp/.font-unix/fs7100

unix2[ACC]STREAMLISTENING9662

Page 183: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

/tmp/.gdm_socket

unix2[ACC]STREAMLISTENING10897

@/tmp/fam-root-

128

Page 184: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

DisplayActiveConnectionswithProcessIDandProgramNameThiscouldbeveryhelpfultoidentifywhichprogramhasinitiatedaspecific

networkconnection.

# netstat-tap

ActiveInternetconnections(serversandestablished)

ProtoRecv-QSend-QLocalAddressForeign

AddressStatePID/Programname

tcp00*:nrpe*:*

LISTEN16277/xinetd

tcp00localhost.localdomain:smtp*:*

LISTEN7263/sendmail:acce

tcp340localhost.localdomain:54221

localhost.localdomain:4089CLOSE_WAIT29881/httpd

tcp03216dev-db:sshcpe-76-

94-215-154.soca:4682ESTABLISHED11717/sshd:ramesh

DisplayRoutingTable# netstat–-route

KernelIProutingtable

DestinationGateway

WindowirttIface

192.168.1.0*

0eth0

162.244.0.0*

0eth0

default192.168.1.1

0eth0

Genmask

255.255.255.0

255.255.0.0

0.0.0.0

U

Page 185: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

Flags

MSS

00

U

UG

00

00

DisplayRAWnetworkstatistics# netstat—statistics—raw

Ip:

129

Page 186: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

11080343totalpacketsreceived

0forwarded

1withunknownprotocol

0incomingpacketsdiscarded

11037744incomingpacketsdelivered

11199763requestssentout

Icmp:

577135ICMPmessagesreceived

64inputICMPmessagefailed.

ICMPinputhistogram:

destinationunreachable:537

timeoutintransit:65

sourcequenches:2

echorequests:576476

echoreplies:12

timestamprequest:3

addressmaskrequest:3

581558ICMPmessagessent

0ICMPmessagesfailed

ICMPoutputhistogram:

destinationunreachable:5079

echoreplies:576476

timestampreplies:3

MiscNetstatCommandso

#netstat–-tcp–-numeric ListofTCPconnectiontoandfromthe

machine.

#netstat—tcp—listening–-programs DisplayTCPportthatthe

serverislisteningonalongwiththeprogramthatislisteningon

Page 187: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

thatparticularport.

#netstat–rnC Displaytheroutingcache

o

o

Hack99.SysctlCommandLinuxkernelparametercanbechangedontheflyusingsysctlcommand.

130

Page 188: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

SysctlhelpstoconfiguretheLinuxkernelparametersduringruntime.

# sysctl–a

dev.cdrom.autoclose=1

fs.quota.writes=0

kernel.ctrl-alt-del=0

kernel.domainname=(none)

kernel.exec-shield=1

net.core.somaxconn=128

net.ipv4.tcp_window_scaling=1

net.ipv4.tcp_wmem=409616384

net.ipv6.route.mtu_expires=600

sunrpc.udp_slot_table_entries=16

vm.block_dump=0

131072

ModifyKernelparameterin/etc/sysctl.confforpermanentchangeAftermodifyingthekernelparameterinthe/etc/sysctl.conf,executesysctl–

ptocommitthechanges.Thechangeswillstillbethereafterthereboot.

#vi/etc/sysctl.conf

# sysctl–p

ModifykernelparametertemporarilyTotemporarilymodifyakernelparameter,executethefollowingcommand.

Pleasenotethatafterrebootthesechangeswillbelost.

#sysctl–w{variable-name=value}

131

Page 189: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

Hack100.NiceCommandKerneldecideshowmuchprocessortimeisrequiredforaprocessbasedon

thenicevalue.Possiblenicevaluerangeis:-20to20.Aprocessthathasa

nicevalueof-20isveryhighpriority.Theprocessthathasanicevalueof20

isverylowpriority.

Usepsaxltodisplaythenicevalueofallrunningprocessasshownbelow.# psaxl

FUIDPIDPPIDPRINIVSZRSSWCHANSTATTTY

TIMECOMMAND

40101602172552-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-id

default4

40514751421603528604-S?

0:00/sbin/pam_timestamp_check-droot

1503175524180160142083920-S?

0:01/home/www/apache2/bin/httpd-f

/home/www/apache2/conf/httpd.conf-kstart

Howtoassignalowprioritytoashell-script?(highernicevalue)Intheexamplebelow,whenIstartedthenice-test.shscriptinthe

background,ittookthenicevalueof0.

$./nice-test.sh&

[3]13009

$psaxl|grepnice-test

05091300912863170

4652

Page 190: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

972wait

S

132

Page 191: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackpts/1

AjayKumarTiwari

0:00/bin/bash./nice-test.sh

[Note:6thcolumnwithvalue0isthenice.]

Now,letusexecutethesameshellscriptwithadifferentnicevalueasshown

below.

$ nice-10./nice-test.sh&

[1]13016

$psaxl|grepnice-test

0509130161286330104236968wait

pts/10:00/bin/bash./nice-test.sh

SN

[Note:6thcolumnwithvalue10isthenicevaluefor

theshell-script.]

Howtoassignahighprioritytoashell-script?(Lowernicevalue)Inthefollowingexample,letusassignanicevalueof-10(minus10)tothe

nice-test.shshellscript.

$ nice—10./nice-test.sh&

[1]13021

$nice:cannotsetpriority:Permissiondenied

Note: Onlyrootusercansetanegativenicevalue.Loginasrootandtrythe

same.Pleasenotethatthereisadoubledashbeforethe10inthenice

commandbelow.

# nice—10./nice-test.sh&

[1]13060

#psaxl|grepnice-test

40130601302410-105388964wait

pts/10:00/bin/bash./nice-test.sh

133

S<

Page 192: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwari

[Note:6thcolumnwithvalue-10isthenicevalueof

theshell-script.]

Hack101.ReniceCommandRenicealterstheschedulingpriorityofarunningprocess.

Howtodecreasethepriorityofarunningprocess?(Increasenice)Intheexamplebelow,anexistingshell-scriptisrunningatnicevalueof10.

(6thcolumninthepsoutput)

$psaxl|grepnice-test

0509132451321630105244968wait

pts/10:00/bin/bash./nice-test.sh

SN

Toincreasethenicevalue(thusreducingthepriority),executetherenice

commandasshownbelow.

$ renice16-p13245

13245:oldpriority10,newpriority16

$psaxl|grepnice-test

0509132451321636165244968wait

pts/10:00/bin/bash./nice-test.sh

SN

[Note:Now,the6thcolumnofthenice-test.sh(PID

13245)showsthenewnicevalueof16.]

Howtoincreasethepriorityofarunningprocess?(Decreasenice)Intheexamplebelow,anexistingshell-scriptisrunningatanicevalueof10.

134

Page 193: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHack(6thcolumninthepsoutput)

AjayKumarTiwari

$psaxl|grepnice-test

0509132541321630104412968wait

pts/10:00/bin/bash./nice-test.sh

SN

Inincreasethepriority,givealowernicevalueasshownbelow.However,

onlyrootcanincreasethepriorityofarunningprocess,elseyou’llgetthe

followingerrormessage.

$ renice5-p13254

renice:13254:setpriority:Permissiondenied

Loginasroottoincreasethepriorityofarunning

process

$su-

# renice5-p13254

13254:oldpriority10,newpriority5

#psaxl|grepnice-test

050913254132162554412968wait

pts/10:00/bin/bash./nice-test.sh

SN

[Note:The6thcolumnnowshowsalowernicevalueof5

(increasedpriority)]

135

Page 194: Linux Hack - index-of.co.ukindex-of.co.uk › Hacking › Linux Hacker by Mr Ajay Kumar Tiwari.pdf · Linux Hack Ajay Kumar Tiwari Foreword Another collection of hacks? Yes! If you

LinuxHackAjayKumarTiwa

LinuxHack