I'd like to write an automation script that sets PasswordAuthentication no
in /etc/ssh/sshd_config
. However, I wan't to guard against locking myself out in case I run the script while being logged in via SSH and password authentication. Is there a way to determine the authentication method of my SSH connection when I'm already logged in?
Short answer, if you turn on a high enough logging level for ssh/sshd to report this detail before you begin the process, it should be trivial. If you haven't already done this it may be quite a bit harder.
In my experience, unless you have ssh or sshd running/logging with verbose (debugging level, maybe?) output, this probably isn't captured for each authentication or login event. You could have a script that checks the setting in the sshd_conf file for you... However, there is a small chance that the daemon you interacted with upon login was started manually with different options, or maybe the file was edited after the daemon was started, etc. There are lot's of "what if" scenarios, each more unlikely than the next. It really depends on the size and scope of your environment and your confidence in the integrity of the config files/systems, i.e. is this your own cluster of vpc nodes that you tinker and learn on, or is it a work/school/club VPC with many users and admins?