How to Check If My Linux Server Was Hacked (Safe Checks)
Start with read-only checks on your server: recent logins (last -a), unexpected users (getent passwd), listening ports (ss -tulpn), new cron jobs, and recently changed binaries. Document everything. If you confirm a breach, isolate, rotate keys, and rebuild from a clean image when needed.
Quick answer
last -a | head
sudo journalctl -u sshd --since "7 days ago" | grep Accepted
sudo ss -tulpn
sudo ls -lt /etc/cron.* /var/spool/cron 2>/dev/null | headWhy a careful checklist?
Panic deletes evidence. A short, ordered checklist keeps you safe and useful. Hannah in Toronto and Rahul in Satara both use the same first five commands.
What are you proving?
You want unexpected access (logins/keys), unexpected presence (processes/ports), or unexpected persistence (cron/systemd).
How do I check step by step?
- Optional but smart: create an EBS / disk snapshot.
- Logins:
last -a | head sudo journalctl -u sshd --since "7 days ago" | grep -i Acceptedec2-user pts/0 ... still logged in 103.60.210.132 Accepted publickey for ec2-user from 103.60.210.132What it means: These successes match a known admin IP in this sample. Unknown IPs need urgent review.
- Users with login shells:
getent passwd | awk -F: '$7 !~ /nologin|false/ {print}'What it means: Lists accounts that can get a shell. Remove strangers.
- Listeners:
sudo ss -tulpnWhat it means: Unknown listeners (crypto miners often use odd high ports) are red flags.
- Persistence points:
sudo crontab -l; ls -la /etc/cron.d /etc/cron.daily 2>/dev/null sudo cat /root/.ssh/authorized_keys 2>/dev/nullWhat it means: New cron lines or SSH keys you did not place are classic persistence.
- Recently changed binaries (example):
sudo find /usr/bin /usr/sbin -type f -mtime -2 2>/dev/null | headWhat it means: Binaries modified in the last two days deserve an explanation (patches vs tampering).
- If you confirm compromise: isolate network, rotate all secrets, rebuild from a known-good image, then harden (SSH keys, Fail2ban, fewer open ports).
For website files and CMS symptoms, also use the Is my website hacked? tool.
Ravindra Bagale's Tip
Do not run random "cleaner" scripts from the internet on a suspect host. They can destroy evidence or be malware. Snapshot the disk first if the cloud allows it. Then investigate.
Ravindra Bagale's Tip – मराठी
संशयास्पद होस्टवर इंटरनेटवरील random "cleaner" scripts चालवू नका. पुरावा नष्ट होऊ शकतो किंवा ते malware असू शकते. शक्य असल्यास आधी disk snapshot घ्या. मग तपासा.
Ravindra Bagale's Tip – हिंदी
संदिग्ध होस्ट पर इंटरनेट की random "cleaner" scripts न चलाओ। सबूत मिट सकते हैं या वे malware हो सकते हैं। हो सके तो पहले disk snapshot लो। फिर जाँच करो।
Quick quiz: check what you learned
Which command lists recent interactive logins?
- last -a for recent logins
- sha256sum on /etc/hosts only
- ping 8.8.8.8 for connectivity
- curl example.com for HTTP check
Show answer
A. last reads wtmp; look for unknown users or IPs.
If you confirm a compromise, the safe recovery path is:
- Rotate secrets and rebuild from a clean image
- Only change the desktop wallpaper theme
- Turn off logging to hide the trail
- Share the host publicly on the internet
Show answer
A. Assume secrets leaked; rebuild from trusted image.
Learn it properly
This guide is the short path. These pages go deeper, with labs:
Frequently asked questions
How do I check if my Linux server was hacked?
Review logins, users, ports, cron, SSH keys and recent file changes. Use the commands in this guide.
What is a strong sign of compromise?
Successful login you do not recognise, new root SSH key, unknown listening port, or cron calling a strange script.
Should I delete files immediately?
Prefer snapshot and notes first. Deleting can remove evidence.
Website-only checklist?
Use /cyber/tools/is-my-website-hacked/ for web-focused steps.
Related lessons?
26.x privilege escalation defence; 31.x IR; 32.x hardening.