0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IDLE状態のセッションを自動で切断する

Posted at

自動で切断する方法と切断されないようにする方法。

sshd_config

Server: 一定時間IDLE状態の接続を自動で切断する

ClientAliveCountMaxを0(応答判定のパケットを投げない)にすればOK。

[vagrant@app01 ~]$ sudo grep ^Client /etc/ssh/sshd_config
ClientAliveInterval 30
ClientAliveCountMax 0
[vagrant@app01 ~]$ sudo systemctl reload sshd.service

確認。

Clientの表示
[vagrant@app00 ~]$ ssh 192.168.20.21
Last login: Thu Dec  5 15:35:15 2019 from 192.168.20.20
[vagrant@app01 ~]$ Connection to 192.168.20.21 closed by remote host.
Connection to 192.168.20.21 closed.
[vagrant@app00 ~]$
Server側でIDLE時間のモニター
[vagrant@app01 ~]$ while true ; do LANG=C w ; sleep 1 ; done
...
 15:38:08 up 46 min,  2 users,  load average: 0.05, 0.06, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0    10.0.2.2         15:08   32.00s  0.49s  0.23s sshd: vagrant [priv]
vagrant  pts/1    192.168.20.20    15:37   29.00s  0.02s  0.02s -bash
 15:38:09 up 47 min,  1 user,  load average: 0.05, 0.06, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0    10.0.2.2         15:08   33.00s  0.49s  0.23s sshd: vagrant [priv]
...

Client: IDLE状態が継続しても自動切断されないようにする

ServerAliveIntervalを設定する。
ClientAliveIntervalより短い時間を設定する必要がある。

~/.ssh/config
Host 192.168.*
    IdentityFile        ~/.ssh/id_ecdsa
    IdentitiesOnly      yes
    ServerAliveInterval 15

確認。

Clientの表示
[vagrant@app00 ~]$ ssh 192.168.20.21
Last login: Thu Dec  5 15:37:39 2019 from 192.168.20.20
[vagrant@app01 ~]$
// 切断されない
Server側でIDLE時間のモニター
[vagrant@app01 ~]$ while true ; do LANG=C w ; sleep 1 ; done
...
 15:51:35 up  1:00,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0    10.0.2.2         15:08   39.00s  0.52s  0.23s sshd: vagrant [priv]
vagrant  pts/1    192.168.20.20    15:51   29.00s  0.02s  0.02s -bash
 15:51:36 up  1:00,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0    10.0.2.2         15:08   40.00s  0.52s  0.23s sshd: vagrant [priv]
vagrant  pts/1    192.168.20.20    15:51   30.00s  0.02s  0.02s -bash
 15:51:37 up  1:00,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0    10.0.2.2         15:08   41.00s  0.52s  0.23s sshd: vagrant [priv]
vagrant  pts/1    192.168.20.20    15:51   31.00s  0.02s  0.02s -bash
...

bash_profile

Server: 一定時間操作がない場合に自動ログアウトする

TMOUTを設定する。

~/.bash_profile
export TMOUT=60

確認。

Clientの表示
[vagrant@app00 ~]$ ssh 192.168.20.21
Last login: Thu Dec  5 15:58:45 2019 from 10.0.2.2
[vagrant@app01 ~]$ timed out waiting for input: auto-logout
Connection to 192.168.20.21 closed.
Server側でIDLE時間のモニター
[vagrant@app01 ~]$ while true ; do LANG=C w ; sleep 1 ; done
...
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0    10.0.2.2         15:58    1:13   0.07s  0.00s w
vagrant  pts/1    192.168.20.20    15:59   59.00s  0.01s  0.01s -bash
 16:00:10 up  1:09,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
vagrant  pts/0    10.0.2.2         15:58    1:14   0.07s  0.00s w
...

Client: 自動ログアウトしないようにする

ログイン時にTMOUTを削除する。

Clientの表示
[vagrant@app00 ~]$ ssh 192.168.20.21
Last login: Thu Dec  5 16:09:28 2019 from 10.0.2.2
[vagrant@app01 ~]$ unset TMOUT
[vagrant@app01 ~]$ echo $TMOUT

[vagrant@app01 ~]$

Server: 自動ログアウトの設定を上書きさせない

readonlyにすればOK($HOME/.bash_profileの更新禁止は別途必要ですが)。

~/.bash_profile
export TMOUT=60
readonly TMOUT
Clientの表示
[vagrant@app00 ~]$ ssh 192.168.20.21
Last login: Thu Dec  5 15:59:10 2019 from 192.168.20.20
[vagrant@app01 ~]$ unset TMOUT
-bash: unset: TMOUT: cannot unset: readonly variable
[vagrant@app01 ~]$ echo $TMOUT
60
[vagrant@app01 ~]$
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?