LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-04-2015, 07:11 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
Unable to use one of two ssh keys to connect


Hi,

I am trying to connect from a Mac OS (Yosemite) to a Centos 6.6 through ssh.
I have two users that I connect to with two different pairs of keys (rsa).

For user1 I'm using ~/.ssh/id_rsa and for user2 I'm using ~/.ssh/id_user2

When I try to connect using user2's credentials, the user1's key takes precedence. I know this, because it prompts me for the mac os's user password to store the passphrase associated with the user1's private key and user2's private key doesn't have a passphrase.

I added both identities with the ssh-add -K, but I receive this error: Too many authentication failures for user2. Which means, of course, that it tries to use user1's credentials.

I even tried to add only the user2's private key with ssh-add -K, but it still tries to connect with ~/.ssh/id_rsa

Of course, I would like the keys to be used automatically depending on the user I'd like to connect through.

/etc/ssh_config contains
Code:
IdentityFile ~/.ssh/id_trans
IdentityFile ~/.ssh/id_rsa
How can I get around this issue?

Thanks in advance
 
Old 07-04-2015, 09:43 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
SSH permissions are pretty simple. Different user's keys don't conflict because they have different home directories and store their keys in ~/.ssh. Two people logging into the the same account from the same IP are the same user.
 
Old 07-04-2015, 11:05 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
If you do not specify an IdentifyFile on the command line with the -i option it defaults to ~/.ssh/id_rsa and I believe when using ssh-agent the id-rsa key is offered first.

My suggestion is to create a users config fle (.ssh/config) and add the following. To login via user1 the command is ssh user1.host and user2 ssh user2.host. Change user1(2).host as desired to be any unique name as desired.

Host user1.host
hostname server
User user1
IdentityFile ~/.ssh/key_for_user1
IdentitiesOnly yes

Host user2.host
hostname server
User user2
IdentityFile ~/.ssh/key_for_user2
IdentitiesOnly yes

Last edited by michaelk; 07-04-2015 at 11:10 AM.
 
Old 07-04-2015, 01:27 PM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by michaelk View Post
If you do not specify an IdentifyFile on the command line with the -i option it defaults to ~/.ssh/id_rsa and I believe when using ssh-agent the id-rsa key is offered first.

My suggestion is to create a users config fle (.ssh/config) and add the following. To login via user1 the command is ssh user1.host and user2 ssh user2.host. Change user1(2).host as desired to be any unique name as desired.

Host user1.host
hostname server
User user1
IdentityFile ~/.ssh/key_for_user1
IdentitiesOnly yes

Host user2.host
hostname server
User user2
IdentityFile ~/.ssh/key_for_user2
IdentitiesOnly yes
Well, actually, it all started from a crontab configuration in which I did specifically use rsync -e 'ssh -i /path/to/key', but nonetheless it didn't work.

Anyway, I've configured it, but when I run
Code:
ssh user2.host
, I get the same error - "Too many authentication failures for user2".

Is it related to the fact that one of the private keys is called id_rsa? Maybe I should rename it, as it's the default file that ssh searches for?
 
Old 07-04-2015, 01:41 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
I would delete or comment out the Identifyfile lines in the global ssh config file (/etc/ssh/ssh_config). Also try deleting the identities from ssh-agent:

ssh-add -D
 
Old 07-04-2015, 02:23 PM   #6
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
I commented out the IdentityFile lines and I deleted the keys. Yet I get the same error. I'm thinking it might be related to the mac os keychain, I don't know.

The problem is that now I cannot connect at all with that user since I changed the pair. Before I could, eventually, after tinkering with it more or less randomly. Now it simply says Permission denied (publickey). I am trying to understand what I'm doing wrong.

I did ssh-keygen -t rsa on the client side, generated both files, and copied the public key to ~/.ssh/authorized_hosts.

I don't get it and I'm not sure how to begin to troubleshoot the problem, now that I am not 100 sure that the pair of keys are correcly configured (even though I've done it before and it works with the other user and I don't think that should be the problem).

The private keys both have 700 permission.

Last edited by vincix; 07-04-2015 at 02:25 PM.
 
Old 07-04-2015, 02:32 PM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Only now did it cross my mind to have a look at /var/log/secure
It was a problem of bad ownership of the home directory (which is /backup). I gave apache group permissions to it so as to have access to it. So ssh doesn't like that, as it sees it as a security risk. And it's rather natural, now that I think of it.

What would be a good workaround for this problem? Change the home folder for that user and leave /backup as a common folder for both apache and the respective user?

Last edited by vincix; 07-04-2015 at 02:34 PM.
 
Old 07-04-2015, 02:38 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
This is what I would do...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to ssh to linux server after key changes in authorized keys Voyager7 Linux - Newbie 6 01-18-2011 06:09 AM
[SOLVED] unable to connect to keys.gnupg.net mrmnemo Linux - Software 2 10-17-2010 06:58 PM
unable to connect through ssh ramesh14 Linux - General 3 12-16-2009 05:39 AM
Unable to use pub/private keys with ssh AncientPC Linux - Newbie 18 07-28-2009 05:30 PM
Unable to connect via ssh BlackBone Linux - Newbie 11 08-25-2007 09:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration