Download - Linux Demystified

Transcript
Page 1: Linux Demystified

Linux DemystifiedLinux Demystified

HARVARDHARVARDCOMPUTERCOMPUTER

SOCIETYSOCIETYGreg Brockman ([email protected])

Andy Brody ([email protected])

Page 2: Linux Demystified

HARVARDCOMPUTERSOCIETY

What is Linux?What is Linux?

Page 3: Linux Demystified

HARVARDCOMPUTERSOCIETY

NO!NO!

Richard StallmanRichard Stallman

Page 4: Linux Demystified

HARVARDCOMPUTERSOCIETY

What is GNU/Linux?What is GNU/Linux?

Page 5: Linux Demystified

HARVARDCOMPUTERSOCIETY

GNU’s Not UnixGNU’s Not UnixIt’s recursive… get itIt’s recursive… get it

The Free Software The Free Software FoundationFoundation

Page 6: Linux Demystified

HARVARDCOMPUTERSOCIETY

Linux is: Linux is: $0.00$0.00

Page 7: Linux Demystified

HARVARDCOMPUTERSOCIETY

Linux is:Linux is:Free SoftwareFree Software

Free as in free speech and Free as in free speech and free beer free beer

Page 8: Linux Demystified

HARVARDCOMPUTERSOCIETY

Available in many Available in many distributions on many distributions on many

platformsplatforms

Page 9: Linux Demystified

HARVARDCOMPUTERSOCIETY

Ultimate flexibility and Ultimate flexibility and controlcontrol

Page 10: Linux Demystified

HARVARDCOMPUTERSOCIETY

Linux General PrinciplesLinux General Principles

Page 11: Linux Demystified

HARVARDCOMPUTERSOCIETY

General PrinciplesGeneral Principles Less is more (no news is good Less is more (no news is good

news)news) Small programs that do one Small programs that do one

thing really wellthing really well (Almost) everything is open-(Almost) everything is open-

sourcesource Input and output to programs is Input and output to programs is

plain-text: easy to see what plain-text: easy to see what programs doprograms do

Page 12: Linux Demystified

HARVARDCOMPUTERSOCIETY

Multi-user computing Multi-user computing environment with permissionsenvironment with permissions

Everything is a file. Everything is a file. Everything.Everything.

Ctrl-Z, Ctrl-D, Ctrl-C – typical Ctrl-Z, Ctrl-D, Ctrl-C – typical ways to get out of somethingways to get out of something

If you don’t know, RTFM: If you don’t know, RTFM: manman

Principles cont.Principles cont.

Page 13: Linux Demystified

HARVARDCOMPUTERSOCIETY

Where is everything?Where is everything?brockman@cato:/$ ls

bin/ etc/ lib/ misc/ oracle@ scratch@ tmp/boot/ home@ local/ mnt/ proc/ shells/ usr/cdrom@ initrd/ lost+found/ nfs/ root/ srv/ var/

dev/ initrd.img@ media/ opt/ sbin/ sys/ vmlinuz@

Page 14: Linux Demystified

HARVARDCOMPUTERSOCIETY

Page 15: Linux Demystified

HARVARDCOMPUTERSOCIETY

Your Home DirectoryYour Home Directory~/~/

Page 16: Linux Demystified

HARVARDCOMPUTERSOCIETY

The Linux filetree is flexible.The Linux filetree is flexible./ ./ ..// ./ ../symlinkssymlinks

Page 17: Linux Demystified

HARVARDCOMPUTERSOCIETY

User configurationsUser configurationsHidden files : Hidden files : .hiddenstuff.hiddenstuff

Page 18: Linux Demystified

HARVARDCOMPUTERSOCIETY

Useful programsUseful programs

Page 19: Linux Demystified

HARVARDCOMPUTERSOCIETY

Useful Linux ProgramsUseful Linux Programs

finger, writefinger, write find, which, find, which, whereiswhereis

grepgrep ps, kill, ps, kill, killall, topkillall, top

jobs, fg, screenjobs, fg, screen quota, du, dfquota, du, df

lnln dig –tracedig –trace ping, wget, curlping, wget, curl emacs, vimemacs, vim echo, catecho, cat head, tail, head, tail, less, moreless, more

chown, chmodchown, chmod

Page 20: Linux Demystified

HARVARDCOMPUTERSOCIETY

Package ManagerPackage Manager

Can install software from centralized Can install software from centralized repositoriesrepositories

apt-get install <package>apt-get install <package> Want a webserver?Want a webserver?

– apt-get install apache2apt-get install apache2 Want Open Office?Want Open Office?

– apt-get install openoffice.orgapt-get install openoffice.org

Page 21: Linux Demystified

HARVARDCOMPUTERSOCIETY

So how do I do anything So how do I do anything useful?useful?

PIPE> >> |

stdinstdinstdinstdinstdinstdin stdout/stderrstdout/stderr

Page 22: Linux Demystified

HARVARDCOMPUTERSOCIETY

Commands & PipesCommands & Pipes

last | lesslast | less find ./ -name “Thumbs.db” -deletefind ./ -name “Thumbs.db” -delete fortune | cowsayfortune | cowsay

________________________________________________________________

< It's all in the mind, ya know. >< It's all in the mind, ya know. >

----------------------------------------------------------------

\ ^__^\ ^__^

\ (oo)\_______\ (oo)\_______

(__)\ )\/\(__)\ )\/\

||----w |||----w |

|| |||| ||

Page 23: Linux Demystified

HARVARDCOMPUTERSOCIETY

Getting to know your shellGetting to know your shell

tcsh bash zsh

Page 24: Linux Demystified

HARVARDCOMPUTERSOCIETY

Environment variablesEnvironment variables

Control the characteristics of the Control the characteristics of the shellshell– View them withView them with [set]env [set]env, or , or $VARIABLE$VARIABLE– Set them with Set them with exportexport– Change up your prompt! Change up your prompt! export export PS1=“myCOOLprompt: ”PS1=“myCOOLprompt: ”

But these have to be declared every But these have to be declared every time you use your shell.time you use your shell.– Solution: Solution: ~/.profile, ~/.bash_profile~/.profile, ~/.bash_profile, ,

etc.etc.

Page 25: Linux Demystified

HARVARDCOMPUTERSOCIETY

But if we have variables…But if we have variables…

And we have all these nifty little And we have all these nifty little programs that can be strung programs that can be strung together…together…

Can we make our own programs?Can we make our own programs? YES.YES. Linux is beautiful.Linux is beautiful.

Page 26: Linux Demystified

HARVARDCOMPUTERSOCIETY

Shell ScriptsShell Scripts

Shell scripts are “programs” that are Shell scripts are “programs” that are completely uncompiled, but read and completely uncompiled, but read and executed by the shell line by line.executed by the shell line by line.

Typically end in .shTypically end in .sh Must be chmod’ed executable.Must be chmod’ed executable. Start with a “shebang” – tells the Start with a “shebang” – tells the

shell what to use to interpret it. e.g.,shell what to use to interpret it. e.g.,– #! /bin/bash#! /bin/bash for a bash script. for a bash script.

Page 27: Linux Demystified

HARVARDCOMPUTERSOCIETY

Quick overview of BASH Quick overview of BASH scriptingscripting

Easy hello world program:Easy hello world program:

#! /bin/bash#! /bin/bashecho echo “Hello World”“Hello World”

Page 28: Linux Demystified

HARVARDCOMPUTERSOCIETY

BASH vs. CBASH vs. C

#! /bin/bash#! /bin/bash

number=3number=3name=name=“bob”“bob”echo echo “$name is your “$name is your

chosen name, $number chosen name, $number your chosen number.”your chosen number.”

let let “inc=number+1”“inc=number+1”ifif [[ “$inc”“$inc” –eq –eq “4”“4” ]]

then echo then echo “Addition “Addition works like a charm.”works like a charm.”

fifi

#include <stdio.h>#include <stdio.h>#include <cs50.h> #include <cs50.h>

int number = 3;int number = 3;string name = string name = “bob”“bob”;;printf(printf(“%s is your chosen “%s is your chosen

name, %d your chosen name, %d your chosen number.\n”number.\n”, number, , number, name);name);

int inc = number++;int inc = number++;if (if ( inc == 4 inc == 4 ) {) {

printf(printf(“Addition works “Addition works like a charm.\n”like a charm.\n”););

}}

Page 29: Linux Demystified

HARVARDCOMPUTERSOCIETY

BASH vs. CBASH vs. C All variables are All variables are

stringsstrings Variables are Variables are

accessed with accessed with $VAR$VAR

Runs other Linux Runs other Linux programs to do its programs to do its workwork

Spacing usually Spacing usually matters.matters.

No line endingsNo line endings

Multiple types, Multiple types, must be declaredmust be declared

Variables do not Variables do not have prefixeshave prefixes

Runs subroutines Runs subroutines or functions from or functions from libraries to do worklibraries to do work

Spacing matters a Spacing matters a lot less.lot less.

Lines end in ;Lines end in ;

Page 30: Linux Demystified

HARVARDCOMPUTERSOCIETY

As you can see there are many As you can see there are many similarities…similarities…

BASH is a programming language in and of itself.

You put all the little pieces of Linux together in the ways that suit you best. It’s your computer to

control.

Page 31: Linux Demystified

HARVARDCOMPUTERSOCIETY

smartsyncsmartsync#!/bin/bash#!/bin/bash

eventevent=='-e close_write''-e close_write'

inotifywaitinotifywait -mrq --format-mrq --format '%w %f''%w %f' $event$event $exclude $1$exclude $1 | | \\

while read path file; dowhile read path file; do

echoecho ""$($(datedate '+%F %R' '+%F %R')) sent sent ${path}${file} $2${path}${file} $2""

rsyncrsync -CR -CR ${path}${file} $2${path}${file} $2 || echo || echo "ERROR""ERROR"

donedone

Bash makes for convenient glue codeBash makes for convenient glue code

Page 32: Linux Demystified

HARVARDCOMPUTERSOCIETY

Focus on command line,Focus on command line,a GUI is often secondarya GUI is often secondary

Page 33: Linux Demystified

HARVARDCOMPUTERSOCIETY

Trinity uses Linux.Trinity uses Linux.

Page 34: Linux Demystified

HARVARDCOMPUTERSOCIETY

Page 35: Linux Demystified

HARVARDCOMPUTERSOCIETY

Try doing that by point-and-Try doing that by point-and-click... oh wait.click... oh wait.

Page 36: Linux Demystified

HARVARDCOMPUTERSOCIETY

got graphix?got graphix?

So far we’ve been staring a lot at So far we’ve been staring a lot at text consoles.text consoles.

Linux does allow for the display of Linux does allow for the display of graphics:graphics:– X11 on nice – demo it!X11 on nice – demo it!– Window managers: Gnome, KDEWindow managers: Gnome, KDE– You can see these on the SC Lab You can see these on the SC Lab

computers, or in most desktop Linux computers, or in most desktop Linux distros, e.g., Ubuntu, Suse, Red Hat, etc.distros, e.g., Ubuntu, Suse, Red Hat, etc.

Page 37: Linux Demystified

HARVARDCOMPUTERSOCIETY

Cool programs with graphicsCool programs with graphics

Firefox (duh)Firefox (duh) WiresharkWireshark VNCVNC XaoSXaoS

Page 38: Linux Demystified

HARVARDCOMPUTERSOCIETY

Anatomy of a DistributionAnatomy of a Distribution Building an OS with Linux is complex!Building an OS with Linux is complex!

Page 39: Linux Demystified

HARVARDCOMPUTERSOCIETY

Try it out!Try it out!

You can try Linux in a VM, with a You can try Linux in a VM, with a LiveCD, or by installing alongside your LiveCD, or by installing alongside your current OS (dual booting).current OS (dual booting).

The internets are your friend: there The internets are your friend: there are lots of forums and email listsare lots of forums and email lists

See Wikipedia for history, etc.See Wikipedia for history, etc. Read the man pages for details on Read the man pages for details on

any particular command (`man ls`)any particular command (`man ls`)

Page 40: Linux Demystified

HARVARDCOMPUTERSOCIETY

Till next time...Till next time...

Come to HCS weekly meetingsCome to HCS weekly meetings

www.hcs.harvard.edu/events www.hcs.harvard.edu/events for schedulefor schedule