Essential Linux Commands for Cybersecurity

Jul 31, 2024

Essential Linux Commands for Cybersecurity

Overview

  • Introduction to essential Linux commands for ethical hacking and cybersecurity
  • Importance of mastering basic commands before moving to advanced tools
  • Commands covered: ls, cd, pwd, mkdir, cp, rm, mv, du, chmod, chown, cat, tail, head, less, nano, ps, top, kill, bg, fg, ifconfig, ip, netstat, ss, ssh, scp, rsync, tar, gzip, find, grep

Navigation Commands

ls

  • Basic usage: ls
  • Flags:
    • ls -l: Long listing format
    • ls -a: Show hidden files
    • ls -t: Sort by modification time
    • ls -h: Human-readable format
    • Combined: ls -lath

cd

  • Change directory: cd [directory]
  • Go back to previous directory: cd -
  • Go up one level: cd ..
  • Go to home directory: cd
  • Use tab for auto-completion of directory names

pwd

  • Print working directory: pwd

File and Directory Management

touch

  • Create empty files: touch file1 file2

mkdir

  • Create directories: mkdir dir1
  • Create nested directories: mkdir -p dir1/dir2/dir3
  • Set permissions while creating: mkdir -m 777 dir1

cp

  • Copy files: cp file1 file2
  • Copy directories: cp -r dir1 dir2

rm

  • Remove files: rm file1
  • Remove directories: rm -r dir1
  • Force removal: rm -rf dir1

mv

  • Move or rename files: mv file1 file2
  • Move files to directories: mv file1 dir1

du

  • Disk usage: du
  • Human-readable format: du -h
  • Summary: du -sh

Permissions and Ownership

chmod

  • Change permissions: chmod [permissions] file
  • Add execute permission: chmod +x file

chown

  • Change ownership: chown user:group file
  • Change owner only: chown user file
  • Change group only: chown :group file

Viewing and Editing Files

cat

  • Display file contents: cat file
  • Concatenate files: cat file1 file2 > file3
  • Append files: cat file3 >> file1

tail

  • Show end of file: tail file
  • Specify number of lines: tail -n 20 file
  • Follow file: tail -f file

head

  • Show beginning of file: head file
  • Specify number of lines: head -n 20 file

less and more

  • View large files page by page: less file, more file
  • Search within less: /search_term

nano and vi

  • nano for simple text editing: nano file
  • vi for more advanced editing: vi file
    • Insert mode: i
    • Save and exit: :wq
    • Exit without saving: :q!

Process Management

ps

  • List processes: ps
  • Detailed list: ps aux

top

  • Interactive process viewer: top
  • Filter by user: top -u username
  • Filter by PID: top -p PID

kill

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

bg and fg

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

Networking

ifconfig and ip

  • Display network interfaces: ifconfig, ip addr
  • Display routing table: ip route
  • Add IP address: ip addr add IP/NETMASK dev INTERFACE

netstat and ss

  • Display network connections: netstat -l, ss -t
  • Display listening ports: ss -lt

ssh and scp

  • SSH to remote host: ssh user@host
  • Copy files over SSH: scp file user@host:/path

rsync

  • Synchronize directories: rsync -av source/ destination/

File Compression

tar

  • Create tar archive: tar -cvf archive.tar files
  • Extract tar archive: tar -xvf archive.tar
  • Create compressed tar: tar -czvf archive.tar.gz files

gzip

  • Compress file: gzip file
  • Decompress file: gunzip file.gz

Searching

find

  • Find files: find /path -name 'filename'
  • Find by size: find /path -size +1M
  • Find by modification time: find /path -mtime -30

grep

  • Search within files: grep 'pattern' file
  • Recursive search in directory: grep -r 'pattern' /path

System Information

uname

  • Display system information: uname -a

df

  • Display disk space usage: df -h

User Management

useradd, usermod, userdel

  • Add user: useradd username
  • Create home directory: useradd -m username
  • Set expiration date: useradd -e YYYY-MM-DD username
  • Delete user: userdel username
  • Delete user with home directory: userdel -r username

groupadd, groupdel

  • Add group: groupadd groupname
  • Delete group: groupdel groupname

Conclusion

  • Importance of mastering these essential commands
  • Encouragement to practice and explore more advanced topics in cybersecurity