How to stop/kill an autossh tunnel?

17

4

Now that I have a autossh tunnel, what is correct way to stop it?

There seems to be no easy way to do this. Documentation is unclear on this part.

The easy way seems to be to reboot the machine? Is this correct?

onknows

Posted 2015-12-02T10:03:14.123

Reputation: 475

Answers

21

No, the proper way to kill autossh is simply to kill the process autossh, nothing else.

The reason is

# file $(which autossh)
/usr/bin/autossh: POSIX shell script, ASCII text executable

that autossh is simply a shell script, not a service. It starts a new program, in its very last line,

exec /usr/lib/autossh/autossh "$@"

again not a service. As for exec (you can double-check it in the wiki of the Bash hackers), it is a shell-builtin command which replaces the current shell with the following command (/usr/lib/autossh/autossh "$@" in this case) without starting a new process. So, the only way to stop autossh is to kill the calling script, for instance

pkill -3 autossh

(thanks to dviljoen for pointing out the importance of using the -3 flag, see below). Incidentally, killing the ssh connection will not work, because the calling command (i.e., the one above) will simply start a new connection as soon as it realizes the old one has been dropped.

MariusMatutiae

Posted 2015-12-02T10:03:14.123

Reputation: 41 321

1Actually you should kill autossh without the -9. If a connection has already been made through the tunnel, it will have spawned a child sshd process to handle it. Killing the parent autossh with -9 will leave the sshd process behind. The tunnel will still operate until the sshd process times out. If you use the default signal (-3) on the kill it will gracefully shutdown and take the spawned sshd process with it. – dviljoen – 2017-03-24T14:17:14.960

hi as the -3 is the default kill signal, you don't actually need to explicitly pass -3, you can omit it – Andrés Alcarraz – 2018-08-01T12:03:55.517

@AndrésAlcarraz You really sure about it? SIGTERM is the default, and that one is 15. SIGQUIT is 3, see https://superuser.com/questions/352147/what-does-kill-3-mean and https://en.wikipedia.org/wiki/Kill_(command) -- Also, FWIW, I just checked and -15 doesn't shut it down, so SIGTERM cannot be used.

– Daniel F – 2018-09-19T20:55:45.903

And using pkill with no signal (= the default) also doesn't terminate autossh. – Daniel F – 2018-09-19T21:01:18.647

@DanielF you're right, I was confused y the "default signal (-3)" from dvijoen comment – Andrés Alcarraz – 2018-09-19T21:03:44.257

@AndrésAlcarraz I need to correct my statement. SIGTERM == 15, the default Signal, does in fact shut down autossh properly. My connection was not established, as autossh was waiting for the password (I thought I had added the public key...). When attempting to shut it down with -3 while in this unconnected state, autossh and ssh got terminated, but when the connection was actually establised, -3 only terminated autossh but left ssh connected. SIGTERM on the other hand closed them both. But SIGTERM did not close it properly when the connection was not fully established – Daniel F – 2018-09-19T22:21:00.473

Wait, actually, ssh never got spawned properly, it was not visible in htop as a child, when autossh was waiting for ssh to "enter it's password"... I need a break... – Daniel F – 2018-09-19T22:24:37.107

Well I just tried plain old killall autossh and it killed autossh and all ssh spawned processes, also I think killall is more suitable than pkill since you know the exact name of the program you want to terminate. – Andrés Alcarraz – 2018-09-20T11:08:46.813

5

run auto ssh with:

AUTOSSH_PIDFILE=/var/run/tunnel.pid autossh

kill it with:

kill pid

BTW

pkill -9 autossh is a wrong

-9 makes sure the process not exiting gracefully, so ssh process is still there when the autossh process is killed

without -9 is still bad, if you have multiple tunnels running, pkill will kill them all

correct way is to set AUTOSSH_PIDFILE env var then kill that pid only

Erik

Posted 2015-12-02T10:03:14.123

Reputation: 51

Welcome to Super User. It appears your final paragraph is your actual answer, while the remainder of your question is a comment on another answer. Given the Q&A nature of this site, It would helpful to add enough detail to your answer so it can stand on its own. Thanks for contributing and don't forget to check out the [tour].

– I say Reinstate Monica – 2017-12-16T02:25:27.203

4

Search for the process:

ps aux | grep ssh

The secon column is the PIDnumber

Kill process by PID :)

kill -9 PIDnumber

Use sudo if you dont have root privileges.

NIZ

Posted 2015-12-02T10:03:14.123

Reputation: 317

1

I know this has been answered, but contrary to the comments above, using pkill -3 autossh does NOT kill the sshd child processes for me.

I use this function in my .bashrc file.
Bascially, it's like adding a --kill argument to autossh.

  if [ "$1" = "--kill" ]; then
    ps aux | 
    grep -P "(/usr/bin/ssh|/usr/lib/autossh/autossh)\s.*$2" |
    awk '{print $2}' |
    xargs -r kill
  else
    $(which autossh) "$@"
    echo "" # prevents line wrapping when you kill the ssh process
  fi

You can run which ssh and which autossh to check the paths on your system.

As long as the first argument isn't --kill, it just passes the args along to autossh.

This script kills the autossh & ssh instances. This is important if you are using port forwarding because, killing ONLY the autossh instance doesn't kill the tunnel, it just prevents it from reconnecting if/when it finally does disconnect.

You can also specify a search term (host name) to kill only specific tunnels.

autossh --kill dbserver1 kills just the connections to dbserver1
autossh --kill dbserver will kill dbserver1, dbserver2, etc.
autossh --kill dbserver will kill ALL autossh connections

To clarify, it SHOULD kill only the SSH sessions started by autossh.

If you run ps aux | grep ssh while you have both autossh and ssh sessions running, you will see that the ones started by autossh use the full path (/usr/bin/ssh and /usr/lib/autossh/autossh).
This script only matches results to processes started with the speicifc path. I did this because I (and I assume most people) usually type ssh and not the full path, which keeps it from killing my normal ssh sessions.

Hope this help others.

KeithMcFly

Posted 2015-12-02T10:03:14.123

Reputation: 31

any particular reason for $(which autossh) instead of simply autossh? – MestreLion – 2019-06-21T07:36:09.123