44
Introduction to remote command line Linux Research Computing Team University of Birmingham

Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Introduction to remote command line LinuxResearchComputingTeamUniversityofBirmingham

Page 2: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Linux/UNIX/BSD/OSX/what?

v Alldifferentv UNIXistheoldest,mostlynowcommercialonlyinlargeenvironments(e.g.bankingmainframe)

v Practicallyfromauserperspective,thesamev Focusoncommandlinetools

Page 3: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Distributions of Linux

v Groupsofsoftwareatspecificversionsv Lotsofthem:

– RedHat– Fedora– Suse– Debian– Ubuntu– Mint…..

Page 4: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Remote graphical applications

v UoB onlyprovideBlueBEARv Nographicaldesktop(Gnome/KDE/Unity)v Remoteconnectionviassh (PuTTy)v Xserverrunningforgraphicalapplications

– Exceed(UoB desktops)– Xming (free)– Xquartz (OSX)

Page 5: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Practical

v http://www.birmingham.ac.uk/bear– Thenfollowtheselinksinorder:

v Servicesv LinuxCompute(BlueBEAR)vAccess&FileTransfervWindowssoftwaretoaccessBlueBEARv “thehelppageforinstallingandconfiguringPuTTYandExceed”(lastparagraphofthe“WindowssoftwaretoaccessBlueBEAR”section)

orv http://tinyurl.com/bear0915

Page 6: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Graphical text editingv Fromthecommandpromptrun‘gedit’togetagraphicaltexteditorrunningontheremotesystem

v IntheXserveryouhavetwocopybuffers– Copyandpaste,asnormal– ‘Middleclick’buffer

Page 7: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Exceed … (useful tip)

v Selecttexttoplaceitintothecopybufferv Middleclicktopastetext

Page 8: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

A word about directory layoutv UnlikeWindows(andDOSsystems)thereareno“drives”(i.e.no

‘C:\’,‘D:\’,‘U:\’etc.)v Everythingexistsunderadirectory,‘/’,calledtherootdirectory.v Usefuldirectoriestoknow:

– ‘/home’– eachuserusuallyhasadirectoryherewiththeirownfilesinit,knownastheir‘homedirectory’.v OnBlueBEAR users’homedirectoriesarelocatedat‘/gpfs/bb’,not‘/home’,fortechnicalreasons,but‘/home’stillexists.

– ‘/tmp’– temporaryfiles.Anyusercancreatefilesinthisdirectory.Oftendeletedwhenthecomputerreboots.v OnBlueBEAR thereisalso‘/scratch’oneachcomputenodewhichisforthesamepurposebutmuchlarger.

Page 9: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

What is a path?

v Aconcretestriptothebottomofthegardenv Wherethesystemlookstofindprograms

echo $PATH

– Command not found – theprogramisn’tinyourpath

Page 10: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Welcome to the command line

v FilesandprogramsareCaSe-SeNsItIvEv Youtypecommandsintoaprogramcalledthe“shell”v Commandsarecase-sensitivev The‘tab’keyismagic,pressingit:

– once:completethecurrentcommandorfilenameifthereis1match

– twice:listallthematchingoptionsifthereismorethan1match

Page 11: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Command line syntax

v Variesfromprogramtoprogramv Typicallyoftheform

tar -cpf foo.tar ~/to_archive/

<command> -<character> <argument> <file>

tar -cpf foo.tar ~/to_archive/

Page 12: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Help? How do I use this command?v Eachcommandshould haveamanualpageforit.– Toviewittypeman command,e.g.toviewthemanualforthemancommandtype:man man

– Pressqtoquit,usethearrowkeystoscroll

Page 13: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Looking at your filesv Basicfilecommands:

ls – listfiles

cd – changedirectory

pwd – printcurrent(working)directory

Page 14: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Looking at your files (continued)v Basicfilecommands:

less filename – view the contents of your (text) files (press q to quit), space to page down, up/down arrows

Page 15: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Looking at your files (continued)v Basicfilecommands:

tail -f filename – watchtheendoffileasitgrows

Page 16: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Organising your filesv Basicdirectorymanagementcommands:

mkdir newdirectory – createdirectory

rmdir directory – removedirectory(onlyworksifdirectoryisempty)

Page 17: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Organising your filesv Basicfilemanagementcommands:

touch file –createanemptyfile

rm file – removeafile(thereisnorecyclebin!)

cp file newfile – copyafile(createsaduplicate)

mv file newfile – moveafile(renamesthefile)

Page 18: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

dot files and directories

v Userconfig settingsstoredinthemv Pleasedon’tdelete!v Usefulonestoedit(tosetthingseverytimeyoulogin):– .bash_profile– .bashrc– BUTBECAREFUL!

Page 19: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Linksv Therearetwotypesoflinksonunix systems:

– symboliclinks– knownas“symlinks”,likea‘shortcut’tothefile

– hardlinks– anewreferencetothesamefile(youareveryunlikelytoeverusethese)

v Creatingasymlink:– Usetheln linkcommandwiththe-s,symlink,option.E.g.tocreatealinkto/gpfs/bb/username/myfile called/tmp/link_to_myfiletype:ln –s /gpfs/bb/username/myfile/tmp/link_to_myfile

Page 20: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Users and groups

v UsernamemapstoanumericUID– Commandid returnsdetails

v Memberofleastonegroupv GroupnamesmaptoanumericGID

– Commandgroups listsyourgroupsgroups username

Page 21: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

User permissions

v Bydefaultusershavenospecialpermissionv rootisequivalentofadministratorv Commandsu - username wouldchangetothatuserifyouknowthepassword

v Modernsystemsusesudo todocommandswithelevatedpermission(orasanotheruser)

v UoB centralsystemsdon’tallowsudo orsu tonormalusers

Page 22: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

File Permissions

v Files(anddirectories)canhavedifferentpermissionsetsforgroupsandusers

Files Directory

r– readthefile r– listcontentsofdirectory

w– writetothefile w– createnewfiles/folders

x– execute(run) x– traverse(e.g.cdtodirectory)

Page 23: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

File Permissions

v Trydoingls –ld ~/

v Thisshowspermissionsonhomedirectoryv Firstcharacterindicatesfile(-),directory(d)orlink(l),next3charactersisuser,next3group,final3isallotherusers

v +signindicatesextendedACLsapplied

Page 24: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

File permissions

mkdir testdirtouchtestdir/testfile

chmod isusedtochangepermissionschmod g+rwx testdir(thencheckwithls -ld testdir)chmod o+x testdir/testfile

Page 25: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

File permissions

v Aswellasusingrwx etc,bitmaskscanalsobeused,thefollowingareequivalent:chmod 755 directorychmod u+rwx,go+rx directory

Page 26: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Shell variablesv Lotssetatlogin

envv Cansetown

export FOO=barecho $FOO– Valueof$FOOisnowavailabletoother“things”inshell

v ~/ - shortcuttohomedirectoryv ~someuser/ - shortcuttoanotheruserhome

directory

Page 27: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Some useful toolsv grep stringfilename

– Lookforstringoccurrencesinthefilegrep ldap /etc/nsswitch.conf

– Whatdoesgrep –c ldap /etc/nsswitch.conf

do?

Page 28: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Some useful toolsv diff file1 file2

– Showsthedifferencesbetweentwofilesdiff /etc/nsswitch.conf

~/nsswitch.conf

Page 29: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Editing files

v CaneditfromWindowsanduseshare– Windowsfilesusedifferentlineendings

dos2unix ~/filename

v LotsofeditorsunderLinux– nano isabasic,easytouseone– Others– joe,vim,emacs

Page 30: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Editing filesv Createandeditanewfileinhomedirectory:

nano ~/testfile.txt

– Typeinsometext– SavethefilewithCTRL+o– ExitwithCTRL+x– Checkthecontentsofthefile(Remembercatorless?)– Editthefileagain,tryCTRL+k,CTRL+u,CTRL+w toseewhattheydo

Page 31: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Pipes and redirection

v |(pipe)canbeusedtosendoutputofcommandintoanotherls –al /gpfs/bb | grep $USER

v >redirectstheoutputofacommand– E.g.toafile– STDOUT– STDERR– 2>&1

Page 32: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Pipes and redirection

v Trythese:ls –al /gpfs/bb | grep $USERls –al ~/ > ~/ls.outputCdgffgffg > ~/error.outputCdgffgffg > ~/error.output 2>&1

Page 33: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Scripts

v Allowseriesofcommandstoberepeatedv Canpassargumentsin,usevariablesetc.v Needtobe“executable”torunfromcommandline

v Firstlineshowsthe“interpreter”(orshell)touse,e.g.#!/bin/bash#!/usr/bin/perl

Page 34: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Scriptsv Usethetexteditortocreateanewscriptfileto:

– Createadirectory(mkdir)– Changetoadirectory(cd)– Listthedirectorycontentswithalldetails(ls)– Createanewemptyfile(touch)– Listthedirectorycontentswithalldetails(ls)

v Remembertosettheshellinthefirstline#!/bin/bash

v Makeitexecutable(chmod)v Runthescript

./SCRIPTNAME– (Youcouldalsotryredirectingtheoutputtoafile)

Page 35: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Scripts

#!/bin/bash

mkdir ~/tempdircd ~/tempdirls -altouch somefilels -al

Page 36: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Archiving and compressing

v Atar file(traditionallytapearchive)isawayofgroupingasetoffilestogetherintoasinglefile

v gzip orbzip2 areoftenusedtocompressfilesv Soforexamplea.tar.gz filewouldbesimilartoazipfile

Page 37: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Archiving and compressingv Createanewtarfile,beingverboseofthefiles,preserving

filepermissions:tar -cvpf ~/newfile.tar ~/testdir

v Compressthefile:gzip -9 ~/newfile.tar

v Listcontentsofcompressedfile:tar -tzf ~/newfile.tar.gz

v Extractcontentstoanewdirectory:mkdir ~/newdircd ~/newdirtar -xvzpf ~/newfile.tar.gz

Page 38: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Process management

v Everythingyouexecutecausesatleast1processtobegenerated

v Programswhich“fork”maycreatemultiplesub-processes

v Thecommandtop showstheprocessesrunningusingmostCPU(qtoexit)

Page 39: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Process managementv Processesmaybeinseveraldifferentstates:

– Runninginforeground– Suspended(stopped,nofurtherprocessinghappens)

– Backgrounded (runningbutnolongerattachedtoterminal)

– Zombie(Inabadway,typicallynolongerdoinganythinguseful)

Page 40: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Process management

v CommandlineprocessescanbestoppedusingCTRL+z

v Runningbg willthenbackgroundtheprocessv Jobslistsprocessesfromthecurrentterminalandtheirstate

v fg returnsasuspendedorbackgroundprocesstothecontrollingshell

Page 41: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Process Priority

v EachprocesshasaprioritywhichdetermineshowmuchCPUtimewillbeallocatedwhentheCPUisbusywithmanyapplications

v renice canbeusedtochangethepriorityofaprocess(userscanonlyreduce)

v Onsharedsystems,itis“sociable”toreniceheavycomputeprocesseswhichyouareleavingrunning

Page 42: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Looking for processes

v Thecommandps willlistyourrunningprocessesandtheirprocessID

v ps -ef | grep vi– Wouldlookforprocessescalledvi

Page 43: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Signals

v Signalsareusedtotellprocessestodosomething,typically:– Totellaprocesstorereadaconfig (HUP)– Totellaprocesstoterminate(TERM)– Toforcefullykillaprocess(KILL)– HUPandTERMrelyonsignalhandlerincode

kill -SIGNAL processID

Page 44: Introduction to remote command line Linux · 2017-09-22 · Scripts vUse the text editor to create a new script file to: –Create a directory (mkdir)–Change to a directory (cd)–List

Any questions?