SSH restart and kill instances?

Solution 1:

Invoking the init.d script should still restart the service:

dermot@porkboy:~$ sudo /etc/init.d/ssh restart
[sudo] password for dermot:
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service ssh restart

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) and then start(8) utilities,
e.g. stop ssh ; start ssh. The restart(8) utility is also available.
ssh stop/waiting
ssh start/running, process 4877
dermot@porkboy:~$

'service ssh restart' works fine here (11.04). It's worth noting that restarting sshd won't kill existing SSH sessions. When you log into a box via SSH, sshd spawns new processes to handle the session. Restarting sshd will kill the main sshd daemon process (and start it again, obviously) but leave other spawned instances of sshd untouched. You want this behaviour because it makes life a lot easier when you're working with headless servers in distant datacenters!

Now, to answer the rest of your question. Instead of running 'ps -A', try this:

dermot@porkboy:~$ ps -ef | grep ssh
root      2522     1  0 Aug29 ?        00:00:00 sshd: dermot [priv]
dermot    2615  2522  0 Aug29 ?        00:00:04 sshd: dermot@pts/0
root      4655     1  0 10:52 ?        00:00:00 sshd: dermot [priv]
dermot    4756  4655  0 10:52 ?        00:00:00 sshd: dermot@pts/1
root      4887     1  0 10:55 ?        00:00:00 /usr/sbin/sshd -D

This probably accounts for the three sshd processes you're seeing - one for the main sshd daemon and then two (root parent, dermot child) per session. I'm SSHed in from two locations o I have five processes. The pts/X bit relates to the virtual terminal that the session is attached so...

dermot@porkboy:~$ who
dermot   pts/0        2011-08-29 21:32 (williams-mb.local)
dermot   pts/1        2011-08-30 10:52 (192.168.253.109)

... gives us some idea which session is which. So if I wanted to kill the session from my MacBook I'd 'kill -9 2522'.

Solution 2:

When you restart SSH daemon with

sudo /etc/init.d/ssh restart

or

service ssh restart

the listening daemon restarts with new configuration options (I assume, you restart it for the new config take effect). All sessions already open remain running with old configuration. To learn, which of ssh sessions is yours, try to execute:

ps -ef | egrep '(ssh|PID)'

You will get one /usr/sbin/sshd with PPID 1 and UID root. This is the listening daemon. All other sshd: user@pts/0 records are user sessions. Look for appropriate session by username and kill the process to terminate this session.

I would recomend to do all this in GNU screen session, so if you make a mistake, you will be able to reconnect and reattach this screen session.

Or I didn't understand the question right.