Essential Linux Commands for Cybersecurity

Jul 31, 2024

Essential Linux Commands for Cyber Security

Introduction

  • Focus on essential Linux commands and flags.
  • Target audience: aspiring ethical hackers and cybersecurity professionals.
  • Importance of mastering these commands before advanced tools like Metasploit, nmap, etc.

Navigating the Linux File System

Using Terminal Over File Explorer

  • Prefer terminal for navigation over GUI file explorer.

Basic Commands

ls Command

  • Basic Usage: Lists contents of a directory.
  • Flags:
    • ls -l: Long listing format with detailed information (links, ownership, size, date).
    • ls -a: Includes hidden files.
    • ls -t: Sorts by modification time.
    • ls -h: Human-readable file sizes.
    • Combined: ls -lath

cd Command

  • Basic Usage: Change directory.
  • Navigation Shortcuts:
    • cd -: Go back to the previous directory.
    • cd ..: Move up one directory level.
    • cd ~: Go to the home directory.
  • Tab Completion: Using tab to auto-complete directory names.

pwd Command

  • Usage: Print working directory.

Manipulating Files and Directories

Creating Files and Directories

  • Create Files: touch file1 file2 file3
  • Create Directories: mkdir dir1
  • Create Nested Directories: mkdir -p parent/child/grandchild
  • Permissions: mkdir -m 777 dir (sets full permissions)

Copying Files and Directories

  • Copy Files: cp source_file destination_file
  • Copy Directories: cp -r source_dir destination_dir

Removing Files and Directories

  • Remove Files: rm file
  • Remove Directories: rm -r dir
  • Remove with Wildcards: rm test*

Moving and Renaming Files and Directories

  • Move Files: mv source_file destination_file
  • Rename Files: mv old_name new_name

Disk Usage and Space

du Command

  • Basic Usage: Disk usage of directories.
  • Flags:
    • du -h: Human-readable sizes.
    • du -s: Summarize total size.
  • Example: du -sh *

Managing File Permissions

Understanding Permissions

  • Symbols: r (read), w (write), x (execute).
  • Groups: Owner, Group, Others.

Changing Permissions

  • Command: chmod
  • Adding/Removing Permissions: chmod u+x file (user execute), chmod g-w file (group write)
  • Numeric Notation: chmod 755 file (sets rwxr-xr-x)

Changing Ownership

  • Command: chown
  • Change Owner: chown user file
  • Change Group: chown :group file
  • Change Both: chown user:group file

Viewing File Contents

cat Command

  • Basic Usage: Display file contents.
  • Combine Files: cat file1 file2 > combined_file
  • Append Files: cat file3 >> combined_file

tail Command

  • Basic Usage: View last 10 lines.
  • Flags:
    • tail -n 15 file: Last 15 lines.
    • tail -f file: Follow updates in real-time.

head Command

  • Basic Usage: View first 10 lines.
  • Flags: head -n 15 file: First 15 lines.

less and more Commands

  • Usage: Page through file contents.
  • Search: Use /pattern in less to search.

Text Editors

vi and vim Editors

  • Basic Usage: vi file
  • Insert Mode: i to insert text.
  • Save and Exit: :wq

nano Editor

  • Basic Usage: nano file
  • Ease of Use: Intuitive commands at the bottom.

Process Management

ps Command

  • Basic Usage: List processes.
  • Flags: ps aux for detailed view.

top Command

  • Usage: Interactive process viewer.
  • Filter by User: top -u username
  • Monitor Specific Process: top -p PID

Killing Processes

  • Command: kill PID
  • Force Kill: kill -9 PID

Networking Commands

Viewing Network Configuration

  • Commands: ifconfig, ip addr, ip route show
  • Setting IP Address: ip addr add 192.168.1.10/24 dev eth0

Checking Routes

  • Command: ip route

Connection and File Transfers

  • SSH: ssh user@hostname
  • Copy Files: scp source_file user@hostname:/path/to/destination
  • Synchronize Files: rsync -avz source/ destination/

System Information

uname Command

  • Flags: uname -a for detailed system info.

df Command

  • Basic Usage: Disk free space.
  • Human Readable: df -h

Searching and Finding Files

find Command

  • Search by Name: find /path -name filename
  • Search by Size: find /path -size +1M
  • Search by Modification Time: find /path -mtime -30

grep Command

  • Basic Usage: Search within files.
  • Example: grep 'pattern' filename

Archiving and Compressing Files

tar Command

  • Creating Archives: tar -cvf archive.tar files
  • Extracting Archives: tar -xvf archive.tar
  • With Compression: tar -czvf archive.tar.gz files

gzip Command

  • Compress Files: gzip file
  • Decompress Files: gunzip file.gz
  • List Contents: gzip -l file.gz

User and Group Management

Adding and Deleting Users

  • Add User: useradd username
  • Add User with Home Directory: useradd -m username
  • Delete User: userdel username

Adding and Deleting Groups

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

Conclusion

  • Recap of essential Linux commands crucial for cybersecurity professionals.
  • Encouragement to practice and master these commands before moving to advanced tools.