Your VPS Is Sending Spam? How to Find the Source and Stop It
Mail starts bouncing, your inbox fills with delivery failure notices for messages you never sent, or a customer tells you your emails land in spam. When that happens, you need to find spam source on VPS quickly, because every hour the leak runs, your IP reputation gets worse. This guide walks through the checks in the order that finds the cause fastest: the mail queue, the logs, the open relay test, then the scripts and settings that let spam out.
Signs Your VPS Is Sending Spam
Spam rarely announces itself. It shows up as side effects:
- Mail queue size jumps from a handful of messages to hundreds or thousands.
- CPU or bandwidth use rises at night when site traffic is low.
- Your provider warns you about abuse, or throttles port 25.
- Legit mail starts bouncing with messages about blacklists or poor reputation.
- Log files grow fast because the mail server writes an entry for every message it tries to send.
One or two of these signs can have a harmless cause, such as a newsletter send. Several at once point to a leak or a break-in. If you also see new user accounts or logins from places you do not recognise, check the warning signs of a compromised server first.
How to Find Spam Source on VPS: Start With the Mail Queue
The mail queue is the fastest place to learn what your server is sending. A postfix mail queue check takes seconds and shows the sender, the recipient, and how many messages are waiting.
postqueue -p
postqueue -p | tail -20
Look at the sender addresses first. If the queue is full of mail from addresses you do not use, the sender tells you where to look next. If it is full of mail to addresses you do not know, your server is sending outward to strangers, which is the classic spam pattern. Do not flush the queue yet. The queued messages are evidence, so read a few of them first.
Check Mail Logs to Trace Each Message
The queue shows what is waiting. The logs show what already went out and which account sent it. To find spam source on VPS from the logs, check mail logs on Linux in /var/log/mail.log on Debian and Ubuntu, or /var/log/maillog on RHEL-based systems.
tail -n 200 /var/log/mail.log
grep "status=sent" /var/log/mail.log | tail -30
Group sent mail by the login name that authenticated:
grep "sasl_username" /var/log/mail.log | awk '{print $NF}' | sort | uniq -c | sort -nr | head
If one account sent far more mail than the rest, you have a lead: either that password leaked, or a script is using those credentials. Change that password at once, then keep reading the logs to see if the sending stops.
A flood of mail from 127.0.0.1 points to a local web script, while a flood from one outside IP points to a remote login or an open relay. Read one queued message with postcat -q MESSAGE_ID before you delete anything: its headers often name the script or form that created it. Only then clear the queue with postsuper -d ALL, and only if no real mail is waiting in it.
Run an Open Relay Test
An open relay lets anyone on the internet send mail through your server without a password. Spammers scan for them all the time, and one open relay can fill your queue in minutes. An open relay test checks whether your server accepts mail for outside addresses from an unauthenticated sender.
From a different machine, not from the VPS itself, connect to port 25 and try to send mail to an outside address:
telnet your-server-ip 25
HELO test.example.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
If the server answers the RCPT TO line with a 250 code, it accepted mail for a stranger, and your relay is open. A correct setup answers with a relay access denied error instead. You can also use an online relay test tool, which runs the same check from its own network.
If the relay is open, fix it in /etc/postfix/main.cf. Make sure smtpd_relay_restrictions ends with reject_unauth_destination and that mynetworks holds only your own server addresses, never 0.0.0.0/0. Then reload Postfix and run the test again:
sudo postfix check
sudo systemctl reload postfix
Look for a Hacked Script or Abused Web Form
When the relay is closed and the logs still show local mail, the sender is usually a script on your own server. Common sources include a contact form with no captcha or rate limit, an outdated WordPress plugin, or a PHP script left in a temp folder.
Find recently changed PHP files, and files that call the mail function:
find /var/www -name "*.php" -mtime -7 -ls
grep -r "mail(" /var/www --include="*.php" -l | head -20
A file you do not recognise, or a known file changed at a time nobody worked on the site, deserves a close look. Check your web server access logs around the same time for requests to that file from a single IP.
If you suspect malware rather than a single bad script, run a scan before you start deleting files by hand. Our guide to scan a Linux server for malware with ClamAV and rkhunter covers the setup and what to do with the results.
Check SPF, DKIM, and DMARC
SPF, DKIM, and DMARC do not stop your server from sending spam, but they decide how other servers treat your mail, and they stop strangers from forging your domain.
dig TXT yourdomain.com +short
dig TXT _dmarc.yourdomain.com +short
A sound SPF record lists only the servers that send mail for you, and ends with a firm policy such as -all. If your SPF record includes a provider you no longer use, remove it. Set up DKIM signing so each message carries a signature receivers can check. Add a DMARC record in monitoring mode, such as v=DMARC1; p=none; rua=mailto:[email protected], so you get reports about who sends mail as your domain before you move to a stricter policy.
The SPF DKIM DMARC trio works as a set, so test each change by sending a message to a mailbox you control and checking the authentication headers.
Block the Leak: Firewall, Rate Limits, and Fail2ban
Add rate limits so one account or one script cannot flood the queue again. In Postfix, set a per-client message limit in main.cf:
smtpd_client_message_rate_limit = 50
anvil_rate_time_unit = 60s
That example caps each client at 50 messages per minute. Pick a number above your real peak, not below it, or normal mail will bounce. Reload Postfix after the change.
For repeated login abuse, Fail2ban watches your logs and blocks IPs that fail authentication too often. If you have not set it up yet, follow our Fail2ban setup guide for SSH brute force attacks and add a jail for your mail service as well.
Clean Up and Get Your IP Off Blocklists
After the sending stops, check whether your IP ended up on a blocklist. Search your IP on a blocklist lookup site and note which lists flag it. Each list has its own removal page and rules. Some remove an IP automatically once the spam stops. Others need a manual request and proof that you fixed the cause.
Set a reverse DNS (PTR) record that matches your mail hostname. Our reverse DNS PTR guide for a VPS walks through the steps at common providers. Make sure the hostname resolves forward and backward to the same IP, remove leftover spam from the queue, and ask your provider to lift any port 25 block they placed during the incident.
Fix the cause, request removal where the list asks for it, and let the reputation recover as clean mail goes out.
Stop It From Happening Again
Spam on a VPS is rarely a one-time accident. It comes back through the same gaps: a weak password, an old plugin, a form with no limit, or a config copied from a tutorial without reading it.
Update your web apps and plugins on a schedule, not only when something breaks. Give each site its own mail credentials, so one leaked password cannot send mail as your whole server. Turn on automatic security updates. Review the mail queue size and the top senders once a week, which takes about a minute and catches a new leak before it grows. Add that check to a regular routine, such as the one in our monthly server security audit checklist, so it survives busy weeks.
To find spam source on VPS the next time it happens, start in the same place: queue, logs, relay test, then scripts and records. Fix the sender first, close the hole second, and clean up your reputation last.