hjaltiatlason

Linux Commands - Useful

Jan 16th, 2021 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 29.90 KB | None | 0 0
  1. #http://cb.vu/unixtoolbox.xhtml Unix toolbox - collection of Unix/Linux/BSD commands
  2.  
  3. Linux Administration: The Complete Linux Bootcamp 2021
  4. =========================================================
  5. ##########################
  6. ## Getting Help in Linux
  7. ##########################
  8.  
  9. # MAN Pages
  10. man command     # => Ex: man ls
  11.  
  12. # The man page is displayed with the less command
  13. # SHORTCUTS:
  14. # h         => getting help
  15. # q         => quit
  16. # enter     => show next line
  17. # space     => show next screen
  18. # /string   => search forward for a string
  19. # ?string   => search backwards for a string
  20. # n / N     => next/previous appearance
  21.  
  22. # checking if a command is shell built-in or executable file
  23. type rm        # => rm is /usr/bin/rm
  24. type cd        # => cd is a shell builtin
  25.  
  26. # getting help for shell built-in commands
  27. help command    # => Ex: help cd
  28. command --help  # => Ex: rm --help
  29.  
  30. # searching for a command, feature or keyword in all man Pages
  31. man -k uname
  32. man -k "copy files"
  33. apropos passwd
  34.  
  35.  
  36. ##########################
  37. ## Bash History
  38. ##########################
  39.  
  40. # removing a line (ex: 100) from the history
  41. history -d 100
  42.  
  43. # removing the entire history
  44. history -c
  45.  
  46. # printing the no. of commands saved in the history file (~/.bash_history)
  47. echo $HISTFILESIZE
  48.  
  49. # printing the no. of history commands saved in the memory
  50. echo $HISTSIZE
  51.  
  52.  
  53. # printing the last command starting with abc
  54. !abc:p
  55.  
  56. # reverse searching into the history
  57. CTRL + R
  58.  
  59. # recording the date and time of each command in the history
  60. HISTTIMEFORMAT="%d/%m/%y %T "
  61.  
  62. ##########################
  63. ## Running commands as root (sudo, su)
  64. ##########################
  65.  
  66. # running a command as root (only users that belong to sudo group [Ubuntu] or wheel [CentOS])
  67. sudo command
  68.  
  69. # becoming root temporarily in the terminal
  70. sudo su      # => enter the user's password
  71.  
  72. # setting the root password
  73. sudo passwd root
  74.  
  75. # changing a user's password
  76. passwd username
  77.  
  78. # becoming root temporarily in the terminal
  79. su     # => enter the root password
  80.  
  81.  
  82. ##########################
  83.  
  84. ## The ls Command
  85.  
  86. ## ls [OPTIONS] [FILES]
  87.  
  88. ##########################
  89.  
  90.  
  91. # -l => long listing
  92. ls -l ~
  93.  
  94. # -a => listing all files and directories including hidden ones
  95. ls -la ~
  96.  
  97. # -d => displaying information about the directory, not about its contents
  98. ls -ld /etc
  99.  
  100. # -h => displaying the size in human readable format
  101. ls -h /etc
  102.  
  103. # -S => displaying sorting by size
  104. ls -Sh /var/log
  105.  
  106. # Note: ls does not display the size of a directory and all its contents. Use du instead
  107. du -sh ~
  108.  
  109. # -X => displaying sorting by extension
  110. ls -lX /etc
  111.  
  112. # --hide => hiding some files
  113. ls --hide=*.log /var/log
  114.  
  115. # -R => displaying a directory recursively
  116. ls -lR ~
  117.  
  118. # -i => displaying the inode number
  119. ls -li /etc
  120.  
  121. ##########################
  122. ## File Timestamps and Date
  123. ##########################
  124.  
  125. # displaying atime
  126. ls -lu
  127.  
  128. # displaying mtime
  129. ls -l
  130. ls -lt
  131.  
  132. # displaying ctime
  133. ls -lc
  134.  
  135. # displaying all timestamps
  136. stat file.txt
  137.  
  138. # displaying the full timestamp
  139. ls -l --full-time /etc/
  140.  
  141. # creating an empty file if it does not exist, update the timestamps if the file exists
  142. touch file.txt
  143.  
  144. # changing only the access time to current time
  145. touch -a file
  146.  
  147. # changing only the modification time to current time
  148. touch -m file
  149.  
  150. # changing the modification time to a specific date and time
  151. touch -m -t 201812301530.45 a.txt
  152.  
  153. # changing both atime and mtime to a specific date and time
  154. touch -d "2010-10-31 15:45:30" a.txt
  155.  
  156. # changing the timestamp of a.txt to those of b.txt
  157. touch a.txt -r b.txt
  158.  
  159.  
  160. # setting the date and time
  161. date --set="2 OCT 2020 18:00:00"
  162.  
  163. # displaying the modification time and sorting the output by name.
  164. ls -l
  165.  
  166. # displaying the output sorted by modification time, newest files first
  167. ls -lt
  168.  
  169. # displaying and sorting by atime
  170. ls -ltu
  171.  
  172. # reversing the sorting order
  173. ls -ltu --reverse
  174.  
  175.  
  176. ##########################
  177. ## Viewing files (cat, less, more, head, tail, watch)
  178. ##########################
  179.  
  180.  
  181.  
  182. # displaying the line numbers
  183. can -n filename
  184.  
  185. # concatenating 2 files
  186. cat filename1 filename2 > filename3
  187.  
  188.  
  189. # showing the last 10 lines of a file
  190. tail filename
  191.  
  192. # showing the last 15 lines of a file
  193. tail -n 15 filename
  194.  
  195. # showing the last lines of a file starting with line no. 15
  196. tail -n +5 filename
  197.  
  198. # showing the last 10 lines of the file in real-time
  199. tail -f filename
  200.  
  201.  
  202. # showing the first 10 lines of a file
  203. head filename
  204.  
  205. # showing the first 15 lines of a file
  206. head -n 15 filename
  207.  
  208. # running repeatedly a command with refresh of 3 seconds
  209. watch -n 3 ls -l
  210.  
  211.  
  212. ##########################
  213. ## Piping and Command Redirection
  214. ##########################
  215.  
  216. ## Piping Examples:
  217.  
  218. ls -lSh /etc/ | head            # see the first 10 files by size
  219. ps -ef | grep sshd              # checking if sshd is running
  220. ps aux --sort=-%mem | head -n 3  # showing the first 3 process by memory consumption
  221.  
  222. ## Command Redirection
  223. # output redirection
  224. ps aux > running_processes.txt
  225. who -H > loggedin_users.txt
  226.  
  227. # appending to a file
  228. id >> loggedin_users.txt
  229.  
  230. # output and error redirection
  231. tail -n 10 /var/log/*.log > output.txt 2> errors.txt
  232.  
  233. # redirecting both the output and errors to the same file
  234. tail -n 2 /etc/passwd /etc/shadow > output_errors.txt 2>&1
  235.  
  236. cat -n /var/log/auth.log | grep -ai "authentication failure" | wc -l
  237. cat -n /var/log/auth.log | grep -ai "authentication failure" > auth.txt     # => piping and redirection
  238.  
  239.  
  240. ##########################
  241. ## Finding Files (find, locate)
  242. ##########################
  243.  
  244. ## LOCATE ##
  245. # updating the locate db
  246. sudo updatedb
  247.  
  248. # displaying statistics
  249. locate -S
  250.  
  251. # finding file by name
  252. locate filename # => filename is expanded to *filename*
  253. locate -i filename # => the filename is case insensitive
  254. locate -b '\filename' # => finding by exact name
  255.  
  256. # finding using the basename
  257. locate -b filename
  258.  
  259. # finding using regular expressions
  260. locate -r 'regex'
  261.  
  262. # checking that the file exists
  263. locate -e filename
  264.  
  265. # showing command path
  266. which command
  267. which -a command
  268.  
  269.  ##########################
  270. ## Account Management
  271. ##########################
  272.  
  273. ## IMPORTANT FILES
  274. # /etc/passwd # => users and info: username:x:uid:gid:comment:home_directory:login_shell
  275. # /etc/shadow # => users' passwords
  276. # /etc/group # => groups
  277.  
  278. # creating a user account
  279. useradd [OPTIONS] username
  280. # OPTIONS:
  281. # -m => create home directory
  282. # -d directory => specify another home directory
  283. # -c "comment"
  284. # -s shell
  285. # -G => specify the secondary groups (must exist)
  286. # -g => specify the primary group (must exist)
  287.  
  288. Exemple:
  289. useradd -m -d /home/john -c "C++ Developer" -s /bin/bash -G sudo,adm,mail john
  290.  
  291. # changing a user account
  292. usermod [OPTIONS] username # => uses the same options as useradd
  293. Example:
  294. usermod -aG developers,managers john # => adding the user to two secondary groups
  295.  
  296. # deleting a user account
  297. userdel -r username # => -r removes user's home directory as well
  298.  
  299. # creating a group
  300. groupadd group_name
  301.  
  302. # deleting a group
  303. groupdel group_name
  304.  
  305. # displaying all groups
  306. cat /etc/groups
  307.  
  308. # displaying the groups a user belongs to
  309. groups
  310.  
  311. # creating admin users
  312. # add the user to sudo group in Ubuntu and wheel group in CentOS
  313. usermod -aG sudo john
  314.  
  315.  
  316. ## Monitoring Users ##
  317. who -H # => displays logged in users
  318. id # => displays the current user and its groups
  319. whoami # => displays EUID
  320.  
  321. # listing who’s logged in and what’s their current process.
  322. w
  323. uptime
  324.  
  325. # printing information about the logins and logouts of the users
  326. last
  327. last -u username
  328.  
  329.  
  330.  
  331. ##########################
  332. ## File Permissions
  333. ##########################
  334.  
  335. ## LEGEND
  336. u = User
  337. g = Group
  338. o = Others/World
  339. a = all
  340.  
  341. r = Read
  342. w = write
  343. x = execute
  344. - = no access
  345.  
  346. # displaying the permissions (ls and stat)
  347. ls -l /etc/passwd
  348.     -rw-r--r-- 1 root root 2871 aug 22 14:43 /etc/passwd
  349.  
  350. stat /etc/shadow
  351.     File: /etc/shadow
  352.     Size: 1721          Blocks: 8          IO Block: 4096   regular file
  353.     Device: 805h/2053d  Inode: 524451      Links: 1
  354.     Access: (0640/-rw-r-----)  Uid: (    0/    root)   Gid: (   42/  shadow)
  355.     Access: 2020-08-24 11:31:49.506277118 +0300
  356.     Modify: 2020-08-22 14:43:36.326651384 +0300
  357.     Change: 2020-08-22 14:43:36.342652202 +0300
  358.     Birth: -
  359.  
  360. # changing the permissions using the relative (symbolic) mode
  361. chmod u+r filename
  362. chmod u+r,g-wx,o-rwx filename
  363. chmod ug+rwx,o-wx filename
  364. chmod ugo+x filename
  365. chmod a+r,a-wx filename
  366.  
  367. # changing the permissions using the absolute (octal) mode
  368. PERMISSIONS      EXAMPLE
  369. u   g   o
  370. rwx rwx rwx     chmod 777 filename
  371. rwx rwx r-x     chmod 775 filename
  372. rwx r-x r-x     chmod 755 filename
  373. rwx r-x ---     chmod 750 filename
  374. rw- rw- r--     chmod 664 filename
  375. rw- r-- r--     chmod 644 filename
  376. rw- r-- ---     chmod 640 filename
  377.  
  378. # setting the permissions as of a reference file
  379. chmod --reference=file1 file2
  380.  
  381. # changing permissions recursively
  382. chmod -R u+rw,o-rwx filename
  383.  
  384. ## SUID (Set User ID)
  385.  
  386. # displaying the SUID permission
  387. ls -l /usr/bin/umount
  388.     -rwsr-xr-x 1 root root 39144 apr  2 18:29 /usr/bin/umount
  389.  
  390. stat /usr/bin/umount
  391.     File: /usr/bin/umount
  392.     Size: 39144         Blocks: 80         IO Block: 4096   regular file
  393.     Device: 805h/2053d  Inode: 918756      Links: 1
  394.     Access: (4755/-rwsr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
  395.     Access: 2020-08-22 14:35:46.763999798 +0300
  396.     Modify: 2020-04-02 18:29:40.000000000 +0300
  397.     Change: 2020-06-30 18:27:32.851134521 +0300
  398.     Birth: -
  399.  
  400. # setting SUID
  401. chmod u+s executable_file
  402. chmod 4XXX executable_file      # => Ex: chmod 4755 script.sh
  403.  
  404.  
  405. ## SGID (Set Group ID)
  406.  
  407. # displaying the SGID permission
  408. ls -ld projects/
  409.     drwxr-s--- 2 student student 4096 aug 25 11:02 projects/
  410.  
  411. stat projects/
  412.     File: projects/
  413.     Size: 4096          Blocks: 8          IO Block: 4096   directory
  414.     Device: 805h/2053d  Inode: 266193      Links: 2
  415.     Access: (2750/drwxr-s---)  Uid: ( 1001/ student)   Gid: ( 1002/ student)
  416.     Access: 2020-08-25 11:02:15.013355559 +0300
  417.     Modify: 2020-08-25 11:02:15.013355559 +0300
  418.     Change: 2020-08-25 11:02:19.157290764 +0300
  419.     Birth: -
  420.  
  421. # setting SGID
  422. chmod 2750 projects/
  423. chmod g+s projects/
  424.  
  425.  
  426. ## The Sticky Bit
  427.  
  428. # displaying the sticky bit permission
  429. ls -ld /tmp/
  430.     drwxrwxrwt 20 root root 4096 aug 25 10:49 /tmp/
  431.  
  432. stat /tmp/
  433.     File: /tmp/
  434.     Size: 4096          Blocks: 8          IO Block: 4096   directory
  435.     Device: 805h/2053d  Inode: 786434      Links: 20
  436.     Access: (1777/drwxrwxrwt)  Uid: (    0/    root)   Gid: (    0/    root)
  437.     Access: 2020-08-22 14:46:03.259455125 +0300
  438.     Modify: 2020-08-25 10:49:53.756211470 +0300
  439.     Change: 2020-08-25 10:49:53.756211470 +0300
  440.     Birth: -
  441.  
  442. # setting the sticky bit
  443. mkdir temp
  444. chmod 1777 temp/
  445. chmod o+t temp/
  446. ls -ld temp/
  447.     drwxrwxrwt 2 student student 4096 aug 25 11:04 temp/
  448.  
  449.  
  450. ## UMASK
  451. # displaying the UMASK
  452. umask
  453.  
  454. # setting a new umask value
  455. umask new_value     # => Ex: umask 0022
  456.  
  457. ## Changing File Ownership (root only)
  458.  
  459. # changing the owner
  460. chown new_owner file/directory      # => Ex: sudo chown john a.txt
  461.  
  462. # changing the group owner
  463. chgrp new_group file/directory
  464.  
  465. # changing both the owner and the group owner
  466. chown new_owner:new_group file/directory
  467.  
  468. # changing recursively the owner or the group owner
  469. chown -R new-owner file/directory
  470.  
  471. # displaying the file attributes
  472. lsattr filename
  473.  
  474. #changing the file attributes
  475. chattr +-attribute filename     # => Ex: sudo chattr +i report.txt
  476.  
  477.  
  478.  
  479. ##########################
  480. ## Killing processes (kill, pkill, killall)
  481. ##########################
  482.  
  483. # listing all signals
  484. kill -l
  485.  
  486. # sending a signal (default SIGTERM - 15) to a process by pid
  487. kill pid        # => Ex: kill 12547
  488.  
  489. # sending a signal to more processes
  490. kill -SIGNAL pid1 pid2 pid3 ...
  491.  
  492. # sending a specific signal (SIGHUP - 2) to a process by pid
  493. kill -2 pid
  494. kill -HUP pid
  495. kill -SIGHUP pid
  496.  
  497. # sending a signal (default SIGTERM - 15) to process by process name
  498. pkill process_name          # => Ex: pkill sleep
  499. killall process_name
  500. kill $(pidof process_name)  # => Ex: kill -HUP $(pidof sshd)
  501.  
  502. # running a process in the background
  503. command &   # => Ex: sleep 100 &
  504.  
  505. # Showing running jobs
  506. jobs
  507.  
  508. # Stopping (pausing) the running process
  509. Ctrl + Z
  510.  
  511. # resuming and bringing to the foreground a process by job_d
  512. fg %job_id
  513.  
  514. # resuming in the background a process by job_d
  515. bg %job_id
  516.  
  517. # starting a process immune to SIGHUP
  518. nohup command &     # => Ex: nohup wget http://site.com &
  519.  
  520.  
  521.  
  522. #########################
  523. ## Getting info about the network interfaces (ifconfig, ip, route)
  524. ##########################
  525.  
  526. # displaying information about enabled interfaces
  527. ifconfig
  528.  
  529. # displaying information about all interfaces (enabled and disabled)
  530. ifconfig -a
  531. ip address show
  532.  
  533. # displaying info about a specific interface
  534. ifconfig enp0s3
  535. ip addr show dev enp0s3
  536.  
  537. # showing only IPv4 info
  538. ip -4 address
  539.  
  540. # showing only IPv6 info
  541. ip -6 address
  542.  
  543. # displaying L2 info (including the MAC address)
  544. ip link show
  545. ip link show dev enp0s3
  546.  
  547. # displaying the default gateway
  548. route
  549. route -n    # numerical addresses
  550. ip route show
  551.  
  552. # displaying the DNS servers
  553. systemd-resolve --status
  554.  
  555.  
  556. ##########################
  557. ## Setting the network interfaces (ifconfig, ip, route)
  558. ##########################
  559. # disabling an interface
  560. ifconfig enp0s3 down
  561. ip link set enp0s3 down
  562.  
  563. # activating an interface
  564. ifconfig enp0s3 up
  565. ip link set enp0s3 up
  566.  
  567. # checking its status
  568. ifconfig -a
  569. ip link show dev enp0s3
  570.  
  571. # setting an ip address on an interface
  572. ifconfig enp0s3 192.168.0.222/24 up
  573. ip address del 192.168.0.111/24 dev enp0s3
  574. ip address add 192.168.0.112/24 dev enp0s3
  575.  
  576. # setting a secondary ip address on sub-interface
  577. ifconfig enp0s3:1 10.0.0.1/24
  578.  
  579. # deleting and setting a new default gateway
  580. route del default gw 192.168.0.1
  581. route add default gw 192.168.0.2
  582.  
  583. # deleting and setting a new default gateway
  584. ip route del default
  585. ip route add default via 192.168.0.1    
  586.  
  587. # changing the MAC address
  588. ifconfig enp0s3 down
  589. ifconfig enp0s3 hw ether 08:00:27:51:05:a1
  590. ifconfig enp0s3 up
  591.  
  592. # changing the MAC address
  593. ip link set dev enp0s3 address 08:00:27:51:05:a3
  594.  
  595.  
  596. ##########################
  597. ## Network Static configuration using Netplan (Ubuntu)
  598. ##########################
  599.  
  600. # 1. Stop and disable the Network Manager
  601.  
  602. sudo systemctl stop NetworkManager
  603. sudo systemctl disable NetworkManager
  604. sudo systemctl status NetworkManager
  605. sudo systemctl is-enabled NetworkManager
  606.  
  607. # 2. Create a YAML file in /etc/netplan
  608.  
  609. network:
  610.   version: 2
  611.   renderer: networkd
  612.   ethernets:
  613.     enp0s3:
  614.       dhcp4: false
  615.       addresses:
  616.         - 192.168.0.20/24
  617.       gateway4: "192.168.0.1"
  618.       nameservers:
  619.         addresses:
  620.           - "8.8.8.8"
  621.           - "8.8.4.4"
  622.  
  623. # 3. Apply the new config
  624. sudo netplan apply
  625.  
  626. # 4. Check the configuration
  627. ifconfig
  628. route -a
  629.  
  630.  
  631.  
  632. ##########################
  633. ## OpenSSH
  634. ##########################
  635.  
  636. # 1. Installing OpenSSH (client and server)
  637. # Ubuntu
  638. sudo apt update && sudo apt install openssh-server openssh-client
  639.  
  640. # CentOS
  641. sudo dnf install openssh-server openssh-clients
  642.  
  643. # connecting to the server
  644. ssh -p 22 username@server_ip        # => Ex: ssh -p 2267 john@192.168.0.100
  645. ssh -p 22 -l username server_ip
  646. ssh -v -p 22 username@server_ip     # => verbose
  647.  
  648. # 2. Controlling the SSHd daemon
  649. # checking its status
  650. sudo systemctl status ssh       # => Ubuntu
  651. sudo systemctl status sshd      # => CentOS
  652.  
  653. # stopping the daemon
  654. sudo systemctl stop ssh       # => Ubuntu
  655. sudo systemctl stop sshd      # => CentOS
  656.  
  657. # restarting the daemon
  658. sudo systemctl restart ssh       # => Ubuntu
  659. sudo systemctl restart sshd      # => CentOS
  660.  
  661. # enabling at boot time
  662. sudo systemctl enable ssh       # => Ubuntu
  663. sudo systemctl enable sshd      # => CentOS
  664.  
  665. sudo systemctl is-enabled ssh       # => Ubuntu
  666. sudo systemctl is-enabled sshd      # => CentOS
  667.  
  668. # 3. Securing the SSHd daemon
  669. # change the configuration file (/etc/ssh/sshd_config) and then restart the server
  670. man sshd_config
  671.  
  672. a) Change the port
  673. Port 2278
  674.  
  675. b) Disable direct root login
  676. PermitRootLogin no
  677.  
  678. c) Limit Users’ SSH access
  679. AllowUsers stud u1 u2 john
  680.  
  681. d) Filter SSH access at the firewall level (iptables)
  682.  
  683. e) Activate Public Key Authentication and Disable Password Authentication
  684.  
  685. f) Use only SSH Protocol version 2
  686.  
  687. g) Other configurations:
  688. ClientAliveInterval 300
  689. ClientAliveCountMax 0
  690. MaxAuthTries 2
  691. MaxStartUps 3
  692. LoginGraceTime 20
  693.  
  694.  
  695.  
  696. ##########################
  697. ## Copying files using SCP and RSYNC
  698. ##########################
  699.  
  700. ### SCP ###
  701. # copying a local file to a remote destination
  702. scp a.txt john@80.0.0.1:~
  703. scp -P 2288 a.txt john@80.0.0.1:~       # using a custom port
  704.  
  705. # copying a local file from a remote destination to the current directory
  706. scp -P 2290 john@80.0.0.1:~/a.txt .
  707.  
  708. # copying a local directory to a remote destination (-r)
  709. scp -P 2290 -r projects/ john@80.0.0.1:~
  710.  
  711.  
  712. ### RSYNC ###
  713. # synchronizing a directory
  714. sudo rsync -av /etc/ ~/etc-backup/
  715.  
  716. # mirroring (deleting from destination the files that were deleting from source)
  717. sudo rsync -av --delete /etc/ ~/etc-backup/
  718.  
  719. # excluding files
  720. rsync -av --exclude-from='~/exclude.txt' source_directory/ destination_directory/
  721. # exclude.txt:
  722. # *.avi
  723. # music/
  724. # abc.mkv
  725.  
  726. rsync -av --exclude='*.mkv' --exclude='movie1.avi' source_directory/ destination_directory/
  727.  
  728. # synchronizing a directory over the network using SSH
  729. sudo rsync -av -e ssh /etc/ student@192.168.0.108:~/etc-backup/
  730.  
  731. # using a custom port
  732. sudo rsync -av -e 'ssh -p 2267' /etc/ student@192.168.0.108:~/etc-backup/
  733.  
  734.  
  735. ##########################
  736. ## WGET
  737. ##########################
  738. # installing wget
  739. apt install wget        # => Ubuntu
  740. dnf install wget        # => CentOS
  741.  
  742. # download a file in the current directory
  743. wget https://cdimage.kali.org/kali-2020.2/kali-linux-2020.2-installer-amd64.iso
  744.  
  745. # resuming the download
  746. wget -c https://cdimage.kali.org/kali-2020.2/kali-linux-2020.2-installer-amd64.iso
  747.  
  748. # saving the file into a specific directory
  749. mkdir kali
  750. wget -P kali/ https://cdimage.kali.org/kali-2020.2/kali-linux-2020.2-installer-amd64.iso
  751.  
  752. # limiting the rate (bandwidth)
  753. wget --limit-rate=100k -P kali/ https://cdimage.kali.org/kali-2020.2/kali-linux-2020.2-installer-amd64.iso
  754.  
  755. # downloading more files
  756. wget -i urls.txt      # urls.txt contains urls
  757.  
  758. # starting the download in the background
  759. wget -b -P kali/ https://cdimage.kali.org/kali-2020.2/kali-linux-2020.2-installer-amd64.iso
  760. tail -f wget-log        # => checking its status
  761.  
  762. # getting an offline copy of a website
  763. wget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.org
  764. wget -mkEpnp http://example.org
  765.  
  766.  
  767. ##########################
  768. ## NETSTAT and SS
  769. ##########################
  770. # displaying all open ports and connections
  771. sudo netstat -tupan
  772. sudo ss -tupan
  773. netstat -tupan | grep :80   # => checking if port 80 is open
  774.  
  775. ##########################
  776. ## LSOF
  777. ##########################
  778. # listing all files that are open
  779. lsof
  780.  
  781. # listing all files opened by the processes of a specific user
  782. lsof -u username
  783.  
  784. # listing all files opened by a specific process
  785. lsof -c sshd
  786.  
  787. # listing all files that have opened TCP ports
  788. lsof -iTCP -sTCP:LISTEN
  789. lsof -iTCP -sTCP:LISTEN -nP
  790.  
  791.  
  792. ##########################
  793. ## Scanning hosts and networks using nmap
  794. ##########################
  795. ##** SCAN ONLY YOUR OWN HOSTS AND SERVERS !!! **##
  796. ## Scanning Networks is your own responsibility ##
  797.  
  798. # Syn Scan - Half Open Scanning (root only)
  799. nmap -sS 192.168.0.1
  800.  
  801. # Connect Scan
  802. nmap -sT 192.168.0.1
  803.  
  804. # Scanning all ports (0-65535)
  805. nmap -p- 192.168.0.1
  806.  
  807. # Specifying the ports to scan
  808. nmap -p 20,22-100,443,1000-2000 192.168.0.1
  809.  
  810. # Scan Version
  811. nmap -p 22,80 -sV 192.168.0.1
  812.  
  813. # Ping scanning (entire Network)
  814. nmap -sP 192.168.0.0/24
  815.  
  816. # Treat all hosts as online -- skip host discovery
  817. nmap -Pn 192.168.0.0/24
  818.  
  819. # Excluding an IP
  820. nmap -sS 192.168.0.0/24 --exclude 192.168.0.10
  821.  
  822. # Saving the scanning report to a file
  823. nmap -oN output.txt 192.168.0.1
  824.  
  825. # OS Detection
  826. nmap -O 192.168.0.1
  827.  
  828. # Enable OS detection, version detection, script scanning, and traceroute
  829. nmap -A 192.168.0.1
  830.  
  831. # reading the targets from a file (ip/name/network separated by a new line or a whitespace)
  832. nmap -p 80 -iL hosts.txt
  833.  
  834. # exporting to out output file and disabling reverse DNS
  835. nmap -n -iL hosts.txt -p 80 -oN output.txt
  836.  
  837. ##########################
  838. ## Software Management (dpkg and apt)
  839. ##########################
  840.  
  841. ### DPKG ###
  842. # getting info about a deb file
  843. dpkg --info google-chrome-stable_current_amd64.deb
  844.  
  845. # installing an application from a deb file
  846. sudo dpkg -i google-chrome-stable_current_amd64.deb
  847.  
  848. # list all installed programs
  849. dpkg --get-selections
  850. dpkg-query -l
  851.  
  852. # filtering the output
  853. dpkg-query -l | grep ssh
  854.  
  855. # listing all files of an installed package
  856. dpkg-query -l | grep ssh
  857. dpkg -L openssh-server
  858.  
  859. # finding to which package a file belongs
  860. which ls
  861. dpkg -S /bin/ls
  862.     coreutils: /bin/cp
  863.  
  864. # removing a package
  865. sudo dpkg -r google-chrome-stable
  866.  
  867. # purging a package
  868. sudo dpkg -P google-chrome-stable
  869.  
  870. ### APT ###
  871. # updating the package index (doesn't install/uninstall/update any package)
  872. sudo apt update
  873. # installing or updating a package named apache2
  874. sudo apt install apache2
  875.  
  876. # listing all upgradable packages
  877. sudo apt list --upgradable
  878.  
  879. # upgrading all applications
  880. sudo apt full-upgrade
  881. sudo apt full-upgrade -y        # => assume yes to any prompt (useful in scripts)
  882.  
  883. # removing a package
  884. sudo apt remove apache2
  885.  
  886. # removing a package and its configurations
  887. sudo apt purge apache2
  888.  
  889. # removing dependencies that are not needed anymore
  890. sudo apt autoremove
  891.  
  892. # removing the saved deb files from the cache directory (var/cache/apt/archives)
  893. sudo apt clean
  894.  
  895. # listing all available packages
  896. sudo apt list
  897. sudo apt list | wc -l
  898.  
  899. # searching for a package
  900. sudo apt list | grep nginx
  901.  
  902. # showing information about a package
  903. sudo apt show nginx
  904.  
  905. # listing all installed packages
  906. sudo apt list --installed
  907.  
  908.  
  909. ##########################
  910. ## Task Scheduling using Cron
  911. ##########################
  912.  
  913. # editing the current user’s crontab file
  914. crontab -e
  915.  
  916. # listing the current user’s crontab file
  917. crontab -l
  918.  
  919. # removing the current user’s crontab file
  920. crontab -r
  921.  
  922. ## COMMON EXAMPLES ##
  923. # run every minute
  924. * * * * * /path_to_task_to_run.sh
  925.  
  926. # run every hour at minute 15
  927. 15 * * * * /path_to_task_to_run.sh
  928.  
  929. # run every day at 6:30 PM
  930. 30 18 * * * /path_to_task_to_run.sh
  931.  
  932. # run every Monday at 10:03 PM
  933. 3 22 * * 1 /path_to_task_to_run.sh
  934.  
  935. # run on the 1st of every Month at 6:10 AM
  936. 10 6 1 * * /path_to_task_to_run.sh
  937.  
  938. # run every hour at minute 1, 20 and 35
  939. 1,20,35 * * * * /path_to_task_to_run.sh
  940.  
  941. # run every two hour at minute 10
  942. 10 */2 * * * /path_to_task_to_run.sh
  943.  
  944. # run once a year on the 1st of January at midnight
  945. @yearly     /path_to_task_to_run.sh
  946.  
  947. # run once a month at midnight on the first day of the month
  948. @monthly    /path_to_task_to_run.sh
  949.  
  950. # run once a week at midnight on Sunday
  951. @weekly      /path_to_task_to_run.sh
  952.  
  953. # once an hour at the beginning of the hour
  954. @hourly     /path_to_task_to_run.sh
  955.  
  956. # run at boot time
  957. @reboot     /path_to_task_to_run.sh
  958.  
  959. All scripts in following directories will run as root at that interval:
  960. /etc/cron.hourly
  961. /etc/cron.daily  
  962. /etc/cron.hourly  
  963. /etc/cron.monthly
  964. /etc/cron.weekly
  965.  
  966.  
  967.  
  968.  
  969. ##########################
  970. ## Getting System Hardware Information
  971. ##########################
  972.  
  973. # displaying full hardware information
  974. lshw
  975. lshw -short     # => short format
  976. lshw -json      # => json format
  977. lshw -html      # => html format
  978.  
  979. inxi -Fx
  980. # displaying info about the CPU
  981. lscpu
  982. lshw -C cpu
  983. lscpu -J    => json format
  984.  
  985. # displaying info about the installed RAM memory
  986. dmidecode -t memory
  987. dmidecode -t memory | grep -i size
  988. dmidecode -t memory | grep -i max
  989.  
  990. # displaying info about free/used memory
  991. free -m
  992.  
  993. # getting info about pci buses and about the devices connected to them
  994. lspci
  995. lspci | grep -i wireless
  996. lspci | grep -i vga
  997.  
  998. # getting info about USB controllers and about devices connected
  999. lsusb
  1000. lsusb -v
  1001.  
  1002. # getting info about hard disks
  1003. lshw -short -C disk
  1004. fdisk -l
  1005. fdisk -l /dev/sda
  1006. lsblk
  1007. hdparm -i /dev/sda
  1008. hdparm -I /dev/sda
  1009.  
  1010. # benchmarking disk read performance
  1011. hdparm -tT --direct /dev/sda
  1012.  
  1013. # getting info about WiFi cards and networks
  1014. lshw -C network
  1015. iw list
  1016. iwconfig
  1017. iwlist wlo1 scan
  1018.  
  1019. # Getting hardware information from the /proc virtual fs
  1020. cat /proc/cpuinfo
  1021. /proc/partitions
  1022. cat /proc/meminfo
  1023. cat /proc/version
  1024. uname -r    # => kernel version
  1025. uname -a
  1026.  
  1027. acpi -bi    # battery information
  1028. acpi -V
  1029.  
  1030. ## Working directly with device files (dd)
  1031.  
  1032. # backing up the MBR (the first sector of /dev/sda)
  1033. dd if=/dev/sda of=~/mbr.dat bs=512 count=1
  1034.  
  1035. # restoring the MBR
  1036. dd if=~/mbr.dat of=/dev/sda bs=512 count=1
  1037.  
  1038. # cloning a partition (sda1 to sdb2)
  1039. dd if=/dev/sda1 of=/dev/sdb2 bs=4M status=progress
  1040.  
  1041.  
  1042.  
  1043. ##########################
  1044. ## Service Management using systemd and systemctl
  1045. ##########################
  1046. # showing info about the boot process
  1047. systemd-analyze
  1048. systemd-analyze blame
  1049.  
  1050. # listing all active units systemd knows about
  1051. systemctl list-units
  1052. systemctl list-units | grep ssh
  1053.  
  1054. # checking the status of a service
  1055. sudo systemctl status nginx.service
  1056.  
  1057. # stopping a service
  1058. sudo systemctl stop nginx
  1059.  
  1060. # starting a service
  1061. sudo systemctl start nginx
  1062.  
  1063. # restarting a service
  1064. sudo systemctl restart nginx
  1065.  
  1066. # reloading the configuration of a service
  1067. sudo systemctl reload nginx
  1068. sudo systemctl reload-or-restart nginx
  1069.  
  1070. # enabling to start at boot time
  1071. sudo systemctl enable nginx
  1072.  
  1073. # disabling at boot time
  1074. sudo systemctl disable nginx
  1075.  
  1076. # checking if it starts automatically at boot time
  1077. sudo systemctl is-enabled nginx
  1078.  
  1079. # masking a service (stopping and disabling it)
  1080. sudo systemctl mask nginx
  1081.  
  1082. # unmasking a service
  1083. sudo systemctl unmask nginx
  1084.  
  1085.  
  1086. ##########################
  1087. ## Bash Aliases
  1088. ##########################
  1089.  
  1090. # listing all Aliases
  1091. alias
  1092.  
  1093. # creating an alias:  alias_name="command"
  1094. alias copy="cp -i"
  1095.  
  1096. # to make the aliases you define persistent, add them to ~/.bashrc
  1097.  
  1098. # removing an alias: unalias alias_name
  1099. unalias copy
  1100.  
  1101. ## Useful Aliases ##
  1102. alias c="clear"
  1103. alias cl="clear;ls;pwd"
  1104. alias root="sudo su"
  1105. alias ports="netstat -tupan"
  1106. alias sshconfig="sudo vim /etc/ssh/sshd_config"
  1107. alias my_server="ssh -p 3245-l user100 80.0.0.1"
  1108. alias update=”sudo apt update && sudo apt dist-upgrade -y && sudo apt clean”
  1109. alias lt="ls -hSF --size -1"
  1110. alias ping='ping -c 5'
  1111.  
  1112. # Interactive File Manipulation
  1113. alias cp="cp -i"
  1114. alias mv="mv -i"
  1115. alias rm="rm -i"
  1116.  
  1117. ## Important alias ##
  1118. # This may look a bit confusing, but essentially,
  1119. # it makes all of the other aliases you define function correctly when used with sudo
  1120. alias sudo='sudo '      # use single quotes, not double quotes.
  1121.  
  1122. ##########################
  1123. ## Bash Variables
  1124. ##########################
  1125.  
  1126. # defining a variable: variable_name=value
  1127. # variable names should start with a letter or underscore and can contain letters, digits and underscore
  1128. os="Kali Linux"
  1129. version=10
  1130.  
  1131. # referencing the value of a variable (getting the variable value): $variable_name
  1132. echo $os
  1133. echo $version
  1134.  
  1135. # defining a read-only variable (constant)
  1136. declare -r temperature=100
  1137.  
  1138. # removing (unsetting) a variable
  1139. unset version
  1140.  
  1141. # listing all environment variables
  1142. env
  1143. printenv
  1144.  
  1145. # searching for an environment variable
  1146. printenv PATH
  1147. env | grep -i path
  1148.  
  1149. # creating new environment variables for the user: in ~/.bashrc add export MYVAR=”value”
  1150. export IP="80.0.0.1"
  1151.  
  1152. # changing the PATH
  1153. export PATH=$PATH:~/scripts     # in ~/.bashrc
  1154.  
  1155. # getting user input
  1156. read MY_VAR
  1157. echo $MY_VAR
  1158.  
  1159. # displaying a message
  1160. read -p "Enter the IP address: " ip
  1161. ping -c 1 $ip
  1162.  
  1163. read -s -p "Enter password:" pswd
  1164. echo $pswd
  1165.  
  1166.  
  1167. ### SPECIAL VARIABLES AND POSITIONAL ARGUMENTS ###
  1168. ./script.sh filename1 dir1
  1169.  
  1170. $0 => the name of the script itself (script.sh)
  1171. $1 => the first positional argument (filename1)
  1172. $2 => the second positional argument (dir1)
  1173. ...
  1174. ${10} => the tenth argument of the script
  1175. ${11} => the eleventh argument of the script
  1176.  
  1177. $# => the number of the positional arguments
  1178. "$*" => string representation of all positional argument
  1179. $? => the most recent foreground command exit status
  1180.  
  1181.  
  1182. # if [ some_condition_is_true ]
  1183. # then
  1184. #   //execute this code
  1185. # elif [ some_other_condition_is_true ]
  1186. # then
  1187. #   //execute_this_code
  1188. # else
  1189. #   //execute_this_code
  1190. # fi
  1191. ## Examples:
  1192.  
  1193. i=1
  1194. if [[ $i -lt 10 ]]
  1195. then
  1196.    echo "i is less than 10."
  1197. fi
  1198. #################
  1199. i=100
  1200. if [[ $i -lt 10 ]]
  1201. then
  1202.    echo "i is less than 10."
  1203. else
  1204.    echo "i is greater than or equal to 10."
  1205. fi
  1206. ################
  1207. i=10
  1208. if [[ $i -lt 10 ]]
  1209. then
  1210.    echo "i is less than 10."
  1211. elif [[ $i -eq 10 ]]
  1212. then
  1213.    echo "i is 10"
  1214. else
  1215.    echo "i is greater than or equal to 10."
  1216. fi
  1217.  
  1218. ################
  1219. ### TESTING CONDITIONS => man test ###
  1220.  
  1221. ### For numbers (integers) ###
  1222. # -eq   equal to
  1223. # -ne   not equal to
  1224. # -lt   less than
  1225. # -le   less than or equal to
  1226. # -gt   greater than
  1227. # -ge   greater than or equal to
  1228.  
  1229. # For files:
  1230. # -s    file exists and is not empty
  1231. # -f    file exists and is not a directory
  1232. # -d    directory exists
  1233. # -x    file is executable by the user
  1234. # -w    file is writable by the user
  1235. # -r    file is readable by the user
  1236.  
  1237. # For Strings
  1238. # =     the equality operator for strings if using single square brackets [ ]
  1239. # ==    the equality operator for strings if using double square brackets [[ ]]
  1240. # !=    the inequality operator for strings
  1241. # -n $str   str is nonzero length
  1242. # -z $str   str is zero length
  1243.  
  1244. # &&  => the logical and operator
  1245. # ||  => the logical or operator
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
Add Comment
Please, Sign In to add comment