SSH Connection refused after multiple previous successful connections

I am running a Wordpress One-Click install and I am using GitLab CI/CD to deploy to my droplet. My script is successfully connecting via SSH creating some folders, then using SCP to copy some files to the server successfully, then after a few SSH connections the droplet refuses the SSH connection and the job fails. Here is a chunk of the log from GitLab:

$ ssh-add <(echo "$SSH_PRIVATE_KEY")
Identity added: /dev/fd/63 (rsa w/o comment)
$ ssh "$PROD_SERVER_USER"@"$PROD_SERVER_ADDRESS" "mkdir -p \"$PROD_DIRECTORY\"/_tmp && mkdir -p \"$PROD_DIRECTORY\"/_old && mkdir -p \"$PROD_DIRECTORY\"/build"
Warning: Permanently added '#######' (ECDSA) to the list of known hosts.
ssh: connect to host ####### port 22: Connection refused
ERROR: Job failed: exit code 1

Does anyone know why it is suddenly refusing to connect?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Do you have any rate limiting happening on port 22? Try this:

iptables -nL|grep "22\|ssh"


Thank you for this! I was stumped on it for ages. My script was doing the same thing - ssh-ing multiple times to execute various commands but there was no indication in the error message that rate limiting was the cause.

One way to solve it without turning off rate limiting is move all your commands into a bash script and then execute like this:

ssh user@droplet 'bash -s' < ./ 

Hey there yea I did! UFW was rate limiting port 22 so I just turned it off and it works perfectly now, thanks!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel