
예방하고 싶다인터렉티브그리고비대화형조건부 논리를 기반으로 하는 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 모듈을 구현할 수 있습니다.
다음을 file 에 저장합니다 /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
사용자 터미널에 메시지를 출력합니다.
조건이 실패하면 효과는 다음과 같습니다.
$ 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.