Linux Commands for Cybersecurity Mastery

Jul 31, 2024

Essential Linux Commands for Ethical Hacking and Cybersecurity

Introduction

  • Focus: Cover essential Linux commands and flags to enhance skills before advancing to tools like Metasploit, nmap, Hydra, etc.
  • Importance: Mastering these basics is necessary for anyone serious about a cybersecurity career.

Navigating the Linux File System

Using the Terminal vs. File Explorer

  • Terminal: Preferred method to navigate and manage files in Linux.

Commands

ls Command

  • Basic Usage: Lists contents of a directory.
  • Flags:
    • -l: Long listing format (details like permissions, owner, size, modification date).
    • -a: Includes hidden files (files starting with .).
    • -t: Sort by modification time (newest first).
    • -h: Human-readable file sizes.
    • Combine flags for more detailed output, e.g., ls -lath.
  • Recursive Listing:
    • -R [directory]: Lists directory contents recursively.
    • Example: ls -R etc

cd Command

  • Basic Usage: Change directory.
  • Auto-Completion: Use tab for auto-complete directory names.
  • Flags/Shortcuts:
    • cd -: Switch to the previous directory.
    • cd ..: Move up one directory level.
    • cd ~: Move to the home directory.

pwd Command

  • Usage: Print working directory.
  • Importance: Helps keep track of current directory.

Manipulating Files and Directories

Creating Files and Directories

touch Command

  • Usage: Create empty files.
  • Example: touch file1 file2 file3

mkdir Command

  • Usage: Create directories.
  • Flags:
    • -p: Create parent directories as needed.
    • -m [permissions]: Set permissions (e.g., 777) when creating.
  • Examples:
    • mkdir -p dir1/dir2/dir3
    • mkdir -m 777 testdir

Copying Files and Directories

cp Command

  • Basic Usage: Copy files.
  • Flags:
    • -r: Copy directories recursively.
    • -f: Force overwrite.
  • Examples:
    • cp file1 file2
    • cp -rf dir1 dir2

Removing Files and Directories

rm Command

  • Basic Usage: Remove files.
  • Flags:
    • -r: Remove directories recursively.
    • -f: Force removal.
  • Examples:
    • rm file1
    • rm -rf directory

Moving and Renaming Files and Directories

mv Command

  • Usage: Move or rename files and directories.
  • Examples:
    • mv oldname newname
    • mv file1 /path/to/destination

Disk Usage and Space Management

du Command

  • Usage: Disk usage of directories and files.
  • Flags:
    • -h: Human-readable format.
    • -s: Summarize total.
  • Example: du -sh /var/log

File Permissions and Ownership

Changing Permissions

chmod Command

  • Usage: Change file and directory permissions.
  • Flags:
    • u, g, o: User, group, other.
    • +, -: Add or remove permissions.
    • r, w, x: Read, write, execute.
  • Examples:
    • chmod u+x filename
    • chmod 777 filename

Changing Ownership

chown Command

  • Usage: Change file and directory ownership.
  • Syntax: chown user:group filename
  • Examples:
    • chown root:users file1
    • chown username: filename

Viewing and Editing Files

Viewing Files

cat Command

  • Usage: Display file contents.
  • Flags:
    • Concatenate and display multiple files.
    • Use > to redirect output.
    • Use >> to append to files.
  • Example: cat file1 file2 > newfile

less and more Commands

  • Usage: View large files page-by-page.
  • Example: less filename, more filename

Editing Files

nano Command

  • Usage: Simple text editor.
  • Example: nano filename

vi Command

  • Usage: Powerful text editor (steeper learning curve).
  • Example: vi filename

Process Management

Viewing Processes

ps Command

  • Usage: View process status.
  • Flags:
    • aux: Detailed process list.
  • Example: ps aux

top Command

  • Usage: Real-time process monitoring.
  • Flags:
    • -u [username]: Filter by user.
    • -p [PID]: Filter by process ID.
  • Example: top -u root

Managing Processes

kill Command

  • Usage: Terminate processes by ID.
  • Flags:
    • -9 [PID]: Force kill.
  • Example: kill -9 1234

jobs, fg, bg Commands

  • Usage: Manage background and foreground jobs.
  • Examples:
    • jobs: List background jobs.
    • fg [job]: Move job to foreground.
    • bg [job]: Move job to background.

Networking Commands

Checking Network Configuration

ifconfig and ip Commands

  • Usage: Display network interfaces and IP addresses.
  • Examples:
    • ifconfig
    • ip addr show
    • ip route show

Network Connections and Ports

netstat and ss Commands

  • Usage: Display network connections, routing tables, etc.
  • Flags:
    • -l: Listening ports.
    • -t: TCP connections.
    • -u: UDP connections.
  • Examples:
    • netstat -ltu
    • ss -lt

Remote Connectivity

ssh Command

  • Usage: Securely connect to remote systems.
  • Examples:
    • ssh username@hostname
    • ssh -p [port] username@hostname

scp Command

  • Usage: Secure copy files between hosts.
  • Example: scp localfile user@remotehost:/path/to/destination

rsync Command

  • Usage: Synchronize files and directories between hosts.
  • Example: rsync -avz /source/ user@remote:/destination/

System Information

uname Command

  • Usage: Display system information.
  • Flags:
    • -a: All information.
  • Example: uname -a

df Command

  • Usage: Display disk space usage.
  • Flags:
    • -h: Human-readable format.
    • -t: File system type.
  • Example: df -hT

Searching for Files and Text

Finding Files

find Command

  • Usage: Search for files in a directory hierarchy.
  • Flags:
    • -name: Search by name.
    • -size: Search by file size.
    • -mtime: Search by modification time.
  • Examples:
    • find /home -name '*.jpg'
    • find /var/log -size +1M
    • find /var/log -mtime -30

Searching Within Files

grep Command

  • Usage: Search text using patterns.
  • Flags:
    • -i: Case insensitive.
    • -r: Recursive.
  • Examples:
    • grep 'search_string' file
    • grep -r 'search_string' /path/to/directory

Archiving and Compression

Creating Archives

tar Command

  • Usage: Create and manage archives.
  • Flags:
    • -c: Create.
    • -x: Extract.
    • -z: Compress with gzip.
    • -f: Specify filename.
  • Examples:
    • tar -czf archive.tar.gz /path/to/directory
    • tar -xzf archive.tar.gz

Compressing Files

gzip Command

  • Usage: Compress files.
  • Flags:
    • -d: Decompress.
  • Examples:
    • gzip filename
    • gzip -d filename.gz

User and Group Management

Adding and Deleting Users

useradd and userdel Commands

  • Usage: Add and delete users.
  • Flags:
    • -m: Create home directory.
    • -G [group]: Add to group.
    • -e [date]: Set account expiration date.
  • Examples:
    • useradd -m -G root username
    • userdel -r username

Adding and Deleting Groups

groupadd and groupdel Commands

  • Usage: Add and delete groups.
  • Examples:
    • groupadd groupname
    • groupdel groupname