The login prompt flashes on the screen. You type your password. It’s not enough.
Multi-Factor Authentication (MFA) in shell scripting is the next barrier. One factor can be stolen. Two or more make unauthorized access far harder. Implementing MFA in your scripts is simple if you know where to start, and vital if you care about security.
MFA combines something you know (password), something you have (token, device), and sometimes something you are (biometrics). In shell scripting, the most common approach is pairing passwords with time-based one-time passwords (TOTP) or an API-driven secondary check. The goal is to integrate these steps into automation without breaking workflows.
Start by securing password entry with read -s to keep it hidden in terminal history. For the second factor, use tools like oathtool for TOTP generation, or hit verification endpoints from providers such as Google Authenticator, Authy, or Okta via curl. Scripts can call these tools, compare the entered code against the server’s expected value, then decide to continue or exit.