Sometimes get error "port 22: Connection refused"

608 views
Skip to first unread message

Faraz Hussain

unread,
Nov 14, 2017, 11:08:58 PM11/14/17
to gce-discussion
I sometime get this error when attempting to ssh into my instance right after starting it. So I generally wait 30 seconds after the instance has started before trying to ssh into it. Do I need to wait 30 seconds? I wonder if there is a more elegant way to know when I can ssh in.

 Here is what my bash script looks like:

echo "Starting gcloud instance"
gcloud compute instances start instance-3
sleep 30
ip=$(gcloud compute instances list | grep instance-3 | awk {'print$(NF-1)'})
ssh_command="ssh -i /home/user/.ssh/id_rsa_google $ip "
$ssh_command "mkdir $jobdir"


Digil (Google Cloud Platform Support)

unread,
Nov 15, 2017, 4:57:00 PM11/15/17
to gce-dis...@googlegroups.com
You can SSH into the machine soon after creating it. There isn't any time delay or waiting period for this.

Have you tried SSH into the machine using the browser? If not, try it and check for any errors. 

Usually "port 22: connection refused" errors occur when there is a port block in the local firewall or the appropriate service (sshd) is not running. But in your case, you are able to SSH after a certain period of time (30 seconds). So your local firewall will be fine. Also in your bash script, I see a line "sleep 30". Disable this line and run the script again. This might lift you from 30 seconds waiting period.  

Let me know if that helps you.

Faraz Hussain

unread,
Nov 15, 2017, 5:45:15 PM11/15/17
to gce-discussion
Thanks for looking into it. I am glad that there isn't any time delay or waiting period. I put the "sleep 30" in the bash script as a workaround to the occasional "connection refused". I'll keep an eye out if it happens again and see if it is reproducible. Might just be some random glitch.


On Wednesday, November 15, 2017 at 4:57:00 PM UTC-5, Digil (Google Cloud Platform Support) wrote:
You can SSH into the machine soon after creating it. There isn't any time delay or waiting period for this.

Have you tried SSH into the machine using the browser? If not, try it and check for any errors. 

Usually "port 22: connection refused" errors occur when there is a port block in the local firewall. But in your case, you are able to SSH after a certain period of time (30 seconds). So your local firewall will be fine. Also in your bash script, I see a line "sleep 30". Disable this line and run the script again. This might lift you from 30 seconds waiting period.  

Digil (Google Cloud Platform Support)

unread,
Nov 16, 2017, 12:04:33 PM11/16/17
to gce-discussion
You can also try interacting with the Serial Console and see for any errors logged there.
Reply all
Reply to author
Forward
0 new messages