Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have set up google two factor authentication on my local machine. So when I do :

ssh localhost

I need to verify the code along with password. But when I try to connect to my Ec2 instance like so :

ssh -i mykey.pem ubuntu@x.x.x.x.x

It just asks for the password (here is no second factor). How could I provide the password along with the verify code that I see in my google app.

Thanks in advance.

share|improve this question
up vote 0 down vote accepted

In general, if you are using key-based authentication (the -i mykey.pem in your above example), ssh will bypass the PAM stack and authenticate you using only your key. If you're being prompted for a password it is probably for the key passphrase.

If you were to connect without using a key:

ssh ubuntu@x.x.x.x

The behavior should be the same as with your initial ssh localhost example.

Based on your questions, you may want to spend a little time reading some documentation. That link is explicitly about Amazon EC2 instances, but there are lots of other guides out there to setting up authentication with google-authenticator.

share|improve this answer
I have tried that, but it doesn't seem to be working either. when I do ssh ubuntu@x.x.x.x I get Permission denied (publickey).! – batman Aug 16 '12 at 12:38
Is your local ssh configured to not use password authentication? What if you run ssh -o PasswordAuthentication=yes ubuntu@x.x.x.x? – larsks Aug 16 '12 at 12:43
That is because you need to use the keyfile. And you do already have some kind of two factor auth, as the keyfile is one - password is two. – Frederik Nielsen Aug 16 '12 at 12:44
If I run ssh -o PasswordAuthentication=yes ubuntu@x.x.x.x I get the same : Permission denied (publickey). – batman Aug 16 '12 at 12:46
AFAIK by default the EC2 Ubuntu AMIs are configured for key based authentication only. – James Yale Aug 16 '12 at 12:48

When you setup 2 factor auth in this way it is a server side process, not client side. You've setup the SSH server component on your local machine to do 2 factor, which is why you're prompted for the additional credentials.

As the 2 factor auth doesn't depend on the client (that would add no security) you need to setup the SSH server on your EC2 instance to do 2 factor as well.

share|improve this answer
Alright, if I do this way(having installed the google authenticator in ec2 instance too): ssh ubuntu@x.x.x.x, its asking for the password(after verification code). What password I need to give over here? I just downloaded my key without any passwords. – batman Aug 16 '12 at 12:48
Are you sure you didn't specify a password when you created the key pair that you're using? – James Yale Aug 16 '12 at 12:50
Yes I'm sure. I just gave a name and downloaded it! – batman Aug 16 '12 at 12:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.