Use a different SSH key for the jump host and the target
ssh -i applies only to the target, not to a -J jump host. Give each host its own Host block with IdentityFile, or pass the jump key in a ProxyCommand.
-i on the command line sets the key for the target only. The connection to the jump host is made by a second ssh that does not get your -i, so it uses that host's own settings in ~/.ssh/config, your default key files and your agent. The ssh manual says it for -J: command-line options generally apply to the destination, so use ~/.ssh/config for the jump hosts.
Set it up in ~/.ssh/config
Give each host its own block. The name after ProxyJump is looked up in the same file, so the jump block applies to the jump connection:
Host jump
HostName jump.example.com
User user1
IdentityFile ~/.ssh/id_ed25519_jump
IdentitiesOnly yes
Host app
HostName 10.0.0.5
User deploy
IdentityFile ~/.ssh/id_ed25519_app
IdentitiesOnly yes
ProxyJump jump
Then run ssh app. Each login uses its own key and user name.
IdentitiesOnly yes makes ssh offer only the key named in that block, even when your agent holds others. Without it, ssh also offers the agent's keys, and the server can stop at its MaxAuthTries limit before the right key is tried; see SSH "Too many authentication failures".
One command, without a config file
Write the jump connection out as a ProxyCommand so you can pass it its own -i. -W connects that ssh's input and output to the target's SSH port through the jump host:
ssh -o ProxyCommand="ssh -i ~/.ssh/id_ed25519_jump -W %h:%p [email protected]" -i ~/.ssh/id_ed25519_app [email protected]
%h and %p are replaced with the target's host and port. Do not set ProxyJump for the same host as well: the ssh_config manual says whichever of the two is specified first prevents the other from taking effect.
Check which key each hop used
-v is passed on to the jump connection, so one run shows both logins:
ssh -v app
Look for Offering public key: lines, and for Authenticated to jump.example.com and Authenticated to 10.0.0.5 (via proxy) followed by the method. If the jump login fails with Permission denied (publickey), the jump host did not get the key you meant: check that the name after ProxyJump matches the Host block. Run ssh -G jump to print the settings ssh will use for it.
Things that stay on your device
With ProxyJump or ProxyCommand ssh -W, your device logs in to the target through the jump host. The target's private key is used on your device and does not need to be copied to the jump host, and the jump host does not need agent forwarding. See Jump hosts (ProxyJump).
Related
- Jump hosts (ProxyJump)
- The ssh config file
- SSH "Too many authentication failures"
- ssh-copy-id through a jump host (ProxyJump)
- SSH port forwarding through a jump host: -J with -L
- Host key verification failed through a jump host (ProxyJump)