how to use ssh agent forwarding
Solution 1
First you have to invoke ssh-agent
on your client to make it remember your key
ssh-agent -t 3600 ~/.ssh/private_key_rsa
(assuming that your key is stored in ~/.ssh/private_key_rsa
, you can also leave out the -t 3600
if you want infinite lifetime)
then you simply ssh into one of your servers using the -A
option
ssh -A server1
from there you will then be able to ssh into server2
ssh server2
If you do not want to specify the -A
option everytime you can add the following to your ~/.ssh/config
(on the client and optionally both servers)
Host server1
ForwardAgent yes
Host server2
ForwardAgent yes
This works for any number of servers. To keep the ~/.ssh/config
short you can introduce wildcards e.g.
Host server?
ForwardAgent yes
Solution 2
Forward server host to localhost :
ssh -L localhost:22:localhost:22 user@host
or
ssh -N -f -L serverhost:22:localhost:22 user@server1
After reading your question again.
You want to ssh into server1 :
ssh user@server1
Then you want to ssh into server2:
Into new terminal from client do:
ssh user@server1
ssh user@server2
Then you have 2 connections:
- client to server 1
- client to server 1 ==> server 2
If you want to have:
- client to server 1
- client to server 2 (With same key.)
Just do following command.
On client:
Use tmux or open 2 terminals
ssh user@server1
In new terminal:
ssh user@server2
Related videos on Youtube
![bojack horseman](https://lh4.googleusercontent.com/-J9RQAn3uYZs/AAAAAAAAAAI/AAAAAAAAAAc/sLW4dm_9Txk/photo.jpg?sz=256)
bojack horseman
Updated on September 18, 2022Comments
-
bojack horseman almost 2 years
I have three systems , a client that only install ssh client and server_1 and server_2 . im using ssh public authentication and i can ssh from client to both server_1 and server_2. i saved same pub key for server 1 and and now i want to ssh from server 1 to server 2 using agent forwarding and i want my private key stay only on client please help me ASAP how can i do this scenario ? i use this link but dont know how to do it .
An Illustrated Guide to SSH Agent Forwarding: Public Key Access with Agent Forwarding
-
bojack horseman over 6 yearsit didnt helped no public key error
-
An0n over 6 yearsYou want to forward or 'client to server1 and client to server2?'
-
bojack horseman over 6 yearssame pub key is stored on both server and i can ssh from client to both . now i want to use ssh agent forwarding to ssh from server 1 to 2 without private key save on server like link above
-
An0n over 6 yearsEdited my answer.
-
bojack horseman over 6 yearsur right in this case . but how can ssh from server 1 to server 2 directly without ssh from client to server 1 using key request and response . imagine server 1 system is some place else and i want ssh from that system to server 2 not from client
-
An0n over 6 yearsYou can' t unless you forward from server 1 to server 2. But then you cant ssh into server 1 separately. So you have to do it manualy.
-
An0n over 6 yearsOr you use different ports. And forward from server1 (another port) to server2.
-
An0n over 6 yearsYou want to ssh "from that system" so you ALWAYS have to ssh into that system first, wherever you are. Otherwise you just dont use that system. In that system you forward to another server.
-
bojack horseman over 6 yearsso it should be like this : a->b->c ???
-
An0n over 6 yearsyes as i did in my answer. Read after the edit the first lines.
-
bojack horseman over 6 yearsi did ur answer before but i thought i can connect to server 2 from server 1 seperatly
-
bojack horseman over 6 yearsusing public key
-
bojack horseman over 6 yearsand ssh agent forwarding but i didnt find anything on net .
-
An0n over 6 yearsIf you can Edit your Question, Then I can Edit My Answer.
-
An0n over 6 yearsWhat about server3?
-
mbeyss over 6 yearsThis works for any number of servers. (see also my recent edit). If all servers accept the same key you can do
ssh -A server1
from theressh -A server2
from theressh -A server3
and so on. -
MadMike over 2 yearsWhich is also mentioned in the other answer ;)
-
Daniel over 2 years@MadMike right, but it's hard to find because of length of answer
-
Brent Sandstrom over 2 yearsFor anyone else having a brain fart moment: If you set up agent forwarding for a user, and then run sudo ssh it won't work because you're running ssh as root