Вы находитесь на странице: 1из 384

32.973.2-018.

2
004.451
64

.
64 Linux. .: , 2016. 384 .: . (
).
ISBN 978-5-496-01952-1
, , . -
Linux, , ,
Linux.
Linux .

, , .

12+ ( 29 2010 . 436-.)

32.973.2-018.2
004.451

OReilly. .

.

, , , -
. , ,

, .

ISBN 978-1593275679 . Copyright 2015 by Brian Ward


Copyright 2015 No Starch Press, Inc.
ISBN 978-5-496-01952-1 , 2016
,
, 2016
, 2016

.............................................................................................18
.........................................................................................21
....................................................................................... 22
1. ........................................................................23
2. ....................33
3. ................................................................................68
4. ................................................89
5. Linux.............................. 118
6. .............. 136
7. : ,
, ............... 174
8.
................................................................. 198
9. .................... 217
10. .................................... 259
11. ..................................... 280
12. ................................................ 302
13. ........................................ 319
14. Linux............................ 330
15. ............................................ 343
16.
C.................................................... 364
17. ..................................................... 379

.............................................................................................18
........................................................................18
...............................................................................18
.......................................................................................18
................................................................................19
..............................................................................19
..................................................................20
.....................................................................20
.........................................................................................21
....................................................................................... 22
1. ........................................................................23
1.1. Linux...................24
1.2. : ........................................25
1.3....................................................................................................26
1.3.1. .........................................................27
1.3.2. ..............................................................28
1.3.3. ..................................28
1.3.4. .............................................29
1.4. ................................................................30
1.5......................................................................................31
1.6. .............................................................................32
2. ....................33
2.1. Bourne shell: /bin/sh............................................................33
2.2. ....................................................................34
2.2.1. .......................................................................34
2.2.2. cat............................................................................35
2.2.3. ................................35
2.3. .............................................................................36
2.3.1. ls..............................................................................36
2.3.2. cp.............................................................................37
2.3.3. mv............................................................................37
7

2.3.4. touch........................................................................37
2.3.5. rm............................................................................37
2.3.6. echo.........................................................................38
2.4. ................................................................38
2.4.1. cd.............................................................................38
2.4.2. mkdir........................................................................39
2.4.3. rmdir.........................................................................39
2.4.4. ( )........39
2.5. .................................................................40
2.5.1. grep..........................................................................40
2.5.2. less...........................................................................41
2.5.3. pwd..........................................................................42
2.5.4. diff............................................................................42
2.5.5. file............................................................................42
2.5.6. find locate..............................................................42
2.5.7. head tail.................................................................43
2.5.8. sort...........................................................................43
2.6. ................................................43
2.7. ...................................................................................44
2.8. ....................................................44
2.9. .................................................................................45
2.10. ......................................................................45
2.11. ...................................................46
2.12. .......................................................................47
2.13. ...................................................48
2.14. ...................................................49
2.14.1. .............................................................50
2.14.2. ..................................51
2.15. ..................................................51
2.15.1. Unix................................51
2.15.2. ......................................................................52
2.16. ................................53
2.16.1. ps.........................................................54
2.16.2. .........................................................54
2.16.3. .........................................................55
2.16.4. ...............................................................55
2.17. ......................................................56
2.17.1. .......................................................57
2.17.2. ..........................................................59
2.17.3. ...........................................59
2.18. ......................................................60
2.18.1. gzip.........................................................................60
8

2.18.2. tar..........................................................................60
2.18.3. (.tar.gz)........................................................62
2.18.4. zcat.........................................................................62
2.18.5. ........................................................62
2.19. Linux..............................63
2.19.1. ..............................................65
2.19.2. /usr..........................................................................65
2.19.3. ..........................................................66
2.20. superuser.............................66
2.20.1. sudo.......................................................................66
2.20.2. /etc/sudoers.................................................................66
2.21. ...........................................................................67

3. ................................................................................68
3.1. ................................................................................68
3.2. sysfs...........................................................................70
3.3. dd ....................................................................71
3.4. .......................................................................72
3.4.1. : /dev/sd*.........................................................72
3.4.2. CD DVD: /dev/sr*...................................................73
3.4.3. PATA: /dev/hd*................................................74
3.4.4.: /dev/tty*, /dev/pts/* /dev/tty.............................74
3.4.5. : /dev/ttyS*......................................75
3.4.6. : /dev/lp0 /dev/lp1...............................75
3.4.7.: /dev/snd/*, /dev/dsp, /dev/audio ....76
3.4.8. ....................................................76
3.5. udev...................................................................77
3.5.1. devtmpfs....................................................77
3.5.2. udevd.....................................78
3.5.3. udevadm...................................................................80
3.5.4. ........................................................81
3.6.: SCSI Linux...........................................82
3.6.1.USB- SCSI............................................85
3.6.2. SCSI ATA..........................................................86
3.6.3. SCSI.................................................87
3.6.4. ..............87
4. ................................................89
4.1. ..............................................................90
4.1.1. ...................................................92
4.1.2. ...................................................93
4.1.3. ......................................................94
4.1.4. ( SSD).................................96
9

4.2. .............................................................................96
4.2.1. ...........................................................97
4.2.2. ..................................................98
4.2.3. ..........................................99
4.2.4. UUID........................................................ 100
4.2.5. , ....... 101
4.2.6. ....................... 102
4.2.7. ..................................... 103
4.2.8. /etc/fstab................................... 104
4.2.9. /etc/fstab.......................................... 105
4.2.10. ............................................. 106
4.2.11. .................... 107
4.2.12. ..................... 109
4.3. ............................................................................. 110
4.3.1.
........................................................................................ 110
4.3.2. ............. 111
4.3.3. ........................... 111
4.4. : .................. 112
4.5. ......................................... 113
4.5.1. inode................................. 115
4.5.2.
................................................................................. 116
4.5.3. ................................................. 117
5. Linux.............................. 118
5.1. .................................................................... 118
5.2. ...................................... 120
5.3. ............................................................................... 120
5.4. .......................................................................... 121
5.4.1. .................................................. 122
5.4.2. ....................................... 123
5.5. GRUB........................................... 123
5.5.1.
GRUB................................................................. 126
5.5.2. GRUB........................................... 128
5.5.3. GRUB.................................................. 130
5.6. UEFI............................................ 132
5.7. ...... 132
5.8. .............................................................. 133
5.8.1. MBR................................. 133
5.8.2. UEFI............................ 133
5.8.3. GRUB............................................... 134
10

6. .............. 136
6.1. init............................................................... 136
6.2. SystemV.................................................... 138
6.3. init............................................................ 138
6.4. systemd.............................................................................. 139
6.4.1. ........................................................ 139
6.4.2. systemd.............................................. 140
6.4.3. systemd........................................... 142
6.4.4. systemd....................................................... 145
6.4.5. systemd............................... 147
6.4.6.
systemd......................................................................... 148
6.4.7.
systemd......................................................................... 149
6.4.8. systemd SystemV.... 154
6.4.9., systemd.......................................... 154
6.5. Upstart................................................................................ 155
6.5.1. Upstart......................... 155
6.5.2. Upstart...................................................... 157
6.5.3. Upstart............................................ 159
6.5.4. Upstart............................................... 163
6.5.5. Upstart.................................................... 163
6.5.6. Upstart
SystemV................................................................. 164
6.6. System V init....................................................................... 165
6.6.1. System V init:
.......................................................................................... 167
6.6.2. System V init.................................... 168
6.6.3. run-parts.................................................................. 169
6.6.4. System V init....................................... 169
6.7. ........................................................................ 170
6.8. ........................... 171
6.9. ...... 173
7. : ,
, ............... 174
7.1. /etc.................................................................... 174
7.2. ............................................................................ 175
7.2.1. ........................................................ 175
7.2.2. ........................................................... 176
7.3. .................................................. 178
7.3.1. /etc/passwd.................................................................. 178
7.3.2. ........................................................... 179
11

7.3.3. /etc/shadow.................................................................. 180


7.3.4. .............................. 180
7.3.5. ................................................................ 180
7.4. getty login........................................................................ 182
7.5. ........................................................................... 182
7.5.1. .................. 183
7.5.2. ...................................................................... 184
7.6. cron.......... 184
7.6.1. crontab..................................................... 185
7.6.2. crontab..................................................... 186
7.6.3. cron........................................................... 186
7.7. at............ 187
7.8.
............................................................................ 187
7.9. ............................ 190
7.10. PAM.................................................................................. 191
7.10.1. PAM............................................................. 192
7.10.2. PAM.............................................. 196
7.10.3. PAM ....................................................... 196
7.11. ......................................................................... 197

8.
................................................................. 198
8.1. .................................................................. 198
8.2. lsof............................. 199
8.2.1. lsof............................. 199
8.2.2. lsof................................................. 200
8.3. ................. 201
8.3.1. strace...................................................................... 201
8.3.2. ltrace...................................................................... 202
8.4............................................................................................... 203
8.4.1. ........................... 203
8.4.2. ................................................................. 204
8.5. .................................................. 205
8.6. .................................................. 205
8.7. .................................................... 206
8.8. .............................................................. 207
8.8.1. uptime............................................ 207
8.8.2. .................................................. 208
8.9.............................................................................................. 209
8.9.1. ............................................................. 209
8.9.2. - ......................................... 209
12

8.10.
vmstat..................................................................... 211
8.11. /.......................................................... 213
8.11.1. iostat............................................ 213
8.11.2. /
iotop............................................... 214
8.12. pidstat..................... 215
8.13. .................................................................... 216

9. .................... 217
9.1. ................................................................. 217
9.2. ................................................................................ 218
9.3.-............................................................................. 220
9.3.1. IP- ......................................... 221
9.3.2................................................................................. 222
9.3.3. CIDR................ 222
9.4. ....................................... 223
9.5. , ICMP
DNS.......................................................................................... 225
9.5.1. ping........................................................................ 225
9.5.2. traceroute............................................................... 226
9.5.3. DNS ............................................................... 226
9.6. Ethernet................................................. 227
9.7. ............................................... 228
9.8. ........................ 228
9.9. , .............. 230
9.10.,
........................................ 230
9.11. ................................................. 231
9.11.1. NetworkManager.................................... 232
9.11.2. NetworkManager
.................................................................. 232
9.11.3. NetworkManager ........................ 233
9.12. ................................................................. 235
9.12.1. /etc/hosts................................................................... 235
9.12.2. resolv.conf.................................................................. 236
9.12.3. DNS ........... 236
9.12.4. /etc/nsswitch.conf....................................................... 237
9.13. .............................................................................. 237
9.14. : TCP, UDP ................... 238
9.14.1. TCP .................................................... 238
9.14.2. TCP-............................................ 239
13

9.14.3. /etc/services.................................... 240


9.14.4. TCP........................................... 240
9.14.5. UDP..................................................................... 241
9.15. ........................................ 243
9.16. DHCP............................................................. 243
9.16.1. DHCP Linux........................................................... 244
9.16.2. DHCP Linux......................................................... 244
9.17. Linux ................................... 244
9.18. .................................................................................. 246
9.19. ( IP-).............. 247
9.20. Linux................................................................. 248
9.21................................................................................... 249
9.21.1. Linux: ............................... 250
9.21.2. ............................... 251
9.21.3. .............................................. 253
9.22. Ethernet, IP ARP.................................................. 254
9.23. Ethernet............................................................ 256
9.23.1. iw.......................................................................... 257
9.23.2. ...................................... 258
9.24............................................................................................ 258
10. .................................... 259
10.1. .......................................................... 259
10.2. ............................................................................ 262
10.3. (SSH)........................................................... 262
10.3.1. SSHD....................................................................... 263
10.3.2. SSH......................................................................... 266
10.4. inetd xinetd...................................................................... 267
10.5. ............................................................... 268
10.5.1. lsof....................................................................... 269
10.5.2. tcpdump................................................................ 270
10.5.3. netcat................................................................... 272
10.5.4. ......................................................... 272
10.6. (RPC)................................................... 273
10.7. ..................................................................... 274
10.7.1. ......................................................... 275
10.7.2.-, ..................... 276
10.8. ......................................................................... 276
10.9.: ............................. 277
10.10. Unix...................................................................... 278
10.10.1. .................................... 278
10.10.2. Unix............................... 279
14

11. ..................................... 280


11.1. ........................................................... 280
11.2. ....................................................................... 281
11.2.1............................................................................ 282
11.2.2. ........................................................... 282
11.2.3. ............................................................... 283
11.2.4. ........................... 283
11.3. .............................................................. 284
11.3.1. : $1, $2.................................... 284
11.3.2. : $#................................................ 285
11.3.3. : $@............................................................. 285
11.3.4. : $0............................................................... 285
11.3.5. : $$.............................................. 286
11.3.6. : $?................................................................... 286
11.4. ................................................................................... 286
11.5. ....................................................................... 287
11.5.1. ................................. 288
11.5.2. ......... 288
11.5.3. elif............................................................ 288
11.5.4. && ||......................................... 289
11.5.5. ............................................................... 289
11.5.6. case............. 292
11.6.............................................................................................. 293
11.6.1. for.............................................................................. 293
11.6.2. while.......................................................................... 293
11.7. ........................................................................ 294
11.8. ..................................................... 295
11.9. heredoc.......................................................................... 296
11.10. ..................................... 296
11.10.1. basename............................................................ 297
11.10.2. awk..................................................................... 297
11.10.3. sed...................................................................... 297
11.10.4. xargs................................................................... 298
11.10.5. expr.................................................................... 299
11.10.6. exec.................................................................... 299
11.11.................................................................................. 300
11.12. .......................................... 300
11.13. .................................................. 301
11.14. () ................................. 301
12. ................................................ 302
12.1. ..................................................................... 302
12.2. rsync................................................................................. 302
15

12.2.1. rsync........................................................ 303


12.2.2. ........................ 304
12.2.3. ......................... 305
12.2.4. ........................................ 306
12.2.5. ,
..................................................................... 307
12.2.6................................................................................ 308
12.2.7. ......................... 308
12.2.8. .................................... 308
12.2.9. , rsync................ 308
12.3. ............................... 309
12.4. Samba........ 309
12.4.1. Samba..................................... 310
12.4.2. ................................................ 311
12.4.3................................................................................ 311
12.4.4. ................................................................... 313
12.4.5. .................................... 313
12.4.6. ..... 313
12.4.7. ............................................................ 314
12.4.8. ................................ 314
12.4.9. Samba........................................... 315
12.4.10. ................................ 315
12.5. NFS................................................................................... 316
12.6.
................................................................................................... 317
13. ........................................ 319
13.1. ................................... 319
13.2. ....................................................... 320
13.3. ................................................. 320
13.3.1. ................................................................. 320
13.3.2. ............................................ 321
13.3.3....................................................................... 322
13.3.4......................................................................... 322
13.3.5. ............................................................ 323
13.4. ............................... 323
13.4.1. bash.................................................................... 323
13.4.2. tcsh..................................................................... 326
13.5. ................................... 327
13.5.1. ............................. 327
13.5.2.............................................................................. 328
13.5.3. PAGER............................................................. 328
13.6. ................................................ 328
13.7. , ................................... 329
16

14. Linux............................ 330


14.1. ............................................................ 330
14.1.1. ................................................................ 331
14.1.2.................................................................. 331
14.1.3. ................................................. 332
14.1.4........................................................................ 332
14.2. X Window....................................................... 332
14.2.1. ........................................................... 333
14.2.2. .............................................................. 333
14.3. X-............................................................... 334
14.3.1.X-........................................................................... 334
14.3.2. X- .................... 336
14.4. X Window............................................................ 338
14.5. D-Bus.................................................................................... 339
14.5.1. ................................... 339
14.5.2. D-Bus............................... 340
14.6............................................................................................. 340
14.6.1. CUPS..................................................................... 341
14.6.2. ...................... 341
14.7. , .................................. 342
15. ............................................ 343
15.1.C................................................................................. 343
15.1.1. ............................ 344
15.1.2. (Include) .......................... 345
15.1.3. ............................................... 347
15.1.4. ................................. 348
15.2. make.................................................................................. 352
15.2.1. Makefile......................................................... 353
15.2.2. ........................................................... 354
15.2.3. ...................................... 354
15.2.4. ........................... 354
15.2.5. ......................... 355
15.2.6. .................. 356
15.2.7. .................................................................... 357
15.2.8. Makefile.................................................. 357
15.3....................................................................................... 358
15.4. Lex Yacc.................................................................. 359
15.5. ............................................................................ 360
15.5.1.Python................................................................................ 361
15.5.2.Perl..................................................................................... 361
15.5.3. ..................................................... 361
15.6.Java................................................................................................ 362
15.7. : .................. 363
17

16.
C.................................................... 364
16.1. ............................. 365
16.2. C............................ 365
16.3. GNU Autoconf..................................................................... 366
16.3.1. Autoconf....................................... 367
16.3.2.
.......................................................................................... 368
16.3.3. configure............................................ 369
16.3.4. ...................................................... 369
16.3.5. Autoconf....................................................... 371
16.3.6. Autoconf..................................... 371
16.3.7. pkg-config.............................................................. 371
16.4. ........................................................................ 373
16.5. ............................................................... 374
16.6. .......................... 375
16.7. ......................................................................... 377

17. ..................................................... 379


17.1.- ............................................................. 379
17.2. ................................................................................... 380
17.3................................................................................. 381
17.4. ................... 381
17.5. ....................................................................... 382
17.6. ............................................................ 383

, , ,
,
.
Linux ,
,
. ,
, . .


Linux
. -
, Linux
, ,
. ,
, ,
, . ,
,
.
, , ? ,
, - ? .


, .
:
( ),
().
. ,
.


,
. ,
19

,
.
,
.
.
, ,
.
. ? ,
.
, ,
. .


Linux.
,
VirtualBox.
superuser (root),
.
, .
, , 2 .
, :
$ ls /
[some output]
, ;
, . $
.
#, superuser (
2).


Linux,
,
. ,
, ,
. ,
, ,
, .
( 2)
Linux,
. , ,
, 1.
20

-
. ,
: Debian (
Ubuntu) RHEL/Fedora/CentOS.
. , Android
OpenWRT, .



Linux.
, .
.

Linux .
: ,
, (,
),
Linux . , ,
, , ,
.
,
. ,
, .
,
. ,
, .
, ,
.
, ,
.
Linux Unix,
(PeterH.Salus) The Daemon, the Gnu, and the Penguin (Reed Media
Services, 2008) ,
.



. Linux
: Linux GNU/Linux (
GNU)?
.

, .
(James Duncan), . (Douglas N. Arnold), (Bill Fenner),
(Ken Hornstein), (Scott Dickson), (Dan
Ehrlich), (Felix Lee), (Scott Schwartz), .
(GregoryP.Smith), (Dan Sully), (Karol Jurado)
(Gina Steele).
(Jordi Gutierrez Hermoso) ;
. (Dominique
Poulain) (Donald Karon)
, (Hsinju Hsieh),
.

(Bill Pollock) (Laurel Chun).
(Serena Yang), (Alison Law) No Starch
Press, ,
.

,
sivchenko@minsk.piter.com ( , ).
!
http://www.piter.com
.
1

, Linux,
,
. , -
, , ,
, .
?

,
. , , , ,
, ,
, ,
. ,
, ( -
), (
).
, .
(, ),
, .
, .
, : ,
. . ,
. ,
, , ,
.

.
,
, .
, . ,
,
, ,
.
,
Linux.
24 1.

, ,
,
.

1.1.
Linux

, , .
, . , ,
( )
. , ..
; :
.
.
Linux . .1.1
, .
. ,
(CPU),
. , ,
.
,
. ,
.

.
, ,
, .

, , , -
, . , -
.

,
: ,
. , ,
. ,
,
. , ,
.
, , (
, )
. ,
. -
,
1.2. : 25

. , , , , ,
,
, .

.1.1. Linux


. ,
,
, , .
,
? , ,
. ,
.

1.2. :

, ,
.
.
.
26 1.

.

. :
, .
,
, , . ,
- . ,
, 0110, 0001 1011
.
,
, ,
. ,
. ,

.

, ,
- .

1.3.
, , .
,

. ,
, .
.
. ,
.
. :
,
.
.
(, ) . ,
.
.
.
.

Operating System Concepts ( -


), 9- , : (Abraham Silberschatz), -
. (PeterB.Galvin) (Greg Gagne) (Wiley, 2012) Modern Operating Systems (-
), 4- , : . (AndrewS.Tanenbaum)
(Herbert Bos) (Prentice Hall, 2014).
1.3. 27

1.3.1.
, ,
. ,
, . ,
.

. ,
.
, : , ,
, .
.
,
.
, ;
;
.. , -
, .

(, ,
).
, ,
(
).
. , ,
, ,
. .
1. ( ) ,
,
.
2. ,
.
3. ,
(, /).
4. .
, , - .
5. , .
6. ,
.
7.
.
: ?
:
, .
28 1.

,
,
- . ,
,
( ,
).

1.3.2.
,
. ,
:
,
;
;
-
, ;
;

;
, ,
.
.
(MMU),
.

. ,
. ,
MMU ,
, ,
. ,
. ,

.

, , 8.

1.3.3.
. ,
, (,
)
1.3. 29

. ,
,
: , .

,
.

1.3.4.
,
. , ,

. , , ,
, .
fork() exec() ,
:
fork(). fork(),
;
exec(). exec(program),
program, .
init (6),
Linux fork(),
exec(), ,
. ,
, ls,
. ls ,
fork(), ,
exec(ls),
ls. .1.2
, ls.

.1.2.

. ,
.1.2, , ,
fork(). -
C. , C. ,
. , -
. , exec()
, , .
30 1.

,
.
. ,
,
. , ,
. , ,
(/dev/random),
.

, , -
. ,
.

1.4.
,
, .
( ) ,
.
(userland),
.
Linux
.
, .
, ,
( ). .1.3
,
Linux. (
), , ,
, . 1.3
, ,
, (
);
, ;
.
, , ,
. ,
, .
,
. ,
, .
1.3
. . ,
,
1.5. 31

.
syslog ,
.

.1.3.

,
- . , -
,
, .
.1.3.
, ,
.
.

1.5.
Linux
Unix. ,
. . ,
billyjoe.
,
( 7 ,
).
,
.
-, ,
32 1.

.
( ),
. ,
.
Linux
, , .
3,
root. ,
,
.
root superuser. ,
root, , root-. Unix
.

root . , -
, .
, root- ,
. , .
, root, -
, .

. ,

.

1.6.
, , Linux.
, .
.
.
,
Linux, .
, .
Linux, :
( , ..).
- .
2


Unix. , ,
, , (. 2.19),
. ,
.
Unix? , Linux?
, , , Linux Unix.
Unix , Linux,
Solaris, BSD ,
Unix.
, Linux, ,
,
, .
Linux , .

Linux The Linux


Command Line ( Linux) (No Starch Press, 2012), UNIX for the Impatient (UNIX
) (Addison-Wesley Professional, 1995) Learning the UNIX Operating System (-
UNIX), 5- (OReilly, 2001).

2.1. Bourne shell: /bin/sh


Unix.
, (, , ).
.
, Unix,
, ,
.
-
,
. MS-DOS,
.BAT.
, 11.
34 2.


, .
, , ,
, .
Unix,
Bourne shell (/bin/sh) ,
Bell Labs Unix.
Unix
Bourne shell. Linux Bourne shell
bash, 1 . bash
Linux, /bin/sh, ,
. bash.

, bash ,
Unix , -
. chsh -
.

2.2.
Linux
( ),
.
.

2.2.1.
, -
.
Gnome Unity Ubuntu.
.
,
($). Ubuntu : name@host:path$,
Fedora : [name@host path]$. Windows,
DOS;
Terminal OSX, , , Linux.
.
, $.
( ,
$) Enter:
$ echo Hello there.

1
, Bourne
born (). . .
2.2. 35

#.
superuser (root). , .

:
$ cat /etc/passwd
/etc/passwd,
. ,
7.

2.2.2. cat
cat Unix .
.
cat :
$ cat file1 file2 ...
cat file1, file2
- ( ...), .
, ()
, .

2.2.3.
cat, (I/O)
Unix. Unix /
.
.
. ,
, , .
, cat ( )
Enter. ,
cat . -,
Enter . cat
. cat
, Ctrl+D .
cat , .
, cat
, Linux,
, .
, cat.

Ctrl+D
( ).
Ctrl+C, .
36 2.

.
, . cat
.
cat , ,
.
stdin stdout.
cat: ,
stdin. .
( cat) stdout,
.
/,
. 2.14.1.

, .
, 2.14 ,
.

2.3.
Unix ,
,
. .

2.3.1. ls
ls - .
. ls -l,
() , ls F, .
,
, 2.17.
,
(3), (4), (5),
( 5 ):
$ ls -l
total 3616
-rw-r--r-- 1 juser users 3804 Apr 30 2011 abusive.c
-rw-r--r-- 1 juser users 4165 May 26 2010 battery.zip
-rw-r--r-- 1 juser users 131219 Oct 26 2012 beav_1.40-13.tar.gz
-rw-r--r-- 1 juser users 6255 May 30 2010 country.c
drwxr-xr-x 2 juser users 4096 Jul 17 20:00 cs335
-rwxr-xr-x 1 juser users 7108 Feb 2 2011 dhry
-rw-r--r-- 1 juser users 11309 Oct 20 2010 dhry.c
-rw-r--r-- 1 juser users 56 Oct 6 2012 doit
drwxr-xr-x 6 juser users 4096 Feb 20 13:51 dw
drwxr-xr-x 3 juser users 4096 May 2 2011 hough-stuff
2.3. 37

2.17 d, 1
.

2.3.2. cp
cp . ,
file1 file2, :
$ cp file1 file2
- ()
dir, :
$ cp file1 ... fileN dir

2.3.3. mv
mv ( . move ) cp.
. , file1
file2, :
$ mv file1 file2
mv,
:
$ mv file1 ... fileN dir

2.3.4. touch
touch . , touch
, ,
ls -l. , ,
:
$ touch file
ls -l. ,
.
touch:
$ ls -l file
-rw-r--r-- 1 juser users 0 May 21 18:32 file

2.3.5. rm
, rm ( . remove ).
, ,
.
$ rm file
38 2.

2.3.6. echo
echo :
$ echo Hello again.
Hello again.

echo ,
*, , $HOME,
.

2.4.
Unix /,
. (/),
(\).
, /usr, 2.19.
, .
/ (, /usr/lib),
.
, (..),
. , ,
/usr/lib, .. /usr. ../bin
/usr/bin.
(.) . , ,
/usr/lib, . - /usr/lib, ./X11
/usr/lib/X11. ,
,
/ ( X11
./X11).
, /, .
,
- .
, ,
.

2.4.1. cd
,
(, ). cd
:
$ cd dir

dir, ,
.
2.4. 39

2.4.2. mkdir
mkdir dir:
$ mkdir dir

2.4.3. rmdir
rmdir dir:
$ rmdir dir

dir , .
, rm -rf dir. !
, ,
superuser. r ,
, dir,
-f . -rf
, (*).
.

2.4.4.
( )
.
.
.
(*), ,
. ,
:
$ echo *

, ,
, ,
. -
, .
, *
:
at* , at;
*at ,
at;
*at* , at.
,
,
*. , , , echo *dfkdsafh.
40 2.

MS-DOS, *.*,
. . Linux
Unix *. Unix *.* -
, .
Unix , .

, (?),
, . ,
b?at boat brat.
,
, (' '). , , '*'
. , ,
grep find ( ,
11.2).

,
. , * ,
; .


,
* ?.

2.5.

Unix.

2.5.1. grep
grep ,
- . , /etc/
passwd, root, :
$ grep root /etc/passwd

grep ,
,
. , /etc,
root, :
$ grep root /etc/*

grep i (
, ) v (
, , ).
2.5. 41

egrep (
grep -E).
grep , .

Unix. ,
, .
, :
.* (
*);
. .

grep(1) , -
. , Mastering
Regular Expressions ( ), 3- (OReilly, 2006)
Programming Perl ( Perl), 4--
(OReilly, 2012). ,
, Automata Theory, Languages and Computation ( ,
), 3- (Prentice Hall, 2006).

2.5.2. less
less ,
.
, /usr/share/dict/words,
less/usr/share/dict/words. less
,
. , ,
B, . ,
Q.

less more. -
Linux less,
Unix. - -
less, more.

less.
, word, /word.
?word. ,
N .
2.14,
.
, ,
-
less. grep less:
$ grep ie /usr/share/dict/words | less
42 2.

. ,
less .

2.5.3. pwd
pwd ( )
. : ,
Linux ,
? .
-, ,
,
. , pwd.
-, , 2.17.2,

. , pwd -P.

2.5.4. diff
,
diff:
$ diff file1 file2
,
,
.
diff -u, -
,
.

2.5.5. file
, ,
file, :
$ file file

2.5.6. find locate


, , - -
, , .
find, file dir:
$ find dir -name file -print
, find
. ,
-exec, ,
2.6. 43

-name -print. find


*,
('*'), ,
. 2.4.4 ,
.
locate .

, .
locate , find,
, locate .

2.5.7. head tail


,
head tail . , head/etc/passwd
, tail/etc/passwd
.
, -n,
n , (,
head-5/etc/passwd). , n,
tail+n.

2.5.8. sort
sort - .

, -n. -r
.

2.6.

passwd.
, .
, - ,
.

. - , (
),
.
.
( , ksh tcsh)
chsh, , ,
bash.
44 2.

2.7.
,
ls, ls -a. ?
ls -a ,
.
(.). .bashrc .login.
, .ssh.
.
,
. , , ls
, -a. ,
,
.*.

, .* .
.. ( ). .[^.]* .??*,
, .

2.8.
, -
.
.
(, bash
PS1 ).
,
(=). :
$ STUFF=blah
STUFF blah.
, $STUFF (,
, echo$STUFF).
11.
,
. Unix
.
,
, ,
, .
export.
, , $STUFF
, :
$ STUFF=blah
$ export STUFF
2.10. 45

,
. ,
less LESS,
less .
Environment (),
.

2.9.
PATH , -
, .
, , - .
, ls, ,
PATH, ls.
,
.
echo$PATH, ,
(:). , :
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
,
PATH. ,
dir,
, PATH.
$ PATH=dir:$PATH
PATH,
:
$ PATH=$PATH:dir

, ,
$PATH. , . -
, . , -
,
. -
.

2.10.
Linux.
, Jargon File (http://
www.catb.org/jargon/html/) The New
Hackers Dictionary ( ) (MIT Press, 1996).
46 2.

.2.1 ,
. , Perl,
! ,
.

2.1.


* ,
. ,
! ,
|
/ ,
\ , ( )
$ ,
'
`
"
^ ,
~ ,
# , , ,
[]
{} ,
_

, ; , ^C Ctrl+.

2.11.
,
, ,
.
Linux.
, .
, .2.2, ,
Unix
.

2.2.


Ctrl+B
Ctrl+F
Ctrl+P ( )
Ctrl+N ( )
2.12. 47


Ctrl+A
Ctrl+E
Ctrl+W
Ctrl+U
Ctrl+K
Ctrl+Y (, CTRL+U)

2.12.
Unix ,
.
( , /etc).
, ,
.
, -
Unix: vi Emacs. Unix
.
, .
.
, ,

,
Emacs.
, vi. -
.

Learning the vi and Vim Editors: Unix Text Processing ( vi Vim:


Unix), 7- (OReilly, 2008), ,
vi. Emacs , ,
Ctrl+H T. GNU Emacs
Manual ( GNU Emacs) (Free Software Foundation, 2011).


, Pico
GUI-. , ,
.


. vi
/.
, .
Emacs GUI-, -
.
48 2.

2.13.
Linux .
. ,
ls, :
$ man ls

, , .

.
,
- (, ).
. ,
.
,
-k:
$ man -k keyword
, .
, -, :
$ man -k sort
--snip--
comm (1) -
qsort (3) -
sort (1) -
sortm (1) -
tsort (1) -
--snip--
, (.
), , .

- , , -
man.

.
- ,
, , : ping(8). .2.3
.

2.3.


1
2
3 Unix
2.14. 49


4
5 ( )
6
7 , (ASCII, ..)
8

1, 5, 7 8 . 4
, 6 ,
. , 3,
, 2,
.
. ,
,
. , /etc/passwd (
passwd), :
$ man 5 passwd

,
. -
, , --help -h
( ).
(, , ls --help) , ,
, .
- GNU Project ,
, info ( texinfo).
, ,
. ,
info :
$ info command


/usr/share/doc, -
man info. , ,
, , .

2.14.
, Unix,
, .
.
command , ,
>:
$ command > file
50 2.

file, . ,
( ).
, . bash, ,
setC.

>>:
$ command >> file

,
.
-
, (|). ,
, :
$ head /proc/cpuinfo
$ head /proc/cpuinfo | tr a-z A-Z

.
.

2.14.1.
,
- .
(stderr).
.
, :
$ ls /fffffffff > f

f ,
:
ls: cannot access /fffffffff: No such file or directory

, . , ,
f, e,
2> :
$ ls /fffffffff > f 2> e

2 , .
1 ( ), 2
.
,
, >&.
, f,
:
$ ls /fffffffff > f 2>&1
2.15. 51

2.14.2.
, <:
$ head < /proc/cpuinfo
,
, Unix
, .
head/proc/cpuinfo.

2.15.
, Unix (,
Linux), .
, Unix , ,
, .

2.15.1. Unix
Unix
,
. ,
:
$ ls /dsafsda
ls: cannot access /dsafsda: No such file or directory
.
: ls.
, ,
, , .
, /dsafsda, .
.
No such file or directory
.
, ls
/dsafsda, , .
, ,
, .
.
,
. , ,
scumd,
:
scumd: cannot access /etc/scumd/config: No such file or directory
52 2.

,
. . ,
/etc/scumd/config.

.
, warning. - ,
. ,
, , -
( 2.16).

2.15.2.
, Unix,
.
- .

No such file or directory


, .
/ Unix ,
. ,
,
, ..

File exists
, , , .
, , -
.

Not a directory, Is a directory


,
. , :
$ touch a
$ touch a/b
touch: a/b: Not a directory
,
a a/b. , ,
, .

No space left on device


.

Permission denied
, ,
, (
). , ,
2.16. 53

(
). 2.17 .

Operation not permitted


, ,
.

Segmentation fault, Bus error


, ,
, - .
, ,
. , ,
.
, , , -
.

2.16.

.
(PID).
, ps. :
$ ps
PID TTY STAT TIME COMMAND
520 p0 S 0:00 -bash
545 ? S 3:59 /usr/X11R6/bin/ctwm -W
548 ? S 0:10 xclock -geometry -0-0
2159 pd SW 0:00 /usr/bin/vi lib/addresses
31956 p3 R 0:00 ps
.
PID .
TTY , (
).
STAT , :
. , S
, R (
ps(1) ).
TIME ,
. ,
,
.
COMMAND , ,
.
54 2.

2.16.1. ps
ps .
, : Unix, BSD GNU.
, BSD (,
),
. .2.4
.

2.4. ps



ps x ,
ps ax , ,

ps u
ps w , ,

, , , :
ps aux ps auxw. - ,
PID ps. ,
, psu$$,
$$ ,
PID . 8
top lsof. ,
,
.

2.16.2.
, kill.
. kill,
.
:
$ kill pid
. TERM
(). ,
kill. , , ,
STOP:
$ kill -STOP pid
.
CONT, :
$ kill -CONT pid
2.16. 55

Ctrl+ , , -
kill INT ().

KILL.
, KILL .
.
. ,
, .
,
kill. , kill-9 kill-KILL.
, .
kill , ,
.

2.16.3.

TSTP ( STOP) CONT
. , TSTP
Ctrl+Z, fg (
) bg ( ; . ).

,
. Ctrl+Z Ctrl+,

.

, ,
jobs.

,
, (.
) screen.

2.16.4.
Unix
, .

(&); .
gunzip ( 2.18),
- ,
:
$ gunzip file.gz &
56 2.

PID ,
, .
, .
, ,
.
.
,
(, ,
). -
, ( fg,
) . ,
,
- :
, - .
, ,
(, , ), 2.14.
, . bash
Ctrl+L
.
, Ctrl+R ,

, . , , Ctrl+R
bash, isearch
mode ( Esc).

2.17.
Unix , ,
, . ls-l
. :
-rw-r--r-- 1 juser somegroup 7041 Mar 26 19:34 endnotes.html

. , .2.1.
. (-) , ,
, .
, .
d , (3.1).
,
: ,
. rw-
, r--,
, , , r .

(.2.5).
2.17. 57

.2.1.

2.5.


r
w
x ( )

( )
, . juser.
( somegroup).
.
groups, , .
7.3.5.
( ) ,
. .

, , , -
. , -
.


x s. ,
setuid ,
, .
setuid, ,
. passwd,
/etc/passwd.

2.17.1.
, chmod.
, , , .
, (g)
(o, other ),
:
$ chmod g+r file
$ chmod o+r file
58 2.

:
$ chmod go+r file
, go-r go+r.

,
, .
-, , ? , ,
. ,
.

, , , :
$ chmod 644 file
,
. , ,
,
( 8
).
chmod(1).
, ,
, . .2.6
.

2.6.


644 : /;
, :
600 : /;
, :
755 : //; ,
, : /
700 : //; ,
, :
711 : //;
, :

.
, ,
,
.
,
.
,
umask,
. ,
umask022, ,
2.17. 59

, umask077 .
umask
,
(.13).

2.17.2.
, ,
, , ( Windows).
.

( l,
):
lrwxrwxrwx 1 ruser users 11 Feb 27 13:52 somedir -> /home/origdir
somedir ,
/home/origdir.
, . ,
, , - . , ,
/home/origdir .
, /home/origdir , ,
somedir, , (
ls somedir, , somedir
somedir). ,
somedir.
,
. ,
, ,
; , ,
. , -
.

2.17.3.
target linkname,
ln-s:
$ ln -s target linkname
linkname , target
, , -s
(. ).

. , (lnslinknametarget),
, linkname .
( ), ln
target linkname , ,
60 2.

linkname .
, .
,
. , ,
, ,
.

-s . ln
, .
, : ,
, . ,
. 4.5, .

,
? -,
, -,
.

2.18.
, ,
gzip tar.

2.18.1. gzip
gzip (GNU Zip)
Unix. , .gz, GNU
Zip. gunzipfile.gz <file>.gz
; gzipfile.

2.18.2. tar
, gzip
,
. tar:
$ tar cvf archive.tar file1 file2 ...
, tar, .tar
( ; ).
file1, file2 .. ,
<archive>.tar. c
. v f .
v ,
tar .
v, tar ,
2.18. 61

. ,
, v.
f -.
-,
tar ( <archive>.tar).
, (
).
(-) .

tar
.tar tar, x:
$ tar xvf archive.tar
x tar ().
, ,
. ,
.

, tar
.tar .


.tar ,
t x.
.
,
, .
t ,
,
. ,
, , .
mv*.., .
p,
. ,
umask ,
. p
superuser. superuser-
,
.
, tar
. ,
.
tar,
. , -
.
62 2.

2.18.3. (.tar.gz)
,
, .tar.gz. ,
: .gz,
.tar. ,
<file>.tar.gz:
$ gunzip file.tar.gz
$ tar xvf file.tar
,
gunzip , tar
. , :
tar, gzip. ,
, .

2.18.4. zcat

tar .
, /.
.
, - <file>.tar.gz:
$ zcat file.tar.gz | tar xvf
zcat gunzip -dc. -d
, -c (
tar).
zcat ,
tar, Linux, -.
z, gzip.
( x t tar),
( c). , ,
, :
$ tar ztvf file.tar.gz
,
.

.tgz , .tar.gz. -
FAT ( MS-DOS).

2.18.5.
Unix bzip2,
, .bz2. gzip,
2.19. Linux 63

bzip2
.
bunzip2,
gzip,
. tar /
bzip2 j.
xz .
unxz,
gzip.
Linux zip
unzip, ZIP- Windows.
.zip, ,
.exe. ,
.Z, , , -
Unix. gunzip ,
gzip .

2.19.
Linux
, , ,
,
. -
Filesystem Hierarchy Standard, or FHS (http://www.pathname.com/fhs/),
.
.2.2 ,
/, /usr /var. ,
/usr , /.

.2.2. Linux
64 2.

.
/bin. (
), Unix,
ls cp. /bin
, C,
.
/dev. ( 3).
/etc.
, , ,
. /etc
. , /etc/X11
.
/home. .
Unix .
/lib. library ().
, ,
. :
. /lib
. ,
/usr/lib, , (
15).
/proc.
-. /proc
Linux, Unix
. /proc
, .
/sys. /proc ,
( /sys 3).
/sbin. .
/sbin ,
, , /sbin.
,
.
/tmp. ,
.
,
.
. - /tmp,
,
.
/tmp ,
-
(, ).
2.19. Linux 65

/usr. user (),


.
, Linux.
, ( /usr/bin /usr/lib),
. ,
,
, .
/var. ,
. , ,
, , ,
. /var/tmp,
.

2.19.1.
.
/boot. .
Linux.
, Linux ( 5
).
/media. , -
. .
/opt. .
/opt.

2.19.2. /usr
/usr ,
/usr/bin /usr/lib .
/usr
. /usr/bin, /usr/sbin /usr/lib,
/usr .
/include. , C.
/info. GNU info (. 2.13).
/local.
.
/ /usr.
/man. .
/share. , Unix
.
, /share
,
. /share
66 2.

. /man, /info
.

2.19.3.
Linux /vmlinuz /boot/vmlinuz.

( 5).
,
.
,
.
/lib/modules.

2.20.
superuser
, ,
superuser. , ,
su .
, :
, ;
, ,
;
;
.

2.20.1. sudo
sudo,
,
. , 7 vipw
/etc/passwd. :
$ sudo vipw
sudo
syslog local2.
7.

2.20.2. /etc/sudoers

superuser.
/etc/sudoers. sudo (,
, ), /
2.21. 67

etc/sudoers . ,
user1 user2
, :
User_Alias ADMINS = user1, user2

ADMINS ALL = NOPASSWD: ALL

root ALL=(ALL) ALL


ADMINS,
. ALL=NOPASSWD:ALL
, ADMINS sudo
. ALL .
ALL .

,
, .

rootALL=(ALL)ALL , superuser
sudo .
(ALL) , superuser
.
ADMINS, (ALL) /etc/sudoers,
:
ADMINS ALL = (ALL) NOPASSWD: ALL

visudo /etc/sudoers.
.

sudo,
sudoers(5) sudo(8).
7.

2.21.
, ,
, ,
.
Linux,
superuser. ,
, ,
, , .
,
,
.
3

,
Linux.
,
.
, ,
sysfs.
, .

.
, ,
. udev

. ,

udev .

3.1.
Unix ,
- .
.
-
:
cat. , ,
,
,
-.
Linux ,
Unix. /dev,
ls/dev
. :
$ echo blah blah > /dev/null
3.1. 69

,
. /dev/null
, , ,
. /dev/null
.
,
ls-l:
3.1.
$ ls -l
brw-rw---- 1 root disk 8, 1 Sep 6 08:37 sda1
crw-rw-rw- 1 root root 1, 3 Sep 6 08:37 null
prw-r--r-- 1 root root 0 Mar 3 19:17 fdata
srw-rw-rw- 1 root root 0 Dec 18 07:43 log

(
) 3.1. b, c, p s,
. ,
, , .
. -
. sda1
.
.
,
.
. .

, /dev/null.
. ,
. ,
, .
:
,
.
. ,
- ,
.
. ,
.
/dev. Unix (
10).
3.1 -
, .
, , , sda3
sdb1 ( ).
70 3.

, -
. , -
.
, , ,
-.

3.2. sysfs
/dev Unix ,
,
, .
. /dev
, . ,
,
.
,
, Linux
sysfs .
/sys/devices. , SATA
/dev/sda sysfs:
/sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda
,
/dev/sda, .
, . /dev
, ,
/sys/devices
. ,
, - .

alignment_offset discard_alignment holders removable size uevent


bdi events inflight ro slaves
capability events_async power sda1 stat
dev events_poll_msecs queue sda2 subsystem
device ext_range range sda5 trace


, , ,
, - , /dev.
catdev 8:0,
/dev/sda.
/sys . , /sys/block
.
. ls-l/sys/block,
sysfs-.
3.3. dd 71

sysfs-
/dev. udevadm, :
$ udevadm info --query=all --name=/dev/sda

udevadm /sbin; -
, .

udevadm udev
3.5.

3.3. dd
dd
.
,
.
dd .
dd
:
$ dd if=/dev/zero of=new_file bs=1024 count=1
, dd
Unix. JCL (Job Control
Language, ), IBM.
(-) ,
(=) .
1024
/dev/zero ( ) new_file.
dd.
if=file. . .
of=file. . .
bs=size. . dd
.
, b k,
512 1024. , bs=1k
bs=1024.
ibs=size, obs=size. .
, bs.
, ibs obs .
count=num. , .
( ,
, /dev/zero) dd
,
72 3.

, .
count skip,
.
skip=num. num
.

dd , , . -
, .
, .

3.4.
(,
). .
udevd udevadm (. 3.5).
/sys.
dmesg
( )
(. 7.2).
.
, ,
mount.
cat/proc/devices,
, .
. ,
3.1. ,
/dev
. .
,
udev. ,
, , ,
.

Linux .

3.4.1. : /dev/sd*
Linux
sd, /dev/sda, /dev/sdb ..
;
, /dev/sda1 /dev/sda2.
. sd
SCSI. SCSI (Small Computer
3.4. 73

System Interface, )

(, ) .

SCSI,
. , USB- .
SATA , Linux
- SCSI
. lsscsi.
, :
$ lsscsi
[0:0:0:0] disk ATA WDC WD3200AAJS-2 01.0 /dev/sda
[1:0:0:0] cd/dvd Slimtype DVD A DS8A5SH XA15 /dev/sr0
[2:0:0:0] disk FLASH Drive UT_USB20 0.00 /dev/sdb
.
, ,
. .
Linux ,
. ,
, -.
,
. , :
/dev/sda, /dev/sdb /dev/sdc. /dev/sdb
, , ,
/dev/sdc, /dev/sdb, /dev/sdc .
fstab (.
4.2.8), ,
( ) .
, Linux
UUID (Universally Unique Identifier, ,
. 4.2.4) .
,
Linux.
4. ,
SCSI Linux.

3.4.2. CD DVD: /dev/sr*


Linux SCSI
/dev/sr0, /dev/sr1 ..
, PATA (
). /dev/sr*
.
SCSI-
, /dev/sg0.
74 3.

3.4.3. PATA: /dev/hd*


Linux /dev/hda, /dev/hdb, /dev/hdc /dev/hdd
.
,
0 1. , SATA
. , SATA
, .
BIOS, , SATA
.

3.4.4.: /dev/tty*, /dev/pts/* /dev/tty



-, ,
.
,
.
,
- . ,
,
- - ,
, , , .
/dev/tty1 (
) /dev/pts/0 ( ).
/dev/pts .
/dev/tty .
- ,
.
.


Linux :
XWindow System ( , ,
). Linux
,
(
, plymouth),
.
.
Linux ,
.
.
Alt -
. , Alt+F1 /dev/tty1,
Alt+F2 /dev/tty2 ..
3.4. 75

getty, (
7.4).
, X- ,
.
, X-
, . ,
getty tty1 tty2, X-
tty3. ,
, , Ctrl+Alt
,
Alt - .
,
, Ctrl+Alt+F1.
X11, Alt+F2, Alt+F3 .., X-
.

- ,
chvt.
, tty1,
root:
# chvt 1

3.4.5. : /dev/ttyS*
RS-232
.
, ,

.
, Windows COM1, /dev/ttyS0; COM2
/dev/ttyS1 .. USB-
USB ACM: /dev/ttyUSB0, /dev/ttyACM0, /dev/ttyUSB1,
/dev/ttyACM1 ..

3.4.6. : /dev/lp0 /dev/lp1


, USB,
/dev/lp0 /dev/lp1
LPT1 LPT2 Windows. (,
) cat,

. , CUPS,
.
/dev/parport0 /dev/
parport1.
76 3.

3.4.7.: /dev/snd/*, /dev/dsp,


/dev/audio
Linux .
ALSA (Advanced Linux Sound Architecture,
Linux), OSS (Open Sound System,
). ALSA /dev/snd,
. Linux,
ALSA, OSS
, OSS.
OSS- dsp audio.
, WAV,
/dev/dsp.
, , .
, ,
.

Linux
. , -
, pulse-audio, -
-
.

3.4.8.
Linux ;
devtmpfs udev
(. 3.5). , ,
.
mknod . ,
. , /dev/sda1,
:
# mknod /dev/sda1 b 8 2
b 8 2 8
2.
c p b (
).
, mknod
.
,
.
Unix Linux /dev
.

3.5. udev 77

, ,
. ,
MAKEDEV /dev
.
MAKEDEV, ,
.
,
. devfs,
/dev , ,
.
, udev
devtmpfs.

3.5. udev
.
:
, ?
Linux (
udevd) (,
USB-). ,
, ,
.
. , :
,
udevd . ,
udevd ,
. ,
,
udevd.

3.5.1. devtmpfs
devtmpfs
(. 4.2).
devfs, .
, udevd ,
. udevd
,
. ,
/dev .
/dev/disk/by-id,
.
78 3.

, , :
lrwxrwxrwx 1 root root 9 Jul 26 10:23 scsi-SATA_WDC_WD3200AAJS-_WD-WMAV2FU80671 ->
../../sda

lrwxrwxrwx 1 root root 10 Jul 26 10:23 scsi-SATA_WDC_WD3200AAJS-_WD-WMAV2FU80671-


part1 ->

../../sda1

lrwxrwxrwx 1 root root 10 Jul 26 10:23 scsi-SATA_WDC_WD3200AAJS-_WD-WMAV2FU80671-


part2 ->

../../sda2

lrwxrwxrwx 1 root root 10 Jul 26 10:23 scsi-SATA_WDC_WD3200AAJS-_WD-WMAV2FU80671-


part5 ->

../../sda5
udevd ,
, ( ).
, udevd
: , ,
. , .
Linux,
, , .

3.5.2. udevd
udevd .
1. udevd -, uevent,
.
2. udevd , uevent.
3. udevd ,
.
uevent, udevd ,
:
ACTION=change
DEVNAME=sde
DEVPATH=/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2/1-1.2:1.0/host4/
target4:0:0/4:0:0:3/block/sde
DEVTYPE=disk
DISK_MEDIA_CHANGE=1
MAJOR=8
MINOR=64
SEQNUM=2752
SUBSYSTEM=block
UDEV_LOG=3
3.5. udev 79

, .
uevent udevd sysfs,
, ,
.
/lib/udev/rules.d /etc/udev/rules.d.
/lib ,
/etc .
udev(7).
/dev/sda 3.5.1.
/lib/udev/rules.d/60-
persistent-storage.rules. :
# ATA devices using the "scsi" subsystem

KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="scsi",


ATTRS{vendor}=="ATA", IMPORT{program}="ata_id --export $tempnode"

# ATA/ATAPI devices (SPC-3 or later) using the "scsi" subsystem

KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="scsi",

ATTRS{type}=="5", ATTRS{scsi_level}=="[6-9]*", IMPORT{program}="ata_id --export


$tempnode"

ATA, SCSI-
(.3.6). ,
,
, , udevd
, sd sr
( KERNEL=="sd*[!0-9]|sr*" ),
(SUBSYSTEMS=="scsi") ,, .
, udevd
:
IMPORT{program}="ata_id --export $tempnode"

,
/lib/udev/ata_id. ,
:
$ sudo /lib/udev/ata_id --export /dev/sda
ID_ATA=1
ID_TYPE=disk
ID_BUS=ata
ID_MODEL=WDC_WD3200AAJS-22L7A0
ID_MODEL_ENC=WDC\x20WD3200AAJS22L7A0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
\x20\x20\x20\x20\x20\x20\x20\x20\x20
ID_REVISION=01.03E10
ID_SERIAL=WDC_WD3200AAJS-22L7A0_WD-WMAV2FU80671
--snip--
80 3.

,
. , ,
, ENV{ID_TYPE} .
ID_SERIAL.
:
ENV{ID_SERIAL}!="?*"

, , ID_SERIAL
. , ,
, udevd
.
? ( )
.
ENV{ID_SERIAL} , udevd
:
KERNEL=="sd*|sr*|cciss*", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL}=="?*",
SYMLINK+="disk/by-id/$env{ID_BUS}-$env{ID_SERIAL}"

ENV{ID_SERIAL}.
:
SYMLINK+="disk/by-id/$env{ID_BUS}-$env{ID_SERIAL}"

udevd
. , ,
.
, ,
. (==)
(!=).
(=), (+=),
(:=).

3.5.3. udevadm
udevadm udevd.
udevd, -.
, , udevadm
,
uevents, udevd .
,
.
.
3.5.2, udev,
( /dev/
sda), :
$ udevadm info --query=all -name=/dev/sda
3.5. udev 81

:
P: /devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda
N: sda
S: disk/by-id/ata-WDC_WD3200AAJS-22L7A0_WD-WMAV2FU80671
S: disk/by-id/scsi-SATA_WDC_WD3200AAJS-_WD-WMAV2FU80671
S: disk/by-id/wwn-0x50014ee057faef84 S: disk/by-path/pci-0000:00:1f.2-scsi-
0:0:0:0
E: DEVLINKS=/dev/disk/by-id/ata-WDC_WD3200AAJS-22L7A0_WD-WMAV2FU80671 /dev/disk/by-
id/scsi
-SATA_WDC_WD3200AAJS-_WD-WMAV2FU80671 /dev/disk/by-id/wwn-
0x50014ee057faef84 /dev/disk/by
-path/pci-0000:00:1f.2-scsi-0:0:0:0
E: DEVNAME=/dev/sda
E: DEVPATH=/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda
E: DEVTYPE=disk
E: ID_ATA=1
E: ID_ATA_DOWNLOAD_MICROCODE=1
E: ID_ATA_FEATURE_SET_AAM=1
--snip--

. P:
sysfs; N: ( ,
/dev), S: ,
udevd /dev ; E:
, udevd.
, ;
,
.

3.5.4.
uevents udevadm,
monitor:
$ udevadm monitor
( , , -)
:
KERNEL[658299.569485] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2
(usb)
KERNEL[658299.569667] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-
1.2:1.0 (usb)
KERNEL[658299.570614] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-
1.2:1.0/host15
(scsi)
KERNEL[658299.570645] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-
1.2:1.0/
host15/scsi_host/host15 (scsi_host)
82 3.

UDEV [658299.622579] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2


(usb)
UDEV [658299.623014] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-
1.2:1.0 (usb)
UDEV [658299.623673] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-
1.2:1.0/host15
(scsi)
UDEV [658299.623690] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-
1.2:1.0/
host15/scsi_host/host15 (scsi_host)
--snip--
,
( KERNEL),
, udevd
. ,
--kernel , ,
--udev. uevent
, , 3.5.2,
--property.
- . ,
,
SCSI, :
$ udevadm monitor --kernel --subsystem-match=scsi
udevadm
udevadm(8).
udev, . , D-Bus
udisks-daemon,
udevd,
,
.

3.6.: SCSI
Linux
SCSI Linux,
.
- , 4,
. ,
, , ,
.
.
SCSI -,
SCSI, .3.1. -
.
3.6.: SCSI Linux 83

SCSI ID, SCSI


8 16. ,
SCSI,
SCSI ID.

.3.1. SCSI -

- SCSI
; -.
, -,
. ,
SCSI- -,
.
SCSI, SAS (Serial Attached SCSI,
SCSI ),
, , , SCSI-
. USB- ,
SCSI. ,
ATAPI (, CD/DVD-ROM),
SCSI.
SATA SCSI,
libata (. 3.6.2).
SATA ( RAID-
) .
? ,
:
$ lsscsi
[0:0:0:0] disk ATA WDC WD3200AAJS-2 01.0 /dev/sda
[1:0:0:0] cd/dvd Slimtype DVD A DS8A5SH XA15 /dev/sr0
[2:0:0:0] disk USB2.0 CardReader CF 0100 /dev/sdb
[2:0:0:1] disk USB2.0 CardReader SM XD 0100 /dev/sdc
[2:0:0:2] disk USB2.0 CardReader MS 0100 /dev/sdd
[2:0:0:3] disk USB2.0 CardReader SD 0100 /dev/sde
[3:0:0:0] disk FLASH Drive UT_USB20 0.00 /dev/sdf
84 3.

( ): -
SCSI, SCSI, SCSI ID
LUN (Logical Unit Number,
).
(scsi0, scsi1, scsi2
scsi3), ( 0)
( 0). - USB
2:0:0 -
, .
.
.3.2
,
.
SCSI (sg, SCSI generic).
,
. ,
SCSI .
. ,
sd ( SCSI-); ,
SCSI
.
SCSI
, SCSI ,
.
, .
SCSI
,
.
, , SCSI
- (, ),
-
.
,
,
.
/dev/sda sd
ATA .

(. 3.6.3).
SCSI, , -
SCSI, RAID-,
.
,
SCSI, .
3.6.: SCSI Linux 85

.3.2. SCSI Linux

3.6.1.USB- SCSI
SCSI USB-
, .3.2, SCSI
86 3.

. - USB, /dev/sdf,
SCSI, ,
USB.
USB SCSI:
, -. , ,
Linux USB,
SCSI: ,
, -
. SCSI
, USB .
lsusb, lsscsi.
, , .
USB-. . ,
SCSI, USB.
SCSI USB,
:
.
(SCSI USB) ,
, -.
SCSI,
USB- USB, SCSI. (

.) ,
SCSI
USB.

3.6.2. SCSI ATA


SATA , .3.2,
SATA. ,
SATA, SCSI, -,
USB-,
. ATAPI (
SCSI, ATA).
ATAPI SCSI!
Linux libata,
SATA ( ATA) SCSI. ATAPI
, SCSI-,
ATA.
, .

: , ,
. .

.
.
3.6.: SCSI Linux 87

libata
SCSI ATA/SATA. ,
,
SATA, .3.2.
.

3.6.3. SCSI
SCSI
/ ,
SCSI (, sd sr). ,
-
SCSI .

SCSI
. , , ,
. , ,
-g lsscsi, :
$ lsscsi -g
[0:0:0:0] disk ATA WDC WD3200AAJS-2 01.0 /dev/sda /dev/sg0
[1:0:0:0] cd/dvd Slimtype DVD A DS8A5SH XA15 /dev/sr0 /dev/sg1
[2:0:0:0] disk USB2.0 CardReader CF 0100 /dev/sdb /dev/sg2
[2:0:0:1] disk USB2.0 CardReader SM XD 0100 /dev/sdc /dev/sg3
[2:0:0:2] disk USB2.0 CardReader MS 0100 /dev/sdd /dev/sg4
[2:0:0:3] disk USB2.0 CardReader SD 0100 /dev/sde /dev/sg5
[3:0:0:0] disk FLASH Drive UT_USB20 0.00 /dev/sdf /dev/sg6

SCSI- ( ). ,
/dev/sr0 /dev/sg1.
SCSI-?
. ,
. CD/DVD.
, .
,
, ,
, . ,
Linux, ,
SCSI-, /dev/sg1.
, ,
sr .

3.6.4.

.3.3 SCSI- Linux (sr sg)


( ,
88 3.

SCSI).
sr, sg.

.

.3.3.

.3.3 .
?
, , : , .
,
, .
4

3 ,
. Linux.
, ,
, .
, /dev/sda,
SCSI.
, .
.4.1 Linux ( ).
, .

.4.1. Linux
90 4.

. Linux
, ,
, , , /dev/sda1 /dev/sdb3.
, .
,
.

- , -
.
,
. , , -
. -
Unix; - Windows
. , -
.


, ,
.
.
,
. 4.2.
.4.1, ,

,
.
, Linux ,
.4.2. SCSI , 3.6,
. ,
,
. .
, , , .

4.1.
. ,
MBR (Master Boot Record). ,

GPT (Globally Unique Identifier Partition Table).
Linux :
parted ,
MBR, GPT;
gparted parted ;
fdisk Linux
. GPT;
4.1. 91

.4.2.

gdisk fdisk, GPT,


MBR.
parted (MBR GPT),
.
fdisk, .

parted ,
, . -
. -
,
. , parted ,
(. 4.2.2).
parted .
92 4.

4.1.1.
parted-l.

:
# parted -l

Model: ATA WDC WD3200AAJS-2 (scsi)


Disk /dev/sda: 320GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags


1 1049kB 316GB 316GB primary ext4 boot
2 316GB 320GB 4235MB extended
5 316GB 320GB 4235MB logical linux-swap(v1)

Model: FLASH Drive UT_USB20 (scsi)


Disk /dev/sdf: 4041MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags


1 17.4kB 1000MB 1000MB myfirst
2 1000MB 4040MB 3040MB mysecond
, /dev/sda, MBR
( parted msdos),
GPT.
,
. , MBR Name
(), . (
myfirst mysecond GPT.)
MBR ,
. ; 1
. MBR
. ,
.
,
.
2 ,
5.

, parted, , -
MBR.
; , 83 Linux, 82 Linux. ,
parted .
MBR, fdisk-l.
4.1. 93


MBR Linux
(, dmesg):
sda: sda1 sda2 < sda5 >
sda2 < sda5 > , /dev/sda2
, , /dev/sda5. ,
,
.

4.1.2.
.
,
. .

,
. ,
, .
,
. , Linux
(
. 4.2.3).
, .
parted,
, gparted.
fdisk gdisk,
GPT.
. - -
, .
, fdisk parted.
fdisk
; fdisk , .
parted , ,
.
.
,
. fdisk parted
; ,
,
.
,
. fdisk
:
,
94 4.

.
, dmesg. ,
/dev/sdf, :
sdf: sdf1 sdf2
parted
.
.
.
.
udevadm, .
, udevadmmonitor--kernel -
.
/proc/partitions.
/sys/block/device/
/dev -.
,
, ,
fdisk, blockdev. ,
/dev/sdf,
:
# blockdev --rereadpt /dev/sdf
.
,
. 4.2,
.

4.1.3.

,
. .

, ,
, .
, .4.3.
, ,
,
. , .
,
. ,
,
. ,
/ ,
4.1. 95

. ,
,
. , , .
CHS (cylinder-head-sector,
--).

.4.3. ,

, , .4.3
.

,
( ,
).
! ,
CHS,
. ,
, .
LBA (Logical Block Addressing),
- .
CHS . , MBR
CHS, LBA-,
- , CHS (
Linux LBA).
,
.
,
. ,
,
96 4.

,
.
,
. .
, CHS
. LBA , ,
.

4.1.4. ( SSD)
, -
(SSD), ,
. ,
.
.
,
SSD, .
SSD, , 4096
, ,
. ,
4096- ,
, .
(, parted gparted)

,
. , , ,
, , ,
/sys/block. /dev/sdf2:
$ cat /sys/block/sdf/sdf2/start
1953126
1953126 .
4096,
, SSD.

4.2.

. ,
, ls cd. ,
; ,

, .
,
, .
4.2. 97

, - ,
,
, /sys /proc.
,
9P Plan9 (http://plan9.bell-labs.com/sys/doc/9.html)
.
FUSE (File System in User Space, )
Linux.
VFS ( )
. , SCSI
,
VFS
,
.
Linux .

4.2.1.
Linux , ,
Linux, , Windows
FAT, ISO 9660 .

. , Linux,
.
(ext4)
Linux . -
(ext2) Linux,
Unix,
Unix (UFS, Unix File System)
(FFS, Fast File System). (ext3)
(
)
. ext4 ,

ext2 ext3, .

. , ext2 ext3
, ext2 ext3 ext4,
ext4 ext2 ext3.
ISO 9660 (iso9660) CD-ROM.
CD-ROM - ISO 9660.
FAT (msdos, vfat, umsdos) Microsoft.
msdos
MS-DOS. Windows
vfat,
98 4.

OC Linux. umsdos Linux:


Unix, ,
MS-DOS.
HFS+ (hfsplus) Apple,
Macintosh.

,
, , ext4
.
, :
, .
Linux, Btrfs,
.

4.2.2.
, (. 4.1),
. ,
,
. mkfs
. ,
ext4 /dev/sdf2 :
# mkfs -t ext4 /dev/sdf2
mkfs
.
, ,
, .
mkfs
, , .
,
. , mkfs
.
mkfs, ,
(. 4.2.11).

, -
.
, (
, ).
.

, mkfs
. mkfs.fs, fs
. ,
mkfstext4, mkfs, , mkfs.ext4.
4.2. 99

. ,
mkfs.*, :
$ ls -l /sbin/mkfs.*
-rwxr-xr-x 1 root root 17896 Mar 29 21:49 /sbin/mkfs.bfs
-rwxr-xr-x 1 root root 30280 Mar 29 21:49 /sbin/mkfs.cramfs
lrwxrwxrwx 1 root root 6 Mar 30 13:25 /sbin/mkfs.ext2 -> mke2fs
lrwxrwxrwx 1 root root 6 Mar 30 13:25 /sbin/mkfs.ext3 -> mke2fs
lrwxrwxrwx 1 root root 6 Mar 30 13:25 /sbin/mkfs.ext4 -> mke2fs
lrwxrwxrwx 1 root root 6 Mar 30 13:25 /sbin/mkfs.ext4dev -> mke2fs
-rwxr-xr-x 1 root root 26200 Mar 29 21:49 /sbin/mkfs.minix
lrwxrwxrwx 1 root root 7 Dec 19 2011 /sbin/mkfs.msdos -> mkdosfs
lrwxrwxrwx 1 root root 6 Mar 5 2012 /sbin/mkfs.ntfs -> mkntfs
lrwxrwxrwx 1 root root 7 Dec 19 2011 /sbin/mkfs.vfat -> mkdosfs
mkfs.ext4 mke2fs.
, -
mkfs -
.
, , mke2fs(8).
, mkfs.ext4(8)
mke2fs(8). .

4.2.3.
Unix .
,
(/).
,
:
(, ;
);
;
, ,
.
. , /cdrom
CD-ROM.
, .

.
, mount. :
$ mount
/dev/sda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
100 4.

none on /sys/kernel/security type securityfs (rw)


udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
--snip--
,
. :
, /dev/sda3. ,
(, proc),
,
;
on;
;
type;
, , ;
( ) (. 4.2.6).
,
mount, ,
:
# mount -t type device mountpoint
, ,
/dev/sdf2 /home/extra, :
# mount -t ext4 /dev/sdf2 /home/extra
t, mount
.
, FAT, .
4.2.6
. () ,
umount:
# umount mountpoint
,
.

4.2.4. UUID
, ,
. ,
. ,
-
UUID (Universally Unique Identifier, ),
. UUID
, .
4.2. 101

, mke2fs, UUID
.
, ,
UUID, blkid (block ID):
# blkid
/dev/sdf2: UUID="a9011c2b-1c03-4288-b3fe-8ba961ab0898" TYPE="ext4"
/dev/sda1: UUID="70ccd6e7-6ae6-44f6-812c-51aab8036d29" TYPE="ext4"
/dev/sda5: UUID="592dcfd1-58da-4769-9ea8-5f412a896980" TYPE="swap"
/dev/sde1: SEC_TYPE="msdos" UUID="3762-6138" TYPE="vfat"
blkid :
ext4, (. 4.3)
FAT. Linux
UUIDs, FAT
. FAT FAT
( 3762-6138).
UUID,
UUID=. ,
/home/extra :
# mount UUID=a9011c2b-1c03-4288-b3fe-8ba961ab0898 /home/extra
,
, , , ,
, UUID.
UUID. ,

/etc/fstab (. 4.2.8).
, UUID
, .
FAT -. Ubuntu, -
, /media/3762-6138
. udevd, 3,
.
, UUID
(, - ,
).
tune2fs(8) , ,
ext2/ext3/ext4.

4.2.5. ,

Linux, Unix,
. ,
, .
,
102 4.

.
.
umount,
.
,
sync. -
,
sync.
, ,
, , .
, - ,

, .

4.2.6.
mount,

. .
mount(8) ,
, , .
.
:
. -t ;
, .
.
- ,
-o. , onorock Rock
Ridge ISO9660,
.


.
-r .
:
.
, CD-ROM, ( ,
).
-n , mount
/etc/mtab.
, ,
, (,
, )
. ,
4.2. 103

,
.
-t .


, -r,
. ,
.
, .
, ,
, .
mount ,
-o .
, -o:
# mount -t vfat /dev/hda1 /dos -o ro,conv=auto
: ro conv=auto. ro
-r.
conv=auto
DOS Unix (
).
:
exec, noexec
;
suid, nosuid setuid (
);
ro (
-r);
rw -;
conv=rule ( FAT)
, rule,
binary, text auto.
binary, .
, text.
auto, .
, .jpg , .txt
. ,
. .

4.2.7.

,
. ,
.
104 4.

-
( -n , mount
,
):
# mount -n -o remount /

,
/etc/fstab (
). , .

4.2.8. /etc/fstab
,
mount , Linux
/etc/fstab. ,
, 4.1.
4.1. /etc/fstab
proc /proc proc nodev,noexec,nosuid 0 0
UUID=70ccd6e7-6ae6-44f6-812c-51aab8036d29 / ext4 errors=remount-ro 0 1
UUID=592dcfd1-58da-4769-9ea8-5f412a896980 none swap sw 0 0
/dev/sr0 /cdrom iso9660 ro,user,nosuid,noauto 0 0

, ,
. ( ).
UUID.
Linux /etc/fstab,
UUID. , /proc
- proc.
. , .
. , swap
; (. 4.3).
. , .
.
0.
. fsck
, 1
2
. 0, -
, CD-ROM,
/proc ( fsck 4.2.11).
mount ,
, , /etc/fstab.
, , 4.1 CD-ROM,
mount/cdrom.
4.2. 105

,
/etc/fstab ( noauto),
:
# mount -a
4.1 , : errors, noauto
user, /etc/fstab. ,
defaults.
.
defaults. mount :
-, , , setuid
.. ,
,
/etc/fstab.
errors. , ext2,
,
. errors=continue,
, .

, errors=remount-ro. errors=panic
( ) , ,
.
noauto. mount -a,
. ,
, CD-ROM
-.
user.
mount - ,
CD-ROM.
setuid ,
nosuid, noexec nodev (
).

4.2.9. /etc/fstab
/etc/fstab
, .
/etc/fstab.d,
( ).
, .
systemd
. systemd 6.
systemd /etc/fstab (
),
.
106 4.

4.2.10.

, df.
:
$ df
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/sda1 1011928 71400 889124 7% /
/dev/sda3 17710044 9485296 7325108 56% /usr

:
Filesystem , ;
1024-blocks 1024;
Used ;
Available ;
Capacity ;
Mounted on .
,
1 17,5. ,
71400 889124 1011928, 9485296
56% 17710044. 5%
. , -
. , superuser
,
.
,
.
,
, du.

, .
cd /;, , Ctrl+C.
du -s .
- ,
du -s *.

POSIX (Portable Operating System Interface for Unix,


Unix) 512. -
, df du
Linux 1024- .
512- , POSIXLY_CORRECT.
1024, -k ( ).
df -m, 1, -h,
.
4.2. 107

4.2.11.

Unix
.
, ,
. ,
.
,
(,
).
,
, .
,
,
.
.
fsck.
mkfs, ,
Linux. , fsck
(ext2/ext3/ext4),
e2fsck. , , ,
e2fsck, fsck
e2fsck.
, ,
e2fsck.
fsck ,
(
/etc/fstab). , :
# fsck /dev/sdb1

fsck ,
. -
, . -
: -
, fsck.

fsck
, :
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information /dev/sdb1: 11/1976 files (0.0% non-
contiguous), 265/7891 blocks
108 4.

fsck ,
, .
,
inode (
inode ;
4.5). fsck
inode, , , ,
-, . fsck
lost+found , .
, ,
; , , .
,
, fsck
, . , e2fsck
-p, ,
, .
Linux - fsck-p.
fsck-a, .
, ,
,
, fsck
.
, ,
, fsck .
fsckn, ,
. ,
(,
), fsck
. .
, (, -
),
, mkfs.
fsckbnum,
num.
, ,
mkfs-n ,
, . - ,
-n, .

ext3 ext4
ext3 ext4,
.
ext3 ext4
ext2,
. ,
4.2. 109

- . ext3
ext4, , :
# e2fsck fy /dev/disk_device



.

dd .
, ,
.
debugfs.

,
. , y fsck,
: fsck-y.
,
, .
debugfs
-.
. , ,
,
.

, ,
.

4.2.12.

.
Unix ,
.
, ,
.
/dev,
-. /proc
Unix,
. (TomJ.Killian)
Bell Labs ( Unix),
Plan9 ,
(http://plan9.bell-labs.com/sys/
doc/9.html).
110 4.

, Linux,
.
proc. /proc. proc
process (). /proc
;
. /proc/self
. proc Linux

/proc/cpuinfo. ,
, /proc /sys.
sysfs. /sys ( 3).
tmpfs. /run .
tmpfs
. , tmpfs ,
, size nr_blocks
.
tmpfs,
. , Sun Microsystems
tmpfs /tmp,
, .

4.3.
.
.
, Linux
.
(),
,
. , ,
( swap).
free
( ):
$ free
total used free
--snip--
Swap: 514072 189804 324268

4.3.1.

,
.
4.3. 111

1. , .
2. mkswapdev, dev
.
.
3. swapondev,
.

/etc/fstab,
. ,
/dev/sda5:
/dev/sda5 none swap sw 0 0
,
UUID .

4.3.2.

,
, ,
. - .
, ,
, :
# dd if=/dev/zero of=swap_file bs=1024k count=num_mb
# mkswap swap_file
# swapon swap_file
swap_file , num_mb
.
,
swapoff.

4.3.3.
, Unix, ,

. ,
,
. ,
, ,
. , ,
, .
,

. ,
112 4.

,
, .
- .
, ,

. ,
, ,
,
-
. :
.
Linux -
. ,

. ,
.

. -
, Linux OOM (out-of-memory,
), . , ,
, . ,
,
.

8 , .

4.4. :

Unix
.
, - ,
-
. , ,
- ,
, .
,
-.
,
.

Linux. ,
, .
, Linux.
4.5. 113

4.5.

Unix :
, , , .
inode. inode
, , , (,
, ) .
inode , .
inode.

.
, ,
.
(
-):
$ mkdir dir_1
$ mkdir dir_2
$ echo a > dir_1/file_1
$ echo b > dir_1/file_2
$ echo c > dir_1/file_3
$ echo d > dir_2/file_4
$ ln dir_1/file_3 dir_2/file_5
, dir_2/file_5
dir_1/file_3. ,
.
,
, .4.4. ,
.4.5, ,
.

.4.4.
114 4.

.4.5. , .4.4

(ext2/3/4)
2 inode. .4.5
, (dir),
, :
dir_1 dir_2, inode12 7633.
,
.
dir_1/file_2
.
1. : dir_1
file_2.
2. .
3. inode 2 dir_1,
12.
4. inode12 ,
.
4.5. 115

5. inode12 (
).
6. (file_2)
inode12. inode 14.
7. inode14 . .
,
inode14.
, ,
, ,
, . , ,
(.)
(..), .
.

4.5.1. inode
,
ls-i. , .
stat.
$ ls -i
12 dir_1 7633 dir_2
, , .
ls-l. ,
.4.5, , , file_5?
(
), inode.
1, .
:
inode.
15 : dir_1/file_3,
dir_2/file_5.
, inode.
ln ( -s) .
.
rmdir_1/file_2, file_2
inode12. , file_2 inode14,
, 1
inode14. 0
, inode .
, .
, rmdir_1/file_3, ,
inode15 2 1 (
dir_2/file_5 ) ,
.
116 4.

. ,
inode12 2,
: dir_1 inode2,
(.) .
dir_1/dir_3, inode12
3, (..),
inode12, ,
inode12 inode2.
. inode2
4. .4.5
. ,
, inode.
.
ls-i stat
.
( ).
:
,
, ?
,
.
,
. 0, , , 1,
. ,
.
,
inode
; ,
. (
4.2.11) fsck inode
,
( ),
.
, fsck ,
inode / ,
. fsck
lost+found.

4.5.2.


, . ,

. ,
4.5. 117

,
, ,
stat() inode .
,
inode ? , .

. , Linux,
. VFS ,
inode
, - .

Unix . ,
ln,
VFAT,
.
, Unix/Linux,

: - , ,
. ,

.
,
.
.

4.5.3.

, .
,
.
,
,
. Btrfs
(. 4.2.1).
, ,

, inode, .
- . ,
, SSD.
, ,
.
5
Linux
Linux,
. ,
, ,
, .
.
1. BIOS
.
2. ,
.
3. .
4. .
5. init 1.
.
6. init .
7. init ,
.
.
,
. 6
.

, ,
.
Linux ( )
, , ,
, .

5.1.
Unix
, .
5.1. 119

, ,
init.
, .
Linux
, . ,
, ,
, ,
, .

. :
. /var/log/
kern.log,
/var/log/messages
- ;
dmesg, less,
, .
dmesg , ,
,
.
, dmesg:
$ dmesg
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Linux version 3.2.0-67-generic-pae (buildd@toyol) (gcc version 4.
6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #101-Ubuntu SMP Tue Jul 15 18:04:54 UTC
2014
(Ubuntu 3.2.0-67.101-generic-pae 3.2.60)
[ 0.000000] KERNEL supported cpus:
--snip--
[ 2.986148] sr0: scsi3-mmc drive: 24x/8x writer dvd-ram cd/rw xa/form2 cdda
tray
[ 2.986153] cdrom: Uniform CD-ROM driver Revision: 3.20
[ 2.986316] sr 1:0:0:0: Attached scsi CD-ROM sr0
[ 2.986416] sr 1:0:0:0: Attached scsi generic sg1 type 5
[ 3.007862] sda: sda1 sda2 < sda5 >
[ 3.008658] sd 0:0:0:0: [sda] Attached SCSI disk
--snip--


. ,
.
, , ,
. ,
.
init, Upstart systemd,
,
.
120 5. Linux

5.2.

Linux
.
1. .
2. .
3. .
4. .
5. ( ..).
6. .
7. .
, ,
, . ,
SCSI.

init. ,
, ,
, .

.
6.8.
-
, .

, ,
:
Freeing unused kernel memory: 740k freed
Write protecting the kernel text: 5820k
Write protecting the kernel read-only data: 2376k
NX-protecting the kernel data: 4420k
,
.

6, -
init, .
.

5.3.
Linux ,
, .
5.4. 121

, , ,
,
.
/proc/
cmdline:
$ cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-3.2.0-67-generic-pae root=UUID=70ccd6e7-6ae6-44f6-
812c-51aab8036d29 ro quiet splash vt.handoff=7

, ro quiet,
key=value, vt.handoff=7.
, splash,
. root.
,
init, , .
,
:
root=/dev/sda1


UUID (. 4.2.4):
root=UUID=70ccd6e7-6ae6-44f6-812c-51aab8036d29

ro ; ,

.
fsck.
-
.
- Linux
, init
. , -s,
init, ,
.
, .

5.4.
, init,
. :
, .
, .
?
?
122 5. Linux

( ) :
. , ,
,
. , ,
, .
.
.
BIOS UEFI,
.
, BIOS
LBA (Linear Block Addressing).
,
.
, BIOS ;
.
.
,
. ,
.
.
Linux ;
.

5.4.1.
Linux
:
;
;

(,
);
.

Linux. , ,
.
, .
, ,
USB-, , ,
,
.
, - .
,
.
5.5. GRUB 123

5.4.2.
,
, :
GRUB Linux;
LILO Linux. ELILO
UEFI;
SYSLINUX
;
LOADLIN MS-DOS;
efilinux UEFI,
UEFI;
coreboot ( LinuxBIOS)
BIOS . ;
Linux Kernel EFISTUB
ESP (EFI/UEFI System Partition),
.
GRUB.

, GRUB, .
, ,
. ,
, Linux -
.
,
, . ,
.

5.5. GRUB
GRUB Grand Unified Boot Loader ( -
). GRUB2;
GRUB Legacy, .
GRUB
,
.
GRUB .
, ,
. Linux
.
GRUB, Shift
, BIOS .

124 5. Linux

. .5.1 GRUB.
Esc,
.

.5.1. GRUB

.
1. Linux.
2. Shift
BIOS / ,
GRUB.
3. E,
. -
.5.2.
,
UUID, /boot/vmlinuz-
3.2.0-31-generic-pae, ro, quiet splash.
/boot/initrd.img-3.2.0-31-generic-pae.
,
. root,
? insmod?
udevd?
, GRUB
Linux . , ,
GRUB.
.
5.5. GRUB 125

.5.2. GRUB

, GRUB
.
insmod, GRUB,
Linux. GRUB
Unix; ls .
- root.
,
:
.
GRUB
- linux. root
GRUB,
. GRUB
,
.
.5.2 GRUB
(hd0, msdos1).
UUID .
,
.
linux (/boot/vmlinuz-...)
Linux. GRUB
. initrd ,
.
126 5. Linux

GRUB;
.
, (.
5.5.2),
GRUB .

5.5.1.
GRUB
.5.2, GRUB
. , hd0,
hd1 .. .
, GRUB UUID
, , ,
search.


, GRUB
, ,
C .
GRUB:
grub>
, ,
ls. ,
, GRUB:
grub> ls
(hd0) (hd0,msdos1) (hd0,msdos5)
,
(hd0), (hd0,msdos1) (hd0,msdos5). msdos
, MBR;
GBT gpt.
,
; , , ,
.
, ls-l.
,
UUID . , :
grub> ls -l
Device hd0: Not a known filesystem - Total size 426743808 sectors
Partition hd0,msdos1: Filesystem type ext2 Last modification time
2015-09-18 20:45:00 Friday, UUID 4898e145-b064-45bd-b7b4-7326b00273b7 -
Partition start at 2048 - Total size 424644608 sectors
Partition hd0,msdos5: Not a known filesystem - Partition start at
424648704 - Total size 2093056 sectors
5.5. GRUB 127

Linux ext2/3/4
MBR, 5,
.
, (hd0,msdos5) .


GRUB.
echo (,
):
grub> echo $root
hd0,msdos1
ls GRUB
,
:
grub> ls (hd0,msdos1)/
,
root:
grub> ls ($root)/

, etc/, bin/ dev/. ,
ls GRUB:
, , ,
. .
.
, /boot, :
grub> ls ($root)/boot

, GRUB,
, . (Ctrl+N,
Ctrl+P ..) .


set:
grub> set
?=0
color_highlight=black/white
color_normal=white/black
--snip--
prefix=(hd0,msdos1)/boot/grub
root=hd0,msdos1
$prefix,
, GRUB
128 5. Linux

.
.
GRUB
boot, , Esc
. :
GRUB, ,
.

5.5.2. GRUB
GRUB
(grub.cfg) .mod.
GRUB
, i386-pc. , , /boot/grub /boot/grub2.
grub.cfg,
grub-mkconfig grub2-mkconfig Fedora.

Grub.cfg
grub.cfg, , GRUB
. , grub.cfg
GRUB,
,
. :
, :
if loadfont /usr/share/grub/unicode.pf2 ; then
set gfxmode=auto
load_video
insmod gfxterm
--snip--
,
menuentry.
, ,
:
menuentry 'Ubuntu, with Linux 3.2.0-34-generic-pae' --class ubuntu --class gnu-linux
--class gnu
--class os {
recordfail
gfxmode $linux_gfx_mode
insmod gzio
insmod part_msdos
insmod ext2
set root='(hd0,msdos1)'
search --no-floppy --fs-uuid --set=root 70ccd6e7-6ae6-44f6-812c-
51aab8036d29
linux /boot/vmlinuz-3.2.0-34-generic-pae root=UUID=70ccd6e7-6ae6-44f6-
812c-51aab8036d29
5.5. GRUB 129

ro quiet splash $vt_handoff


initrd /boot/initrd.img-3.2.0-34-generic-pae
}
submenu. grub.cfg
menuentry, , ,
submenu. ,
GRUB.


GRUB,
grub.cfg ,
.
- , grub-mkconfig,
.
, ,
grub.cfg. , :
### BEGIN /etc/grub.d/00_header ###
, , /etc/grub.d
, grub.cfg.
grub-mkconfig , ,
/etc/grub.d.
.
, :
.
# grub-mkconfig
,
GRUB?
custom.cfg
, /boot/grub/custom.cfg.
. /etc/grub.d
: 40_custom 41_custom. , 40_custom,
, , , ,
: .
41_custom . ,
custom.cfg GRUB. , ,
, , .

. /etc/grub.d
. , , Ubuntu
(memtest86+).
GRUB,
GRUB, -o
grub-mkconfig:
# grub-mkconfig -o /boot/grub/grub.cfg
130 5. Linux

Ubuntu, install-grub.
, ,
, .

GRUB . ,
6.

5.5.3. GRUB
GRUB
. , , , ,
.
,
,
.
, 5.8.3,
, , ,
: MBR EFI. GRUB ,
; /boot/grub.
GRUB,
. , 16, ,
. ,
: MBR UEFI
( 32- 64- EFI).

GRUB
,
.
GRUB
. /boot/grub, ,
GRUB
.
GRUB.
UEFI
UEFI.
, GRUB ,
,
GRUB. GRUB,
,
( grub.cfg)
. Linux , GRUB
ext2.mod.
, ,
/boot/grub.
5.5. GRUB 131

GRUB grub-install (
install-grub Ubuntu),
GRUB . ,
/dev/sda
GRUB /boot/grub,
GRUB MBR:
# grub-install /dev/sda

GRUB
, . , -
, MBR dd,
GRUB ,
.

GRUB

GRUB ,
,
. , /dev/sdc,
/ (, /dev/sdc1)
/mnt . ,
GRUB
/mnt/boot/grub. grub-install ,
, :
# grub-install --boot-directory=/mnt/boot /dev/sdc

GRUB UEFI
, UEFI ,
. ,

efibootmgr. grub-install , ,
UEFI
:
# grub-install --efi-directory=efi_dir -bootloader-id=name

efi_dir UEFI,
( /boot/efi/efi, UEFI
/boot/efi), name
, 5.8.2. UEFI.
, UEFI
. , ,
, ,
.
.
132 5. Linux


UEFI.

5.6. UEFI
, Linux,
,
. , UEFI ,

. Microsoft ,
Windows8 . ,
(
Linux ), .
, Windows,
EFI.
,
. Linux
.
GRUB,
( );
( efilinux).

5.7.

UEFI
, EFI
. MBR ,
UEFI
, MBR. ,
GRUB
.

GRUB ( Grub.cfg
5.5.2). Windows:
menuentry "Windows" {
insmod chain
insmod ntfs
set root=(hd0,3)
chainloader +1
}
+1 chainloader , ,
.
5.8. 133

, , io.sys
MS-DOS:
menuentry "DOS" {
insmod chain
insmod fat
set root=(hd0,3)
chainloader /io.sys
}

5.8.
.
, .
, , GRUB,
, , .

, : MBR UEFI.

5.8.1. MBR
, 4.1, MBR
(Master Boot Record, )
(441), BIOS POST (Power-
On Self-Test, ). ,
, ,
. , -
. MBR
, .
MBR
.
,
. , GRUB,
. , GPT
BIOS , GPT
MBR. GPT
MBR .
GPT
BIOS UUID,
. GPT
UEFI, BIOS,
UEFI.

5.8.2. UEFI
, ,
, BIOS ,
134 5. Linux

(EFI,
Extensible Firmware Interface). ,
, .
UEFI (Unified EFI),
, ,
. GPT UEFI.
UEFI ,
. ,
,
EFI (ESP, EFI System Partition),
efi.
, efi/microsoft, efi/apple efi/grub.
.efi
.

ESP BIOS, 5.8.1,


UUID.

:
ESP,
BIOS. ,
UEFI. , GRUB
UEFI- GRUB, BIOS-.
, .
5.6, .

5.8.3. GRUB
GRUB, ,
.
1. BIOS

.
2. , BIOS
. GRUB.
3. GRUB.
4. . GRUB
.
5. GRUB
.
6. GRUB .
7.
GRUB ( ,
5.5.2).
5.8. 135

8. GRUB
() .
9. GRUB boot, ,
linux .
3 4 , GRUB,

. :
GRUB? :
MBR ;
;
: GPT,
EFI (ESP) -.
, ESP, BIOS
512 MBR, GRUB.
( boot.img
GRUB) , ,
.
ESP GRUB
. ESP
GRUB ,
.
.

.
initrd 6.8.
,
. .
6


, , init,
,
, ,
, .

,
.
. ,
.
,
.
, .
1. init.
2. , udevd syslogd.
3. .
4. (, ..).
5. ,
.

6.1. init
init ,
Linux. /sbin
.
,
.
Linux init.
System V init. init (SysV,
sys-five). Red Hat Enterprise Linux
.
6.1. init 137

systemd. init.
systemd, , ,
.
Upstart. init Ubuntu.
Ubuntu systemd.
init,
. , Android
init. BSD init,
Linux.
System V init , BSD.
init , System V init
,
.
,
,
.
, :

,
init. systemd Upstart
, ,
. -.
systemd . ,
, ,
. systemd
. -
, .
Upstart . ,
, , , ,
, Upstart , ..
systemd Upstart
. init ,
. -,
. PID
, ps -
, . , Upstart systemd
,
.
init ,
. ,
System V init ,
, .
systemd Upstart,
, .
138 6.

, , systemd Upstart,
. ,
( System V init),
. ,
inetd,
.
systemd Upstart
System V. , .

6.2. SystemV
Linux
( crond udevd). System V init
0 6.
,
, init ,
.
who-r.
, Upstart, - :
$ who -r
run-level 2 2015-09-06 08:37
, 2.
.
,
,
. , , Fedora
2 4 ; 5
,
.
.
init, , ,
systemd Upstart
. systemd Upstart
,
System V init.
, init,
, , .

6.3. init
, , init
. ,
.
6.4. systemd 139

/usr/lib/systemd /etc/systemd,
systemd. 6.4.
/etc/init .conf, ,
, Upstart ( Debian7:
, , System V init). 6.5.
, /etc/
inittab, , System V init.
6.6.
, init(8)
init.

6.4. systemd
systemd init init
Linux. , systemd
Unix, cron inetd.
launchd Apple.

, .
systemd , ,
. , ,
systemd.
1. systemd .
2. systemd , defa
ult.target.
3. systemd ,
..
4. systemd .
5. systemd
( uevents) .
systemd
. init,
systemd .
systemd
,
.

6.4.1.
systemd ,
,
, , .
,
. - , .
140 6.

(
systemd(1)), ,
Unix.
. Unix.
. .
. , .
,
.
,
.
systemctldot. , ,
.
.6.1 default.target,
Fedora.
.

.6.1.

6.4.2. systemd
,
,
. , ,
,
.
,
6.4. systemd 141

, ,
.
Unix
.
, ,
/etc/fstab ,
.
.
,
systemd .
,
6.4.3. .
Requires. .
Requires systemd .
, systemd
.
Wants. , .
- systemd Wants-
, , .
Requisite. , .
Requisite systemd
. ,
systemd .
Conflicts. .
Conflict systemd
, .
.

Wants , -
. systemd , -
. ,
, init.

. ,
Wants- ,
Wants .
WantedBy- .
RequiredBy. ( )
By ,
(. [Install] 6.4.3).
- sys
temctl. , Wants
Requires:
# systemctl show -p type unit
142 6.


, ,
.
Requires Wants , systemd
. ,
,
, . ,
. , ,
.6.1, default.target multi-
user.service ( ).
,
.
Before.
. , foo.target Before=bar.target,
systemd foo.target bar.target.
After.
.



systemd. :
ConditionPathExists=p: , () p ;
ConditionPathIsDirectory=p: , p ;
ConditionFileNotEmpty=p: , p .
, systemd
, .
, . ,
, ,
, systemd ,
.
.
, RequiresOverridable Requires,
, Wants,
.
systemd.unit(5) .

systemd, , .

6.4.3. systemd
systemd ,
, , .
systemd:
6.4. systemd 143

( , /usr/lib/systemd/
system) ( ,
/etc/systemd/system).
:
, .
. ,
- /usr /etc,
/etc.

systemd ( )
:
# systemctl -p UnitPath show
pkg-config.
, :
$ pkg-config systemd -variable=systemdsystemunitdir
$ pkg-config systemd --variable=systemdsystemconfdir


XDG (
.desktop,
.ini Microsoft),
([]),
().
media.mount /usr/lib/systemd/
system, Fedora.
tmpfs, /media
.
[Unit]
Description=Media Directory
Before=local-fs.target

[Mount]
What=tmpfs
Where=/media
Type=tmpfs
Options=mode=755,nosuid,nodev,noexec
. [Unit]
. ,
, local-fs.target.
[Mount] ,
,
, 4.2.6. What=
UUID ,
. tmpfs,
144 6.

.
systemd.mount(5) .
. ,
sshd.service :
[Unit]
Description=OpenSSH server daemon
After=syslog.target network.target auditd.service

[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStartPre=/usr/sbin/sshd-keygen
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
, [Service]
, , ,
. systemd.service(5) (
systemd.exec(5)) .
6.4.6.

[Install]
[Install] sshd.service ,
, WantedBy RequiredBy
systemd.
- .
systemd [Install]. ,
sshd.service . -
, systemd [Install];
sshd.service , systemd
WantedBy multi-user.target. systemd

sshd.service:
ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.
target.wants/sshd.service'
, ,
( multi-user.target).
[Install] .wants .requires
(/etc/systemd/system). .wants
(/usr/lib/systemd/system),
, [Install],
.
, ,
(, ).
6.4. systemd 145

.
systemd, ,
. .
[Install],
systemctlenable; -
. systemctlstart,
. , .


sshd.service ,
$OPTIONS $MAINPID ,
systemd. $OPTIONS ,
sshd systemctl ,
$MAINPID (.
6.4.6).
,
.
(%). , %n ,
%H .

. -
, - ,
getty, tty1, tty2 .. ,
@ . getty getty@.service,
, getty@tty1 getty@tty2. ,
@, , systemd -
%I . getty@.service,
, systemd.

6.4.4. systemd
systemd
systemctl, ,
, .

. ,
, list-units.
systemctl, list-units :
$ systemctl list-units
Unix. , ,
media.mount:
UNIT LOAD ACTIVE SUB JOB DESCRIPTION
media.mount loaded active mounted Media Directory
,
, ,
146 6.

systemctl .
, --full,
( ) --all.
systemctl
. , ,
:
$ systemctl status media.mount
media.mount - Media Directory
Loaded: loaded (/usr/lib/systemd/system/media.mount; static)
Active: active (mounted) since Wed, 13 May 2015 11:14:55 -0800;
37min ago
Where: /media
What: tmpfs
Process: 331 ExecMount=/bin/mount tmpfs /media -t tmpfs -o
mode=755,nosuid,nodev,noexec (code=exited, status=0/SUCCESS)
CGroup: name=systemd:/system/media.mount
, ,
init. ,
, ,
PID, .

. ,
systemd:/system/media.mount,
. , , NetworkManager.service,
.
systemd-cgls.
6.4.6.

(
).
:
$ journalctl _SYSTEMD_UNIT=unit
, journalctl
systemd.
,
systemd start, stop restart.
, :
systemctl reload unit unit;
systemctl daemon-reload .
,
systemd, , ,
.
:
$ systemctl list-jobs
6.4. systemd 147

, ,
,
.
,
, . :
JOB UNIT TYPE STATE
1 graphical.target start waiting
2 multi-user.target start waiting
71 systemd-...nlevel.service start waiting
75 sm-client.service start waiting
76 sendmail.service start running
120 systemd-...ead-done.timer start waiting
76, sendmail.service,
.
, , , 76.
sendmail.service , 76
, .

, init, Upstart, -
, ( ) ,
systemd . , , systemd,
- , ,
, .

6.7, ,
.

6.4.5. systemd
,
.
/etc/systemd/system,
-, ,
.
,
, .
, .
1. test1.target:
[Unit]
Description=test 1
2. test2.target test1.target:
[Unit]
Description=test 2
Wants=test1.target
148 6.

3. test2.target ( ,
test2.target systemd
test1.target):

# systemctl start test2.target


4. , :
# systemctl status test1.target test2.target
test1.target - test 1
Loaded: loaded (/etc/systemd/system/test1.target; static)
Active: active since Thu, 12 Nov 2015 15:42:34 -0800; 10s ago
test2.target - test 2
Loaded: loaded (/etc/systemd/system/test2.target; static)
Active: active since Thu, 12 Nov 2015 15:42:34 -0800; 10s ago

[Install], :
# systemctl enable unit
. test2.target
[Install] test1.target, WantedBy=test2.target.

. , .
1. , :
# systemctl stop unit
2. [Install], ,
:
# systemctl disable unit
3. , .

6.4.6.
systemd
systemd
. ,
,

.
,
, systemd
(cgroups) Linux,
.
Upstart, ,
, ,
6.4. systemd 149

- . systemd
, , , .
Type ,
. .
Type=simple .
Type=forking , systemd
. systemd ,
.
Type=simple ,
, systemd ,
, .
(.
6.4.7). Type ,
systemd :
Type=notify ,
systemd ( sd_notify());
Type=dbus , D-Bus (
).
Type=oneshot.
.
RemainAfterExit=yes,
systemd -
.
, : Type=idle. systemd
, .
,
,
. : ,
systemd, , .

6.4.7.
systemd
systemd
, .
.
1. systemd ( )
, .
2. (, /,
), .
3. systemd, R,
. : ,
.
150 6.

.
1. R systemd .
2. - , systemd
, .
3. systemd A.
4. A , ,
.
, .
, ,
. ,
.
, ,
. .
,
systemd .
, ,
systemd.
, inetd, xinetd
automount, . ,
( systemd automount).

. ,
.


systemd

. ,
, ,
systemd ,
.
,
, syslog dbus ,
. systemd
(, ) ,
,
, .
, .
.6.2 ,
. E
R. A, B C
E.
, .
6.4. systemd 151

.6.2. .

.6.3
systemd. A, B C, R
, E. systemd
R, E, A,B,
C E . E
R, . , A,
B C R
B.

,
.

,
, , .
, syslog
D-Bus , systemd; ,
.


-,
. , ,
, 9 10,
TCP, , .
.
152 6.

.6.3. systemd

, , , ,
, , .
TCP 22222. -
, echo.socket:
[Unit]
Description=echo socket

[Socket]
ListenStream=22222
Accept=yes
, ,
.
?
echo@.service.
: ,
.socket ( echo), systemd ,
, .
systemd echo@.service,
echo.socket.
echo@.service:
[Unit]
Description=echo service

[Service]
6.4. systemd 153

ExecStart=-/bin/cat
StandardInput=socket


,
, . , ,
Socket=bar.socket foo.service, bar.socket -
foo.service.

,
echo.socket:
# systemctl start echo.socket
, 22222.
telnet , -
Enter. .
$ telnet localhost 22222
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hi there.
Hi there.
, Ctrl+] ,
Ctrl+D. ,
:
# systemctl stop echo.socket


echo@.service
, @ (,
). ?
, ,
,
.

, echo.socket Accept.
systemd, ,
, ,
.
,
, .


/, , -
, echo@.service.
154 6.

,
@.
, systemd
, .

.
. ,
, systemd.socket(5), systemd.path(5) systemd.device(5).
systemd.exec(5),
. ,
.

6.4.8. systemd
SystemV
, systemd init-
, ,
, ,
SystemV. .
1. systemd runlevel<N>.target, N
.
2. /etc/rc<N>.d systemd
/etc/init.d.
3. systemd (,
/etc/init.d/foo foo.service).
4. systemd
start stop, rc<N>.d.
5. systemd
.
systemd ,
systemctl
. SystemV.
- init- .

6.4.9., systemd
systemd,
/lib/systemd.
. , udevd systemd,
systemd-udevd. , systemd-fsck,
systemd fsck.
,
, .
systemd .
6.5. Upstart 155

fsck
systemd.

, , -
systemd .
, ( -
), , -
, .

/lib/systemd,
. ,
, , .
( ) Upstart,
6.6, init- SystemV.

6.5. Upstart
init Upstart .
, Upstart
(, ), ,
Upstart (, udevd).
Upstart .
, , udev
udevd. /etc/init/
udev.conf, :
start on virtual-filesystems
stop on runlevel [06]
, Upstart udev
virtual-filesystems,
runlevel 0 6.
. ,
Upstart ,
, started udev, udev,
. ,
, Upstart .

6.5.1. Upstart
Upstart .
1. ,
/etc/init.
2. startup.
3. ,
startup.
156 6.

4. ,
.
, ,
Upstart
.
Upstart .
1. , Upstart
startup, mountall.
,
.
2. mountall ,
filesystem, virtual-filesystems, local-filesystems, remote-filesystems
all-swaps. ,
.
3. Upstart
. , udev virtual-filesystems,
dbus local-filesystems.
4. Upstart
network-interfaces, , local-filesystems
udevd.
5. network-interfaces static-network-up.
6. Upstart rc-sysinit filesystem
static-network-up.
,
, runlevel.
7. Upstart
runlevel .
, ,
. Upstart ,
. ,
, ,
, , .
, . , ,
static-network-up. network-interface.conf
, , . ,
ifup,
/etc/network/if-up.d/upstart.

( interfaces(5)
ifup.d ifup(8)), ,
, . grep, -
, , , -
.
6.5. Upstart 157

Upstart
. ,
( ,
/var/log/syslog),
.

6.5.2. Upstart
/etc/init Upstart
- ,
.conf. , /etc/init/mountall.conf
mountall.
Upstart.
(Task jobs). . ,
mountall ,
.
(Service jobs). .
(), udevd, -,
-.
.
.
Upstart ,
,
,
.


Upstart,
initctl. , ,
:
$ initctl list
,
, .
:
mountall stop/waiting
, mountall /,
, . ( ),
, ,
, /
.
, ,
:
tty1 start/running, process 1634
158 6.

, tty1
ID 1634 . .

job,
initctl status job.

initctl (,
stop/waiting) . ( /) ,
, (,
). ,
(, ).
tty1 start/running,
, . ,
.
.
mountall ,
. / ,
.
,
Upstart.
, , ,
, /, ,
- ,
, 6.5.5.

, Upstart
SystemV.


,
. , , .
1. /.
2. ,
.
3. Upstart ,
/ (start/starting).
4. Upstart starting job.
5. , .
6. Upstart
pre-start job.
7.
, .
8. Upstart started.
6.5. Upstart 159

.
upstart-events(7),
.

6.5.3. Upstart
: mountall,
tty1. ,
/etc/init mountall.conf tty1.conf.
, .
, description start.
mountall.conf.
:
description "Mount filesystems on boot"

.
,
mountall:
start on startup
stop on starting rcS

Upstart
startup ( , Upstart).
Upstart ,
rcS, .
Upstart ,
mountall:
expect daemon
task

task Upstart , ,
- . expect .
, mountall ,
. Upstart
, ,
mountall.
expect Upstart .
mountall.conf emits,
, :
emits virtual-filesystems
emits local-filesystems
emits remote-filesystems
emits all-swaps
emits filesystem
emits mounting
emits mounted
160 6.

6.5.1, -
. , .

console, ,
Upstart:
console output
output, Upstart mountall
.

script:
script
. /etc/default/rcS
[ -f /forcefsck ] && force_fsck="--force-fsck"
[ "$FSCKFIX" = "yes" ] && fsck_fix="-fsck-fix"

# set $LANG so that messages appearing in plymouth are translated


if [ -r /etc/default/locale ]; then
. /etc/default/locale
export LANG LANGUAGE LC_MESSAGES LC_ALL
fi

exec mountall --daemon $force_fsck $fsck_fix


end script
(. 11),
: ,
fsck.
execmountall, .
.

: tty1
tty1 ,
. tty1.conf :
start on stopped rc RUNLEVEL=[2345] and (
not-container or
container CONTAINER=lxc or
container CONTAINER=lxc-libvirt)

stop on runlevel [!2345]

respawn
exec /sbin/getty -8 38400 tty1
,
, container,
:
start on stopped rc RUNLEVEL=[2345]
6.5. Upstart 161

Upstart,
stoppedrc,
rc. , rc
RUNLEVEL 2 5 (. 6.5.6).

, , . -
, , : start on runlevel [2345].
start ; -
, , -
SystemV.

, Upstart
Linux
, .
,
getty .
tty1 :
stop on runlevel [!2345]
stop Upstart , ,
2 5 (,
).
exec , :
exec /sbin/getty -8 38400 tty1
script,
mountall, , tty1
.
getty /dev/tty1,
(, ,
Ctrl+Alt+F1).
respawn Upstart tty1,
. Upstart
getty , .
Upstart.
init(5), -.
. expect, .

expect
Upstart
Upstart ,
( ),
, .
, Unix
, ,
- - .
162 6.

Upstart
PID .
Upstart expect.
:
expect ,
;
expect fork ,
;
expect daemon ,
;
expect stop SIGSTOP,
. .
Upstart init,
systemd, (
expect),
. ,

, Unix
.

, , .
sshd -D.
/etc/init/ssh.conf,
sshd,
:
respawn
respawn limit 10 5
umask 022

# 'sshd -D' leaks stderr and confuses things in conjunction with 'console log'
console none
--snip--

exec /usr/sbin/sshd -D

, expect,
expectfork. , ,
/etc/init/cron.conf:
expect fork
respawn

exec cron

, ,
.
6.5. Upstart 163

expect upstart.ubuntu.com,
. , -
, , fork(),
strace.

6.5.4. Upstart
list status, 6.5.2,
initctl, Upstart
. initctl(8),
.
Upstart, initctlstart:
# initctl start job
, initctlstop:
# initctl stop job
:
# initctl restart job
Upstart,
:
# initctl emit event
,
event key=value.

,
Upstart SystemV. 6.6.1 ,
System V init.

Upstart,
, :
( /etc/init/<job>.conf),
/etc/init/<job>.override,
:
manual

initctlstartjob.
.
, .override.

6.5.5. Upstart
Upstart:
, Upstart.
164 6.

,
. , /var/log/upstart,
,
syslog ( syslog 7).
, , .
,
. ,
syslog .
Upstart
, ,
.
syslog. , , /var/log/kern.log,
/var/log/syslog.
Upstart
, , - ,
Upstart.
message.
, info:
# initctl log-priority info
,
. Upstart
, --verbose,
5.5.

6.5.6. Upstart
SystemV
,
Upstart SystemV, ,
SystemV .
, Ubuntu.
1. rc-sysinit filesystem
static-network-up. .
2. rc-sysinit , . ,
,
/etc/inittab
( /proc/cmdline).
3. rc-sysinit telinit, .
runlevel,
RUNLEVEL.
4. Upstart runlevel.
runlevel
, Upstart .
6.6. System V init 165

5. , , rc,
SystemV. , rc
/etc/init.d/rc, SystemV init
(. 6.6).
6. rc Upstart ,
stoppedrc (, tty1,
: tty1 6.5.3).
, , Upstart
, ,
Upstart .
,

.
, .
runlevel , .
Upstart .
,
Upstart, SystemV.

SystemV (. 6.6.2). , 2,
/etc/rc2.d. , , , ,
SystemV.

/etc/init.d.
Upstart SystemV,
, ,
Upstart.
SystemV. , Upstart,
initctl .

6.6. System V init


System V init Linux.


. SystemV
, System V init Red Hat Enterprise Linux,
Linux (, ).
System V init :
,
.
/etc/inittab. System V init,
, , inittab:
id:5:initdefault:
166 6.

, 5.
inittab ,
:
( , id );
( );
, init (
5 );
().
, inittab,
:
l5:5:wait:/etc/rc.d/rc 5
,
. wait ,
System V init :
/etc/rc.d/rc5 5, , -
, . ,
rc 5 , /etc/rc5.d (
).
inittab
initdefault and wait.

respawn
respawn init ,
, , .
, , inittab:
1:2345:respawn:/sbin/mingetty tty1
getty .
(/dev/tty1), ,
Alt+F1 Ctrl+Alt+F1 (. 3.4.4).
respawn , .

ctrlaltdel
ctrlaltdel , ,
Ctrl+Alt+Delete .
, shutdown (
6.7).

sysinit
sysinit ,
- .

inittab(5).
6.6. System V init 167

6.6.1. System V init:



, System V init ,
.
inittab:
l5:5:wait:/etc/rc.d/rc 5
.
rc run commands ( ),
, .
?
5 , 5.
, /etc/rc.d/rc5.d /etc/rc5.d.
1 rc1.d, 2 rc2.d .. ,
rc5.d:
S10sysklogd S20ppp S99gpm
S12kerneld S25netstd_nfs S99httpd
S15netstd_init S30netstd_misc S99rmnologin
S18netbase S45pcmcia S99sshd
S20acct S89atd
S20logoutd S89cron
rc5 rc5.d,
:
S10sysklogd start
S12kerneld start
S15netstd_init start
S18netbase start
--snip--
S99sshd start
start . S
, ,
( 00 00) rc .
rc*.d ,
/sbin /usr/sbin.
, ,
less .

rc*.d , K ( ).
rc stop start. -
K , .

.
rc*.d, init.d,
.
168 6.

6.6.2. System V init


rc*.d
, , init.d.
rc*.d, ,
, .
, rc5.d, :
lrwxrwxrwx . . . S10sysklogd -> ../init.d/sysklogd
lrwxrwxrwx . . . S12kerneld -> ../init.d/kerneld
lrwxrwxrwx . . . S15netstd_init -> ../init.d/netstd_init
lrwxrwxrwx . . . S18netbase -> ../init.d/netbase
--snip--
lrwxrwxrwx . . . S99httpd -> ../init.d/httpd
--snip--

,
, . Linu ,
.
, .


,
init.d. , - httpd
init.d/httpd start.
stop (httpdstop, ).


System V init
.
- init.d
. , .
,
rc*.d. : -
,
.
(_) , :
# mv S99httpd _S99httpd

, rc _S99httpd,
S K, -
.
, , ,
init.d,
rc*.d.
, ( 11
).
6.6. System V init 169


. ,
- .

.
.

6.6.3. run-parts
, System V init
init.d, Linux,
, System V init. run-parts,

.
,
ls , ,
.
,
.

. , Fedora
run-parts.
, Debian Ubuntu,
run-parts.
(, S[0-9]{2}
/etc/init.d),
.
SystemV .
run-parts;
. ,
,
.

6.6.4. System V init


init,
,
. System V init telinit.
, 3 :
# telinit 3
init
, inittab ,
.
, -
inittab, init
170 6.

.
:
# telinit q

telinits
(. 6.9).

6.7.
init .
,
init. Linux
shutdown.
shutdown.

. ,
:
# shutdown -h now

Linux
.
. -r -h.
.
.
now .
, . ,
, ,
, +n, n ,
shutdown, .
shutdown(8).
10, :
# shutdown -r +10

Linux shutdown ,
, . ,
now, shutdown /etc/nologin.
, ,
superuser.
, shutdown
init . systemd
; Upstart ;
System V init 0 6.
init
.
6.8. 171

1. init .
2. - , init
, TERM.
3. TERM , init KILL
.
4.
.
5. , .
6.
.
7.
sync.
8.
reboot(2). init
- reboot, halt poweroff.
reboot halt -, ,
. shutdown -r
-h. ,
,
. ,

, -f.

6.8.

Linux ,
: initramfs,
.
,
. , .

. , Linux
BIOS EFI ,
,
, . , ,
RAID,
,
. ,
, ,
.
, ,
172 6.

,
.

, .
.

(initramfs),
init initramfs.
initramfs
. ,

init.
.
init initramfs
, udevd ,
init. ,
systemd, ,
systemd,
udevd.

, () , ,
. ,
,
.
, , ,
. ,
GRUB initrd.
GRUB,
, .
,
, UUID,
.
,
cpio (.
cpio(1)), gzip.
, (, ,
grep initrd grub.cfg).
cpio, -
. :
$ mkdir /tmp/myinitrd
$ cd /tmp/myinitrd
$ zcat /boot/initrd.img-3.2.0-34 | cpio -i --no-absolute-filenames
--snip--
init
.
6.9. 173

(
) .
,
,
. ,
, -
.
dracut mkinitramfs.

(initramfs) ,
cpio .
, initrd, -
.
, cpio.
initrd
cpio. ( )
- initrd.

6.9.

- ,

(
) , SystemRescueCd,
.
:
;
;
, /etc/fstab /etc/passwd;
.

. ,
. System
V init 1;
-s.
,
.
,
.
( , ),
, .

.
7 :
,
,

/etc, .
,
.
,
, 4,
, 2. ,
:
,
;
( ),
;
,
;
.
, ,
. 9 ,
.

7.1. /etc
Linux /etc.
,
, Unix ,
/etc .
:
,
. , ,
, /etc/syslog.conf.
7.2. 175

, ,
.

/etc,
(/etc/init Upstart /etc/systemd systemd).
/etc - ,
ls -F /etc, ,
.
,

, /etc/grub.d.
/etc?
: ,
(/etc/passwd) (/etc/network),
/etc. ,
,
/etc. ,
, - ,
systemd /usr/lib/systemd.
,
.
.

7.2.

syslog. syslogd
, ,
- ,
.

7.2.1.
. -
, ,
. :
Aug 19 17:59:48 duplex sshd[484]: Server listening on 0.0.0.0 port 22.
Linux syslogd
rsyslogd, ,
. , ,
. ,
, ,
/var/log. ,
, , .
176 7. : , ,

/var/log
. ,
rsyslogd, .

7.2.2.
rsyslogd /etc/rsyslog.conf,
,
/etc/rsyslog.d.
rsyslog . :
- ($), .
, ,
( 7.1).
7.1. syslog
kern.* /dev/console
*.info;authpriv.none /var/log/messages
authpriv.* /var/log/secure,root
mail.* /var/log/maillog
cron.* /var/log/cron
*.emerg *
local7.* /var/log/boot.log
. ,
. :
. 7.1 ,
. , , /dev/console
, root
superuser, , *
, .
@host.


,
. (.
rsyslog.conf(5) ).
.
, 7.1, ,
kern, authpriv, mail, cron local7.
() , ,
.
(.) .
, : debug, info, notice, warning, err, crit, alert
emerg.

- , rsyslog.conf -
none, 7.1.
7.2. 177

, rsyslogd

. 7.1 *.info

/var/log/messages, info .


rsyslogd syslogd.

$.
. rsys
log.conf , .
.conf, /etc/rsyslog.d:
$IncludeConfig /etc/rsyslog.d/*.conf

. ,
:
$FileOwner syslog
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022

rsyslogd -
. , rsyslogd(5)
, - .



logger, :
$ logger -p daemon.info something bad just happened
rsyslogd . ,
,
.
Linux /var/log
logrotate , ,
, -
.

, rsyslogd, -
. 6 , -
systemd Upstart, ,
- Apache, , , .
, .
178 7. : , ,

:
syslog . , ,
klogd,
syslogd. dmesg.
rsyslogd.
Linux . Unix
.
.

7.3.
Unix .
(
ID), , , Linux
( ).
,
, , ,
ID,
.

7.3.1. /etc/passwd
/etc/passwd
ID. ( 7.2).
7.2. /etc/passwd
root:x:0:0:Superuser:/root:/bin/sh
daemon:*:1:1:daemon:/usr/sbin:/bin/sh
bin:*:2:2:bin:/bin:/bin/sh
sys:*:3:3:sys:/dev:/bin/sh
nobody:*:65534:65534:nobody:/home:/bin/false
juser:x:3119:1000:J. Random User:/home/juser:/bin/bash
beazley:x:143:1000:David Beazley:/home/beazley:/bin/bash
,
. .
.
. Linux
passwd, shadow
(. 7.3.3). shadow passwd,
shadow.
passwd shadow ,
, d1CVEWiB/oppc. Unix
.
x passwd ,
shadow. (*) ,
7.3. 179

, (
, ::), .
. .
(UID),
.
, , .
.
(GID)
/etc/group. - .
.
( GECOS).
, .
.
(,
).
.7.1 7.2.

.7.1.

/etc/passwd
.

, /etc/passwd,
.

7.3.2.
/etc/passwd .
superuser () UID 0 GID 0,
7.2. , ,
. nobody () .
nobody,
- .
, , -
. ,
. , nobody,
.
180 7. : , ,

7.3.3. /etc/shadow
shadow (/etc/shadow) Linux
,
,
/etc/passwd.
shadow ( )
. ,
P (. 7.10).
Linux, PAM
/etc/shadow, , /etc/login.defs.

7.3.4.
/etc/passwd
passwd. passwd ,
-f, ,
-s,
/etc/shells. chfn chsh
. passwd suid-root-,
superuser /etc/passwd.

/etc/passwd
superuser
/etc/passwd ,
superuser .
,
; ,
.
vipw,
/etc/passwd , .
/etc/shadow /etc/passwd,
vipw-s. , , .

passwd, .
( ) ,
.
, , passwd
user . adduser userdel
.

7.3.5.
Unix
,
.
7.3. 181

, - . ,
,
,
.
/etc/group ( ,
/etc/passwd file). ( 7.3).
7.3. /etc/group
root:*:0:juser
daemon:*:1:
bin:*:2:
sys:*:3:
adm:*:4:
disk:*:6:juser,beazley
nogroup:*:65534:
user:*:1000:
/etc/passwd, /etc/group
, .
.
. , , ls-l.
. ,
( sudo). *
.
(). GID
group.
/etc/passwd .
, .

,
passwd.
.7.2 group.

.7.2. group

, , groups.

Linux , -
.
182 7. : , ,

7.4. getty login


getty .
Linux getty ,
.
(, /dev/tty1):
$ ps ao args | grep getty
/sbin/getty 38400 tty1

38400
. getty .
;
,
.
getty
login, . , login
exec() .
.
, getty login, , ,
.
,
, gdm,
, SSH,
getty login.
PAM (. 7.10).

7.5.
Unix .
, , , date.
date, ,
, ,
. ,
.
(RTC, real-time
clock) . , ,
. RTC
,
hwclock.
UTC (Universal Coordinated Time,
), ,
. RTC
UTC- :
# hwclock --hctosys --utc
7.5. 183

, , RTC,
Unix
, .
(
- ).

hwclock, , ,
. adjtimex,
,
(.
7.5.2).

7.5.1.

,
1 1970 UTC.
, :
$ date +%s
,

, (
1). /etc/localtime.
, .
/usr/share/
zoneinfo.
. ,
/usr/share/zoneinfo /etc/localtime,
,
. tzselect .
,
,
TZ /usr/share/zoneinfo , ,
:
$ export TZ=US/Central
$ date
,
:
$ TZ=US/Central date

1
, , ,
- .
.
. .
184 7. : , ,

7.5.2.
,
NTP (Network Time Protocol, ),
.
NTP, .
ntpd, .
,
NTP (http://www.ntp.org/),
, .
1. NTP, -
ntp.org.
2. /etc/ntpd.conf.
3. ntpdate server.
4. ntpdate ntpd.
,
chronyd, , .
,
.
. ,
ntpdate ( ntpd),
, :
# hwclock --systohc -utc

7.6.
cron
cron Unix
. cron
,
. , ,
.
, cron, , , .
cron
. , cron, cron.
cron, crontab,
crontab.
, /home/juser/bin/spmake
9:15 :
15 09 * * * /home/juser/bin/spmake

(. .7.3). :
7.6. cron 185

( 0 59). cron
15- ;
( 0 23). 9- ;
( 1 31);
( 1 12);
( 0 7). 0 7 .

.7.3. crontab

(*) .
spmake , ,
, cron
, .
spmake 14- ,
crontab:
15 09 14 * * /home/juser/bin/spmake
. , ,
5- 14- , 5,14
:
15 09 5,14 * * /home/juser/bin/spmake

cron , ,
cron .
, /dev/null - .

crontab(5)
crontab.

7.6.1. crontab
crontab. ,
, , ,
/var/spool/cron/crontabs.
. crontab ,
, crontab.
186 7. : , ,

crontab -
, crontab file,
crontab. crontab ,
. cron,
crontab-l. crontab,
crontab-r.
, crontab,

. crontab
crontab-e. ,
crontab , ,
.

7.6.2. crontab

crontab superuser, Linux
/etc/crontab. crontab
, ,
, . ,
. , , cron,
/etc/crontab 6:42
(root, ):
42 6 * * * root /usr/local/bin/cleansystem > /dev/null 2>&1

crontab /etc/cron.d.
, , /etc/crontab.

7.6.3. cron
cron Linux,
( Linux).
.
.

init: systemd , Upstart

. init
; ,
.
, systemd, Upstart
cron. ,
,
, cron. cron
.
7.8. 187

7.7.
at
cron,
at. , myjob 22:30 ,
:
$ at 22:30
at> myjob

, Ctrl+D. at
.
, , atq.
, atrm.
, .., , : 22:3030.09.15.
at . ,
, ,
.

7.8.


, setuid- sudo su
,
login, . , ,

.
,
. setuid,
2.17.
setuid(). ,
,
, .
, ,
. .
, (userid 0),
setuid(), .
, ,
setuid();
.
setuid-,
.
188 7. : , ,

.
,
/etc/passwd 7.3.1. , ,
7.9.

, , -
.
.
.
(euid), .
, (ruid),
. setuid- Linux

,
.

,
, , .
-
, .
,
, , , ,
. , ,
B ( setuid),
A - .
Linux
. ps

. ,
, , ,
:
$ ps -eo pid,euser,ruser,comm

,
, : setuid-
sleep, ,
ps , .
,

( ).

. , Linux
(fsuid),
, .
.
7.8. 189

setuid. ,
, .

? , sudo
, sudo;
.
,
?
, sudo
setuid-
setuid().
,
,
.

, -
, setuid(2), ,
SEE ALSO (. ).
.


. sudo
, /etc/sudoers (
,
!):
Defaults stay_setuid
. Linux
( )
setuid ,

:
, setuid;
, .
bash,
setuid,
. . , ,
setuid, ,
. ,
, ,
.
,
.

.
190 7. : , ,

7.9.


. -
, . -
, ,
. , ,
.
, Linux
.
, ,
setuid setuid(),
.
: , .. ,
, .
7.3.1
. ,
. ,
(,
).
Unix - .
1.
geteuid().
2. /etc/passwd .
3. /etc/passwd. ,
.
4. ( ,
).
.
5. , , ,
. , ,
, ;
.
6. /etc/passwd
.
, .
.
,
, , ,
, . ,
,
. ,
7.10. PAM 191

getpwuid()
geteuid(). ,
, .
,
,
. , /etc/passwd
,
LDAP (Lightweight Directory Access Protocol, ()
() ).
,
, .
7.3.1 ,
/etc/passwd, ,
, , ,
/etc/passwd.
.
.
, .
,
, -,
.
, .
, -, -
,
.
, shadow-
, 7.3.3. ,
.

PAM.

7.10. PAM
, 1995
Sun Microsystems PAM (Pluggable
Authentication Modules, )
( Open
Source Software Foundation, 86.0, 1995).
PAM, ,
.
,
.
PAM
192 7. : , ,

(, , ,
cron).
, PAM
.
; , pam_unix.so
.
. , ,
PAM
. PAM
, Linux,
.
API , Unix,
( )
.

7.10.1. PAM
PAM, .
PAM /etc/pam.d (
/etc/pam.conf).
, , .
, ,
cron passwd.

, .
, chsh
( ):
auth requisite pam_shells.so
,
/etc/shells,
chsh. , .
: , .
.
. ,
PAM. auth,
.
. ,
PAM-
(requisite ). .
. ,
, . pam_shells.so ,
/etc/shells.
PAM pam.conf(5).
.
7.10. PAM 193


PAM-
:
auth (,
, );
account ( ,
, - );
session - (
, );
password
.

PAM-. ,
,
. , pam_unix.so
, auth, password,
.


PAM , ,
, , ,
.
:
,
.
:
.
, .
sufficient. ,
PAM- .
, PAM
.
requisite. , PAM-
. ,
PAM- .
required. , PAM-
. , PAM-
,
,
.
,
chsh:
194 7. : , ,

auth sufficient pam_rootok.so


auth requisite pam_shells.so
auth sufficient pam_unix.so
auth required pam_deny.so
, chsh
PAM- ,
(. - .7.4).
1. pam_rootok.so ,
. ,
. ,
sufficient,
, ,
PAM chsh.
.
2. pam_shells.so , /etc/shells.
, , requisite
, PAM chsh
. ,
/etc/shells, ,
required, .
3. pam_unix.so .
sufficient,
( ) PAM,
chsh. , PAM
.
4. pam_deny.so ,
required, PAM chsh.
,
. , required
PAM
,
.

, PAM.
: PAM
(, ). ,
, . , -
, PAM ,
.

,
([]), ,
, ( ).
. pam.conf(5) .
, .
7.10. PAM 195

.7.4. PAM


PAM .
pam_unix.so:
auth sufficient pam_unix.so nullok
nullok ,
( ).
196 7. : , ,

7.10.2. PAM

PAM-
.
, ,
PAM.
, PAM ,
mankpam_ ( ).
.
locateunix_pam.so , .

.

PAM, /etc/pam.d.
;
, , .
/etc/pam.d/other
,
. .

PAM. @include
, ,
.
.
PAM .
/etc/security,
, .

7.10.3. PAM
Linux
, . -,
/etc/login.defs.
shadow-. ,
shadow,
PAM, PAM
. , ,
/etc/login.defs, PAM
, ,
PAM.
PAM ?
, :
auth ( ) password (
7.11. 197

). , .
, , grep:
$ grep password.*unix /etc/pam.d/*
pam_unix.so ,
, :
password sufficient pam_unix.so obscure sha512
obscure sha512 PAM ,
. ,
(, , ),
SHA512, .
,
, PAM . ,
? ,
; pam_unix.so auth.
( ), pam_unix.so
,
libcrypt, ,
- . ,
.

7.11.
Linux,
.
Linux ,
,
.

,
. 8
.
8



,
.
: , /.
, .
,
, ,
.
, ,
. ,
.
:
, ,
. ,
,
, .

8.1.
2.16 , ps,
, . ps
, ,
. , ,
.
top ps,
, ,
ps, . ,
, top
( ,
) .
8.2. lsof 199

, top.
(.8.1).

8.1. top



M
T ()
P ( )
u
f
? top

Linux, top, atop htop


.
. , htop
lsof, .

8.2.
lsof
lsof , .
Unix , lsof
.

, , .

8.2.1. lsof
lsof
. , .
, init, vi:
$ lsof
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
init 1 root cwd DIR 8,1 4096 2 /
init 1 root rtd DIR 8,1 4096 2 /
init 1 root mem REG 8, 47040 9705817 /lib/i386-linux-gnu/libnss_files-
2.15.so
init 1 root mem REG 8,1 42652 9705821 /lib/i386-linux-gnu/libnss_nis-
2.15.so
init 1 root mem REG 8,1 92016 9705833 /lib/i386-linux-gnu/libnsl-2.15.so
--snip--
vi 22728 juser cwd DIR 8,1 4096 14945078 /home/juser/w/c
vi 22728 juser 4u REG 8,1 1288 1056519 /home/juser/w/c/f
--snip--
200 8.

( ).
COMMAND. , .
PID. .
USER. , .
FD. .
FD .
,
,
.
TYPE. ( , , ..).
DEVICE. ,
.
SIZE. .
NODE. inode .
NAME. .
lsof(1) ,
, , ,
. , , ,
FD cwd ( ).
.
: ,
vi.

8.2.2. lsof
lsof.
, less
, . ,
.
, lsof,
.
,
lsof ,
. ,
, /usr:
$ lsof /usr
, PID,
:
$ lsof -p pid
lsof
lsof-h. (. 10,
lsof).
8.3. 201

lsof . , -
, lsof. ,
, lsof, lsof
, .

8.3.

, , .
, -
, lsof .
, lsof ,
.
strace ( ) ltrace (
) , .
, , ,

.

8.3.1. strace
, ,
(
, ). strace
, . ,
:
$ strace cat /dev/null
1 , ,
, fork(),
, exec(),
. strace
( ) fork(). ,
execve() ,
, brk(), :
execve("/bin/cat", ["cat", "/dev/null"], [/* 58 vars */]) = 0
brk(0) = 0x9b65000

. ,
, .
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0xb77b5000
202 8.

access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)


open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
--snip--
open("/lib/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\200^\1"..., 1024)= 1024
, mmap ,
, :
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0
open("/dev/null", O_RDONLY|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
fadvise64_64(3, 0, 0, POSIX_FADV_SEQUENTIAL)= 0
read(3,"", 32768) = 0
close(3) = 0
close(1) = 0
close(2) = 0
exit_group(0) = ?
.
open(), . 3 ,
( ,
). , cat /dev/
null ( read(), 3).
,
exit_group().
, ?
stracecat not_a_file open()
:
open("not_a_file", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
open() , -1,
. , strace
.

Unix,
, , strace
. ,
. , :
$ strace -o crummyd_strace -ff crummyd
-o strace
, crummy crummyd_strace.pid,
pid .

8.3.2. ltrace
ltrace .
strace,
8.4. 203

, . :
, .
, ltrace
, .

. 15.1.4, . ltrace
.

8.4.
Linux , .
: (TID, ID ),
, .
,
,
/, -
.

8.4.1.
.
, .
.
. ,
,
fork() .

, .
, ,
.

:
,
, .
,
/

,
.
,
/.
- fork(),
.
.
204 8.

8.4.2.
ps top .
ps, m ( 8.1).
8.1. ps m
$ ps m
PID TTY STAT TIME COMMAND
3587 pts/3 - 0:00 bash
- - Ss 0:00 -
3592 pts/4 - 0:00 bash
- - Ss 0:00 -
12287 pts/8 - 0:54 /usr/bin/python /usr/bin/gm-notify
- - SL1 0:48 -
- - SL1 0:00 -
- - SL1 0:06 -
- - SL1 0:00 -
8.1 .
PID ( , )
ps. PID
, .
, 12287 ()
.
ps,
. 8.2
, .
8.2. ps m
$ ps m -o pid,tid,command
PID TID COMMAND
3587 - bash
- 3587 -
3592 - bash
- 3592 -
12287 - /usr/bin/python /usr/bin/gm-notify
- 12287 -
- 12288 -
- 12289 -
- 12295 -
8.2 ,
8.1. ,
: .
12287 12287 .

, ,
. ,
, - ,
.
8.6. 205

,
.
, top ;
H, .
, ,
, .

8.5.
, ,
, /.
,
.
Linux
.
, ,
,
, . ,
, ,
, , , .

8.6.
,
-p top :
$ top -p pid1 [-p pid2 ...]
,
, time.
time, ,
/usr/bin/time. ,
, ls, :
$ /usr/bin/time ls
ls time ,
. :
0.05user 0.09system 0:00.44elapsed 31%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (125major+51minor)pagefaults 0swaps
. ,
.

, .
.
(, ).
206 8.

. ,
, , ,
. , ,
, ,
,
, .

/.
8.9.

8.7.
, ,
-
.
, 20 20,
20 . (, !)
ps-l ,
top, :
$ top
Tasks: 244 total, 2 running, 242 sleeping, 0 stopped, 0 zombie
Cpu(s): 31.7%us, 2.8%sy, 0.0%ni, 65.4%id, 0.2%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 6137216k total, 5583560k used, 553656k free, 72008k buffers
Swap: 4135932k total, 694192k used, 3441740k free, 767640k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
28883 bri 20 0 1280m 763m 32m S 58 12.7 213:00.65 chromium-
browse
1175 root 20 0 210m 43m 28m R 44 0.7 14292:35 Xorg
4022 bri 20 0 413m 201m 28m S 29 3.4 3640:13 chromium-browse
4029 bri 20 0 378m 206m 19m S 2 3.5 32:50.86 chromium-browse
3971 bri 20 0 881m 359m 32m S 2 6.0 563:06.88 chromium-browse
5378 bri 20 0 152m 10m 7064 S 1 0.2 24:30.21 compiz
3821 bri 20 0 312m 37m 14m S 0 0.6 29:25.57 soffice.bin
4117 bri 20 0 321m 105m 18m S 0 1.8 34:55.01 chromium-browse
4138 bri 20 0 331m 99m 21m S 0 1.7 121:44.19 chromium-browse
4274 bri 20 0 232m 60m 13m S 0 1.0 37:33.78 chromium-browse
4267 bri 20 0 1102m 844m 11m S 0 14.1 29:59.27 chromium-browse
2327 bri 20 0 301m 43m 16m S 0 0.7 109:55.65 unity-2d-shell

top PR ()
. ,
, ,
.
,

, .
8.8. 207

, -
(NI), .
, .
,
.
0. ,
,
.
,
,
20 renice ( pid
, ):
$ renice 20 pid
-superuser,
, ,

. , ,
, Linux
, .
,
.

8.8.

. ,
. ,
.
, ,
( , , ), ,

. , - ,
.

8.8.1. uptime
uptime ,
:
$ uptime
... up 91 days, ... load average: 0.08, 0.03, 0.01
, ,
1, 5 15 . ,
: 15
208 8.

0,01. , ,
1%
15.
0, -
. ,
,
.

-
, . , Linux Flash
, Flash-

.

1, , ,
. ,
, top;
.

,
. , 1 ,
, 2 ,
.

8.8.2.
,
. ,
/,
. ,
, :
.
,
,
. ,
, -,
,
.
, ,
, ,
. ,
. ,
, ,
(
) , .
.
8.9. 209

8.9.

free /proc/meminfo, ,
.
,
. / (
), .
,
.

8.9.1.
(MMU),
, , .
MMU, , -
. ,

. , MMU
, ,
.

.
;
. , ,
.
1. .
2.
.
3. ,
,
. ,
.
4. , ,
, ,
( ) .

8.9.2. -
, ,
- .
,
. - :
.
210 8.


- ,
, MMU , .
, MMU

. MMU
. -
, .
- ,
, , .


- ,
, ,
- .
,
,
.
: ,
.
,
, .

-

ps, top time. ,
time . cal
, , /dev/null.
$ /usr/bin/time cal > /dev/null
0.00user 0.00system 0:00.06elapsed 0%CPU (0avgtext+0avgdata 3328maxresident)k
648inputs+0outputs (2major+254minor)pagefaults 0swaps

,
254 - .
,
. , , ,
, .
,
top ps. top f,
, u, .
, nFLT.
.
ps
, .
ID 20365:
8.10. vmstat 211

$ ps -o pid,min_flt,maj_flt 20365
PID MINFL MAJFL
20365 834182 23
MINFL MAJFL -
. ,
, ps(1).

. ,
, ,
.

8.10.

vmstat
,
, vmstat ,
.
, ,
/.
vmstat .
, , vmstat2,
:
$ vmstat 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
2 0 320416 3027696 198636 1072568 0 0 1 1 2 0 15 2 83 0
2 0 320416 3027288 198636 1072564 0 0 0 1182 407 636 1 0 99 0
1 0 320416 3026792 198640 1072572 0 0 0 58 281 537 1 0 99 0
0 0 320416 3024932 198648 1074924 0 0 0 308 318 541 0 0 99 1
0 0 320416 3024932 198648 1074968 0 0 0 0 208 416 0 0 99 0
0 0 320416 3026800 198648 1072616 0 0 0 0 207 389 0 0 100 0
: procs ,
memory , swap ,
, io , system
cpu ,
.
,
.
. ,
(swpd) 320416 ,
3025000 (3) .
, si (swap-in,
) so (swap-out, ) ,
212 8.

. buff
, (. 4.2.5).
CPU
( us, sy, id wa).
, ,
( ), /.
(
1% ); ,
99% .
, ,
(
) ( 8.3).
8.3.
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 320412 2861252 198920 1106804 0 0 0 0 2477 4481 25 2 72 0
1 0 320412 2861748 198924 1105624 0 0 0 40 2206 3966 26 2 72 0
1 0 320412 2860508 199320 1106504 0 0 210 18 2201 3904 26 2 71 1
1 1 320412 2817860 199332 1146052 0 0 19912 0 2446 4223 26 3 63 8
2 2 320284 2791608 200612 1157752 202 0 4960 854 3371 5714 27 3 51 18
1 1 320252 2772076 201076 1166656 10 0 2142 1190 4188 7537 30 3 53 14
0 3 320244 2727632 202104 1175420 20 0 1890 216 4631 8706 36 4 46 14

8.3 ( ),
, .
,
, .
( ):
( si). , ,
, ,
. ,
.
, b ,
( ) .
,
. ,
bi (blocks in, ) bo
(blocks out, ).
, .

,
. ,
so ( ),
.
, .
, ,
8.11. / 213

, , ,
, (
).
vmstat.
vmstat(8). ,
, :
Operating System Concepts (
), 9- (Wiley, 2012).

8.11. /
vmstat /
.
vmstat-d,
. ,
/, iostat.

8.11.1. iostat
vmstat, iostat
:
$ iostat
[kernel information]
avg-cpu: %user %nice %system %iowait %steal %idle
4.46 0.01 0.67 0.31 0.00 94.55

Device: tp s kB_read/s kB_wrtn/s kB_read kB_wrtn


sda 4.6 7 7.2 8 49.86 9493727 65011716
sde 0.0 0 0.0 0 0.00 1230 0
avg-cpu
, , .
, .
tps
kB_read/s
kB_wrtn/s
kB_read
kB_wrtn

vmstat :
, iostat2, 2
.
. -d (, iostatd2).
iostat .
, pALL.
214 8.

, .
, :
$ iostat -p ALL
--snip
--Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
--snip-
sda 4.67 7.27 49.83 9496139 65051472
sda1 4.38 7.16 49.51 9352969 64635440
sda2 0.00 0.00 0.00 6 0
sda5 0.01 0.11 0.32 141884 416032
scd0 0.00 0.00 0.00 0 0
--snip--
sde 0.00 0.00 0.00 1230 0
sda1, sda2 sda5 sda,
, ,
. , ,
. c sda1
sda, , sda
, .

8.11.2.
/
iotop
, /,
, iotop.
top.
, ,
/, :
# iotop
Total DISK READ: 4.76 K/s | Total DISK WRITE: 333.31 K/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
260 be/3 root 0.00 B/s 38.09 K/s 0.00 % 6.98 % [jbd2/sda1-8]
2611 be/4 juser 4.76 K/s 10.32 K/s 0.00 % 0.21 % zeitgeist-daemon
2636 be/4 juser 0.00 B/s 84.12 K/s 0.00 % 0.20 % zeitgeist-fts
1329 be/4 juser 0.00 B/s 65.87 K/s 0.00 % 0.03 % soffice.b~ash-pipe=6
6845 be/4 juser 0.00 B/s 812.63 B/s 0.00 % 0.00 % chromium-browser
19069 be/4 juser 0.00 B/s 812.63 B/s 0.00 % 0.00 % rhythmbox
,
, / TID ( )
. iotop ,
.
PRIO () /.
, , ,
.
8.12. pidstat 215

, be/4, be ,
. ,
. , /
be/3, be/4.
,
/.
iotop.
be .
/ .
.
rt . /
/, .
idle . / ,
/.
.
/
ionice; . ionice(1).
/.

8.12.
pidstat
,
, top iotop. ,
. pidstat

vmstat. ,
1329:
$ pidstat -p 1329 1
Linux 3.2.0-44-generic-pae (duplex) 07/01/2015 _i686_ (4 CPU)
09:26:55 PM PID %usr %system %guest %CPU CPU Command
09:27:03 PM 1329 8.00 0.00 0.00 8.00 1 myprocess
09:27:04 PM 1329 0.00 0.00 0.00 0.00 3 myprocess
09:27:05 PM 1329 3.00 0.00 0.00 3.00 1 myprocess
09:27:06 PM 1329 8.00 0.00 0.00 8.00 3 myprocess
09:27:07 PM 1329 2.00 0.00 0.00 2.00 3 myprocess
09:27:08 PM 1329 6.00 0.00 0.00 6.00 2 myprocess


,
. , .
%guest : ,
- .
, .
216 8.

pidstat ,
. , -r,
, -d, .
, pidstat(1),
,
- , .

8.13.
,
, ,
. , , , /
, .
. ,
.
,
. ,
, -
, .
.
,
, .
sar (System Activity Reporter, ). sar
vmstat,
.
sar ,
. ,
, .
acct ( ). acct
.
.
.
/etc/security/limits.conf;
limits.conf(5). PAM,
, -,
PAM (, ).
, ,
quota.
, ,
, (Brendan Gregg) Systems Performance: Enterprise and
the Cloud ( : ) (Prentice Hall,
2013) .
,
. ,
, . .
9


. , , , ,
.
, , , ?
- , ,
?
, ,
,
. , -
, ,
. Linux ,
SCSI, 3.
,
.
.
. ,
,
, ,
. , ,
. ,
; .

9.1.
, ,
.9.1.
,
. ,
, . ,
, .
( A, B C)
(LAN, local area network).
.
218 9.

.9.1. ,

,
.
, ,
. ,
.
Linux,
.9.1.
. ,
. : .
,
/ . ,
, (,
HTML ).

, ,
, , .

, .

, ,
.
, .

9.2.
,
. .
- , .
. ,
. , - .
:
9.2. 219

HTTP (Hypertext Transfer Protocol, ;


), SSL (Secure Socket Layer,
) FTP (File Transfer Protocol, ).
. , ,
SSL HTTP.
.
. ,
,
( ).
TCP (Transmission
Control Protocol, ) UDP (User Datagram Protocol,
).
.
-. , -
-.
IP (Internet Protocol, -).
,
-. ,
,
( IP, IPv6, IPX
AppleTalk) .
. ,
, Ethernet .
-.
,
,
. , , A B,
.9.1, A
A. ,
, ,
B . -
- ,
(, , ) ,
.
,
. ,
, ,
-,
.
, .
, Linux ,
?, .
.
, ?.
220 9.

, , , OSI (Open Systems


Interconnection Reference Model). , -
, ,
, .
( ), . (AndrewS.Tanenbaum) -
. (DavidJ.Wetherall) Computer Networks ( ), 5-
(Prentice Hall, 2010).

9.3.-
, ,
, . ,
, -, 4 (IPv4)
6 (IPv6). -
, ,

. ,
- ,
.
;
, . , -
. , .9.1
.
.
9.1, ,
(
). 9.2 .9.1
, -
.
: 10.23.2.1 , - (
-,
). , .
- IP-
a.b.c.d, 10.23.2.37.
, . ,
IP- . IP-
, , ,
(NAT)
.

IP- 4 ( 32), abcd. a d


1 254, b c 0 255. IP-
. -
, , 10.23.2.37,
0x0A170225.
9.3.- 221

.9.2. IP-

IP- .
, IP- .
.

9.3.1. IP-
IP-. ,
Linux, :
$ ifconfig
, , ,
:
eth0 Link encap:Ethernet HWaddr 10:78:d2:eb:76:97
inet addr:10.23.2.4 Bcast:10.23.2.255 Mask:255.255.255.0
inet6 addr: fe80::1278:d2ff:feeb:7697/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:85076006 errors:0 dropped:0 overruns:0 frame:0
TX packets:68347795 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:86427623613 (86.4 GB) TX bytes:23437688605 (23.4 GB)
Interrupt:20 Memory:fe500000-fe520000
ifconfig -
( -!).
, ,
, IPv4 (inetaddr) 10.23.2.4.
Mask 255.255.255.0.
, , IP-. ,
.

ifconfig, , ( route
arp), ip. ip
222 9.

, , . -
- ,
Unix.
.

9.3.2.
, IP- -
. ,
.9.2. , 10.23.2.1 10.23.2.254
, 10.23.1.1 10.23.255.254.
:
( , ifconfig
). , , IP-
10.23.2.1 10.23.2.254. ,
; 10.23.2.0,
255.255.255.0. , .
, ,
IP- .
.
IP-, . , ,
10.23.2.0 255.255.255.0:
10.23.2.0: 00001010 00010111 00000010 00000000
255.255.255.0: 11111111 11111111 11111111 00000000
10.23.2.0,
255.255.255.0:
10.23.2.0: 00001010 00010111 00000010 00000000
, .
1, IP-
, ,
.
, , IP- 10.23.2.1
255.255.255.0 ,
, IP- 10.23.2.
10.23.2.0/255.255.255.0.

9.3.3.
CIDR
,
255.255.255.0 255.255.0.0.
255.255.255.192, - ,
. , ,
, CIDR (Classless Inter-Domain Routing,
9.4. 223

). 10.23.2.0/255.255.255.0
10.23.2.0/24.
, , (
, ). ,
,
. , , 255.255.255.0
24 ,
8 . CIDR
. , ,
10.23.2.0/24, , .
.9.1
CIDR.

9.1.

CIDR
255.0.0.0 8
255.255.0.0 16
255.240.0.0 12
255.255.255.0 24
255.255.255.192 26

, -
, -, bc dc,
. , bc
obase=2;240, 240 ( 2).


, .
.

9.4.

,
. .9.2
IP- 10.23.2.4. 10.23.2.0/24
.
,
10.23.2.1.
Linux ?
, ,
. ,
route-n. ,
10.23.2.4:
224 9.

$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.23.2.1 0.0.0.0 UG 0 0 0 eth0
10.23.2.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
.
Destination , Genmask ,
. : 0.0.0.0/0 (
) 10.23.2.0/24.
Flags U, ,
(up).

Gateway Flags. 0.0.0.0/0 Flags G,
, ,
Gateway ( 10.23.2.1). 10.23.2.0/24
Flags G, ,
- . 0.0.0.0
Gateway.
.
: , -
10.23.2.132,
, 0.0.0.0/0 10.23.2.0/24. ,
? .
CIDR : 10.23.2.0/24
, 24; 0.0.0.0/0 ,
( ),
10.23.2.0/24.

-n route IP- . -
, , -
, netstat.

. 0.0.0.0/0
, .
, , Gateway (
route-n) ,
. ,
, ,
. ,
,
.

255.255.255.0 -
1 (, 10.23.2.1 10.23.2.0/24). ,
.
9.5. , ICMP DNS 225

9.5. ,
ICMP DNS
,
. ,
: ICMP (Internet Control Message Protocol,
),
, DNS (Domain Name Service,
), IP-,
.

9.5.1. ping
ping (. http://ftp.arl.mil/~mike/ping.html)
. -
ICMP - - .
, -
ICMP.
, , ping10.23.2.1
:
$ ping 10.23.2.1
PING 10.23.2.1 (10.23.2.1) 56(84) bytes of data.
64 bytes from 10.23.2.1: icmp_req=1 ttl=64 time=1.76 ms
64 bytes from 10.23.2.1: icmp_req=2 ttl=64 time=2.35 ms
64 bytes from 10.23.2.1: icmp_req=4 ttl=64 time=1.69 ms
64 bytes from 10.23.2.1: icmp_req=5 ttl=64 time=1.61 ms
, 56 (
, 84) 10.23.2.1 (
); 10.23.2.1.
(icmp_req)
(time).
8.
.
( 24)
- . ,
. ,
, ping .
(1000),
.
-
-.
, , ,
ping ICMP- host unreachable ( ).

.
226 9.

.

-.

-,
- , ping.

9.5.2. traceroute
ICMP traceroute ,
, .
traceroute host, ,
. traceroutenhost
.
traceroute ,
,
:
4 206.220.243.106 1.163 ms 0.997 ms 1.182 ms
5 4.24.203.65 1.312 ms 1.12 ms 1.463 ms
6 64.159.1.225 1.421 ms 1.37 ms 1.347 ms
7 64.159.1.38 55.642 ms 55.625 ms 55.663 ms
8 209.247.10.230 55.89 ms 55.617 ms 55.964 ms
9 209.244.14.226 55.851 ms 55.726 ms 55.832 ms
10 209.246.29.174 56.419 ms 56.44 ms 56.423 ms
67,
, - .
traceroute . ,
,
.
, traceroute,
.
,
.

9.5.3. DNS
IP- , .
www.example.com.
DNS , ,
, IP-.
IP-, ,
host:
$ host www.example.com
www.example.com has address 93.184.216.119
www.example.com has IPv6 address 2606:2800:220:6d:26bf:1447:1097:aa7
9.6. Ethernet 227

, IPv4
(93.184.216.119), IPv6. ,
.
host : IP-
, ,
IP-. , .
IP-, DNS , ,
.
, .
host, , DNS.
, 9.12.

9.6. Ethernet

.
- , :

, .
- -
, .

Ethernet. IEEE 802
Ethernet, ,
.
Ethernet MAC (Media Access Control,
),
. IP-
Ethernet (
, ). MAC- , ,
: 10:78:d2:eb:76:97.
Ethernet ,
. MAC-
.
Ethernet
. , , Ethernet
, (
), Ethernet
, Ethernet-.
( -).
Ethernet .
,
,
.
228 9.

9.7.
- ,
-
. Linux

(). ,
IP-
. ,
,
eth0 ( Ethernet ) wlan0 ( ).
9.3.1
: ifconfig.
:
eth0 Link encap:Ethernet HWaddr 10:78:d2:eb:76:97
inet addr:10.23.2.4 Bcast:10.23.2.255 Mask:255.255.255.0
inet6 addr: fe80::1278:d2ff:feeb:7697/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:85076006 errors:0 dropped:0 overruns:0 frame:0
TX packets:68347795 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:86427623613 (86.4 GB) TX bytes:23437688605 (23.4 GB)
Interrupt:20 Memory:fe500000-fe520000

,
. -
MAC- (HWaddr). ,
UP RUNNING, , .
ifconfig
( ,
),
,
. ,
, - ethtool
Ethernet.
9.23.

9.8.

,
: , (-)
Linux.
Linux , - .
9.8. 229

1. ,
. , ifconfig-a
, .
2. ,
.
3. IP- ,
( ) (-)
.
4. ,
.
,
, :
,
ifconfig route.
IP- ,
:
# ifconfig interface address netmask mask

interface , eth0.
, , ,
, , , :
# route add default gw gw-address

gw-address IP- ;
,
.
.
, :
# route del -net default


. , 10.23.2.0/24,
192.168.45.0/24 , 10.23.2.44
. ,
, 192.168.45.0 :
# route add -net 192.168.45.0/24 gw 10.23.2.44

, :
# route del -net 192.168.45.0/24

, ,
. ,
192.163.45.0/24 10.23.2.0/24,
.
230 9.


, .
,
,

. 9.17.

9.9. ,

.
,
.
, ifconfig route, -
. .

Linux. ifup ifdown
: , ()
ifup eth0, ifconfig route
eth0. ,
ifup ifdown,
. Ubuntu, , ifupdown,
/etc/network,
Fedora
/etc/sysconfig/network-scripts.
, ,

, ifup(8)
interfaces(5). , ,
, .
( lo, . 9.13) ,
,
.

9.10.,



( ),
,
() IP-. IP-
, -
9.11. 231

, .
,
IP- , .
DHCP (Dynamic Host Configuration Protocol,
; 9.16)
.
. , ,
,
, . ,
, ,
.
(,
Ethernet-),
, ?
?
,
.
,
, -?
? ,
?

?
, ,
, .
, (
) ,
.
,
,
, - .

9.11.

Linux.
NetworkManager.
, netifd OpenWRT,
ConnectivityManager Android, ConnMan Wicd.
NetworkManager, , ,
.
, , , NetworkManager
.
232 9.

9.11.1. NetworkManager
NetworkManager ,
. ,
.

.
NetworkManager
. ,
udev
Desktop Bus (D-Bus).
:
. ,
.
, NetworkManager
,
dhclient, -
.
, NetworkManager
, ,
. , ,
Debian/Ubuntu, Red Hat.
NetworkManager
, ,
- .
Ethernet.
1. ,
. .
2. . ,
, NetworkManager
.
3. ,
, , .
NetworkManager :
, (
, , )
.

9.11.2.
NetworkManager
NetworkManager
: ,
, (,
9.11. 233

). ,
,
.
, -.
,
.
,
nm-tool .
. ifconfig,
, ,
.
NetworkManager
nmcli. ;
. nmcli(1).
, nm-online , .
, ;
.
. 11.

9.11.3. NetworkManager
NetworkManager
/etc/NetworkManager, .
NetworkManager.conf. XDG-
.desktop .ini Microsoft:
. ,
[main],
. , ifupdown,
Ubuntu Debian:
[main]
plugins=ifupdown,keyfile
, , ifcfg-rh (
Red Hat) ifcfg-suse ( SuSE). keyfile, ,
NetworkManager.

/etc/NetworkManager/system-connections.
NetworkManager.conf,
.


NetworkManager
, ,
. , ,
-
(lo), .
234 9.


, .
NetworkManager
.
NetworkManager,
- , . ifupdown
(, Ubuntu Debian), /etc/
network/interfaces, ifupdown NetworkManager.conf
managed false:
[ifupdown]
managed=false
ifcfg-rh, Fedora Red Hat,
, , /etc/sysconfig/network-scripts,
ifcfg-*:
NM_CONTROLLED=yes
no, NetworkManager
. , ,
ifcfg-lo. ,
:
HWADDR=10:78:d2:eb:76:97
,
keyfile,
NetworkManager.conf MAC. :
[keyfile]
unmanaged-devices=mac:10:78:d2:eb:76:97;mac:1c:65:9d:cc:ff:b9

NetworkManager
,
. ,
,
(, ,
).
, NetworkManager
, /etc/NetworkManager/dispatcher.d -
, up down. ,
,
. Ubuntu,
, 01ifupdown, ,
/etc/network,
/etc/network/if-up.d.
NetworkManager,
; ,
9.12. 235

, . ,
, .

9.12.

DNS. host,
, www.example.com, IP- 10.23.2.132.
DNS ,
, .
,
- . DNS
- .
IP- ,
IP- , .
, DHCP,
DNS.
Linux DNS.
.
1. , IP-,
.
, ,
, .
2. ,
( /etc/nsswitch.conf),
. , ,
DNS /etc/hosts.
3. DNS ,
,
DNS. IP-.
4. DNS- ( ) .
5. IP- ,
IP- .
.
, / .
.

9.12.1. /etc/hosts

/etc/hosts. :
127.0.0.1 localhost
10.23.2.3 atlantic.aem7.net atlantic
10.23.2.4 pacific.aem7.net pacific
236 9.

(.
9.13).

-, -
, (.
606, 608, 623 625), ARPANET/Internet
.

9.12.2. resolv.conf
DNS /etc/
resolv.conf. , (
10.32.45.23 10.3.2.3 -):
search mydomain.example.com example.com
nameserver 10.32.45.23
nameserver 10.3.2.3
search - (
; , myserver myserver.example.com).
host.mydomain.examp
le.com host.example.com. , , .
DNS .

9.12.3. DNS

DNS . -
, , ,

. ,
( , )
, ,
, .
. Linux
dnsmasq nscd. BIND (
Unix) . ,
, /etc/resolv.conf 127.0.0.1
(localhost) 127.0.0.1,
nslookup-debughost.
,
. dnsmasq
/etc/dnsmasq.conf, .
, Ubuntu, ,
NetworkManager,
/etc/NetworkManager/system-connections ,
NetworkManager dnsmasq
9.13. 237

. ,
NetworkManager.conf, dnsmasq.
, ,
, ,
, .
, , ,
. ,
, , mDNS (Multicast DNS,
DNS) SSDP (Simple Service Discovery Protocol,
). ,
;
, .
, .
mDNS Linux
Avahi. mdns
/etc/nsswitch.conf, .

9.12.4. /etc/nsswitch.conf
/etc/nsswitch.conf ,
, .
DNS. ,
:
hosts: files dns
files dns,
IP- /etc/hosts, DNS.
( ,
), /etc/hosts .
,
.
/etc/hosts, :
- DNS,
/etc/hosts. /etc/hosts
, .

, DNS, . - -
, (Cricket Liu) (Paul Albitz) DNS and BIND
( DNS BIND), 5- (OReilly, 2006).

9.13.
ifconfig, lo:
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
238 9.

inet6 addr: ::1/128 Scope:Host


UP LOOPBACK RUNNING MTU:16436 Metric:1
lo ,
, . ,
127.0.0.1 ,
.
lo ,
lo.
lo ,
.
, , ifup Ubuntu /etc/network/interfaces, Fedora
/etc/sysconfig/network-interfaces/ifcfg-lo.
- ,
/etc grep.

9.14. : TCP,
UDP
,
, , ? .
?. ,
, ,
.
, .
:
(,
).

- .
TCP (Transmission
Control Protocol, ) UDP (User Datagram Protocol,
).
TCP, ,
UDP.

9.14.1. TCP
TCP
. . IP-
- ,
: .
TCP (
NetworkManager)
- . ,
9.14. : TCP, UDP 239

, , 36406
80 . 36406
, 80 .
,
IP- . ,
, netstat. ,
TCP- ( -n
(DNS), -t TCP):
$ netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 10.23.2.4:47626 10.194.79.125:5222 ESTABLISHED
tcp 0 0 10.23.2.4:41475 172.19.52.144:6667 ESTABLISHED
tcp 0 0 10.23.2.4:57132 192.168.231.135:22 ESTABLISHED
Local Address Foreign Address
. , IP-
10.23.2.4, 47626, 41475 57132 .
47626 5222 10.194.79.125.

9.14.2. TCP-
, -

.
, , -
. , -
, ( 10).
: ,
,
- .
netstat :
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 10.23.2.4:47626 10.194.79.125:5222 ESTABLISHED
,
, -, ,
(47626) ,
(5222) (
Jabber XMPP, ).

, ,
, .
172.24.54.234 80 (- )
.
240 9.

Proto Recv-Q Send-Q Local Address Foreign Address State


tcp 0 0 10.23.2.4:80 172.24.54.234:43035 ESTABLISHED
,
, ,
. , TCP-,
, netstat:
$ netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN
--snip--
, 0.0.0.0:80 ,
, 80
. ,
,
. ,
lsof ,
( 10.5.1).

9.14.3. /etc/services
, ? ,
/etc/services,
. .
:
ssh 22/tcp # SSH Remote Login Protocol
smtp 25/tcp
domain 53/udp
,
( TCP).

/etc/services http://www.iana.org/ - -
, RFC6335 .

Linux , (superuser)
, 1 1023.
, 1024
.

9.14.4. TCP
TCP ,
.
9.14. : TCP, UDP 241

, ( ), ,
.
; , .
. TCP
, .
,
, ,
. ,
TCP, :
, TCP
. .9.3 ,
TCP .
, , ,
Linux TCP , ,
,
.
IP-, 9.21.

9.14.5. UDP
UDP
TCP.
, . , TCP,
UDP
. , UDP ,
!
, - , .
UDP
. , ,
.
TCP , UDP
, (
, ). ,
UDP, :
, . ,
TCP, ,
. , TCP-,
,
.
, UDP,
NTP (Network Time Protocol, ).
, ,
.
, UDP; -
,
242 9.

. :
UDP.
,
.

.9.3. TCP
9.16. DHCP 243

, -
,
: , . , -
, ,
. , ,
.

9.15.

,
9.3. ,
,
. :

;
;
;
, IP- ;
, -
.
,
.

9.16. DHCP

, , DHCP (Dynamic Host
Configuration Protocol, )
IP-, , DNS.
, DHCP
,
IP- .
, DHCP.
DHCP,
DHCP- ,
. ,
DHCP-, ( , 9.3)
.

DHCP- DHCP-,
( , ).
244 9.

IP- DHCP,
. ,
.

9.16.1. DHCP Linux


,
dhclient ( ISC (Internet
Software Consortium, ))
. dhclient
, .
, (
eth0):
# dhclient eth0
dhclient
/var/run/dhclient.pid, /var/state/
dhclient.leases.

9.16.2. DHCP Linux


Linux DHCP,
,
. , ,
,
, DHCP.
, DHCP- :
,
IP- .

9.17. Linux

, ,
.
Linux .
, 10.23.2.0/24 192.168.45.0/24.
, - Linux,
:
, .9.4. ,
, .
IP- : 10.23.2.1 192.168.45.1.

( ; -
):
9.17. Linux 245

.9.4. ,

Destination Gateway Genmask Flags Metric Ref Use Iface


10.23.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.45.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
,
(10.23.2.1 10.23.2.0/24 192.168.45.1
192.168.45.0/24). 10.23.2.4 -
10.23.2.0/24, 10.23.2.1. ,
10.23.2.4 ( A) 192.168.45.61 ( E),
10.23.2.1 () eth0,
eth1.
Linux
.
, IP-
:
# sysctl -w net.ipv4.ip_forward
,
,
,
.
246 9.

,
/etc/sysctl.conf.
/etc/sysctl.d,
.
- .
- ,
,
.
. , IP-,
10.23.2.4, ;
. ,
NAT (Network Address
Translation, ).
,
, .

9.18.
, .
, .
, , : IP-
?
-,
, -. ,
IPv4 ,
, ,
.
, .

RFC 1918/6761, -,
.9.2.

9.2. , RFC 1918 6761

CIDR
10.0.0.0 255.0.0.0 10.0.0.0/8
192.168.0.0 255.255.0.0 192.168.0.0/16
172.16.0.0 255.240.0.0 172.16.0.0/12

.
254 ,
10.23.2.0/24, .
C. ,
- .
?
,
9.19. ( IP-) 247

. (
), , - ,
.

9.19.
( IP-)
NAT
IP- .
. Linux NAT,
, IP-.
NAT ,
, .
- , ,
.
; .
.
1.
,
.
2. ,
( ,
).
3. -
.
4. ,
,
.
5.
. ;
.
, . IP-
IP- -. ,
-,

( ),
, -,
,
. , NAT -
,
, UDP TCP, .
UDP , , ;
TCP.
248 9.

Linux NAT-,
:
( ), ,
IP-, NAT (MASQUERADE).
.
iptables,
NAT . ,
Ethernet eth1,
eth0 (
iptables 9.21):
# sysctl -w net.ipv4.ip_forward
# iptables -P FORWARD DROP
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

NAT , , -
,
IPv4. - IPv6 (-
), .

, , ,
, .
. Linux
.

9.20. Linux

.
,
, Linux,
,
NAT.
,
,
, -
,
, DHCP,
NAT. ,
Linux, .
,
.

.
9.21. 249

, Linksys,
,
Linux, OpenWRT. WRT
Linksys.
,
: , ,
, , , .
, ,
.
OpenWRT, ,
. ,
- , ,
, .
,
Linux,
, .
, BusyBox,
. BusyBox
,
Unix, shell, ls, grep, cat more.
. , init
.
, Linux
- ,
, .

9.21.
-
.
/ ,
, ,
- .
,
(
,
).
IP-.
, :
;
;
.
,
.
250 9.

. ,
, , :
IP- ;
( );
.
Linux,
IP-. .

9.21.1. Linux:
Linux ,
. .
Linux,
. ,
, .
.
iptables, iptables
.

, nftables, iptables.
iptables
.

,
,
. ,
.
: INPUT , OUTPUT
FORWARD .
.9.5 9.6 ,
. ,
(..9.5),
(..9.6). ,
FORWARD
OUTPUT. , ,
INPUT FORWARD.
, ,
. , PREROUTING
POSTROUTING,
. , -
- Linux,
,
.
, .9.5 9.6.
9.21. 251

.9.5.

.9.6.

9.21.2.
, IP-.
, :
# iptables -L
:
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)


target prot opt source destination

Chain OUTPUT (policy ACCEPT)


target prot opt source destination
,
, , .
ACCEPT. ,
. DROP
, . ,
iptables-P, , :
# iptables -P FORWARD DROP
252 9.

,
.

, - IP- 192.168.34.63 .
, :
# iptables -A INPUT -s 192.168.34.63 -j DROP
-A INPUT INPUT.
s192.168.34.63 IP- , -jDROP
, ,
. , ,
192.168.34.63.
, iptables-L:
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.34.63 anywhere
: 192.168.34.63
, SMTP (TCP- 25).
, :
# iptables -A INPUT -s 192.168.34.0/24 -p tcp --destination-port 25 -j DROP
,
ptcp, TCP. ,
--destination-port25, ,
25. IP- INPUT :
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.34.63 anywhere
DROP tcp -- 192.168.34.0/24 anywhere tcp dpt:smtp
, - 192.168.34.37
, ,
. , ,
:
# iptables -A INPUT -s 192.168.34.37 -j ACCEPT
. ,
:
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.34.63 anywhere
DROP tcp -- 192.168.34.0/24 anywhere tcp dpt:smtp
ACCEPT all -- 192.168.34.37 anywhere
, ,
.
9.21. 253

192.168.34.37, ,
192.168.34.1 192.168.34.254
, . ,
. , ,
, 192.168.34.37
, 25, .
.
:
# iptables -D INPUT 3

iptables-I:
# iptables -I INPUT -s 192.168.34.37 -j ACCEPT
- ,
(, iptablesIINPUT4...).

9.21.3.
,
IP-, ,
. .
:
( INPUT
), (
FORWARD ).
,
ACCEPT, ,
, .
, , .
, SSH-,
TCP- 22. , -
,
. ,
INPUT DROP:
# iptables -P INPUT DROP
ICMP- ( ping ),
:
# iptables -A INPUT -p icmp -j ACCEPT
, ,
IP- , 127.0.0.1 ( ).
, my_addr IP- , :
# iptables -A INPUT -s 127.0.0.1 -j ACCEPT
# iptables -A INPUT -s my_addr -j ACCEPT
254 9.

( ),
my_addr , , 10.23.2.0/24.
, - TCP-,
, TCP-
. TCP- SYN
( ), TCP-,
SYN-, :
# iptables -A INPUT -p tcp '!' --syn -j ACCEPT
, DNS UDP,
,
DNS. DNS,
/etc/resolv.conf, ( ns_addr
):
# iptables -A INPUT -p udp --source-port 53 -s ns_addr -j ACCEPT
, SSH- :
# iptables -A INPUT -p tcp --destination-port 22 -j ACCEPT
IP- ,
( ),
, , .
,
FORWARD INPUT ,
.
, Shorewall.
.
: , ,
. , IP-
.
.

9.22. Ethernet, IP ARP


IP Ethernet ,
. , IP-
Ethernet, .
, IP-,
MAC (). :
Ethernet IP- , MAC-
IP- ?
,
MAC-,
ARP (Address Resolution Protocol, ). ,
Ethernet IP
9.22. Ethernet, IP ARP 255

, , ARP,
IP- MAC. Linux ARP .
ARP , arp.
, -n DNS.
$ arp -n
Address Hwtype Hwaddr Flags Mask Iface
10.1.2.141 ether 00:11:32:0d:ca:82 C eth0
10.1.2.1 ether 00:24:a5:b5:a0:11 C eth0
10.1.2.50 ether 00:0c:41:f6:1c:99 C eth0
ARP . MAC-
? ,
. IP- ARP,
.
1. - Ethernet,
ARP , IP-.
2. - .
3. MAC-,
, ,
.
MAC-.
4. - IP-MAC ARP
.

, ARP (
9.4, ).
, ,
- . , MAC- -
ARP, .

ARP ,
, IP-
(, ),
MAC-. Unix
ARP, ,
, .
ARP :
# arp -d host
ARP
:
$ arp -i interface
arp(8) ,
ARP, .
256 9.

ARP RARP (Reverse Address Resolution Protocol, -


). RARP MAC-
IP-. DHCP,
RARP ,
.

9.23. Ethernet
Ethernet ( Wi-Fi)
. ,
MAC- Ethernet,
, Linux
, . ,
, ;
, ,
, ..
,

, .
, Linux
.
.
. ,
.
.
,
. SSID (Service Set Identifier,
, )
.
. ,
,
,
.
,
.
.
. , ,
- ,
.
.
, , ,
.
9.23. Ethernet 257

Linux, ,
, . :
Linux ,
.

,
,
NetworkManager, . -
, .

9.23.1. iw

iw. ,
, wlan0. ,
. ,
.
# iw dev wlan0 scan

, ,
( , ifconfigwlan0up), -
, IP-.

,
:
# iw dev wlan0 link

MAC, ,
, .

iw , phy0,
, wlan0,
.
.
.


:
# iw wlan0 connect network_name

.
WEP (Wired Equivalent Privacy,
) iw keys. ,
, WEP.
258 9.

9.23.2.
Linux wpa_supp
licant,
. WPA
(Wi-Fi Protected Access, ) WPA2,
, .
(
/etc/wpa_supplicant.conf)
, .
; , wpa_supplicant(1)
wpa_supplicant.conf(5) .
,
, .

. , iw
wpa_supplicant
, .
, NetworkManager,
.
,

.

9.24.

, Linux
. , ,
, , ,
. -
.
, ,

, (,
). .
-
. .
, NetworkManager ,
.
, BGP (Border Gateway Protocol,
), -
.
, .
.
10

,
,
. ,
, ,
9. ,
, , .

. Unix
. ,
.
.
(
), syslog
. ,
, .

,
TCP UDP. ,
, TCP.

10.1.
TCP ,
, . ,
, , -
TCP-80 ,
. , , -:
$ telnet www.wikipedia.org 80
:
Trying some address...
Connected to www.wikipedia.org.
Escape character is '^]'.
260 10.

:
GET / HTTP/1.0
Enter .
HTML-, .
, :
-, TCP- 80;
, , telnet.

telnet .
telnet Kerberos -
( ), telnet
. telnet UDP ,
TCP. , netcat,
10.5.3.

-
telnet, HTTP (Hypertext
Transfer Protocol, ) .

, telnet , ,
HTTP. curl
, :
$ curl --trace-ascii trace_file http://www.wikipedia.org/

curl, ,
.

HTML. (
/dev/null)
trace_file. , ,
, , curl TCP-
, :
== Info: About to connect() to www.wikipedia.org port 80 (#0)
== Info: Trying 10.80.154.224... == Info: connected
, , .
, , curl
();
:
=> Send header, 167 bytes (0xa7)
0000: GET / HTTP/1.1
0010: User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenS
0050: SL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
007f: Host: www.wikipedia.org
10.1. 261

0098: Accept: */*


00a5:
curl,
. ,
curl .
, ;
curl,
, .
, curl GET (
telnet),
. ,
, :
<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 16 bytes (0x10)
0000: Server: Apache
<= Recv header, 42 bytes (0x2a)
0000: X-Powered-By: PHP/5.3.10-1ubuntu3.9+wmf1
--snip--
, <=
, 0000:, , .
,

, , :
<= Recv header, 55 bytes (0x37)
0000: X-Cache: cp1055 hit (16), cp1054 frontend hit (22384)
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 877 bytes (0x36d)
0000: 008000
0008: <!DOCTYPE html>.<html lang="mul" dir="ltr">.<head>.<!-- Sysops:
--snip--
.
Recvheader Recvdata,
, ,
curl
, , , ,
.
curl . curl ,
, (
), HTTP-,
, ,
.
, .
,
262 10.

;
.

10.2.
, cron,
, . ,
syslogd, 7: UDP
514, -r.
,
:
httpd, apache, apache2 -;
sshd (. 10.3);
postfix, qmail, sendmail ;
cupsd ;
nfsd, mountd (
);
smbd, nmbd Windows (.12);
rpcbind (RPC, Remote Procedure Call)
.
,
.
, ,
fork(), ,
. -, -
, .
.
, .
. fork()
. :
TCP-, - Apache,
-,
. , UDP-,
. , .

10.3. (SSH)
-. SSH.

(SSH) -
Unix. SSH
, ,
10.3. (SSH) 263

, ,
telnet rlogin
.
- SSH
, Linux (
, )
SSH. OpenSSH (http://www.openssh.com/)
SSH Unix,
Linux. OpenSSH ssh, sshd.
SSH: 1 2. OpenSSH
, .
SSH
:
;
, ,
X Window ( 14);
;
.

-
. SSH X Window
, SSH
.

SSH . ,
SSH-, ,
(
, , ).
,
(Bruce Schneier) Applied Cryptography: Protocols, Algorithms, and
Source Code in C ( : ,
C), 2- (Wiley, 1996). SSH
. (MichaelW.Lucas) OpenSSH,
PuTTY, Tunnels and Keys ( OpenSSH, PuTTY, ),
. (DanielJ.Barrett), .
(RichardE.Silverman) . (RobertG.Byrnes) SSH, The Secure Shell
(SSH ), 2- (OReilly, 2005).

10.3.1. SSHD
sshd , .
/etc/ssh,
sshd,
. sshd_config
ssh_config, .
264 10.

sshd_config - ,
. ,
:
Port 22
#Protocol 2,1
#ListenAddress 0.0.0.0
#ListenAddress ::
HostKey /etc/ssh/ssh_host_key
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
, #, ,
sshd_config .
sshd_config(5) ,
:
HostKey file file (
);
LogLevel level level
;
PermitRootLogin value superuser
, value yes. ,
no;
SyslogFacility name name
;
X11Forwarding value X Window,
value yes;
XAuthLocation path xauth.
X11 . xauth
/usr/bin, path
xauth.


OpenSSH : 1
2- .
( .pub) ( ).
, ,
.
SSH 1 RSA, 2 RSA
DSA. RSA DSA .
.10.1.
10.1. OpenSSH


ssh_host_rsa_key RSA (2)
ssh_host_rsa_key.pub RSA (2)
10.3. (SSH) 265


ssh_host_dsa_key DSA (2)
ssh_host_dsa_key.pub DSA (2)
ssh_host_key RSA (1)
ssh_host_key.pub RSA (1)

,
OpenSSH ,
, , ,
ssh-agent. SSH 2,
ssh-keygen, OpenSSH:
# ssh-keygen -t rsa -N '' -f /etc/ssh/ssh_host_rsa_key
# ssh-keygen -t dsa -N '' -f /etc/ssh/ssh_host_dsa_key

1 :
# ssh-keygen -t rsa1 -N '' -f /etc/ssh/ssh_host_key

SSH ssh_known_hosts,
.
, ssh_known_hosts
.
, .
,

.

SSH
SSH, sshd
. Ubuntu Debian SSH-
,
. Fedora sshd ,
. sshd ,
chkconfig ( ;
servicesshdstart):
# chkconfig sshd on

Fedora sshd
-.
init,
sshd sshd
PID /var/run/sshd.pid.
sshd systemd
inetd, , , ,
,
.
266 10.

10.3.2. SSH
, :
$ ssh remote_username@host

remote_username@,
, . ssh
, , dir
:
$ tar zcvf - dir | ssh remote_host tar zxvf -

ssh_config SSH
/etc/ssh sshd_config.
, ,
.
SSH ,
ssh_known_hosts .ssh/known_hosts
.
:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
38:c2:f6:0d:0d:49:d4:05:55:68:54:2a:2f:83:06:11.
Please contact your system administrator.
Add correct host key in /home/user/.ssh/known_hosts to get rid of this
message.
Offending key in /home/user/.ssh/known_hosts:12
RSA host key for host has changed and you have requested
strict checking.
Host key verification failed.

,
( ),
, .
,
12 known_hosts (
).
-
.

SSH
OpenSSH , scp sftp,
, rcp ftp.
10.4. inetd xinetd 267

scp
.
cp. :
$ scp user@host:file .
$ scp file user@host:dir
$ scp user1@host1:file user2@host2:dir
sftp ftp ,
get put. sftp-
server, ,
OpenSSH.

, scp sftp (,
), rsync, -
12.

SSH ,
Unix
SSH ,
- OpenSSH (http://www.openssh.com/).
? PuTTY Windows,
. MacSSH
Mac OS 9.x . Mac OS X Unix
OpenSSH.

10.4. inetd xinetd


.
,
.
, ,
.

inetd , ,

. inetd ,
, .
inetd
.
inetd xinetd
, xinetd
systemd,
,
6.4.7.
268 10.

inetd ,
. , sshd
inetd, ,
/etc/inetd.conf:
ident stream tcp nowait root /usr/sbin/sshd sshd -i

, , :
/etc/services (. 9.14.3);
stream TCP dgram
UDP;
, tcp udp;
UDP wait nowait.
, ,
nowait;
, . .group,
;
, inetd
;
.
.
TCP: tcpd, /etc/hosts.allow /etc/hosts.deny.
,
TCP ,
. inetd tcpd,
,
/etc/hosts.allow /etc/hosts.deny. tcpd ,
, ,
. , -
TCP, ,
.

10.5.

,
. ,
, ,
, -, .
9, netstat
,
. .10.2
.
10.5. 269

10.2. netstat,


-t TCP
-u UDP
-l
-a
-n ( ; ,
DNS)

10.5.1. lsof
8 , lsof
, ,
. ,
, :
# lsof -i
,
.
,
:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 700 root 6u IPv4 10492 0t0 UDP *:sunrpc
rpcbind 700 root 8u IPv4 10508 0t0 TCP *:sunrpc (LISTEN)
avahi-dae 872 avahi 13u IPv4 21736375 0t0 UDP *:mdns
cupsd 1010 root 9u IPv6 42321174 0t0 TCP ip6-localhost:ipp (LISTEN)
ssh 14366 juser 3u IPv4 38995911 0t0 TCP thishost.local:55457->
somehost.example.com:ssh (ESTABLISHED)
chromium- 26534 juser 8r IPv4 42525253 0t0 TCP thishost.local:41551->
anotherhost.example.com:https (ESTABLISHED)

, RPC
DNS, avahi,
(cupsd), IPv6.
: SSH-
-, Chromium.
, (
).
lsof netstat ,
IP- , .
-n, :
# lsof -n -i
-P,
/etc/services.
270 10.


- (, , -
, , ),
:
# lsof -i:port
:
# lsof -iprotocol@host:port
protocol, @host :port
lsof.
, host port , . ,
TCP- 80 (
HTTP), :
# lsof -iTCP:80


lsof .
, , , TCP,
:
# lsof -iTCP -sTCP:LISTEN
,
. , UDP
, -iUDP,
. ,
, , , UDP.

10.5.2. tcpdump
, ,
tcpdump
, .
tcpdump , , ,
ARP -:
# tcpdump
tcpdump: listening on eth0
20:36:25.771304 arp who-has mikado.example.com tell duplex.example.com
20:36:25.774729 arp reply mikado.example.com is-at 0:2:2d:b:ee:4e
20:36:25.774796 duplex.example.com.48455 > mikado.example.com.www: S
3200063165:3200063165(0) win 5840 <mss 1460,sackOK,timestamp 38815804[|tcp]>
(DF)
20:36:25.779283 mikado.example.com.www > duplex.example.com.48455: S
3494716463:3494716463(0) ack 3200063166 win 5792 <mss 1460,sackOK,timestamp
4620[|tcp]> (DF)
20:36:25.779409 duplex.example.com.48455 > mikado.example.com.www: . ack 1 win
10.5. 271

5840 <nop,nop,timestamp 38815805 4620> (DF)


20:36:25.779787 duplex.example.com.48455 > mikado.example.com.www: P 1:427(426)
ack 1 win 5840 <nop,nop,timestamp 38815805 4620> (DF)
20:36:25.784012 mikado.example.com.www > duplex.example.com.48455: . ack 427
win 6432 <nop,nop,timestamp 4620 38815805> (DF)
20:36:25.845645 mikado.example.com.www > duplex.example.com.48455: P 1:773(772)
ack 427 win 6432 <nop,nop,timestamp 4626 38815805> (DF)
20:36:25.845732 duplex.example.com.48455 > mikado.example.com.www: . ack 773
win 6948 <nop,nop,timestamp 38815812 4626> (DF)

9 packets received by filter


0 packets dropped by kernel

, .
, ,
Ethernet, ,
. , tcpdump,
ARP, RARP, ICMP, TCP, UDP, IP, IPv6, AppleTalk IPX. ,
, tcpdump TCP, :
# tcpdump tcp

- UDP, :
# tcpdump udp or port 80

, -
tcpdump , Wireshark.


tcp, udp port 80 .
.10.3.

10.3. tcpdump


tcp TCP
udp UDP
port port TCP / UDP port
host host host
net network network


or.
tcpdump ( and !),
. -
tcpdump, ,
, .
272 10.

tcpdump
tcpdump.
tcpdump, ,
TCP ( ) IP (-),
tcpdump .
,
, .

10.5.3. netcat
, telnethostport
, netcat ( nc).
TCP/UDP,
, , /
, . TCP-
netcat, :
$ netcat host port
netcat ,
. ,
netcat.
Ctrl+C. ,
,
sock.
:
$ netcat -l -p port_number

10.5.4.
,
IP- . Network Mapper (Nmap)
,
.
Nmap, http://www.insecure.org/.
Nmap -,
, .

: - (
, ). ,
.

- , -
Nmap, . ,
, , .
10.6. (RPC) 273

nmaphost,
. , :
$ nmap 10.1.2.2
Starting Nmap 5.21 ( http://nmap.org ) at 2015-09-21 16:51 PST
Nmap scan report for 10.1.2.2
Host is up (0.00027s latency).
Not shown: 993 closed ports
PORT STATE SERVICE
22/tcp open ssh
25/tcp open smtp
80/tcp open http
111/tcp open rpcbind
8800/tcp open unknown
9000/tcp open cslistener
9090/tcp open zeus-admin

Nmap done: 1 IP address (1 host up) scanned in 0.12 seconds

, ,
. ,
, 111 ( rpcbind).

10.6. (RPC)
rpcbind,
? RPC (Remote Procedure
Call). , .
,
, :
( ),
.
RPC , TCP
UDP, ,
TCP UDP. rpcbind
, RPC.
, RPC ,
:
$ rpcinfo -p localhost

RPC ,
. (NFS, Network File System)
(NIS, Network Information Service)
RPC, . ,
, rpcbind,
- , (FAM,
File Access Monitor) GNOME.
274 10.

10.7.
Linux Unix
, , -,
,
. ,
.
: ,
( ), ,

( ).
, , .
.
. ,
. - ,
, ,
- .
, .
Unix ,
(, TCP-111 RPC),
.
,
.
, ,
.
, .
SSH, Postfix ,
(. 10.7.2).
.

.
, Debian Unstable Fedora Rawhide,
.
, .

, .
( ),
,
. ,
, (
).
.
-.
10.7. 275

. ?
.
.
( ). ,
,
,
setuid.
DoS- (Denial-of-Service, ).

- , -
. , .
. Linux
, , ,
, ,
.
Linux .
-.

10.7.1.
, :
.
.
, ,
.
, ,
.
, .
,
. , .

( , ),

.

, ,
Stunnel (http://www.stunnel.org/) , TCP.
tcpd, Stunnel inetd.


. (
).
ftpd - FTP- .
, FTP- .
276 10.

,
SSH rsync.
telnetd, rlogind, rexecd
( ) . ,
Kerberos.
fingerd
.

10.7.2.-,

, :
http://www.sans.org/ , ,
,
;
http://www.cert.org/ ;
http://www.insecure.org/ Nmap
.
.
, TLS
(Transport Layer Security, () )
SSL (Secure Socket Layer, ).


. (Davie) Implementing
SSL/TLS Using Cryptography and PKI ( SSL/TLS
, Wiley, 2011).

10.8.
-
, - Apache Postfix. ,
Apache , .

NAT,
, .

. ,
(, tcpdump), .
,
.
,
, , .
10.9.: 277

10.9.:

,
.
/ , :
,
recv(2) send(2). , ,
,
. Unix ,
, . ,
,
.
(IPC, Interprocess
Communication).
,
-. , TCP-
(SOCK_STREAM, ), UDP-
(SOCK_DGRAM).
,
, IP-,
. ,
,
.
.10.1 ,
. ,
: .
.
,
accept(), , /,
.
fork(),
. ,
.
,
. :
,
,
; .
,
, 3- .
(W.Richard Stephens), (Bill Fenner) .
(AndrewM.Rudoff) Unix Network Programming, Volume 1 (
Unix. 1, Addison-Wesley Professional, 2003).
.
278 10.

.10.1.

10.10. Unix
, ,
.
, ,
, (IPC), ,
. , , ,
systemd NetworkManager D-Bus
.
IP-
(127.0.0.1), , ,
, 3,
Unix. Unix,
, : ,
,
TCP UDP.

, Unix -
. . -
Unix .
Unix .

10.10.1.
Unix
. . -,
,
10.10. Unix 279

, ,
. ,
. ,
/var/run/dbus D-Bus:
$ ls -l /var/run/dbus/system_bus_socket
srwxrwxrwx 1 root root 0 Nov 9 08:52 /var/run/dbus/system_bus_socket
-, Linux Unix
,
.
Unix
.
,
, Unix. , mysqld
MySQL ,
Unix /var/run/mysqld/mysqld.sock.

10.10.2. Unix
Unix,
, lsof-U:
# lsof -U
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 19701 mysql 12u unix 0xe4defcc0 0t0 35201227 /var/run/mysqld/
mysqld.sock
chromium- 26534 juser 5u unix 0xeeac9b00 0t0 42445141 socket
tlsmgr 30480 postfix 5u unix 0xc3384240 0t0 17009106 socket
tlsmgr 30480 postfix 6u unix 0xe20161c0 0t0 10965 private/tlsmgr
--snip--
,
.
socket NAME .
11

, ,
( Bourne shell).
, .
, .

11.1.
Bourne shell ,
,
/bin/sh. , .
#!/bin/sh

#! ( shebang)
.
, ,
#!/bin/sh. , :
#!/bin/sh
#
# Print something, then run ls

echo About to run the ls command.


ls

# , ,
, . -
, .


, ,
.
./script, ,
.
11.2. 281

Unix,
, ,
.
:
$ chmod +rx script

chmod
. , 700
( 2.17,
).
, .
. Bourne shell
. 2.14 ,
,
.
Unix,
, .

,
(, ).
,
,
,
Python, Perl awk, , C. ,
.
, .
. Bourne shell
( ).

11.2.

, ,
. , $100
:
$ echo $100
00

00?
$1, ( ).
, , ,
$1. :
$ echo "$100"
00
282 11.

, ,
:
$ echo '$100'
$100
?

11.2.1.
, , ,
.
$ ( ),
* , grep, ,
, ,
- (;).
,
, .
1. ,
, , .
2. .
, , . ,
/etc/passwd,
r.*t ( , r t;
, root, ruth robot).
:
$ grep r.*t /etc/passwd
,
. ? , , .
, r.input r.output,
r.*t r.inputr.output :
$ grep r.input r.output /etc/passwd
, -,
, ,
, .

11.2.2.
,
, ,
grep *:
$ grep 'r.*t' /etc/passwd
,
, , . ,
11.2. 283

, grep
r.*t/etc/passwd ( grep
):
$ grep 'r.*t /etc/passwd'
,
, ,
- .
.
, .

11.2.3.
(") , ,
,
. , ,
:
$ echo "There is no * in my path: $PATH"
,
$PATH, *.

, -
heredoc, 11.9.

11.2.4.
Bourne shell
, .

:
$ echo I don\'t like contractions inside shell scripts.

, 'don\'t
. ,
, (
, ):
$ echo "I don't like contractions inside shell scripts."

, .
1. ' ( ) '\'' ( ,
, , ).
2. .
284 11.

, , thisisn'taforwardslash:\,
:
$ echo 'this isn'\''t a forward slash: \'

, ,
, . , abc
, a"bc" .

11.3.

.
,
,
Bourne shell.
, 2.8, ,
.

, -
, . -
, ,
, .

11.3.1. : $1, $2...


$1, $2, ,
, .
, , pshow:
#!/bin/sh
echo First argument: $1
echo Third argument: $3
, ,
:
$ ./pshow one two three
First argument: one
Third argument: three
shift -
, ($1) .
, $2 $1, $3 $2 .. ,
shiftex:
#!/bin/sh
echo Argument: $1
11.3. 285

shift
echo Argument: $1
shift
echo Argument: $1
, , :
$ ./shiftex one two three
Argument: one
Argument: two
Argument: three
, shiftex : ,
.

11.3.2. : $#
$# , ,
shift .
$# 0, , $1 (.
11.6, ).

11.3.3. : $@
$@
. , Ghostscript (gs)
.
PostScript 150dpi,
,
gs. ,
:
#!/bin/sh
gs -q -dBATCH -dNOPAUSE -dSAFER -sOutputFile=- -sDEVICE=pnmraw $@

- -
, \. ,
:
#!/bin/sh
gs -q -dBATCH -dNOPAUSE -dSAFER \
-sOutputFile=- -sDEVICE=pnmraw $@

11.3.4. : $0
$0 ,
. ,
, $BADPARM.
286 11.

,
:
echo $0: bad option $BADPARM

. 2.14.1 , 2>&1
.
1>&2.
, :
echo $0: bad option $BADPARM 1>&2

11.3.5. : $$
$$ .

11.3.6. : $?
$? ,
. , ,
.

11.4.
Unix , ,
, . ,
. ,
, .
, , 0 (
, ).
$?,
:
$ ls / > /dev/null
$ echo $?
0
$ ls /asdfasdf > /dev/null
ls: /asdfasdf: No such file or directory
$ echo $?
1
, 0,
1 ( ,
/asdfasdf).
,
. , ,
echo $? ,
0, echo .
11.5. 287


- exit1, 1 ,
.
.
, , diff grep,
, . ,
grep 0, -,
, 1, . 1
; grep diff 2.
, - ,
,
. EXIT VALUE (
) DIAGNOSTICS ().

11.5.
Bourne shell ,
if/then/else case. ,
if , hi
:
#!/bin/sh
if [ $1 = hi ]; then
echo 'The first argument was "hi"'
else
echo -n 'The first argument was not "hi" -- '
echo It was '"'$1'"'
fi
if, then, else fi
; . ,
, [$1 ="hi"], [,
Unix, .
, .
Unix [,
. test.
[ test ,
inode,
.
, 11.4, ,
.
1. , if,
.
2. 0, ,
then, ,
else fi.
288 11.

3. 0 else,
, else.
4. fi.

11.5.1.
,
: $1 ,
.
[= hi], [ , .
,
( ):
if [ "$1" = hi ]; then
if [ x"$1" = x"hi" ]; then

11.5.2.

, if, . ,
then,
(;) . ,
then .
, then
.
[.
, grep:
#!/bin/sh
if grep -q daemon /etc/passwd; then
echo The daemon user is in the passwd file.
else
echo There is a big problem. daemon is not in the passwd file.
fi

11.5.3. elif
elif if,
. elif,
case, 11.5.6,
.
#!/bin/sh
if [ "$1" = "hi" ]; then
echo 'The first argument was "hi"'
elif [ "$2" = "bye" ]; then
echo 'The second argument was "bye"'
else
11.5. 289

echo -n 'The first argument was not "hi" and the second was not "bye"-- '
echo They were '"'$1'"' and '"'$2'"'
fi

11.5.4. && ||
,
: && () || (). &&
:
command1 && command2
command1, 0,
command2. || :
, ||, ,
.
&& || if,
,
. &&,
, if,
,
. ||

, .
:
#!/bin/sh
if [ "$1" = hi ] || [ "$1" = bye ]; then
echo 'The first argument was "'$1'"'
fi
([),
, -a -o && ||,
.

11.5.5.
, [:, 0,
, 0, . ,
[str1=str2]. ,
,
[
. , , file
( ):
[ -f file ]
-f, ,
. ,
( ):
290 11.

for filename in *; do
if [ -f $filename ]; then
ls -l $filename
file $filename
else
echo $filename is not a regular file.
fi
done

, !
. , [!ffile] true,
file . , -a -o
(, [-ffile1afile2]).

test ,
Bourne shell ( bash) . -
,
.

,
: ,
. info ,
test(1) .
.


, -f, ,
: , .
:
-e true, ;
-s true, y.
, ,
, - ,
, .11.1.
, , .11.2 (.
2.17).

11.1.


-f
-d
-h
-b
-c
11.5. 291


-p
-s

test ( -h), link -


, [-flink] 0 (true).

11.2.


-r
-w
-x
-u Setuid
-g Setgid
-k

, ( ,
) ,
. ,
-nt ( , ):
[ file1 -nt file2 ]
true, file1
file2. -ot ( , )
.
, -ef true,
inode .


=,
true, . != true,
. :
-z true, ( [-z""]
0);
-n true, ( [-n""]
1).


, (=) , .
, [1=1] 0 (true), [01=1]
false. -eq
: [01eq1] true. .11.3
.
292 11.

11.3.

true,
-eq
-ne
-lt
-gt
-le
-ge

11.5.6.
case
case ,
. case
, , .
.
:
#!/bin/sh
case $1 in
bye)
echo Fine, bye.
;;
hi|hello)
echo Nice to see you.
;;
what*)
echo Whatever.
;;
*)
echo 'Huh?'
;;
esac
.
1. $1 ,
).
2. - $1,
, ,
;;,
esac.
3. esac.

( bye ) ,
| ( hi|hello true,
$1 hi hello). * ? (what*).
11.6. 293

,
, , *,
.

(;;), -
.

11.6.
Bourne shell : for while.

11.6.1. for
for ( ) .
:
#!/bin/sh
for str in one two three four; do
echo $str
done
for, in, do done .
.
1. str (one) ,
in .
2. echo, do done.
3. for, str
(two), do done,
, ,
in.
:
one
two
three
four

11.6.2. while
while Bourne shell ,
if. , :
#!/bin/sh
FILE=/tmp/whiletest.$$;
echo firstline > $FILE
while tail -10 $FILE | grep -q firstline; do
# add lines to $FILE until tail -10 $FILE no longer prints "firstline"
294 11.

echo -n Number of lines in $FILE:' '


wc -l $FILE | awk '{print $1}'
echo newline >> $FILE
done

rm -f $FILE

grepqfirstline.
( , firstline
$FILE), .
while break. Bourne
shell until, while,
, , , ,
0. while until.
, while, ,
awk Python.

11.7.
Bourne shell -
.

, $().
FLAGS.
.
#!/bin/sh
FLAGS=$(grep ^flags /proc/cpuinfo | sed 's/.*://' | head -1)
echo Your processor supports:
for f in $FLAGS; do
case $f in
fpu) MSG="floating point unit"
;;
3dnow) MSG="3DNOW graphics extensions"
;;
mtrr) MSG="memory type range register"
;;
*) MSG="unknown"
;;
esac
echo $f: $MSG
done

,
, .
grep sed ( . 11.10.3),
, .*:,
sed head.
11.8. 295

, . ,
$(ls),
* . ,
, find,
xargs,
exec (. 11.10.4).

: -
(``). . $() -
, POSIX, .

11.8.
,
. ,
,
.
, mktemp
. ,
.
#!/bin/sh
TMPFILE1=$(mktemp /tmp/im1.XXXXXX)
TMPFILE2=$(mktemp /tmp/im2.XXXXXX)

cat /proc/interrupts > $TMPFILE1


sleep 2
cat /proc/interrupts > $TMPFILE2
diff $TMPFILE1 $TMPFILE2
rm -f $TMPFILE1 $TMPFILE2
mktemp . mktemp XXXXXX
.
,
, , ,
.

Unux mktemp. ,
GNU- coreutils .

, , :
,
. , Ctrl+C
, /tmp .
. trap ,
Ctrl+C
, :
296 11.

#!/bin/sh
TMPFILE1=$(mktemp /tmp/im1.XXXXXX)
TMPFILE2=$(mktemp /tmp/im2.XXXXXX)
trap "rm -f $TMPFILE1 $TMPFILE2; exit 1" INT
--snip--
exit ,
,
.

mktemp. ,
/tmp/tmp.

11.9. heredoc
,
. echo,
heredoc, :
#!/bin/sh
DATE=$(date)
cat <<EOF
Date: $DATE

The output above is from the Unix date command.


It's not a very interesting command.
EOF
, , heredoc.
<<EOF ,
, , <<EOF (
cat). , -
EOF.
, ,
heredoc .
.
$DATE .
heredoc,
, ,
.

11.10.

.
, basename,
11.10. 297

,, .
, awk, .

11.10.1. basename

, basename.
, , :
$ basename example.html .html
$ basename /usr/local/bin/example
basename example.
.html example.html,
.
, basename
, GIF PNG:
#!/bin/sh
for file in *.gif; do
# exit if there are no files
if [ ! -f $file ]; then
exit
fi
b=$(basename $file .gif)
echo Converting $b.gif to $b.png...
giftopnm $b.gif | pnmtopng > $b.png
done

11.10.2. awk
awk ;
. ,
awk ,
, Python.
awk , The AWK Programming Language
( AWK) . (AlfredV.Aho),
(BrianW.Kernighan) . (PeterJ.Weinberger)
(Addison-Wesley, 1988). awk
: , :
$ ls -l | awk '{print $5}'
ls ( ).
, .

11.10.3. sed
sed ( stream editor )
, (
298 11.

),
. sed
ed, Unix.
, .
awk, sed,
: sed & awk Pocket Reference (
sed awk) (Arnold Robbins), 2- (OReilly,
2002).
sed
, , . ,
sed .
, , .
sed: -
(. 2.5.1), , :
$ sed 's/exp/text/'
, /etc/passwd
%, ,
:
$ sed 's/:/%/' /etc/passwd
/etc/passwd ,
g , :
$ sed 's/:/%/g' /etc/passwd
, ; /etc/passwd
,
:
$ sed 3,6d /etc/passwd
3,6 ( ), d
(). , sed
.
sed s ( ) d.
.
, exp:
$ sed '/exp/d'

11.10.4. xargs
,
,
. ,
xargs, .
xargs find. ,
,
11.10. 299

.gif GIF (Graphic


Interchange Format, ):
$ find . -name '*.gif' -print | xargs file

xargs file.
,
,
. ,
find
xargs NULL:
$ find . -name '*.gif' -print0 | xargs -0 file

xargs ,
, .
(--) xargs,
, -
(-). (--) , -
, , , ,
. ,
.
xargs find: -exec.
,
{} ;,
.
find:
$ find . -name '*.gif' -exec file {} \;

11.10.5. expr

, expr (
). , expr1+2 3.
expr-help, .
expr
. , ,
, - Python .

11.10.6. exec
exec ,
, exec.
exec(), 1.
,
: exec ,
, .
300 11.

, execcat.
Ctrl+D Ctrl+C cat
, .

11.11.
, ,
. ,
(, ),
, .
,
, , -
. ,
, , ,
.
, ,
. , uglyprogram
uglydir, :
$ (cd uglydir; uglyprogram)
, ,
, :
$ (PATH=/usr/confusing:$PATH; uglyprogram)

,
, :
$ PATH=/usr/confusing:$PATH uglyprogram
.
tar
orig, target,
orig ( ,
,
, cp-r):
$ tar cf - orig | (cd target; tar xvf -)

, ,
target orig.

11.12.
,
(.). , config.sh:
. config.sh
11.14. () 301


,
.

11.13.
read
. , $var:
$ read var

, .

11.14. ()

,
. ,
, ,
(, Unix Shell Programming ( Unix)
. (StephenG.Kochan) (3- , SAMS Publishing, 2003))
(The UNIX Programming Environment (
Unix) . (BranW.Kernighan)
(Rob Pike) (Prentice Hall, 1984)).
, , (
read), :
- ? ,
: .
, - ,
, ,
, Python, Perl awk.
12


. ,
scp sftp, .
, -
.
,
.
,
, , ,
. .

12.1.
, ( )

- .
. Python.
:
$ python -m SimpleHTTPServer
-,
. 8000,
, , , 10.1.2.4,
http://10.1.2.4:8000 ,
.

12.2. rsync
,
scpr , ,
tar:
$ tar cBvf - directory | ssh remote_host tar xBvpf -
12.2. rsync 303

, . ,

.
- ,
.
(
),
. Linux rsync
,
.
rsync, .

12.2.1. rsync
rsync ,
-, -
.
,
, SSH-.
, rsync
,
.
rsync scp.
rsync . ,
,
:
$ rsync file1 file2 ... host:
rsync ,
SSH .
:
rsync not found
rsync: connection unexpectedly closed (0 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(165)
, rsync
. rsync ,
, --rsync-path=path,
.
,
user@, user
:
$ rsync file1 file2 ... user@host:
, rsync
. , ,
304 12.

, dir
, :
skipping directory dir

,
, , -a.
, - ,
,
:
$ rsync -a dir host:destination_dir

,
, ,
, -nv. -n
rsync ,
, . -v
,
:
$ rsync -nva dir host:destination_dir

:
building file list ... done
ml/nftrans/nftrans.html
[more files]
wrote 2183 bytes read 24 bytes 401.27 bytes/sec

12.2.2.

rsync ,
. ,
d, a b, ,
d/c, rsync
d/a, d/b d/c.
,
, ( d/c
). , --delete:
$ rsync -a --delete dir host:destination_dir

,
, . : -
, -n, , -
rsync .
12.2. rsync 305

12.2.3.
-
rsync. , :
$ rsync -a dir host:dest_dir
dir dest_dir.
.12.1 , rsync
a b. (/)
:
$ rsync -a dir/ host:dest_dir
rsync , dir,
dest_dir , dir
. , dir/
, cpdir/*dest_dir .
, dir, a b (dir/a dir/b).
, dest_dir
:
$ rsync -a dir/ host:dest_dir
dest_dir a b,
dir. dir,
dest_dir dir a b . ,

dest_dir/dir/a dest_dir/dir/b. .12.2 , rsync
, .12.1,
.

/ ;
, dir
. , , ,
--delete,
.

.12.1. rsync
306 12.

.12.2. rsync


. readline
.

12.2.4.
rsync
. ,
src host, , .git.
:
$ rsync -a --exclude=.git src host:
,
.git, --exclude ,
. , ,
/, :
$ rsync -a --exclude=/src/.git src host:

/ /src/.git , -
.

.
--exclude, .
,
( )
--exclude-from=file.
item,
, : --exclude=item/.

. , t*s
this, ethers.
, ,
, --include,
.
12.2. rsync 307

12.2.5. ,


rsync ,
- .
.
, rsync ,
,
. rsync-n.
rsync
: rsync-v. , ,

.

, rsync
.
,
, ,
.

. , .
--checksum ( -c) (
) , .
/
, ,
, .
--ignore-existing ,
.
--backup ( -b) ,
, , ~
.
--suffix=s , --backup, c ~ s.
--update ( -u) ,

.
, rsync ,
,
. rsync-v
rsync-vv, .
v, , , ,
.
,
rsync--stats.
308 12.

12.2.6.
-z
-a, :
$ rsync -az dir host:destination_dir
,
(
, , DSL-)
.

, ,
.

12.2.7.
-,
. ( )
,
, rsync
, TCP-, HTTP,
.
, --bwlimit,
. , ,
10000 , :
$ rsync --bwlimit=10000 -a dir host:destination_dir

12.2.8.
rsync
.
,
. ,
src_dir dest_dir ,
:
$ rsync -a host:src_dir dest_dir

, rsync -
, host:.

12.2.9. ,
rsync
, rsync
.
12.4. Samba 309

, ,
, ,
. ,
.
rsync .
, , Linux - S3
Amazon, rsync--delete,
,
.
,
. , rsync--help.
rsync(1),
, rsync (http://rsync.samba.org/).

12.3.

Linux, , ,
,
.
Windows Mac OSX,
, Linux
. Linux
(NAS, Network
Area Storage) (NFS,
Network File System) .

12.4.
Samba
Windows, , ,
Linux ,
Windows SMB (Server Message Block, ).
SMB
Mac OSX.
Linux
Samba. Windows-
Linux, :
Windows c Linux-
Samba.
Samba, .
1. smb.conf.
2. .
310 12.

3. .
4. Samba: nmbd smbd.
Samba
,
. , -,
, Linux-
.

Samba , ,
Windows- Linux- -
Windows Network Places. Samba
, .
,
Using Samba ( Samba, 3- , OReilly, 2007),
, Samba http://www.samba.org/.

12.4.1. Samba
Samba smb.conf,
etc, /etc/samba.
,
lib, /usr/local/samba/lib.
smb.conf XDG,
(, systemd): ,
(, : [global] [printers]).
[global] ,
.
.
[global] , ,
:
[global]
# server name
netbios name = name
# server description
server string = My server via Samba
# workgroup
workgroup = MYNETWORK

:
netbios name . , Samba
Unix;
server string .
Samba;
workgroup SMB. Windows,
.
12.4. Samba 311

12.4.2.
smb.conf ,
, Samba.
, [global],
, ( ).
interfaces. , Samba
. :

interfaces = 10.23.2.0/255.255.255.0
interfaces = eth0

bind interfaces only. yes,


interfaces,
.
valid users. , .
:

valid users = jruser, bill

guest ok. true,


.
guest only. true,
.
browseable. ,
.
no , -
Samba,
.

12.4.3.
Samba
. , Unix
Windows, ,
Windows,
.
, Trivial
Database (TDB), .
[global] smb.conf,
Samba:
# use the tdb for Samba to enable encrypted passwords
security = user
passdb backend = tdbsam
obey pam restrictions = yes
smb passwd file = /etc/samba/passwd_smb
312 12.

smbpasswd.
obeypamrestrictions , ,
smbpasswd,
, PAM (Pluggable Authentication
Module, ) .
passdbbackend
TDB, : tdbsam:/etc/samba/private/passwd.tdb.

Windows, security = domain,


Samba .
, Samba,

Samba.


, Windows-
Samba,
smbpasswd-a:
# smbpasswd -a username
username smbpasswd
Linux.
passwd, smbpasswd
.
, smbpasswd
.
, -x smbpasswd:
# smbpasswd -x username
, -d;
-e :
# smbpasswd -d username
# smbpasswd -e username


Samba, superuser
smbpasswd - ,
:
# smbpasswd username
, Samba ,
Samba, smbpasswd.
, ,
. smb.conf ,
, :
unix password sync = yes
12.4. Samba 313

, smbpasswd
Samba. ,
, Samba -,
Linux, , .
Samba!

12.4.4.
, Samba
. , nmbd smbd
( smb_config_file
smb.conf):
# nmbd -D -s smb_config_file
# smbd -D -s smb_config_file
nmbd NetBIOS, smbd
. -D
. smb.conf smbd,
HUP
( systemctl initctl).

12.4.5.
- Samba,
.
log.nmbd log.smbd,
/var/log, /var/log/samba.
, .

12.4.6.

SMB (
- ), smb.conf ,
( label
, path ):
[label]
path = path
comment = share description
guest ok = no
writable = yes
printable = no
.
guest ok. .
public.
314 12.

writable. yes true .


,
.
printable. .
no false .
veto files. ,
. (
/pattern/).
, bin:

veto files = /*.o/bin/

12.4.7.
smb.conf [homes],
.
:
[homes]
comment = home directories
browseable = no
writable = yes

Samba /etc/passwd ,
,
[homes]. , , Samba
( Windows ,
Linux),
%S path. , ,
[homes] /u/user:
path = /u/%S

Samba %S.

12.4.8.
Windows, [printers]
smb.conf. ,
Unix CUPS:
[printers]
comment = Printers
browseable = yes
printing = CUPS
path = cups
printable = yes
writable = no
12.4. Samba 315

printing = CUPS, Samba


CUPS .

-
guestok=yes,
, . , -
, .

12.4.9. Samba
Samba smbclient
Windows . ,
,
Windows, Unix
.
smbclient, -L

SERVER:
$ smbclient -L -U username SERVER

-U username,
Linux SERVER.
smbclient .
, Enter;
SERVER.
, :
Sharename Type Comment
--------- ---- -------
Software Disk Software distribution
Scratch Disk Scratch space
IPC$ IPC IPC Service
ADMIN$ IPC IPC Service
Printer1 Printer Printer in room 231A
Printer2 Printer Printer in basement

Type .
Disk Printer ( IPC
).
. Sharename,
.

12.4.10.

, . -
316 12.

Uusername, Linux
.
$ smbclient -U username '\\SERVER\sharename'

,
, , :
smb: \>
smbclient ftp Unix,
:
get file file
;
put file file ;
cd dir dir ;
lcd localdir localdir ;
pwd ,
;
!command command .
: !pwd !ls,
;
help .

CIFS
Windows,

. .
SERVER:sharename \\SERVER\sharename.
# mount -t cifs SERVER:sharename mountpoint -o user=username,pass=password
mount , Samba
CIFS (Common Internet File System,
).
.

12.5. NFS
Unix
NFS (Network File System, );
NFS .
NFS TCP UDP
. ,
NFS ,
NFS.
12.6. 317

NFS,
,
CIFS:
# mount -t nfs server:directory mountpoint
tnfs , mount ,
,
nfs(5).
sec.
.
, Kerberos,
.
,
, ,
,
.
.
automount, amd,
automount systemd.

12.6.

NFS Linux-
, NFS.
(mountd nfsd) /etc/
exports, , .
NFS, ,
,
NAS, .
Linux,
NFS. NAS ,
,
, RAID
.
,
.
,
.
,
.
-, NFS.
NFS
,
318 12.

.
, , ,
,
.
, ,
, .
,
,
. ,
,
,
.
13

Linux,
.
- .
,
. ,
.

, , -
, .
,
( ) .
Linux, ,

. ,
(.).
, - ,
.
, , , .
, .

13.1.

.
, ,
.
, ,
,
, -
, .
, ,
.
320 13.


.
. , ,
, ,
.
, .
. ,
, .

13.2.
, ,
.
.
.
- , .
, , .
.
, .
, ,
/etc.
, , ,
. , .

13.3.
?
, .
?
?

, ,
.

13.3.1.
.
, ,
.
:
/usr/local/bin
/usr/bin
/bin
13.3. 321

,
,
/usr/local.
Linux
/usr/bin. ,
/usr/games,
, .
,
- . ,
, , - .
, .

/usr/local/bin.
bin
, :
$HOME/bin

$HOME/.local/bin.

( traceroute, ping lsmod),


sbin:
/usr/local/sbin
/usr/sbin
/sbin

(.)
, .
(.) ,
, ./ .
,
.
.
. :
- ls , .
, -
, sl ks.
. ,
.

13.3.2.

MANPATH, ,
/etc/manpath.config.
322 13.

13.3.3.
,
. :
.
; , ,
, .
, , -
, :
{ } = & < >

>, -
,
( , > ).

, , .
, bash .
bash
$ ( csh %):
PS1='\u\$ '
\u (.
PROMPTING ( bash(1))).
:
\h ( , );
\! ;
\w . ,
, \W;
\$ $,
#.

13.3.4.
-
,
. ,
. :
;
. which ,
, , ;
,
.
, , ,
.
13.4. . 323


,
.
,
.
, .
.

13.3.5.
umask ( )
. umask
, ,
. .
077. ,

. ,
, - .
, , ,
,
.
.
022.
, ,
, ,
, 077.

( ) , -
umask, 077, ,
.

13.4.
.
, , ,
. ,

, .
Unix: bash tcsh.

13.4.1. bash
bash .bash_profile, .profile, .bash_
login .bashrc. ,
324 13.

, , ? :
.bashrc .bash_profile,
.bashrc,
.
,
,
(, , )
.
, , ,
,
.


,
, , /bin/login.
SSH
. :
. ,
, echo$0; (-),
.
bash , /etc/
profile. .bash_profile, .bash_login
.profile, , .
,
,
. -l --login.


,
. ,
.
(xterm, GNOME Terminal ..)
, .
bash
/etc/bash.bashrc, .bashrc.


, ,
,
,
.
,
, ,
.
, .bash_profile,
.bashrc.
13.4. . 325


( ).
,
.
, (,
..) .bashrc,
. ,
.bash_profile, ,
.bashrc.

.bashrc
, .bashrc,
.bash_profile?
( )
:
# Command path.
PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
PATH=$HOME/bin:$PATH

# PS1 is the regular prompt.


# Substitutions include:
# \u username \h hostname \w current directory
# \! history number \s shell name \$ $ if regular user
PS1='\u\$ '

# EDITOR and VISUAL determine the editor that programs such as less
# and mail clients invoke when asked to edit a file.
EDITOR=vi
VISUAL=vi

# PAGER is the default text file viewer for programs such as man.
PAGER=less

# These are some handy options for less.


# A different style is LESS=FRX
# (F=quit at end, R=show raw characters, X=don't use alt screen)
LESS=meiX

# You must export environment variables.


export PATH EDITOR VISUAL PAGER LESS

# By default, give other users read-only access to most new files.


umask 022

, .bashrc
.bash_profile .
, .bash_profile, :
. $HOME/.bashrc
326 13.

,
.bashrc .bash_profile,
. ,
,
.bashrc,
i $-:
case $- in
*i*) # interactive commands go here
command
--snip--
;;
*) # non-interactive commands go here
command
--snip-
;;
esac

13.4.2. tcsh
csh Linux
tsch, C-,
,
. tsch
(
bash), ,
.
tsch
/ . tsch .tcshrc.
, .cshrc csh.
, .tcshrc
tcsh, csh. , ,
.cshrc .tcshrc; ,
- csh.
- csh ,
.cshrc .

.cshrc
.cshrc:
# Command path.
setenv PATH /usr/local/bin:/usr/bin:/bin:$HOME/bin

# EDITOR and VISUAL determine the editor that programs such as less
# and mail clients invoke when asked to edit a file.
setenv EDITOR vi
setenv VISUAL vi

# PAGER is the default text file viewer for programs such as man.
13.5. 327

setenv PAGER less

# These are some handy options for less.


setenv LESS meiX

# By default, give other users read-only access to most new files.


umask 022

# Customize the prompt.


# Substitutions include:
# %n username %m hostname %/ current directory
# %h history number %l current terminal %% %
set prompt="%m%% "

13.5.



.
.
.
, ,
( ,
..). , , ,
.
, ,
. ,
, .

.

13.5.1.
Linux
bash, :
,
( , csh
);
bash Linux;
bash GNU readline, , ,
;
bash
/ .
328 13.

Unix
, csh tcsh, , .
, , ,
, bash,
.
chsh,
.

(rc, ksh, zsh, es ..).


, , -
, zsh fish.

13.5.2.
vi emacs.
,
Unix, ,
. Linux
nano,
.
,
. setshowmatch .exrc
: ,
(, showmode,
).

13.5.3. PAGER
PAGER
less.

13.6.
.
.
DISPLAY .
.
.
,
.
LD_LIBRARY_PATH
(. 15.1.4).
13.7. , 329

13.7. ,

, Linux,
.
, ,
Linux, ,
.xsession, .xinitrc, ,
GNOME KDE.
,
Linux.
. ,
, ,
, . ,
. ,
,
. ,
, .
14
Linux
,
Linux.
, Linux,
,
,
.
Linux, ,
.
,
.
( ,
),
, .

, : X Window,
,
D-Bus, ,
.
, ,
(
, ),
, ,
. .

14.1.
Linux . ,
Linux (
),
. - , , ,
. , , ,
. Linux ,
14.1. 331

,
.
- ,
Linux X-
( X Window).
, ,
,
. X- ,
(. 14.4).
X- ,
-.
X-. X-,
, X- .
X- , , .
, X- .

14.1.1.
X- ,
;
. , ,
, ,
,
, .
.
. , Mutter/
GNOME Shell Compiz, ,
, Xfce.
, Linux,
,
. -
Linux,
,
.

14.1.2.
,
, .
-
. ,
Windows Mac OS X,
, . Linux
GTK+, ,
Qt .
332 14. Linux


, .

14.1.3.
,

. ,

.
, ,
.
Linux GNOME, KDE, Unity Xfce.
,

, ,
. ,
,
, ,
.

14.1.4.
,
. X- (
xclock), (, Chrome
LibreOffice). , , ,
,
. , - ,

.
D-Bus, 14.5.

14.2. X Window
X Window (http://www.x.org/) ,
, X-,
. , GNOME
KDE, X ,
, ,
.
X- . X.
; ,
, , :
/usr/bin/X :0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
14.2. X Window 333

:0, , . ,
,
/ .
, ,
. X-
DISPLAY
.

, :0.0 :0.1, ,
, RandR,
.

Linux X- .
vt7 , /dev/tty7
( ).
X- ,
,
.
Ctrl+Alt+Fn chvt.

14.2.1.
X- ,
, .
, .
X-
,
. ,
, ,
.
, gdm (
GNOME) kdm ( KDE). lightdm,
X-, -
, GNOME KDE.
X- ,
, startx xinit. ,
, , ,
, ,
.

14.2.2.
X- .
,
,
334 14. Linux

, X- TCP- 6000.
, , ,
.
, ,
, . ,
X- (
nolistentcp, ). X-
SSH-
, 10, Unix X-
.

14.3. X-

, ,
X Window. ,
.
xwininfo.
, .
$ xwininfo
xwininfo: Please select the window about which you
would like information by clicking the
mouse in that window.

,
:
xwininfo: Window id: 0x5400024 "xterm"

Absolute upper-left X: 1075


Absolute upper-left Y: 594
--snip--

X-
, .
, xlsclients-l.

, , . , ,
(. 14.3.2).

14.3.1.X-
X-
. X-
, udev
14.3. X- 335

D-Bus: X- (,
),
X-.
xev.
, ,
. , xev
, X-, . ,
, , :
$ xev
--snip--
MotionNotify event, serial 36, synthetic NO, window 0x6800001,
root 0xbb, subw 0x0, time 43937883, (47,174), root:(1692,486),
state 0x0, is_hint 0, same_screen YES

MotionNotify event, serial 36, synthetic NO, window 0x6800001,


root 0xbb, subw 0x0, time 43937891, (43,177), root:(1688,489),
state 0x0, is_hint 0, same_screen YES
.
x y , (root:)
.

, ,
. , ,
:
LeaveNotify event, serial 36, synthetic NO, window 0x6800001,
root 0xbb, subw 0x0, time 44348653, (55,185), root:(1679,420),
mode NotifyNormal, detail NotifyNonlinear, same_screen YES,
focus YES, state 0

FocusOut event, serial 36, synthetic NO, window 0x6800001,


mode NotifyNormal, detail NotifyNonlinear
xev
,
. L;
46:
KeyPress event, serial 32, synthetic NO, window 0x4c00001,
root 0xbb, subw 0x0, time 2084270084, (131,120), root:(197,172),
state 0x0, keycode 46 (keysym 0x6c, l), same_screen YES,
XLookupString gives 1 bytes: (6c) "l"
XmbLookupString gives 1 bytes: (6c) "l"
XFilterEvent returns: False
xev
-id id ( id ,
xwininfo)
-root.
336 14. Linux

14.3.2. X-

X- ,
,
. ,
Linux Caps Lock Control.
,
xmodmap
setxkbmap. ,
( ) ? ,
, . ,
.
,
.

( )
X- X Input Extension,
.
(), , .
,
X Input Extension ,
X-. ;
, , .
,
xinput--list:
$ xinput --list
Virtual core pointer id=2 [master pointer (3)]
Virtual core XTEST pointer id=4 [slave pointer (2)]
Logitech Unifying Device id=8 [slave pointer (2)]
Virtual core keyboard id=3 [master keyboard (2)
Virtual core XTEST keyboard id=5 [slave keyboard (3)]
Power Button id=6 [slave keyboard (3)]
Power Button id=7 [slave keyboard (3)]
Cypress USB Keyboard id=9 [slave keyboard (3)]

,
xinput .
2 3 , 8 9
. ,
.
X- ,
,
.
X Input Extension.
, - .
14.3. X- 337

.
, xinput , :
$ xinput --list-props 8
Device 'Logitech Unifying Device. Wireless PID:4026':
Device Enabled (126): 1
Coordinate Transformation Matrix (128): 1.000000, 0.000000, 0.000000,
0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000
Device Accel Profile (256): 0
Device Accel Constant Deceleration (257): 1.000000
Device Accel Adaptive Deceleration (258): 1.000000
Device Accel Velocity Scaling (259): 10.000000
--snip--
, ,
--set-prop.
xinput(1).


, ,
xinput, ().
,
--set-ptr-feedback --set-button-map
xinput. , dev
( ),
:
$ xinput --set-button-map dev 3 2 1


,
, .
X
, , xmodmap,
XKB (X-),
.
XKB ,
xmodmap, .
, XKB, :
xkbcomp,
X- setxkbmap.
.
, .
, Caps Lock
Control,
.

.
338 14. Linux


xsetroot X
,
, .
,
, .
(,
gsettings GNOME),
, , , .

xset
xset.
, xsetq,
. ,
DPMS (Display Power Management
Signaling, ).

14.4. X Window
,
X ,
, .
. X Window 80- .
(
),
.
X Window ,
,
. , , :
, , ,
, .

, .
, -
.
, (Wayland), .
Wayland ,
, .

X. , Wayland
. Linux, GNOME KDE,
Wayland.
Wayland X.
, , Mir,
14.5. D-Bus 339

, . -
,
- .
,
Linux.
X Window
, ,
,
Linux.
.

14.5. D-Bus
, Linux,
(D-Bus, Desktop Bus) .
D-Bus ,
, .
, Linux
, USB-.
,
,
.
, IPC,
Unix. D-Bus
, dbus-daemon. ,
,
.
. , udisks-daemon ubus
, dbus-daemon,
, .

14.5.1.
D-Bus Linux,
. , systemd Upstart
D-Bus.
Linux.
, () dbus-daemon,
. ,
init --system.
D-Bus,
/etc/dbus-1/system.conf (, ,
).
Unix /var/run/dbus/system_bus_socket.
340 14. Linux

D-Bus
, ,
.
.

14.5.2. D-Bus

dbus-daemon ,
. dbus-monitor
:
$ dbus-monitor --system
signal sender=org.freedesktop.DBus -> dest=:1.952 serial=2 path=/org/
freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
string ":1.952"

, .
,
. - ,
USB-.
.
, ,
:
$ dbus-monitor --session

;
D-Bus,
.

14.6.
Linux .
.
1. , ,
PostScript. .
2. .
3. .
4. ,
.
5. PostScript,
.
6. PostScript,
, .
14.6. 341

7. ,
.
8. ,
.

PostScript. , ,
, .
PostScript Unix,
.tar .
PDF,
.
,
.

14.6.1. CUPS
Linux CUPS (http://www.cups.org/).
Mac OS X. CUPS cupsd,
lpr
.
CUPS IPP (Internet
Print Protocol, ). ,
, HTTP,
TCP-631. ,
CUPS, , , http://localhost:631/,
.
IPP.
Windows, .
, -
, .
,
.
, /etc/cups. ,
,
.
,
, .

14.6.2.
, ,
PostScript PDF. Linux
, , .
CUPS RIP (Raster Image Processor,
), .
342 14. Linux

RIP Ghostscript (gs)


, ,
. ,
CUPS, PPD (PostScript Printer Definition,
PostScript), ,
.

14.7. ,

Linux ,
, , , ,
, .
, ,
http://www.freedesktop.org/. ,
Ayatana, Unity Mir.
Linux
Chromium OS , Google Chrome OS,
Chromebook. Linux,
, ,
Chromium/Chrome. , ,
Chrome OS.
15

Linux Unix
,
,
. Linux
, ,
,
Unix,
.
, ,
.
,
, .
. ,
, , ,
. , , ,
, .

15.1.C
, C,
,
Linux. Linux,
C C++.
C,
C++.
C :
, , . ,
C ,
, ,
. (
), .
344 15.

,
C, .
, -
Debian/Ubuntu -
Fedora/CentOS yum. ,
C compiler.

C Unix
GNU C, gcc, clang,
LLVM, . C
.c. hello.c
C, . (BrianW.Kernighan)
. (DennisM.Ritchie) The C Programming Language (
C), 2- (Prentice Hall, 1988):
#include <stdio.h>

main() {
printf("Hello, World.\n");
}
hello.c,
:
$ cc hello.c
a.out,
.
(, hello).
, -o:
$ cc -o hello hello.c
.
(. 15.1.2
15.1.3), , , ,
.

15.1.1.
C ,
.
,
.
,
.
.c
. -c
, . , , ,
15.1.C 345

, main.c aux.c.
:
$ cc -c main.c
$ cc -c aux.c

:
main.o aux.o.
,
, . -,
, , -, , ,

, .

, , ld
Unix. ,
C , .
myprog
:
$ cc -o myprog main.o aux.o

,
, ,
. make, 15.2,
Unix .
, .

15.1.2. (Include)
C ,
. ,
stdio.h (. 15.1).
,
. ,
. ,
, ,
.

,

.

, .
, :
badinclude.c:1:22: fatal error: notfound.h: No such file or directory
346 15.

,
notfound.h, badinclude.c.
badinclude.c:
#include <notfound.h>
Unix /usr/include;
,
. ,
(
include - ).

16 , .

, notfound.h /usr/junk/include.
,
-I:
$ cc -c -I/usr/junk/include badinclude.c
badinclude.c,
.
,
("") (<>), :
#include "myheader.h"
,
.
, ,
. ,
, , .

C (cpp)?
, C
. C ,
,
.
, ; ,
(
).
,
#. .
. #include ,
. ,
-I ,
,
.
15.1.C 347

. , #defineBLAHsomething,
, BLAH
something .
, ,
,
. .

.

, -
: -DBLAH=something ,
.

.
#ifdef, #if #endif. #ifdefMACRO ,
MACRO , #ifcondition ,
condition .
, , if, ,
,
#if #endif. ,
- C.
.
, , DEBUG, ,
, fprintf().

#endif:
#ifdef DEBUG
fprintf(stderr, "This is a debugging message.\n");
#endif

C C, , .
.

Unix C cpp,
gcc-E.
.

15.1.3.
C ,
.
. C ,
, .
, math,
.
348 15.

,
- .
, , gobject,
,
, :
badobject.o(.text+0x28): undefined reference to 'g_object_new'
.
badobject.o, ,
, , ,
. ,
gobject, g_object_new().

, .
. ,
.

, gobject,
-l,
. ,
( /usr/lib),
lib.
gobject libgobject.a, gobject.
, :
$ cc -o badobject badobject.o -lgobject

; -L. , badobject
libcrud.a /usr/junk/lib.
, , :
$ cc -o badobject badobject.o -lgobject -L/usr/junk/lib -lcrud

, nm.
. , , : nmlibgobject.a. -
locate, libgobject.a; -
, , /usr/lib.

15.1.4.
, .a (, libgobject.a),
.
,
. ,
, ,
.
15.1.C 349

,
;
. ,
,
,
, .
.
, ,
, .
.
,
, .
:
.
, .
,
.
.
.
.
,
. ,
, ,
. (JohnR.Levine) Linkers and Loaders (
, Morgan Kaufmann, 1999) . (DavidM.Beazley),
. (BrianD.Ward) . (IanR.Cooke) The Inside Story on
Shared Libraries and Dynamic Loading (
, Computing in Science&Engineering,
/ 2001), -, Program Library
HOWTO (http://dwheeler.com/program-library/).
ld.so(8).



,
. Linux
/lib /usr/lib. /lib .
.so (shared
object ), , , libc-2.15.so
libc.so.6. ,
, lddprog ( prog
). :
$ ldd /bin/bash
linux-gate.so.1 => (0xb7799000)
350 15.

libtinfo.so.5 => /lib/i386-linux-gnu/libtinfo.so.5 (0xb7765000)


libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb7760000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75b5000)
/lib/ld-linux.so.2 (0xb779a000)

;
, , , .
ld.so ( / )

. ldd
. ,
ld.so .

ld.so: ld-linux.so.2.

ld.so


, .
,
, -
(rpath) ,
. , , .
/etc/ld.so.cache,
, .
, ,
/etc/ld.so.conf.

ld.so.conf, Linux, -
, , , /etc/ld.so.conf.d.

ld.so.conf ,
. :
/lib/i686-linux-gnu
/usr/lib/i686-linux-gnu
/lib /usr/lib ,
, /etc/ld.so.conf.
ld.so.conf
, /etc/ld.so.ca
che :
# ldconfig -v
-v (
ldconfig ),
.
15.1.C 351

, ld.so
: LD_LIBRARY_PATH. .
- /etc/ld.so.conf. ,
,
,
. ,
,
. , .



, libweird.so.1
/opt/obscure/lib, myprog.
:
$ cc -o myprog myprog.o -Wl,-rpath=/opt/obscure/lib -L/opt/obscure/lib -lweird

-Wl,-rpath ,

. , -Wl,-rpath, -L -
.
, patchelf,
,
.

,

,
,
,
. :
;
;
.

LD_LIBRARY_PATH.
, ,
ld.so
- .
,
patchelf, ,
. , ,
.
LD_LIBRARY_PATH
.
352 15.

,
, .
, , ,
,
.
LD_LIBRARY_PATH,
- , ( ,
, Mozilla - ),
. , /opt/crummy/bin/
crummy.bin /opt/
crummy/lib. crummy :
#!/bin/sh
LD_LIBRARY_PATH=/opt/crummy/lib
export LD_LIBRARY_PATH
exec /opt/crummy/bin/crummy.bin $@
LD_LIBRARY_PATH,
.
:
(API) -
,
. :
,
-Wl,-rpath
,
.

15.2. make
,
,
. ,
Unix make.
, Unix,
.
. make ,
Managing Projects with GNU Make (
GNU Make) (Robert Mecklenburg) (OReilly, 2004).
, Linux
, make .
; Autotools 16.
make , ,
, . Makefile makefile,
, make. make,
, - .
15.2. make 353

make , .
( .o, ..) . ,
; ,
.o, .
.
, make - , ,
, .c .o.
make , ,
.

15.2.1. Makefile
Makefile myprog aux.
c main.c:
# object files
OBJS=aux.o main.o

all: myprog

myprog: $(OBJS)
$(CC) -o myprog $(OBJS)
# .
;
OBJS . .
,
( ($(OBJS) ).
Makefile , all.
, make ,
make .
. all
Makefile , - myprog.
; all myprog. , myprog
.
( all OBJS).
myprog, Makefile
$(OBJS) . aux.o main.o,
myprog (
, Makefile ).
Makefile ,
C: aux.c main.c . make
Makefile, , :
$ make
cc -c -o aux.o aux.c
cc -c -o main.o main.c
cc -o myprog aux.o main.o
354 15.

.15.1.

.15.1. Makefile

15.2.2.
make , aux.c
aux.o? aux.c Makefile. : make
. , .c,
.o, , , , cc-c
.c, .o.

15.2.3.
myprog ,
. $(OBJS),
C (
$(CC) ):
$(CC) -o myprog $(OBJS)
$(CC) .
, .
:
Makefile:7: *** missing separator. Stop.
, Makefile .
, , - ,
.

15.2.4.
make ,
.
15.2. make 355

make , myprog,
:
make: Nothing to be done for 'all'.
make ,
myprog , ,
, .
, .
1. touch aux.c.
2. make . , aux.c
, aux.o, ,
aux.o .
3. myprog aux.o, aux.o
, myprog, make
myprog .
.

15.2.5.
make, ,
.

. Makefile
makeaux.o, aux.o.
. , ,
clang, :
$ make CC=clang
make CC
cc.
,
CFLAGS LDFLAGS,
.
Makefile make.
,
. ,
blah.c, makeblah.
:
$ make blah
cc blah.o -o blah

.
, , ,
Makefile. Makefile
356 15.

, - Fortran, Lex Yacc


, .
make ? , ,
, ,
.
make :
-n , ,
make - ;
-f file make file
Makefile makefile.

15.2.6.

make .
,
-,
, .
,
Makefile. .
CFLAGS. C. .c
make .
LDFLAGS. CFLAGS,
.
LDLIBS. LDFLAGS,
,
.
CC. C. cc.
CPPFLAGS. C. make -
,
.
CXXFLAGS. GNU make
C++.
make , .
make ,
$:
$@ ;
$* . ,
blah.o, blah.
Linux info-
make.
15.2. make 357

, GNUmake ,
, . , Linux, ,
Solaris BSD ,
, . ,
, GNU autotools.

15.2.7.
Makefiles ,
, .
clean. clean ; makeclean
make ,
.
Makefile myprog:
clean:
rm -f $(OBJS) myprog
distclean. Makefile, GNU autotools,
distclean, ,
, Makefile.
16. ,
, -
realclean.
install.
, Makefile . ,
makeninstall, ,
, .
test check. test check,
, .
depend. , -M
. ,
Makefile. , ,
,
, .
all. Makefile.
, .

15.2.8. Makefile
Makefile,
.
Makefile ( )
,
:
358 15.

MYPACKAGE_INCLUDES=-I/usr/local/include/mypackage
MYPACKAGE_LIB=-L/usr/local/lib/mypackage -lmypackage
PNG_INCLUDES=-I/usr/local/include
PNG_LIB=-L/usr/local/lib -lpng

:
CFLAGS=$(CFLAGS) $(MYPACKAGE_INCLUDES) $(PNG_INCLUDES)
LDFLAGS=$(LDFLAGS) $(MYPACKAGE_LIB) $(PNG_LIB)

. , , ,
boring trite. .c
util.c. :
UTIL_OBJS=util.o

BORING_OBJS=$(UTIL_OBJS) boring.o
TRITE_OBJS=$(UTIL_OBJS) trite.o

PROGS=boring trite
Makefile :
all: $(PROGS)

boring: $(BORING_OBJS)
$(CC) -o $@ $(BORING_OBJS) $(LDFLAGS)
trite: $(TRITE_OBJS)
$(CC) -o $@ $(TRITE_OBJS) $(LDFLAGS)

, ,
Makefile
. ,
: boring trite,
trite boring.c, boring trite.c, make
,
.

,
, . -
, -
.

15.3.
Linux gdb;
, Eclipse IDE Emacs.
,
15.4. Lex Yacc 359

-g
. gdb program,
:
$ gdb program
(gdb). program
options,
:
(gdb) run options
, ,
. , gdb ,
(gdb).
,
- ,
. print C.
(gdb) print variable
,
. file
, line_num ,
:
(gdb) break file:line_num
:
(gdb) continue
, :
(gdb) clear file:line_num
gdb
, , ,
10- . (RichardM.Stallman) .
Debugging with GDB ( GDB, GNU Press, 2011).
(Norman Matloff)
(Peter Jay Salzman) The Art of Debugging (
, No Starch Press, 2008).

, -
Valgrind (http://valgrind.org/).

15.4. Lex Yacc


Lex Yacc ,
.
.
360 15.

Lex (tokenizer),
. GNU/Linux flex.
-ll
-lfl.
Yacc ,
. GNU bison;
Yacc bison-y.
-ly.

15.5.
Unix
, Bourne shell awk.
( 11) -
Unix, awk .
,
C, (,
whois). .
:
, Bourne shell.
, Python :
#!/usr/bin/python
:
#!/usr/bin/env python
Linux , #!,
. , ,
. Unix
, #!,
, .
, :
#!/usr/bin/tail -2
This program won't print this line,
but it will print this line...
and this line, too.

: .
, myscript. ,
tail /bin /usr/bin?
myscript :
bash: ./myscript: /usr/bin/tail: bad interpreter: No such file or directory
,
. -2 ,
15.5. 361

, -2
, .
,
, .
.

15.5.1.Python
Python
, , ,
.
.
python
/usr/bin. Python
.
. ,
, 4- . (DavidM.Beazley) Python
Essential Reference ( Python, Addison-Wesley,
2009).

15.5.2.Perl
Unix, ,
Perl.
. Perl
Python, , , ,
. ,
. , , 6-
. (RandalL.Schwartz), . (BrianD.Foy)
(Tom Phoenix) Learning Perl ( Perl, OReilly,
2011). Modern Perl (
Perl) Chromatic (Onyx Neon Press, 2014).

15.5.3.
.
PHP.
-. PHP
. PHP http://www.php.net/.
Ruby. - -
(http://www.ruby-lang.org/).
JavaScript.
.
-
,
362 15.

-.
Node.js node.
Emacs Lisp. Lisp,
Emacs.
Matlab, Octave. Matlab
.
Octave.
R. .
http://www.r-project.org/
(Norman Matloff) The Art of R Programming (
R, No Starch Press, 2011).
Mathematica.
.
m4. ,
GNU autotools.
Tcl (Tool command language, ).
,
Tk Expect. Tcl
, ,
. Tk,
.
http://www.tcl.tk/.

15.6.Java
Java , C,
- .
Unix . ,
-, .
, Android Java.
Linux, ,
, .
Java: ,
( ), -
- ( -
, ,
, 17). Linux
-.
- Java .class.
Java (JRE, Java runtime environment) ,
-. -,
:
$ java file.class
15.7. : 363

-, .jar;
.class. .jar,
:
$ java -jar file.jar
JAVA_HOME
Java. , CLASSPATH,
, .
-,
PATH .
.java -,
Java- (JDK, Java Development Kit). javac
JDK .class :
$ javac file.java
JDK jar,
.jar. tar.

15.7. :

.
, ,
Go (golang) Swift.
LLVM (http://llvm.org/)
.
, : Compilers: Principles, Techniques
and Tools (: , ) .
(Alfred V. Aho) (2- , Addison-Wesley, 2006) Modern Compiler Design (
) (Dick Grune) . (2- ,
Springer, 2012).
-, .
,
, , .
, Linux
.
16



C
Unix
, .

Unix ( Linux),
.
, Unix-
,
.
, Linux,
: C .
, ()
. , , ,
,
- .
Linux
, /bin,
,
. ,
. ,
:
;
, .
;
, .
,
( Python);
, .
16.2. C 365

16.1.

Linux ,
C , Python.

, Linux.

C ,
GNU Autotools. ,
Linux .
, make,
, .
C
.
1. .
2. .
3. make .
4. makeinstall ,
.

, 15, .

16.2.
C
- .tar.gz,
.tar.bz2 .tar.xz, ,
2.18.
tartvf tarztvf,
, .
, , ,
:
package-1.23/Makefile.in
package-1.23/README
package-1.23/main.c
package-1.23/bar.c
--snip--
,
( package-1.23 ):
Makefile
README
366 16. C

main.c
--snip--

. ,
. , ,
, :
/etc/passwd
/etc/inetd.conf
, , -
. , -
- .
.
.
, README INSTALL.
README, , ,
.
INSTALL, .
.
README INSTALL ,
.
, make, Makefile, Makefile.in, configure
CMakeLists.txt. Makefile,
,
, GNU Autoconf CMake.
( configure CMakeLists.txt),
Makefile Makefile.in,
.
, .c, .h .cc.
C .
C++ .cc, .C .cxx.
, .o, .
,
,
-
, .
( ) ,
makeclean,
.

16.3. GNU Autoconf


C ,

16.3. GNU Autoconf 367

Makefile.
Makefile
, .
, Makefiles ,
.
GNU Autoconf
Makefile. , ,
configure, Makefile.in config.h.in. .in
; , ,
,
.in .
; Makefile Makefile.in,
configure:
$ ./configure
,
.
, configure Makefile
config.h, (config.cache),
.
make .
configure , make
, (. 16.6,
).
.

,
. Debian Ubuntu ,
; , Fedora, groupinstall.

16.3.1. Autoconf
, Autoconf,
, , .
GNU coreutils ( ,
). - http://ftp.gnu.org/
gnu/coreutils/ ( ), ,

:
$ ./configure --prefix=$HOME/mycoreutils
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
--snip--
config.status: executing po-directories commands
config.status: creating po/POTFILES
config.status: creating po/Makefile
368 16. C

make:
$ make
GEN lib/alloca.h
GEN lib/c++defs.h
--snip--
make[2]: Leaving directory '/home/juser/coreutils-8.22/gnulib-tests'
make[1]: Leaving directory '/home/juser/coreutils-8.22'

,
, ./src/ls, makecheck,
. ,
.
.
maken, , makeinstall,
:
$ make -n install

, (
, mycoreutils),
:
$ make install


mycoreutils, bin, share .
bin (
, 2). ,
mycoreutils , ,
, .

16.3.2.

,

. Debian, Ubuntu, ,
: make install
checkinstall :
# checkinstall make install

--pkgname=name, .
RPM ,
.
rpmdev-setuptree;
rpmbuild, .
.
16.3. GNU Autoconf 369

16.3.3. configure
configure:
--prefix .
install Autoconf Makefile
/usr/local /usr/local/bin,
/usr/local/lib ..
:
$ ./configure --prefix=new_prefix
configure --help,
. ,
, ,
, .
--bindir=directory. directory.
--sbindir=directory.
directory.
--libdir=directory. directory.
--disable-shared.
.
(. 15.1.4).
--with-package=directory. configure , package
directory. ,
. ,
,
.


,
. ,
- configure
. , configure
,
.
, ,
. ,

.

16.3.4.
configure ,
configure make.
CPPFLAGS, CFLAGS LDFLAGS. :
370 16. C

configure .
, CPPFLAGS
CFLAGS , configure
.
bash
configure ./configure
. , DEBUG
, :
$ CPPFLAGS=-DDEBUG ./configure

configure, :
$ ./configure CPPFLAGS=-DDEBUG

, configure ,
. ,
, include_dir,
:
$ CPPFLAGS=-Iinclude_dir ./configure

15.2.6,
lib_dir, :
$ LDFLAGS=-Llib_dir ./configure

lib_dir (.
15.1.4), , ,
.
-rpath -L:
$ LDFLAGS="-Llib_dir -Wl,-rpath=lib_dir" ./configure

.
. ,
-I, :
$ CPPFLAGS=Iinclude_dir ./configure

:
configure: error: C compiler cannot create executables
See 'config.log' for more details

config.log, ,
:
configure:5037: checking whether the C compiler works
configure:5059: gcc Iinclude_dir conftest.c >&5
gcc: error: Iinclude_dir: No such file or directory
configure:5063: $? = 1
configure:5101: result: no
16.3. GNU Autoconf 371

16.3.5. Autoconf
configure , ,
Makefile ,
all install.
make clean. , .
make distclean. makeclean,
, Makefiles, config.h, config.log ..
,
makedistclean.
make check.
; makecheck
.
make install-strip. makeinstall,
.
.

16.3.6. Autoconf
- ,
, config.log, . ,
,
.

config.log (, , G less)
, .
- , configure
, ,
. ,
,
, formoredetails -
configure. ,
less
?. , ,
.

16.3.7. pkg-config
,
.
,
. , ,
OpenSSH, OpenSSL.
OpenSSH ,
OpenSSL ?
372 16. C

pkg-config
,
,
. :
$ pkg-config options package1 package2 ...
, , , OpenSSL,
:
$ pkg-config --libs openssl
:
-lssl -lcrypto
, pkg-config,
:
$ pkg-config --list-all

pkg-config
, , pkg-config
, , .pc.
, openssl.pc OpenSSL
Ubuntu ( /usr/lib/i386-linux-gnu/pkgconfig):
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib/i386-linux-gnu
includedir=${prefix}/include

Name: OpenSSL
Description: Secure Sockets Layer and cryptography libraries and tools
Version: 1.0.1
Requires:
Libs: -L${libdir} -lssl -lcrypto
Libs.private: -ldl -lz
Cflags: -I${includedir} exec_prefix=${prefix}
, , ,
-Wl,-rpath=${libdir},
. :
pkg-config .pc? pkg-config
lib/pkgconfig . ,
pkg-config, /usr/local, /usr/
local/lib/pkgconfig.

pkg-config

, pkg-config .pc
. .pc
16.4. 373

, /opt/openssl/lib/pkgconfig/openssl.pc,
pkg-config.
.pc
pkg-config.
( ) .pc
pkgconfig.
PKG_CONFIG_PATH ,
.
.

16.4.
, ,
, .
Linux ,
, ,
. :
;
,
;
, ;
;
(
).
:
;
.
, .
, ,
, ;
, ;
.
, , , coreutils,
(ls, cat ..),
. ,
, Apache,
.
. GNU Autoconf
/usr/local,
. /usr/local,
.
/usr/local .
374 16. C

,
.
/usr/local, , .
- ,
, 16.3.2.

16.5.

(, git).

, .
diff, ,
diff.
:
--- src/file.c.orig 2015-07-17 14:29:12.000000000 +0100
+++ src/file.c 2015-09-18 10:22:17.000000000 +0100
@@ -2,16 +2,12 @@
.
(---), , ,
, .
, src/file.c.
, ,
src, src.
, patch:
$ patch -p0 < patch_file
, patch ,
.
:
File to patch:
, ,
,
. , , :
, ,
, .
,
, , :
--- package-3.42/src/file.c.orig 2015-07-17 14:29:12.000000000 +0100
+++ package-3.42/src/file.c 2015-09-18 10:22:17.000000000 +0100
(
), patch,
. , , ,
16.6. 375

src ( ). patch package-3.42/


( ), -p1:
$ patch -p1 < patch_file

16.6.

,
,
, 15,
,
. .
, Autoconf -
, , .
, ,
make.
. , :
make: *** [target] Error 1
Makefiles ,
- , , .
:
make: *** [target] Error 1 (ignored)
GNU make ,

[N], N . ,
make,
. :
[compiler error message involving file.c]
make[3]: *** [file.o] Error 1
make[3]: Leaving directory '/home/src/package-5.0/src'
make[2]: *** [all] Error 2
make[2]: Leaving directory '/home/src/package-5.0/src'
make[1]: *** [all-recursive] Error 1 make[1]: Leaving directory '/home/src/
package-5.0/'
make: *** [all] Error 2
: file.c,
/home/src/package-5.0/src. ,
, .
, make,
.
. ,
.
376 16. C


:
src.c:22: conflicting types for 'item'
/usr/include/file.h:47: previous declaration of 'item'


item
22 src.c. , (
, #ifdef - ).

:
src.c:37: 'time_t' undeclared (first use this function)
--snip--
src.c:37: parse error before '...'

.
.
( 37 src.c). ,
:
time_t v1;
, v1 -
. , :
v1 = time(NULL);
man2time man3time,
time().
:
SYNOPSIS
#include <time.h>

time_t time(time_t *t);


, time() time.h.
#include<time.h> src.c .

():
src.c:4: pkg.h: No such file or directory
(long list of errors follows)

C src.c,
pkg.h. , , ,
,
. ,
-I C (CPPFLAGS), .
16.7. 377

, -L,
.
,
, ,
.
Makefile README, .
Debian,
apt-file :
$ apt-file search pkg.h
. ,
yum, :
$ yum provides */pkg.h

make:
make: prog: Command not found

, prog.
- cc, gcc ld,
. , , prog
, Makefile,
prog.
make prog,
, , (.) .
$PATH ,
Makefile prog ./prog. ,
.

16.7.
.
, ,
.
, , -
Autoconf (, CMake SCons).
.
, .
GNU Autoconf
(John Calcote) Autotools ( Autotools) (No
Starch Press, 2010).
Linux.
. ,
.
378 16. C

. , ,
.
: ,
, .
. Linux

. ,
,
- .
, debuild Debian
mock RPM.

. ,
, ,
.
: ,
.
17


Linux, ,
,
. . Linux
,
. ,
Linux, , , .

17.1.-
Linux -,
Linux Apache HTTP Server (
Apache). -,
, Tomcat ( Apache);
Java-.
- : ,
. -, Apache,
-.
, Wikipedia MediaWiki,
, wiki-.
Wordpress Drupal
.
, Linux. ,
, MediaWiki, Wordpress Drupal PHP.
, -,
,
, Django, Flask Rails,
- ,
, ,
.
-
. ,
, 810.
380 17.

, , , ,
, .
,
.

17.2.
,
Linux .
:

, .
,
. ,
/etc/passwd /etc/shadow
Linux . ,
LDAP (Lightweight Directory Access Protocol,
() ),
Linux. Linux
; , , /etc/nsswitch.conf
.
,
, ,
, . ,
, , ,
. ,
, . -
,
.
. :
. (
, ; Relational Database Management
Systems, RDBMS), MySQL, PostgreSQL, Oracle MariaDB,
,
. , :
,
.
, .

SQL (Structured Query Language, ).
, NoSQL,
,
. , ,
MongoDB, .
, redis,
17.4. 381

. NoSQL ,
SQL.
.
, ,
8, ,
,
( ) .
,
.
: , ,
, ,
.

17.3.

,
, , ,
, .

( ) ,
.
.
Linux , KVM (
) Xen. -
. Apache
,
. ,
, .

, ,
, .
, , -
. Linux
, ,
Linux ,
Linux.

17.4.

,
. -
,
382 17.

. , (IaaS, Infrastructure
as a Service), ,
, , ,
, . OpenStack
,
IaaS.
,
, ,
-. , ,
(PaaS, Platform as a Service).
Linux ,
. ,
, , .

17.5.
-, ,
, .
,
( - ).

:
( ) ,
. Android , ,
Linux.

. ,
, ,
Linux.
,
. ,
,
.
, ,
. OpenWRT,
9, Linux.
,
,
(SoC, System-on-a-chip),
, . ,
Raspberry Pi BeagleBone ,
Linux.
,
Python,
.
17.6. 383

Linux ,
/ . ,
, ,
. ,
BusyBox.

Linux, , System V init.
,
. , Raspberry Pi,

,
.
, ,
Linux, : ,
, init,
. , , ( ) ,
.
, .

17.6.
Linux,
, . ,
-
. .
.

Linux
.

.
.
.
.
. , .
.

, 192102, -, . (. ), 3, , . 7.
034-2014, 58.11.12
, .
30.10.15. 70100/16. . . . . 30,960. 1000. 0000.
. .
142300, , . , . , 1.
: www.chpk.ru. E-mail: marketing@chpk.ru
: 8(496) 726-54-10, : (495) 988-63-87