[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2118
  • Last Modified:

Remote logoff of same user - Debian

It happens somtimes that I logon as root via ssh and for different reasons it happens sometimes that the session gets closed without the root user logging out. So after some time I will have several inactive root sessions logged in. I want to log those inactive root sessions off. I found out using 'killall -u root' was not a good idea. Any ideas?
  • 2
2 Solutions
In this case, I use the next commands:
- know what terminal you have now
ps xa | grep bash
- see what bash sessions active now, find pocess numbers
kill -KILL bash_proc_number
- kill bash, be careful to not kill session in which you work now
Maciej SsysadminCommented:
Do you have TCPKeepAlive option enabled in sshd_config (it should be enabled by default)? This should help disconnecting clients which are not responding (network is down, or client crashed).

You may also consider using ClientAliveInterval and ClientAliveCountMax instead of TCPKeepAlive.
Refer to man sshd_config for explanation of these options.

To use SergKz solution, you should check for bash sessions of root user (ps ax | grep -E "^root.*bash") - with ps ax you will get all instances - if there are some other users logged in, you will log them off with kill command.
You can use some oneliner to do this (create some alias for this command, or save this as some shell script).
kill `ps aux | grep -E "^root.*bash$" | grep -vw \`tty | sed 's,/dev/,,'\` | awk '{print $2}'`

However, I would rather use proper options in sshd configuration file instead of killing unused sessions manually.
itniflAuthor Commented:
Thanks! That covered it!
itniflAuthor Commented:
Thanks. Both comments were useful and addressed the issue.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now