I want to prevent interactive and non-interactive ssh login based on a conditional logic. I need to test the username and give an error message.
People using sftp (non-interactive ssh) should also be subject to the luckness test.
How would I implement that? I have full control of the system.
I tried to use sshd ForceCommand, but according to https://stackoverflow.com/a/33714333/746461 it does not work for notty.
I'm not familiar with PAM and I doubt if PAM can output custom error message in case the login is interactive.
https://linuxhint.com/understanding_bash_shell_configuration_startup/ says non-interactive login shell with --noprofile
option can bypass all bash config files. That's why I can't write my logic there.
I figured out. I can implement a PAM module to do that.
Save the following to, say, file
/root/checkConnections
.Then in /etc/pam.d/sshd, add
pam_exec.so
executes the shell script,stdout
outputs messages to user terimal.When the condition fails, the effect is like this.