View difference between Paste ID: N8NDKG7E and 6cPavK1N
SHOW: | | - or go back to the newest paste.
1
                               #############################
2
############################## # Day 1: Linux Fundamentals # ##############################
3
                               #############################
4
5
6
7
8
9
10
11
12
#####################################################
13-
# 2020 Intro to Linux & Comptia Linux+ Exam Prep    # 
13+
# 2021 Intro to Linux & Comptia Linux+ Exam Prep    # 
14
# By Joe McCray                                     #
15
#####################################################
16
17
- Here is a good set of slides for getting started with Linux:
18
http://www.slideshare.net/olafusimichael/linux-training-24086319
19
20
21
- Here is a good tutorial that you should complete before doing the labs below:
22
http://linuxsurvival.com/linux-tutorial-introduction/
23
24
25-
- I prefer to use Putty to SSH into my Linux host.
25+
site:	https://app.shellngn.com/
26-
- You can download Putty from here:
26+
user:	joseph.mccray@gmail.com
27-
- http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
27+
pass:	P@ssw0rd123!@#123
28
 
29-
Here is the information to put into putty
29+
30
NOTE: Ask me for the correct password
31-
Host Name:          149.28.201.171
31+
32-
protocol:           ssh
32+
33-
port:               22
33+
34-
username:           linuxtraining
34+
35-
password:           linux!training123!
35+
36
37
---------------------------Type This-----------------------------------
38
cd ~
39
40
pwd
41
42
whereis pwd
43
44
which pwd
45
46
sudo find / -name pwd
47
48
/bin/pwd
49
50
cd ~/students/
51
52
mkdir yourname					<---- replace 'yourname' with your first name in lowercase with no spaces or special characters please
53
54
cd yourname						<---- replace 'yourname' with your first name in lowercase with no spaces or special characters please
55
56
touch one two three
57
58
ls -l t							(without pressing the Enter key, press the Tab key twice. What happens?)
59
60
h								(and again without pressing the Enter key, press the Tab key twice. What happens?)
61
62
Press the 'Up arrow key'		(What happens?)
63
64
Press 'Ctrl-A'					(What happens?)
65
66
ls
67
68
clear							(What happens?)
69
70
echo one > one
71
72
cat one							(What happens?)
73
74
man cat							(What happens?)
75
	q
76
77
cat two
78
79
cat one > two
80
81
cat two
82
83
cat one two > three
84
85
cat three
86
87
echo four >> three
88
89
cat three 						(What happens?)
90
91
wc -l three
92
93
man wc
94
	q
95
96
info wc
97
	q
98
99
cat three | grep four
100
101
cat three | grep one
102
103
man grep
104
	q
105
106
107
man ps
108
	q
109
110
ps
111
112
ps aux
113
114
ps aux | less
115
116
Press the 'Up arrow key'		(What happens?)
117
118
Press the 'Down arrow key'		(What happens?)
119
	q
120
121
top
122
    q
123
-----------------------------------------------------------------------
124
125
126
#########
127
# Files #
128
#########
129
---------------------------Type This-----------------------------------
130
cd ~
131
132
pwd
133
134
cd ~/students/yourname/
135
136
pwd
137
138
ls
139
140
mkdir LinuxBasics
141
142
cd LinuxBasics
143
144
pwd
145
146
ls
147
148
mkdir files
149
150
touch one two three
151
152
cp one files/
153
154
ls files/
155
156
cd files/
157
158
cp ../two .
159
160
ls
161
162
cp ../three .
163
164
ls
165
166
tar cvf files.tar *
167
168
ls
169
170
gzip files.tar
171
172
ls
173
174
rm -rf one two three
175
176
ls
177
178
tar -zxvf files.tar.gz
179
180
rm -rf files.tar.gz
181
182
zip data *
183
184
unzip -l data.zip
185
186
mkdir /tmp/yourname/
187
188
unzip data.zip -d /tmp/yourname/
189
-----------------------------------------------------------------------
190
191
192
193
############
194
# VIM Demo #
195
############
196
---------------------------Type This-----------------------------------
197
cd ~/students/yourname/LinuxBasics
198
199
mkdir vimlesson
200
201
cd vimlesson
202
203
vi lesson1.sh
204
205
i								(press "i" to get into INSERT mode and then paste in the lines below)
206
207
#!/bin/bash
208
209
echo "This is my first time using vi to create a shell script"
210
echo " "
211
echo " "
212
echo " "
213
sleep 5
214
echo "Ok, now let's clear the screen"
215
sleep 3
216
clear
217
218
219
---------------don't put this line in your script----------------------------
220
221
ESC			(press the ESC key to get you out of INSERT mode)
222
223
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
224
225
226
wq			(typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
227
228
229
230
vi lesson1.sh
231
232
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
233
234
set number 	(typing "set number" immediately after SHIFT: will add line numbers to vim).
235
236
wq			(typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
237
238
239
240
241
vi lesson1.sh
242
243
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
244
245
set number 	(typing "set number" immediately after SHIFT: will add line numbers to vim).
246
247
248
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
249
250
/echo		(typing "/echo" immediately after SHIFT: will search the file for the word echo).
251
252
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
253
254
wq			(typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
255
256
257
258
259
vi lesson1.sh
260
261
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
262
263
set number 	(typing "set number" immediately after SHIFT: will add line numbers to vim).
264
265
266
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
267
268
4		(typing "4" immediately after SHIFT: will take you to line number 4).
269
270
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
271
272
wq			(typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
273
274
275
276
277
vi lesson1.sh
278
279
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
280
281
set number 	(typing "set number" immediately after SHIFT: will add line numbers to vim).
282
283
284
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
285
286
4		(typing "4" immediately after SHIFT: will take you to line number 4).
287
288
dd		(typing "dd" will delete the line that you are on)
289
290
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
291
292
wq			(typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
293
294
295
296
297
vi lesson1.sh
298
299
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
300
301
set number 	(typing "set number" immediately after SHIFT: will add line numbers to vim).
302
303
304
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
305
306
4		(typing "4" immediately after SHIFT: will take you to line number 4).
307
308
dd		(typing "dd" will delete the line that you are on)
309
310
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
311
312
syntax on		(typing "syntax on" immediately after SHIFT: will turn on syntax highlighting
313
314
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
315
316
set tabstop=5	(typing "set tabstop=5" immediately after SHIFT: will set your tabs to 5 spaces
317
318
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
319
320
wq			(typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
321
322
323
324
325
vi .vimrc
326
i			(press "i" to get into INSERT mode and then paste in the lines below)
327
328
329
set number
330
syntax on
331
set tabstop=5
332
333
ESC			(press the ESC key to get you out of INSERT mode)
334
335
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
336
337
wq			(typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
338
339
340
341
342
343
344
vi lesson1.sh
345
346
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
347
348
echo $MYVIMRC	(typing "echo $MYVIMRC" immediately after SHIFT: will display the path to your new .vimrc file
349
350
[SHIFT+:]	(press SHIFT and the : keys at the same time and you should see a : in the bottom left corner of the screen.
351
352
wq			(typing "wq" immediately after SHIFT: will save (w for write, and q for quit meaning exit vim).
353
-----------------------------------------------------------------------
354
355
356
357
358
359
360
361
362
###############
363
# Permissions #
364
###############
365
---------------------------Type This-----------------------------------
366
cd ~/students/yourname/LinuxBasics
367
368
ls -l one
369
-----------------------------------------------------------------------
370
We can determine a lot from examining the results of this command. The file "one" is owned by user "me". 
371
Now "me" has the right to read and write this file. 
372
The file is owned by the group "me". Members of the group "me" can also read and write this file. 
373
Everybody else can read this file
374
375
376
---------------------------Type This-----------------------------------
377
ls -l /bin/bash
378
-----------------------------------------------------------------------
379
380
Here we can see:
381
382
The file "/bin/bash" is owned by user "root". The superuser has the right to read, write, and execute this file. 
383
The file is owned by the group "root". Members of the group "root" can also read and execute this file.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              Everybody else can read and execute this file
384
385
386
The next command you need to know is "chmod"
387
rwx rwx rwx = 111 111 111
388
rw- rw- rw- = 110 110 110
389
rwx --- --- = 111 000 000
390
391
and so on...
392
393
rwx = 111 in binary = 7
394
rw- = 110 in binary = 6
395
r-x = 101 in binary = 5
396
r-- = 100 in binary = 4
397
398
399
---------------------------Type This-----------------------------------
400
ls -l one
401
402
chmod 600 one
403
404
ls -l one
405
406
sudo useradd yourname
407
	aegisweaponssystem
408
409
410
sudo passwd yourname
411
412
     P@$$w0rd321
413
     P@$$w0rd321
414
415
sudo chown testuser one
416
	aegisweaponssystem
417
418
ls -l one
419
420
sudo chgrp testuser one
421
	aegisweaponssystem
422
423
ls -l one
424
425
id
426
427
su testuser
428
     P@$$w0rd321
429
-----------------------------------------------------------------------
430
431
Here is a table of numbers that covers all the common settings. The ones beginning with "7" are used with programs (since they enable execution) and the rest are for other kinds of files.
432
433
Value	Meaning
434
777 (rwxrwxrwx) 	No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
435
436
755 (rwxr-xr-x) 	The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
437
438
700 (rwx------) 	The file's owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
439
440
666 (rw-rw-rw-) 	All users may read and write the file.
441
442
644 (rw-r--r--) 	The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
443
444
600 (rw-------) 	The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.
445
446
447
448
Directory permissions
449
---------------------
450
The chmod command can also be used to control the access permissions for directories. In most ways, the permissions scheme for directories works the same way as they do with files. However, the execution permission is used in a different way. It provides control for access to file listing and other things. Here are some useful settings for directories:
451
452
Value	Meaning
453
777 (rwxrwxrwx) 	No restrictions on permissions. 
454
Anybody may list files, create new files in the directory and delete files in the directory. 
455
Generally not a good setting.
456
457
458
459
755 (rwxr-xr-x) 	The directory owner has full access. 
460
All others may list the directory, but cannot create files nor delete them. 
461
This setting is common for directories that you wish to share with other users.
462
463
464
465
700 (rwx------) 	The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.
466
467
######################
468
# Process Management #
469
######################
470
---------------------------Type This-----------------------------------
471
top
472
	q
473
474
htop
475
	q
476
477
ps
478
479
ps aux
480
481
ps -A
482
483
ps -A | less
484
485
ps axjf
486
487
pstree
488
489
pstree -A
490
491
pgrep bash
492
493
pgrep init
494
495
ps aux | grep apache
496
-----------------------------------------------------------------------
497
498
499
500
You can list all of the signals that are possible to send with kill by typing:
501
---------------------------Type This-----------------------------------
502
kill -l
503
504
sudo kill -HUP pid_of_apache
505
506
The pkill command works in almost exactly the same way as kill, but it operates on a process name instead:
507
508
pkill -9 ping
509
The above command is the equivalent of:
510
511
kill -9 `pgrep ping`
512
-----------------------------------------------------------------------
513
514
515
516
517
################
518
# Hashing Demo #
519
################
520
---------------------------Type This-----------------------------------
521
cd ~/students/yourname/LinuxBasics
522
523
mkdir hashdemo
524
525
cd hashdemo
526
527
echo test > test.txt
528
529
cat test.txt
530
531
md5sum test.txt
532
533
echo hello >> test.txt
534
535
cat test.txt
536
537
md5sum test.txt
538
539
echo test2 > test2.txt
540
541
cat test2.txt
542
543
sha256sum test2.txt
544
545
echo hello >> test2.txt
546
547
cat test2.txt
548
549
sha256sum test2.txt
550
551
cd ..
552
-----------------------------------------------------------------------
553
554
555
556
#################################
557
# Symmetric Key Encryption Demo #
558
#################################
559
---------------------------Type This-----------------------------------
560
cd ~/students/yourname/LinuxBasics
561
562
mkdir gpgdemo
563
564
cd gpgdemo
565
566
echo test > test.txt
567
568
cat test.txt
569
570
gpg -c test.txt
571
	password
572
	password
573
574
ls | grep test
575
576
cat test.txt
577
578
cat test.txt.gpg
579
580
rm -rf test.txt
581
582
ls | grep test
583
584
gpg -o output.txt test.txt.gpg
585
	P@$$w0rD!@#$P@$$w0rD!@#$
586
587
cat output.txt
588
-----------------------------------------------------------------------
589
590
591
592
#########################################################################################################################
593
# Asymmetric Key Encryption Demo 											                                            #
594
#															                                                            #
595
# Configure random number generator 											                                        #
596
# https://www.howtoforge.com/helping-the-random-number-generator-to-gain-enough-entropy-with-rng-tools-debian-lenny	    #
597
#########################################################################################################################
598
---------------------------Type This-----------------------------------
599
cd ~/students/yourname/LinuxBasics/gpgdemo
600
601
echo hello > file1.txt
602
603
echo goodbye > file2.txt
604
605
echo green > file3.txt
606
607
echo blue > file4.txt
608
609
tar czf files.tar.gz *.txt
610
611
gpg --gen-key
612
	1
613
	1024
614
	0
615
	y
616
	John Doe
617
	john@doe.com
618
	--blank comment--
619
	O
620
		P@$$w0rD!@#$P@$$w0rD!@#$
621
		P@$$w0rD!@#$P@$$w0rD!@#$	
622
623
624
625
gpg --armor --output file-enc-pubkey.txt --export 'John Doe'
626
627
cat file-enc-pubkey.txt
628
629
gpg --armor --output file-enc-privkey.asc --export-secret-keys 'John Doe'
630
631
cat file-enc-privkey.asc
632
633
gpg --encrypt --recipient 'John Doe' files.tar.gz
634
635
rm -rf files.tar.gz *.txt
636
637
ls
638
639
tar -zxvf files.tar.gz.gpg
640
641
gpg --output output.tar.gz --decrypt files.tar.gz.gpg
642
	P@$$w0rD!@#$P@$$w0rD!@#$
643
644
tar -zxvf output.tar.gz
645
646
ls
647
-----------------------------------------------------------------------
648
649
650
651
##############################################
652
# Log Analysis with Linux command-line tools #
653
##############################################
654
- The following command line executables are found in the Mac as well as most Linux Distributions.
655
 
656
cat –  prints the content of a file in the terminal window
657
grep – searches and filters based on patterns
658
awk –  can sort each row into fields and display only what is needed
659
sed –  performs find and replace functions
660
sort – arranges output in an order
661
uniq – compares adjacent lines and can report, filter or provide a count of duplicates
662
 
663
 
664
 
665
 
666
 
667
##############
668
# Cisco Logs #
669
##############
670
---------------------------Type This-----------------------------------
671
cd ~/students/yourname/ 
672
mkdir security
673
cd security
674
mkdir log_analysis
675
cd log_analysis
676
wget http://45.63.104.73/cisco.log
677
-----------------------------------------------------------------------
678
679
680
AWK Basics
681
----------
682
- To quickly demonstrate the print feature in awk, we can instruct it to show only the 5th word of each line. Here we will print $5. Only the last 4 lines are being shown for brevity.
683
---------------------------Type This----------------------------------- 
684
cat cisco.log | awk '{print $5}' | tail -n 4
685
----------------------------------------------------------------------- 
686
 
687
 
688
 
689
- Looking at a large file would still produce a large amount of output. A more useful thing to do might be to output every entry found in “$5”, group them together, count them, then sort them from the greatest to least number of occurrences. This can be done by piping the output through “sort“, using “uniq -c” to count the like entries, then using “sort -rn” to sort it in reverse order.
690
---------------------------Type This----------------------------------- 
691
cat cisco.log | awk '{print $5}'| sort | uniq -c | sort -rn
692
----------------------------------------------------------------------- 
693
 
694
 
695
 
696
- While that’s sort of cool, it is obvious that we have some garbage in our output. Evidently we have a few lines that aren’t conforming to the output we expect to see in $5. We can insert grep to filter the file prior to feeding it to awk. This insures that we are at least looking at lines of text that contain “facility-level-mnemonic”.
697
---------------------------Type This----------------------------------- 
698
cat cisco.log | grep %[a-zA-Z]*-[0-9]-[a-zA-Z]* | awk '{print $5}' | sort | uniq -c | sort -rn
699
----------------------------------------------------------------------- 
700
 
701
 
702
 
703
 
704
- Now that the output is cleaned up a bit, it is a good time to investigate some of the entries that appear most often. One way to see all occurrences is to use grep.
705
---------------------------Type This----------------------------------- 
706
cat cisco.log | grep %LINEPROTO-5-UPDOWN:
707
 
708
cat cisco.log | grep %LINEPROTO-5-UPDOWN:| awk '{print $10}' | sort | uniq -c | sort -rn
709
 
710
cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10}' | sort | uniq -c | sort -rn
711
 
712
cat cisco.log | grep %LINEPROTO-5-UPDOWN:| sed 's/,//g' | awk '{print $10 " changed to " $14}' | sort | uniq -c | sort -rn
713
-----------------------------------------------------------------------
714
715
716
717
718
719
##################
720
# Day 1 Homework #
721
##################
722
Task Option 1: Linux Survival
723
-----------------------------
724
Do all of the exercises in Linux Survival (http://linuxsurvival.com/linux-tutorial-introduction/)
725
Create a word document that contains the screenshots of the quizzes NOTE: You must score a perfect 100 for all 4 quizzes
726
Name the word document 'YourFirstName-YourLastName-LinuxDay1-LinuxSurvival.docx' (ex: 'Joseph-McCray-LinuxDay1-LinuxSurvival.docx')
727
Email the document to me at joe-at-secureninja-dot-com before the start of class tomorrow
728
729
730
Task Option 2: Basic Shell Scripting
731
------------------------------------
732
Watch and do all of the exercises in the video https://www.youtube.com/watch?v=_n5ZegzieSQ
733
Create a word document that contains the screenshots of the tasks performed in this video
734
Name the word document 'YourFirstName-YourLastName-LinuxDay1-ShellScripting.docx' (ex: 'Joseph-McCray-LinuxDay1-ShellScripting.docx')
735
Email the document to me at joe-at-secureninja-dot-com before the start of class tomorrow