nguyễn vũ hưng the unix and gnu/linux command line - power tools
Post on 29-Jan-2018
2.488 Views
Preview:
TRANSCRIPT
The Unix and GNU/Linux
Command Line
Nguyen Vu Hung Vuhung16plus (at) gmail.com
2012/05/12
Based on “intro_unix_linux” by Michael Opdenacker from Free Electrons
12/05/12
2
Best viewed with...
T h i s d o cu m e n t i s b e s t vi e w e d w i th a
L i b re O ffi c e .
T ak e ad van tag e o f i n te rn al o r e x te rn al
h yp e rli n k s .
S o , d o n ’t h e s i tate to c li c k o n th e m !
F i n d p ag e s q u i c k ly th an k s to
au to m ati c s e arch
U s e th u m b n ai ls to n avig ate i n th e
d o cu m e n t i n a q u i c k w ay
12/05/12
3
Command memento sheet
I t i s a u s e fu l co m p an io n to th i s p re s e n tati o n .
E xam p le s fo r th e m o s t u s e fu l co m m an d s are g ive n i n
ju s t o n e s h e e t.
S u g g e s ti o n s fo r u s e
S ti c k th i s s h e e t o n yo u r w all, u s e i t as d e s k to p
w allp ap e r, m ak e i t a m o u s e m at, p ri n t i t o n c lo th i n g ,
s li c e i t i n to b o o k m ark s ...
C au ti o n
S to re aw ay fro m m i ce !
12/05/12
4
Training Contents (1)
S h e lls , fi le sys te m an d fi le h an d li n g
E ve ryth i n g i s a fi le
G N U / L i n u x fi le sys te m s tru c tu re
C o m m an d li n e i n te rp re te rs
H an d li n g fi le s an d d i re c to ri e s
D i s p layin g , s can n i n g an d s o rti n g fi le s
S ym b o li c an d h ard li n k
F i le acce s s ri g h ts
12/05/12
5
Training contents (2)
S tan d ard I /O , re d i re c ti o n s , p i p e s
S tan d ard i n p u t an d o u tp u t,
re d i re c ti n g to fi le s
P i p e s : re d i re c ti n g s tan d ard o u tp u t to
o th e r co m m an d s
S tan d ard e rro r
12/05/12
6
Training Contents (3)
T ask co n tro l
F u ll co n tro l o n tas k s
E x e cu tin g i n b ack g ro u n d , s u s p e n d i n g , re s u m in g
an d ab o rtin g
L i s t o f active task s
K i lli n g p ro ce s s e s
E n vi ro n m e n t variab le s
PAT H e n vi ro n m e n t variab le s
S h e ll alias e s , .b ash rc fi le
12/05/12
7
Training contents (4)
M isc e llan e o u s
T e x t e d i to rs
C o m p re s s i n g an d arch ivi n g
Pri n ti n g fi le s
C o m p ari n g fi le s an d d i re c to ri e s
L o o k i n g fo r fi le s
G e tti n g i n fo rm ati o n ab o u t u s e rs
12/05/12
8
Training contents (5)
S ys te m ad m in i s trati o n b as i c s
F i le o w n e rsh i p
S e tti n g u p n e tw o rk i n g
F i le sys te m s : c re ati n g an d m o u n ti n g
G o in g fu rth e r
G e tti n g h e lp , acce s s i n g m an u al p ag e s
S e arch i n g th e I n te rn e t fo r re so u rce s
12/05/12
9
Training contents (6)
B as i c L AM P Ad m in i s trati o n
L i n u x
Ap ach e
M yS Q L
PH P
G o in g fu rth e r
L AM P o n U b u n tu
L AM P o n C e n tO S (R e d h at)
12/05/12
10
GNU/Linux and Free Software
“F re e ” as i n R M S 's d e fi n i ti o n .
T h i s p re s e n tati o n i n c lu d e d an
i n tro d u c ti o n to F O S S : o p e rati n g
sys te m s , ap p li cati o n s , k e y p ro je c ts an d
ru le s fo r s u cc e s s .
Ph i lo s o p h y o f th e G N U Pro je c t
D e fi n i ti o n (fre e to ru n , s tu d y, d i s tri b u te , c h an g e & & co p y)
W h y S o ftw are S h o u ld N o t H ave O w n e rs
C o p yle ft
F re e D o cu m e n tatio n
S e lli n g F re e S o ftw are i s O K
12/05/12
11
The Unix and GNU/Linux command line
Unix/Linux Filesystem
12/05/12
12
Everything is a file
R e g u lar fi le s
D i re cto ri e s
D i re cto ri e s are ju s t fi le s
li s ti n g a s e t o f fi le s
S ym b o li c li n k s
F i le s re fe rri n g to th e
n am e o f an o th e r fi le
D e vi c e s an d p e ri p h e rals
R e ad an d w ri te fro m
d e vi c e s as w i th re g u lar
fi le s
P i p e s
U s e d to cas cad e
p ro g ram s
cat *.log | grep error
S o ck e ts
I n te r p ro ce s s
co m m u n i cati o n
/d e v/< s th >
Alm o s t e ve ryth i n g i n U n i x i s a fi le !
12/05/12
13
File names
F i le n am e fe atu re s s i n c e th e b e g i n n i n g o f L i n u x
C as e s e n s i ti ve (i n d e x .h tm l ! = I n d e x .H T M L )
N o o b vi o u s le n g th lim i t
C an co n tai n an y ch aracte r(i n c lu d i n g w h i te s p ace ,
e x c e p t /).
- F i le typ e s s to re d i n th e fi le (“m ag i c n u m b e rs ”).
- F i le n am e e x te n s i o n s n o t n e e d e d an d n o t
i n te rp re te d . - Ju s t u s e d fo r u s e r co n ve n i e n ce .
F i le n am e e xam p le s :
README .bashrc Windows Buglistindex.htm index.html index.html.old
12/05/12
14
File paths
A p ath i s a s e q u e n ce o f n e s te d d i re cto ri e s w i th a fi le o r
d i re cto ry at th e e n d , s e p arate d b y th e / c h aracte r
R e lative p ath : Documents/fun/microsoft_jokes.html
R e lative to th e c u rre n t d i re c to ry
Ab so lu te p ath : /home/vuhung/work/present.odt
/ : root d ire ctory .
S tart o f ab s o lu te p ath s fo r all fi le s o n th e sys te m (e ve n
fo r fi le s o n re m o vab le d e vi c e s o r n e tw o rk s h are d ).
12/05/12
15
GNU/Linux filesystem structure (1)
N o t im p o s e d b y th e s ys te m . C an vary fro m o n e
s ys te m to th e o th e r, e ve n b e tw e e n tw o G N U /L in u x
i n s tallati o n s !
/ R o o t d i re c to ry
/bin/ B as i c , e s s e n tial s ys te m co m m an d s
/boot/ K e rn e l im ag e s , i n i trd , c o n fig u ratio n
fi le s
/dev/ F i le s re p re s e n ti n g d e vi c e s
/dev/hda: fi rs t I D E h ard d i s k
/etc/ S ys te m co n fi g u rati o n fi le s
/home/ U se r d i re c to ri e s
/lib/ B as i c sys te m s h are d li b rari e s
12/05/12
16
GNU/Linux filesystem structure (2)
/lost+found C o rru p t fi le s th e s ys te m tri e d to
re co ve r
/media M o u n t p o i n ts fo r re m o vab le m e d ia:
/media/usbdisk, /media/cdrom/mnt/ T e m p o rary m o u n t p o i n ts
/opt/ S p e c i fi c to o ls i n s talle d b y th e
s ysad m in
/usr/local/ o fte n u s e d i n s te ad
/proc/ Acce s s to s ys te m i n fo rm atio n
/proc/cpuinfo, /proc/version .../root/ ro o t u s e r h o m e d i re cto ry
/sbin/ Ad m in i s trato r-o n ly co m m an d s
/sys/ System and device controls(cpu frequency, device power, etc.)
12/05/12
17
GNU/Linux filesystem structure (3)
/tmp/ T e m p o rary fi le s
/usr/ R e g u lar u s e r to o ls (n o t e s s e n tial to th e
s ys te m )
/usr/bin/, /usr/lib/, /usr/sbin.../usr/local/ S p e c i fi c s o ftw are i n s talle d b y th e
s ysad m in
(o fte n p re fe rre d to /opt/)
/var/ D ata u s e d b y th e sys te m o r s ys te m
s e rve rs
/var/log/, /var/spool/mail (i n co m in g
m ai l), /var/spool/lpd (p ri n t jo b s )...
T h e U n i x fi le sys te m s tru c tu re i s d e fi n e d
b y th e F i le sys te m H i e rarch y S tan d ard (F H S ):
h ttp ://w w w .p ath n am e .co m /fh s/
12/05/12
18
The Unix and GNU/Linux command line
Shells and File Handling
12/05/12
19
Command line interpreters
S h e lls : to o ls to e x e cu te u s e r co m m an d s
C alle d “s h e lls ” b e cau s e th e y h i d e th e d e tai ls o n
th e u n d e rlyin g o p e rati n g sys te m u n d e r th e
s h e ll's s u rface .
C o m m an d s are i n p u t i n a te x t te rm in al, e i th e r a
w in d o w i n a g rap h i cal e n vi ro n m e n t o r a te x t-
o n ly co n so le .
R e s u lts are also d i s p laye d o n th e te rm in al. N o
g rap h i c s are n e e d e d at all.
S h e lls can b e s c ri p te d : p ro vi d e all th e
re s o u rce s to w ri te co m p le x p ro g ram s (variab le ,
c o n d i ti o n als , i te rati o n s ...)
12/05/12
20
Well known shells
M o st fam o u s an d p o p u lar s h e lls
bash: The Bourne Again shell (most popular)An improved implementation of sh with lots of added features too.
sh: T h e B o u rn e s h e ll (o b s o le te )
T rad i ti o n al, b as i c s h e ll fo u n d o n U n i x s ys te m s , b y
S te ve B o u rn e .
csh: T h e C s h e ll (o b s o le te )
O n ce p o p u lar s h e ll w i th a C -li k e s yn tax
tcsh: T h e T C s h e ll (s ti ll ve ry p o p u lar)
A C s h e ll co m p ati b le im p le m e n tati o n w i th e vo lve d
fe atu re s (co m m an d co m p le ti o n , h i s to ry e d i ti n g an d
m o re ...)
12/05/12
21
fish: a great new shellT h e F ri e n d ly I n te ractive S H e ll
h ttp ://w w w .fi s h s h e ll.o rg /
S tan d ard fe atu re s : h i s to ry, c o m m an d an d fi le
co m p le ti o n ...
B ran d n e w fe atu re s : co m m an d o p ti o n co m p le ti o n ,
co m m an d co m p le ti o n w i th s h o rt d e s cri p ti o n , s yn tax
h i g h li g h ti n g ..
E as i e r to an y o p e n fi le s : open b u i lt-i n c o m m an d .
M u ch s im p le r an d co n s i s te n t syn tax (n o t PO S I X
co m p lian t)
M ak e s i t e as i e r to cre ate s h e ll s cri p ts .
C o m m an d li n e b e g i n n e rs can le arn m u ch fas te r!
E ve n e x p e ri e n c e d u s e rs s h o u ld fi n d th i s s h e ll ve ry
co n ve n i e n t.
12/05/12
22
ls command
ls a (all)
L i s ts all th e fi le s
(i n c lu d i n g .* fi le s )
ls l (lo n g )
L o n g li s ti n g (typ e , d ate ,
s i z e , o w n e r, p e rm i s s i o n s )
ls t (time)Lists the most recent files first
ls S (s i z e )
L i s ts th e b i g g e s t fi le s fi rs t
ls r (re ve rs e )
R e ve rs e s th e s o rt o rd e r
ls ltr (o p ti o n s can b e
com b in e d )
L o n g li s ti n g , m o s t re ce n t
fi le s at th e e n d
Lists the files in the current directory, in alphanumeric order,except files starting with the “.” character.
12/05/12
23
File name pattern substitutions
B e tte r i n tro d u ce d b y e x am p le s !
ls *txtT h e s h e ll fi rs t re p lace s *txt b y all th e fi le an d
d i re c to ry n am e s e n d i n g b y txt (i n c lu d i n g .txt),
e x c e p t th o s e s tarti n g w i th ., an d th e n e x e cu te s th e
ls co m m an d li n e .
ls d .*L i s ts all th e fi le s an d d i re c to ri e s s tarti n g w i th .d te lls ls n o t to d i s p lay th e co n te n ts o f d i re c to ri e s .
cat ?.logD i s p lays all th e fi le s w h i c h n am e s s tart b y 1
ch aracte r an d e n d b y .log
12/05/12
24
Special directories (1)
./
The current directory. Useful for commands taking a directory argument. Also sometimes useful to run commands in the current directory (see later).
So ./readme.txt and readme.txt are equivalent.
../
T h e p are n t (e n clo s i n g ) d i re c to ry. Alw ays b e lo n g s to
th e . d i re cto ry (s e e ls a). O n ly re fe re n ce to th e
p are n t d i re cto ry.
T yp i cal u sag e :
cd ..
12/05/12
25
Special directories (2)
~/
N o t a s p e c ial d i re c to ry i n d e e d . S h e lls ju s t
s u b s ti tu te i t b y th e h o m e d i re c to ry o f th e
c u rre n t u s e r.
C an n o t b e u s e d i n m o s t p ro g ram s , as i t i s n o t a
re al d i re c to ry.
~vuhung/
S im i larly, s u b s ti tu te d b y s h e lls b y th e h o m e
d i re c to ry o f th e vuhung u s e r.
12/05/12
26
The cd and pwd commands
cd <dir>C h an g e s th e c u rre n t d i re c to ry to <dir>.
cd Gets back to the previous current directory.
pwdD i s p lays th e c u rre n t d i re c to ry ("w o rk i n g
d i re c to ry").
12/05/12
27
The cp command
cp <source_file> <target_file>C o p i e s th e s o u rce fi le to th e targ e t.
cp file1 file2 file3 ... dirC o p i e s th e fi le s to th e targ e t d i re c to ry (las t
arg u m e n t).
cp i (i n te ractive )
Ask s fo r u s e r c o n fi rm ati o n i f th e targ e t fi le
alre ad y e x i s ts
cp r <source_dir> <target_dir> (re cu rs ive )
C o p i e s th e w h o le d i re c to ry.
12/05/12
28
Smart directory copy with rsync
rsync (re m o te s yn c ) h as b e e n d e s i g n e d to k e e p i n
syn c
d i re cto ri e s o n 2 m ach i n e s w i th a lo w b an d w i d th
co n n e c ti o n .
O n ly co p i e s fi le s th at h ave ch an g e d . F i le s w i th th e sam e
s i ze are co m p are d b y ch e ck su m s .
O n ly tran s fe rs th e b lo ck s th at d i ffe r w i th i n a fi le !
C an co m p re s s th e tran s fe rre d b lo ck s
Pre s e rve s sym b o li c li n k s an d fi le p e rm i s s i o n s : als o ve ry
u s e fu l fo r co p i e s o n th e sam e m ach i n e .
C an w ork th ro u g h s s h (s e cu re re m o te s h e ll). Ve ry u s e fu l
to u p d ate th e co n te n ts o f a w e b s i te , fo r e xam p le .
12/05/12
29
rsync examples (1)
rsync a /home/vuhung/work /home/backup/work
a: archive mode. Equivalent to rlptgoD... easy way to tell you want recursion and want to preserve almost everything.
rsync Pav delete /home/vuhung/work/ /home/backup/work/
P: partial (keep partially transferred files) and progress (show progress during transfer)
delete: delete files in the target which don't exist in the source.
Caution: directory names should end with / . Otherwise, you get abackup/work/ directory at the destination.
12/05/12
30
rsync examples (2)
Copying to a remote machine
rsync Pav /home/vuhung/work/001.svn/ \ vuhung@192.168.52.123:/home/work/002.ipad/
User vuhung will be prompted for a password.
Copying from a remote machine through ssh
rsync Pav e ssh vuhung@192.168.52.123:/home/vuhung/work/ \tmp/work/
User vuhung will be prompted for his ssh key password.
12/05/12
31
mv and rm commands
mv <old_name> <new_name> (move)R e n am e s th e g ive n fi le o r d i re cto ry.
mv i (interactive)I f th e n e w fi le alre ad y e x i ts , as k s fo r u s e r co n fi rm
rm file1 file2 file3 ... (remove)R e m o ve s th e g ive n fi le s .
rm i (i n te ractive )
Alw ays as k fo r u s e r co n fi rm .
rm r dir1 dir2 dir3 (re cu rs ive )
R e m o ve s th e g ive n d i re c to ri e s w i th all th e i r
co n te n ts .
12/05/12
32
Create and delete directories
mkdir dir1 dir2 dir3 ... (make dir)C re ate s d i re c to ri e s w i th th e g ive n n am e s .
rmdir dir1 dir2 dir3 ... (remove dir)R e m o ve s th e g ive n d i re c to ri e s
S afe : o n ly w o rk s w h e n d i re c to ri e s an d e m p ty.
Alte rn ative : rm r (doesn't need empty directories).
mkdir p /dir1/dir2/dir3
12/05/12
33
Displaying file contents
S e ve ral w ays o f d i s p layi n g th e co n te n ts o f fi le s .
cat file1 file2 file3 ... (concatenate)C o n cate n ate s an d o u tp u ts th e co n te n ts o f th e g ive n
fi le s .
more file1 file2 file3 ...Afte r e ach p ag e , as k s th e u s e r to h i t a k e y to
co n ti n u e .
C an also ju m p to th e fi rs t o ccu rre n ce o f a k e yw o rd
(/ c o m m an d ).
less file1 file2 file3 ...D o e s m o re th an more w i th le s s .
D o e sn 't re ad th e w h o le fi le b e fo re s tarti n g .
S u p p o rts b ack w ard m o ve m e n t i n th e fi le (?
co m m an d ).
“m o re i s le s s ”
12/05/12
34
The head and tail commands
head [<n>] <file>D i s p lays th e fi rs t < n > li n e s (o r 1 0 b y d e fau lt) o f th e g ive n fi le .
D o e s n 't h ave to o p e n th e w h o le fi le to d o th i s !
tail [<n>] <file>D i s p lays th e las t < n > li n e s (o r 1 0 b y d e fau lt) o f th e g ive n fi le .
N o n e e d to lo ad th e w h o le fi le i n R AM ! Ve ry u s e fu l fo r h u g e
fi le s .
tail f <file> (follow)D i s p lays th e las t 1 0 li n e s o f th e g ive n fi le an d c o n ti n u e s to
d i s p lay n e w li n e s w h e n th e y are ap p e n d e d to th e fi le .
Ve ry u s e fu l to fo llo w th e c h an g e s i n a lo g fi le , fo r e x am p le .
E x am p le s
head /etc/passwdtail f /var/log/httpd/acces_log
12/05/12
35
The grep command
grep <pattern> <files>S can s th e g i ve n fi le s an d d i s p lays th e li n e s w h i ch m atch
th e g i ve n p atte rn .
grep URGETN *.logD i s p lays all th e li n e s co n tai n i n g error i n th e *.log fi le s
grep i URGENT *.logS am e , b u t cas e i n s e n s i ti ve
grep ri URGENT .S am e , b u t re cu rs i ve ly i n all th e fi le s i n . an d i ts
s u b d i re cto ri e s
grep v INFO *.logO u tp u ts all th e li n e s i n th e fi le s e x ce p t th o s e co n tai n i n g
info.
12/05/12
36
The sort command
sort <file>S o rts th e li n e s i n th e g ive n fi le i n c h aracte r
o rd e r an d o u tp u ts th e m .
sort r <file>S am e , b u t i n re ve rs e o rd e r.
sort ru <file>u: u n i q u e . S am e , b u t ju s t o u tp u ts i d e n ti cal
li n e s o n c e .
M o re p o s s i b i li ti e s d e s cri b e d late r!
12/05/12
37
Symbolic links
A sym b o li c li n k i s a s p e c ial fi le w h i c h
i s ju s t a re fe re n ce to th e n am e o f
an o th e r o n e (fi le o r d i re c to ry):
U s e fu l to re d u c e d i s k u sag e an d
co m p le x i ty w h e n 2 fi le s h ave th e sam e
co n te n t.
E x am p le :
/var/lo g /s e cu re /home/var.log.secure→
H o w to i d e n ti fy sym b o li c li n k s :
ls l d i s p lays > an d th e li n k e d fi le n am e .
G N U ls d i s p lays li n k s w i th a d i ffe re n t co lo r.
12/05/12
38
Creating symbolic links
T o cre ate a sym b o li c li n k (sam e o rd e r as i n cp):
ln s file_name link_name
T o cre ate a li n k w i th to a fi le i n an o th e r
d i re c to ry, w i th th e sam e n am e :
ln s ../README.txt
T o cre ate m u lti p le li n k s at o n ce i n a g ive n
d i re c to ry:
ln s file1 file2 file3 ... dir
T o re m o ve a li n k :
rm link_nameOf course, this doesn't remove the linked file!
12/05/12
39
Hard links
T h e d e fau lt b e h avio r fo r ln i s to c re ate hard links
A hard link to a fi le i s a re g u lar fi le w i th e xactly
th e sam e p h ys i cal co n te n ts
W h i le th e y s ti ll save s p ace , h ard li n k s can 't b e
d i s ti n g u i s h e d fro m th e o rig i n al fi le s .
I f yo u re m o ve th e o rig i n al fi le , th e re i s n o
im p act o n th e h ard li n k c o n te n ts .
T h e c o n te n ts are re m o ve d w h e n th e re are n o
m o re fi le s (h ard li n k s ) to th e m .
12/05/12
40
Files names and inodes
M ak e s h ard an d s ym b o li c (s o ft) li n k s
e as i e r to u n d e rs tan d !
Inode
File Hard linkSoft link
Inodeinterface
File name interface
Filesystem
Users
rmrm
12/05/12
41
File access rights
3 typ e s o f acce s s ri g h ts
R e ad acce s s (r)
W ri te acce s s (w)
E x e cu te ri g h ts (x)
3 typ e s o f acce s s le ve ls
U s e r (u): fo r th e o w n e r
o f th e fi le
G ro u p (g): e ach fi le also
h as a “g ro u p ” attri b u te ,
co rre s p o n d i n g to a g ive n
li s t o f u s e rs
O th e rs (o): fo r all o th e r
u s e rs
Use ls l to check file access rights
12/05/12
42
Access right constraints
x w i th o u t r i s le g al b u t i s u s e le s s
Yo u h ave to b e ab le to re ad a fi le to e x e cu te i t.
B o th r an d x p e rm i s s i o n s n e e d e d fo r d i re cto ri e s :
x to e n te r, r to li s t i ts co n te n ts .
Yo u can 't re n am e , re m o ve , c o p y fi le s i n a d i re cto ry
i f yo u d o n 't h ave w acce s s to th i s d i re cto ry.
I f yo u h ave w acc e s s to a d i re cto ry, yo u C AN
re m o ve a fi le e ve n i f yo u d o n 't h ave w ri te acce s s to
th i s fi le (re m e m b e r th at a d i re c to ry i s ju s t a fi le
d e s cri b i n g a li s t o f fi le s ). T h i s e ve n le ts yo u m o d i fy
(re m o ve + re cre ate ) a fi le e ve n w i th o u t w acce s s to
i t.
12/05/12
43
Access rights examples
rwrrR e ad ab le an d w ri tab le fo r fi le o w n e r, o n ly re ad ab le
fo r o th e rs
rwrR e ad ab le an d w ri tab le fo r fi le o w n e r, o n ly re ad ab le
fo r u s e rs b e lo n g i n g to th e fi le g ro u p .
drwxD i re cto ry o n ly acce s s i b le b y i ts o w n e r
rxF i le e x e cu tab le b y o th e rs b u t n e i th e r b y yo u r
fri e n d s n o r b y yo u rs e lf. N i c e p ro te cti o n s fo r a
trap ...
12/05/12
44
chmod: changing permissions
chmod <permissions> <files>2 fo rm ats fo r p e rm i s s i o n s :
O ctal fo rm at (ab c ):
a,b,c = r*4+w*2+x (r, w, x: b o o le an s )
E xam p le : chmod 644 <file>(rw fo r u, r fo r g an d o)
O r sym b o li c fo rm at. E asy to u n d e rs tan d b y
e xam p le s :
chmod go+r: ad d re ad p e rm i s s i o n s to g ro u p an d
o th e rs .
chmod uw: re m o ve w ri te p e rm i s s i o n s fro m
u s e r.
chmod ax: (a: all) re m o ve e x e cu te p e rm i s s i o n
fro m all.
12/05/12
45
More chmod (1)
chmod R a+rX folder/M ak e s folder an d e ve ryth i n g i n i t
avai lab le to e ve ryo n e !
R: ap p ly c h an g e s re cu rs ive ly
X: x, b u t o n ly fo r d i re c to ri e s an d fi le s
alre ad y e x e cu tab le
Ve ry u s e fu l to o p e n re cu rs ive acce s s to
d i re c to ri e s , w i th o u t ad d i n g e x e cu ti o n ri g h ts
to all fi le s .
12/05/12
46
More chmod (2)
chmod a+t /tmp
t: (sticky). Special permission for directories, allowing only the directory and file owner to delete a file in a directory.
Useful for directories with write access to anyone,like /tmp.
Displayed by ls l with a t character.
12/05/12
47
The Unix and GNU / Linux command line
Standard I/O, Redirections, Pipes
12/05/12
48
Standard output
M o re ab o u t co m m an d o u tp u t
All th e co m m an d s o u tp u tti n g te x t o n
yo u r te rm in al d o i t b y w ri ti n g to th e i r
stand ard outp ut.
S tan d ard o u tp u t can b e w ri tte n
(re d i re c te d ) to a fi le u s i n g th e >
s ym b o l
S tan d ard o u tp u t can b e ap p e n d e d to
an e x i s ti n g fi le u s i n g th e >> s ym b o l
12/05/12
49
Standard output redirection examples
ls ~vuhung/* > ~vuhung/vuhunghomelist.txt
cat america.txt > the_earth.txtcat america.txt >> the_world.txt
echo “README: No such file or directory” > READMEUseful way of creating a file without a text editor.Nice Unix joke too in this case.
12/05/12
50
Standard input
M o re ab o u t co m m an d i n p u t
L o ts o f co m m an d s , w h e n n o t g ive n i n p u t
arg u m e n ts , can tak e th e i r i n p u t fro m stand ard inp ut.
sortwindowslinux[Ctrl][D]linuxwindows
sort < participants.txtT h e s tan d ard i n p u t o f s o rt i s tak e n fro m th e g ive n
fi le .
sort takes its input fromthe standard input: in this case,what you type in the terminal(ended by [Ctrl][D])
12/05/12
51
Pipes
U n ix p i p e s are ve ry u s e fu l to re d i re ct th e s tan d ard
o u tp u t o f a co m m an d to th e s tan d ard i n p u t o f
an o th e r o n e .
E x am p le s
cat *.log | grep i error | sort
grep ri error . | grep v “ignored” | sort u \ > serious_errors.log
cat /home/*/homework.txt | grep grade | more
T h i s o n e o f th e m o s t p o w e rfu l fe atu re s i n U n i x
s h e lls !
12/05/12
52
The tee command
tee [a] file
T h e tee c o m m an d can b e u s e d to s e n d
s tan d ard o u tp u t to th e s cre e n an d to a fi le
s im u ltan e o u s ly.
make | tee build.logR u n s th e make c o m m an d an d s to re s i ts
o u tp u t to build.log.
make install | tee a build.logR u n s th e make install c o m m an d an d
ap p e n d s i ts o u tp u t to build.log.
12/05/12
53
Standard error
E rro r m e s sag e s are u s u ally o u tp u t (i f th e p ro g ram
i s w e ll w ri tte n ) to stand ard e rror i n s te ad o f
s tan d ard o u tp u t.
S tan d ard e rro r can b e re d i re cte d th ro u g h 2> o r 2>>
E x am p le :
cat f1 f2 nofile > newfile 2> error_file
Note: 1 is the descriptor for standard output, so 1> is equivalent to >.
C an re d i re ct b o th s tan d ard o u tp u t an d s tan d ard
e rro r to th e sam e fi le u s i n g &> :cat f1 f2 nofile &> whole_file
12/05/12
54
The yes commandU se fu l to fi ll s tan d ard i n p u t w i th
alw ays th e sam e s tri n g .
yes <string> | <command>K e e p s fi lli n g th e s tan d ard i n p u t o f
<command> w i th <string> (y b y
d e fau lt).
E x am p le s
yes | rm r dir/bank> yes no | credit_applicantyes "" | make oldconfig (equivalent to hitting [Enter] to accept all default settings)
12/05/12
55
Special devices (1)
D e vi c e fi le s w i th a s p e c ial b e h avio r o r co n te n ts
/dev/nullT h e d ata s i n k ! D i s card s all d ata w ri tte n to th i s fi le .
U s e fu l to g e t ri d o f u n w an te d o u tp u t, typ i cally lo g
i n fo rm ati o n :
mplayer big_buck_bunny_1080p_surround.avi &> /dev/null
/dev/zeroR e ad s fro m th i s fi le alw ays re tu rn \0 c h aracte rs
U s e fu l to c re ate a fi le fi lle d w i th ze ro s :
dd if=/dev/zero of=disk.img bs=1k count=2048
S e e man null o r man zero fo r d e tai ls
12/05/12
56
Special devices (2)/dev/randomR e tu rn s ran d o m b yte s w h e n re ad . M ain ly u s e d b y
cryp to g rap h i c p ro g ram s . U s e s i n te rru p ts fro m
s o m e d e vi c e d rive rs as s o u rce s o f tru e ran d o m n e s s
(“e n tro p y”).
R e ad s can b e b lo ck e d u n ti l e n o u g h e n tro p y i s
g ath e re d .
/dev/urandomF o r p ro g ram s fo r w h i c h p s e u d o ran d o m n u m b e rs
are fi n e .
Alw ays g e n e rate s ran d o m b yte s , e ve n i f n o t e n o u g h
e n tro p y i s avai lab le (i n w h i c h cas e i t i s p o s s i b le ,
th o u g h s ti ll d i ffi c u lt, to p re d i c t fu tu re b yte
s e q u e n ce s fro m p as t o n e s ).
S e e man random fo r d e tai ls .
12/05/12
57
Special devices (3)
/dev/fullM im i c s a fu ll d e vi c e .
U s e fu l to c h e ck th at yo u r ap p li cati o n p ro p e rly
h an d le s
th i s k i n d o f s i tu ati o n .
S e e man full fo r d e tai ls .
12/05/12
58
The Unix and GNU / Linux command line
Task Control
12/05/12
59
Full control on tasks
S in c e th e b e g i n n i n g , U n i x s u p p o rts
tru e p re e m p tive m u lti task i n g .
Ab i li ty to ru n m an y task s i n p aralle l,
an d ab o rt th e m e ve n i f th e y co rru p t
th e i r o w n s tate an d d ata.
Ab i li ty to c h o o s e w h i c h p ro g ram s yo u
ru n .
Ab i li ty to c h o o s e w h i c h i n p u t yo u r
p ro g ram s tak e s , an d w h e re th e i r
o u tp u t g o e s .
12/05/12
60
Processes
“E ve ryth i n g i n U n i x i s a fi le
E ve ryth i n g i n U n i x th at i s n o t a fi le i s a p ro ce s s ”
Pro ce s s e s
I n s tan ce s o f a ru n n i n g p ro g ram s
S e ve ral i n s tan ce s o f th e sam e p ro g ram can ru n at
th e sam e tim e
D ata as so c iate d to p ro ce s s e s :
O p e n fi le s , allo cate d m e m o ry, s tack , p ro ce s s i d ,
p are n t, p ri o ri ty, s tate ...
12/05/12
61
Running jobs in background
S am e u sag e th ro u g h o u t all th e s h e lls
U s e fu l
F or co m m an d li n e jo b s w h i ch o u tp u t can b e
e xam in e d late r, e s p e c ially fo r tim e
co n s u m in g o n e s .
T o s tart g rap h i cal ap p li cati o n s fro m th e
co m m an d li n e an d th e n co n ti n u e w i th th e
m o u s e .
S tarti n g a tas k : ad d & at th e e n d o f
yo u r li n e :
find_prince_charming cute clever rich &
12/05/12
62
Background job control
jobsR e tu rn s th e li s t o f b ack g ro u n d jo b s fro m th e sam e
s h e ll
[1] Running ~/bin/find_meaning_of_life withoutgod &[2]+ Running make mistakes &
fgfg %<n>Pu ts th e las t / n th b ack g ro u n d jo b i n fo re g ro u n d
m o d e
M o vin g th e cu rre n t task i n b ack g ro u n d m o d e :
[Ctrl] Zbg
kill %<n>Aborts the nth job.
12/05/12
63
Job control example
> jobs[1] Running ~/bin/find_meaning_of_life withoutgod &[2]+ Running make mistakes &
> fgmake mistakes
> [Ctrl] Z[2]+ Stopped make mistakes
> bg[2]+ make mistakes &
> kill %1[1]+ Terminated ~/bin/find_meaning_of_life withoutgod
12/05/12
64
Listing all processes
... whatever shell, script or process they are started from
ps uxLists all the processes belonging to the current user
ps aux (N o te : ps edf o n S ys te m V sys te m s )
L i s ts all th e p ro ce s s e s ru n n in g o n th e s ys te m
ps aux | grep bart | grep bashUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDbart 3039 0.0 0.2 5916 1380 pts/2 S 14:35 0:00 /bin/bashbart 3134 0.0 0.2 5388 1380 pts/3 S 14:36 0:00 /bin/bashbart 3190 0.0 0.2 6368 1360 pts/4 S 14:37 0:00 /bin/bashbart 3416 0.0 0.0 0 0 pts/2 RW 15:07 0:00 [bash]
PID: Process idVSZ: Virtual process size (code + data + stack)RSS: Process resident size: number of KB currently in RAMTTY: TerminalSTAT: Status: R (Runnable), S (Sleep), W (paging), Z (Zombie)...
12/05/12
65
Live process activitytop D i s p lays m o s t im p o rtan t p ro ce s s e s , s o rte d
b y cp u p e rce n tag e
top 15:44:33 up 1:11, 5 users, load average: 0.98, 0.61, 0.59Tasks: 81 total, 5 running, 76 sleeping, 0 stopped, 0 zombieCpu(s): 92.7% us, 5.3% sy, 0.0% ni, 0.0% id, 1.7% wa, 0.3% hi, 0.0% siMem: 515344k total, 512384k used, 2960k free, 20464k buffersSwap: 1044184k total, 0k used, 1044184k free, 277660k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND3809 vuhung 25 0 6256 3932 1312 R 93.8 0.8 0:21.49 bunzip22769 root 16 0 157m 80m 90m R 2.7 16.0 5:21.01 X
h to p : I m p ro ve d to p
Yo u can ch an g e th e s o rti n g o rd e r b y typ i n g
M: M e m o ry u sag e , P: % C PU , T: T im e .
Yo u can k i ll a task b y typ i n g k an d th e p ro ce s s i d .
12/05/12
66
Live process activityhtop D i s p lays m o s t im p o rtan t p ro ce s s e s , s o rte d
b y cp u p e rce n tag e
top 15:44:33 up 1:11, 5 users, load average: 0.98, 0.61, 0.59Tasks: 81 total, 5 running, 76 sleeping, 0 stopped, 0 zombieCpu(s): 92.7% us, 5.3% sy, 0.0% ni, 0.0% id, 1.7% wa, 0.3% hi, 0.0% siMem: 515344k total, 512384k used, 2960k free, 20464k buffersSwap: 1044184k total, 0k used, 1044184k free, 277660k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND3809 jdoe 25 0 6256 3932 1312 R 93.8 0.8 0:21.49 bunzip22769 root 16 0 157m 80m 90m R 2.7 16.0 5:21.01 X3006 jdoe 15 0 30928 15m 27m S 0.3 3.0 0:22.40 kdeinit3008 jdoe 16 0 5624 892 4468 S 0.3 0.2 0:06.59 autorun3034 jdoe 15 0 26764 12m 24m S 0.3 2.5 0:12.68 kscd3810 jdoe 16 0 2892 916 1620 R 0.3 0.2 0:00.06 top
Yo u can ch an g e th e s o rti n g o rd e r b y typ i n g
M: M e m o ry u sag e , P: % C PU , T: T im e .
Yo u can k i ll a task b y typ i n g k an d th e p ro ce s s i d .
12/05/12
67
Killing processes (1)kill <pids>S e n d s an ab o rt s i g n al to th e g ive n p ro ce s s e s .
L e ts p ro ce s s e s save d ata an d e x i t b y
th e m s e lve s . S h o u ld b e u s e d fi rs t. E x am p le :
kill 3039 3134 3190 3416
kill 9 <pids>S e n d s an im m e d iate te rm in ati o n s i g n al. T h e
sys te m i ts e lf te rm in ate s th e p ro ce s s e s . U s e fu l
w h e n a p ro c e s s i s re ally s tu ck (d o e sn 't an sw e r
to kill 1).
kill 9 1K i lls all th e p ro ce s s e s o f th e c u rre n t u s e r. 1:
m e an s all p ro ce s s e s .
12/05/12
68
Killing processes (2)
killall [<signal>] <command>K i lls all th e jo b s ru n n i n g <command>. E xam p le :
killall bash
xkillLets you kill a graphical application by clicking on it!Very quick! Convenient when you don't know the application command name.
12/05/12
69
Recovering from stuck graphics
I f yo u r g rap h i cal s e s s i o n i s s tu ck an d yo u can n o lo n g e r
typ e i n yo u r te rm i n als , d o n 't re b o o t!
I t i s ve ry li k e ly th at yo u r sys te m i s s ti ll fi n e . T ry to
acce s s a te x t co n so le b y p re s s i n g th e [Ctrl][Alt][F1]
k e ys
(o r [F2],[F3] fo r m o re te x t co n so le s )
I n th e te x t co n so le , yo u can try to k i ll th e g u i lty
ap p li cati o n .
O n ce th i s i s d o n e , yo u can g o b ack to th e g rap h i c
s e s s i o n b y p re s s i n g [Ctrl][Alt][F5] or [Ctrl][Alt][F7] (depending on your distribution)
If you can't identify the stuck program, you can also kill all your processes: kill 9 1You are then brought back to the login screen.
12/05/12
70
Sequential commands
C an typ e th e n e x t co m m an d i n yo u r te rm in al e ve n
w h e n th e c u rre n t o n e i s n o t o ve r.
C an s e p arate co m m an d s w i th th e ; s ym b o l:
echo “I love thee”; sleep 10; echo “ not”
C o n d i ti o n als : u s e || (o r) o r && (an d ):
more God || echo “Sorry, God doesn't exist”Runs echo only if the first command fails
ls ~sd6 && cat ~sd6/* > ~sydney/recipes.txtOnly cats the directory contents if the ls command succeeds (means read access).
12/05/12
71
Quoting (1)
D o u b le (") q u o te s can b e u s e d to p re ve n t th e s h e ll
fro m i n te rp re ti n g s p ace s as arg u m e n t s e p arato rs ,
as w e ll as to p re ve n t fi le n am e p atte rn e x p an s i o n .
> echo "Hello World"Hello World
> echo "You are logged as $USER"You are logged as vuhung
> echo *.logfind_prince_charming.log cosmetic_buys.log
> echo "*.log"*.log
12/05/12
72
Quoting (2)
S i n g le q u o te s b ri n g a s im i lar fu n c ti o n ali ty, b u t
w h at i s b e tw e e n q u o te s i s n e ve r s u b s ti tu te d
> echo 'You are logged as $USER'You are logged as $USER
Back quotes (`) can be used to call a command within another
> cd /lib/modules/`uname r`; pwd/lib/modules/2.6.91.6_FC2
Back quotes can be used within double quotes
> echo "You are using Linux `uname r`"You are using Linux 2.6.91.6_FC2
12/05/12
73
Measuring elapsed time
time find_expensive_housing near<...command output...>real 0m2.304s (actual elapsed time)user 0m0.449s (CPU time running program code)sys 0m0.106s (CPU time running system calls)
real = user + sys + waitingwaiting = I/O waiting time + idle time (running other tasks)
12/05/12
74
Environment variables
S h e lls le t th e u s e r d e fi n e variab le s.
T h e y can b e re u s e d i n s h e ll co m m an d s .
C o n ve n ti o n : lo w e r cas e n am e s
e x p o rt M YN AM E = 'vu h u n g '
Yo u can also d e fi n e e nvironm ent variab le s:
variab le s th at are als o vi s i b le w i th i n s cri p ts
o r e x e cu tab le s calle d fro m th e s h e ll.
C onve ntion : up p e r case nam e s.
$setL i s ts all d e fi n e d e n vi ro n m e n t variab le s an d
th e i r valu e .
12/05/12
75
Shell variables examples
S h e ll variab le s (b ash )
projdir=/home/vuhung/coolstuffls la $projdir; cd $projdir
E n vi ro n m e n t variab le s (b ash )
cd $HOME
export DEBUG=1./find_extraterrestrial_life(displays debug information if DEBUG is set)
12/05/12
76
Main standard environment variables
LD_LIBRARY_PATHS h are d li b rary s e arch p ath
DISPLAYS cre e n i d to d i s p lay X
(g rap h i cal) ap p li cati o n s o n .
EDITORD e fau lt e d i to r (vi , e m acs ...)
HOMEC u rre n t u s e r h o m e
d i re c to ry
HOSTNAMEN am e o f th e lo cal m ach i n e
MANPATHM an u al p ag e s e arch p ath
PATHC o m m an d s e arch p ath
PRINTERD e fau lt p ri n te r n am e
SHELLC u rre n t s h e ll n am e
TERMC u rre n t te rm i n al typ e
USERC u rre n t u s e r n am e
Extremely widely used!
12/05/12
77
PATH environment variables
PATHS p e c i fi e s th e s h e ll s e arch o rd e r fo r co m m an d s
/home/vuhung/bin:/usr/local/bin:/usr/kerberos/bin:
/usr/bin:/bin:/usr/X11R6/bin:/bin:/usr/bin
LD_LIBRARY_PATHS p e c i fi e s th e s h are d li b rary (b i n ary co d e li b rari e s
s h are d b y ap p li cati o n s , li k e th e C li b rary) s e arch o rd e r
fo r ld
/usr/local/lib:/usr/lib:/lib:/usr/X11R6/lib
MANPATHS p e c i fi e s th e s e arch o rd e r fo r m an u al p ag e s
/usr/local/man:/usr/share/man
12/05/12
78
PATH usage warning
I t i s s tro n g ly re co m m e n d e d n o t to h ave th e “.”
d i re c tory i n yo u r PATH e n vi ro n m e n t variab le , i n
p arti cu lar n o t at th e b e g i n n i n g :
A crack e r co u ld p lace a m ali c i o u s ls fi le i n yo u r
d i re c tori e s . I t w ou ld g e t e x e cu te d w h e n yo u ru n ls i n
th i s d i re cto ry an d co u ld d o n au g h ty th i n g s to you r d ata.
I f yo u h ave an e x e cu tab le fi le calle d test i n a d i re cto ry,
th i s w i ll o ve rri d e th e d e fau lt test p ro g ram an d s o m e
s cri p ts w i ll s to p w ork i n g p ro p e rly.
E ach tim e yo u cd to a n e w d i re c to ry, th e s h e ll w i ll w as te
tim e u p d ati n g i ts li s t o f avai lab le com m an d s .
C all yo u r lo cal co m m an d s as fo llo w s : ./test
12/05/12
79
Alias
S h e lls le t yo u d e fi n e com m an d aliase s: s h o rtcu ts fo r
com m an d s yo u u s e ve ry fre q u e n tly.
E xam p le s
alias ls='ls la'Useful to always run commands with default arguments.
alias rm='rm i'U s e fu l to m ak e rm alw ays as k fo r co n fi rm atio n .
alias frd='find_rambaldi_device asap risky'Useful to replace very long and frequent commands.
alias cia='. /home/vuhung/env/cia.sh'Useful to set an environment in a quick way(. is a shell command to execute the content of a shell script).
12/05/12
80
The which command
B e fo re yo u ru n a co m m an d , which te lls yo u w h e re
i t i s fo u n d
bash> which lsalias ls='ls color=tty' /bin/ls
tcsh> which lsls: aliased to ls color=tty
bash> which alias/usr/bin/which: no alias in (/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin)
tcsh> which aliasalias: shell builtin command.
12/05/12
81
~/.bashrc file
~/.bashrc, ~/.bash_profile, /etc/bashrcS h e ll s cri p t re ad e ach tim e a bash
s h e ll i s s tarte d
Yo u can u s e th i s fi le to d e fi n e
Yo u r d e fau lt e n vi ro n m e n t variab le s (PATH,
EDITOR...).
Yo u r alias e s .
Yo u r p ro m p t (s e e th e bash m an u al fo r d e tai ls ).
A g re e ti n g m e s sag e .
12/05/12
82
Command editing
Yo u can u s e th e le ft an d ri g h t arro w
k e ys to m o ve th e c u rs o r i n th e c u rre n t
co m m an d .
Yo u can u s e [Ctrl][a] to g o to th e
b e g i n n i n g o f th e li n e , an d [Ctrl][e]
to g o to th e e n d .
Yo u can u s e th e u p an d d o w n arro w s
to s e le c t e arli e r co m m an d s .
12/05/12
83
Command history (1)historyD i s p lays th e late s t co m m an d s th at yo u ran an d
th e i r n u m b e r. Yo u can co p y an d p as te co m m an d
s tri n g s .
Yo u can re call th e late s t co m m an d :
!!
Yo u can re call a co m m an d b y i ts n u m b e r
!1003
Yo u can re call th e late s t co m m an d m atch i n g a
s tarti n g s trin g :
!cat
CtrlR
Search bash history backward
12/05/12
84
Command history (2)
Yo u can m ak e s u b s ti tu ti o n s o n th e
late s t co m m an d :
^more^less
Yo u can ru n an o th e r co m m an d w i th
th e sam e arg u m e n ts :
more !*
12/05/12
85
The Unix and GNU / Linux command line
MiscellaneousText Editors
12/05/12
86
Text editors
G rap h i cal te x t e d i to rs
F i n e fo r m o s t n e e d s
n e d i t
E m acs , X e m acs
T e x t-o n ly te x t e d i to rs
O fte n n e e d e d fo r sysad m in s an d g re at fo r
p o w e r u s e rs
vi (vim )
n an o
12/05/12
87
The gedit text editor
G n o m e g e d i t
B e s t te x t e d i to r fo r n on vi or emacs experts
F e atu re h i g h lig h ts :
Ve ry e asy te x t s e le c ti o n an d m o vi n g
S yn tax h i g h li g h ti n g fo r m o s t lan g u ag e s an d
fo rm ats . C an b e tai lo re d fo r yo u r o w n lo g
fi le s , to h i g h li g h t p arti c u lar e rro rs an d
w arn i n g s .
E asy to cu s to m i z e th ro u g h m e n u s
I n s talle d b y d e fau lt b y m o s t
d i s tri b u ti o n s
12/05/12
88
gedit screenshot
12/05/12
89
Emacs/Xemacs
E m acs an d X e m acs are p re tty
s im i lar (u p to yo u r
p re fe re n c e )
E x tre m e ly p o w e rfu l te x t
e d i to r fe atu re s
G re at fo r p o w e r u s e rs
L e s s e rg o n o mic than nedit
N o n s tan d ard s h o rtcu ts
M u ch m o re th an a te x t e d i to r
(g am e s , e -m ai l, s h e ll,
b ro w s e r).
S o m e p o w e r co m m an d s h ave
to b e le arn t.
12/05/12
90
vi (vim)
T e x t-m o d e te x t e d i to r avai lab le i n all
U n i x s ys te m s . C re ate d b e fo re
co m p u te rs w i th m i c e ap p e are d .
D i ffi c u lt to le arn fo r b e g i n n e rs u s e d to
g rap h i cal te x t e d i to rs .
Ve ry p ro d u c tive fo r p o w e r u s e rs .
O fte n can 't b e re p lace d to e d i t fi le s i n
s ys te m ad m in i s trati o n o r i n E m b e d d e d
S ys te m s , w h e n yo u ju s t h ave a te x t
co n so le .
12/05/12
91
vim - vi improved
vi im p le m e n tati o n n o w fo u n d i n m o s t G N U /
L i n u x h o s t sys te m s
I m p le m e n ts lo ts o f fe atu re s avai lab le i n
m o d e rn e d i to rs : s yn tax h ig h li g h ti n g ,
co m m an d h i s to ry, h e lp , u n lim i te d u n d o an d
m u ch m u ch m o re .
C o o l fe atu re e xam p le : can d i re ctly o p e n
co m p re s s e d te x t fi le s .
C o m e s w i th a G T K g rap h i cal i n te rface (gvim)
U n fo rtu n ate ly, n o t fre e s o ftw are (b e cau s e o f
a sm all re s tri c ti o n i n fre e d o m to m ak e
ch an g e s )
12/05/12
92
vi basic commands
Though vi is extremely powerful, its main 30 commands are easy to learn and are sufficient for 99% of everyone's needs!
You can also take the quick tutorial by running vimtutor.
12/05/12
93
GNU nano
An o th e r sm all te x t-o n ly, m o u s e fre e te x t
e d i to r.
An e n h an ce d P i co c lo n e (n on fre e e d i to r i n
P i n e )
F ri e n d ly an d e as i e r to le arn fo r b e g i n n e rs
th an k s to o n s cre e n co m m an d s u m m ari e s .
Avai lab le i n b i n ary p ack ag e s fo r s e ve ral
p latfo rm s .
An alte rn ative to vi i n e m b e d d e d sys te m s .
H o w e ve r, n o t avai lab le as a b u syb o x b u i lt-
i n .
12/05/12
94
GNU nano screenshot
12/05/12
95
The Unix and GNU/Linux command line
MiscellaneousCompressing and Archiving
12/05/12
96
Measuring disc usage
C au ti o n : d i ffe re n t fro m fi le s i z e !
du h <file> (d i s k u sag e )
h: re tu rn s s i z e o n d i s k o f th e g ive n fi le , i n
h u m an re ad ab le fo rm at: K (k i lo b yte s ), M
(m e g ab yte s ) o r G (g ig ab yte s ), . W ith o u t h, du
re tu rn s th e raw n u m b e r o f d i s k b lo ck s u s e d b y
th e fi le (h ard to re ad ).
N o te th at th e h o p ti o n o n ly e x i s ts i n G N U du.
du sh <dir>s: re tu rn s th e s u m o f d i s k u sag e o f all th e fi le s
i n th e g ive n d i re c to ry.
12/05/12
97
Measuring disk space
df h <dir>R e tu rn s d i s k u sag e an d fre e s p ace fo r th e
fi le sys te m co n tain i n g th e g ive n d i re c to ry.
S im i larly, th e h o p ti o n o n ly e x i s ts i n G N U df.
Example:> df h .Filesystem Size Used Avail Use% Mounted on/dev/hda5 9.2G 7.1G 1.8G 81% /
df hR e tu rn s d i s k s p ace i n fo rm ati o n fo r all
fi le sys te m s avai lab le i n th e sys te m . W h e n
e rro rs h ap p e n , u s e fu l to lo o k fo r fu ll
fi le sys te m s .
d u -s h /
12/05/12
98
Compressing
Ve ry u s e fu l fo r s h ri n k i n g h u g e fi le s an d savin g
s p ace
g[un]zip <file>G N U z i p co m p re s s i o n u ti li ty. C re ate s .gz fi le s .
O rd i n ary p e rfo rm an ce (s im i lar to Z i p ).
b[un]zip2 <file>M o re re ce n t an d e ffe ctive co m p re s s i o n u ti li ty.
C re ate s .bz2 fi le s . U s u ally 2 0-2 5 % b e tte r th an gzip.
U s i n g 7-z i p
M u ch b e tte r co m p re s s i o n rati o th an b z i p 2 (u p to 1 0
to 2 0% ).
S e e th e 7-z i p p ag e fo r d e tai ls .
12/05/12
99
Archiving (1)
U se fu l to b ack u p o r re le as e a s e t o f fi le s w i th i n
1 fi le
tar: o ri g i n ally “tap e arch ive ”
C re ati n g an arch ive :
tar cvf <archive> <files or directories>c: c re ate
v: ve rb o s e . U s e fu l to fo llo w arch ivi n g p ro g re s s .
f: fi le . Arch ive cre ate d i n fi le (tap e u s e d
o th e rw i s e ).
E xam p le :
tar cvf /backup/home.tar /homebzip2 /backup/home.tar
12/05/12
100
Archiving (2)
Vie w in g th e co n te n ts o f an arch ive o r
i n te g ri ty ch e ck :
tar tvf <archive>t: te s t
E x tracti n g all th e fi le s fro m an arch ive :
tar xvf <archive>
E xtracti n g ju s t a fe w fi le s fro m an arch ive :
tar xvf <archive> <files or directories>F i le s o r d i re c to ri e s are g ive n w i th p ath s
re lative to th e arch ive ro o t d i re c to ry.
12/05/12
101
Extra options in GNU tar
tar = gtar = G N U tar o n G N U / L i n u x
C an co m p re s s an d u n co m p re s s arch ive s o n th e
fly. U s e fu l to avo i d c re ati n g h u g e i n te rm e d iate
fi le s
M u ch s im p le r to d o th an w i th tar an d bzip2!
j o p ti o n : [u n ]co m p re s s e s o n th e fly w i th bzip2
z o p ti o n : [u n ]co m p re s s e s o n th e fly w i th gzip
E xam p le s
tar cvjf work.tar.bz2 work/
tar cvzf work.tar.gz work/
tar xvzf work.tar.bz2; tar xvjf work.tar.gz
12/05/12
102
7-zip (1)h ttp ://w w w .7-z i p .o rg /
N o w th e b e s t s o lu ti o n fo r yo u r arch ive s !
L i c e n s e : G N U L G PL
7-z i p c o m p re s s e s m u ch b e tte r th an b z i p 2 (u p to 1 0 o r
2 0% )
an d o f co u rs e z i p (30 to 5 0 % ).
B e n ch m ark co m p re s s i n g K n o p p i x 5 .0.1 : -2 2 % (vs .
b z i p 2 )!
C au ti o n : 7-z i p can n o t re p lace tar fo r arch ivi n g o n U n i x .
I t d o e sn 't k e e p fi le o w n e r an d g ro u p i n fo rm ati o n ,
b u t o f co u rs e k e e p s fi le p e rm i s s i o n s .
U s e i t to co m p re s s tar arch ive s !
I n vo k e : 7z , 7za
12/05/12
103
7-zip (2)
7-z i p s u p p o rts s tro n g AE S -2 5 6 e n cryp ti o n .
N o n e e d to e n cryp t i n a s e p arate p as s .
At las t a s o lu ti o n avai lab le fo r U n i x an d W in d o w s !
T h e to o l s u p p o rts m o s t o th e r co m p re s s i o n fo rm ats :
zip, cab , arj, g z i p , b z i p 2 , tar, cp i o , rp m an d d e b .
12/05/12
104
Using 7-zip
Arch ive fi le s are u s u ally cre ate d w i th a .7z e x te n s i o n
C re ati n g an arch ive : (a: ad d )
7z a <archive> <files or directories>
L i s t fi le s i n arch ive : (l: li s t)
7z l <archive>
E xtracti n g fro m a 7-z i p arch ive : (e: e x tract)
7z x <archive>
Backup a directory (keeping owner and group information):tar cf <dir> | 7z a si dir.tar.7z
Restore this backup:7z x so dir.tar.7z | tar xf
standard input
standard output
12/05/12
105
Checking file integrity
Very low cost solution to check file integrity
md5sum FC3i386disk*.iso > MD5SUMComputes a MD5 (Message Digest Algorithm 5) 128 bit checksum of the given files. Usually redirected to a file.
Example output:db8c7254beeb4f6b891d1ed3f689b412 FC3i386disc1.iso2c11674cf429fe570445afd9d5ff564e FC3i386disc2.isof88f6ab5947ca41f3cf31db04487279b FC3i386disc3.iso6331c00aa3e8c088cc365eeb7ef230ea FC3i386disc4.iso
md5sum c MD5SUMChecks the integrity of the files in MD5SUM by comparing their actual MD5 checksum with their original one.
12/05/12
106
The Unix and GNU / Linux command line
MiscellaneousPrinting
12/05/12
107
Unix printing
M u lti -u s e r, m u lti -jo b , m u lti -c li e n t, m u lti -p ri n te r
I n U n i x / L i n u x , p ri n ti n g co m m an d s d o n 't re ally
p ri n t. T h e y s e n d jo b s to p ri n ti n g q u e u e s , p o s s i b ly
o n th e lo cal m ach i n e , o n n e tw o rk p ri n ti n g s e rve rs
o r o n n e tw o rk p ri n te rs .
Pri n te r i n d e p e n d e n t s ys te m :
Pri n t s e rve rs o n ly acce p t jo b s i n Po s tS cri p t
o r te x t. Pri n te r d rive rs o n th e s e rve r tak e
care o f th e co n ve rs i o n to e ach p ri n te rs o w n fo rm at.
R o b u s t s ys te m :
R e b o o t a s ys te m , i t w i ll co n ti n u e to p ri n t p e n d i n g
jo b s .
12/05/12
108
Printing commands
U se fu l e n vi ro n m e n t variab le : PRINTERS e ts th e d e fau lt p ri n te r o n th e sys te m . E x am p le :
export PRINTER=lp
lpr [P<queue>] <files>S e n d s th e g ive n fi le s to th e s p e c i fi e d p ri n ti n g q u e u e
T h e fi le s m u s t b e i n te x t o r Po s tS cri p t fo rm at.
O th e rw i s e , yo u o n ly p ri n t g arb ag e .
a2ps [P<queue>] <files>“An y to Po s tS cri p t” co n ve rts m an y fo rm ats to
Po s tS cri p t an d s e n d th e o u tp u t to th e s p e c i fi e d
q u e u e . U s e fu l fe atu re s : s e ve ral p ag e s / s h e e t, p ag e
n u m b e ri n g , i n fo fram e ...
12/05/12
109
Print job control
lpq [P<queue>]Lists all the print jobs in the given or default queue.
lp is not readyRank Owner Job File(s) Total Size1st asloane 84 nsa_windows_backdoors.ps 60416 bytes2nd amoore 85 gw_bush_iraq_mistakes.ps 65024000 bytes
cancel <job#> [<queue>]Removes the given job number from the default queue.
12/05/12
110
Using PostScript and PDF files
Vie w in g a Po s tS cri p t fi le
Po s tS cri p t vi e w e rs e x i s t, b u t th e i r q u ali ty i s p re tty
p o o r.
B e tte r co n ve rt to PD F w i th ps2pdf:
ps2pdf decss_algorithm.psxpdf decss_algorithm.pdf &
Prin ti n g a PD F fi le
Yo u d o n 't n e e d to o p e n a PD F re ad e r!
B e tte r co n ve rt to Po s tS cri p t w i th pdf2ps:
pdf2ps rambaldi_artifacts_for_dummies.pdflpr rambaldi_artifacts_for_dummies.ps
12/05/12
111
The Unix and GNU / Linux command line
MiscellaneousComparing Files and Directories
12/05/12
112
Comparing files and directories
diff file1 file2R e p o rts th e d i ffe re n ce s b e tw e e n 2 fi le s , o r
n o th in g i f th e fi le s are i d e n ti cal.
diff r dir1/ dir2/R e p o rts all th e d i ffe re n c e s b e tw e e n fi le s w i th
th e sam e n am e i n th e 2 d i re c to ri e s .
T o i n ve s tig ate d i ffe re n c e s i n d e tai l, b e tte r u s e
g rap h i cal to o ls !
vim d i ff
12/05/12
113
vimdiffvimdiff file1 file2
12/05/12
114
meld
12/05/12
115
kdiffAn o th e r n i c e to o l to co m p are fi le s an d m e rg e
d i ffe re n ce s
12/05/12
116
gvimdiffAn o th e r n i c e to o l to vi e w d i ffe re n ce s i n
fi le s
Available in most distributions with gvimApparently not using diff.No issue with files with binary sections!
12/05/12
117
The Unix and GNU / Linux command line
MiscellaneousLooking for Files
12/05/12
118
The find command
B e tte r e x p lain e d b y a fe w e xam p le s !
find . name “*.pdf”L i s ts all th e *.pdf fi le s i n th e c u rre n t (.)
d i re c to ry o r s u b d i re c to ri e s . Yo u n e e d th e
d o u b le q u o te s to p re ve n t th e s h e ll fro m
e x p an d in g th e * c h aracte r.
find docs name "*.pdf" exec xpdf {} ';'F in d s all th e *.pdf fi le s i n th e docs d i re c to ry
an d d i s p lays o n e afte r th e o th e r.
M an y m o re p o s s i b i li ti e s avai lab le ! H o w e ve r,
th e ab o ve 2 e xam p le s c o ve r m o s t n e e d s .
12/05/12
119
The locate commandM u ch fas te r re g u lar e x p re s s i o n s e arch alte rn ative
to find
locate keysLists all the files on your system with keys in their name.
locate “*.pdf”L i s ts all th e *.pdf fi le s avai lab le o n th e w h o le
m ach i n e
locate “/home/fridgevuhung/*
eer*”Lists all the *beer* files in the given directory (absolute
p ath )
locate i s m u ch fas te r b e cau s e i t i n d e x e s all fi le s i n
a d e d i cate d d atab as e , w h i ch i s u p d ate d o n a
re g u lar b as i s .
updatedb
find i s b e tte r to s e arch th ro u g h re ce n tly cre ate d
fi le s .
12/05/12
120
The Unix and GNU / Linux command line
MiscellaneousVarious Commands
12/05/12
121
Getting information about users
whoL i s ts all th e u s e rs lo g g e d o n th e s ys te m .
whoamiT e lls w h at u s e r I am lo g g e d as .
groupsT e lls w h i ch g ro u p s I b e lo n g to .
groups <user>T e lls w h i ch g ro u p s <user> b e lo n g s to .
finger <user>T e lls m o re d e tai ls (re al n am e , e tc ) ab o u t <user>D i sab le d i n s o m e s ys te m s (s e cu ri ty re aso n s ).
12/05/12
122
Changing users
Yo u d o n o t h ave to lo g o u t to lo g o n an o th e r
u s e r acco u n t!
su vuhung(R are ) C h an g e to th e hyde acco u n t, b u t
k e e p i n g th e e n vi ro n m e n t variab le s e tti n g s
o f th e o rig i n al u s e r.
su vuhung(M o re fre q u e n t) L o g o n th e jekyll acco u n t,
w i th e xactly th e sam e s e tti n g s as th i s n e w
u s e r.
su W h e n n o arg u m e n t i s g ive n , i t m e an s th e
root u s e r.
12/05/12
123
The wget command
I n s te ad o f d o w n lo ad i n g fi le s fro m yo u r b ro w s e r,
ju s t co p y an d p as te th e i r U R L an d d o w n lo ad th e m
w i th wget!
wget m ai n fe atu re s
h ttp an d ftp s u p p o rt
C an re s u m e i n te rru p te d d o w n lo ad s
C an d o w n lo ad e n ti re s i te s o r at le as t c h e ck fo r b ad
li n k s
Ve ry u s e fu l i n s cri p ts o r w h e n n o g rap h i c s are
avai lab le
(sys te m ad m in i s trati o n , e m b e d d e d sys te m s )
Pro x y s u p p o rt (http_proxy an d ftp_proxy e n v.
(s e t) variab le s )
12/05/12
124
wget examples
wget c \ http://microsoft.com/customers/dogs/winxp4dogs.zipContinues an interrupted download.
wget m https://www.facebook.com/nguyenvuhungMirrors a site.
wget r np http://virror.hanoilug.org/Recursively downloads an online book for offline access.np: "noparent". Only follows links in the current directory.
12/05/12
125
Misc commands (1)
sleep 60W aits fo r 60 s e co n d s
(d o e s n 't co n s u m e sys te m re s o u rce s ).
wc report.txt (word count)438 2115 18302 report.txtC o u n ts th e n u m b e r o f li n e s , w o rd s an d
ch aracte rs i n a fi le o r i n s tan d ard i n p u t.
12/05/12
126
Misc commands (2)
bc ("basic calculator?")bc is a handy but fullfeatured calculator. Even includes a programming language! Use the l option to have floating point support.
python as a calculator
dateR e tu rn s th e c u rre n t d ate . U s e fu l i n s cri p ts
to re co rd w h e n co m m an d s s tarte d o r
co m p le te d .
12/05/12
127
The Unix and GNU/Linux command line
System Administration Basics
12/05/12
128
File ownership
Parti cu larly u s e fu l i n (e m b e d d e d ) sys te m
d e ve lo p m e n t
w h e n yo u cre ate fi le s fo r an o th e r s ys te m .
chown R vuhung /home/linux/src (R: recursive)M ak e s u s e r vuhung th e n e w o w n e r o f all th e fi le s i n
/home/linux/src.
chgrp R core /home/vuhungM ak e s core th e n e w g ro u p o f e ve ryth i n g i n
/home/vuhung.
chown R vuhung:core /FileServer/Backup/chown can b e u s e d to ch an g e th e o w n e r an d g ro u p
at th e sam e tim e .
12/05/12
129
Shutting down
shutdown h +5 (h: halt)S h u ts th e sys te m d o w n i n 5 m i n u te s .
U s e rs g e t a w arn i n g i n th e i r co n s o le s .
shutdown h now (0)
shutdown r now (r: re b o o t)
init 0Another way to shutdown(init is used internally by shutdown).
init 6Another way to reboot.
[Ctrl][Alt][Del]Also works on GNU/Linux (at least on PCs!).
12/05/12
130
Network setup (1)
/sbin/ifconfig a; echo $PATHPrin ts d e tai ls ab o u t all th e n e tw o rk i n te rface s
avai lab le o n yo u r sys te m .
ifconfig eth0L i s ts d e tai ls ab o u t th e eth0 i n te rface
ifconfig eth0 192.168.52.100Ass ig n s th e 192.168.52.100 I P ad d re s s
to eth0 (1 I P ad d re s s p e r i n te rface ).
ifconfig eth0 downS h u ts d o w n th e eth0 i n te rface
(fre e s i ts I P ad d re s s ).
12/05/12
131
Network setup (2)
route add default gw 192.168.52.3S e ts th e d e fau lt ro u te fo r p ack e ts o u ts i d e th e
lo cal n e tw o rk . T h e g ate w ay (h e re
192.168.52.3) i s re s p o n s i b le fo r s e n d i n g th e m
to th e n e x t g ate w ay, e tc ., u n ti l th e fi n al
d e s ti n ati o n .
routeL i s ts th e e x i s ti n g ro u te s
route del defaultroute del <IP>D e le te s th e g ive n ro u te
U s e fu l to re d e fi n e a n e w ro u te .
12/05/12
132
Network testingping freshmeat.net (external)
ping 192.168.52.16 (internal)ping 192.168.51.1 (semiinternal)T ri e s to s e n d p ack e ts to th e g ive n m ach i n e an d g e t
ack n o w le d g m e n t p ack e ts i n re tu rn .
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.64 bytes from 192.168.51.1: icmp_seq=0 ttl=150 time=2.51 ms64 bytes from 192.168.51.1: icmp_seq=1 ttl=150 time=3.16 ms64 bytes from 192.168.51.1: icmp_seq=2 ttl=150 time=2.71 ms64 bytes from 192.168.51.1: icmp_seq=3 ttl=150 time=2.67 ms
W h e n yo u can p i n g yo u r g ate w ay, yo u r n e tw o rk
i n te rface w o rk s fi n e .
W h e n yo u can p i n g an e x te rn al I P ad d re s s , yo u r
n e tw o rk s e tti n g s are co rre ct!
12/05/12
133
Network setup summary
O n ly fo r s im p le cas e s w i th 1 i n te rface , n o d h cp
s e rve r...
C o n n e c t to th e n e tw o rk (cab le , w ire le s s card o r
d e vi c e ...)
I d e n ti fy yo u r n e tw o rk i n te rface :
ifconfig a
Ass ig n an I P ad d re s s to yo u r i n te rface
(as s u m in g eth0)
ifconfig eth0 192.168.52.100 (e xam p le )
Ad d a ro u te to yo u r g ate w ay (as s u m in g
192.168.52.3) fo r p ack e ts o u ts i d e th e n e tw o rk :
route add default gw 192.168.52.3
12/05/12
134
Name resolution
Yo u r p ro g ram s n e e d to k n o w w h at I P ad d re s s
co rre s p o n d s to a g ive n h o s t n am e (s u ch as
kernel.org)
D o m ain N am e S e rve rs (D N S ) tak e care o f th i s .
Yo u ju s t h ave to s p e c i fy th e I P ad d re s s o f 1 o r
m o re D N S s e rve rs i n yo u r /etc/resolv.conf
fi le :
nameserver 192.168.52.3 (Internal DNS server)nameserver 8.8.8.8 (Google Open DNS)
nameserver 8.8.4.4 (Google Open DNS)
nameserver 203.162.4.190 (Viettel)
T h e c h an g e s tak e s e ffe c t im m e d iate ly!
12/05/12
135
Creating filesystems
E xam p le s
mkfs.ext2 /dev/sda1F orm ats yo u r U S B k e y (/dev/sda1: 1
s t p arti ti o n raw
d ata) i n ext2 fo rm at.
mkfs.ext2 F disk.imgF orm ats a d i s k im ag e fi le i n e x t2 fo rm at
mkfs.vfat v F 32 /dev/sda1 (v: ve rb o s e )
F orm ats yo u r U S B k e y b ack to F AT 32 fo rm at.
mkfs.vfat v F 32 disk.imgF orm ats a d i s k im ag e fi le i n F AT 32 fo rm at.
B lan k d i s k im ag e s can b e cre ate d as i n th e b e lo w
e xam p le :
dd if=/dev/zero of=disk.img bs=1024 count=65536
12/05/12
136
Mounting devices (1)
T o m ak e fi le sys te m s o n an y d e vi c e
(i n te rn al o r e x te rn al s to rag e )
vi s i b le o n yo u r sys te m , yo u h ave to m ount th e m .
T h e fi rs t tim e , cre ate a m o u n t p o i n t i n yo u r s ys te m :
mkdir /mnt/usbdisk (e x am p le )
N o w , m o u n t i t:
mount t vfat /dev/sda1 /mnt/usbdisk/dev/sda1: physical devicet: specifies the filesystem (format) type(ext2, ext3, vfat, reiserfs, iso9660...)
raw data(file descriptors
and file contents)
mount
filestructure
12/05/12
137
Mounting devices (2)L o ts o f mount o p ti o n s are avai lab le , i n p arti cu lar to
ch o o s e p e rm i s s i o n s o r th e fi le o w n e r an d g ro u p ... S e e
th e mount m an u al p ag e fo r d e tai ls .
M o u n t o p ti o n s fo r e ach d e vi c e can b e s to re d i n th e
/etc/fstab fi le .
T h an k s to th i s fi le , yo u ju s t n e e d to s tate th e m o u n t
p o i n t:
# /etc/fstab: static file system information.# <file system> <mount point> <type> <options> <dump> <pass>proc /proc proc defaults
0 0/dev/hda3 / ext3 defaults,errors=remountro 0 1/dev/hda4 /home ext3 defaults
0 2/dev/hda2 /root2 ext3 defaults
0 2/dev/hda1 none swap sw
0 0/dev/hdc /media/cdrom0 udf,iso9660 user,noauto
0 0
mount e xam p le s w i th /etc/fstab:
mount /procmount /media/cdrom0
Device list: /proc/partitions
12/05/12
138
Mounting devices (3)
Yo u can als o m o u n t a fi le sys te m im ag e
s to re d i n a re g u lar fi le (loop d e vice s)
U s e fu l to d e ve lo p fi le sys te m s fo r an o th e r m ach i n e
U s e fu l to acce s s th e co n te n ts o f an I S O c d ro m
im ag e w i th o u t h avi n g to b u rn i t.
U s e fu l to c re ate a L i n u x p arti ti o n o n a h ard d i s k
w i th o n ly W in d o w s p arti ti o n s
cp /dev/sda1 usbkey.imgmount o loop t vfat usbkey.img /mnt/usbdisk
12/05/12
139
Listing mounted filesystems
Ju s t u s e th e mount co m m an d w i th n o arg u m e n t:
/dev/hda6 on / type ext3 (rw,noatime)none on /proc type proc (rw,noatime)none on /sys type sysfs (rw)none on /dev/pts type devpts (rw,gid=5,mode=620)usbfs on /proc/bus/usb type usbfs (rw)/dev/hda4 on /data type ext3 (rw,noatime)none on /dev/shm type tmpfs (rw)/dev/hda1 on /win type vfat (rw,uid=501,gid=501)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
O r d i s p lay th e /etc/mtab fi le
(sam e re s u lt, u p d ate d b y mount an d umount e ach
tim e th e y are ru n )
12/05/12
140
Unmounting devices
umount /mnt/usbdiskC o m m i ts all p e n d i n g w ri te s an d u n m o u n ts th e g ive n
d e vi c e , w h i c h can th e n b e re m o ve d i n a safe w ay.
T o b e ab le to u n m o u n t a d e vi c e , yo u h ave to c lo s e
all th e o p e n fi le s i n i t:
C lo s e ap p li cati o n s o p e n i n g d ata i n th e m o u n te d
p arti ti o n
M ak e s u re th at n o n e o f yo u r s h e lls h ave a w o rk i n g
d i re c to ry i n th i s m o u n t p o in t.
Yo u can ru n th e lsof c o m m an d (li s t o p e n fi le s ) to
vi e w w h i c h p ro ce s s e s s ti ll h ave o p e n fi le s i n th e
m o u n te d p arti ti o n .
12/05/12
141
Beware of the dark side of root
root u s e r p rivi le g e s are o n ly n e e d e d fo r ve ry
s p e c i fi c task s w i th s e cu ri ty ri s k s : m o u n ti n g ,
cre ati n g d e vi c e fi le s , lo ad i n g d rive rs , s tarti n g
n e tw o rk i n g ,
ch an g in g fi le o w n e rs h i p , p ack ag e u p g rad e s ...
E ve n i f yo u h ave th e root p as sw o rd , yo u r re g u lar
acco u n t s h o u ld b e s u ffi c i e n t fo r 99.9 % o f yo u r
task s
(u n le s s yo u are a s ys te m ad m in i s trato r).
I n a trai n i n g s e s s i o n , i t i s acc e p tab le to u s e root.
I n re al li fe , yo u m ay n o t e ve n h ave acce s s to th i s
acco u n t, o r p u t yo u r s ys te m s an d d ata at ri s k i f yo u
d o .
12/05/12
142
Using the root account
I n cas e you re ally w an t to u s e root...
I f yo u h ave th e root p as sw o rd :
su (s w itch u s e r)
I n m o d e rn d i s tri b u ti o n s , th e sudo co m m an d g ive s yo u
acce s s to s o m e root p ri vi le g e s w i th yo u r o w n u s e r
p as sw o rd .
E xam p le : sudo mount /dev/hda4 /home
D i s tri b u ti o n s li k e D e b ian o r U b u n tu b y d e fau lt d o n 't
allo w th e root u s e r to u s e g rap h i cal ap p li cati o n s (m o re
com p le x an d th u s m o re vu ln e rab le ).
I f yo u re ally w an t to s tart a g rap h i cal ap p li cati o n w i th
root, typ e :
As root: export DISPLAY=:0 (i .e . u s e th e fi rs t d i s p lay)
As yo u r re g u lar u s e r: xhost + (i .e . allo w o th e r u s e rs )
12/05/12
143
The Unix and GNU/Linux command line
GNU/Linux: Distribution Packages
12/05/12
144
How to find packages
U b u n tu , D e b ian
ap t-cach e s e arch m e ld
h ttp ://w w w .d e b ian .o rg /d i s tri b /p ack ag e s
S e arch b y p ack ag e o r fi le n am e
rp m fi n d : h ttp ://rp m fi n d .n e t/
L o ts o f R PM p ack ag e s fo r R e d H at,
M an d riva, S u s e ...
R e d h at varian ts
yu m s e arch “k e yw o rd ”
12/05/12
145
Identifying packages
W h at p ack ag e d o e s a fi le b e lo n g to ?
U se fu l to g e t m o re i n fo rm ati o n , g e t th e co d e ,
fi n d n e w e r ve rs i o n s , re p o rt i s s u e s ...
D i s tri b u ti o n w i th R PM p ack ag e s :
(R e d H at, F e d o ra, M an d riva, S u s e ...)
> rpm qf /bin/lscoreutils5.2.17
D e b ian , U b u n tu :
> dpkg S /bin/lsfileutils: /bin/ls
12/05/12
146
Information about packages
Acce s s p ack ag e d e s cri p ti o n , ve rs i o n n u m b e r,
s o u rc e s , e tc .
R PM b ase d d i s tri b u ti o n s :
rpm qi <packagename>
D e b ian :
dpkg s <packagename>
12/05/12
147
The Unix and GNU/Linux command line
Going Further
12/05/12
148
Command help
S o m e U n i x c o m m an d s an d m o s t G N U / L i n u x
co m m an d s o ffe r at le as t o n e h e lp arg u m e n t:
h( i s m o s tly u s e d to i n tro d u ce 1 -ch aracte r
o p ti o n s )
help( i s alw ays u s e d to i n tro d u c e th e
co rre s p o n d i n g “lo n g ” o p ti o n n am e , w h i c h
m ak e s s c ri p ts e as i e r to u n d e rs tan d )
Yo u als o o fte n g e t a s h o rt s u m m ary o f o p ti o n s
w h e n yo u i n p u t an i n vali d arg u m e n t.
12/05/12
149
Manual pages
man <keyword>D i s p lays o n e o r s e ve ral m an u al p ag e s fo r
<keyword>
man man
M o st avai lab le m an u al p ag e s are ab o u t U n i x
c o m m an d s , b u t s o m e are als o ab o u t C fu n c ti o n s ,
h e ad e rs o r d ata s tru ctu re s , o r e ve n ab o u t s ys te m
co n fi g u rati o n fi le s !
man stdio.h
man fstab (for /etc/fstab)
M an u al p ag e fi le s are lo o k e d fo r i n th e d i re cto ri e s
s p e c i fi e d b y th e MANPATH e n vi ro n m e n t variab le .
12/05/12
150
Info pages
I n G N U , m an p ag e s are b e i n g re p lace d b y i n fo
p ag e s . S o m e m an u al p ag e s e ve n te ll to re fe r to
i n fo p ag e s i n s te ad .
info <command>
info fe atu re s :
D ocu m e n tati o n s tru ctu re d i n s e c ti o n s
(“n o d e s ”) an d s u b s e c ti o n s (“s u b n o d e s ”)
Po s s i b i li ty to n avig ate i n th i s s tru ctu re : to p ,
n e x t, p re v, u p
I n fo p ag e s g e n e rate d fro m th e sam e te x i n fo
s o u rce as th e H T M L d o cu m e n tati o n p ag e s
12/05/12
151
Searching the Internet for resources (1)
I n ve s ti g ati n g i s s u e s
M o s t fo ru m s an d m ai li n g li s t arch ive s are
p u b li c , an d are i n d e x e d o n a ve ry fre q u e n t
b as i s b y G o o g le .
I f yo u i n ve s ti g ate an e rro r m e s sag e , co p y i t
ve rb atim i n th e s e arch fo rm , e n c lo s e d i n d o u b le
q u o te s (“e rro r m e s sag e ”). L o ts o f c h an ce s th at
s o m e b o d y e ls e alre ad y face d th e sam e i s s u e .
D o n 't fo rg e t to u s e G o o g le G ro u p s :
h ttp ://g ro u p s .g o o g le .c o m /
T h i s s i te i n d e x e s m o re th an 2 0 ye ars o f
n e w sg ro u p s m e s sag e s .
12/05/12
152
Searching the Internet for resources (2)
L o o k i n g fo r d o cu m e n tati o n
L o o k fo r <tool> o r <tool> page to fi n d th e to o l o r
p ro je ct h o m e p ag e an d th e n fi n d th e late s t
d o cu m e n tati o n re s o u rce s .
L o o k fo r <tool> documentation o r <tool> manual
i n yo u r favo ri te s e arch e n g i n e .
L o o k i n g fo r g e n e ri c te ch n i cal
i n fo rm ati o n
W ik iPe d ia: h ttp ://w ik i p e d ia.o rg
L o ts o f u s e fu l d e fi n i ti o n s i n co m p u te r s c i e n c e . A
re al e n cyclo p e d ia! O p e n to an yo n e 's c o n tri b u ti o n s .
12/05/12
153
Local Communities
Vie tlu g
U b u n tu -vn
H an o iL u g
S aig o n L u g
H u e L u g
T h aiN g u ye n L U G
D an an g L u g ?
12/05/12
154
Related documents
h ttp ://fre e -e le c tro n s .co m /trai n i n g
I n tro d u cti o n to U n i x an d G N U /L in u x
E m b e d d e d L i n u x k e rn e l an d d rive r d e ve lo p m e n t
F re e S o ftw are to o ls fo r e m b e d d e d L in u x sys te m s
Au d i o i n e m b e d d e d L i n u x sys te m s
M u ltim e d ia i n e m b e d d e d L i n u x sys te m s
h ttp ://fre e -e le c tro n s .co m /arti c le s
Ad van tag e s o f F re e S o ftw are i n e m b e d d e d sys te m s
E m b e d d e d L i n u x o p tim i zati o n s
E m b e d d e d L i n u x fro m S cratch ... i n 40 m i n !
L i n u x U S B d rive rs
R e al-tim e i n e m b e d d e d L in u x sys te m s
I n tro d u cti o n to u C li n u x
L i n u x o n T I O M AP p roce s s o rs
F re e S o ftw are d e ve lo p m e n t to o ls
Java i n e m b e d d e d L i n u x sys te m s
I n tro d u cti o n to G N U /L in u x an d F re e S o ftw are
L i n u x an d e co lo g y
W h at's n e w i n L i n u x 2 .6?
H o w to p o rt L i n u x o n a n e w PD A
M o re th an 1 5 00 p ag e s u n d e r fre e li c e n s e s .
12/05/12
155
LAMP BasicC e n tO S
yu m i n s tall h ttp d m ysq l-s e rve r p h p
yu m i n s tall h ttp d -d e ve l m ysq l-d e ve l p h p -d e ve l
U b u n tu
ap t-g e t i n s tall ap ach e 2 p h p 5 -m ysq l li b ap ach e 2 -
m o d -p h p 5 m ysq l-s e rve r
su d o /e tc/in i t.d /ap ach e 2 re s tart
su d o /e tc/in i t.d /h ttp d s tart|re s tart|s to p
s u d o /e tc/in i t.d /m ysq ld s tart|re s tart|s to p
Ad d i ti o n p ack ag e s : p h p -co m m o n p h p -g d p h p -
m cryp t p h p -p e ar p h p -p e c l-m e m cach e p h p -
m h ash p h p -m ysq l p h p -xm l
12/05/12
156
Further Readings
h ttp ://arti c le s .s li c e h o s t.c o m /2 008/2 /6/
ce n to s -i n s talli n g -ap ach e -an d -p h p 5
h ttp ://tld p .o rg /L D P/sag /h tm l/i n d e x .h t
m l
An y b o o k s o n L AM P
Q&A
12/05/12
158
Rights to copyA t tr ibu t ion – S hareA l ike 2 .5You are free
to co p y, d i s tri b u te , d i s p lay, an d p e rfo rm th e
w o rk
to m ak e d e rivative w o rk s
to m ak e com m e rc ial u s e o f th e w o rk
U nder the fol lowing c ondi t ionsA t t r ibu t ion . You m u s t g i ve th e
o ri g i n al au th or cre d i t.
S hare A l ike . I f yo u alte r, tran sform ,
o r b u i ld u p on th i s w o rk , yo u m ay
d i s tri b u te th e re su lti n g w ork o n ly
u n d e r a li c e n se i d e n ti cal to th i s o n e .
F or an y re u se o r d i s tri b u ti o n , yo u m u st m ak e
c le ar to o th e rs th e li c e n se te rm s o f th i s
w o rk .
An y o f th e se co n d i ti o n s can b e w aive d i f you
g e t p e rm i s s i o n from th e cop yrig h t h o ld e r.
You r fai r u se and other r ights are in no way affec ted by the above.
L i c e n s e te x t:
h ttp ://cre ative co m m o n s .o rg /li c e n s e s/b y-sa/2 .5 /le g alco d e
12/05/12
159
Copyright© Copyright 2004-2007
Free Electronsfeedback@free-electrons.com
Document sources, updates and translations: http://free-electrons.com/training/intro_unix_linuxCorrections, suggestions, contributions and
translations are welcome!
top related