Ravindra BagaleCourses & study guides मराठी Track your progress

6. Linux Advanced Commands

Chala mitrano, basic commands ne aapan Linux madhe firayla shiklo. Aata advanced commands – logs madhe shodhne, text process karne, services, networking, disk, cron, ssh/scp aani scripting. SOC analyst la 2 GB cha log nano madhe ughadta yet nahi; to grep, awk aani pipes vaprto. Pentester pan tech tools vaprto. He chapter nit kela tar tumhi terminal var "pro" disal. Dhyan rakho, pratyek command type kara.

What you will learn in this chapter

  • Searching with grep and find; processing text with awk and sed
  • Pipes, redirection and here-documents
  • Archives with tar, gzip and zip
  • Services with service, boot-time enable with systemctl, logs with journalctl
  • Networking commands, disk commands and scheduling with cron
  • ssh, scp, rsync, environment variables and basic shell scripts

Concepts in this chapter

  1. 6.1Searching Text: grep
  2. 6.2Finding Files: find
  3. 6.3Text Processing: awk and sed
  4. 6.4Pipes, Redirection and Here-Documents
  5. 6.5Archives and Compression: tar , gzip , zip
  6. 6.6Services and Logs: service , systemctl enable , journalctl
  7. 6.7Networking Commands
  8. 6.8Disk and Storage Commands
  9. 6.9Scheduling Tasks: cron
  10. 6.10Remote Access: ssh , scp , rsync and Environment Variables
  11. 6.11Basic Shell Scripting

The chapter recap is at the end of the last concept page.

Chapter 6: Linux advanced commands — practice tasks

Open chapter lessons · Full guided lab: read ssh login log failed attempts (not repeated here)

6.1 Top attacking IPs from a sample log

MediumRetail

Why: grep, awk, sort and uniq turn thousands of log lines into a short answer.

What: Use the sample log and find which IP failed most.

Data file: auth-sample.log

Line
Oct 24 10:01 sshd: Failed password for root from 203.0.113.7
Oct 24 10:02 sshd: Failed password for admin from 203.0.113.7
Oct 24 10:03 sshd: Failed password for root from 198.51.100.4
Oct 24 10:04 sshd: Accepted publickey for ravi from 192.0.2.10
Oct 24 10:05 sshd: Failed password for test from 203.0.113.7

About this file: Made-up SSH log. Each line is one login attempt. Parts: time, program (sshd), result (Failed/Accepted), user, source IP. IPs are documentation ranges, not real people.

How:

  1. Save lines to auth-sample.log.
  2. grep 'Failed' auth-sample.log | awk '{print $NF}' | sort | uniq -c | sort -rn

Expected output: 3 203.0.113.7 then 1 198.51.100.4.

Hint

$NF = last field.

Answer / how to check
  1. Top line must be 203.0.113.7 with 3.

6.2 Watch a process and its open ports

MediumIT

Why: Malware hides as normal names. ps + lsof show what really runs.

What: Start a Node or Python server and find its PID, user and port.

How:

  1. node -e "require('http').createServer((q,r)=>r.end('ok')).listen(3000,'127.0.0.1')" & (or python3 -m http.server 3000 &)
  2. ps aux | grep -E 'node|http.server' | grep -v grep
  3. sudo lsof -i :3000
  4. kill %1

Expected output: lsof shows the PID listening on 127.0.0.1:3000.

Hint

%1 is the first background job.

Answer / how to check
  1. After kill, lsof -i :3000 prints nothing.

6.3 Hash files to detect changes

EasyBanking

Why: A changed hash means a changed file — key for integrity checks.

What: Hash a config file, change one letter, hash again.

How:

  1. echo 'limit=50000 INR' > cfg.txt; sha256sum cfg.txt
  2. sed -i 's/50000/90000/' cfg.txt; sha256sum cfg.txt

Expected output: Two completely different hashes.

Hint

Even one character changes the whole hash.

Answer / how to check
  1. Compare the first 8 characters; they must differ.

Extended teaching notes

Why this lesson matters

Use this lesson to strengthen systems you own: understand the risk, practise safely in a lab, then write and apply the fix.

Apply the fix across stacks

Same class of bug, same idea in every language.

Java / Spring Boot

Parameterise queries, encode output, CSRF tokens, limited Actuator, dependency scanning.

ASP.NET Core

EF Core / parameterised SQL, Razor encoding, HTTPS, Data Protection.

Node.js / Express

helmet, parameterised SQL, npm audit, avoid eval and shell concat.

Python / Django / Flask / FastAPI

ORM, autoescape, pip-audit, Bandit; never shell=True with user input.

PHP / Laravel

PDO/Eloquent, Blade escaping, composer audit.

Go / Ruby

database/sql placeholders / ActiveRecord; html/template / ERB escaping; govulncheck / Brakeman.

nginx / Apache / Tomcat

Hide versions, TLS, security headers, least-open ports.

MySQL / PostgreSQL / MongoDB / Redis

Private networks, auth on, least-privilege users, no 0.0.0.0/0.

AWS / Azure / GCP

Roles not long-lived keys, private data stores, cloud audit logs on.

Mini case study

Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.

Defender's worksheet (do this today)

  1. Write the lesson title, date and the exact lab target you are allowed to use.
  2. List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
  3. Run one safe check from the lesson on a lab system only. Paste the command and output.
  4. Write a two-sentence fix you would give a teammate.
  5. Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.

Indian workplace (sample data)

Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).

Global practice

Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.

Everyday analogy

Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.

Key terms

  • Control: a safeguard (patch, config, process, detection).
  • Finding: a weakness backed by evidence.
  • Remediation: the fix plus how you verified it.
  • Scope: systems you have written permission to touch.
  • Least privilege: only the access the task needs.
  • Defence in depth: several layers so one failure does not end the story.

Common mistakes

  • Skipping written permission "just for a quick scan".
  • Practising on hostel, office or public targets without authorisation.
  • Collecting tool output without writing the fix.
  • Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
  • Publishing screenshots that contain secrets, cookies or internal hostnames.

Recap

  • Evidence, fix, owner, date.
  • Own lab only.
  • Fix > spectacle.