Tải bản đầy đủ - 0 (trang)
Chapter 4.  Basic Unix Commands and Concepts

Chapter 4.  Basic Unix Commands and Concepts

Tải bản đầy đủ - 0trang

completelynewtoUnix,thematerialfoundinPartIIshouldbe

easytofollowonceyou'vecompletedthetutorialhere.

Onebigjobwemerelytouchoninthischapterishowtoedit

files.It'soneofthefirstthingsyouneedtolearnonany

operatingsystem.ThetwomostpopulareditorsforLinux,vi

andEmacs,arediscussedinChapter19.







4.1.LoggingIn

Let'sassumethatyourinstallationwentcompletelysmoothly,

andyouarefacingthefollowingpromptonyourscreen:

Linuxlogin

:



SomeLinuxusersarenotsolucky;theyhavetoperformsome

heavytinkeringwhenthesystemisstillinarawstateorin

single-usermode.Butfornow,we'lltalkaboutloggingintoa

functioningLinuxsystem.

Loggingin,ofcourse,distinguishesoneuserfromanother.It

letsseveralpeopleworkonthesamesystematonceand

makessurethatyouaretheonlypersontohaveaccesstoyour

files.

YoumayhaveinstalledLinuxathomeandarethinkingright

now,"Bigdeal.Nooneelsesharesthissystemwithme,andI'd

justassoonnothavetologin."Butlogginginunderyour

personalaccountalsoprovidesacertaindegreeofprotection:

youraccountwon'thavetheabilitytodestroyorremove

importantsystemfiles.Thesystemadministrationaccount

(coveredinthenextchapter)isusedforsuchtouchymatters.

IfyouconnectyourcomputertotheInternet,evenviaa

modem,makesureyousetnontrivialpasswordsonallyour

accounts.Usepunctuationandstringsthatdon'trepresentreal

wordsornames.AlthoughUnixsystemsarenotassusceptible

torandombrute-forceattacksfromtheoutsideworldas

Windowssystemsare(accordingtosomesources,ittakes

about20minutesfromconnectingaWindowsboxtothe

Internetuntilthatcomputerisattacked,whereasittakesabout



40minutestodownloadthesecurityfixesfromMicrosoft),you

certainlydonotwantanybodytosnooparoundinyourfiles.

Notethatsomedistributionsinstallaso-calledgraphicallogin

managerrightaway,soyoumightnotbegreetedbythe

somewhatarcanelogin:promptinwhitelettersonblack

background,butwithafancygraphicalloginscreen,possibly

evenpresentingyouwiththeuseraccountsavailableonyour

system(maybeevenwithalittlepictureforeachuser)aswell

asdifferentmodestologinto.Thebasicloginprocedureisthe

sameasdescribedhere,however:youstilltypeyourusername

andpassword.

Youwereprobablyaskedtosetupaloginaccountforyourself

whenyouinstalledLinux.Ifyouhavesuchanaccount,typethe

nameyouchoseattheLinuxlogin:prompt.Ifyoudon'thavean

accountyet,typerootbecausethataccountiscertaintoexist.

Somedistributionsmayalsosetupanaccountcalledinstallor

someothernameforfoolingaroundwhenyoufirstinstallthe

system.

Afteryouchooseyouraccount,yousee:

Password:



andyouneedtoenterthecorrectpassword.Theterminalturns

offthenormalechoingofcharactersyouenterforthisoperation

sothatpeoplelookingatthescreencannotreadyourpassword.

Ifthepromptdoesnotappear,youshouldaddapasswordto

protectyourselffromotherpeople'stampering;we'llgointo

thislater.

Bytheway,boththenameandthepasswordarecasesensitive.MakesuretheCapsLockkeyisnotsetbecause

typingROOTinsteadofrootwillnotwork.



Whenyouhavesuccessfullyloggedin,youwillseeaprompt.If

you'reroot,thismaybeasimple:

#



Forotherusers,thepromptisusuallyadollarsign($).The

promptmayalsocontainthenameyouassignedtoyoursystem

orthedirectoryyou'reincurrently.Whateverappearshere,you

arenowreadytoentercommands.Wesaythatyouareatthe

"shelllevel"hereandthatthepromptyouseeisthe"shell

prompt."Thisisbecauseyouarerunningaprogramcalledthe

shellthathandlesyourcommands.Rightnowwecanignorethe

shell,butlaterinthischapterwe'llfindthatitdoesanumberof

usefulthingsforus.

Asweshowcommandsinthischapter,we'llshowtheprompt

simplyas$.Soifyousee:

$pwd



itmeansthattheshellprints$andthatpwdiswhatyou're

supposedtoenter.







4.2.SettingaPassword

Ifyoudon'talreadyhaveapassword,werecommendyouset

one.Justenterthecommandpasswd.Thecommandwill

promptyouforapasswordandthenaskyoutoenterita

secondtimetomakesureyouenteritwithouttypos.

Therearestandardguidelinesforchoosingpasswordssothat

they'rehardforotherpeopletoguess.Somesystemseven

checkyourpasswordandrejectanythatdon'tmeetthe

minimalcriteria.Forinstance,itisoftensaidthatyoushould

haveatleastsixcharactersinthepassword.Furthermore,you

shouldmixuppercaseandlowercasecharactersorinclude

charactersotherthanlettersanddigits.

Ifyouthinkitisagoodideatopickanordinary,butrarelyused

wordasyourpassword,thinkagain.Therearepasswordattack

programsavailablethatcomewithanEnglishdictionaryand

justtryallwordsinthatdictionaryinordertofindthecorrect

onesothattheaccountcanbecompromised.Also,neveruse

theaccountnameforthepassword.Thisissometimescalleda

"joe,"andislikelytobethefirstthingapasswordattackeris

goingtotry.

Agoodtrickforchoosingagoodpasswordistotakeafull

phrasethatyoucanremember(maybealinefromyourfavorite

song),andthenjusttakethefirstletters.Thenblendinadigit

andmaybeaspecialcharacter.Forexample,ifyourlineisI'd

reallyliketogofishingnow,yourpasswordcouldbeIrl2gfn!.

Butdonotuseexactlythisone;thefactthatithasbeen

publishedinthisbookmakesitabadpassword.Thereareeven

programsavailable(notunlikelyintegratedintothegraphical

usermanagementtoolsofyourdistribution)thatgeneratea

randompasswordfromrandomcharacters,butofcoursethese

passwordsaredifficulttorememberifyouhavetowritethe

passworddowninordertorememberit,itisabadpasswordas



well.

Tochangeyourpassword,justenterthepasswdcommandagain.

Itpromptsyouforyouroldpassword(tomakesureyou'reyou)

andthenletsyouchangeit.







4.3.VirtualConsoles

Asamultiprocessingsystem,Linuxgivesyouanumberof

interestingwaystodoseveralthingsatonce.Youcanstarta

longsoftwareinstallationandthenswitchtoreadingmailor

compilingaprogramsimultaneously.

MostLinuxusers,whentheywantthisasynchronousaccess,

willemploytheXWindowSystem(seeChapter16).Butbefore

yougetXrunning,youcandosomethingsimilarthroughvirtual

consoles.ThisfeatureappearsonafewotherversionsofUnix,

butisnotuniversallyavailable.

Totryoutvirtualconsoles,holddowntheleftAltkeyandpress

oneofthefunctionkeys,F1throughF8.Asyoupresseach

functionkey,youseeatotallynewscreencompletewithalogin

prompt.Youcanlogintodifferentvirtualconsolesjustasifyou

weretwodifferentpeople,andyoucanswitchbetweenthemto

carryoutdifferentactivities.YoucanevenrunacompleteX

sessionineachconsole.TheXWindowSystemwillusevirtual

console7bydefault.SoifyoustartXandthenswitchtooneof

thetext-basedvirtualconsoles,youcangobackagaintoXby

typingAlt-F7.IfyoudiscoverthattheAlt-+functionkey

combinationbringsupanXmenuorsomeotherfunction

insteadofswitchingvirtualconsoles,useCtrl+Alt+function

key.YoucanevenhavetwoXserversrunningtheXWindow

System;thesecondonewouldthenbeonvirtualconsole8.







4.4.PopularCommands

ThenumberofcommandsonatypicalUnixsystemisenoughto

fillafewhundredreferencepages.Andyoucanaddnew

commandstoo.Thecommandswe'lltellyouaboutherearejust

enoughtonavigateandtoseewhatyouhaveonthesystem.



4.4.1.Directories

AswithWindowsandvirtuallyeverymoderncomputersystem,

Unixfilesareorganizedintoahierarchicaldirectorystructure.

Uniximposesnorulesaboutwherefileshavetobe,but

conventionshavegrownupovertheyears.Thus,onLinux

you'llfindadirectorycalled/homewhereeachuser'sfilesare

placed.Eachuserhasasubdirectoryunder/home.Soifyour

loginnameismdw,yourpersonalfilesarelocatedin

/home/mdw.Thisiscalledyourhomedirectory.Youcan,of

course,createmoresubdirectoriesunderit.

IfyoucomefromaWindowssystem,theslash(/)asapath

separatormaylookoddtoyoubecauseyouareusedtothe

backslash(\).Thereisnothingtrickyabouttheslash.Slashes

wereactuallyusedaspathseparatorslongbeforepeopleeven

startedtothinkaboutMS-DOSorWindows.Thebackslashhas

adifferentmeaningonUnix(turningoffthespecialmeaningof

thenextcharacter,ifany).

Asyoucansee,thecomponentsofadirectoryareseparatedby

slashes.Thetermpathnameisoftenusedtorefertothisslashseparatedlist.

Whatdirectoryis/homein?Thedirectorynamed/,ofcourse.

Thisiscalledtherootdirectory.Wehavealreadymentionedit

whensettingupfilesystems.



Whenyoulogin,thesystemputsyouinyourhomedirectory.

Toverifythis,usethe"printworkingdirectory,"orpwd,

command:

$pwd

/home/mdw



Thesystemconfirmsthatyou'rein/home/mdw.

Youcertainlywon'thavemuchfunifyouhavetostayinone

directoryallthetime.Nowtryusinganothercommand,cd,to

movetoanotherdirectory:

$cd/usr/bin

$pwd

/usr/bin

$cd



Wherearewenow?Acdwithnoargumentsreturnsustoour

homedirectory.Bytheway,thehomedirectoryisoften

representedbyatilde(~).Sothestring~/programsmeansthat

programsislocatedrightunderyourhomedirectory.

Whilewe'rethinkingaboutit,let'smakeadirectorycalled

~/programs.Fromyourhomedirectory,youcanentereither:

$mkdirprograms



orthefullpathname:

$mkdir/home/mdw/programs



Nowchangetothatdirectory:

$cdprograms

$pwd

/home/mdw/programs



Thespecialcharactersequence..referstothedirectoryjust

abovethecurrentone.Soyoucanmovebackuptoyourhome

directorybytypingthefollowing:

$cd..



Youcanalsoalwaysgobacktoyourhomedirectorybyjust

typing:

$cd



nomatterwhereinthedirectoryhierarchyyouare.

Theoppositeofmkdirisrmdir,whichremovesdirectories:

$rmdirprograms



Similarly,thermcommanddeletesfiles.Wewon'tshowithere

becausewehaven'tyetshownhowtocreateafile.You

generallyusetheviorEmacseditorforthat(seeChapter19),

butsomeofthecommandslaterinthischapterwillcreatefiles



too.Withthe-r(recursive)option,rmdeletesawholedirectory

andallitscontents.(Usewithcare!)

Atthispoint,weshouldnotethatthegraphicaldesktop

environmentsforLinux,suchasKDEandGNOME,comewith

theirownfilemanagersthatcanperformmostoftheoperations

describedinthischapter,suchaslistinganddeletingfiles,

creatingdirectories,andsoforth.Someofthem,likeKonqueror

(shippedwithKDE)andthewebbrowserinthatenvironment,

arequitefeature-rich.However,whenyouwanttoperforma

commandonmanyfiles,whichperhapsfollowacertain

specification,thecommandlineishardtobeatinefficiency,

evenittakesawhiletolearn.Forexample,ifyouwantedto

deleteallfilesinthecurrentdirectoryandalldirectories

beneaththatwhichstartwithanrandendin.txt,theso-called

Zshell(zsh)wouldallowyoutodothatwithoneline:

$rm**/r*.txt



Moreaboutthesetechniqueslater.



4.4.2.ListingFiles

Enterlstoseewhatisinadirectory.Issuedwithoutan

argument,thelscommandshowsthecontentsofthecurrent

directory.Youcanincludeanargumenttoseeadifferent

directory:

$ls/home



Somesystemshaveafancylsthatdisplaysspecialfilessuchas

directoriesandexecutablefilesinbold,orevenindifferent



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 4.  Basic Unix Commands and Concepts

Tải bản đầy đủ ngay(0 tr)

×