
防止したい相互の作用そして非インタラクティブ条件付きロジックに基づく ssh ログイン。ユーザー名をテストしてエラー メッセージを表示する必要があります。
sftp (非対話型 ssh) を使用する人も、運テストを受ける必要があります。
それをどのように実装するのでしょうか? システムを完全に制御できます。
sshd ForceCommandを使用しようとしましたが、https://stackoverflow.com/a/33714333/746461nottyでは機能しません。
私は PAM に精通していないので、ログインが対話型の場合に PAM がカスタム エラー メッセージを出力できるかどうか疑問です。
https://linuxhint.com/understanding_bash_shell_configuration_startup/オプション付きの非対話型ログイン シェルは--noprofile
すべての bash 構成ファイルをバイパスできると書かれています。そのため、そこにロジックを記述することはできません。
答え1
分かりました。それを実現するには PAM モジュールを実装できます。
以下を、たとえばファイル に保存します/root/checkConnections
。
#! /bin/bash
limit=$1
c=$(pgrep -xcu $PAM_USER sshd)
if [[ $c -ge "$limit" ]]; then
echo "Max $limit ssh connections allowed, but you have $c."
exit 1
fi
次に/etc/pam.d/sshdに以下を追加します。
session required pam_exec.so stdout /root/checkConnections 5
pam_exec.so
シェル スクリプトを実行し、stdout
ユーザー terimal にメッセージを出力します。
条件が失敗した場合の効果は次のようになります。
$ ssh localhost
Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-74-generic x86_64)
94 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Max 5 ssh connections allowed, but you have 5.
/root/checkConnections failed: exit code 1
Last login: Thu Jun 24 12:19:27 2021 from 172.18.33.67
Connection to localhost closed.