I currently have a Ubuntu Server 12.04 running OpenSSH along with Samba and a few other services. At the current time I have public key authentication set up, and I'm wondering if it's possible to set up two factor authentication? I've been looking at Google Authenticator which I currently use with my Gmail account.

I've found a PAM module that looks like it will be compatible however it seems that you are forced to use a password and the code generated.

I'm wondering if there is a way to use the Google Authenticator Application (or something similar) along with my public key to authenticate into my SSH server?

  • Most of the comments seem to be bug reports mentioning that it's impossible to use PAM and public key authentication with OpenSSH. I've also found parts mentioning it's redundant as I'm using a pass phrase with my key. With all of the solutions seemingly only allowing Google Authenticator and a password not a public key. I could be missing it completely, but I just don't see how to implement both. Jul 20, 2012 at 3:38
  • Not sure why this has a -1, this is a very interesting question and I too would like to know the answer (not that I'm likely to use it, but even so, good to stash in the knowledge banks) Jul 20, 2012 at 3:45
  • @Pierre Are you trying to require both public key authentication, and a Google OTP?
    – mgorven
    Jul 20, 2012 at 4:24
  • @mgorven Yes, I was trying to set up both public key and Google OTP. I've heard some people say it's sufficient as having a passphrase on the key counts as two factor but I'm concerned about malware stealing the unencrypted key from memory. I'd rather have two completely separate devices used for authentication, I am slightly paranoid. Jul 20, 2012 at 20:03
  • This is intended to become officially implemented in 6.2: bugzilla.mindrot.org/show_bug.cgi?id=983#c59 Feb 8, 2013 at 17:20

7 Answers 7


Red Hat have added a patch to OpenSSH in RHEL (and therefore CentOS) 6.3 to require multiple authentication mechanisms, so you can do something like this:

RequiredAuthentications2 publickey,keyboard-interactive

See the release notes for not much more detail.

Unfortunately this feature doesn't seem to be in OpenSSH upstream nor Ubuntu 12.04, so unless you want to find the patch and recompile OpenSSH I'm afraid you're out of luck.

  • I have to say I appreciate the effort you put into finding me an answer, I certainly tried going over a few pages of Google results but all implied what you mentioned I had to use a password and a OTP only. I will probably create a CentOS VM to play around with the feature. Jul 20, 2012 at 23:02
  • @Pierre Not that much effort, I knew about this feature before ;-)
    – mgorven
    Jul 20, 2012 at 23:13
  • I've found the corresponding bug and some further notes. The bug includes the patch as an attachment. Jan 18, 2013 at 19:15
  • And here's an upstream openssh bug. It looks like similiar functionality will be upstream in openssh soon. Jan 18, 2013 at 19:20
  • openssh 6.2 has landed in the development release of Ubuntu, so save for any disasters this support will be in the expected 13.10 release. It uses upstream's AuthenticationMethods to specify multiple required methods, so that you can require both an ssh key and PAM, with PAM doing the Google Authenticator end. Jul 31, 2013 at 9:39

You are looking for Duo Security

  • 1
    This. Yes. I love this thing!
    – LVLAaron
    Jul 26, 2012 at 23:15
  • Definitely - Duo is easy to set up for Unix/Linux (link in answer), OpenVPN (duosecurity.com/docs/openvpn_as), or any OATH TOTP based two-factor service, or LastPass password management. Any service that's compatible with Google Authenticator (which uses TOTP) can be used with Duo's mobile app, or a hardware token supporting TOTP.
    – RichVel
    Aug 23, 2014 at 7:15

You can use both the Google Authenticator PAM module and public keys, but only one at the time will be used for a given authentication. That is, if a user logs in with an authorized public key, no token will be required.

Or, to say it otherwise: tokens are only required for password authentications, not SSH keys.

This limitation does not come from the Google Authenticator module by the way, but from SSH, which only implements two factor authentication (via ChallengeResponseAuthentication) for PAM, but doesn't call PAM when a valid public key is provided.

  • This was correct, but has now changed. openssh 6.2 adds an AuthenticationMethods configuration paramaeter that can flip this. Now you can require both. Jul 31, 2013 at 9:41

This question is from 2012. Since, SSH has changed and the SSH2 protocol has been implemented.

On more recent versions of SSH ( >= 6.2), man sshd_config mentions:

       Specifies the authentication methods that must be successfully completed for a user to be
       granted access.  This option must be followed by one or more comma-separated lists of
       authentication method names.  Successful authentication requires completion of every method
       in at least one of these lists.

       For example, an argument of ``publickey,password publickey,keyboard-interactive'' would
       require the user to complete public key authentication, followed by either password or key-
       board interactive authentication.  Only methods that are next in one or more lists are
       offered at each stage, so for this example, it would not be possible to attempt password or
       keyboard-interactive authentication before public key.

       This option is only available for SSH protocol 2 and will yield a fatal error if enabled if
       protocol 1 is also enabled.  Note that each authentication method listed should also be
       explicitly enabled in the configuration.  The default is not to require multiple authentica-
       tion; successful completion of a single authentication method is sufficient.

This page http://lwn.net/Articles/544640/ also mentions the possibility of using a publickey and a PAM authentication at the same time.


I know this question is a little stale, but for the sake of future people (myself included) who are looking for a solution, there's also talk of using the ForceCommand option in the sshd_config file to run a script which then performs the authentication. There's an example script here you can modify a bit to your needs, although in that example he calls it from the authorized_keys file instead of making it system-wide with sshd_config's ForceCommand.


Get a YubiKey and follow this guide http://berrange.com/posts/2011/12/18/multi-factor-ssh-authentication-using-yubikey-and-ssh-public-keys-together/

AFAIK, this is the best to way to implement Yubikey on your server for SSH access. The above guide enables you go use public-key + yubikey whereas if you go with the official guide (http://code.google.com/p/yubico-pam/wiki/YubikeyAndSSHViaPAM), it doesn't work with public-key.

Regards, Vip


If you set a passphrase on your private key then you have two-factor authentication already. In order to log in people would need:

  1. something you have - your private key
  2. something you know - the passphrase to your private key
  • 3
    Two passwords do not make two-factor authentication. The key itself is not a valid "something you have" because it's not a thing, just a piece of information. The thing must be non-copiable, or at least non-trivially copiable, to be any use as an authentication factor.
    – MadHatter
    Jul 20, 2012 at 15:41
  • 2
    I disagree entirely. If keys and certificates are not "something you have" then what are they? They are definitely not "something you know" (are you in the habit of learning off your SSH key?), and absolutely not "something you are". Those are the only three choices, so by a process of elimination, they most certainly are "something you have".
    – Bart B
    Jul 23, 2012 at 13:20
  • 1
    I agree; for me, the problem is in the maxim (something you have|know|are). The idea of two-factor authentication is to require members of two classes with different properties; something you know is easy to carry, but it can be known by someone else as the same time as you know it; so we add a second, different factor. The "something you have" is only different if it's uncopiable (or hard to copy). Otherwise, sure, it's not something you know, but it's not qualitatively different from something you know, because someone else can have it at the same time as you.
    – MadHatter
    Jul 23, 2012 at 13:34
  • 2
    I definitely agree that a password-protected keypair is a big improvement in security over straight username+password. I don't, sadly, agree that such a pair counts as two-factor, and we'll probably have to agree to disagree on that.
    – MadHatter
    Jul 25, 2012 at 7:28
  • 3
    If you have a passphrase-encrypted private key, you only prove one thing to the server: that your client knows the private key. It doesn't prove to the server that you know your passphrase; the server doesn't even know about the existence of your passphrase. Therefore it is only "something you know" (since your client knows it) and only one factor. If an attacker gets hold of just one thing (your private key) then you are compromised. That's one factor. Arguing that your passphrase and private key count as two factors is just an exercise in sophistry. What counts is what happens on the wire. Jan 18, 2013 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .