2

Under certain conditions, I want the sshd daemon to be stopped. When I do this, I want the currently open connections to be stopped too. By default, the sshd service does not kill existing connections when it stops.

Is this configurable? Can I setup sshd so that whenever it is stopped, open connections will be stopped too?

What are the alternatives? Simply killall sshd?

1

Indeed, sshd service does not close active sessions while shutting down.

With a killall sshd, you would be shutting down your own sshd server as well, which is no big deal if it is already stopped, though may be risky, working from a remote station.

To avoid this, I would first locate PIDs for sshd instances bound to a client:

# who am i
root     pts/0        2019-10-25 13:52 (1.2.3.4)
# ps fxww|grep pts/
12144 ?        Ss     0:00  \_ sshd: root@pts/0
12150 pts/0    Ss     0:00  |   \_ -bash
12205 pts/0    R+     0:00  |       \_ ps fxww
12206 pts/0    S+     0:00  |       \_ grep pts/
12169 ?        Ss     0:00  \_ sshd: root@pts/1
12175 pts/1    Ss+    0:00      \_ -bash

Now I know I can kill 12144 or 12169 closing existing sessions. COnsidering that my session is attached to pts/0, I would probably avoid killing 12144.

To automate this:

exclude=`who am i | awk '{print $2}'`
ps axww | grep -v "$exclude" \
    | awk '/sshd: [^ ]*@pts/{print $1}' \
    | while read pid;
        do
            kill $pid
        done
| improve this answer | |

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.