After connecting succesfully to AWS on the latest iMac through Bash Script 1:

chmod 400 ~/PEM.pem

ssh -i "~/PEM.pem" [email protected]

jupyter notebook --no-browser --port=8888

Then opening a new bash script and entering in Bash Script 2:

ssh -i "~/PEM.pem" -L 8000:localhost:8888 [email protected]

Both the Ec2 commands and Python script (on localhost:8000 opened in Chrome) all work fine for about 30 minutes, then all of a sudden I get the error on the second bash script:

channel 3: open failed: connect failed: Connection refused

This message then repeats when I try to reconnect to localhost:8000 in Chrome, which I am not able to do. I've tried replacing localhost with, but have not been succesful.

Would anyone understand what the problem is, and how to possibly fix this? The objective is to use Python through EC2, which I have done successfully many times on other computers, but am now having problems.

  • 1
    Once the tunnel fails, are there any problems creating a new tunnel? Are there any services that need to be restarted on the server? The error message may need some context... and some information from the server's side. Dec 24, 2018 at 14:13
  • When the tunnel fails, I can connect again with ssh, but then as soon as I go to localhost in the browser, the error appears again. Dec 24, 2018 at 14:36
  • Maybe the problem stems because an ssh tunnel is not the ideal solution for that. Dec 24, 2018 at 16:18
  • 1
    Try -o ServerAliveInterval=10 Dec 24, 2018 at 16:36
  • autossh? Jan 18, 2019 at 14:21

1 Answer 1


Instead of chmod you could use chown and prodive the user read access to the file:

sudo chown user:user ~/certs/mycert.pem

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .