Ravindra BagaleCourses & study guides

49. Cheat Sheets

49.2 Linux Cheat Sheet

Amazon Linux / CentOS-family first – sudo yum + sudo service. Ubuntu: swap install to sudo apt update && sudo apt install … and service names may differ (apache2 vs httpd). Aata pudhe jaauya.

Files and paths

CommandMeaningBlue note
pwdPrint working directoryKnow where you edit configs before reload
ls -laList including hidden + permissionsSpot world-writable secrets (777 on config.php)
cd /pathChange directory—
cp -a src dstCopy preserving attributesBackup before risky edit
mv old newMove / rename—
rm -i fileRemove with promptPrefer -i; never blind rm -rf /
mkdir -p a/bCreate nested dirs—
cat / tail -​f / lessRead / follow / page a filetail -f for live log watch in IR
head -n 20 fFirst 20 linesQuick banner / config peek
ln -s target linkSymbolic linkAudit unexpected symlinks in web roots

Permissions, ownership

CommandMeaningBlue note
chmod 600 fileOwner read/write onlyPrivate keys / secrets → 600 or tighter
chmod 644 fileOwner rw; group/other rTypical public web asset – not secrets
chmod 755 dirDir executable-as-traverseAvoid 777 on app dirs
chmod 400 key.pemRead-only owner (SSH key habit)Required before ssh -i on many clients
chown user:group fChange owner/groupWeb files: least-privilege owner, not world
umaskDefault new-file maskTighten if lab creates world-readable secrets
getfacl / setfacl (where available)ACL view/setComplex shares – document who has access

Users, groups, sudo

CommandMeaningBlue note
whoami / idCurrent user / uid gid groupsConfirm least privilege before admin work
sudo -lList allowed sudoAudit NOPASSWD / broad ALL entries
useradd -m u / passwd uCreate user / set passwordPrefer keys over passwords for SSH
usermod -​aG group uAdd to groupMinimal groups only
userdel -r uDelete user + homeOffboarding checklist
visudoEdit sudoers safelyNever chmod 777 /​etc/​sudoers
last / lastbLogin / failed login historyBrute-force hygiene review

Processes

CommandMeaningBlue note
ps aux / ps -efProcess listSpot unexpected listeners / miners in lab IR drills
top / htopLive CPU/memSudden CPU spike → investigate
kill PID / kill -9 PIDTerminate (9 = force)Prefer graceful stop via sudo service for daemons
pgrep -a nameFind PIDs by name—
nice / renicePriorityRare in beginner SOC – know it exists

Networking

CommandMeaningBlue note
ip a / ip rAddresses / routesConfirm host-only NIC before scanning
ss -tulpnListening socketsClose unexpected bind-all services
ping -c 4 hostReachabilityICMP may be blocked – not sole health check
curl -I URLHTTP headersQuick TLS / redirect sanity
dig / nslookupDNS lookupVerify expected A/CNAME before go-live
traceroute hostPath hopsLab / troubleshooting – not attack

Packages and services (Amazon Linux focus)

CommandMeaningBlue note
sudo yum install -​y pkgInstall packageOnly needed packages; remove unused later
sudo yum update -yApply updatesCore Blue hygiene – schedule it
sudo yum check-​updateList available updatesPatch planning
sudo yum remove -​y pkgRemove packageShrink attack surface
sudo service name startStart servicePrefer this over day-to-day systemctl start
sudo service name stopStop serviceDisable unused first in hardening
sudo service name restartRestartAfter config change (test first)
sudo service name reloadReload config if supportedOften gentler than full restart
sudo service name statusStatusConfirm fail2ban / firewalld running
sudo systemctl enable nameStart on bootEnable only what must survive reboot
sudo nginx -t / sudo apachectl configtestConfig syntax testAlways before reload

Logs, search, archives, disk

CommandMeaningBlue note
sudo tail -​f /​var/​log/​secureAuth log follow (RHEL-family)SSH brute / sudo noise
sudo journalctl -​u name -​fUnit logs (where systemd)Service crash / fail loops
grep -​Rni pattern dirRecursive searchFind secrets / TODOs before commit
find /​path -​name '*.​log'Find by nameLocate large logs filling disk
tar -​czvf a.​tgz dirCreate gzip archiveLab evidence packs – hash after
tar -xzvf a.tgzExtractInspect untrusted archives in sandbox
df -h / du -sh *Disk free / dir sizesFull disk = silent outage / log loss

SSH / SCP and hardening one-liners

Command / ideaMeaningBlue note
ssh -​i key.​pem user@hostSSH with keyKeys-only; chmod 400 the key
scp -​i key.​pem f user@host:​pathCopy over SSHPrefer over clear FTP
sshd: PasswordAuthentication noDisable password SSHAfter key works; second session open
PermitRootLogin noNo root SSHStandard harden
sudo yum install -​y fail2ban then sudo service fail2ban startBan noisy authTune jails; watch false positives
sudo yum install -​y firewalld + allow only needed portsHost firewallPair with cloud SG least privilege
Red team (attacker) does Blue team (defender) detects / stops
Brute SSH password on open port 22 Keys-only sshd; fail2ban; SG restrict source; lastb review
Leaves world-writable web configs ls -la; chmod 640 secrets; non-root owner
Skips sudo yum update so old daemons stay open Patch cadence; yum check-update; remove unused packages
Binds MySQL/SSH to 0.0.0.0 “for convenience” ss -tulpn; bind localhost where possible; firewalld drop

Ravindra Bagale's Tip

Day-to-day service control sathi sudo service name start|stop|restart|reload|status vapra. sudo systemctl start avoid kara (boot sathi sudo systemctl enable OK). Package sathi sudo yum – dnf rattu naka unless OS docs force it. Config change → test (nginx -t / apachectl configtest) → mag reload. Samjla ka?

Lab

On YOUR Amazon Linux host-only or Free Tier EC2: (1) ss -tulpn baseline, (2) sudo yum check-update, (3) confirm sudo service sshd status, (4) set a practice user’s key auth in a second session, (5) document three Blue fixes you would apply before any public EIP. No scan outside OWN IPs.