0

I'd like to create an SSH connection from localhost that would go through host1 and connect to host2.

My local machine runs macOS. host1 and host2 are AWS machines that require PEM files for authentication.

I run the following command:

$ ssh -i cert.pem -L 9999:localhost:9999 user@host1-ip ssh -L 9999:localhost:1234 -N user2@host2-ip

And receive the following error:

Permission denied (publickey).

How can I make this work?

1 Answer 1

4

Ignoring for the moment the port forwarding part of your example, you need to use the ProxyCommand or ProxyJump directives / -J option. From the command line, try this:

ssh -i cert.pem -J user@host1-ip user2@host2-ip

Since -J and ProxyJump were added in OpenSSH 7.3 (IIRC), you might need to use the older ProxyCommand directive in your ~/.ssh/config file, especially if your ssh on your Mac is not updated recently. Also, this allows you to specify other options such as IdentityFile to put your PEM file, as in:

Host host1
    User host1user
    Hostname host1-ip
    IdentityFile ~/.ssh/cert.pem

Host host2
    User host2user
    Hostname host2-ip
    IdentityFile ~/.ssh/cert.pem
    # hop through host1
    ProxyJump host1
    # or if older ssh-en, one of: 
    #ProxyCommand ssh -A host1 nc %h %p
    #ProxyCommand ssh -A host1 -W %h:%p

With that in place, it shoud be as simple as:

 ssh host2

To add your port forwarding:

ssh host2 -L 9999:localhost:1234

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .