joemccray

2021 Linux+ and having fun with Linux

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