🔐

Fundamental Linux Commands for Cybersecurity

Jul 31, 2024

Essential Linux Commands for Cyber Security

Introduction

  • Essential Linux commands and flags for cyber security
  • Importance of mastering basic commands before advanced tools like Metasploit, nmap, Hydra

Navigating the File System

Using Terminal over File Explorer

  • Open terminal to navigate file system

ls Command

  • Basic usage: ls similar to dir in Windows
  • Flags for ls:
    • ls -l: Long listing (detailed file info)
    • ls -a: Show hidden files
    • ls -t: Sort by modification time
    • ls -h: Human-readable file sizes
    • Combine flags: ls -lath

Changing Directories with cd

  • Basic usage: cd directory_name
  • Tips:
    • Use Tab for auto-completion
    • cd -: Go back to the previous directory
    • cd ..: Move up one level
    • cd ~: Go to home directory
  • Display current directory: pwd

Manipulating Files and Directories

Creating Files

  • touch filename: Create a blank file
  • Create multiple files: touch file1 file2 file3

Creating Directories

  • mkdir directory_name: Create a single directory
  • mkdir -p parent/child: Create nested directories
  • Set permissions while creating: mkdir -m 777 directory_name

Copying Files and Directories

  • Copy a file: cp source_file target_file
  • Copy a directory: cp -r source_directory target_directory

Removing Files and Directories

  • Remove a file: rm filename
  • Remove a directory: rm -r directory_name
  • WARNING: rm -rf / can delete the entire system

Moving and Renaming Files

  • Move/Rename a file: mv source_file target_file
  • Move multiple files: mv file1 file2 directory

Disk Usage and Filesystem

Disk Usage

  • Check disk usage: du
  • Human-readable: du -h
  • Summary: du -sh

File Permissions and Ownership

Changing Permissions

  • chmod ugo+rwx filename: Change permissions
  • Numerical mode: chmod 777 filename
  • Remove execute rights: chmod u-x filename

Changing Ownership

  • Change owner: chown user filename
  • Change group: chgrp group filename
  • Change both owner and group: chown user:group filename

Viewing and Editing Files

Viewing Files

  • cat filename: Display file content
  • Combine files: cat file1 file2 > file3
  • Append to a file: cat file1 >> file3

Tail and Head

  • tail filename: Show last 10 lines
  • head filename: Show first 10 lines
  • Follow file updates: tail -f filename

Less and More

  • less filename: View file one page at a time
  • more filename: Similar to less but with fewer features

Text Editors

  • nano filename: Easy-to-use text editor
  • vi filename: Powerful but complex text editor

Process Management

Viewing Processes

  • ps: Show current user's processes
  • ps aux: Detailed view of all processes
  • top: Real-time system statistics
  • View user-specific processes: top -u username

Killing Processes

  • kill PID: Terminate a process
  • Forced kill: kill -9 PID
  • Interrupt process: kill -2 PID

Background and Foreground Processes

  • Send to background: command &
  • View background jobs: jobs
  • Bring to foreground: fg %job_number

Networking Commands

Viewing and Configuring Network

  • ifconfig: View network interfaces (deprecated)
  • ip a: View network interfaces (replacement)
  • ip route: Show routing table
  • Add route: ip route add subnet via gateway dev interface

Inspecting Network Connections

  • netstat -l: View listening ports
  • ss -t: View TCP connections
  • ss -u: View UDP connections
  • ss -p: View processes using sockets

SSH and File Transfer

  • ssh user@host: Secure Shell connection
  • scp source_file user@host:target_directory: Securely copy files
  • rsync: Advanced file copy and synchronization

System Information

  • uname -a: Display system information
  • df -h: Show disk space usage (human-readable)

Searching Files

Find and Grep

  • find /path -name filename: Search for files
  • Search by size: find /path -size +1M
  • Search by modification time: find /path -mtime -30
  • grep pattern file: Search within files

Archiving and Compression

Tar and Gzip

  • Create archive: tar cf archive.tar files
  • Extract archive: tar xf archive.tar
  • Compress file: gzip filename
  • Decompress: gunzip filename.gz

User and Group Management

  • Add user: useradd username
  • Delete user: userdel username
  • Add group: groupadd groupname
  • Delete group: groupdel groupname

Conclusion

  • Importance of mastering these commands for cyber security
  • Encouragement to practice and explore additional resources