Dev.to Security πŸ” Cybersecurity πŸ‘ 1 πŸ“– 4 min read

Was my Linux server hacked? A 10-minute check from the terminal

Something feels off. The server is slow, the bill jumped, or you saw a login you don't remember. Before you panic, there's a short list of places an attacker almost always leaves a trace. This is a check you can run you

Something feels off. The server is slow, the bill jumped, or you saw a login you don't remember. Before you panic, there's a short list of places an attacker almost always leaves a trace.

This is a check you can run yourself. It takes about ten minutes and works on Debian, Ubuntu, Rocky Linux and AlmaLinux. Run the commands as a user with sudo.

One honest warning first: a careful attacker with root can hide from every command below. A clean result lowers the odds; it doesn't prove the server is safe. If you find something real, skip to the end.

1. Who has logged in

Recent successful logins, with the address they came from:

last -a | head -20

Failed attempts. A long list is normal on any public server; it's bots guessing.

sudo lastb -a | head -20

Who is logged in right now:

w

Look for logins at times you were asleep, or from countries you've never been to.

2. SSH logins in the logs

last can be wiped. The SSH log is a second source. This works on all four systems:

sudo journalctl _COMM=sshd --since "7 days ago" | grep "Accepted"

Each line shows the user, the address and whether it was a key or a password. If you turned password logins off and you still see Accepted password, that's worth a closer look.

3. Keys that let someone back in

The most common way back in is an extra key in authorized_keys. List every one:

for d in /root /home/*; do
  echo "== $d"
  sudo cat "$d/.ssh/authorized_keys" 2>/dev/null
done

Every line should be a key you recognise. The comment at the end of each line, often an email or hostname, helps.

4. Users you didn't create

Any account with user ID 0 has full root power. There should be exactly one, root:

awk -F: '$3 == 0 {print $1}' /etc/passwd

Accounts that can log in with a shell:

grep -vE 'nologin|false' /etc/passwd

Who has sudo (the group is sudo on Debian and Ubuntu, wheel on Rocky and AlmaLinux):

getent group sudo wheel

5. What is eating the CPU

Crypto miners are the most common thing found on a hacked VPS. They show up at the top of this list:

ps aux --sort=-%cpu | head -15

Be suspicious of random-looking names, processes running from /tmp, or something using close to 100% CPU all the time.

6. Open ports and outside connections

What the server is listening on:

sudo ss -tulpn

What it is connected to right now:

sudo ss -tnp state established

You should recognise every listening port. A connection from an unknown process to an unfamiliar address on a port like 3333 or 4444 is a classic miner pattern.

7. Scheduled jobs

Attackers like to come back every few minutes through cron. Check every user's crontab:

for u in $(cut -d: -f1 /etc/passwd); do
  sudo crontab -l -u "$u" 2>/dev/null | sed "s/^/$u: /"
done

And the system-wide ones:

sudo ls -la /etc/cron.d /etc/cron.daily /etc/cron.hourly

systemd timers can do the same job:

systemctl list-timers --all

8. Services that start on boot

systemctl list-unit-files --type=service --state=enabled

Look for names you don't recognise, then check what one runs with systemctl cat name.service.

9. Files changed recently

System files that changed in the last seven days:

sudo find /etc /usr/bin /usr/sbin -type f -mtime -7 -ls 2>/dev/null | head -50

Package updates also change files, so compare the dates with your last update. Also look for programs hiding in the temporary folders:

ls -la /tmp /var/tmp /dev/shm

10. System programs that were swapped out

The package manager knows what its files should look like.

On Debian and Ubuntu:

sudo apt install -y debsums
sudo debsums -s

On Rocky and AlmaLinux:

sudo rpm -Va | grep '^..5'

Config files you edited yourself will show up. A changed ls, ps, ss or sshd is a very bad sign.

If you find something real

Don't try to clean it up. Once someone has had root, you can't be sure you found everything.

  1. Take a snapshot at your provider, so you have evidence.
  2. Build a new server from a clean image.
  3. Restore your data from a backup made before the break-in, and look through it before you trust it.
  4. Replace every key, password and token that was ever on the old server.
  5. Harden the new server before you open it to the internet. I wrote a checklist for that: How to harden a new Ubuntu VPS without locking yourself out.

The short version

  • Logins: last, lastb, and the SSH log
  • Keys and users: authorized_keys, UID 0, sudo group
  • Activity: top CPU processes, ports, live connections
  • Persistence: cron, timers, enabled services
  • Tampering: recent changes, /tmp, package checks

Doing this from a phone

I built VpsPilot, an Android app for managing servers over SSH, for moments like this, when you're away from a laptop. It has a "Was I hacked?" check that runs this kind of review and shows every command before it runs. It's a paid app with a 14-day free trial, then $2.99 a month or $19.99 a year.

Everything above works without any app. If you check something I left out, tell me in the comments.

I used an AI assistant to help edit this post. The app and the mistakes are mine.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.