1 Linux for Beginners Bahador Bakhshi [email protected].

76
1 Linux for Beginners Bahador Bakhshi [email protected]

Transcript of 1 Linux for Beginners Bahador Bakhshi [email protected].

Page 1: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

1

Linux

for Beginners

Bahador [email protected]

Page 2: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

2

Agenda Introduction Linux Distributions Quick Start Files Process Shell & Programming Networking Security

Page 3: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

3

Introduction This course is for beginners What you learn

General introduction to GNU/Linux OS Basic operations in Text Mode A little about administration

What you don't learn learn In depth Linux structure Kernel Internal System programming …

Page 4: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

4

Introduction Security perspectives OS role

Govern any things in computer Has privilege Insecure OS insecure machine

Page 5: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

5

Introduction Why Linux? Free

Open Source, Freedom, GPL (GNU Public License) No charge

More secure Open source

OS for computer scientists You see what happen, no hidden things

You need it

Page 6: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

6

History 1960

MULTICS, Multiplex Computing System

1974 in AT&T Free Unix Free BSD

POSIX Unix is commercial

MINIX Simple and free UNIX liked OS, by Tanenbaum

Page 7: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

7

History 1991

Linus Torvalds Free kernel for i386

5 Oct 1991 in newsgroup of minix Linux 0.0.2

1994 Linux 1.0

Linux Logo TUX

Page 8: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

8

Now linux-2.6.26, www.kernel.org Multitasking, SMP, multi-user Wide range of CPUs Popular OS in universities and academic

research Open source

Platform of network services Wide range of network protocols and services

Platform for embedded development

Page 9: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

9

Linux Distributions What required

OS, kernel, kernel-space Applications, user-space

Interfaces and basic commands Applications Services

Kernel by Linus and world wide developers Most of applications by GNU project

GNU : GNU is Not Unix Our operating system: GNU/Linux

Page 10: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

10

Linux Distributions Red Hat & Fedora

Stable and commercial support

SuSE Most updated and user friendly Supported by Novel

UBUNTU New fast growing user friendly Debian based

Debian Most complete distribution, the Sarge

Page 11: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

11

Linux Distributions Bluecat

Linux for embedded systems

LinuxPPC Linux to run on PowerPC machines

Astaro Security applaince, Firewall, Antivirus

Live CD KNOPPIX, PHLAK, Karamad, ...

Page 12: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

12

Quick Start Access to Linux

Remote Local

Remote Through network Telnet, insecure and disabled now SSH, Secure Shell Protocol

Local Linux installation or Live CDs

Page 13: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

13

Quick Start System powered on BIOS POST Bootloader, Grub or LiLo

Initialize Hardware Kernel extraction

Kernel Initialize Hardware Kernel data structures initialization

Page 14: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

14

Quick Start System Services Configured Services

network and network services

User interface Graphical

Called as X window system, it is a service KDE, GNOME

Text, Shells

Switch between them: ALT-F? and CTRL-ALT-F? Screen 7 is the X

Page 15: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

15

Text vs. X Contrary to MS Windows

X isn't integrated into OS X is just an application

Shell Interactive program, interface between user and kernel Real power of Linux, specially for hackers and

administrators Bash (Brown Again Shell), tsh, csh

We study Linux Text Mode and just applications not Kernel

Page 16: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

16

Bash After successful login, you get shell prompt

$ : user # : root

There are two privilege in user space reset: reinitialize your screen Linux is case sensitive Autocomplete: commands and file names

Commands in $PATH File name in specified path

Page 17: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

17

Bash Linux is multiprocess, even in command line

Process can be run in background

Widecards * : string ?: a char []: regular expression

IO redirection to file > : overwrite, 2> : overwrite by stderror >> : append < : Read input

Page 18: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

18

Bash IO redirection to a process

| : pass output of command as input to other command One of the main features in UNIX-like systems Simple commands which are piped together

Some short-cuts CTRL-D: Logout, End of file CTRL-L: Clear Screen CTRL-C: Stop CTRL-Z: Suspend CTRL-E: cut to end of line CTRL-U: cut to start of line

Page 19: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

19

Bash History for each user Use arrow keys !# : # command !<start_of_command>: last command started

with ... CTRL-R: search history history: see command history .bash_history: saved $HISTSIZE number

of command

Page 20: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

20

Linux/shell vs. Windows/cmd Path separator: / not \ File extensions have NOT any meaning Hidden file started

by ., .bashrc, .bash_history End of file is CTRL-D New line is \n not \n\r Options are passed by - or -- All system configurations are saved in text files

Page 21: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

21

Help & Doc Command's builtin helps: -h or --help Man pages Info pages Documents in /usr/share/doc Info pages

are NOT complete Easy to use info <command name> or pinfo <command

name>

whatis

Page 22: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

22

Help & Doc Man pages

Most complete documentation in Linux Very technical

Title, description, see also and files

man <man-category> file/command/function searched in <MANPATH> manpath : find out the <MANPATH> configuration file: /etc/manpath.conf man -k topic : search topic in titles = apropos man -K topic: search topic in body

Page 23: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

23

Mans/usr/share/manman1: User commandsman2: System libraries man3: Programming libraries man4: Special filesman5: File formats man7: Misc. network protocols, ...man8: System administrationOther mans: Application man pages

Page 24: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

24

Internet documents The Linux Documentation Project:

www.tldp.orgTutorials HOWTOs Software home pagesMailing listsEverything is googlized

Page 25: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

25

Files Everything in Linux is file, if it is NOT process Files can be

Regular file Directory Links device named pipe, ...

Uniform interface, open, read/write, close You should fully understand the Linux file structure

Page 26: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

26

Files Files system

User Space, a tree structure

Kernel Space, VFS and file system depended drivers

What file systems in Linux Kernel configuration depended

ext2, ext3, jfs, nfs, fat, ntfs, ...

man fs

mkfs.ext2, mkfs.ext3, mkfs.fat, ... make file systems

Page 27: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

27

File system structure /boot

Bootloader, bootloader config and kernel images

/bin Binary files, basic utilities, required for boot

/sbin System binary, system management tools

/lib Shared libraries and kernel modules

Page 28: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

28

File system structure /etc

System configuration, passwords, service config

/home Home directory of users

/root Home directory of root

/var log files, message files, lock files, www root, ...

Page 29: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

29

File system structure /tmp

temporary files, socket files, pipe files

/usr Like the /, /usr/include additional directories bin, sbin,

/opt Additional softwares

/proc Virtual file system, process and system information

Kernel Interface in user space

Page 30: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

30

File system structure /dev

Device files, block devices, character devices

/sys Kernel interface for hardwares info and management

/mnt and /media Add new media and file system into your file system

New media has its own file system (kernel level

driver)

mount -t vfat /dev/sdb1 /mnt/flash

Page 31: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

31

File System Navigation List directories

ls

Options: -a -l -h -R

Walking in file system Absolute vs. Relative path

cd <path>

cd , cd –, cd ~

pushd

popd

Page 32: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

32

File System Navigation & Modification Find where are you, absolute name

pwd

Make directory

mkdir <directory path>

Remove empty directory

rmdir <directory path>

Page 33: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

33

File System Navigation & Modification Remove file

rm <file name>

rm -r <directory>

-i: ask you, -f: force

Secure remove shred

-n: number, -z: fill zero shred -n 10 -z -v /tmp/xxx

Page 34: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

34

File System Navigation & Modification move file and directories

mv <source> <destination>

copy file and directories

cp <source file> <destination file>

cp -r <source directory> <destination directory>

What is the rename?

Page 35: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

35

File System Navigation & Modification

Links (like windows short-cuts) Two Types

Hard (only for files) and soft (files & directories)

Hard ln <target file> <link name>

Soft ln -s <target name> <link name>

View Links readlink <link name>, ls -l

Page 36: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

36

File Commands Commands

File as an object

File content

Alert timestamp of file Creation, access and modification

touch <file name> : update mod. time to now

-a : access time, -m : modification time

-t : set time

create new file

Page 37: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

37

File Commands Find files and directories

find <path> <regular expression>

name, size, time, type, permission, ... find /etc/ -name *.conf -exec cp '{}' /home/backup ';'

Where are commands and man pages whereis <command name>

Which command is executed which <command name>

Page 38: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

38

File Commands Archive

Create: tar -cf <archive name> <directory>

Extract: tar -xf <archive name>

File Compression gzip <file name>, gunzip <zipped file name>

best compress: -9

bzip2 <file name>, bunzip2 <zipped file>

z* commands zcat, zdiff, zless

Page 39: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

39

File Security File permissions

ls -l

-rwxrwxrwx: -(user)(group)(other)

In binary format -421421421

r: read, w: write, x: execute

Page 40: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

40

File Security More file permissions

t: sticky bit. Others can not delete your file even with “w”

permission

s & g: Set User/Group ID. Change process id to file owner

Chang permissions

chmod [ugo][+-=][rwx/binary] file

Attributes: undelete, fill zero, append only, ...

lsattr, chattr

Page 41: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

41

File Security Default permission

umask: Invert of your permission

umask 077: no one else can do anything

Chang owner and group (only root)

chgrp <group> file

chown <user> file

Page 42: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

42

File Commands Most important file type in Linux: Text

Config files

Log files

Source codes

File type file <file name>

Binary files xpdf, gimp, openoffice2, firefox, konqueror, xdiv,

kde, ...

Page 43: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

43

File Commands What is in a file

cat <file name>

tac <file name>

View large files more <file name> or pipe: ls -l | more

less <file name> or pipe: cat test.txt |

less

View not all of file tail -# <file name>, -f is continues

head -# <file name>

Page 44: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

44

File Commands Search content of file

grep <regular expression> <file name>

-i: ignore case, -v: invert result, -r: recursive

Count file words wc <file name>

-l: Lines, -w: words, -c: characters

Difference between files diff <file 1> <file 2>

-Nu : create patch

Page 45: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

45

Editors Again Text editor Text Editors

X editors

text mode editors

Again text mode editors

X editors gedit, kwrite

kate

Page 46: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

46

Editors emacs

Old and very user friendly

Menu based, F10

mcedit A part of the midnight commander

Menu based, easy to use

vi & vim (vi improved) Difficult

Editor for programmers

Page 47: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

47

vim Three modes

Input mode: edit your document

Command mode: simple commands

Line input mode: special and advance commands

Input mode Go from command mode by i or a

Type what you want

Arrow keys, del, home, ... are workings

Page 48: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

48

vim Command mode, the default mode

Go from input mode by: Esc

Navigation commands home : start of line

end : end of line

b : previous word

w : next work

:# : go to line #

Page 49: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

49

vim Edit commands

x : cut a char

#dw : cut # of words

#dd : cut # of lines

d$ : cut to end of line, d^ : cut to start of line

#yw : copy # of words

#yy: copy # of lines

p : past

u : undo

Page 50: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

50

vim Line Input mode

Go from command mode by :

:w : save file, :w! : force to save

:e <filename> : open file

:q : quit, :q! : force to quit

:! <any command>: run shell command

/<str> : search str

:#1,#2 s/<str1>/<str2> /[c,g]: replace

Page 51: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

51

Process In Linux, every things is file, if it is NOT a process

Linux is multi-user, multi-process, time-sharing

OS

Each process has a unique id and a parent (tree)

Process can be run in Foreground

Background

Foreground is default

Page 52: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

52

Process Background

& at end of command

suspend (CTRL-Z), send resume signal (bg <job

id>)

Process id is NOT job id

fg <job id> : Job come from background to

foreground

jobs : list of jobs

Page 53: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

53

Process Scheduling, running in background

at time -f <file name>, atq, atrm <id>

cron, crontab, man crontab

nohup <command> & : Leave job running

Priority less nice is better

nice -# <command> : priority is decreased by #

renice # <process id> : Change process priority

Page 54: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

54

Process Monitoring

ps : list of your process

ps aux : all running process, with command Lines

top : top processes

ksysgaurd : Graphical monitor, more than process

monitor

pstree : tree of running processes

/proc/process-id : kernel information about

processes

Page 55: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

55

Process Process and Signal

Signals are notifiers

Kernel and users can send signals

skill -<signal number> <process id>: user send signal

skill -L : List of signals

man 7 signal : Full description of signals

kill <procees-id>, kill -9 <process-id> : Kill the

process

killall -9 <process name> : kill the process

Page 56: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

56

System Information uname -a, -r : Kernel name

dmesg : Kernel messages

/var/log/ : system logs (syslogd) and application's

messages

date : date of system

uptime : How long time your system is alive?

iostat <device name>: usage of cpu and device

users, who, w : list of on-line users

finger <user-name>/@<computer name>: remote users

info

Page 57: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

57

Shell Executable applications

Binary programs: usually in developed in C

Scripts: shell, perl, awk, python, ...

Shell (Bash)

After login, your shell executes (/etc/passwd)

.bash_profile

/etc/bashrc

.bashrc

Page 58: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

58

Bash Environment Variables

Save, parameters and some configurations

Some important variables $PATH: Path of commands

$HOME: Your home directory

$SHELL: Your shell address

$PS1: Bash prompt

$USER: Your login name

Page 59: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

59

Bash Define a variable

ABC=“This is test”

View variable content echo $ABC

Define variable for subsequent shells export AAA=“This is test”

View all variables

printenv

Page 60: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

60

Bash Script Bash programming language

Variables User defined variables

Built in variables: $0, $?, ...

All commands

Page 61: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

Bash Script

Control statements for <name> in <word>; do <commands>; done

case <variable> in x) <command>;; y) <commands>;;

esac

if <condition>; then <commands>; elif <condition>

then <command>; fi

while <condition>; do <commands>; done

Page 62: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

62

Example vim test.sh

#!/bin/bash

for file in *; do

if grep -q $1 $file; then

echo “Found in file: “ $file

fi;

done;

exit 0

chmod +x test.sh

./test.sh test

Page 63: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

63

C programming GCC: GNU Compiler Collection

C, C++, Java, ADA, Fortran, ...

Tools to build C or C++ program cpp: C preprocessor

cc: C Compiler

c++: C++ Compiler

as: The assembler

ld: Linker

gdb: GNU debugger

Page 64: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

64

C programming All steps altogether

gcc -o <output name> <input C>

Intermediate results

-c : Just compile, with out linking

-S: Produce assembly code

-E: Just process the file

-g: Add debugging options

Page 65: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

65

C programming (make & Makefile)

Open source world

Automate compiling large packages: make &

Makefile

target : req1 req2 ....

Rule1 to make target

Rule2 to make target

req1 : req11 req12

Rule11 to make req1

....

Page 66: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

Installing programs

Binary format Distribution depended

Red Hat & Fedora: rpm & yum

Debian based (Ubuntu): apt-get

Source code

./configure

make

make install

Page 67: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

67

Networking Most of network protocols are implemented in Linux

Application layer: bind, apache, sendmail, ssh, ftp, NFS

Transport layer: TCP, UDP

Network Layer: IP, IPX, ATM

Physical layer: PPP, Ethernet, 802.11

Configurations

On line

Config files

Page 68: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

68

Networking Network interface (Ethernet)

ifconfig eth0 10.10.10.1 netmask 255.255.255.0 up

Red Hat: /etc/sysconfig/network & networking

Ubuntu: /etc/network/interfaces

Routing route add -net 192.168.0.0/24 gw 10.10.10.2 dev

eth0

/etc/sysconfig/networking & network

Page 69: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

69

Networking DNS

/etc/resolve.conf

/etc/hosts

Host Name

hostname

/etc/hostname

IP suit: ip route, ip address, ip route ....

Page 70: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

70

Networking Monitoring and Troubleshooting

List of all connections: netstat -an

Find route and its MTU: tracepath, traceroute

Check liveness: ping

Your machine ip traffic: iptraf

Sniffer: tcpdum, ethereal

Scanners: nmap, amap, ...

Page 71: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

71

Using from Network Remote shell: telnet, ssh

Remote file transfer: ftp, lftp, sftp, scp

Web: lynx, firefox

Download: wget, crul, prozilla

Mail: fetchmail, mail

DNS resolver: nslookup, dig

Windows NetBIOS: smb* commands

Page 72: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

72

Services Linux services

Network services: web, mail, ...

Local services: udev, alsa

Startup scripts

/etc/init.d/<sevice name>, /etc/init.d/apache2

Configuration files

/etc, /etc/apache2

Page 73: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

73

Security Security

Local: File security, Process security

Network

SELinux: role-based access control and

sandboxing

Each user

Has its own unique ID, /etc/passwd

Belong to some groups, /etc/group

Page 74: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

74

Security Process security

Each process has its own user id

You can kill only your processes, root can do

anythings

Page 75: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

75

Network Security Statefull Firewall: iptables

IDS: snort

VPNs

IPSec: openswan

SSL: stunnel, openswan

Secure services

SSH, SSL, Tunneling

Page 76: 1 Linux for Beginners Bahador Bakhshi bakhshi@gmail.com.

Now is the end time of my presentation

But it is the start of your Linux learning