I was connected to an ubuntu machine and instead of typing exit to logout, I killed the ssh process by its PID:

kill PID

Now, when I try to connect again to the ubuntu machine, I get the following error message:

Connection closed by xx.xxx.xxx.xx port 22

Where the x represent the machine's address. Did I do something wrong? Why can't I connect to the machine anymore? Note that I am writing a bash script that connects to the server, runs some commands and then terminates the connection to the server, so that's why I killed the ssh process instead of manually logging out.

PS: I'm using a Mac.


Any particular reason you don't run "exit" command instead? That will close your ssh session gracefully.


ssh user@remotehost

You could also execute command like this: ssh user@remotehost "ls -l"

This command will effectively list the contents of user's home directory on the remote host, return the output to your console and exit automatically.

"ls -l" - is used as an example of the command you want to execute on the remote host.

The message you are getting make sense, as it looks like you are killing SSHD process/service on the machine you were connected to. As a result of your actions, SSHD process is not running, thus port TCP22 is not listening on your remote machine.

  • I'm not using the exit command because I've read in multiple places people saying that simply using an exit command like that doesn't work as intended. Instead you have to do something like ssh remotehost >> EOF exit EOF, or alternatively, a command like the one from your second example code. Additionally, I can't do that because I need to connect to the server to do a port-forward so that other commands (locally - in my computer) can run, so I would be pointless to connect to the server and exit right away, I need to wait for the local commands to finish executing and then exit. – ARR Feb 20 '19 at 15:31
  • Lastly, I didn't kill anything on the remote machine, I killed the (local) command that connected to the server (ssh remotehost). But say it was the case that I killed it remotely, how can I fix it? – ARR Feb 20 '19 at 15:33
  • 1
    @ARR What on earth did you read that possibly said anything so bizarre? exit is the way you are supposed to exit, and if it doesn't work, then there is a problem to be fixed by the admin. Not killing ssh, that doesn't fix any problem. – Michael Hampton Feb 20 '19 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.