There are 2 ways to do this
1. Adding the keys via ssh-add command for ssh-agent
Add key A for machine B on local machine (i.e. Machine A)
ssh-add path_to_keyA/key_A.pem
Add key B for Machine C on local machine (i.e. Machine A)
ssh-add path_to_keyB/key_B.pem
Then use the ssh command to connect
ssh -v username@Machine_C -o "ProxyCommand=ssh -W %h:$p username@Machine_B"
- The -v is just to output what is going on
- You would replace the username for each machine with the username that is configured on the remote machines
- Replace Machine_C and Machine_B with host names or IP addresses in your setup
2. Without adding keys to the agent
If you want to just connect through a single command without using agent, then you can use
ssh -v -i path_to_key_B/key_B.pem username@Machine_C -o "ProxyCommand=ssh -i path_to_key_A/key_A.pem -W %h:%p username@Machine_B"
Again...
- The -v is just to output what is going on
- You would replace the username for each machine with the username that is configured on the remote machines
- Replace Machine_C and Machine_B with host names or IP addresses in your setup
- the -i flag is followed by where the keys are located in each case on the local machine (i.e. Machine A)
In the event of an issue with key permissions ensure the keys are sudo chmod 600 key_file_name.pem