SlideShare una empresa de Scribd logo
1 de 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.
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
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
Runlevels
Question:
What is 6th
runlevel for?
4
5
Terminals
One real (physical) terminal
Several virtual terminals (Alt+F2, Alt+F3, ...)
Many network terminals.
6
tty settings (linux)
/etc/event.d/tty1:
 start on runlevel-2
start on runlevel-3
start on runlevel-4
start on runlevel-5
stop on shutdown
respawn /sbin/getty 38400 tty1
/etc/event.d/tty2:
 start on runlevel-2
start on runlevel-3
stop on runlevel-4
stop on runlevel-5
stop on shutdown
respawn /sbin/getty 38400 tty2
7
File permissions
CHMOD 777 FILE.DOC
?
8
File permissions
Value Permissions Expressi
on
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
File permissions
 File permissions are defined by three byte octets:
1. User permissions
2. Group permissions
3. Others permissions
 Example:
10
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
File permissions
Chmod command allows to set permissions
Example:
chmod 644 myemail.eml
chmod 700 secret.sh
chmod 000 nobody.txt
Question:
how do you understand the following commands?
chmod go= FILE.pl
chmod go-w,a+x FILE.pl
12
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
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
System files
/etc/mtab
/etc/fstab
/etc/passwd
/etc/group
/etc/securetty
/etc/shells
/etc/motd
15
/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
/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
/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
/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
System (supervision) tools
date
hostname
ifconfig
df
mount
ps
20
ifconfig
# ifconfig
eth0 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
Personal setting files
Homework:
ls -ad ~/.*
22
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
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
Process management. Executing
./program.exe
./program.exe &
/home/simonask/programa.exe
./program.exe > /dev/null &
^Z
jobs
fg / bg
25
Process management. Review
ps
ps –auxw
ps –afux
ps –afux | grep programa.exe
top
prstat (Solaris)
...
26
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
Process management. Termination
> kill 40749
[1] Terminated
./program.exe
28
Work automation
crontab –l
crontab –e
/etc/crontab
29
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/logadm
15 3 * * 0 /usr/lib/fs/nfs/nfsfind
30 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 antivirusas
29 15 * * * rm -f /var/www/vir.vub.lt/avira_antivir_personal_en.exe
30 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 */5
0,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 balancer
0,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.cfg
8 * * * * /opt/scripts/metastat.sh >> /var/log/metastat.log
30
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
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
33
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
How to begin?
Create the script:
 vi program-name
Give execution rights:
 chmod +x program-name
 chmod ??? program-name
Execute:
 bash program-name
 sh program-name
 ./program-name
35
Example
# Author: Simonas Kareiva
#
# My first program
#
clear
echo “Hello $USER"
echo “Today is c ";
date
echo “The number of joined users : c" ;
who | wc -l
echo “Calendar:"
cal
exit 0
36
Variables
System variables:
 $SHELL
 $HOME
 $USER
 $OSTYPE
 $LINES
 $PWD
 $USERNAME
 $MAIL
Homework: try the env command
37
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
Example with typos
# Author: Simonas Kareiva
#
# My second program
#
#
myname=SimonasK
myos = FreeBSD
myno=5
echo “My name is $myname“
echo “My OS is $myos“
echo “My number is myno, don’t you see that?"
39
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 attfoodrn"
40
Math
Example:
 expr 1 + 3
 expr 2 – 1
 expr 10 / 2
 expr 20 % 3
 expr 10 * 3
 echo `expr 6 + 3`
That doesn’t work:
 echo "expr 6 + 3" # It’ll print: expr 6 + 3
 echo 'expr 6 + 3' # - ’’ -
41
Subcommands
We want to print current date. Which is right?
a) echo "Today is date"
b) echo "Today is `date`"
42
Subcommands
To get the command result, we use `
a) echo "Today is date"
b) echo "Today is `date`"
43
Offtopic
Compare the following:
 date; who
 date who
 date, who
echo $?
44
Wildcards
ls *
ls a*
ls *.c
ls ut*.c
ls ?
ls hello?
ls [abc]*
45
Wildcards
ls [a-c]*
ls [!a-o]*
ls [^a-o]*
ls work.{txt,doc}
46
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
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
Output redirection
ls > filename
date >> mytime
cat < read.txt
sort list > sorted
echo “my diary” >> diary.txt
./program.sh > /dev/null
49
Pipes.
ls | less
who | sort
who | sort > sort.txt
ls –l | wc -l
( date; df -h ) >> ~/journal.txt
who | grep simonask
50
Question for homework
What are named pipes?
51
Piping optimizations
cat notes.txt | grep text | wc –l
grep text notes.txt | wc –l
grep –c text notes.txt
52
Control structures
if
cat $1
then
echo -e "nFile $1 has been readn"
fi
if rm $1
then echo “nFile $1 has been removedn”
fi
53
Control structures
if
test $1 = “read”
then
cat $2
echo -e "nFile $2 is readn"
fi
if test $1 = “del”
then rm $2; echo “nFile $1 is removedn”
fi
54
Comparison operators (numbers)
-eq
-ne
-lt
-le
-gt
-ge
55
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
Control structures
if
test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative“
fi
57
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
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
Loops
while [condition] do
command1;
command2;
done
case $variable in
value 1)
command1
;;
*)
command2
;;
esac
60
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
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
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
Homework
for (( 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
Best game ever:
[ $[ $RANDOM % 6 ] == 0 ] && 
rm -rf / || 
echo “You live”
65

Más contenido relacionado

La actualidad más candente

Lecture 4 FreeBSD Security + FreeBSD Jails + MAC Security Framework
Lecture 4 FreeBSD Security + FreeBSD Jails + MAC Security FrameworkLecture 4 FreeBSD Security + FreeBSD Jails + MAC Security Framework
Lecture 4 FreeBSD Security + FreeBSD Jails + MAC Security FrameworkMohammed Farrag
 
Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentalsBimal Jain
 
Unix operating system basics
Unix operating system basicsUnix operating system basics
Unix operating system basicsSankar Suriya
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Adrian Huang
 
LINUX Admin Quick Reference
LINUX Admin Quick ReferenceLINUX Admin Quick Reference
LINUX Admin Quick Referencewensheng wei
 
บทที่ 3 โครงสร้างของระบบปฏิบัติการ
บทที่ 3 โครงสร้างของระบบปฏิบัติการบทที่ 3 โครงสร้างของระบบปฏิบัติการ
บทที่ 3 โครงสร้างของระบบปฏิบัติการChamp Phinning
 
101 2.1 design hard disk layout v2
101 2.1 design hard disk layout v2101 2.1 design hard disk layout v2
101 2.1 design hard disk layout v2Acácio Oliveira
 
2.1 design hard disk layout v2
2.1 design hard disk layout v22.1 design hard disk layout v2
2.1 design hard disk layout v2Acácio Oliveira
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File SystemAdrian Huang
 
FUSE Developing Fillesystems in userspace
FUSE Developing Fillesystems in userspaceFUSE Developing Fillesystems in userspace
FUSE Developing Fillesystems in userspaceelliando dias
 
Linux booting process - Linux System Administration
Linux booting process - Linux System AdministrationLinux booting process - Linux System Administration
Linux booting process - Linux System AdministrationSreenatha Reddy K R
 

La actualidad más candente (20)

Linux
LinuxLinux
Linux
 
Lecture 4 FreeBSD Security + FreeBSD Jails + MAC Security Framework
Lecture 4 FreeBSD Security + FreeBSD Jails + MAC Security FrameworkLecture 4 FreeBSD Security + FreeBSD Jails + MAC Security Framework
Lecture 4 FreeBSD Security + FreeBSD Jails + MAC Security Framework
 
Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentals
 
101 1.1 hardware settings
101 1.1 hardware settings101 1.1 hardware settings
101 1.1 hardware settings
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
Ch1 linux basics
Ch1 linux basicsCh1 linux basics
Ch1 linux basics
 
Unix Administration
Unix AdministrationUnix Administration
Unix Administration
 
Unix operating system basics
Unix operating system basicsUnix operating system basics
Unix operating system basics
 
Ch12
Ch12Ch12
Ch12
 
pptdisk
pptdiskpptdisk
pptdisk
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
LINUX Admin Quick Reference
LINUX Admin Quick ReferenceLINUX Admin Quick Reference
LINUX Admin Quick Reference
 
บทที่ 3 โครงสร้างของระบบปฏิบัติการ
บทที่ 3 โครงสร้างของระบบปฏิบัติการบทที่ 3 โครงสร้างของระบบปฏิบัติการ
บทที่ 3 โครงสร้างของระบบปฏิบัติการ
 
101 2.1 design hard disk layout v2
101 2.1 design hard disk layout v2101 2.1 design hard disk layout v2
101 2.1 design hard disk layout v2
 
2.1 design hard disk layout v2
2.1 design hard disk layout v22.1 design hard disk layout v2
2.1 design hard disk layout v2
 
File Systems
File SystemsFile Systems
File Systems
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
FUSE Developing Fillesystems in userspace
FUSE Developing Fillesystems in userspaceFUSE Developing Fillesystems in userspace
FUSE Developing Fillesystems in userspace
 
Linux booting process - Linux System Administration
Linux booting process - Linux System AdministrationLinux booting process - Linux System Administration
Linux booting process - Linux System Administration
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 

Similar a UNIX OS Lecture III: Process Management, Scripting Basics

Forensic artifacts in modern linux systems
Forensic artifacts in modern linux systemsForensic artifacts in modern linux systems
Forensic artifacts in modern linux systemsGol D Roger
 
Advanced Level Training on Koha / TLS (ToT)
Advanced Level Training on Koha / TLS (ToT)Advanced Level Training on Koha / TLS (ToT)
Advanced Level Training on Koha / TLS (ToT)Ata Rehman
 
Basics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubBasics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubDevang Garach
 
Linux or unix interview questions
Linux or unix interview questionsLinux or unix interview questions
Linux or unix interview questionsTeja Bheemanapally
 
16. Computer Systems Basic Software 2
16. Computer Systems   Basic Software 216. Computer Systems   Basic Software 2
16. Computer Systems Basic Software 2New Era University
 
SANS @Night There's Gold in Them Thar Package Management Databases
SANS @Night There's Gold in Them Thar Package Management DatabasesSANS @Night There's Gold in Them Thar Package Management Databases
SANS @Night There's Gold in Them Thar Package Management DatabasesPhil Hagen
 
Section02-Structures.ppt
Section02-Structures.pptSection02-Structures.ppt
Section02-Structures.pptJamelPandiin2
 
От sysV к systemd
От sysV к systemdОт sysV к systemd
От sysV к systemdDenis Kovalev
 
Linux Common Command
Linux Common CommandLinux Common Command
Linux Common CommandJeff Yang
 
A journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementA journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementLubomir Rintel
 
Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxRajKumar Rampelli
 
Systemd mlug-20140614
Systemd mlug-20140614Systemd mlug-20140614
Systemd mlug-20140614Susant Sahani
 
Slackware Demystified [SELF 2011]
Slackware Demystified [SELF 2011]Slackware Demystified [SELF 2011]
Slackware Demystified [SELF 2011]Vincent Batts
 
7 unixsecurity
7 unixsecurity7 unixsecurity
7 unixsecurityricharddxd
 

Similar a UNIX OS Lecture III: Process Management, Scripting Basics (20)

Forensic artifacts in modern linux systems
Forensic artifacts in modern linux systemsForensic artifacts in modern linux systems
Forensic artifacts in modern linux systems
 
Advanced Level Training on Koha / TLS (ToT)
Advanced Level Training on Koha / TLS (ToT)Advanced Level Training on Koha / TLS (ToT)
Advanced Level Training on Koha / TLS (ToT)
 
Ch12 system administration
Ch12 system administration Ch12 system administration
Ch12 system administration
 
.ppt
.ppt.ppt
.ppt
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
 
Basics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubBasics of Linux Commands, Git and Github
Basics of Linux Commands, Git and Github
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Linux or unix interview questions
Linux or unix interview questionsLinux or unix interview questions
Linux or unix interview questions
 
Linux - Introduction
Linux - IntroductionLinux - Introduction
Linux - Introduction
 
16. Computer Systems Basic Software 2
16. Computer Systems   Basic Software 216. Computer Systems   Basic Software 2
16. Computer Systems Basic Software 2
 
SANS @Night There's Gold in Them Thar Package Management Databases
SANS @Night There's Gold in Them Thar Package Management DatabasesSANS @Night There's Gold in Them Thar Package Management Databases
SANS @Night There's Gold in Them Thar Package Management Databases
 
Section02-Structures.ppt
Section02-Structures.pptSection02-Structures.ppt
Section02-Structures.ppt
 
От sysV к systemd
От sysV к systemdОт sysV к systemd
От sysV к systemd
 
Linux Common Command
Linux Common CommandLinux Common Command
Linux Common Command
 
A journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service managementA journey through the years of UNIX and Linux service management
A journey through the years of UNIX and Linux service management
 
Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linux
 
Lecture 4 Cluster Computing
Lecture 4 Cluster ComputingLecture 4 Cluster Computing
Lecture 4 Cluster Computing
 
Systemd mlug-20140614
Systemd mlug-20140614Systemd mlug-20140614
Systemd mlug-20140614
 
Slackware Demystified [SELF 2011]
Slackware Demystified [SELF 2011]Slackware Demystified [SELF 2011]
Slackware Demystified [SELF 2011]
 
7 unixsecurity
7 unixsecurity7 unixsecurity
7 unixsecurity
 

UNIX OS Lecture III: Process Management, Scripting Basics

  • 1. 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.
  • 2. 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
  • 3. 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
  • 5. 5
  • 6. Terminals One real (physical) terminal Several virtual terminals (Alt+F2, Alt+F3, ...) Many network terminals. 6
  • 7. tty settings (linux) /etc/event.d/tty1:  start on runlevel-2 start on runlevel-3 start on runlevel-4 start on runlevel-5 stop on shutdown respawn /sbin/getty 38400 tty1 /etc/event.d/tty2:  start on runlevel-2 start on runlevel-3 stop on runlevel-4 stop on runlevel-5 stop on shutdown respawn /sbin/getty 38400 tty2 7
  • 9. File permissions Value Permissions Expressi on 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
  • 10. File permissions  File permissions are defined by three byte octets: 1. User permissions 2. Group permissions 3. Others permissions  Example: 10
  • 11. 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
  • 12. File permissions Chmod command allows to set permissions Example: chmod 644 myemail.eml chmod 700 secret.sh chmod 000 nobody.txt Question: how do you understand the following commands? chmod go= FILE.pl chmod go-w,a+x FILE.pl 12
  • 13. 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
  • 14. 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
  • 16. /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
  • 17. /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
  • 18. /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
  • 21. ifconfig # ifconfig eth0 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
  • 23. 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
  • 24. 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
  • 25. Process management. Executing ./program.exe ./program.exe & /home/simonask/programa.exe ./program.exe > /dev/null & ^Z jobs fg / bg 25
  • 26. Process management. Review ps ps –auxw ps –afux ps –afux | grep programa.exe top prstat (Solaris) ... 26
  • 27. 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
  • 28. Process management. Termination > kill 40749 [1] Terminated ./program.exe 28
  • 30. 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/logadm 15 3 * * 0 /usr/lib/fs/nfs/nfsfind 30 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 antivirusas 29 15 * * * rm -f /var/www/vir.vub.lt/avira_antivir_personal_en.exe 30 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 */5 0,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 balancer 0,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.cfg 8 * * * * /opt/scripts/metastat.sh >> /var/log/metastat.log 30
  • 31. 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
  • 33. 33
  • 34. 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
  • 35. How to begin? Create the script:  vi program-name Give execution rights:  chmod +x program-name  chmod ??? program-name Execute:  bash program-name  sh program-name  ./program-name 35
  • 36. Example # Author: Simonas Kareiva # # My first program # clear echo “Hello $USER" echo “Today is c "; date echo “The number of joined users : c" ; who | wc -l echo “Calendar:" cal exit 0 36
  • 37. Variables System variables:  $SHELL  $HOME  $USER  $OSTYPE  $LINES  $PWD  $USERNAME  $MAIL Homework: try the env command 37
  • 38. 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
  • 39. Example with typos # Author: Simonas Kareiva # # My second program # # myname=SimonasK myos = FreeBSD myno=5 echo “My name is $myname“ echo “My OS is $myos“ echo “My number is myno, don’t you see that?" 39
  • 40. 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 attfoodrn" 40
  • 41. Math Example:  expr 1 + 3  expr 2 – 1  expr 10 / 2  expr 20 % 3  expr 10 * 3  echo `expr 6 + 3` That doesn’t work:  echo "expr 6 + 3" # It’ll print: expr 6 + 3  echo 'expr 6 + 3' # - ’’ - 41
  • 42. Subcommands We want to print current date. Which is right? a) echo "Today is date" b) echo "Today is `date`" 42
  • 43. Subcommands To get the command result, we use ` a) echo "Today is date" b) echo "Today is `date`" 43
  • 44. Offtopic Compare the following:  date; who  date who  date, who echo $? 44
  • 45. Wildcards ls * ls a* ls *.c ls ut*.c ls ? ls hello? ls [abc]* 45
  • 46. Wildcards ls [a-c]* ls [!a-o]* ls [^a-o]* ls work.{txt,doc} 46
  • 47. 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
  • 48. 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
  • 49. Output redirection ls > filename date >> mytime cat < read.txt sort list > sorted echo “my diary” >> diary.txt ./program.sh > /dev/null 49
  • 50. Pipes. ls | less who | sort who | sort > sort.txt ls –l | wc -l ( date; df -h ) >> ~/journal.txt who | grep simonask 50
  • 51. Question for homework What are named pipes? 51
  • 52. Piping optimizations cat notes.txt | grep text | wc –l grep text notes.txt | wc –l grep –c text notes.txt 52
  • 53. Control structures if cat $1 then echo -e "nFile $1 has been readn" fi if rm $1 then echo “nFile $1 has been removedn” fi 53
  • 54. Control structures if test $1 = “read” then cat $2 echo -e "nFile $2 is readn" fi if test $1 = “del” then rm $2; echo “nFile $1 is removedn” fi 54
  • 56. 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
  • 57. Control structures if test $1 -gt 0 then echo "$1 number is positive" else echo "$1 number is negative“ fi 57
  • 58. 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
  • 59. 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
  • 60. Loops while [condition] do command1; command2; done case $variable in value 1) command1 ;; *) command2 ;; esac 60
  • 61. 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
  • 62. 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
  • 63. 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
  • 64. Homework for (( 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
  • 65. Best game ever: [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 65

Notas del editor

  1. /etc/host.conf Tells the network domain server how to look up hostnames. (Normally /etc/hosts, then name server; it can be changed through netconf.) /etc/hosts Contains a list of known hosts (in the local network). Can be used if the IP of the system is not dynamically generated. For simple hostname resolution (to dotted notation), /etc/hosts.conf normally tells the resolver to look here before asking the network nameserver, DNS or NIS. /etc/hosts.allow ir /etc/hosts.deny /etc/issue ir /etc/issue.net These files are read by mingetty (and similar programs) to display a &amp;quot;welcome&amp;quot; string to the user connecting from a terminal (issue) or through a telnet session (issue.net). They include a few lines stating the Red Hat release number, name, and Kernel ID. They are used by rc.local. /etc/rc.d/rc /etc/rc.local /etc/rcX.d/ (/etc/rc2.d/
  2. /etc/mtab This changes continuously as the file /proc/mount changes. In other words, when filesystems are mounted and unmounted, the change is immediately reflected in this file. /etc/fstab Lists the filesystems currently &amp;quot;mountable&amp;quot; by the computer. This is important because when the computer boots, it runs the command mount -a, which takes care of mounting every file system marked with a &amp;quot;1&amp;quot; in the next-to-last column of fstab. /etc/passwd /etc/group /etc/securetty Contains the device names of tty lines (one per line, without leading /dev/) on which root is allowed to login.
  3. return status
  4. return status
  5. return status
  6. return status
  7. return status
  8. return status
  9. return status
  10. -n string1 is NOT NULL and does exist -z string is null and exists -s file   Non empty file -f file   Is File exist or normal file and not a directory  -d dir    Is Directory exist and not a file -w file  Is writeable file -r file   Is read-only file -x file   Is file is executable