Changes between Initial Version and Version 1 of Csle2022/Agenda/linuxhandson


Ignore:
Timestamp:
Nov 24, 2022, 6:50:21 AM (2 years ago)
Author:
dushmantha
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Csle2022/Agenda/linuxhandson

    v1 v1  
     1
     2== '''Linux''' ==
     3
     4== '''Working with directories''' ==
     5
     6
     7A brief overview of the most common commands to work with directories: pwd, cd, ls, mkdir and rmdir. These commands are available on any Linux (or Unix) system.
     8
     9•       '''pwd''' : Print Working Directory (Will tell you the location you are currently working)
     10•       '''cd''' : You can change your current directory with the cd command
     11o       cd : shortcut to get back into your home directory. Just typing cd without a target directory, will put you in your home directory
     12o       cd .. : To go to the parent directory (the one just above your current directory in the directory tree)
     13o       cd - : To go to the previous directory
     14•       ls : You can list the contents of a directory with ls
     15o       ls -a : To show all files. Showing all files means including the hidden files. When a file name on a Linux file system starts with a dot, it is considered a hidden file and it doesn't show up in regular file listings.
     16o       ls -l : to display the contents of the directory in different formats or to display different parts of the directory.
     17o       ls -lh : shows the numbers (file sizes) in a more human readable format.
     18•       mkdir : Create new directories
     19•       mkdir -p: To create parent directories as needed
     20•       rmdir: To remove the directory. (Directory has to be empty)
     21
     22
     23== Exercise ==
     24
     251.      Login to your VM. and Display your current directory
     262.      Change to /etc directory and display current directory
     273.      Go to root directory and list the contents
     284.      List a long listing of the root directory
     295.      Go to your home directory
     306.      Make directory named 'test'
     317.      make a directory inside test directory named 'one' and make a hidden directory inside 'one' directory named '.hidden'. Make a directory inside test directory named 'one' and make a hidden directory inside 'one' directory named 'unhidden'.
     328.      Go to 'one' directory and list the content.
     339.      Then list all contents
     3410.     Remove directory 'unhidden'
     3511.     Go to your home and try to remove directory 'test'
     3612.     Type ‘pwd’ in different directories
     3713.     Go to ‘cd  /etc’  type ‘pdw’  type ‘ls’  type ‘ls -l’
     38
     39
     40== File Editing ==
     41
     42The vi editor is installed on almost every Unix. Linux will very often install vim (vi improved) which is similar. Every system administrator should know vi(m), because it is an easy tool to solve problems.
     43vi Commands and Tips
     44Command Result
     45Open:   
     46vi filename     (fn=filename)
     47vi + filename   Place the cursor on last line of file.
     48vi +n filename  Place the cursor on line "n" of file.
     49vi +/pat filename       Place cursor on line with first occurrence of "pat"tern
     50       
     51Close: 
     52:w      Write the file to disk. Don't exit.
     53:w!     Write the file to disk even if read/only.
     54:wq     Write the file to disk and exit.
     55:wq!    Write the file to disk even if read/only and quit.
     56:q      Quit the file (only if no changes).
     57:q!     Quit the file even if changes.
     58       
     59Movement:       
     60       
     61A       Move to end of line, change to insert mode.
     62h       Move 1 space backwards (back/left arrow).
     63j       Move down 1 line (down arrow).
     64k       Move up 1 line (up arrow).
     65l       Move 1 space forwards (forward/right arrow)
     66w       Move cursor to start of next word.
     67W       Same as "w".
     68b       Move cursor to start of previous word.
     69B       Same as "b".
     70:n      Go to line number "n" in the file.
     71       
     72Editing:       
     73       
     74i       Enter in to input mode.
     75o       Add a line below cursor and enter in to input mode.
     76x       Delete character (del key in some cases).
     77D       Delete line from right of cursor to end of line.
     78dd      Delete entire line.
     79u       Undo last edit or restore current line.
     80yy      Yank current line.
     81p       Put yanked text before the cursor.
     82       
     83Searching:     
     84       
     85/pattern        Search for "pattern" in the file going forwards.
     86?pattern        Search for "pattern" in the file going backwards.
     87n       Find the next occurrence of pattern found forwards.
     88N       Find next occurrence of pattern found backwards.
     89       
     90Copy/ Cut and Paste     
     91       
     92<NUM>yyp        Copy n lines to buffer, paste below cursor
     93<NUM>yyP        Copy n lines to buffer, paste above cursor
     94<NUM>ddp        Cut n lines and copy to buffer, paste below cursor
     95<NUM>ddP        Cut n lines and copy to buffer, paste above cursor
     96       
     97Find and replace       
     98ESC: %s/Pattern/text/g  Find and replace words in vi editor (pattern – word to find & replace, text – word to replace with)
     99ESC: %s/\<pattern\>/text/g      Replace only an exact matching word in vi editor
     100       
     101Locate 
     102locate [name]   finds files in Linux using the file name
     103
     104
     105== Practicing with vi ==
     106
     107
     108The easiest thing to do if you get confused in vi is to press the ESCape key a couple of times and start over with what you were doing. Log in to your VM and...
     109
     1101.      Type ‘cd’
     1112.      Type ‘vi temp.txt’
     112
     113vi wil create the file “temp.txt” for you. Press the "i" key to switch to input mode.
     114
     115Type something like,
     116
     117"VI is great! I think I'll be using vi from now on instead of Word”
     118
     119Press <ENTER> to add lines.
     120Type some more text
     121
     122Save the file that you are in. To do this do:
     123
     124Press the ESCape key for command mode Type “:wq” then hit Enter to save and quit the file (notice the “:” before the “wq”).
     125
     126Copy a large file to your home directory so that you can play around with some more vi commands. We'll copy over your /etc/sysctl.conf file for this exercise. To do this do:
     127
     128$ cd
     129$ cp /etc/sysctl.conf sysctl.conf.bak
     130
     131Edit the file, but let's start at the bottom of the file:
     132
     133$ vi + sysctl.conf.bak
     134
     135Go to the first line of the file. Notice the colon (“:”) before the “1”.
     136
     137:1 <ENTER>
     138
     139Go to line 10, add a new line, and add in some text:
     140
     141:10 <ENTER>
     142Press the “i” key
     143Add the following text:
     144##
     145## A sample comment
     146##
     147
     148Delete the three lines you just created:
     149
     150Move to the first line of new text Press the ESCape key Press “dd” to delete a line, repeat until the text is gone
     151
     152Save the file, but don’t exit.
     153
     154:w
     155press <ENTER>
     156
     157Practice copying and pasting text.
     158
     159Go to line 12, copy 3 lines of text, go to the bottom of the file, place the text there:
     160
     161ESC                     (go to command mode)
     162:12 <ENTER>             (go to line 12 of the file)
     1633yy                     (“yank” 3 lines of text and place in copy buffer)
     164G                       (go to the end of the file)
     165P                       (place the contents of the copy buffer here)
     166
     167If want to undo this you would type (in command mode):
     168
     169u
     170
     171Now let’s exit from the file and not save the few changes we’ve made.
     172
     173:q! <Enter>
     174
     175
     176Nano Editor
     177
     178GNU nano is a text editor for Unix-like computing systems or operating environments using a command line interface. It emulates the Pico text editor, part of the Pine email client, and also provides additional functionality. Easy to use and all the commands are given in the editor.
     179
     180To create a new file type nano test.txt 
     181Find and Replace
     182
     183Create a file test1.txt
     184
     185Add the following text inside test.txt
     186Foo
     187Fool
     188Food
     189Foo
     190
     191Replace Foo with Bar
     192Command - ESC: %s/Foo/Bar/g
     193Enter, then save and quit
     194Find and replace exact matching word
     195
     196Create a file test2.txt
     197
     198Add the following text inside test.txt
     199Foo
     200Fool
     201Food
     202Foo
     203
     204Replace Foo with Bar
     205Command - ESC: %s/\<Foo\>/Bar/g
     206Enter then save and quit
     207
     208
     209
     210==  Working with Files ==
     211
     212
     213Files on Linux (or any Unix) are case sensitive. This means that FILE1 is different from file1, and /etc/hosts is different from /etc/Hosts (the latter one does not exist on a typical Linux computer). In Linux everything is considered as a file even a directory is a special kind of file. A small overview of some basic and important file handling commands
     214
     215•       file: The file utility determines the file type. Linux does not use extensions to determine the file type. The command line does not care whether a file ends in .txt or .pdf. As a system administrator, you should use the file command to determine the file type.
     216•       cp: To copy a file, use cp with a source and a target argument.
     217o       cp -r: To copy complete directories, use cp -r (the -r option forces recursive copying of all files in all subdirectories).
     218o       cp -i: To prevent cp from overwriting existing files, use the -i (for interactive) option.
     219•       mv: Use to rename a file or to move the file to another directory.
     220•       rm: Use to remove files *rm -i:To prevent yourself from accidentally removing a file. *rm -rf:rm -r will not remove non-empty directories. However, rm accepts several options that will allow you to remove any directory. The rm -rf will erase anything
     221
     222Dangerous Command
     223
     224When you are logged on as root, be very careful with rm -rf (the f means force and the r means recursive) since being root implies that permissions don't apply to you. You can literally erase your entire file system by accident.
     225
     226•       less:The less command is useful for displaying files that take up more than one screen
     227•       head/tail:You can use head to display the first ten lines of a file. and tail to display the last ten lines of a file. you can use both commands with -n and specify the number of lines
     228•       cat:The cat command is one of the most universal tools, yet all it does is copy standard input to standard output.
     229
     230
     231Exercise
     232
     233Create numbers.txt (Containing ten lines of numbers one to ten) file with cat command.
     234$ cat > numbers.txt
     235
     236Add the lines with enter key at the end. After inserting all the lines press Ctrl + D
     237
     238one
     239two
     240. .
     241Ten
     242
     243View the file you created using less and cat
     244
     245Cat
     246
     247$ cat numbers.txt
     248one
     249two
     250three
     251four
     252five
     253six
     254seven
     255eight
     256nine
     257ten
     258
     259less
     260
     261$ less numbers.txt
     262
     263Press q to exit from less
     264
     265View first four lines and last four lines using head and tail commands
     266
     267$ head -n 4 numbers.txt
     268one
     269two
     270three
     271four
     272$ tail -n 4 numbers.txt
     273seven
     274eight
     275nine
     276ten
     277Create another file numbers2.txt(with lines contain eleven to fifteen)
     278
     279$ cat > numbers2.txt
     280eleven
     281twelve
     282thirteen
     283fourteen
     284fifteen
     285
     286Combine numbers.txt and numbers2.txt and create numbers3.txt. and view the file.
     287
     288$ cat numbers.txt numbers2.txt > numbers3.txt
     289$ cat numbers3.txt
     290
     291one
     292two
     293three
     294four
     295five
     296six
     297seven
     298eight
     299nine
     300ten
     301eleven
     302twevelve
     303thirteen
     304fourteen
     305fifteen
     306
     307Check the file format of the newly created file
     308
     309$ file numbers3.txt
     310numbers3.txt: ASCII text
     311
     312Copy all the created files to the test directory. and verify
     313
     314$ cp numbers.txt numbers2.txt numbers3.txt test/
     315$ cd test
     316$ ls
     317numbers2.txt  numbers3.txt  numbers.txt  one
     318
     319Make a copy of test directory as newtest and rename it to numbers
     320$ cd
     321$ cp -r test/ newtest
     322$ mv newtest/ numbers
     323
     324Delete the test directory
     325
     326$ rm -rf test
     327
     328
     329
     330== File Permission ==
     331
     332
     333•       Commands preceded with "$" imply that you should execute the command as a general user - not as root.
     334•       Commands preceded with "#" imply that you should be working as root with "sudo"
     335•       Commands with more specific command lines (e.g. "RTR-GW>" or "mysql>") imply that you are executing commands on remote equipment, or within another program.
     336
     337chmod: Change file read write permission chown: Change the owner of the file chgrp: Change the group of the file
     338
     339Reference
     340If you look at files in a directory using "ls -al" you will see the permissions for each file and directories. Here is an example:
     341
     342drwxr-xr-x  6 test   radius 4096 Oct 16 18:18 .
     343drwxr-xr-x  3 root   root   4096 Sep 21 12:29 ..
     344
     345So, the directory has r (read), w (write), x (execute) access for the User and Group. For Other it has r (read) and x (execute) access. The file has read/write/execute access for User and read only access for everyone else (Group and Other).
     346
     347To change permissions you use the chmod command. chmod uses a base eight (octal) system to configure permissions. Or, you can use an alternate form to specify permissions by column (User/Group/Other) at a time.
     348
     349Permissions have values like this:
     350
     351 
     352
     353
     354Thus you can give permissions to a file using the sum of the values for each permission you wish to give for each column. Here is an example:
     355
     356
     357 
     358
     359
     360This is just one column. Since we have three areas of permissions (User, Group, Other), it looks like this will all 3 sets:
     361
     362
     363 
     364
     365
     366
     367== Exercise ==
     368
     369
     370Go to the numbers directory and get a detailed list
     371
     372cd numbers
     373ls -al
     374
     375drwxr-xr-x      3       test test 4096 Oct 17 13:53 .
     376drwxrwxrwx  50  test test 4096 Oct 17 13:10 ..
     377-rw-r--r--      1       test test      42 Oct 17 13:08 numbers2.txt
     378-rw-r--r--      1       test test      91 Oct 17 13:08 numbers3.txt
     379-rw-r--r--      1       test test      49 Oct 17 13:08 numbers.txt
     380drwxr-xr-x      3       test test 4096 Oct 17 13:08 one
     381
     382
     383Change file permission as follows
     384
     385$ chmod 044 numbers.txt
     386
     387Now you have remove read privilege try view the file using cat
     388
     389$ chmod 444 numbers.txt
     390
     391Now you have set privilege as read only. Open the file via vi editor and try to edit the file
     392Switch to root user
     393
     394$ sudo su
     395
     396•       Change the ownership and group of the numbers2.txt to root and make it read only for all the other users.
     397•       Change the ownership and group of the numbers3.txt to root and remove all the privileges from all the other users.
     398•       Switch back to your user and try to view numbers3.txt and try to edit numbers2.txt
     399
     400 
     401
     402More Linux Commands
     403
     404who: who command will give you information about who is logged on the system.
     405
     406$ who
     407
     408whoami command tells you your username
     409
     410$ whoami
     411
     412df -dh: report file system disk space usage
     413
     414 
     415
     416man: Will give you man pages (also called manual pages) on your Unix or Linux computer
     417
     418 
     419
     420date: Will give you the date and time of the system
     421
     422$ date
     423
     424To set time zone – type $ timedatectl set-timezone Asia/Colombo
     425
     426ln: Use to create links between files. Frequently use with -s to create symbolic links
     427
     428 
     429
     430ps: report a snapshot of the current processes. frequently use with -aux.
     431
     432 
     433
     434
     435
     436== Networking ==
     437
     438
     439
     440Get network related information
     441
     442netstat -a | more : To show both listening and non-listening sockets.
     443netstat -at : To list all tcp ports.
     444netstat -au : To list all udp ports.
     445netstat -l : To list only the listening ports.
     446netstat -lt : To list only the listening tcp ports.
     447netstat -lu : To list only the listening udp ports.
     448
     449Checking the network connectivity
     450
     451Ping 8.8.8.8
     452Ping google.com
     453
     454The tracepath command in Linux allows to trace the path to the destination path determining MTU along this path using UDP port or any other ports that will not require any superuser permissions.
     455
     456tracepath [destination]
     457
     458nslookup is the name of a program that lets an Internet server administrator or any computer user enter a host name (for example, "whatis.com") and find out the corresponding IP address or domain name system (DNS) record.
     459
     460        nslookup ac.lk
     461
     462 
     463
     464
     465The dig (domain information groper) command is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the queried name server(s).
     466
     467dig ac.lk
     468
     469 
     470
     471traceroute command in Linux prints the route that a packet takes to reach the host. This command is useful when you want to know about the route and about all the hops that a packet takes.
     472
     473        traceroute ac.lk
     474
     475On a Windows machine, this command is called tracert; on Linux and Mac, it's called traceroute.
     476
     477 
     478
     479'''ipconfig''' stands for Internet Protocol Configuration, while ifconfig stands for Interface Configuration.
     480
     481'''ifconfig'''
     482
     483'''ipconfig'''
     484