Essential Linux Commands for Cybersecurity

Jul 31, 2024

Essential Linux Commands for Cybersecurity Professionals

Introduction

  • Presenter: Neilson Networking
  • Topic: Essential Linux commands for ethical hacking and cybersecurity
  • Goal: Mastering these basic commands before moving on to advanced tools like Metasploit, nmap, Hydra, etc.

Navigating the Linux File System

Opening the Terminal

  • Preferred Method: Use the terminal over GUI file explorers.

ls Command

  • Basic Usage: ls lists directory contents.
  • Flags: -l: Long listing format.
  • Explanation of Long Listing: (ls -l)
    • l: Link
    • Permissions (read, write, execute) for owner, group, others
    • Owner and group
    • File size
    • Date modified
    • d: Directory indicator
  • Flags: -a: Show hidden files.
  • Flags: -t: Sort by time modified.
  • Flags: -h: Human-readable sizes.
  • Combining Flags: ls -lath to combine multiple flags.

cd Command

  • Basic Usage: Change directory
  • Example Usage:
    • cd /etc to change to the etc directory
    • cd - to go back to the previous directory
    • cd .. to go up one level
    • cd to go to home directory
  • Tips: Using tab for auto-completion

pwd Command

  • Usage: Print working directory

Manipulating Files and Directories

touch Command

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

mkdir Command

  • Usage: Create directories
  • Example:
    • mkdir dir1 to create a single directory
    • mkdir -p parent/child to create nested directories
    • mkdir -m 777 dir to set permissions

cp Command

  • Usage: Copy files and directories
  • Example:
    • cp file1 file2 to copy a file
    • cp -r dir1 dir2 to copy directories recursively

rm Command

  • Usage: Remove files and directories
  • Example:
    • rm file1 to remove a file
    • rm -r dir1 to remove a directory recursively
    • rm file* to remove files with a pattern

mv Command

  • Usage: Move or rename files
  • Example:
    • mv oldname newname to rename
    • mv file1 dir to move a file

du Command

  • Usage: Disk usage
  • Example:
    • du -h for human-readable format
    • du -sh for summary

File Permissions

chmod Command

  • Usage: Change file permissions
  • Example:
    • chmod o-wx bad_dir to remove write/execute for others
    • chmod g+x file1 to add execute for group
    • chmod 777 file1 for full permissions

chown Command

  • Usage: Change file ownership
  • Example:
    • chown user file to change owner
    • chown user:group file to change owner and group

Viewing Files

cat Command

  • Usage: View file contents
  • Example:
    • cat file1 to view content
    • cat file1 file2 > file3 to concatenate
    • cat file1 >> file3 to append

tail Command

  • Usage: View end of file
  • Example:
    • tail file1 to view last 10 lines
    • tail -n 15 file1 for last 15 lines
    • tail -f file1 for continuous updates

head Command

  • Usage: View beginning of file
  • Example:
    • head file1 for first 10 lines
    • head -n 15 file1 for first 15 lines

less and more Commands

  • Usage: View large files page by page
  • Example:
    • less file or more file
    • Navigation using space for next page
    • Search within less using / command

Text Editors

  • Editors: vi, nano
  • vi: More complex but powerful
    • i to insert, esc to exit insert mode, :wq to save and quit
  • nano: User-friendly
    • ctrl + x to exit, y to confirm save

Process Management

ps Command

  • Usage: View current processes
  • Example:
    • ps aux for detailed view
  • Columns: User, PID, CPU%, MEM%, VSZ, RSS, TTY, STAT, START, TIME, COMMAND
  • Filtering: ps aux | grep process_name

top Command

  • Usage: Interactive process viewer
  • Example:
    • top to start
    • top -u username to filter by user
    • top -p PID to monitor a specific process
  • Columns: Similar to ps, sorted by memory usage

Killing Processes

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

Job Control

  • Background Jobs: command &
  • Listing Jobs: jobs
  • Foreground: fg %job_number
  • Example:
    • ping example.com &
    • fg %1 to bring to foreground

Networking Commands

  • Ifconfig: Deprecated, use ip
  • IP Address: ip addr
  • Default Gateway: ip route show default
  • Adding Routes: ip route add subnet via gateway dev iface
  • Adding IP: ip addr add ip/mask dev iface
  • Netstat: Deprecated, use ss
  • Listening Ports: ss -lt for TCP, ss -lu for UDP
  • SSH: ssh user@host, ssh -p port user@host
  • SCP: Secure copy, scp file user@host:directory
  • Rsync: File synchronization, rsync -options source destination

System Information

uname Command

  • Usage: System information
  • Example: uname -a for detailed info

Disk Usage

  • Command: df
  • Example:
    • df -h for human-readable format
    • df -Th for file system types

Searching Files

Find Command

  • Usage: Search files
  • Example:
    • find /path -name pattern
    • find /path -size +1M
    • find /path -mtime -30

Grep Command

  • Usage: Search within file contents
  • Example:
    • grep pattern file
    • grep -r pattern /path

Archiving and Compression

Tar Command

  • Usage: Archive files
  • Example:
    • tar czf archive.tar.gz files to create
    • tar xzf archive.tar.gz to extract
    • tar rf archive.tar file to add files

Gzip Command

  • Usage: Compress files
  • Example:
    • gzip file to compress
    • gunzip file.gz to decompress
    • gzip -l file.gz to list contents

User Management

Adding Users

  • Command: useradd username
  • Options:
    • -m to create home directory
    • -g group to add to group
    • -e YYYY-MM-DD to set expiration date

Deleting Users

  • Command: userdel username
  • Options: -r to remove home directory

Adding and Deleting Groups

  • Commands: groupadd groupname, groupdel groupname

Conclusion

  • Summary: Covered essential Linux commands for navigation, file manipulation, permissions, process management, networking, and user management.
  • Recommendation: Master these commands for a strong foundation in Linux before moving on to advanced tools.
  • Next Steps: Check out additional videos on advanced tools like Metasploit, nmap, etc.
  • Encouragement: Subscribe, like, and follow for more content.