Essential Linux Commands for Cybersecurity

Jul 31, 2024

Essential Linux Commands for Cybersecurity

Introduction

  • Purpose: Cover essential Linux commands for ethical hacking and cybersecurity.
  • Focus: Basic commands and flags to enhance capabilities.

Navigating the File System

Using the Terminal

  • Navigate with Terminal: Preferred over file explorer.
  • Open Terminal: Essential for command usage.

ls Command

  • Basic Command: ls lists files and directories.
  • Flags:
    • -l (long listing): Provides detailed info (links, rights, owner, group, size, date).
    • -a (all): Shows hidden files.
    • -t (time): Orders by modification date.
    • -h (human-readable): Displays file sizes in KB, MB, etc.
    • Combine Flags: ls -lath for comprehensive listing.

cd Command

  • Change Directory: cd [directory_name]
  • Tips:
    • cd -: Switches to the previous directory.
    • cd ..: Moves up one level.
    • cd /path/to/dir: Directly navigate to a directory.
    • Tab Completion: Use Tab for auto-complete directory names.
    • Home Directory: cd ~ or just cd.

PWD Command

  • Print Working Directory: pwd shows the current directory.

File and Directory Management

touch Command

  • Create Files: touch file1 file2 ...

mkdir Command

  • Create Directories: mkdir dir1 dir2 ...
  • Flags:
    • -p: Creates parent directories as needed.
    • -m: Sets permissions (e.g., mkdir -m 777 dir).

cp Command

  • Copy Files: cp source destination
  • Copy Directories: cp -r source_dir destination_dir

rm Command

  • Remove Files: rm file
  • Remove Directories: rm -r dir
  • Force Removal: rm -rf dir

mv Command

  • Move/Rename Files: mv old_name new_name or mv file /new/path

Disk Usage

du Command

  • Disk Usage: du [options] [dir]
  • Flags:
    • -h: Human-readable sizes.
    • -s: Summarize total size.

Permissions and Ownership

chmod Command

  • Change Permissions: chmod [permissions] file/dir
  • Examples:
    • chmod 777 file: Full permissions.
    • chmod u+x file: Adds execute permission to the user.

chown Command

  • Change Ownership: chown user:group file/dir
  • Examples:
    • chown root:users file
    • chown user file

Viewing Files

cat Command

  • View File Contents: cat file
  • Combine Files: cat file1 file2 > combined_file
  • Append Files: cat file >> existing_file

tail Command

  • View End of File: tail file
  • Flags:
    • -n [number]: Number of lines to show.
    • -f: Follow file updates.

head Command

  • View Beginning of File: head file
  • Flags:
    • -n [number]: Number of lines to show.

less & more Commands

  • Paged Viewing: less file or more file
  • Navigation: Use space to scroll.
  • Search: /pattern in less

Text Editors

vi and nano

  • vi: Advanced text editor (insert mode, command mode).
  • nano: User-friendly text editor.

Process Management

ps Command

  • View Processes: ps aux for detailed list.
  • Flags:
    • aux: All processes with details.

top Command

  • Real-Time Process Viewer: top
  • User-Specific: top -u username

kill Command

  • Terminate Processes: kill PID
  • Force Terminate: kill -9 PID

Background Processes

  • Start in Background: command &
  • List Jobs: jobs
  • Foreground: fg %job_number

Networking Commands

ifconfig and ip

  • View Network Info: ifconfig or ip addr
  • Default Gateway: ip route
  • Add IP: ip addr add 192.168.1.1/24 dev eth0
  • Remove IP: ip addr del 192.168.1.1/24 dev eth0

netstat and ss

  • Network Statistics: netstat -l or ss -t

ssh and scp

  • Secure Shell: ssh user@host
  • Secure Copy: scp source user@host:/path

Checking System Info

uname Command

  • System Info: uname -a

df Command

  • Disk Free Space: df -h

Searching Files

find Command

  • Search Files: find /path -name "pattern"
  • By Size: find /path -size +1M
  • By Time: find /path -mtime -30

grep Command

  • Search in Files: grep "pattern" file

Archiving and Compression

tar Command

  • Create Archive: tar czf archive.tar.gz files
  • Extract Archive: tar xzf archive.tar.gz
  • Add to Archive: tar rf archive.tar file

gzip Command

  • Compress File: gzip file
  • Decompress File: gunzip file.gz
  • List Contents: gzip -l archive.tar.gz

User Management

useradd and userdel

  • Add User: useradd username
  • Delete User: userdel username

groupadd and groupdel

  • Add Group: groupadd groupname
  • Delete Group: groupdel groupname

Conclusion

  • Essentials Covered: Commands to navigate, manage files, processes, networking, and users.
  • Further Learning: Practice and explore more advanced tools and commands.