0% found this document useful (0 votes)
1 views61 pages

Video Notes

The document provides a comprehensive guide on Linux commands, covering topics such as getting help, keyboard shortcuts, managing bash history, root access, file and directory operations, and viewing files. It includes practical examples for commands like 'ls', 'cp', 'mv', and 'rm', as well as techniques for piping and redirection. Additionally, it explains how to find files using 'locate' and 'find' commands.

Uploaded by

Obaid Abdullah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views61 pages

Video Notes

The document provides a comprehensive guide on Linux commands, covering topics such as getting help, keyboard shortcuts, managing bash history, root access, file and directory operations, and viewing files. It includes practical examples for commands like 'ls', 'cp', 'mv', and 'rm', as well as techniques for piping and redirection. Additionally, it explains how to find files using 'locate' and 'find' commands.

Uploaded by

Obaid Abdullah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Commands - Getting Help


2. ##########################

3. ## Getting Help in Linux

4. ##########################

5.

6. # MAN Pages

7. man command # => Ex: man ls

8.

9. # The man page is displayed with the less command

10. # SHORTCUTS:

11. # h => getting help

12. # q => quit

13. # enter => show next line

14. # space => show next screen

15. # /string => search forward for a string

16. # ?string => search backwards for a string

17. # n / N => next/previous appearance

18.

19. # checking if a command is shell built-in or executable file

20. type rm # => rm is /usr/bin/rm

21. type cd # => cd is a shell builtin

22.

23. # getting help for shell built-in commands

24. help command # => Ex: help cd

25. command --help # => Ex: rm --help

26.

27. # searching for a command, feature or keyword in all man Pages

28. man -k uname

29. man -k "copy files"

30. apropos passwd


2. Commands - Keyboard Shortcuts
1. ##########################

2. ## Keyboard Shortcuts

3. ##########################

4. TAB # autocompletes the command or the filename if its unique

5. TAB TAB (press twice) # displays all commands or filenames that start with those letters

6.

7. # clearing the terminal

8. CTRL + L

9.

10. # closing the shell (exit)

11. CTRL + D

12.

13. # cutting (removing) the current line

14. CTRL + U

15.

16. # moving the cursor to the start of the line

17. CTRL + A

18.

19. # moving the cursor to the end of the line

20. Ctrl + E

21.

22. # stopping the current command

23. CTRL + C

24.

25. # sleeping a the running program

26. CTRL + Z

27.

28. # opening a terminal

29. CTRL + ALT + T


3. Commands - The Bash History
1. ##########################

2. ## Bash History

3. ##########################

4.

5. # showing the history

6. history

7.

8. # removing a line (ex: 100) from the history

9. history -d 100

10.

11. # removing the entire history

12. history -c

13.

14. # printing the no. of commands saved in the history file (~/.bash_history)

15. echo $HISTFILESIZE

16.

17. # printing the no. of history commands saved in the memory

18. echo $HISTSIZE

19.

20. # rerunning the last command from the history

21. !!

22.

23. # running a specific command from the history (ex: the 20th command)

24. !20

25.

26. # running the last nth (10th) command from the history

27. !-10

28.

29. # running the last command starting with abc


30. !abc

31.

32. # printing the last command starting with abc

33. !abc:p

34.

35. # reverse searching into the history

36. CTRL + R

37.

38. # recording the date and time of each command in the history

39. HISTTIMEFORMAT="%d/%m/%y %T"

40.

41. # making it persistent after reboot

42. echo "HISTTIMEFORMAT=\"%d/%m/%y %T\"" >> ~/.bashrc

43. # or

44. echo 'HISTTIMEFORMAT="%d/%m/%y %T"' >> ~/.bashrc


4. Commands - Getting root access
1. ##########################

2. ## Running commands as root (sudo, su)

3. ##########################

4.

5. # running a command as root (only users that belong to sudo group [Ubuntu] or wheel
[CentOS])

6. sudo command

7.

8. # becoming root temporarily in the terminal

9. sudo su # => enter the user's password

10.

11. # setting the root password

12. sudo passwd root

13.

14. # changing a user's password

15. passwd username

16.

17. # becoming root temporarily in the terminal

18. su # => enter the root password


5. Commands – Paths
1. ##########################

2. ## Linux Paths

3. ##########################

4.

5. . # => the current working directory

6. .. # => the parent directory

7. ~ # => the user's home directory

8.

9. cd # => changing the current directory to user's home directory

10. cd ~ # => changing the current directory to user's home directory

11. cd - # => changing the current directory to the last directory

12. cd /path_to_dir # => changing the current directory to path_to_dir

13. pwd # => printing the current working directory

14.

15. # installing tree

16. sudo apt install tree

17.

18. tree directory/ # => Ex: tree .

19. tree -d . # => prints only directories

20. tree -f . # => prints absolute paths


6. Commands - ls
##########################

## The ls Command

## ls [OPTIONS] [FILES]

##########################

# listing the current directory

# ~ => user's home directory

# . => current directory

# .. => parent directory

ls

ls .

# listing more directories

ls ~ /var /

# -l => long listing

ls -l ~

# -a => listing all files and directories including hidden ones

ls -la ~

# -1 => listing on a single column

ls -1 /etc

# -d => displaying information about the directory, not about its contents

ls -ld /etc

# -h => displaying the size in human readable format

ls -h /etc
# -S => displaying sorting by size

ls -Sh /var/log

# Note: ls does not display the size of a directory and all its contents. Use du instead

du -sh ~

# -X => displaying sorting by extension

ls -lX /etc

# --hide => hiding some files

ls --hide=*.log /var/log

# -R => displaying a directory recursively

ls -lR ~

# -i => displaying the inode number

ls -li /etc
7. Commands - File Types and
Timestamps
1. ##########################

2. ## File Timestamps and Date

3. ##########################

4.

5. # displaying atime

6. ls -lu

7.

8. # displaying mtime

9. ls -l

10. ls -lt

11.

12. # displaying ctime

13. ls -lc

14.

15. # displaying all timestamps

16. stat [Link]

17.

18. # displaying the full timestamp

19. ls -l --full-time /etc/

20.

21. # creating an empty file if it does not exist, update the timestamps if the file exists

22. touch [Link]

23.

24. # changing only the access time to current time

25. touch -a file

26.

27. # changing only the modification time to current time


28. touch -m file

29.

30. # changing the modification time to a specific date and time

31. touch -m -t 201812301530.45 [Link]

32.

33. # changing both atime and mtime to a specific date and time

34. touch -d "2010-10-31 15:45:30" [Link]

35.

36. # changing the timestamp of [Link] to those of [Link]

37. touch [Link] -r [Link]

38.

39. # displaying the date and time

40. date

41.

42. # showing this month's calendar

43. cal

44.

45. # showing the calendar of a specific year

46. cal 2021

47.

48. # showing the calendar of a specific month and year

49. cal 7 2021

50.

51. # showing the calendar of previous, current and next month

52. cal -3

53.

54. # setting the date and time

55. date --set="2 OCT 2020 18:00:00"

56.

57. # displaying the modification time and sorting the output by name.

58. ls -l
59.

60. # displaying the output sorted by modification time, newest files first

61. ls -lt

62.

63. # displaying and sorting by atime

64. ls -ltu

65.

66. # reversing the sorting order

67. ls -ltu --reverse


8. Commands - Viewing Files
1. ##########################

2. ## Viewing files (cat, less, more, head, tail, watch)

3. ##########################

4.

5. # displaying the contents of a file

6. cat filename

7.

8. # displaying more files

9. cat filename1 filename2

10.

11. # displaying the line numbers

12. can -n filename

13.

14. # concatenating 2 files

15. cat filename1 filename2 > filename3

16.

17. # viewing a file using less

18. less filename

19.

20. # less shortcuts:

21. # h => getting help

22. # q => quit

23. # enter => show next line

24. # space => show next screen

25. # /string => search forward for a string

26. # ?string => search backwards for a string


27. # n / N => next/previous appearance

28.

29.

30. # showing the last 10 lines of a file

31. tail filename

32.

33. # showing the last 15 lines of a file

34. tail -n 15 filename

35.

36. # showing the last lines of a file starting with line no. 5

37. tail -n +5 filename

38.

39. # showing the last 10 lines of the file in real-time

40. tail -f filename

41.

42.

43. # showing the first 10 lines of a file

44. head filename

45.

46. # showing the first 15 lines of a file

47. head -n 15 filename

48.

49. # running repeatedly a command with refresh of 3 seconds

50. watch -n 3 ls -l
9. Commands - touch, mkdir, cp, mv, rm,
shred
1. ##########################

2. ## Working with files and directory (touch, mkdir, cp, mv, rm, shred)

3. ##########################

4.

5. # creating a new file or updating the timestamps if the file already exists

6. touch filename

7.

8. # creating a new directory

9. mkdir dir1

10.

11. # creating a directory and its parents as well

12. mkdir -p mydir1/mydir2/mydir3

13.

14. ######################

15. ### The cp command ###

16. ######################

17. # copying file1 to file2 in the current directory

18. cp file1 file2

19.

20. # copying file1 to dir1 as another name (file2)

21. cp file1 dir1/file2

22.

23. # copying a file prompting the user if it overwrites the destination

24. cp -i file1 file2


25.

26. # preserving the file permissions, group and ownership when copying

27. cp -p file1 file2

28.

29. # being verbose

30. cp -v file1 file2

31.

32. # recursively copying dir1 to dir2 in the current directory

33. cp -r dir1 dir2/

34.

35. # copy more source files and directories to a destination directory

36. cp -r file1 file2 dir1 dir2 destination_directory/

37.

38.

39. ######################

40. ### The mv command ###

41. ######################

42. # renaming file1 to file2

43. mv file1 file2

44.

45. # moving file1 to dir1

46. mv file1 dir1/

47.

48. # moving a file prompting the user if it overwrites the destination file

49. mv -i file1 dir1/

50.

51. # preventing a existing file from being overwritten

52. mv -n file1 dir1/

53.

54. # moving only if the source file is newer than the destination file or when the destination file
is missing
55. mv -u file1 dir1/

56.

57. # moving file1 to dir1 as file2

58. mv file1 dir1/file2

59.

60. # moving more source files and directories to a destination directory

61. mv file1 file2 dir1/ dir2/ destination_directory/

62.

63. ######################

64. ### The rm command ###

65. ######################

66. # removing a file

67. rm file1

68.

69. # being verbose when removing a file

70. rm -v file1

71.

72. # removing a directory

73. rm -r dir1/

74.

75. # removing a directory without prompting

76. rm -rf dir1/

77.

78. # removing a file and a directory prompting the user for confirmation

79. rm -ri fil1 dir1/

80.

81. # secure removal of a file (verbose with 100 rounds of overwriting)

82. shred -vu -n 100 file1


10. Commands - Piping and Redirection
1. ##########################

2. ## Piping and Command Redirection

3. ##########################

4.

5. ## Piping Examples:

6.

7. ls -lSh /etc/ | head # see the first 10 files by size

8. ps -ef | grep sshd # checking if sshd is running

9. ps aux --sort=-%mem | head -n 3 # showing the first 3 process by memory consumption

10.

11. ## Command Redirection

12.

13. # output redirection

14. ps aux > running_processes.txt

15. who -H > loggedin_users.txt

16.

17. # appending to a file

18. id >> loggedin_users.txt

19.

20. # output and error redirection

21. tail -n 10 /var/log/*.log > [Link] 2> [Link]

22.

23. # redirecting both the output and errors to the same file

24. tail -n 2 /etc/passwd /etc/shadow > output_errors.txt 2>&1

25.

26. cat -n /var/log/[Link] | grep -ai "authentication failure" | wc -l

27. cat -n /var/log/[Link] | grep -ai "authentication failure" > [Link] # => piping and
redirection
11. Commands - plocate, find
1. ##########################

2. ## Finding Files (find, plocate)

3. ##########################

4.

5. ## LOCATE ##

6. # locate is a symlink (shortcut) to plocate

7.

8. # updating the plocate db

9. sudo updatedb

10.

11. # finding file by name

12. locate filename # => filename is expanded to *filename*

13. locate -i filename # => the filename is case insensitive

14. locate -r '/filename$' # => finding by exact name

15.

16. # finding using the basename

17. locate -b filename

18.

19. # finding using regular expressions

20. locate -r 'regex'

21.

22. # checking that the file exists

23. locate -e filename

24.

25. # showing command path

26. which command

27. which -a command

28.
29.

30. ## FIND ##

31. find PATH OPTIONS

32.

33. # Example: find ~ -type f -size +1M # => finding all files in ~ bigger than 1 MB

34.

35. ## Options:

36. # -type f, d, l, s, p

37. # -name filename

38. # -iname filename # => case-insensitive

39. # -size n, +n, -n

40. # -perm permissions

41. # -links n, +n, -n

42. # -atime n, -mtime n, ctime n

43. # -user owner

44. # -group group_owner


12. Commands - grep
1. ##########################

2. ## Searching for text patterns (grep)

3. ##########################

4.

5. grep [OPTIONS] pattern file

6.

7. Options:

8. -n # => print line number

9. -i # => case insensitive

10. -v # inverse the match

11. -w # search for whole words

12. -a # search in binary files

13. -R # search in directory recursively

14. -c # display only the no. of matches

15. -C n # display a context (n lines before and after the match)

16.

17.

18. # printing ASCII chars from a binary file

19. strings binary_file


13. Commands - VIM
1. ##########################

2. ## VIM

3. ##########################

4.

5. Modes of operation: Command, Insert, and Last Line Modes.

6. VIM Config File: ~/.vimrc

7.

8. # Entering the Insert Mode from the Command Mode

9. i => insert before the cursor

10. I => insert at the beginning of the line

11. a => insert after the cursor

12. A => insert at the end of the line

13. o => insert on the next line

14.

15. # Entering the Last Line Mode from the Command Mode

16. :

17.

18. # Returning to Command Mode from Insert or Last Line Mode

19. ESC

20.

21. # Shortcuts in Last Line Mode

22. w! => write/save the file

23. q! => quit the file without saving

24. wq! => save/write and quit

25. e! => undo to the last saved version of the file

26. set nu => set line numbers


27. set nonu => unset line numbers

28. syntax on|off

29. %s/search_string/replace_string/g

30.

31. # Shortcuts in Command Mode

32. x => remove char under the cursor

33. dd => cut the current line

34. 5dd => cut 5 lines

35. ZZ => save and quit

36. u => undo

37. G => move to the end of file

38. $ => move to the end of line

39. 0 or ^ => move to the beginning of file

40. :n (Ex :10) => move to line n

41. Shift+v => select the current line

42. y => yank/copy to clipboard

43. p => paste after the cursor

44. P => paste before the cursor

45. /string => search for string forward

46. ?string => search for string backward

47. n => next occurrence

48. N => previous occurrence

49.

50. # Opening more files in stacked windows

51. vim -o file1 file2

52.

53. # Opening more files and highlighting the differences

54. vim -d file1 file2

55. Ctrl+w => move between files


14. Commands - Account Management
1. ## Account Management

2. ##########################

3.

4. ## IMPORTANT FILES

5. # /etc/passwd # => users and info: username:x:uid:gid:comment:home_directory:login_shell

6. # /etc/shadow # => users' passwords

7. # /etc/group # => groups

8.

9. # creating a user account

10. useradd [OPTIONS] username

11. # OPTIONS:

12. # -m => create home directory

13. # -d directory => specify another home directory

14. # -c "comment"

15. # -s shell

16. # -G => specify the secondary groups (must exist)

17. # -g => specify the primary group (must exist)

18.

19. Exemple:

20. useradd -m -d /home/john -c "C++ Developer" -s /bin/bash -G sudo,adm,mail john

21.

22. # changing a user account

23. usermod [OPTIONS] username # => uses the same options as useradd

24. Example:

25. usermod -aG developers,managers john # => adding the user to two secondary groups

26.

27. # deleting a user account

28. userdel -r username # => -r removes user's home directory as well


29.

30. # creating a group

31. groupadd group_name

32.

33. # deleting a group

34. groupdel group_name

35.

36. # displaying all groups

37. cat /etc/groups

38.

39. # displaying the groups a user belongs to

40. groups

41.

42. # creating admin users

43. # add the user to sudo group in Ubuntu and wheel group in CentOS

44. usermod -aG sudo john

45.

46.

47. ## Monitoring Users ##

48. who -H # => displays logged in users

49. id # => displays the current user and its groups

50. whoami # => displays EUID

51.

52. # listing who’s logged in and what’s their current process.

53. w

54. uptime

55.

56. # printing information about the logins and logouts of the users

57. last

58. last -u username


15. Commands - File Permissions

1. ##########################

2. ## File Permissions

3. ##########################

4.

5. ## LEGEND

6. u = User

7. g = Group

8. o = Others/World

9. a = all

10.

11. r = Read

12. w = write

13. x = execute

14. - = no access

15.

16. # displaying the permissions (ls and stat)

17. ls -l /etc/passwd

18. -rw-r--r-- 1 root root 2871 aug 22 14:43 /etc/passwd

19.

20. stat /etc/shadow

21. File: /etc/shadow

22. Size: 1721 Blocks: 8 IO Block: 4096 regular file

23. Device: 805h/2053d Inode: 524451 Links: 1

24. Access: (0640/-rw-r-----) Uid: ( 0/ root) Gid: ( 42/ shadow)

25. Access: 2020-08-24 11:31:49.506277118 +0300

26. Modify: 2020-08-22 14:43:36.326651384 +0300

27. Change: 2020-08-22 14:43:36.342652202 +0300


28. Birth: -

29.

30. # changing the permissions using the relative (symbolic) mode

31. chmod u+r filename

32. chmod u+r,g-wx,o-rwx filename

33. chmod ug+rwx,o-wx filename

34. chmod ugo+x filename

35. chmod a+r,a-wx filename

36.

37. # changing the permissions using the absolute (octal) mode

38. PERMISSIONS EXAMPLE

39. u g o

40. rwx rwx rwx chmod 777 filename

41. rwx rwx r-x chmod 775 filename

42. rwx r-x r-x chmod 755 filename

43. rwx r-x --- chmod 750 filename

44. rw- rw- r-- chmod 664 filename

45. rw- r-- r-- chmod 644 filename

46. rw- r-- --- chmod 640 filename

47.

48. # setting the permissions as of a reference file

49. chmod --reference=file1 file2

50.

51. # changing permissions recursively

52. chmod -R u+rw,o-rwx filename

53.

54. ## SUID (Set User ID)

55.

56. # displaying the SUID permission

57. ls -l /usr/bin/umount

58. -rwsr-xr-x 1 root root 39144 apr 2 18:29 /usr/bin/umount


59.

60. stat /usr/bin/umount

61. File: /usr/bin/umount

62. Size: 39144 Blocks: 80 IO Block: 4096 regular file

63. Device: 805h/2053d Inode: 918756 Links: 1

64. Access: (4755/-rwsr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)

65. Access: 2020-08-22 14:35:46.763999798 +0300

66. Modify: 2020-04-02 18:29:40.000000000 +0300

67. Change: 2020-06-30 18:27:32.851134521 +0300

68. Birth: -

69.

70. # setting SUID

71. chmod u+s executable_file

72. chmod 4XXX executable_file # => Ex: chmod 4755 [Link]

73.

74.

75. ## SGID (Set Group ID)

76.

77. # displaying the SGID permission

78. ls -ld projects/

79. drwxr-s--- 2 student student 4096 aug 25 11:02 projects/

80.

81. stat projects/

82. File: projects/

83. Size: 4096 Blocks: 8 IO Block: 4096 directory

84. Device: 805h/2053d Inode: 266193 Links: 2

85. Access: (2750/drwxr-s---) Uid: ( 1001/ student) Gid: ( 1002/ student)

86. Access: 2020-08-25 11:02:15.013355559 +0300

87. Modify: 2020-08-25 11:02:15.013355559 +0300

88. Change: 2020-08-25 11:02:19.157290764 +0300

89. Birth: -
90.

91. # setting SGID

92. chmod 2750 projects/

93. chmod g+s projects/

94.

95.

96. ## The Sticky Bit

97.

98. # displaying the sticky bit permission

99. ls -ld /tmp/

100. drwxrwxrwt 20 root root 4096 aug 25 10:49 /tmp/

101.

102. stat /tmp/

103. File: /tmp/

104. Size: 4096 Blocks: 8 IO Block: 4096 directory

105. Device: 805h/2053d Inode: 786434 Links: 20

106. Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)

107. Access: 2020-08-22 14:46:03.259455125 +0300

108. Modify: 2020-08-25 10:49:53.756211470 +0300

109. Change: 2020-08-25 10:49:53.756211470 +0300

110. Birth: -

111.

112. # setting the sticky bit

113. mkdir temp

114. chmod 1777 temp/

115. chmod o+t temp/

116. ls -ld temp/

117. drwxrwxrwt 2 student student 4096 aug 25 11:04 temp/

118.

119.

120. ## UMASK
121. # displaying the UMASK

122. umask

123.

124. # setting a new umask value

125. umask new_value # => Ex: umask 0022

126.

127. ## Changing File Ownership (root only)

128.

129. # changing the owner

130. chown new_owner file/directory # => Ex: sudo chown john [Link]

131.

132. # changing the group owner

133. chgrp new_group file/directory

134.

135. # changing both the owner and the group owner

136. chown new_owner:new_group file/directory

137.

138. # changing recursively the owner or the group owner

139. chown -R new-owner file/directory

140.

141. # displaying the file attributes

142. lsattr filename

143.

144. #changing the file attributes

145. chattr +-attribute filename # => Ex: sudo chattr +i [Link]


16. Commands - ps, pstree, pgrep

1. ##########################

2. ## Process Viewing (ps, pstree, pgrep)

3. ##########################

4. # checking if a command is shell built-in or executable file

5. type rm # => rm is /usr/bin/rm

6. type cd # => cd is a shell built-in

7.

8. # displaying all processes started in the current terminal

9. ps

10.

11. # displaying all processes running in the system

12. ps -ef

13. ps aux

14. ps aux | less # => piping to less

15.

16. # sorting by memory and piping to less

17. ps aux --sort=%mem | less

18.

19. # ASCII art process tree

20. ps -ef --forest

21.

22. # displaying all processes of a specific user

23. ps -f -u username

24.

25. # checking if a process called sshd is running

26. pgrep -l sshd

27. ps -ef | grep sshd


28.

29. #displaying a hierarchical tree structure of all running processes

30. pstree

31.

32. # prevent merging identical branches

33. pstree -c
17. Commands – top
1. ##########################

2. ## Dynamic Real-Time View of Processes(top)

3. ##########################

4.

5. # starting top

6. top

7.

8. ## top shortcuts while it's running

9. h # => getting help

10. space # => manual refresh

11. d # => setting the refresh delay in seconds

12. q # => quitting top

13. u # => display processes of a user

14. m # => changing the display for the memory

15. 1 # => individual statistics for each CPU

16. x/y # => highlighting the running process and the sorting column

17. b # => toggle between bold and text highlighting

18. < # => move the sorting column to the left

19. > # => move the sorting column to the right

20. F # => entering the Field Management screen

21. W # => saving top settings

22.

23. # running top in batch mode (3 refreshes, 1 second delay)

24. top -d 1 -n 3 -b > top_processes.txt

25.

26. # Interactive process viewer (top alternative)

27. sudo apt update && sudo apt install htop # => Installing htop

28. htop
18. Commands - kill, pkill, killall, jobs, fg,
bg, nohup

1. ##########################

2. ## Killing processes (kill, pkill, killall)

3. ##########################

4.

5. # listing all signals

6. kill -l

7.

8. # sending a signal (default SIGTERM - 15) to a process by pid

9. kill pid # => Ex: kill 12547

10.

11. # sending a signal to more processes

12. kill -SIGNAL pid1 pid2 pid3 ...

13.

14. # sending a specific signal (SIGHUP - 1) to a process by pid

15. kill -1 pid

16. kill -HUP pid

17. kill -SIGHUP pid

18.

19. # sending a signal (default SIGTERM - 15) to process by process name

20. pkill process_name # => Ex: pkill sleep

21. killall process_name

22. kill $(pidof process_name) # => Ex: kill -HUP $(pidof sshd)

23.

24. # running a process in the background

25. command & # => Ex: sleep 100 &

26.
27. # Showing running jobs

28. jobs

29.

30. # Stopping (pausing) the running process

31. Ctrl + Z

32.

33. # resuming and bringing to the foreground a process by job_d

34. fg %job_id

35.

36. # resuming in the background a process by job_d

37. bg %job_id

38.

39. # starting a process immune to SIGHUP

40. nohup command & # => Ex: nohup wget [Link] &
19. Commands - ifconfig, ip, route

1. ##########################

2. ## Getting info about the network interfaces (ifconfig, ip, route)

3. ##########################

4.

5. # displaying information about enabled interfaces

6. ifconfig

7.

8. # displaying information about all interfaces (enabled and disabled)

9. ifconfig -a

10. ip address show

11.

12. # displaying info about a specific interface

13. ifconfig enp0s3

14. ip addr show dev enp0s3

15.

16. # showing only IPv4 info

17. ip -4 address

18.

19. # showing only IPv6 info

20. ip -6 address

21.

22. # displaying L2 info (including the MAC address)

23. ip link show

24. ip link show dev enp0s3

25.

26. # displaying the default gateway

27. route
28. route -n # numerical addresses

29. ip route show

30.

31. # displaying the DNS servers

32. resolvectl status

33.

34.

35. ##########################

36. ## Setting the network interfaces (ifconfig, ip, route)

37. ##########################

38. # disabling an interface

39. ifconfig enp0s3 down

40. ip link set enp0s3 down

41.

42. # activating an interface

43. ifconfig enp0s3 up

44. ip link set enp0s3 up

45.

46. # checking its status

47. ifconfig -a

48. ip link show dev enp0s3

49.

50. # setting an ip address on an interface

51. ifconfig enp0s3 [Link]/24 up

52. ip address del [Link]/24 dev enp0s3

53. ip address add [Link]/24 dev enp0s3

54.

55. # setting a secondary ip address on sub-interface

56. ifconfig enp0s3:1 [Link]/24

57.

58. # deleting and setting a new default gateway


59. route del default gw [Link]

60. route add default gw [Link]

61.

62. # deleting and setting a new default gateway

63. ip route del default

64. ip route add default via [Link]

65.

66. # changing the MAC address

67. ifconfig enp0s3 down

68. ifconfig enp0s3 hw ether 08:00:27:51:05:a1

69. ifconfig enp0s3 up

70.

71. # changing the MAC address

72. ip link set dev enp0s3 address 08:00:27:51:05:a3


20. Commands – netplan

21. ##########################
22. ## Network Static configuration using Netplan (Ubuntu)
23. ##########################
24.
25. # 1. Stop and disable the Network Manager
26.
27. sudo systemctl stop NetworkManager
28. sudo systemctl disable NetworkManager
29. sudo systemctl status NetworkManager
30. sudo systemctl is-enabled NetworkManager
31.
32. # 2. Create a YAML file in /etc/netplan
33.
34. network:
35. version: 2
36. renderer: networkd
37. ethernets:
38. enp0s3:
39. dhcp4: false
40. addresses:
41. - [Link]/24
42. gateway4: "[Link]"
43. nameservers:
44. addresses:
45. - "[Link]"
46. - "[Link]"
47.
48. # 3. Apply the new config
49. sudo netplan apply
50.
51. # 4. Check the configuration
52. ifconfig
53. route -a
21. Commands – SSH

1. ##########################

2. ## OpenSSH

3. ##########################

4.

5. # 1. Installing OpenSSH (client and server)

6. # Ubuntu

7. sudo apt update && sudo apt install openssh-server openssh-client

8.

9. # CentOS

10. sudo dnf install openssh-server openssh-clients

11.

12. # connecting to the server

13. ssh -p 22 username@server_ip # => Ex: ssh -p 2267 john@[Link]

14. ssh -p 22 -l username server_ip

15. ssh -v -p 22 username@server_ip # => verbose

16.

17. # 2. Controlling the SSHd daemon

18. # checking its status

19. sudo systemctl status ssh # => Ubuntu

20. sudo systemctl status sshd # => CentOS

21.

22. # stopping the daemon

23. sudo systemctl stop ssh # => Ubuntu

24. sudo systemctl stop sshd # => CentOS

25.

26. # restarting the daemon

27. sudo systemctl restart ssh # => Ubuntu


28. sudo systemctl restart sshd # => CentOS

29.

30. # enabling at boot time

31. sudo systemctl enable ssh # => Ubuntu

32. sudo systemctl enable sshd # => CentOS

33.

34. sudo systemctl is-enabled ssh # => Ubuntu

35. sudo systemctl is-enabled sshd # => CentOS

36.

37. # 3. Securing the SSHd daemon

38. # change the configuration file (/etc/ssh/sshd_config) and then restart the server

39. man sshd_config

40.

41. a) Change the port

42. Port 2278

43.

44. b) Disable direct root login

45. PermitRootLogin no

46.

47. c) Limit Users’ SSH access

48. AllowUsers stud u1 u2 john

49.

50. d) Filter SSH access at the firewall level (iptables)

51.

52. e) Activate Public Key Authentication and Disable Password Authentication

53.

54. f) Use only SSH Protocol version 2

55.

56. g) Other configurations:

57. ClientAliveInterval 300

58. ClientAliveCountMax 0
59. MaxAuthTries 2

60. MaxStartUps 3

61. LoginGraceTime 20

22. Commands - scp, rsync

1. ##########################

2. ## Copying files using SCP and RSYNC

3. ##########################

4.

5. ### SCP ###

6. # copying a local file to a remote destination

7. scp [Link] john@[Link]:~

8. scp -P 2288 [Link] john@[Link]:~ # using a custom port

9.

10. # copying a local file from a remote destination to the current directory

11. scp -P 2290 john@[Link]:~/[Link] .

12.

13. # copying a local directory to a remote destination (-r)

14. scp -P 2290 -r projects/ john@[Link]:~

15.

16.

17. ### RSYNC ###

18. # synchronizing a directory

19. sudo rsync -av /etc/ ~/etc-backup/


20.

21. # mirroring (deleting from destination the files that were deleting from source)

22. sudo rsync -av --delete /etc/ ~/etc-backup/

23.

24. # excluding files

25. rsync -av --exclude-from='~/[Link]' source_directory/ destination_directory/

26. # [Link]:

27. # *.avi

28. # music/

29. # [Link]

30.

31. rsync -av --exclude='*.mkv' --exclude='[Link]' source_directory/ destination_directory/

32.

33. # synchronizing a directory over the network using SSH

34. sudo rsync -av -e ssh /etc/ student@[Link]:~/etc-backup/

35.

36. # using a custom port

37. sudo rsync -av -e 'ssh -p 2267' /etc/ student@[Link]:~/etc-backup/


23. Commands - wget, netstat, ss, nmap

1. ##########################

2. ## WGET

3. ##########################

4. # installing wget

5. apt install wget # => Ubuntu

6. dnf install wget # => CentOS

7.

8. # download a file in the current directory

9. wget [Link]

10.

11. # resuming the download

12. wget -c [Link]

13.

14. # saving the file into a specific directory

15. mkdir kali

16. wget -P kali/ [Link]

17.

18. # limiting the rate (bandwidth)

19. wget --limit-rate=100k -P kali/ [Link]


[Link]

20.

21. # downloading more files

22. wget -i [Link] # [Link] contains urls

23.

24. # starting the download in the background

25. wget -b -P kali/ [Link]

26. tail -f wget-log # => checking its status

27.
28. # getting an offline copy of a website

29. wget --mirror --convert-links --adjust-extension --page-requisites --no-parent


[Link]

30. wget -mkEpnp [Link]

31.

32.

33. ##########################

34. ## NETSTAT and SS

35. ##########################

36. # displaying all open ports and connections

37. sudo netstat -tupan

38. sudo ss -tupan

39. netstat -tupan | grep :80 # => checking if port 80 is open

40.

41. ##########################

42. ## LSOF

43. ##########################

44. # listing all files that are open

45. lsof

46.

47. # listing all files opened by the processes of a specific user

48. lsof -u username

49.

50. # listing all files opened by a specific process

51. lsof -c sshd

52.

53. # listing all files that have opened TCP ports

54. lsof -iTCP -sTCP:LISTEN

55. lsof -iTCP -sTCP:LISTEN -nP

56.

57.
58. ##########################

59. ## Scanning hosts and networks using nmap

60. ##########################

61. ##** SCAN ONLY YOUR OWN HOSTS AND SERVERS !!! **##

62. ## Scanning Networks is your own responsibility ##

63.

64. # Syn Scan - Half Open Scanning (root only)

65. nmap -sS [Link]

66.

67. # Connect Scan

68. nmap -sT [Link]

69.

70. # Scanning all ports (0-65535)

71. nmap -p- [Link]

72.

73. # Specifying the ports to scan

74. nmap -p 20,22-100,443,1000-2000 [Link]

75.

76. # Scan Version

77. nmap -p 22,80 -sV [Link]

78.

79. # Ping scanning (entire Network)

80. nmap -sP [Link]/24

81.

82. # Treat all hosts as online -- skip host discovery

83. nmap -Pn [Link]/24

84.

85. # Excluding an IP

86. nmap -sS [Link]/24 --exclude [Link]

87.

88. # Saving the scanning report to a file


89. nmap -oN [Link] [Link]

90.

91. # OS Detection

92. nmap -O [Link]

93.

94. # Enable OS detection, version detection, script scanning, and traceroute

95. nmap -A [Link]

96.

97. # reading the targets from a file (ip/name/network separated by a new line or a whitespace)

98. nmap -p 80 -iL [Link]

99.

100. # exporting to out output file and disabling reverse DNS

101. nmap -n -iL [Link] -p 80 -oN [Link]

24. Commands - dpkg, apt

1. ##########################

2. ## Software Management (dpkg and apt)

3. ##########################

4.

5. ### DPKG ###

6. # getting info about a deb file

7. dpkg --info google-chrome-stable_current_amd64.deb

8.

9. # installing an application from a deb file


10. sudo dpkg -i google-chrome-stable_current_amd64.deb

11.

12. # list all installed programs

13. dpkg --get-selections

14. dpkg-query -l

15.

16. # filtering the output

17. dpkg-query -l | grep ssh

18.

19. # listing all files of an installed package

20. dpkg-query -l | grep ssh

21. dpkg -L openssh-server

22.

23. # finding to which package a file belongs

24. which ls

25. dpkg -S /bin/ls

26. coreutils: /bin/cp

27.

28. # removing a package

29. sudo dpkg -r google-chrome-stable

30.

31. # purging a package

32. sudo dpkg -P google-chrome-stable

33.

34. ### APT ###

35. # updating the package index (doesn't install/uninstall/update any package)

36. sudo apt update

37. # installing or updating a package named apache2

38. sudo apt install apache2

39.

40. # listing all upgradable packages


41. sudo apt list --upgradable

42.

43. # upgrading all applications

44. sudo apt full-upgrade

45. sudo apt full-upgrade -y # => assume yes to any prompt (useful in scripts)

46.

47. # removing a package

48. sudo apt remove apache2

49.

50. # removing a package and its configurations

51. sudo apt purge apache2

52.

53. # removing dependencies that are not needed anymore

54. sudo apt autoremove

55.

56. # removing the saved deb files from the cache directory (var/cache/apt/archives)

57. sudo apt clean

58.

59. # listing all available packages

60. sudo apt list

61. sudo apt list | wc -l

62.

63. # searching for a package

64. sudo apt list | grep nginx

65.

66. # showing information about a package

67. sudo apt show nginx

68.

69. # listing all installed packages

70. sudo apt list --installed


25. Commands – Cron

1. ##########################

2. ## Task Scheduling using Cron

3. ##########################

4.

5. # editing the current user’s crontab file

6. crontab -e

7.

8. # listing the current user’s crontab file

9. crontab -l

10.

11. # removing the current user’s crontab file

12. crontab -r

13.

14. ## COMMON EXAMPLES ##

15. # run every minute

16. * * * * * /path_to_task_to_run.sh

17.

18. # run every hour at minute 15

19. 15 * * * * /path_to_task_to_run.sh

20.

21. # run every day at 6:30 PM

22. 30 18 * * * /path_to_task_to_run.sh

23.

24. # run every Monday at 10:03 PM

25. 3 22 * * 1 /path_to_task_to_run.sh

26.

27. # run on the 1st of every Month at 6:10 AM


28. 10 6 1 * * /path_to_task_to_run.sh

29.

30. # run every hour at minute 1, 20 and 35

31. 1,20,35 * * * * /path_to_task_to_run.sh

32.

33. # run every two hour at minute 10

34. 10 */2 * * * /path_to_task_to_run.sh

35.

36. # run once a year on the 1st of January at midnight

37. @yearly /path_to_task_to_run.sh

38.

39. # run once a month at midnight on the first day of the month

40. @monthly /path_to_task_to_run.sh

41.

42. # run once a week at midnight on Sunday

43. @weekly /path_to_task_to_run.sh

44.

45. # once an hour at the beginning of the hour

46. @hourly /path_to_task_to_run.sh

47.

48. # run at boot time

49. @reboot /path_to_task_to_run.sh

50.

51. All scripts in following directories will run as root at that interval:

52. /etc/[Link]

53. /etc/[Link]

54. /etc/[Link]

55. /etc/[Link]

56. /etc/[Link]
26. Commands - Getting Hardware
Information

1. ##########################

2. ## Getting System Hardware Information

3. ##########################

4.

5. # displaying full hardware information

6. lshw

7. lshw -short # => short format

8. lshw -json # => json format

9. lshw -html # => html format

10.

11. inxi -Fx

12. # displaying info about the CPU

13. lscpu

14. lshw -C cpu

15. lscpu -J => json format

16.

17. # displaying info about the installed RAM memory

18. dmidecode -t memory

19. dmidecode -t memory | grep -i size

20. dmidecode -t memory | grep -i max

21.

22. # displaying info about free/used memory

23. free -m

24.

25. # getting info about pci buses and about the devices connected to them

26. lspci
27. lspci | grep -i wireless

28. lspci | grep -i vga

29.

30. # getting info about USB controllers and about devices connected

31. lsusb

32. lsusb -v

33.

34. # getting info about hard disks

35. lshw -short -C disk

36. fdisk -l

37. fdisk -l /dev/sda

38. lsblk

39. hdparm -i /dev/sda

40. hdparm -I /dev/sda

41.

42. # benchmarking disk read performance

43. hdparm -tT --direct /dev/sda

44.

45. # getting info about WiFi cards and networks

46. lshw -C network

47. iw list

48. iwconfig

49. iwlist wlo1 scan

50.

51. # Getting hardware information from the /proc virtual fs

52. cat /proc/cpuinfo

53. /proc/partitions

54. cat /proc/meminfo

55. cat /proc/version

56. uname -r # => kernel version

57. uname -a
58.

59. acpi -bi # battery information

60. acpi -V

61.

62. ## Working directly with device files (dd)

63.

64. # backing up the MBR (the first sector of /dev/sda)

65. dd if=/dev/sda of=~/[Link] bs=512 count=1

66.

67. # restoring the MBR

68. dd if=~/[Link] of=/dev/sda bs=512 count=1

69.

70. # cloning a partition (sda1 to sdb2)

71. dd if=/dev/sda1 of=/dev/sdb2 bs=4M status=progress

27. Commands - systemd, systemctl

1. ##########################

2. ## Service Management using systemd and systemctl

3. ##########################

4. # showing info about the boot process

5. systemd-analyze

6. systemd-analyze blame

7.
8. # listing all active units systemd knows about

9. systemctl list-units

10. systemctl list-units | grep ssh

11.

12. # checking the status of a service

13. sudo systemctl status [Link]

14.

15. # stopping a service

16. sudo systemctl stop nginx

17.

18. # starting a service

19. sudo systemctl start nginx

20.

21. # restarting a service

22. sudo systemctl restart nginx

23.

24. # reloading the configuration of a service

25. sudo systemctl reload nginx

26. sudo systemctl reload-or-restart nginx

27.

28. # enabling to start at boot time

29. sudo systemctl enable nginx

30.

31. # disabling at boot time

32. sudo systemctl disable nginx

33.

34. # checking if it starts automatically at boot time

35. sudo systemctl is-enabled nginx

36.

37. # masking a service (stopping and disabling it)

38. sudo systemctl mask nginx


39.

40. # unmasking a service

41. sudo systemctl unmask nginx

Bash Scripting :-
28. Commands – Aliases

1. ##########################

2. ## Bash Aliases

3. ##########################

4.

5. # listing all Aliases

6. alias

7.

8. # creating an alias: alias_name="command"

9. alias copy="cp -i"

10.

11. # to make the aliases you define persistent, add them to ~/.bashrc

12.

13. # removing an alias: unalias alias_name

14. unalias copy


15.

16. ## Useful Aliases ##

17. alias c="clear"

18. alias cl="clear;ls;pwd"

19. alias root="sudo su"

20. alias ports="netstat -tupan"

21. alias sshconfig="sudo vim /etc/ssh/sshd_config"

22. alias my_server="ssh -p 3245-l user100 [Link]"

23. alias update=”sudo apt update && sudo apt dist-upgrade -y && sudo apt clean”

24. alias lt="ls -hSF --size -1"

25. alias ping='ping -c 5'

26.

27. # Interactive File Manipulation

28. alias cp="cp -i"

29. alias mv="mv -i"

30. alias rm="rm -i"

31.

32. ## Important alias ##

33. # This may look a bit confusing, but essentially,

34. # it makes all of the other aliases you define function correctly when used with sudo

35. alias sudo='sudo ' # use single quotes, not double quotes.

29. Coding - Variables in Bash

1. ##########################

2. ## Bash Variables
3. ##########################

4.

5. # defining a variable: variable_name=value

6. # variable names should start with a letter or underscore and can contain letters, digits and
underscore

7. os="Kali Linux"

8. version=10

9.

10. # referencing the value of a variable (getting the variable value): $variable_name

11. echo $os

12. echo $version

13.

14. # defining a read-only variable (constant)

15. declare -r temperature=100

16.

17. # removing (unsetting) a variable

18. unset version

19.

20. # listing all environment variables

21. env

22. printenv

23.

24. # searching for an environment variable

25. printenv PATH

26. env | grep -i path

27.

28. # creating new environment variables for the user: in ~/.bashrc add export MYVAR=”value”

29. export IP="[Link]"

30.

31. # changing the PATH

32. export PATH=$PATH:~/scripts # in ~/.bashrc


33.

34. # getting user input

35. read MY_VAR

36. echo $MY_VAR

37.

38. # displaying a message

39. read -p "Enter the IP address: " ip

40. ping -c 1 $ip

41.

42. read -s -p "Enter password:" pswd

43. echo $pswd

44.

45.

46. ### SPECIAL VARIABLES AND POSITIONAL ARGUMENTS ###

47. ./[Link] filename1 dir1

48.

49. $0 => the name of the script itself ([Link])

50. $1 => the first positional argument (filename1)

51. $2 => the second positional argument (dir1)

52. ...

53. ${10} => the tenth argument of the script

54. ${11} => the eleventh argument of the script

55.

56. $# => the number of the positional arguments

57. "$*" => string representation of all positional argument

58. $? => the most recent foreground command exit status


30. Coding - If...Elif...Else Statements

1. # if [ some_condition_is_true ]

2. # then

3. # //execute this code

4. # elif [ some_other_condition_is_true ]

5. # then

6. # //execute_this_code

7. # else

8. # //execute_this_code

9. # fi

10. ## Examples:

11.

12. i=1

13. if [[ $i -lt 10 ]]

14. then

15. echo "i is less than 10."

16. fi

17. #################

18. i=100

19. if [[ $i -lt 10 ]]

20. then

21. echo "i is less than 10."

22. else

23. echo "i is greater than or equal to 10."

24. fi

25. ################

26. i=10

27. if [[ $i -lt 10 ]]
28. then

29. echo "i is less than 10."

30. elif [[ $i -eq 10 ]]

31. then

32. echo "i is 10"

33. else

34. echo "i is greater than or equal to 10."

35. fi

36.

37. ################

38. ### TESTING CONDITIONS => man test ###

39.

40. ### For numbers (integers) ###

41. # -eq equal to

42. # -ne not equal to

43. # -lt less than

44. # -le less than or equal to

45. # -gt greater than

46. # -ge greater than or equal to

47.

48. # For files:

49. # -s file exists and is not empty

50. # -f file exists and is not a directory

51. # -d directory exists

52. # -x file is executable by the user

53. # -w file is writable by the user

54. # -r file is readable by the user

55.

56. # For Strings

57. # = the equality operator for strings if using single square brackets [ ]

58. # == the equality operator for strings if using double square brackets [[ ]]
59. # != the inequality operator for strings

60. # -n $str str is nonzero length

61. # -z $str str is zero length

62.

63. # && => the logical and operator

64. # || => the logical or operator

You might also like