LoginSignup
88

More than 5 years have passed since last update.

不要なユーザを強制ログアウトさせる

Posted at

bashでリモート環境へのセッションが残ってしまったなどの不要なユーザを強制的にログアウトさせるコマンドのメモ。

wコマンドでユーザを調べる

wコマンドでログアウトさせたいユーザを探す

$ w
 20:16:19 up 1 day,  5:45,  2 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
imajo    pts/0    6.41.102.121.dy. 20:09    6:47   0.00s  0.00s -bash
imajo    pts/1    6.41.102.121.dy. 20:10    0.00s  0.01s  0.00s w

pts/0をログアウトさせたい(pts/1のyimajoはwコマンドを使っている自分自身)。

sshdを使っているプロセスを表示

psコマンドからsshdを使ってるプロセスを表示

$ ps ax | grep sshd

出力は次のようになる

$ ps ax | grep sshd
26519 ?        Ss     0:00 /usr/sbin/sshd
27126 ?        Ss     0:00 sshd: yimajo [priv]
27128 ?        S      0:00 sshd: yimajo@pts/0
27148 ?        Ss     0:00 sshd: yimajo [priv]
27150 ?        S      0:00 sshd: yimajo@pts/1
27172 pts/1    S+     0:00 grep sshd

27128が終了させたいプロセス番号ですよと。

killでプロセスを終了させる

killコマンドで9(SIGKILL)を送信してプロセスを終了させる。
kill -9 <プロセスID>で指定するだけ

kill -9 27128

最後にもう一度wコマンドを打ってkillできたかを確認しておわり

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
88