Linux Internals - Part 1

  • 1,732 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,732
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Linux Internals (Day 1) Pradeep D. Tewani © 2012 Pradeep Tewani<pradeep@sysplay.in> All Rights Reserved.
  • 2. What to Expect?Introduction to Linux & Linux ArchitectureKernel Mode & User ModeLinux Directory StructureLinux File types & Permissions, File commandsShell Basicsvi EditorShell Scripting © 2012 Pradeep Tewani<pradeep@sysplay.in> 2 All Rights Reserved.
  • 3. Ws of LinuxFree and Open SourceDeveloped by Linus Torvalds With the assistance of the developers around the world © 2012 Pradeep Tewani<pradeep@sysplay.in> 3 All Rights Reserved.
  • 4. Ws of OSInfinitely Running ProgramManages the system resources CPU Process Management or Process Scheduling Memory Memory Management Storage Storage Management I/O I/O Management Network Network Management © 2012 Pradeep Tewani<pradeep@sysplay.in> 4 All Rights Reserved.
  • 5. Why use Linux?Free and Open SourceReliableSecureScalable © 2012 Pradeep Tewani<pradeep@sysplay.in> 5 All Rights Reserved.
  • 6. Properties of LinuxMultitaskingMulti-UserMultiprocessing (SMP)Protected MemoryHeirarchical Filesystem © 2012 Pradeep Tewani<pradeep@sysplay.in> 6 All Rights Reserved.
  • 7. Linux System ArchitectureHardware Controllers Comprises of all the possible physical devices in Linux sytem such as Memory, hard disk, CPU and n/w hardware © 2012 Pradeep Tewani<pradeep@sysplay.in> 7 All Rights Reserved.
  • 8. Linux System Architecture...Linux Kernel Core of an Operating system Abstracts & Mediates access to the CPUOS Service Set of interface interact with the user Windowing system, shell are typical exampleUser Applications Set of applications on a particular Linux Sytem Examples include word processing application and a web browser © 2012 Pradeep Tewani<pradeep@sysplay.in> 8 All Rights Reserved.
  • 9. User Space, Kernel SpaceUser space User applications executes Low Privileged Space No direct access to h/wKernel Space Kernel Resides Privileged Mode Full Access to Hardware © 2012 Pradeep Tewani<pradeep@sysplay.in> 9 All Rights Reserved.
  • 10. System CallsSet of Service points provided by Kernel To allow the user program to interact with h/w Means to ask the Kernel to perform certain tasks on behalf of user programSet of interface to interact with hardware devicessuch as CPU, Hard disks, Memory, Printer & so on.Application is said to be executing in kernel modeduring system call executing in Kernel Mode © 2012 Pradeep Tewani<pradeep@sysplay.in> 10 All Rights Reserved.
  • 11. The Kernel Structure © 2012 Pradeep Tewani<pradeep@sysplay.in> 11 All Rights Reserved.
  • 12. Kernel ArchitecturesMonolithic Kernel Whole kernel executes as single executable in kernel Mode Includes all the basic system services UNIX, MS DOS and earlier MAC OSMicro Kernel Divided into processes and most of them run in user space Only teh Baseic © 2012 Pradeep Tewani<pradeep@sysplay.in> 12 All Rights Reserved.
  • 13. Linux Directory Structure © 2012 Pradeep Tewani<pradeep@sysplay.in> 13 All Rights Reserved.
  • 14. Linux Directory structure.../ - This is the root directory. This is where the whole tree starts.The partition where / (theroot directory) will be located on a UNIX or UNIX- compatible system./boot - Contains static files for the boot loader. This directory only holds the fileswhich are needed during the boot process./dev - Special or device files, which refer to physical devices/sys – It is the virtual filesystem used to export the data from kernel to user space. Suchas when a usb device is connected, the info appears over here./bin - Contains the essential binaries for users and those utilities that are required insingle user mode. Examples, include cat, ls, cp etc./sbin - Like /bin, this directory holds commands needed to boot the system, but which areusually not exe‐cuted by normal users./lib - This directory should hold those shared libraries that are necessary to boot thesystem and to run the commands in the root file system. © 2012 Pradeep Tewani<pradeep@sysplay.in> 14 All Rights Reserved.
  • 15. Linux Directory Structure.../home - All the user home directories are held under this directory with the exception ofthe root home directory which is kept under /root directory. This directory holds users files,personal settings like .profile etc./media - This directory contains mount points for removable media such as CD and DVDdisks or USB sticks./mnt - This directory is a mount point for a temporarily mounted file system./opt - A rarely used directory in Linux for Optional Software Packages. This is extensivelyused in UNIX OS like Sun Solaris where the software packages are installed/var - This directory contains files which may change in size, such as log files./proc - This is a mount point for the proc file system, which provides information aboutrunning processes and the kernel. © 2012 Pradeep Tewani<pradeep@sysplay.in> 15 All Rights Reserved.
  • 16. Linux Directory Structure.../tmp - A temporary file system which hold temporary fileswhich are cleared at system reboot./etc - Contains configuration files which are local to themachine. The /etc/directory contain essential Systemconfiguration files /etc/hosts, and network configurationfiles.when you login bash reads the /etc/profileinstructions.These usually set the shell variabelsPATH,USER,MAIL,HOSTNAME © 2012 Pradeep Tewani<pradeep@sysplay.in> 16 All Rights Reserved.
  • 17. The Shell What is a shell? Different types of shells Login-shell Non-login shell Sh Bash Ksh Csh echo $0 will display shell type you are using presentlyNB :The file cat /etc/shells gives an overview of known shells on a your linuxsystem © 2012 Pradeep Tewani<pradeep@sysplay.in> 17 All Rights Reserved.
  • 18. How Shell InvokesThe main task of a shell is to provide a userenvironment. (input) ls Shell Lists all the files Error Command a.c b.c d.c e.c ... not found © 2012 Pradeep Tewani<pradeep@sysplay.in> 18 All Rights Reserved.
  • 19. The Bash Shell Variables & Friends$ env - lists shell environment variable/value pairs$ export [var_name] - exports/sets a shell variableHOME - path to user’s home directoryPATH - executable search pathPWD - present working directoryPS1 - command prompt © 2012 Pradeep Tewani<pradeep@sysplay.in> 19 All Rights Reserved.
  • 20. Some Basic Shell Commands $ pwd - gives present working directory $ cd - change directory $ man/info - gives information about command $ exit - exits from the shell $ which - shows full path of command e.g;which ls NB These all are the commands which haveexecutables, which <cmds> will list where are thesecommands available as executable files © 2012 Pradeep Tewani<pradeep@sysplay.in> 20 All Rights Reserved.
  • 21. Shell Built in CommandsBuilt-in commands are contained with in the shellitself, means shell executes the command directly,without creating a new process. Built-in commands: break, cd, exit ,pwd, export, return, unset, alias, echo, printf, read, logout, help,manFind out commands that doesnt have executables,rather they are shell built-ins. © 2012 Pradeep Tewani<pradeep@sysplay.in> 21 All Rights Reserved.
  • 22. Linux File systemsNative files systems ext2, ext3, ext4Superblock – Stores filesystem metadataInode – Stores file metadataFile System related Shell Command Set stat - File and Inode information mount - Mounting filesystem find, locate - Search for files file – Determine the file type © 2012 Pradeep Tewani<pradeep@sysplay.in> 22 All Rights Reserved.
  • 23. File Types in LinuxSeven file types in Linux. Regular Normal files which contain normal data, for example text file, binary files or pragrams The symbol for it is - Commands : touch and rm Directory Files that are list of other files The symbol is d Commands – mkdir and rm -r Symbolic link A reference to another file The symbol is s Commands – ln and rm © 2012 Pradeep Tewani<pradeep@sysplay.in> 23 All Rights Reserved.
  • 24. File Types in Linux...Socket To Provide inter process network communication The symbol is sNamed Pipe To achieve the inter process communication between processes on the system. The symbol is p Commands : mkfifo & rmCharacter device files Provide only a stream of serial input & output Symbol is c Commands : mknod & rmBlock device files Are randomly accessible Symbol is b Command : mknod & rm © 2012 Pradeep Tewani<pradeep@sysplay.in> 24 All Rights Reserved.
  • 25. File Related Shell CommandsEvery thing is viewed as a file in Linux. Even a Directory is a file.Basic Shell Command Setpwd - print working directory.$ cd - change directory.ls - list directory/file contentsdf - disk freedu - disk usagecp - copymv - move, renamerm - remove © 2012 Pradeep Tewani<pradeep@sysplay.in> 25 All Rights Reserved.
  • 26. File Detailed Listing Type ls -l on the shell Owner & group Filepermessions Created time & date File size File name © 2012 Pradeep Tewani<pradeep@sysplay.in> 26 All Rights Reserved.
  • 27. File Related Shell Commands...mkdir - make directoryrmdir - remove directorycat, less, head, tail - used to view text filestouch - create and update fileswc - counts the number of lines in a filesed - stream editor for filtering and transforming text © 2012 Pradeep Tewani<pradeep@sysplay.in> 27 All Rights Reserved.
  • 28. File Permissions Three types of users Owner, Group and Others Three types of permissions r – read, w – write, x – execute For viewing the permissions ll or ls -l 10 bits pattern is used t rwx rwx rwxFile type 421 421 421 user group others © 2012 Pradeep Tewani<pradeep@sysplay.in> 28 All Rights Reserved.
  • 29. Changing File PermissionsChanging the File Permissions chmod – Change file permissions chown – Change file owner chmod [ ug+r, 746 ] file.txt chown -R user:group [ filename | dir ] © 2012 Pradeep Tewani<pradeep@sysplay.in> 29 All Rights Reserved.
  • 30. File linksA link is simply a reference to another fileTypes of File Links Hard Link ln Hard link is a pointer to the files i-node. All hard links share the same inode number. If you delete the source hard link file you can still access the other one. Soft Link ln -s Is also known a Symbolic link. Soft link is like a pointer to the files content Each soft link has a unique inode number. With a soft link, if you delete the source link, you cannot access the other one. © 2012 Pradeep Tewani<pradeep@sysplay.in> 30 All Rights Reserved.
  • 31. Advance File related commandscut - Print selected parts of lines from each FILE to standardoutput. eg :- $ cut -c 4,5,20 filenamesplit - split a file in to pieces eg :- $ split [-n] filename output filenamediff - compare files line by line eg :- $ diff file1 file2uniq - Discard all but one of successive identical lines from inputwriting to output © 2012 Pradeep Tewani<pradeep@sysplay.in> 31 All Rights Reserved.
  • 32. File CompressionCommand : gzip, bzip2gzip filename This will compress and save as filename.gzgunzip filename.gz The filenam.gz will be replaced with filenameBzip2 filename This will compress and save as filename.bz2Bunzip2 filename.bz2 The filenam.bz2 will be replaced with filename © 2012 Pradeep Tewani<pradeep@sysplay.in> 32 All Rights Reserved.
  • 33. FiltersFilters are the programs, which read some input,perform the transformation on it and gives theoutput. Some commonly used filters are tail : print the last 10 lines of the file to standard output sort : Sort lines of text file tr : Translate, squeeze, and / or delete the characters from standard input, writing to the standard output wc : Print newline, word, and byte counts for each file © 2012 Pradeep Tewani<pradeep@sysplay.in> 33 All Rights Reserved.
  • 34. Archiving with tarConverting the directory into a single fileArchive the file tar -cvf filename.tar filenameUntar the file tar -xvf filename.tarView the table of contents of the tar file tar -tvf filename.tarAdd a new file to filename.tar tar -rvf filename.tar newfileTo extract the particular files from the tar file tar -xvf filename.tar file1 file2 © 2012 Pradeep Tewani<pradeep@sysplay.in> 34 All Rights Reserved.
  • 35. Pattern Matching CommandsGrep is pattern matching tool used to search thename input file. Basically its used for linesmatching a pattern Command: grepExample: $ ls | grep “Linux” This will list the files from the current directory with Linux in their filename. © 2012 Pradeep Tewani<pradeep@sysplay.in> 35 All Rights Reserved.
  • 36. DIUCount the Number of lines in a fileHow do you list only empty lines in a file?Archive and zip the directory © 2012 Pradeep Tewani<pradeep@sysplay.in> 36 All Rights Reserved.
  • 37. More Commandswho Tells who is logged on to the system who -H prints heading line who -u shows the users and their process idswhat is <command> Prints some info about the commanddate date +”%H:%M” date +”%l:%M” Date +”%l:%M %p”echo Used to send the output on a screen © 2012 Pradeep Tewani<pradeep@sysplay.in> 37 All Rights Reserved.
  • 38. More Commands...Logging into the another system ssh username@ipaddresCopying the file to another system scp <source_file> username@ipaddress:<distination>Password protecting a file gpg -c <file_name> gpg <file_name.gpg> (Gives the file back) © 2012 Pradeep Tewani<pradeep@sysplay.in> 38 All Rights Reserved.
  • 39. Visual EditorTo open a file vi <filename>The power of vi comes from its 3 modes Escape Mode (Command Mode) Edit Mode Visual Mode © 2012 Pradeep Tewani<pradeep@sysplay.in> 39 All Rights Reserved.
  • 40. Cursor Movementvi has many cursor movement commandsThe four basic keys appear below k moves up one line h line move one character to the left l line move one character to the right j move down one lineTo exit :q -> Close with out saving. :wq -> Close the file with saving. :q! -> Close the file forcefully with out saving © 2012 Pradeep Tewani<pradeep@sysplay.in> 40 All Rights Reserved.
  • 41. Editing Modes Command Mode Name Insertion Pointa Append just after the current characterA Append end of the current linei Insert just before the current characterI Insert beginning of the current lineo Open new line below the current lineO Open new line above the current line © 2012 Pradeep Tewani<pradeep@sysplay.in> 42 All Rights Reserved.
  • 42. Editing TextDeleting Text Sometimes you will want to delete some of thetext you are editing. To do so, first move the cursor so that itcovers the first character of the group you want to delete, thentype the desired command from the table below. dd - For deleting a line. ndd - For deleting a n lines x - To delete a single character shift + d - Delete contents of line after cursor dw - Delete words ndw - Delete n words © 2012 Pradeep Tewani<pradeep@sysplay.in> 43 All Rights Reserved.
  • 43. Some Useful Shortcutsshift-g - Go to last line in fileshift-j - Joining the two lines. - It repeats the previous command executedctrl+a - Increment number under the cursorctrl+x - Decrements numbers under the cursor © 2012 Pradeep Tewani<pradeep@sysplay.in> 44 All Rights Reserved.
  • 44. Visual Mode Visual Mode Visual mode helps to visually select some text, may be seen as a sub mode of the the command mode. To switch from the command mode to the visual mode type one of ctrl+v :- Gos to visual block mode. Only v for visual mode d or y Delete or Yank selected text I or A Insert or Append text in all lines (visual block only)DIU: Replace the string xyz by abc within a file globally. © 2012 Pradeep Tewani<pradeep@sysplay.in> 45 All Rights Reserved.
  • 45. Shell ScriptingCollection of shell commands stored in a file Has a variables and flow control statements like other programming languageShell Scripts are interpreted, not compiled Shell reads commands from the script line by line Searches for those commands on the system Interprets into the machine code as every command is executed Far slower than compiled language © 2012 Pradeep Tewani<pradeep@sysplay.in> 46 All Rights Reserved.
  • 46. Where to use Shell Scripting?System Administration Automate tasks Repeated tasksDevelopment Allows testing a limited sub-set of functionality Testing toolsDaily usage Simple scripts © 2012 Pradeep Tewani<pradeep@sysplay.in> 47 All Rights Reserved.
  • 47. Special Characters~: The current users home directory$ : used to access a variable (eg : $HOME)& : used to put a command in the background* : wildcard, matching zero or more characters (eg : ls doc_*)? : wildcard, matching exactly one character (eg: ls doc_?)${#} : No of arguments passed to shell script${@} : Value of all arguments passed$0 : contains the name of the script ur execauted © 2012 Pradeep Tewani<pradeep@sysplay.in> 48 All Rights Reserved.
  • 48. DIUHow do you find what shell you are using?What does $? denote?List the various shells available on your system. © 2012 Pradeep Tewani<pradeep@sysplay.in> 49 All Rights Reserved.
  • 49. Invoking ScriptsExample vi hello.sh and type the following inside it: #!/bin/bash echo “Hello World”The first line tells the script to use the bash interpreter to run this script.Then, make the script executable chmod 700 hello.sh ./hello.sh © 2012 Pradeep Tewani<pradeep@sysplay.in> 50 All Rights Reserved.
  • 50. VariablesVariables are a way of storing information temporarily. Example :- NAME="SysPlay” Example :- x=10A couple of conventions we need to follow Variables usually appear in uppercase There is no white space between the variable name and the equal signVariable substitution echo “Hello $NAME”Variable assignmentBash variables & Environmental variables. © 2012 Pradeep Tewani<pradeep@sysplay.in> 51 All Rights Reserved.
  • 51. White Space & LinebreaksBash shell scripts are very sensitive to whitespace & line-breaks Because the “keywords” of this programming language are actually commands evaluated by the shell. Need to separate arguments with whitespace. Likewise a linebreak in the middle of command will mislead the shell into thinking the command is incomplete. Example – x=10; x = 10; x = “OK”, x=”OK” © 2012 Pradeep Tewani<pradeep@sysplay.in> 52 All Rights Reserved.
  • 52. Single, Double & Back QuotesSingle Quotes Causes the variable name to be used literally No substitution will take place They dont honour special characters such as $ Eg : var=test string Newvar=Value of var is $var echo $newvar gives Value of var is $var © 2012 Pradeep Tewani<pradeep@sysplay.in> 53 All Rights Reserved.
  • 53. DIUEcho Hello $USERNAME, How are utouch {hello, bye}.{world,earth} and touch{hello,bye}.{world,earth} © 2012 Pradeep Tewani<pradeep@sysplay.in> 54 All Rights Reserved.
  • 54. Single, Double & Back Quotes... Using double quotes to show a string of  characters will allow any variables in the quotes  to be  resolvedExample $ var=“ test string” $ newvar=“ Value of var is $var” $ echo $newvar Value of var is test string © 2012 Pradeep Tewani<pradeep@sysplay.in> 55 All Rights Reserved.
  • 55. Single, Double & Back Quotes..Backticks Allows to run a command Capture the output of the command Eg Date = `data` echo “$Date”The equivalent construct is $( ) Eg: DATE=`date` is equivalent to DATE=$(date) © 2012 Pradeep Tewani<pradeep@sysplay.in> 56 All Rights Reserved.
  • 56. Arithmetic in ShellMath & CalculationAvailable Operators +, -, *, /, %With expr I=10 I=$(expr $i + 5) or I=`expr i + 5`With $(()) I=10, $((I=I+5)), echo $I © 2012 Pradeep Tewani<pradeep@sysplay.in> 57 All Rights Reserved.
  • 57. Command Line ArgumentsShell scripting can accept command linearguments.Within a shell script, you can refer to these argsas $1, $2, $3 and so$# - Number of command line arguments$* - Display all the arguments$0 – Name of the script © 2012 Pradeep Tewani<pradeep@sysplay.in> 58 All Rights Reserved.
  • 58. If-then ConditionSimplest Form isif test then expressionfi © 2012 Pradeep Tewani<pradeep@sysplay.in> 59 All Rights Reserved.
  • 59. if-then-elif-else statementMultiple elif blocks can be strung together to make an elaborate set of conditionalresponsesif [ condition_A ] then code to run if condition_A true elif [ condition_B ] then code to run if condition_A false and condition_B true else code to run if both conditions false fi © 2012 Pradeep Tewani<pradeep@sysplay.in> 60 All Rights Reserved.
  • 60. case statementThe structure of the case statement is as follows case $1 in pattern) .... .... ;; pattern) .... ;; *) ..... .... ;; esac © 2012 Pradeep Tewani<pradeep@sysplay.in> 61 All Rights Reserved.
  • 61. String TestAn expression can be: String comparison, Numeric comparison, File operators and Logical operators and it is represented by [expression]:String Comparisons: =  compare if two strings are equal !=  compare if two strings are not equal ­n  evaluate if string length is greater than zero ­z  evaluate if string length is equal to zero © 2012 Pradeep Tewani<pradeep@sysplay.in> 62 All Rights Reserved.
  • 62. ExpressionsExamples:[ s1 = s2 ]  (true if s1 same as s2, else false)[ s1 != s2 ]  (true if s1 not same as s2, else  false)[ s1 ]  (true if s1 is not empty, else false)[ ­n s1 ]  (true if s1 has a length greater then 0, else  false)[ ­z s2 ]  (true if s2 has a length of 0, otherwise false) © 2012 Pradeep Tewani<pradeep@sysplay.in> 63 All Rights Reserved.
  • 63. Numeric TestsNumber Comparisons: ­eq compare if two numbers are equal -ge compare if one number is greater than are equal to num -le compare if one number is lessthan or equal to a num -ne compare if two numbers are not equal -gt compare if one number is greater than another number -lt compare if one number is less than another number © 2012 Pradeep Tewani<pradeep@sysplay.in> 64 All Rights Reserved.
  • 64. Combining & Negating TestsLogical operators:! negate (NOT) a logical expression­a logically AND two logical expression­o  logically OR two logical expressions © 2012 Pradeep Tewani<pradeep@sysplay.in> 65 All Rights Reserved.
  • 65. User Input to Scriptread A shell built in comman read X Y Type in – 12 25 echo $X $Y read -p “Enter two numbers :” X Y read -p “Enter your name: “ Name echo $NAME © 2012 Pradeep Tewani<pradeep@sysplay.in> 66 All Rights Reserved.
  • 66. Flow Control with Forfor i in 1 2 3 4do echo “$i”donefor NAME in Santosh Shahid Manish echo “$NAME”done © 2012 Pradeep Tewani<pradeep@sysplay.in> 67 All Rights Reserved.
  • 67. Flow Control – While and UntilGeneral syntax: while [ condition is true] do code block done until [ condition is true] do Code block Done © 2012 Pradeep Tewani<pradeep@sysplay.in> 68 All Rights Reserved.
  • 68. FunctionsFunctions allow to segment the code intomeaningful byte size chunkfunction name(){}The word function is optional. © 2012 Pradeep Tewani<pradeep@sysplay.in> 69 All Rights Reserved.
  • 69. What all have we Learnt?Introduction to Linux & Linux ArchitectureKernel Mode & User ModeLinux Directory StructureLinux File types & Permissions, File commandsShell Basicsvi EditorShell Scripting © 2012 Pradeep Tewani<pradeep@sysplay.in> 70 All Rights Reserved.
  • 70. Any Queries?© 2012 Pradeep Tewani<pradeep@sysplay.in> 71 All Rights Reserved.