What I would like to achieve is to be able to connect to some target.server
from my local machine, without having to type any passwords, with the following commands:
ssh user1@target.server
ssh user2@target.server
Each user above only accepts a connection from a specific IP address, corresponding to respectively gateway1.example
and gateway2.example
. I have added all necessary SSH keys to the right authorized_keys
, so that I can do, without typing passwords:
ssh gateuser1@gateway1.example
from local machine, thenssh user1@target.server
fromgateway1
.ssh gateuser2@gateway2.example
from local machine, thenssh user2@target.server
fromgateway2
.
Now I think it remains to put the right things in my local ~/.ssh/config
. When I add:
Host target.server
ProxyCommand ssh -o 'ForwardAgent yes' gateuser1@gateway1.example 'ssh-add && nc %h %p'
Then ssh user1@target.server
works. But I don't know how to make both user1
and user2
work. I tried:
Host target.server
User user1
ProxyCommand ssh -o 'ForwardAgent yes' gateuser1@gateway1.example 'ssh-add && nc %h %p'
Host target.server
User user2
ProxyCommand ssh -o 'ForwardAgent yes' gateuser2@gateway2.example 'ssh-add && nc %h %p'
or
Host target.server
User user1
ProxyCommand ssh -o 'ForwardAgent yes' gateuser1@gateway1.example 'ssh-add && nc %h %p'
User user2
ProxyCommand ssh -o 'ForwardAgent yes' gateuser2@gateway2.example 'ssh-add && nc %h %p'
but it still didn't work for user2
: all went through gateway1
and not gateway2
.
Thank you very much in advance for your help!
Update: I've been able to make the following commands work:
ssh user1
ssh user2
(without @target.server
), using the following configuration:
Host user1
HostName target.server
User user1
ProxyCommand ssh -o 'ForwardAgent yes' gateuser1@gateway1.example 'ssh-add && nc %h %p'
Host user2
HostName target.server
User user2
ProxyCommand ssh -o 'ForwardAgent yes' gateuser2@gateway2.example 'ssh-add && nc %h %p'
This is what I will be using, but I'm still curious to see a solution to the two commands at the beginning of this question.