4.intro2unix notes

Upload: wesley-mutai

Post on 07-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 4.Intro2UNIX Notes

    1/114

    1. Week 1 - Introduction to LinuxLecture Notes

    Course Overview

    Course outline, evaluation, syllabus, and other administrative details.

    Course emphasis on: Linux use in the real world, administrator responsibilities,comparison with Windows OSs, getting comfortable with the shell.

    Recommended books: Running Linux, O'Reilly (lab bookshelf); Linux in a Nutshell,O'Reilly (command reference)

    Introduction to LinuxHistory of UNIX and Linux

    1965: Bell Labs (AT&T), MIT, and GE develop Multiplexed Information and ComputingService (MULTICS). Features multi-user, multi-processor, hierarchical file system.MULTICS never really succeeded as a product.

    1969: AT&T drops out of MULTICS project. Ken Thompson and Dennis Ritchie decideto port Space Travel game to unused PDP-7 minicomputer, and in doing so realize itcould host a MULTICS-like operating system. Thompson and Ritchie (along with DougMcIlroy and JF Ossanna) write the Uniplexed Information and Computing Service(UNICS). UNICS is quickly renamed UNIX.

    1971: UNIX now running on a Digital Equipment Corporation PDP-11 with 16KB ofRAM (8KB for applications) and a whopping 512KB of disk.

    1972: Dennis Ritchie and Brian Kernighan modify the BCPL language into B and then C.By late 1972, UNIX is largely rewritten in C, allowing it to be ported to other computerarchitectures much more readily.

    1974: AT&T licences UNIX to universities who can clearly see the educational potential. 1977: 500 UNIX installations worldwide. 1980: Berkeley's enhancements released as Berkeley Software Distribution (BSD) 4.1. 1983: BSD 4.2 released, AT&T releases System V, and Sun (also a Berkeley spinoff)

    releases SunOS. 1984: 100,000 UNIX installations worldwide. 1988: AT&T and Sun join forces to produce System V Release 4, later rebranded as

    UnixWare and Solaris 2. 1991: Linus Torvalds of Helsinki, Finland, decides the free but limited Minix operating

    system is too confining and creates the Linux kernel version 0.01 for the 386. Firstversion of Linux is so limited, it requires Minix to compile the kernel.

    October 5, 1991: Linus releases the first official Linux kernel, version 0.02. The bashshell and gcc compiler could run on this kernel, but not much else. Fortunately, a numberof interested hackers from across the Internet took up the challenge to build on Linus'swork.

    March 1994: Linux version 1.0 released. 1996: 3 Million UNIX systems shipped world-wide. Many different brands of UNIX:

    Linux, HP-UX (Hewlett Packard), AIX (IBM), Solaris (Sun), FreeBSD, etc. January 2002: There are aguestimated18 Million users of Linux.

    http://college.yukondude.com/2002_01_comp225/notes/week-1.phphttp://college.yukondude.com/2002_01_comp225/notes/week-1.phphttp://counter.li.org/http://counter.li.org/http://counter.li.org/http://counter.li.org/http://college.yukondude.com/2002_01_comp225/notes/week-1.php
  • 8/4/2019 4.Intro2UNIX Notes

    2/114

    Open SourceGNU and the GPL

    1983: Richard M Stallman (RMS) at MIT founds the Free Software Foundation(www.fsf.org).

    RMS believes all software should be freely available, including source code, withoutrestrictions.

    Money to be made from service and customization rather than software sales. To prove the point, RMS and others start the GNU (GNU's Not Unix) Project to duplicate

    all of the UNIX system utilities and programs and release them under a new license. The GNU General Public License (GPL) requires source code to be distributed alongside

    binaries, and that all programs that arise from modifications to the source, or that are builton top of this source, are also licensed under the GPL.

    GPL-licensed software can be sold for profit. "copyleft" describes this form of licensing: credit for work, rather than control of the

    work.

    copyleft is not public domain: author(s) of the work are still clearly defined. Many other Open Source licenses: GNU Library/Lesser General Public License (LGPL)

    does not require products build on top of LGPL-licensed code to be released under theLGPL or GPL, Apache, Mozilla, Python, IBM Public License, BSD, Intel Open SourceLicense, etc.

    Linux originally released under restrictions that it cannot be sold, but later released underGPL.

    How can Open Source Exist?

    Volunteers write code for many reasons: necessity (like Linus), to make a name foroneself, and because it's fun.

    To make money, consider analogy of giving away VCRs but then selling the tapes. IBM is a big Linux booster, in part because they make their big money through service

    contracts to support and maintain systems like Linux.

    Why Open Source?

    For popular projects like Linux, so many eyeballs go over the code that very few bugs orsecurity holes last for long.

    Less popular projects may be very buggy. Easy to create new or custom software based on existing source. Usually free, or at least cheap. Good for the soul. How much more money does Bill Gates need anyway?

    What is Linux?The Kernel

    "Linux" is just the operating system kernel.

    http://www.fsf.org/http://www.fsf.org/http://www.fsf.org/http://www.fsf.org/
  • 8/4/2019 4.Intro2UNIX Notes

    3/114

    The kernel is the intermediary between application programs and the computer hardware.The kernel manages: process scheduling, device I/O, virtual memory, file management(at a lower level than the filesystem)

    The kernel protects users and processes from each other. If a program crashes, it shouldnot affect any of the user's other programs, nor any of the other users.

    Linux kernel very stable. Crashes are rare.

    Windows NT/2K kernel hard to see, except when it crashes: Blue Screen of Death.Usually a device driver is the culprit.

    The Operating System

    Technically, the operating system should be called "GNU/Linux" because only thekernel can really be called Linux.

    All of the rest of the OS, shell, utilities, networking, etc. is part of the GNU Project. Nevertheless, "Linux" is widely used to describe the whole of the OS.

    Why Linux?

    It's Open Source: free as in speech, free as in beer. Also enjoys the stability and security of a popular Open Source project. 35 years of UNIX knowledge has been built up and almost all of it applies to Linux. Minimal hardware requirements compared with similar Windows or Macintosh systems. Highly configurable. A hacker's delight! Runs on a host of different processors: x86, Alpha, SPARC, PowerPC, ARM, 680x0, and

    many more. Very popular. Jump on the bandwagon!

    Why Not Linux?

    Not as mature as the other UNIXes, especially on big iron hardware (although IBM ischanging this).

    Not as well supported as the large UNIX products. Still not user-friendly for the desktop. Linux mostly runs on the server.

    Linux Distributions (Linux Distros)

    Kernel Versioning

    2 kernel versions at all times: stable and development.

    Stable version is the one to use. Development (or Beta) version is strictly for kernel-hackers. Version numbering is: major.minor.patchlevel If minor number is even, then it is a stable version. If minor number is odd, then it is a development version. Linux kernels obey these rules, but most other software versioning doesn't. As of today, current stable kernel version is 2.4.17, and current development version is

    2.5.1.

  • 8/4/2019 4.Intro2UNIX Notes

    4/114

    Distributions

    You could download the kernel and all of the GNU Project utilities, create an install,configure everything manually, and you'd have a working Linux machine.

    Or, you could save yourself a year or so of effort and buy or download a LinuxDistribution.

    Linux distributions contain: the kernel, GNU Project utilities, system admin tools,documentation, installation tools, hardware device drivers, technical support, GUIs (Xand window managers), productivity apps (word processors, spreadsheets, graphicsstuff), and so forth.

    Companies bundle up all of these files and programs into a Distribution which they thensell, or give away.

    Version number of distributions has nothing to do with version number of kernel, or withversion numbers of the other distributions for that matter.

    Popular distributions include: Ubuntu, Red Hat, Debian, Mandrake, SuSE, Slackware,and others (see Resources section)

    Red Hat: Used to be the most popular distribution.

    Debian: created by GNU folk. Not for the Linux beginner. Tends not to include utilitiesuntil they are thoroughly tested.

    Mandrake: appeals to the first time Linux user. Very smooth installation process. SuSE: favoured by developers. Slackware: one of the first, but takes a very DIY approach to installation and

    configuration. Most distributions also have different packaging: basic, workstation, server, enterprise,

    etc.

    Distribution Versions

    Red Hat 6.0 (Hedwig) = 2.2.5 kernel Red Hat 6.1 (Cartman) = 2.2.12 kernel Red Hat 7.0 (Guinness) = 2.2.16 kernel Red Hat 7.1 (Seawolf) = 2.4.2 kernel Red Hat 7.2 (Enigma) = 2.4.7 kernel, with most recent patches = 2.4.9-13 kernel Debian 2.2r4 (Potato) = 2.2.19 kernel Mandrake 8.1 = 2.4.8 kernel SuSE 7.3 = 2.4.10 kernel Slackware 8.0 = 2.4.5 kernel

    Shell Basics

    What is the Shell?

    The shell is the interface between the user and the operating system. The commandprompt and the GUI are built on top of the shell.

    In class, "shell" and "command prompt" will be used interchangably.

  • 8/4/2019 4.Intro2UNIX Notes

    5/114

    There are a number of different shells available: Bourne (the first), C (uses a C-likesyntax), Korn (combines best of Bourne and C), the Bourne Again Shell (bash, also acombo of the others), and more.

    The Bourne Again Shell, bash, is the default for Linux and the one we'll use. It is partof the GNU Project.

    Bash is responsible for: command line editing, job control, stream manipulation (piping& redirection), wildcard expansion, aliases, file completion, command history, variables,control structures, sub shells, etc.

    Linux vs. Windows

    In Linux, letter case matters: FOO.txt is different from Foo.txt is different from foo.txt. Linux (and the Internet) uses forward slashes (/) when specifying a directory path,

    Windows uses backslashes (\). Linux commands usually use hyphen (-) for command switches, Windows uses a

    combination of hyphens and slashes (/).

    Logging In

    There are two types of Linux users: root, and everybody else The root user has complete control over the system, for that reason do not log in as root

    unless you need to perform a system administration task . All other times, log in as aregular user ("student") so that a mistake can't wipe out the system.

    Linux will display a login prompt. Enter your user name and hit return, and then enteryour password and hit return.

    If you log in to a GUI console, you can start the command prompt by selecting the shellicon on the bottom of the GNOME (foot in front of a monitor) or KDE (seashell in front

    of a monitor) screen. If you log in to a text console, you're already in the command prompt.

    Virtual Consoles

    You may run multiple independent login sessions through Linux's virtual console feature.Typically 6 of these are available.

    To access the different consoles, press Alt and one of the function keys F1 through F6together.

    Depending on the configuration you may not need to log in when you switch to a newconsole.

    The virtual consoles are completely independent, you cannot share items (like theclipboard, for example) between them. They can share files though.

    Logging Out

    If you're in a GUI window command prompt, use "exit" to close the window. Then selectLogout from the GNOME or KDE main menu (bottom left of screen).

    If you're logged in through text mode, use "logout" to log out of the system.

  • 8/4/2019 4.Intro2UNIX Notes

    6/114

    "exit" can also be used to logout of a text mode session.Readings

    Chapter 1 Chapter 4, pp105-117

    Chapter 6

    2. Week 2 - FilesystemsLecture Notes

    Miscellaneous InfoHandouts

    Most of this week's notes are included in the handouts. Make sure to read them closely,especially the examples.

    Pronunciation

    Pronunciation of punctuation can be troublesome when describing shell command syntax,so the following is a list of the common names (most common first):

    ! = bang * = splat, star @ = at, strudel . = dot, point, period # = hash, crunch, pound, pound sign ~ = twiddle, tilde | = pipe, bar - = dash, minus, hyphen _ = underscore / = slash \ = backslash, escape & = amp, ampersand $ = dollar % = percent ^ = caret, hat, control ? = question ( = prance, parens, parenthesis [ = bracket, square bracket { = brace, curly

    ' = quote ` = back quote " = double quote

    Shell Tips Ctrl+C breaks (stops) a running program (eventually/usually) Ctrl+\ stops a running program (use if Ctrl+C didn't work)

    http://college.yukondude.com/2002_01_comp225/notes/week-2.phphttp://college.yukondude.com/2002_01_comp225/notes/week-2.phphttp://college.yukondude.com/2002_01_comp225/notes/week-2.php
  • 8/4/2019 4.Intro2UNIX Notes

    7/114

    Ctrl+D signals End of File (EOF). If you run the cat program without any parameters, itwill expect input from the keyboard instead. Use Ctrl+D to exit.

    Ctrl+S suspends output to screen. Use Ctrl+Q to restart. Ctrl+U erase command line Ctrl+Z suspends the current job. Use fg to restart.

    Use the TAB key to autocomplete file names when typing commands. Hit TAB again toshow list of possible matches if it beeps the first time. Use the up and down arrows to go back and forward in the command line history. Return

    will execute the command again.

    vi Editor

    Seems like stone age tool, but useful for 3 reasons: fits on a floppy, comes with everyUNIX/Linux distro you can think of, runs without GUI so it can be used remotelythrough telnet or ssh (with less keyboard map fiddling as usually in the case of Emacs)

    Actually quite powerful once you're used to it:Running Linux book was written using vi(and Emacs).

    At the very least, you should be able to open a file, make simple additions or changes,and save the file again using vi.

    vi Basics Type vi at the shell prompt to launch the vi editor. You can also specify a file to load on

    the command line. In vi parlance, the onscreen text is called the "buffer", just as"document" is used in most GUI word processors. Unlike most/any editor you're likelyused to, vi has "modes" as described below. You'll find that you switch back and forthbetween modes frequently.ConsultLearning the vi Editorto more fully embrace the vi experience.

    vi Modesvi Editor Quick Reference

    Mode Description

    Command

    Keystrokes are interpreted as commands for movement, entry into the Insert orReplace mode, deletions, search, replace, file manipulation, and so on. There are noindications that vi is in Command Mode. Press ESC once or twice until the terminalbeeps. The beep means Command Mode (sometimes called "Beep Mode").

    Insert,Replace

    Keystrokes are inserted (Insert Mode) into the buffer, or replace existing buffer text

    (Replace Mode). Either --INSERT-- or --REPLACE-- will appear at the bottom ofthe screen as a reminder of the current mode. Press ESC to return to CommandMode.

    Commands Commands are keystrokes entered while in Command Mode. The case of the command is

    significant. Some commands can be executed more than once by first typing the numberof repetitions. There are many, many more commands in vi than listed here.

    http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978156592426&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1501E0374307064A4E7DA37031BC914BC6CA2312http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978156592426&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1501E0374307064A4E7DA37031BC914BC6CA2312http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978156592426&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1501E0374307064A4E7DA37031BC914BC6CA2312http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978156592426&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1501E0374307064A4E7DA37031BC914BC6CA2312
  • 8/4/2019 4.Intro2UNIX Notes

    8/114

    Insert Commands Description Examples

    i, aEnter Insert Mode and begin inserting textbefore, or after the cursor, respectively.

    I, A

    Enter Insert Mode and begin inserting text at the

    beginning, or end of the current line,respectively.

    o, OEnter Insert Mode and begin inserting text on anew blank line inserted below, or above thecurrent line, respectively.

    rRemain in Command Mode but replace thecharacter under the cursor with the nextkeystroke.

    rQ replace the characterunder the cursor with thecharacter Q

    REnter Replace Mode and begin replacing text atthe current cursor position.

    cwDelete the word to the right of the cursor, andenter Insert Mode.

    3cw delete the 3 words tothe right of the cursor andenter Insert Mode

    cc Delete the current line, and enter Insert Mode.

    ESC Return to Command Mode.

    Movement

    CommandsDescription Examples

    h, j, k, l

    Move the cursor one position left, down, up, orright, respectively. On most terminals, the

    keyboard arrow keys will also work, even inInsert Mode.

    j move one line down,13l move thirteencharacters to the right

    w, bMove the cursor one word forward or backwardrespectively. Use capital W or B to move byword, including punctuation.

    4w move 4 wordsforward,3B move 3 wordsbackward treatingpunctuation as part of thewords

    eMove the cursor to the end of the word. Usecapital E to end of word, including punctuation.

    0, $ Move the cursor to the first or last position ofthe current line, respectively.

    GMove to the specified line number in the buffer.If no line number is given, move to the end ofthe buffer.

    1G move to the beginningof the buffer,13G move to 13th line ofthe buffer,G move to the end of the

  • 8/4/2019 4.Intro2UNIX Notes

    9/114

    buffer

    Ctrl+f, Ctrl+b

    Scroll forward or backward one screen,respectively. On most terminals, the keyboardPageUp and PageDown keys will also work,even in Insert Mode.

    3Ctrl+b scroll up 3screens

    Ctrl+gDisplay the current line number and other bufferinformation at the bottom of the screen.

    Ctrl+lRedraw the screen without changing anything.This may be necessary if the terminal displays amessage on the vi screen.

    Editing

    CommandsDescription Examples

    x, XDelete character under, or before the cursor,respectively. The deleted text can be restored

    with the p command.

    5X delete the fivecharacters before the

    cursor

    dwDelete one word to the right of the cursor. Thedeleted text can be restored with the pcommand.

    6dw delete the 6 words tothe right of the cursor

    ddDelete the current line. The deleted text can berestored with the p command.

    10dd delete the next 10lines, starting with thecurrent line

    DDelete to the end of the line. The deleted textcan be restored with the p command.

    p, P Put deleted text after, or before the cursor,respectively.

    ywYank (copy) the word to the right of the cursor.The yanked text can be restored with the pcommand.

    2yB yank the twoprevious words, includingpunctuation

    yyYank (copy) the current line. The yanked textcan be restored with the p command.

    4yy yank the next 4 lines,starting with the currentline

    J Join the current line with the following line.

    u Undo the last edit command.

    . Repeat the last edit command.

    SearchCommands

    Description Examples

    /pattern,?pattern

    Search forward, or backward forpatternrespectively. Type Return to begin the search.Thepattern can be a text fragment or a regular

    /root search forward for"root"?T[io]m search backward

  • 8/4/2019 4.Intro2UNIX Notes

    10/114

    expression. for "Tim" or "Tom"

    n, NRepeat last search in same, or differentdirection, respectively.

    :%s/find_pattern/replace_pattern/g

    Globally replacefind_pattern with

    replace_pattern in the buffer. Type Return tobegin the replacement. Either pattern may be atext fragment or a regular expression.

    :%s/usr/user/g replace

    every occurrance of "usr"with "user"

    File Commands Description Examples

    :w Write (save) the buffer to the file on disk.

    :wq Write (save) the buffer and quit vi.

    :x, ZZWrite (save) the buffer, if changes have beenmade, and quit vi. ZZ is a handy shortcut.

    :q!Quit vi without saving any changes, even if the

    buffer has been modified.

    :e!Restore the buffer to the version at the time ofthe last write (save).

    Linux Directory Structure

    The various distributions of Linux or UNIX tend to use slightly different directory structures, ororganize files into different locations. The following structure is typical of most Linuxdistributions and Red Hat in particular.

    Linux uses the term "directory" instead of "folder" as is preferred by the Macintosh and recentversions of Windows.

    Path Description

    /The root directory. Don't confuse this with the root user directory (/root). Theparent directory (..) of / is / itself.

    /bin Essential user commands, such as ls or bash.

    /bootLocation of the kernel itself, as well as the boot manager (e.g. LILO orGRUB).

    /dev

    Files that represent hardware devices. For instance /dev/hda2 corresponds to thesecond partition of the first IDE hard disk, and /dev/fd0 represents the first floppydrive. A special device, /dev/null, is like a black hole: anything that is copied orstreamed to /dev/null (also known as the "bit bucket") disappears forever.Another special device, /dev/zero, is like the opposite: it spews out an infinitestream of zeros (technically, ASCII NUL). There are usually many more files inthis directory than there are hardware devices installed. You'll rarely need towork directly with this directory.

  • 8/4/2019 4.Intro2UNIX Notes

    11/114

    /etc System configuration files. Similar in concept to Windows' Registry.

    /etc/rc.d The hierarchy for startup configuration scripts.

    /etc/rc.d/init.d Startup configuration scripts.

    /etc/rc.d/rcN.d

    Runlevel directories. Each of the rcN.d directories corresponds to runlevelN. The

    scripts in each directory (typically links to the scripts in /etc/rc.d/init.d) specifywhich services should start, or stop, for that particular runlevel, and in whichorder.

    /homeHome directories for user accounts (other than the root user). Each user'sdirectory will be directly beneath this directory: /home/user_name.

    /libSystem libraries shared by various programs. These library files are similar inintent to Windows' DLL files. The /lib/modules/kernel_version directory containsthe kernel modules, such as device drivers.

    /mnt

    The preferred location of temporarily-mounted device filesystems, such as thefloppy drive (/mnt/floppy) or the CDROM (/mnt/cdrom) drive. Depending onsystem settings, these device filesystems may be automatically mounted when adisk is inserted into the drive, or you may have to mount themselves using themount command.

    /proc

    The mount point for a virtual filesystem that contains files and directories thatrepresent current system settings and statistics. For instance, /proc/meminfoshows the current status of the system memory, and /proc/version displays thecurrent kernel version. There are also subdirectories for each of the runningprocesses, identified by the process ID (pid) numbers.

    /root The root user directory.

    /sbin Essential privileged commands, usable only by the root user, such as shutdown.

    /tmpTemporary files used by the system or users. Don't store important files in thisdirectory as they may be erased without notice.

    /usr

    The primary hierarchy for programs, data, and documentation on the system. The/usr directory may actually be located on a separate system, but accessedremotely. Therefore the /bin and /sbin directories must contain all of the utilitiesnecessary to launch the system before any network connections are made.

    /usr/bin The bulk of the user commands.

    /usr/local

    Programs, data, and documentation installed locally on this system. Thisdirectory is the preferred location for additional software packages that were not

    part of the initial Linux installation. The /usr/local/bin and /usr/local/sbindirectories parallel those higher up in the hierarchy.

    /usr/sbin The bulk of the privileged commands, usable only by the root user.

    /usr/srcSource code files for various programs on the system, possibly including thekernel itself.

    /var Variable (changing) administrative files, such as log files.

  • 8/4/2019 4.Intro2UNIX Notes

    12/114

    Linux Filesystem Basics

    File NamesWhile technically any character other than slash (/) or the ASCII NUL can be used in a filename(depending on the filesystem), it's best to restrict yourself to alphanumerical characters(remember that Linux is case-sensitive), underscore (_), hyphen (-), and period (.). While spacecharacters are allowed, they're more trouble than they're worth because you'll have to enclose thefilename in quotes every time you reference it.

    There is no concept of a "file extension" in Linux as there is in Windows. It's common to append".txt" to text files, or ".c" to C source files, but doing so has no intrinsic meaning to the operatingsystem. Executable files are identified by their access permissions rather than an ".exe"extension (see the File Listings section below).

    File Types

    Almost every "thing" in Linux can be considered some type of file. The types of files are listedbelow (note that in this case "file type" does not distinguish a text file from a GIF image file, butindicates the various "things" that Linux treats like files). The Listing Character is the leftmostcharacter in the detailed file listing that is displayed using the ls -l command. The Listing Colouris the colour of the filename displayed by the ls command (in the default Red Hat installation atany rate).

    Listing

    CharacterListing Colour File Type Description

    -

    normal text

    colour, or greenfor executablefiles

    regular file A normal text, data, or program file.

    d dark blue directoryA directory, containing possibly other files ordirectories.

    l light bluesymboliclink

    A symbolic (soft) link to another file. See theLinks section below.

    b yellowblock-orienteddevice

    A device that reads and writes data in blocks, suchas a hard disk. Typically only found in the /devdirectory.

    c yellowcharacter-orienteddevice

    A device that reads and writes data in characters,such as a terminal or a serial port. Typically onlyfound in the /dev directory.

    p brown pipe

    An interprocess communication (IPC)mechanism. Also used in memory when piping (|)the output of one program into the input ofanother using the shell. Typically only found in

  • 8/4/2019 4.Intro2UNIX Notes

    13/114

    the /dev directory.

    s pink socket

    An interprocess communication (IPC)mechanism. Not exactly equivalent to TCP/IPsockets. Typically only found in the /devdirectory.

    LinksIt is possible to create a link to an existing file that, for all intents and purposes, appears to be theoriginal file, although it may have a different name or be in a different location. Links are createdusing the ln command. There are two types of link: hard and soft (symbolic).

    Hard links cannot easily be identified as such, although the file listing will reflect the number oftimes a file has been hard-linked (see the File Listings section below). Hard links also have otherlimitations: directories cannot be hard-linked (except by the root user), and links cannot crossfilesystem boundaries.

    Soft (symbolic) links are generally preferred because, even though they act just like hard links,they can easily be identified for what they are, and they don't suffer from the same limitations ashard links.

    Deleting a link (hard or soft) does not delete the file that is linked to.

    File ListingsWhen displaying files using the long output format (ls -l), a typical file might look as follows:

    -rwxr-xr-- 2 sally managers 48712 Jan 4 09:11 calcpayroll

    The first character is the file type, in this case "-" means a regular file.

    The next 9 characters indicate the file permissions. The permissions are split into three groups ofthree characters each: owner, group, and others. An r means the file can be read (or a directorycan be listed). A w means the file can be written to, or modified. An x means the file can beexecuted (or a directory can be navigated to); in other words it is a program of some sort. In thiscase, the first three characters, rwx means that the owner of the file can read, write, and executethis file. The second set of three characters, r-x means that members of the group can read andexecute the file, but not write to it. The final set of three characters, r-- means that any other usercan read the file, but cannot write to it nor execute it.

    The number 2 indicates the number of hard links to this file. If there are no hard links, a 1 will bedisplayed.

    sally is the owner of the file.

    sally belongs to the managers group, and members of that group have the access permissionsdescribed above (the second set of three permission characters).

    The file is 48712 bytes in size.

  • 8/4/2019 4.Intro2UNIX Notes

    14/114

    The file was last modified on January 4th (of this year) at 9:11AM. Older dates will display ayear instead of the time.

    The file is named calcpayroll.

    Filename ExpansionJust as in MS-DOS, the Linux shell can use wildcards to specify filename patterns. This is alsosometimes called "globbing". Don't confuse Filename Expansion with Regular Expressions,which are far more powerful and use a more complex syntax.

    Another important but subtle difference is that MS-DOS passes the wildcard string to thecommand program for it to handle. The Linux shell first evaluates the wildcard, expands it into alist of all of the matching files, and passes this full list to the command program. This distinctionbecomes apparent when you write shell scripts that accept filename parameters.

    Wildcard Description Examples

    ? Matches exactly one character. T?m matches Tim, Tom, T7m, etc.

    * Matches zero, one, or many characters.

    *.* matches any filename that includes aperiod (note difference from MS-DOS!),* matches all filenames,??* matches all filenames with at least twocharacters

    [set]Matches any one character in set. Ahyphen can be used to indicatesequences.

    s[aeI]t matches sat, set, and sIt;*[0-9] matches all filenames that end in adigit;[a-zA-Z]?* matches all filenames that begin

    with a letter, and are at least two charactersin length

    [!set]Matches any one character not in set. Ahyphen can be used to indicatesequences.

    T[!i]m matches Tom, Tum, T_m, and soforth, but not Tim

    FilesystemsA filesystem is a device (real or virtual) that can be formatted to store information organized asfiles. Many different filesystems can, and are, mounted (loaded) into the Linux directoryhierarchy. Some filesystems have limitations (e.g. MS-DOS's 8.3 filenames, and no concept ofaccess permissions) but the filesystem driver will attempt to present each filesystem in such away that the user usually doesn't know or care which filesystem he or she is using.

    The following table lists the most common filesystems. The Type column contains the nameused when mounting a filesystem using the mount command.

    Filesystem Type Description

    Second ext2 The most common Linux filesystem (prior to ext3).

  • 8/4/2019 4.Intro2UNIX Notes

    15/114

    Extended

    ThirdExtended

    ext3

    The latest Linux standard filesystem as of the 2.2.19 kernel but notincluded in many distributions until very recently (Red Hat 7.2). ext3features "journalling" which can help prevent file corruption duringcatastrophic system failure (e.g. power failure).

    FourthExtended

    ext4 Latest ext FS. Very fast and also supports Journalling

    Network FileSystem

    NFS Used for remote access to files on other *NIX systems on the network.

    DOS-FAT msdosUsed mainly for floppy disks. Filenames are limited to the old 8.3convention.

    VFAT vfatThe filesystem used by Windows 9x that supports long filenames. Not tobe confused with FAT32 though. It's common to use this filesystem onmulti-boot machines to share files between Windows and Linux.

    /proc procThe virtual filesystem used by the /proc directory to display processinformation.

    ISO 9660 iso9660The filesystem used by CD-ROMs. Current drivers include the Jolietextensions that allow long or Unicode filenames.

    SMB smbfs Used for remote access to files on Windows machines (Samba).

    Week 2 Linux Commands

    The following table demonstrates only some of the options possible for each command. Consult

    the man pages for the complete list, or a reference such asLinux in a Nutshell.

    HelpCommands

    Description Options Examples

    aproposDisplay all of the man pages thatmatch the search string

    none

    apropos filesystemdisplays all of the manpages relating tofilesystems

    info

    The GNU hypertext reader.Displays online documentation for

    most GNU Project commands.Type q to exit.

    see

    documentation

    infoegrep displays the

    documentation for theegrep utility

    man

    Display the online referencemanuals. Type q to exit. Themanual pages are divided intosections:1. executable programs orshell commands, 2. kernel system

    see

    documentation

    man man displays theonline manual for theman(1) command

    http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978059600025&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1601DC2D912E49D94EE7990E1A861C9B2C8D4305http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978059600025&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1601DC2D912E49D94EE7990E1A861C9B2C8D4305http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978059600025&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1601DC2D912E49D94EE7990E1A861C9B2C8D4305http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978059600025&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1601DC2D912E49D94EE7990E1A861C9B2C8D4305
  • 8/4/2019 4.Intro2UNIX Notes

    16/114

    functions, 3. library functions, 4.special files (devices), 5. fileformats and conventions, 6. games,7. macros and miscellaneous, 8.system administration commands,

    9. kernel routines. Usually you willbe interesting in sections 1 and 8.

    Navigation

    CommandsDescription Options Examples

    cd Change directory. none

    cd changes to the user'shome directory;cd ~ changes to the user'shome directory;cd .. changes to the parentdirectory (unlike MS-

    DOS, space must bebetween cd and ..);cd - changes to theprevious directory

    find

    Searches for a file or files thatmatch the specified conditions.

    File expansion wildcard patternsmust be enclosed in quotes (').

    -namepatternfind filenamesthat matchpattern;-type find filesof a specificLinux file type

    find / -name httpd.confdisplays any copy of thefile httpd.conf, beginningthe search at the rootdirectory;find /etc -name 'rc*d' -type d displays any

    directories that begin with"rc" and end with "d"under the /etc directory

    ls

    List files. If no parameters aregiven, list all of the files in thecurrent directory. It's common tohave an alias for ls -l called ll, andan alias for ls -d . [a-zA-Z]*, whichdisplays just the hidden files in adirectory, called l. ("el dot").

    -l long format;-d do notdescend intosubdirectories;-a display allfiles, includinghidden ones;-F append filetype flags to thelisting(/=directory,*=executables,@=symboliclinks, |=FIFOsor pipes,

    ls -laFsR ~ recursivelylists all of the contents theuser's home directory andall of its subdirectories,displaying block sizes foreach file and appending afile type flag to the

    filename if warranted;ls -d /etc/rc* displays allof the files beginning with"rc" in the /etc directorywithout descending intoany directories that beginwith "rc"

  • 8/4/2019 4.Intro2UNIX Notes

    17/114

    "="=sockets);-R recursivelylistsubdirectories;-s display the

    size of the filesin blocks

    pwdPrint the full pathname of thecurrent directory.

    nonepwd displays, forexample: /usr/local/bin

    File Display

    CommandsDescription Options Examples

    cat

    Display a file or files on thestandard output (terminal window).Short for "catenate". Often used

    with redirection operators tocombine files.

    see

    documentation

    cat httpd.confdisplaysthe httpd.conf filecontentscat one two three > all

    copies the contents ofone, two, and three, intoall

    fileDisplays the type of a file accordingto its contents.

    -L followsymbolic links

    file index.html displays:index.html: HTMLdocument text

    less

    Display the contents of a file onescreenful at a time. A "more"capable version of more. Type q toexit.

    see

    documentation

    less /etc/profile displaysthe contents of the/etc/profile file one screenat a time

    more

    Display the contents of a file onescreenful at a time. A "less" capableversion of less. Type q to exit.

    see

    documentation

    more /etc/passwddisplays the contents ofthe /etc/passwd file onescreen at a time

    FileManipulation

    Commands

    Description Options Examples

    cp

    Copy files from one location to

    another. It's common to create analias to cp -i called cp to preventaccidents.

    -i prompt beforeoverwriting;-fforce the

    copy, even ifthe target fileexists;-r recursive

    cp /tmp/foo* . copiesevery file in the /tmp

    directory that begins with"foo" to the currentdirectory (.)

    ln

    Create a link for a file. Hard linkscannot cross filesystem boundaries,nor can they be created for

    -s create asymbolic, orsoft, link

    ln -s /var/log/dmesgkernel.msg creates asymbolic link in the

  • 8/4/2019 4.Intro2UNIX Notes

    18/114

    directories unless you are the rootuser. The name or the location ofthe link can be different from thesource file.

    current directory calledkernel.msg that points tothe file/var/log/dmesg

    mkdir Create a directory. seedocumentation

    mkdir foo creates thedirectory foo under thecurrent working directory

    mv

    Move one or more files from onelocation to another, or rename afile. It's common to create an aliasto mv -i called mv to preventaccidents.

    -i prompt beforeoverwriting;-fforce themove, even ifthe target fileexists;-r recursive

    mv foo.txt /tmp/foo.txtmoves the file foo.txtfrom the currentdirectory to the/tmpdirectory;

    mv -i foo.txt quux.txtrenames foo.txt toquux.txt, but asks first if

    quux.txt already exists

    rm

    Delete (remove) files. Deleted filescannot be recovered It's common tocreate an alias to rm -i called rm toprevent accidents.

    -i prompt beforeoverwriting;-f force thedeletion;-r recursive

    rm -fr / tmp/*completely erases theentire system, andanything under the tmpsubdirectory of thecurrent directory if itexists (this is whyhabitually logging on asthe root user is a verybad idea);

    rm -fr /tmp/* erases justthe contents of the /tmpdirectory (notice subtledifference from theprevious example)

    rmdir

    Removes a directory, only if itcontains no files or subdirectories.Use rm -r (carefully!) to erasepopulated directories.

    see

    documentation

    rmdir ~/tempdir removesthe empty directorytempdir under the user'shome directory

    Filesystem

    Commands

    Description Options Examples

    fdformat

    Low-level format a floppy disk.Run on an unmounted floppybefore installing the filesystem. Anon-root user must have writepermissions on the floppy device.

    -n skipverification step

    fdformat /dev/fd0 formatsthe floppy disk

    mkfs Construct a filesystem on a device. -t type specifies mkfs -t msdos /dev/fd0

  • 8/4/2019 4.Intro2UNIX Notes

    19/114

    mkfs is a front-end for otherprograms specific to the givenfilesystem. Run on an unmounteddevice. A non-root user must havewrite permissions on the floppy

    device (and may not have mkfs intheir default path).

    the filesystemtype

    creates an MS-DOSfilesystem on the floppydevice;mkfs.msdos /dev/fd0same as above, using the

    specific filesystem mkfsprogram

    mount

    Mounts a filesystem to a point inthe directory hierarchy. The mountpoint (directory) must exist and

    should be empty. The /etc/fstab filelists which filesystem mounts areknown to the system, and whichcan be mounted by non-root users.If non-root users are permitted tomount certain devices (floppies andCD-ROMs typically) they do nothave any choice over the mountpoint or the filesystem used.Otherwise, only the root user ispermitted to execute this command.

    -t type specifiesthe filesystemtype to mount;-r mounts thefilesystem read-only (requiredfor CD-ROMdrives)

    mount displays thecurrent mounts (root &non-root);mount -t msdos /dev/fd0/mnt/floppy mounts anMS-DOS-formattedfloppy in the first floppydisk drive at the location/mnt/floppy (root only);

    mount -t iso9660 -r/dev/cdrom /mnt/cdrommounts a CD-ROM in theCD-ROM drive at thelocation /mnt/cdrom (rootonly),mount /mnt/floppymounts the floppy usingthe filesystem specified inthe /etc/fstab file (root &non-root);

    mount /dev/cdrom mountsthe CD-ROM at thelocation specified in the/etc/fstab file (root & non-root)

    umount

    Unmounts a filesystem from thedirectory hierarchy previouslymounted using the mountcommand. Always unmount aremovable-media device (e.g.

    floppy, CD-ROM) before ejectingthe media. This precaution isnecessary because Linux buffers itswrites to devices and your mostrecent changes may not have beencommitted to the media. Even withCD-ROMs, it's a good idea tounmount the disk so that Linux

    seedocumentation

    umount /mnt/floppyunmount the floppy disk;

    umount /dev/fd0 unmountthe floppy disk (same asabove);umount /mnt/cdromunmount the CD-ROM

  • 8/4/2019 4.Intro2UNIX Notes

    20/114

    doesn't become confused. Undercertain circumstances a non-rootuser may unmount somefilesystems (floppies and CD-ROMs typically) but otherwise only

    the root user can execute thiscommand.

    Session

    CommandsDescription Options Examples

    exitExits the current (sub)shell.Equivalent to logout if run from theparent shell.

    see

    documentationexit exits the current shell

    logoutLogs the user out of the system.Use exit if you are currently withina subshell.

    nonelogout logs the user out ofthe system

    Week 2 Class Exercises

    Perform all of these exercises through the shell terminal window.

    1. Change your user password on the nfs2 server:Use the telnet program to remotely log in to the nfs2 server: telnet nfs2Your user name is the first letter of your first name followed by your complete last name.Your password is your student number without any leading zeros.Change your password: passwd

    You will have to enter your old password first, and then your new password twice.Exit the telnet program: exitTry telnet-ing back into the nfs2 server using your new password to make sure it works.

    2. Experiment with the apropos, man, and info commands:Use the commands whenever you're unsure about the syntax of a command. For instance,look up any man pages that mention links. Then look at the man page and the info pagefor the ln command.

    3. Find an example of each of the Linux filetypes on your system:Use the cd command to move around, and the ls -l command to view the full listing of adirectory. Try to find examples of: normal files, directories, links, block devices,character devices, pipes, and sockets. Hint: the last four can be found in the /devdirectory. Use the -d and -F options for ls, together with wildcards, to help identify someof these file types.

    4. Look for files on the system using the find command:Search for the following: the ls command, directories under /etc that begin with "rc", thePine configuration file: pine.conf.

  • 8/4/2019 4.Intro2UNIX Notes

    21/114

    5. Use the cat, more, and less commands to view files: Take a look at the following: /etc/fstab, /etc/passwd, and the pine.conf file from theprevious step. The first two are relatively short, while the last is probably many screenslong.

    6. Create a symbolic link:Create a symbolic link in your home directory to the file /etc/passwd. Experiment withviewing the file through the link. View the listing of the link using the ls -l command. Tryto modify it.

    7. Create and delete new files and directories: In your home directory, create a new directory called "scratch". In that directory create anew file using vi called "erase.me". You can type whatever you'd like into the file. Exit viand use cat, or less, or more, to view the contents of the erase.me file. Try to delete thescratch directory using the rmdir command. Delete the erase.me file first and then trydeleting the directory again.

    8. View the filesystems currently mounted on your system:Run the mount command without any options to view the current mounts. Are there anythat you can't explain? If you have a floppy disk handy, put it into the machine and see ifthe mount then shows different information. If not, try mount /dev/fd0 first. Don't forgetto umount.

    9. Add a HTML home page to your account on the nfs2 server:Telnet into the nfs2 server as in the first step. Change to the public_html directory. Usingvi, create a new file called "index.html". Enter the following into the file:This isyour_name_here's home page.. Save the file andtry viewing it through your systems browser by going to http://nfs2/~your_username/. If

    you want to view it outside of the College, use the URLhttp://netlab2.yukoncollege.yk.ca/~your_username/. You can use this site for the durationof the course for whatever legitimate and non-offensive purposes you'd like. The sites aremonitored by College administrators for inappropriate content.

    10.Logout and shut down the system:Exit the terminal shell program using the exit command. Log out of the system throughthe graphical interface (click the bottom-left icon then choose logout from the menu).Shut down the system by selecting the shutdown option from one of the login dialogbox's menus.

    Week 3 - System Startup and the KernelLecture Notes

    Handout:Linux Startup Sequence

    Handout:Week 3 Linux Commands

    Handout:Week 3 Class Exercises

    Readings:Linux Network Administrators Guide Chapter1

    http://college.yukondude.com/2002_01_comp225/notes/week-3.phphttp://college.yukondude.com/2002_01_comp225/notes/week-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-startup-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-startup-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-startup-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-commands-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-commands-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-commands-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-exercises-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-exercises-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-exercises-3.phphttp://www.linuxdoc.org/LDP/nag2/index.htmlhttp://www.linuxdoc.org/LDP/nag2/index.htmlhttp://www.linuxdoc.org/LDP/nag2/index.htmlhttp://www.linuxdoc.org/LDP/nag2/index.htmlhttp://www.linuxdoc.org/LDP/nag2/x-087-2-intro.htmlhttp://www.linuxdoc.org/LDP/nag2/x-087-2-intro.htmlhttp://www.linuxdoc.org/LDP/nag2/x-087-2-intro.htmlhttp://www.linuxdoc.org/LDP/nag2/x-087-2-intro.htmlhttp://www.linuxdoc.org/LDP/nag2/index.htmlhttp://www.linuxdoc.org/LDP/nag2/index.htmlhttp://college.yukondude.com/2002_01_comp225/notes/ho-exercises-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-commands-3.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-startup-3.phphttp://college.yukondude.com/2002_01_comp225/notes/week-3.php
  • 8/4/2019 4.Intro2UNIX Notes

    22/114

    (Intro to Networking); Chapter2(TCP/IP)

    Alternate Readings:Running Linux Chapter 15, pp514-533 (TCP/IP)

    Week 3 Lecture Notes

    System Startup

    The Linux System Startup handout covers the sequence of startup steps in detail. Theimportant parts to keep in mind are: the bootloader (LILO or GRUB), kernel messages(visible using dmesg), the init process and the rc scripts, and the roles of /etc/profile,.bash_profile, and .bashrc.

    Bootloaders

    LILO (LInux LOader) is the most common bootloader and the one installed in the lab. The configuration file for LILO is /etc/lilo.conf. The bootloader's job is to select an operating system/kernel version to load. Typically the OS will be Linux but it is possible to use LILO to boot MS-DOS or

    Windows 9x on dual-boot machines. LILO can also boot different versions of kernels that are installed on the machine. LILO is responsible for the Red Hat logo screen that appears while the machine is

    starting up. It can be configured to allow the user to choose an operating system and/orkernel version. There is usually a default option as well if the user doesn't make aselection in a given period of time.

    LILO cannot be used to dual-boot OS/2, Windows NT/2000/XP. Those operatingsystems have to be installed in the MBR (Master Boot Record) and so conflict with LILOif it too is installed in the MBR.

    It is possible to use WinNT/2K/XP as the "master" bootloader which in turn can callLILO on a Linux partition, but this can be tricky to configure. See the articles: Linux +Windows 95 mini-HOWTO, and NT OS Loader + Linux mini-HOWTO in the Resourcessection of this site for more information.

    GRUB (GRand Unified Bootloader) is another bootloader. It's part of the GNU Project. GRUB is much more flexible than LILO, but in most respects does exactly the same

    thing as LILO.

    The init Process

    The init process is the master process in Linux. It has Process ID 1 (PID). init isresponsible for launching all other processes.

    init is configured through the /etc/inittab file. init's job during startup is the following: set the default runlevel, run the rc.sysinit script

    (usually in /etc/rc.d), run the rc script (usually in /etc/rc.d), install an interrupt handler tocatch Ctrl+Alt+Del sequences, and launch the tty processes that handle each of the virtualterminals. Each of these tty processes in turn runs the login process that is responsible foraccepting the user's login name and password, and then launching the user's shell andrunning the /etc/profile script for global settings.

    http://www.linuxdoc.org/LDP/nag2/x-087-2-intro.htmlhttp://www.linuxdoc.org/LDP/nag2/x-087-2-issues.htmlhttp://www.linuxdoc.org/LDP/nag2/x-087-2-issues.htmlhttp://www.linuxdoc.org/LDP/nag2/x-087-2-issues.htmlhttp://www.linuxdoc.org/LDP/nag2/x-087-2-issues.html
  • 8/4/2019 4.Intro2UNIX Notes

    23/114

    Runlevels

    A runlevel is a "mode of operation that provides a particular set of services." Linux has 7 runlevels: 0 through 6. Runlevels 0, 1, and 6 are standardized across distributions, but the meanings of the other

    runlevels can vary considerably. Runlevel 0: halts the system Runlevel 1: single-user text mode. Usually used to diagnose and repair serious problems. Runlevel 2: multi-user text mode without networking (Red Hat). Rarely used. Runlevel 3: multi-user text mode with networking (Red Hat). The usual runlevel for

    servers that don't use the X Windows GUI. Runlevel 4: not used (Red Hat) Runlevel 5: multi-user X Windows mode with networking (Red Hat). The usual runlevel

    for graphical workstations (as in the lab). Runlevel 6: reboots the system The runlevel program (in /sbin) displays the current and previous runlevels.

    The rc Script

    The rc script (usually in /etc/rc.d) is called by init to launch the services depending on thecurrent runlevel (also set by init).

    Simply put, rc runs all of the scripts in one of the /etc/rc.d/rcN.d directories (N= runlevel)in alphabetical order.

    In each rcN.d directory, there will be a number of files beginning with "K" (Kill) and "S"(Start). Following the K or S, a two digit number is used to precisely order the files.These files are usually symbolic links to scripts in the /etc/rc.d/init.d directory. Links areused so that multiple copies of script files don't have to be copied amongst the various

    runlevel directories. Each of these scripts is responsible for starting or stopping (killing) aservice: printer daemons, web servers, databases, email, firewalls, etc. rc first runs all of the Kill scripts in order. This can be thought of as a "clean-up"

    operation for runlevels other than 0 and 6. As expected, the scripts listed in the rc0.d andrc6.d directories are mostly Kill scripts since there is very little that needs to be startedwhen the system is shutting down.

    rc then runs all of the Start scripts in order. For runlevels 2 through 5, the last Start script is usually /etc/rc.d/rc.local. The rc.local

    script is a handy place to put any of your own service initialization for which you don'thave a full startup script like those in the /etc/rc.d/init.d directory.

    The green "OK" messages that appear during startup (or shutdown) are produced as the rcscripts iterates through the rcN.d directory.

    The Shell rc Scripts

    The /etc/profile script contains global settings for all users (including root). This is theplace to put any special settings that apply to everyone (e.g. "safe" versions of rm, mv,and cp).

  • 8/4/2019 4.Intro2UNIX Notes

    24/114

    The .bash_profile script (in the user's home directory) contains any settings that onlyapply to that particular user. .bash_profile is only run once when the user first logs in. Itusually calls the .bashrc script to do most of the work.

    The .bashrc script (in the user's home directory) also contains user-specific settings, but itis run for every new shell the user opens. This is usually the best place to put personal

    settings.

    Kernel

    /boot

    The kernel is typically located in the /boot directory. The compressed version of the kernel is called "vmlinuz" ("vm" = Virtual Machine, "z" =

    Compressed/Zipped). This is usually a symbolic link to the full name of the compressedkernel which may include the kernel version number.

    Many different kernel versions may be in /boot. The vmlinuz link will usually point to themost recent one, but LILO can still be used to select any particular kernel during startup.

    Compiling the Kernel

    While not common, it is possible to compile the kernel itself from source. The main reason for doing so is to trim away any unnecessary code, thereby producing a

    lean 'n' mean kernel that consumes less memory. This may be necessary to run Linux on amachine with limited hardware (esp memory).

    The sorts of things that can be pruned out of the kernel include: device drivers, floating-point emulation, SMP (multi-CPU) support, loadable modules, networking, PCI support,parallel port support, Plug 'n' Play card support, ISDN, sound, SCSI, etc.

    Kernel Modules

    While Linux is thought of as a "monolithic" kernel (meaning that it is one big programwith all of the device drivers built-in), recent versions of the kernel support loadablemodules.

    These modules, usually device drivers, can be manually or automatically loaded asneeded, rather than having to re-compile the kernel each time a new piece of hardware isadded to the machine.

    The modules themselves are stored in the /lib/modules/kernel_version directory. The /boot/module-info file contains short descriptions of the modules installed on the

    system. The kerneld(replaced by kmodin more recent distributions) daemon process can

    automatically load or remove modules as they are needed. The /etc/modules.conf file (this used to be called conf.modules) lists the modules that

    should be loaded by the system during startup. The lsmod command lists modules currently loaded in the kernel. Non-root users can run

    this command, but may have to run it as /sbin/lsmod since it won't be in the user's path.

  • 8/4/2019 4.Intro2UNIX Notes

    25/114

    Shell Environment Variables

    Linux makes extensive use of environment variables for configuration or personalizationsettings.

    By convention, environment variable names are always fully capitalized.

    When referring to an environment variable, the variable is prefixed with "$".

    The Path

    Linux has the concept of a "path" very much like in Windows/MS-DOS. The path is a setof directories that the shell searches though to locate a command you enter on thecommand line.

    The current path is stored in the $PATH environent variable. Directories are separated bycolons (:).

    Unlike MS-DOS, Linux does not consider the current working directory part of the pathunless the "." directory is explicitly included in $PATH.

    To run a program or script in the current directory, the program name must be prefixedwith "./" to indicate to the shell that you mean to execute the program in this directory.

    The current directory (.) is not usually included in $PATH for security reasons.Aliases

    Aliases are handy synonyms for commands, or specific versions of commands. The current aliases can be listed by running the alias command without any parameters. Aliases can be deleted using the unalias command.

    Quotes

    Single quotes (') and double quotes (") have slightly different meanings to the shell. Use single quotes to specify literal strings (i.e. the shell won't evaluate anything within

    the string). Use double quotes if you want environment variables within the string to be evaluated.

    Redirection

    Most Linux programs can be thought of as possessing one input and two outputs. Theinput is called Standard Input. The outputs are called Standard Output and StandardError.

    By default, both Standard Output and Standard Error are sent to the terminal screen.

    By using the output redirection operators, the output can be diverted to files, orelsewhere.

    > redirects Standard Output 1> also redirects Standard Output 2> redirects Standard Error &> redirects both Standard Output and Standard Error

  • 8/4/2019 4.Intro2UNIX Notes

    26/114

    The output of programs can be redirected to a file, or perhaps /dev/null which just throwsout anything it's given.

    If redirecting to a file, the file will be overwritten using the above operators. Use >> toappend to the file.

    Tarballs

    Large groups of files are usually distributed in what is called a compressed tarball format. A "tarball" is a file created using the tar command (Tape ARchiver) which can group

    together many files into a single archive file. The tar command does not compress its contents. Tarballs are usually identified by the filename suffix ".tar". The gzip (and gunzip) programs are used to compress (and uncompress) individual files

    or tarballs. A gzip-ed tarball usually has the suffix ".tar.gz", or ".tgz" for short. Examine the commands in the handout, or the man pages, for more details.

    Linux Startup Sequence

    To trace the full startup sequence from hardware powerup to shell, we'll assume: x86 hardware,the boot manager is LILO installed in the MBR, the kernel is on the hard disk, the systemnormally starts in runlevel 3, the example user's shell is bash, and the system is not using XWindows. Other possibilities are indicated where appropriate.

    Hardware

    1. When the computer power is turned on, a special circuit signals the RESET pin of theCPU.

    2. The CPU resets its registers and executes the code at a fixed address (0xFFFFFFF0), thestarting point of the BIOS (Basic Input/Output System).

    BIOS

    3. Executes the POST (Power-On Self-Test) and displays various banners on the monitorscreen.

    4. Initializes the hardware devices and maps IRQs for PCI devices.5. Searches for an operating system to load (usually floppy disk then CD-ROM then hard

    disk).6. Copies first sector of operating system device into memory and begins execution.

    LILO Bootloader

    7. Only part of LILO fits into the MBR (the first sector loaded by the BIOS) so its role is toload the rest of the bootloader code into memory and begin execution of that code. (If thesystem was booted from a floppy, a portion of the kernel itself is located in the firstsector--enough to load the rest of the compressed kernel image into memory.)

    8. Displays a timed prompt for the user to choose a kernel to load.

  • 8/4/2019 4.Intro2UNIX Notes

    27/114

    9. Loads the full kernel image into memory and begins execution at the setup() functionentry point.

    Kernel

    10.setup(): determines amount of RAM, initializes keyboard and mouse, initializes videoadapter, looks up hard disk partitions, resets the FPU, maps the IRQ lines, switches theCPU from Real to Protected mode, and finally jumps to Part I of the startup_32()function.

    11.startup_32() Part I: decompresses the kernel, and jumps to the now uncompressed Part IIof the startup_32() function.

    12.startup_32() Part II: initializes internal kernel tables, identifies the processor type, andthen jumps to the start_kernel() function.

    13.start_kernel(): initializes the memory management tables, finalizes the interrupt tables,spawns the kernel threads (which are responsible for the dmesg messages as theycomplete the system initialization), and finally launches init as process ID (PID) 1.

    init

    14.Reads the /etc/inittab configuration file.15.Sets the default runlevel.16.Runs the rc.sysinit script which: enables disk swapping, checks and mounts filesystems,

    and synchronizes the system time with the CMOS clock. The rc.sysinit script isresponsible for the OK messages that appear during the startup before enteringinteractive/non-interactive mode.

    17.Runs the rc script, passing the runlevel as a parameter. The rc script in turn starts or stopsall of the services in the approprate rcN.d directory in numerical order. Usually the

    rc.local script is run last. The rc script is responsible for the OK messages that appearduring the startup after entering interactive/non-interactive mode.18.Installs the Ctrl+Alt+Del interrupt handler (usually a form of shutdown ).19.Starts the virtual terminals using a variant of the getty program which in turn run login to

    accept user logins.

    login

    20.User logs in through one of the tty processes (virtual terminals).21.Authenticates the login and password against the /etc/passwd file.22.Runs the /etc/profile script to set global user settings.23.Sets the current working directory to the user's home directory (from /etc/passwd).24.Launches the user's preferred shell (from /etc/passwd).

    bash

    25.Runs the .bash_profile script to set personalized shell settings. Typically .bash_profile inturn calls .bashrc for the bulk of the settings. On subsequent opening of subshells, onlythe .bashrc script is run.

  • 8/4/2019 4.Intro2UNIX Notes

    28/114

    26.The command line prompt signals to the user that the shell is ready to accept commands.Week 3 Linux Commands

    The following table demonstrates only some of the options possible for each command. Consultthe man pages for the complete list, or a reference such asLinux in a Nutshell.

    Kernel

    Command

    s

    Description Options Examples

    dmesg

    Display the kernel system controlmessages since the last system boot, orthe most recent messages if the kernelring buffer has been filled. Only the rootuser may execute this command.

    see

    documentatio

    n

    dmesg displays thekernel messages

    insmodLoad the specified module into thekernel. Only the root user may executethis command.

    see

    documentatio

    n

    insmod/lib/modules/2.4.9-13/kernel/drivers/net/via-rhine.o loads theEthernet driver used bymost DLink NICs intothe kernel

    lsmodList all of the modules loaded into thekernel.

    nonelsmod lists the kernelmodules

    modprobe

    Load the specified module, as well asany modules on which it depends. The

    dependencies are specified in the/lib/modules/kernel_version/modules.dep file which is created using the depmodutility. Only the root user may executethis command.

    seedocumentatio

    n

    modprobe hisax loads

    the hisax module (ISDNdriver) along with all ofthe modules that hisaxrequires (e.g. isdn.o)

    rmmod

    Unload a module from the kernel,provided the module is not currently inuse, and no other modules depend on it(unless the -r option is used). Only theroot user may execute this command.

    -r recursivelyremovedependentmodules

    rmmod unloads the via-rhine module from thekernel

    uname

    Display information about the kernel,machine, and operating system. Verysimilar to the information available byexecuting cat /proc/version.

    -a display allinformation;-m displaymachine type;-n displaynetworkhostname;-p display

    uname displays the nameof the operating system(i.e. "Linux");uname -a displays theoperating system,hostname, kernelversion, kernel build,machine type, and

    http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978059600025&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1601DC2D912E49D94EE7990E1A861C9B2C8D4305http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978059600025&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1601DC2D912E49D94EE7990E1A861C9B2C8D4305http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978059600025&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1601DC2D912E49D94EE7990E1A861C9B2C8D4305http://chapters.indigo.ca/item.asp?Catalog=Books&Section=Books&Cat=&Lang=en&Item=978059600025&mscssid=GTA6EH8GP0679MTJD35F10KEVAMFFUHC&WSID=1601DC2D912E49D94EE7990E1A861C9B2C8D4305
  • 8/4/2019 4.Intro2UNIX Notes

    29/114

    processor type(if available,use -motherwise);-r display

    kernelversion;-s displayoperatingsystem;-v displaykernel build

    processor type;

    Archive

    Command

    s

    Description Options Examples

    gunzipUncompress a specified file previouscompressed using gzip and remove the.gz suffix. This is a easy-to-remembershortcut for the gzip -d command.

    seedocumentatio

    n

    gunzip stuff.txt.gzuncompressesstuff.txt.gz into stuff.txt

    gzip

    Compress the specified file usingLempel-Ziv coding and rename the fileby adding a .gz suffix.

    -r recursethroughdirectories

    gzip stuff.txtcompresses stuff.txt intostuff.txt.gz

    tar

    Copy files to, or restore files from, anarchive (usually a file). By convention,tar-created archives are suffixed with.tar. For historical reasons, options don'talways need to be prefixed by -. Whenused with the -z option, there is rarelyneed to use the gzip or gunzip utilities.gzip-ed archives are often suffixed with.tar.gz, or .tgz for short.

    -c create anew archive;-f identify thearchivefilename;-r append toan existingarchive;-t list files inan archive;-v displayfiles forcurrentoperation;-x extract filesfrom anarchive;-z gzip addedfiles, orgunzipextracted files

    tar -xf stuff.tar extractthe files from stuff.tar;tar xf stuff.tar extractthe files from stuff.tar;tar -cf morestuff.tar./tmp/*.c create a newarchive calledmorestuff.tar from thefiles in the currentdirectory (includingsubdirectories), and allof the files ending in ".c"from the /tmp directory;

    tar xzvf stuff.tgz firstgunzip the contents ofstuff.tgz and then extractthe files, displaying eachfile as it is extracted

    zcat Display the contents of a file previously see zcat stuff.txt.gz display

  • 8/4/2019 4.Intro2UNIX Notes

    30/114

    compressed using gzip. documentation

    the contents of thecompressed stuff.txt.gzfile

    ShellCommand

    s

    Description Options Examples

    alias

    Assign a synonym inside the shell for aspecified command, including any givenoptions. When run without parameters,alias displays all of the currently-definedsynonyms. This command is built intothe bash shell.

    none

    alias displays all of thecurrent aliases;alias la='ls -la' creates asynonym for the ls -lacommand called la

    echo

    Write the given string to the output and

    append a newline. This command is builtinto the bash shell, although there is alsoa program, /bin/echo, which does thesame thing.

    -e interpretescapedcharacters

    (e.g. \t = tab);-n do notappendnewline tooutput.

    echo -ne 'Hi!\n' displaysthe string "Hi!" followed

    by a newline;echo $PATH displaysthe value of the $PATHenvironment variable.

    source

    Execute the contents of the specified file.The file does not have to be executable.The . shell command is a handy shortcutfor source. This command is built intothe bash shell.

    none

    source dostuff executesthe lines in the dostufffile;. dostuff executes thelines in the dostuff file

    unaliasDelete the defined alias from the shell.This command is built into the bashshell.

    -a delete allaliases

    unalias la deletes the laalias

    which

    List the full pathname of the precise filethat will be executed when the specifiedcommand is run. Aliases are alsodisplayed.

    nonewhich ls displays the fullpath to the ls command

    SessionCommand

    s

    Description Options Examples

    haltA synonym for shutdown -h now. Onlythe root user may execute this command.

    seedocumentatio

    n

    halt immediately shutsdown the system

    rebootA synonym for shutdown -r now. Onlythe root user may execute this command.

    see

    documentatio

    n

    reboot immediatelyreboots the system

    runlevel Display the previous and current none runlevel displays the

  • 8/4/2019 4.Intro2UNIX Notes

    31/114

    runlevel. If there is no previous runlevel(since the last complete startup), "N" isdisplayed instead.

    previous and currentrunlevel

    shutdown

    Gracefully shut down the system byterminating all running processes.Shutdowns may be run immediately,scheduled for a number of minutes in thefuture, or scheduled for a specified timein the future. Following the shutdown,the system may be rebooted or haltedusing the -r or -h options, respectively.Depending on the hardware, halting thesystem may turn off the power. Amessage may be added that will bebroadcast to all users on the system to

    warn them of the impending shutdown.Only the root user may execute thiscommand.

    -c cancel anin-progressshutdown;-h halt systemaftershutdown;-k printwarningmessage, butsuppressactualshutdown;

    -r rebootsystem aftershutdown

    shutdown -h nowimmediately shuts downand then halts thesystem;shutdown -h 19:00schedules a shutdownand halt for 7:00pm;shutdown -r +10'Everybody Out!' sendsthe message "EverybodyOut!" to all logged-inusers and schedules ashutdown followed by a

    reboot in 10 minutes;shutdown -c cancels ascheduled systemshutdown

    telinit

    Signal the init process to change thesystem's runlevel. This is often just alink to the init command. Only the root

    user may execute this command.

    see

    documentatio

    n

    telinit 0 halts the system;telinit 1 changes to thesingle-user runlevel;telinit s changes to thesingle-user runlevel;telinit 6 reboots the

    system;telinit q rereads the/etc/inittab for thecurrent runlevel

    Week 3 Class Exercises

    Perform all of these exercises through the shell terminal window.

    1. Use the echo command to view environment variables:Take a look at the $PATH, $PWD, and $COLORS variables for starters. Experiment withusing double-quotes and single-quotes around strings containing environment variables.What's the difference?

    2. Use alias to create slightly-altered versions of other commands:Type alias on its own to view the currently-configured aliases. Create an alias called lathat will let you list both hidden and visible files. Create an alias for the rm command thatincorporates the -i option. Then try deleting an unimportant file using rm to see thedifference.

  • 8/4/2019 4.Intro2UNIX Notes

    32/114

    3. Add "safety" aliases to your shell's initialization script:Use vi to add aliases to the cp, mv, and rm commands incorporating the -i option, asabove, in the .bashrc file. Re-run the .bashrc script using the source command (or its .representation). Open a new shell window to see if the aliases still exist. Try them out.

    4. Use which to view paths or aliases for given commands: What are the paths or aliases for the ls, mv, or which commands?

    5. Create a tarball archive:Use tar to create an archive of the two files: /etc/termcap and /etc/pine.conf. Note the sizeof the tarball compared to the size of the original files. Use tar to view the filenames (andpaths) in the tarball.

    6. Compress a file using gzip:Compress the tarball created in the step above using gzip. Compare the gzipped file's sizeto the size of the original files. Gzip a single file and view its contents using zcat.

    7. Uncompress a gzipped tarball:Use gunzip and tar, or just tar alone to uncompress and de-archive the gzipped tarballfrom above.

    8. Experiment with dmesg, runlevel, and uname:Take them for a spin. Try the different options for uname. Compare uname with thecontents of /proc/version.

    9. View the loaded kernel modules:Run lsmod to view the loaded modules. You may have to specify a path to the commandfor it to run. Can you explain what all of the modules are?

    Week 4 - NetworksLecture Notes

    Handout:Networking Basics

    Handout:Linux Networking Particulars

    Handout:Redirection and Pipelines

    Handout:Week 4 Linux Commands

    Handout:Week 4 Class Exercises

    Week 4 Lecture Notes

    Networking

    Most of this week's notes are included in the handouts. Make sure to read them closely,especially the examples.

    The root User

    Be extremely careful when logged in as the root user. One slip could trash the machine.

    http://college.yukondude.com/2002_01_comp225/notes/week-4.phphttp://college.yukondude.com/2002_01_comp225/notes/week-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-net-basics-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-net-basics-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-net-basics-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-linux-net-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-linux-net-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-linux-net-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-redir-pipes-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-redir-pipes-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-redir-pipes-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-commands-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-commands-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-commands-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-exercises-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-exercises-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-exercises-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-exercises-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-commands-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-redir-pipes-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-linux-net-4.phphttp://college.yukondude.com/2002_01_comp225/notes/ho-net-basics-4.phphttp://college.yukondude.com/2002_01_comp225/notes/week-4.php
  • 8/4/2019 4.Intro2UNIX Notes

    33/114

    The most dangerous way to execute privileged commands is to log in as root. Despite the# prompt that serves as a reminder, you can easily forget your power and do somethingnasty.

    A better way to execute privileged commands is to use the "su -" command ("-" is thesame as "-l" and runs through the login process). But you can still forget to exit the root

    shell. The safest way to execute privileged commands is to use the "su -lc" command which

    will run just the specified command as root and then exit immediately.

    Networking Basics

    Networking ProtocolsWhile IP is now the de facto standard for networking computers, there are still a number ofcompetitors hanging about. There are also many specialized protocols that are better suited forcertain types of data or transmission media: video, voice, satellite, wireless, Quality of Service(QoS), etc.

    Protocol Description

    Appletalk Apple's networking protocol for the Macintosh.

    IP (Internet Protocol)

    Currently the most popular networking protocol, and the foundation ofthe Internet. Also commonly called TCP/IP, although UDP/IP and ICMPare also IP-based. The current version of IP is IPv4, with IPv6 expected"real soon now."

    IPX (Internet PacketeXchange)

    Novell's variant of IP.

    NetBEUI (NetBIOS

    Extended UserInterface)

    Microsoft's LAN protocol. NetBEUI can't be routed across networks, soit is often layered on top of TCP/IP.

    SLIP (Serial Line IP),PPP (Point-to-PointProtocol)

    Two protocols for carrying IP traffic over serial lines (e.g. telephone ordedicated serial lines).

    UUCP (Unix-to-UnixCopy Protocol)

    A batch-oriented protocol for moving files across serial lines.

    IP Network ModelThis model is a simplification of the 7-layer ISO-OSI network model. The program code that

    makes up these layers is often referred to as the "stack".

    Layer Description Data Structure Examples

    4 Application End user application protocols.stream (TCP),message (UDP)

    FTP, DNS, telnet,ping

    3 TransportDatagram delivery (order, guarantee,connection), as well as port

    segment (TCP),packet (UDP)

    TCP, UDP

  • 8/4/2019 4.Intro2UNIX Notes

    34/114

    addressing.

    2 Internet Datagram addressing and routing. datagram IP, ICMP

    1NetworkAccess

    Hardware-level transmission andaddressing.

    frameEthernet, Token Ring,FDDI, cable, ADSL

    Network Access LayerThe Network Access Layer is a fusion of the OSI model's Physical and Data Link layers. Itincludes such protocols as, for example, the Ethernet CSMA/CD (Carrier Sense Multiple Access/ Collision Detection) protocol, the ARP (Address Resolution Protocol) for mapping IP addressesto Ethernet MAC (Media Access Control) addresses, and even the cabling standards such as100base-T or Thin Coax.

    Similar hardware-level protocols for other Network Access technologies (e.g. FDDI, TokenRing) would also be included in this layer.

    Internet LayerIP AddressesThe Internet Layer corresponds to the OSI model's Network layer. The Internet Protocol'sprimary function is to route packets to a specific address, possibly across heterogeneousnetworks. The IP is a connectionless protocol and so does not construct virtual circuits but mustappend addressing data to each individual datagram. IP does not guarantee delivery; instead itrelies on the protocols from the Transport layer.

    Because the Internet Layer is separate from the hardware (Network Access Layer), IP too ishardware-independent. Instead of MAC addresses, or token-based traffic, IP uses unique 32-bitnumbers, called IP addresses, to identify network nodes. IP addresses are usually written using

    dotted-quad notation, aaa.bbb.ccc.ddd, where each of the quads is an integer between 0 and 255(0x0 and 0xFF). The Internet Assigned Numbers Authority (IANA) is responsible for assigningIP addresses. The addresses are allocated into ranges as follows:

    Address Range Description

    0.0.0.0Defaultroute

    This range is not used for datagram addressing.0.0.0.1 -0.255.255.255

    Reserved

    1.0.0.0 -

    9.255.255.255 Class A

    Class A networks may have ~1.6 million hosts per network.10.0.0.0 -10.255.255.255

    Class APrivate

    11.0.0.0 -126.255.255.255

    Class A

    127.0.0.0 - Loopback The loopback address (127.0.0.1) always refers to the local

  • 8/4/2019 4.Intro2UNIX Notes

    35/114

    127.255.255.255 host, allowing it to be treated in the same manner as aremote host.

    128.0.0.0 -172.15.255.255

    Class B

    Class B networks may have 65,024 hosts per network.172.16.0.0 -172.31.255.255 Class BPrivate

    172.32.0.0 -191.255.255.255

    Class B

    192.0.0.0 -192.167.255.255

    Class C

    Class C networks may have 254 hosts per network.192.168.0.0 -192.168.255.255

    Class CPrivate

    193.0.0.0 -223.255.255.255

    Class C

    224.0.0.0 -239.255.255.255

    MulticastEach of the addresses in this range refers to a specificmulticast group.

    240.0.0.0 -255.255.255.255

    Reserved This range is not used for datagram addressing.

    IP NetworksIP networks do not have to strictly follow the class A through C designations. Instead, a networkmay be identified by an IP address, and either an address bit mask or a prefix length. Forinstance, the class C network 192.168.54.0 would have a bit mask of 255.255.255.0 or a prefixlength of 24, written as 192.168.54.0/24.

    This means that every IP address can be split into a network part and a host part. The bit mask orthe prefix length refer to the network part of the address; everything else is the host part. Everybit that is a 1 in the mask matches the network part of the address. The prefix length is thenumber of bits that refer to the network part, starting from the left.

    In the above example, the address 192.168.54.0 can be written in binary as:11000000.10101000.00110110.00000000 (address = 192.168.54.0)11111111.11111111.11111111.00000000 (mask = 255.255.255.0, prefix = 24 bits)

    If all of the host bits are 0, then the address refers to a network (as in the above example). If allof the host bits are 1, then the address refers to the broadcast address for the network

    (192.168.54.255/24 would be the broadcast address). All the other addresses in the network (254in this case) are usable for hosts.

    Internet Control Message Protocol (ICMP)A companion protocol to IP, ICMP also uses the same addressing mechanisms as IP. ICMP isused for flow control, error reporting, and information. The most common ICMP message, ICMPEcho, is used by the ping utility.

  • 8/4/2019 4.Intro2UNIX Notes

    36/114

    Transport LayerThe Transport Layer is a fusion of the OSI model's Session and Transport layers. The protocolsin this layer (chiefly TCP and UDP) are concerned with the delivery of data.

    Both TCP and UDP use ports to identify processes on the communicating hosts. A port is a 16-

    bit value. Both the source and destination ports are encoded in the TCP segment or UDP packet.A typical service will use a well-known port (see Application Layer table below) to recieverequests, and the return the response over a dynamically-established client port. The combinationof an IP address and a port number, usually written aaa.bbb.ccc.ddd:ppppp, is called a socket.

    Transmission Control Protocol (TCP)TCP is a reliable, connection-oriented, byte-stream protocol. TCP first sets up a connectionbetween two hosts, and then for each segment that is transmitted, ensures that the receiving hostreturns an aknowledgement that the segments were received successfully. Each segment alsoincludes a checksum so that corrupted segments can be identified, discarded, and re-requestedfrom the sending host. Each segment is also numbered so that the receiving host may reassemblethem in the correct order.

    User Datagram Protocol (UDP)UDP is an unreliable, connectionless protocol. Because it does not guarantee the order of packetdelivery (or even successful delivery at all) and does not set up connections, UDP is much moreefficient than TCP. Services that only transmit small amounts of data and that use a query-response (the response is an acknowledgement of success) model benefit greatly from UDP'sspeed.

    Application LayerThe Application Layer is a fusion of the OSI model's Application and Presentation layers. Thereare a great many protocols in this layer, the most common of which are listed below. A more

    complete list may be found in the /etc/services file.

    Like IP addresses, port numbers are assigned by IANI. The first 1024 ports are assigned to well-known services. Only processes running as the root user may connect sockets to these ports. Therest of the ports are usually dynamically-allocated by client processes.

    Service DescriptionTransport

    Protocol

    Assigned

    Port

    DNSDomain NameSystem

    Maps human-friendly URLs tonumeric IP addresses.

    UDP 53

    FTPFile TransferProtocol

    Transfers files between computers. TCP21 (command)20 (active-mode data)

    HTTPHyperTextTransfer Protocol

    The protocol behind the World WideWeb.

    TCP 80

    HTTPSHTTP overSecure Sockets

    Certificate-based encryption and partyauthentication for HTTP traffic.

    TCP 443

  • 8/4/2019 4.Intro2UNIX Notes

    37/114

    Layer

    POP3Post OfficeProtocol -Version 3

    Retrieves email messages. TCP 110

    Telnet NetworkTerminalProtocol

    Remote shell access. TCP 23

    SMBServer MessageBlock (Samba)

    Shares files and printers with MSWindows systems.

    TCPUDP

    139137, 138

    SMTPSimple MailTransfer Protocol

    Sends email messages. TCP 25

    SSH Secure SHellSecure remote shell access. Also usedto securely tunnel other protocols (e.g.FTP, X Windows)

    TCP 22

    Linux Networking Particulars

    InterfacesIn Linux, network devices use hardware drivers calledNetwork Interfaces. These interfaces maybe compiled into the kernel itself, or more commonly these days, loaded dynamically asmodules. Interfaces are usually numbered, starting at zero, so a machine with two Ethernet cardswould have interfaces eth0 and eth1. The following table lists a few of the more commoninterface types:

    Interface DescriptionethN Ethernet card interfaces.

    loThe local loopback interface (127.0.0.1). Traffic sent through this interface willimmediately reappear in the same host's IP stack. There is always one lo in the kernel,and no real need for more than one.

    slNSLIP (Serial Line IP) interfaces. Such an interface would typically map to one of themachine's serial ports, or directly to a modem.

    pppNPPP (Point-to-Point Protocol) interfaces. Such an interface would typically map toone of the machine's serial ports, or directly to a modem.

    trN Token Ring card interfaces.

    The ifconfig command is used to configure the interfaces (assign IP addresses, set the IRQ,enable/disable, etc.). When run without options, ifconfig displays the current status of all of theinterfaces on the machine.

    Sockets

  • 8/4/2019 4.Intro2UNIX Notes

    38/114

    A combination of an IP address and a port is called a socket. Processes attach themselves tosockets so that they can send or receive IP traffic. Server processes typically attach to well-known sockets so that client processes, most likely on remote hosts, can find and connect to theservice.