Fundamental Linux Commands for Cybersecurity

Jul 31, 2024

Essential Linux Commands for Cyber Security

Introduction

  • Focus on essential Linux commands and flags.
  • Important for ethical hacking and cybersecurity.
  • Mastering these commands is crucial before advanced tools like Metasploit, nmap, etc.

Navigating the File System

Using Terminal

  • Preferred method over GUI for navigating Linux file system.
  • Open terminal to start.

Basic Commands

ls Command

  • ls: Lists directory contents.
  • Flags:
    • -l: Long listing format.
      • Displays permissions, number of links, owner, group, size, and date modified.
    • -a: Shows hidden files (those starting with .).
    • -t: Sorts by modification time, newest first.
    • -h: Human-readable file sizes.
    • Combining Flags: ls -lath for all combined.

cd Command

  • cd: Change directory.
  • Shortcuts:
    • Tab Completion: Auto-completes directory names.
    • cd -: Returns to the previous directory.
    • cd ..: Moves up one directory level.
    • cd: Returns to home directory.
    • pwd: Print working directory.

Manipulating Files and Directories

Creating Files and Directories

  • touch: Creates empty files (e.g. touch file1 file2).
  • mkdir: Makes directories (e.g. mkdir dir1).
  • mkdir -p: Creates nested directories.
  • Assigning Permissions: mkdir -m 777 dir sets full permissions.

Copying Files and Directories

  • cp: Copy files (e.g. cp file1 file2).
  • cp -r: Copy directories recursively.

Removing Files and Directories

  • rm: Remove files (e.g. rm file1).
  • rm -r: Remove directories recursively.
  • rm -rf: Force remove files/directories (use with caution).

Moving and Renaming Files

  • mv: Move or rename files (e.g. mv file1 newlocation).

Disk Usage and Space

Checking Disk Usage

  • du: Disk usage of files and directories.
  • Flags:
    • -h: Human-readable format.
    • -s: Summary of total usage.

File Permissions and Ownership

Modifying Permissions

  • chmod: Change permissions (e.g. chmod g+rw file).
  • Symbols: u (user), g (group), o (others), a (all).
  • Numeric Mode: chmod 777 file gives full permissions.

Changing Ownership

  • chown: Change file ownership (e.g. chown user:group file).

Viewing File Contents

cat Command

  • cat: Concatenate and display file contents.
  • Appending: cat file1 >> file2 appends file1 to file2.

tail Command

  • tail: Displays last part of a file.
  • Flags:
    • -n: Number of lines to display.
    • -f: Follow file updates (real-time).

head Command

  • head: Displays first part of a file.
  • Flags:
    • -n: Number of lines to display.

less and more

  • less: View file contents page by page (better than more).
  • more: Similar to less but with fewer features.

Text Editors

vi and nano

  • vi: Powerful, complex text editor.
  • nano: User-friendly, easy-to-use text editor.
  • Basic Commands: Insert, save, quit, etc.

Process Management

Viewing Processes

  • ps: Display current processes.
  • ps aux: Detailed process listing.
  • grep: Filter processes (e.g. ps aux | grep processname).

top Command

  • top: Real-time process monitoring.
  • htop: Enhanced version of top.

Killing Processes

  • kill: Terminate process by ID (e.g. kill 1234).
  • kill -9: Force kill (use with caution).

Background and Foreground Processes

  • Background: command & to run in background.
  • jobs: List background jobs.
  • fg: Bring background job to foreground.

Networking Commands

Checking Network Configuration

  • ifconfig: Network interface configuration (deprecated).
  • ip: Newer command replacing ifconfig (e.g. ip addr).

Checking Routes

  • ip route: Show routing table.

SSH and SCP

  • ssh: Securely connect to remote machines.
  • scp: Securely copy files between machines.

Viewing Network Connections

  • netstat: Network statistics (deprecated).
  • ss: Newer command showing socket statistics.

Finding Files

find Command

  • find: Search for files (e.g. find /home -name '*.txt').
  • grep: Search text within files.

Archiving and Compression

tar and gzip

  • tar: Archive files into a tarball (e.g. tar -czf archive.tar.gz dir).
  • gzip: Compress files (e.g. gzip file).
  • Uncompress: gunzip for .gz files, tar -xzf for tarballs.

User Management

Adding and Deleting Users

  • useradd: Add a new user (e.g. useradd username).
  • userdel: Delete a user (e.g. userdel username).

Adding and Deleting Groups

  • groupadd: Add a new group (e.g. groupadd groupname).
  • groupdel: Delete a group (e.g. groupdel groupname).

Conclusion

  • Importance of mastering basic Linux commands.
  • Foundation for advanced cybersecurity tools and techniques.
  • Encouragement to practice and utilize these commands efficiently.