unix 3 en

65
UNIX OS Lecture III Simonas Kareiva Vilnius University Faculty of Mathematics and Informatics Preparation of the material was supported by the project „Increasing Internationality in Study Programs of the Department of Computer Science II“, project number VP1–2.2–ŠMM- 07-K-02-070, funded by The European Social Fund Agency and the Government of Lithuania.

Upload: simonas-kareiva

Post on 14-Aug-2015

72 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Unix 3 en

UNIX OS Lecture III

Simonas Kareiva

Vilnius University

Faculty of Mathematics and Informatics

Preparation of the material was supported by the project „Increasing Internationality in Study Programs of the Department of Computer Science II“, project number VP1–2.2–ŠMM-07-K-02-070, funded by The European Social Fund Agency and the Government of Lithuania.

Page 2: Unix 3 en

Lecture #3 outline

Working with OS. Runlevels. Terminals

Access rights, file administration

System and network settings, date/time, etc.

File systems, mounting, management

Process review and management, listing, killing…

System logs, work automation

2

Page 3: Unix 3 en

Runlevels

System booting stages

Services are assigned for particular runlevels

ID Name Description

1 Single-User Mode Mode for administrative tasks

2 Multi-User Mode No network and service configuration

3 Multi-User Mode with Networking

Normal system startup

4 Not used/User-definable

Special purpose mode

5 X11 Like 3 + graphic environment

3

Page 4: Unix 3 en

Runlevels

Question:

What is 6th runlevel for?

4

Page 5: Unix 3 en

5

Page 6: Unix 3 en

Terminals

One real (physical) terminal

Several virtual terminals (Alt+F2, Alt+F3, ...)

Many network terminals.

6

Page 7: Unix 3 en

tty settings (linux)

/etc/event.d/tty1: start on runlevel-2

start on runlevel-3start on runlevel-4start on runlevel-5stop on shutdownrespawn /sbin/getty 38400 tty1

/etc/event.d/tty2: start on runlevel-2

start on runlevel-3stop on runlevel-4stop on runlevel-5stop on shutdownrespawn /sbin/getty 38400 tty2

7

Page 8: Unix 3 en

File permissions

CHMOD 777 FILE.DOC

?

8

Page 9: Unix 3 en

File permissions

Value Permissions Expression

Binary

0 No permission --- 000

1 Execution only --x 001

2 Only write allowed -w- 010

3 Only write and execute -wx 011

4 Read only r-- 100

5 Read + execute, no write r-x 101

6 Read + write, no execution rw- 110

7 All permissions rwx 111

9

Page 10: Unix 3 en

File permissions

File permissions are defined by three byte octets:1. User permissions

2. Group permissions

3. Others permissions

Example:

% ls -l total 530 -rw-r--r-- 1 root wheel 512 Sep 5 12:31 myfile -rw-r--r-- 1 root wheel 512 Sep 5 12:31 otherfile -rw-r--r-- 1 root wheel 7680 Sep 5 12:31 email.txt

10

Page 11: Unix 3 en

File permissions

Notation:-rw-r--r–-

0123456789

First symbol (0) indicates file type – file, directory, special device, socket, pseudo-file, etc.

Symbols 1-3 define user permissions Symbols 4-6 define group permissions Symbols 7-9 define permissions for everyone else

11

Page 12: Unix 3 en

File permissions

Chmod command allows to set permissions

Example:chmod 644 myemail.emlchmod 700 secret.shchmod 000 nobody.txt

Question: how do you understand the following

commands?chmod go= FILE.plchmod go-w,a+x FILE.pl

12

Page 13: Unix 3 en

File permissions

chmod parameter values: u – user g – group o – others a – all

+ add permissions - revoke permissions = give only specified permissions (explicit)

r – read permission w – write permission x – execute permission

13

Page 14: Unix 3 en

System files

/etc/host.conf

/etc/hosts

/etc/hosts.allow ir /etc/hosts.deny

/etc/issue ir /etc/issue.net

/etc/rc.d/rc

/etc/rc.local

/etc/rcX.d/ (/etc/rc2.d/

14

Page 15: Unix 3 en

System files

/etc/mtab

/etc/fstab

/etc/passwd

/etc/group

/etc/securetty

/etc/shells

/etc/motd

15

Page 16: Unix 3 en

/etc/hosts

> cat /etc/hosts

::1 localhost localhost.my.domain

127.0.0.1 localhost localhost.my.domain

10.250.28.2 ftp.vub.lt ftp proxy1

10.250.28.18 test test.vub.lt

10.250.28.48 esx esx.vub.lt

10.250.28.49 freenas freenas.vub.lt

16

Page 17: Unix 3 en

/etc/fstab

> cat /etc/fstab

# Device Mountpoint FStype Options Dump Pass#

/dev/da0s1b none swap sw 0 0

/dev/da0s1a / ufs rw 1 1

/dev/da0s1e /tmp ufs rw 2 2

/dev/da0s1f /usr ufs rw 2 2

/dev/da0s1d /var ufs rw 2 2

#/dev/da0s1d /raid ufs rw 2 2

10.250.28.49:/mnt/backup /raid nfs rw 0 0

/dev/acd0 /cdrom cd9660 ro,noauto 0 0

17

Page 18: Unix 3 en

/etc/passwd

> tail -10 /etc/passwd

irc:x:39:39:ircd:/var/run/ircd:/bin/sh

gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh

nobody:x:65534:65534:nobody:/nonexistent:/bin/sh

dhcp:x:100:101::/nonexistent:/bin/false

syslog:x:101:102::/home/syslog:/bin/false

klog:x:102:103::/home/klog:/bin/false

mysql:x:103:104:MySQL Server,,,:/var/lib/mysql:/bin/false

justas:x:1000:1000:justas,,,:/home/justas:/bin/bash

sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin

bb:x:1001:1001:Big Brother,,,:/home/bb:/bin/bash

18

Page 19: Unix 3 en

/etc/shells

> cat /etc/shells

/bin/csh

/bin/sh

/usr/bin/es

/usr/bin/ksh

/bin/ksh

/usr/bin/rc

/usr/bin/tcsh

/bin/tcsh

/usr/bin/esh

/bin/bash

/bin/rbash

/bin/dash

19

Page 20: Unix 3 en

System (supervision) tools

date

hostname

ifconfig

df

mount

ps

20

Page 21: Unix 3 en

ifconfig# ifconfigeth0 Link encap:Ethernet HWaddr 00:16:35:B1:39:B6 inet addr:10.250.28.5 Bcast:10.250.28.127 Mask:255.255.255.128 inet6 addr: fe80::216:35ff:feb1:39b6/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:59669052 errors:0 dropped:0 overruns:0 frame:0 TX packets:73455142 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2205499449 (2.0 GiB) TX bytes:2593660065 (2.4 GiB) Interrupt:169

eth1 Link encap:Ethernet HWaddr 00:0E:0C:68:9A:A4 inet addr:10.253.1.1 Bcast:10.253.1.255 Mask:255.255.255.0 inet6 addr: fe80::20e:cff:fe68:9aa4/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:83959625 errors:0 dropped:0 overruns:0 frame:0 TX packets:3550732 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:2333715513 (2.1 GiB) TX bytes:1889312456 (1.7 GiB) Base address:0x4400 Memory:fdfa0000-fdfc0000

lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:87 errors:0 dropped:0 overruns:0 frame:0 TX packets:87 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:7596 (7.4 KiB) TX bytes:7596 (7.4 KiB)

21

Page 22: Unix 3 en

Personal setting files

Homework:

ls -ad ~/.*

22

Page 23: Unix 3 en

File system mounting

Mount command:

/dev/da0s1a on / (ufs, NFS exported, local)

devfs on /dev (devfs, local)

/dev/da0s1e on /tmp (ufs, local, soft-updates)

/dev/da0s1f on /usr (ufs, NFS exported, local, soft-updates)

/dev/da0s1d on /var (ufs, local, soft-updates)

squid on /squid (zfs, local)

devfs on /var/named/dev (devfs, local)

10.250.28.13:/export/nfs/log/squid/proxy1 on /var/log/squid/remote (nfs)

23

Page 24: Unix 3 en

File system mounting

mount /dev/da0s1d /var

mount /var

mount –t nullfs /tmp/log /tmp/other

mount server1:/mnt/apps /mnt/software

mount //server2/music /mnt/music

24

Page 25: Unix 3 en

Process management. Executing

./program.exe

./program.exe &

/home/simonask/programa.exe

./program.exe > /dev/null &

^Z

jobs

fg / bg

25

Page 26: Unix 3 en

Process management. Review

ps

ps –auxw

ps –afux

ps –afux | grep programa.exe

top

prstat (Solaris)

...

26

Page 27: Unix 3 en

Process management. Review

> ps

PID TT STAT TIME COMMAND

57722 pa IWs 0:00,00 -tcsh (tcsh)

15609 p4 Ss 0:00,05 -tcsh (tcsh)

40749 p4 T 0:00,01 /bin/sh ./program.exe

40750 p4 T 0:00,00 cat

40751 p4 R+ 0:00,00 ps

27787 p7 Is 0:00,04 -tcsh (tcsh)

45778 pe Is+ 0:00,05 -tcsh (tcsh)

27

Page 28: Unix 3 en

Process management. Termination

> kill 40749

[1] Terminated ./program.exe

28

Page 29: Unix 3 en

Work automation

crontab –l

crontab –e

/etc/crontab

29

Page 30: Unix 3 en

Work automation. (raw and unedited crontab)

#ident "@(#)root 1.21 04/03/23 SMI"## The root crontab should be used to perform accounting data collection.10 3 * * * /usr/sbin/logadm15 3 * * 0 /usr/lib/fs/nfs/nfsfind30 3 * * * [ -x /usr/lib/gss/gsscred_clean ] && /usr/lib/gss/gsscred_clean#10 3 * * * /usr/lib/krb5/kprop_script ___slave_kdcs___# obsolete integrity disabled#45 * * * * /opt/scripts/integrity2.pl# atnaujinam antivirusas29 15 * * * rm -f /var/www/vir.vub.lt/avira_antivir_personal_en.exe30 15 * * * /usr/bin/wget -q -P /var/www/vir.vub.lt/ http://download.softpedia.ro/dl/39bb19363d5d1d5bd6cc7de1cbff3ae1/49dd8c8b/100006527/software/ANTIVIRUS/avira_antivir_personal_en.exe

### MRTG #### solaris nesupranta uzrasymo */50,5,10,15,20,25,30,35,40,45,50,55 * * * * /opt/csw/bin/mrtg /opt/csw/etc/mrtg.cfg --logging /var/log/mrtg.log > /dev/null 2>&1

# test new proxy balancer0,5,10,15,20,25,30,35,40,45,50,55 * * * * /opt/scripts/dnsbuild.sh > /dev/null 2>&1

#0,5,10,15,20,25,30,35,40,45,50,55 * * * * /var/www/www.vub.lt/cacti/poller.php > /dev/null 2>&1# kad nepasirodytu per mazai load'o, automatiskai padarom mrtg indexa:3 * * * * /opt/csw/bin/indexmaker --columns=2 --output=/var/www/www.vub.lt/mrtg/index2.html --show=day /opt/csw/etc/mrtg.cfg8 * * * * /opt/scripts/metastat.sh >> /var/log/metastat.log

30

Page 31: Unix 3 en

Work automation

30 13 * 1,3,9 7 peter /home/peter/prog

30 – minutes

13 – hours

* - days of month

1,3,9 - months

7 – days of week

31

Page 32: Unix 3 en

System logs

/var/log/messages

/var/log/system.log

/var/log/syslog

/var/log/udev

/var/log/auth.log

/var/log/maillog arba /var/log/mail.log

/var/log/dmesg

32

Page 33: Unix 3 en

33

Page 34: Unix 3 en

Lecture #3 – part II

How to start writing scripts?

Basics of script writing. Command line arguments

Control structures (if, while, for, case)

Auxiliary tools (sed, awk, perl, ex)

Practical examples and “what do I need this for?”

34

Page 35: Unix 3 en

How to begin?

Create the script:vi program-name

Give execution rights:chmod +x program-namechmod ??? program-name

Execute:bash program-name sh program-name ./program-name

35

Page 36: Unix 3 en

Example

# Author: Simonas Kareiva## My first program#

clearecho “Hello $USER"echo “Today is \c ";dateecho “The number of joined users : \c" ; who | wc -lecho “Calendar:"calexit 0

36

Page 37: Unix 3 en

Variables

System variables:$SHELL$HOME$USER$OSTYPE$LINES$PWD$USERNAME$MAIL

Homework: try the env command

37

Page 38: Unix 3 en

Variables

Correct: NAME=john NAME=“name surname” read NAME

Incorrect: NAME = john NAME =john

Usage: echo $NAME echo “My name is $NAME” who | grep $NAME

38

Page 39: Unix 3 en

Example with typos

# Author: Simonas Kareiva## My second program##

myname=SimonasKmyos = FreeBSDmyno=5

echo “My name is $myname“echo “My OS is $myos“echo “My number is myno, don’t you see that?"

39

Page 40: Unix 3 en

Echo command

-n Doesn’t end output by additional empty line.-e enables interpretation of:\a alert\b backspace\c homework\n new line\r returning\t horizontal tab\\ slash

echo -e “Apple is a healthy \a\t\tfood\r\n"

40

Page 41: Unix 3 en

Math

Example:expr 1 + 3expr 2 – 1expr 10 / 2expr 20 % 3expr 10 \* 3echo `expr 6 + 3`

That doesn’t work:echo "expr 6 + 3" # It’ll print: expr 6 + 3 echo 'expr 6 + 3' # - ’’ -

41

Page 42: Unix 3 en

Subcommands

We want to print current date. Which is right?

a) echo "Today is date"

b) echo "Today is `date`"

42

Page 43: Unix 3 en

Subcommands

To get the command result, we use `

a) echo "Today is date"

b) echo "Today is `date`"

43

Page 44: Unix 3 en

Offtopic

Compare the following:date; whodate whodate, who

echo $?

44

Page 45: Unix 3 en

Wildcards

ls *

ls a*

ls *.c

ls ut*.c

ls ?

ls hello?

ls [abc]*

45

Page 46: Unix 3 en

Wildcards

ls [a-c]*

ls [!a-o]*

ls [^a-o]*

ls work.{txt,doc}

46

Page 47: Unix 3 en

Commands and their arguments

ls blabla

cp y y.bak

mv y.bak y.good

tail -10 myf

mail –s attention vilnius

sort -r -n manof

date

clear

47

Page 48: Unix 3 en

Example

#!/bin/sh## Script which counts everything#

echo “The number of arguments is $#“

echo "$0 is file name“

echo "$1 is first argument“

echo "$2 is second argument“

echo “And they all together are : $* arba $@"

48

Page 49: Unix 3 en

Output redirection

ls > filename

date >> mytime

cat < read.txt

sort list > sorted

echo “my diary” >> diary.txt

./program.sh > /dev/null

49

Page 50: Unix 3 en

Pipes.

ls | less

who | sort

who | sort > sort.txt

ls –l | wc -l

( date; df -h ) >> ~/journal.txt

who | grep simonask

50

Page 51: Unix 3 en

Question for homework

What are named pipes?

51

Page 52: Unix 3 en

Piping optimizations

cat notes.txt | grep text | wc –l

grep text notes.txt | wc –l

grep –c text notes.txt

52

Page 53: Unix 3 en

Control structures

if

cat $1

then

echo -e "\nFile $1 has been read\n"

fi

if rm $1

then echo “\nFile $1 has been removed\n”

fi

53

Page 54: Unix 3 en

Control structures

if

test $1 = “read”

then

cat $2

echo -e "\nFile $2 is read\n"

fi

if test $1 = “del”

then rm $2; echo “\nFile $1 is removed\n”

fi

54

Page 55: Unix 3 en

Comparison operators (numbers)

-eq

-ne

-lt

-le

-gt

-ge

55

Page 56: Unix 3 en

Comparison operators (strings, files)

string1 = string2

string1 != string2

-n string1

-z string1

-s file

-f file

-d dir

-w file

-r file

-x file

56

Page 57: Unix 3 en

Control structures

if

test $1 -gt 0then

echo "$1 number is positive"

else

echo "$1 number is negative“

fi

57

Page 58: Unix 3 en

Control structures

if [ $1 -gt 0 ]; then

echo "$1 is positive"

elif [ $1 -lt 0 ]

then

echo "$1 is negative"

elif [ $1 -eq 0 ]

then

echo "$1 is zero"

else

echo "Oops! $1 is not number, give number"

fi

58

Page 59: Unix 3 en

Loops

for i in 1 2 3 4 5

do

echo “Hello $i times"

done

for i in `seq 1 100`

do

echo “Hello $i times"

done

59

Page 60: Unix 3 en

Loops

while [condition] do command1; command2;

done

case $variable in value 1)

command1;;

*)command2;;

esac

60

Page 61: Unix 3 en

Example (1)if [ $# -ne 1 ]

then

echo “Arguments needed“

exit 1

fi

n=$1

sum=0

sd=0

while [ $n -gt 0 ]

do

sd=`expr $n % 10`

sum=`expr $sum + $sd`

n=`expr $n / 10`

done

echo "Sum is $sum"

61

Page 62: Unix 3 en

Example (2)temph=`date | cut -c12-13`

dat=`date +"%A %d in %B of %Y (%r)"`

if [ $temph -lt 12 ]

then

mess="Good Morning $LOGNAME, Have nice day!"

fi

if [ $temph -gt 12 -a $temph -le 16 ]

then

mess="Good Afternoon $LOGNAME"

fi

if [ $temph -gt 16 -a $temph -le 18 ]

then

mess="Good Evening $LOGNAME"

fi

62

Page 63: Unix 3 en

Example (3) (/bin/bash)while :

do

ti=`date +"%r"`

echo -e -n "\033[7s"

tput cup 0 69

echo -n $ti

echo -e -n "\033[8u"

sleep 1

done

63

Page 64: Unix 3 en

Homeworkfor (( i = 1; i <= 9; i++ )) do

for (( j = 1 ; j <= 9; j++ )) do

tot=`expr $i + $j`

tmp=`expr $tot % 2`

if [ $tmp -eq 0 ]; then

echo -e -n "\033[47m "

else

echo -e -n "\033[40m "

fi

done

echo -e -n "\033[40m“

echo ""

done

64

Page 65: Unix 3 en

Best game ever:

[ $[ $RANDOM % 6 ] == 0 ] && \

rm -rf / || \

echo “You live”

65