Essential Linux Commands for Cybersecurity

Jul 31, 2024

Essential Linux Commands for Cybersecurity

Introduction

  • Focus on essential Linux commands and flags to enhance your skills
  • Important for ethical hacking and cybersecurity
  • Recommended to master these before advancing to tools like Metasploit or nmap

Navigating the File System

Using ls Command

  • Basic command to list directory contents
  • ls -l: Long listing format
    • Shows file type, permissions, owner, group, size, and modification date
  • ls -a: Includes hidden files (those starting with a dot)
  • ls -t: Sorts files by modification time
  • ls -h: Human-readable sizes
  • ls -R: Recursively lists directory contents

Using cd Command

  • Change directory command
  • cd -: Go back to the previous directory
  • cd ..: Move up one directory level
  • cd ~: Go to the home directory
  • Tab completion: Autocomplete directory names

pwd Command

  • Displays the current working directory

clear Command

  • Clears the terminal screen

Manipulating Files and Directories

Creating Files and Directories

  • touch filename: Creates an empty file
  • mkdir directoryname: Creates a new directory
  • mkdir -p directory/subdirectory: Creates nested directories
  • mkdir -m permissions directory: Creates a directory with specific permissions

Copying Files and Directories

  • cp source destination: Copy files
  • cp -r source destination: Recursively copy directories

Removing Files and Directories

  • rm filename: Removes a file
  • rm -r directory: Recursively removes a directory and its contents
  • rm -rf directory: Forcefully removes directory and contents (be cautious)

Moving and Renaming Files

  • mv source destination: Move or rename files

Viewing and Editing Files

Viewing File Contents

  • cat filename: Displays file contents
  • cat file1 file2 > newfile: Concatenates files
  • cat file >> existingfile: Appends a file to an existing file
  • tail filename: Shows the last 10 lines of a file
  • tail -n number filename: Shows the last 'number' of lines
  • tail -f filename: Continuously monitors a file for changes
  • head filename: Shows the first 10 lines of a file
  • head -n number filename: Shows the first 'number' of lines
  • less filename: Opens file in a scrollable view
  • more filename: Similar to less, but simpler

Editing Files

  • vi filename: Opens file in vi editor
  • nano filename: Opens file in nano editor (easier for beginners)

Process Management

Viewing Processes

  • ps: Lists processes for the current user
  • ps aux: Detailed process information for all users
  • top: Interactive process viewer
  • htop: An enhanced version of top (if installed)

Managing Processes

  • kill PID: Kill a process by its process ID
  • kill -9 PID: Force kill a process
  • jobs: Lists background jobs
  • fg %jobnumber: Brings a background job to the foreground
  • bg %jobnumber: Sends a job to the background

Networking Commands

Basic Networking

  • ifconfig: Displays network interfaces (deprecated)
  • ip a: Shows IP addresses
  • ip route: Shows routing table
  • ping hostname: Checks connectivity
  • traceroute hostname: Traces route to a host

SSH and SCP

  • ssh user@host: Connects to a remote host via SSH
  • scp source user@host:destination: Securely copies files between hosts

Monitoring Network Connections

  • netstat -l: Shows listening ports
  • ss -t: Shows TCP connections
  • ss -u: Shows UDP connections

Checking System Information

Disk Usage

  • df -h: Shows disk usage in human-readable format
  • du -sh directory: Shows disk usage of a directory

System Information

  • uname -a: Displays system information

Searching and Finding Files

Find Command

  • find /path -name filename: Searches for files by name
  • find /path -size +1M: Finds files larger than 1MB
  • find /path -mtime -30: Finds files modified within the last 30 days

Grep Command

  • grep pattern filename: Searches for a pattern in a file
  • grep -r pattern directory: Recursively searches for a pattern in a directory

Archiving and Compressing Files

Tar and Gzip

  • tar czf archive.tar.gz /path: Creates a compressed tar archive
  • tar xzf archive.tar.gz: Extracts a compressed tar archive
  • gzip filename: Compresses a file
  • gunzip filename.gz: Decompresses a file

User Management

Adding and Deleting Users

  • useradd username: Adds a new user
  • userdel username: Deletes a user
  • usermod -aG groupname username: Adds a user to a group

Adding and Deleting Groups

  • groupadd groupname: Adds a new group
  • groupdel groupname: Deletes a group

Conclusion

  • Mastering these basic commands is essential before moving on to more advanced tools.
  • Practice these commands to gain confidence and efficiency in using Linux for cybersecurity tasks.
  • Check out additional videos and resources for more in-depth tutorials on specific topics.