Can't ssh from one (public) ec2 instance to another (private) one

,

I’m experimenting with private/public subnets in a VPC and have the following setup:

VPC set up using 10.24.0.0/16
3 x public subnets: 10.24.0.0/24, 10.24.1.0/24 & 10.24.2.0/24
3 x private subnets: 10.24.20.0/24, 10.24.21.0/24 & 10.24.22.0/24

I have a “public” ec2 instance spun up in the second public subnet and allocated a public ip address. Security group rules are set such that I can ssh in and see the internet from this machine (and run Apache on it, which is visible to the outside world) - this all works as expected.

I have another “private” ec2 instance spun up in the second private subnet, with no public ip address set and security group rules set to allow ICMP traffic and SSH access from 10.24.0.0/16 only.

I can ping the private machine from the public one, but can’t ssh into it. As per the AWS docs, I’m doing

johno@mac_laptop:~$ ssh-add -K my-keypair.pem [WORKS]
johno@mac_laptop:~$ ssh -A ubtuntu@ PUBLIC_IP [WORKS]
ubtuntu@PRIVATE_IP:~$ ssh ubuntu@PRIVATE_IP [WORKS]

Both machines are Ubuntu 14.04 systems, set up using the same keypair and I’m reasonably certain it’s an ssh thing rather than a network one because:

  • I get output from ‘ssh -v’ that looks like it’s seeing the remote machine [see below]
  • I’ve since spun up another machine in the same public subnet and am getting the same issue

Permissions on the public machine .ssh folder look ok:

ubuntu@ip-10-24-1-43:~$ ls -l ~/.ssh/
-rw------- 1 ubuntu ubuntu 390 Apr 10 15:42 authorized_keys
-rw-r–r-- 1 ubuntu ubuntu 444 Apr 10 16:55 known_hosts

I suspect I’m being a complete twit here, so any pointers on this would be very much appreciated.

ubuntu@ip-10-24-1-43:~$ ssh -v ubuntu@10.24.21.145
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 10.24.21.145 [10.24.21.145] port 22.
debug1: Connection established.
debug1: identity file /home/ubuntu/.ssh/id_rsa type -1
debug1: identity file /home/ubuntu/.ssh/id_rsa-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_dsa type -1
debug1: identity file /home/ubuntu/.ssh/id_dsa-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_ecdsa type -1
debug1: identity file /home/ubuntu/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/ubuntu/.ssh/id_ed25519 type -1
debug1: identity file /home/ubuntu/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.4
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.4 pat OpenSSH_6.6.1* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5-etm@openssh.com none
debug1: kex: client->server aes128-ctr hmac-md5-etm@openssh.com none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA b2:c1:9d:bc:05:9a:e1:f1:75:d1:8f:9a:af:df:71:3b
debug1: Host '10.24.21.145' is known and matches the ECDSA host key.
debug1: Found key in /home/ubuntu/.ssh/known_hosts:1
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/ubuntu/.ssh/id_rsa
debug1: Trying private key: /home/ubuntu/.ssh/id_dsa
debug1: Trying private key: /home/ubuntu/.ssh/id_ecdsa
debug1: Trying private key: /home/ubuntu/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

Related discussions:

Looks like a simple key issue - for a quick test try the -i flag with the correct key.

ssh -ivv /path/to/key.pem ubuntu@10.24.21.145

I’m mobile and can’t read your post carefully. However, mistakes I have made myself: using public IP when I should be using private IP and also security groups is a big one. Make sure both machines are in security groups that allow the correct incoming and outgoing. Sorry not more specific but maybe that will help you or others later.

Hmmm. That ssh command works OK from my mac laptop, where I have the .pem file stored (i.e. I can connect and everything looks as I’d expect). It doesn’t work at all (no connection allowed) from the PUBLIC_IP machine because I don’t have the .pem file stored locally on the PUBLIC_IP machine and that’s the only machine that has network access to the 10.24.21.145 one.

What I’d like to achieve would be straightforward access to several private subnet machines via a single bastion host in a public subnet. The AWS docs indicate that the ssh-agent running on my laptop should take care of forwarding the credentials on to the PUBLIC_IP machine but that looks not to be the case(?).

Put the pem key on the public server and try this

ssh -v -i /path/to/key.pem ubuntu@10.24.21.145

Just so you know it’s working

  • check routing entries among subnets
  • check security groups
  • upload .pem used to spawn instance with private ip to the instance with public ip to /home/ubuntu/.ssh/ and use it to connect to the private instance
  • or instead of uploading ssh key (because it is really not a good idea) - tunnel via instance with public ip (aka jump host) using -A or just forward ssh using -L
  • if you can just set up IPsec tunnels between VPC and your office branch

Looks like a simple matter of not connecting to the Public instance with key forwarding on the session. Add -A to your initial SSH connection string, also specify the pem file directly with -i and try again

Edit: note, I’m on mobile and the formatting of your post is shite. So apologies if youre already doing what I suggested

if you can ping the private machine from the public one, the routing should be good. as others have said, the most likely cause is not fowarding the correct ssh key. can you drop the pem for the private machine on to the public one and initiate a connection referencing it? (windows guy here - ssh semi-noob)

Thanks - that works (scping the pem to the public one and using it from the public machine to get to the private one). What’s not appears to be the key forwarding part - I’m doing

local_laptop:~$ ssh -A -i keypair.pem public
public:~$ ssh private

and the docs imply that this should work without having to load up the public machine with the keypairs - am I correct in assuming that?

Sorry about the formatting but, yes, I am doing -A initially:

local_laptop:~$ ssh -A -i keypair.pem public
public:~$ ssh private

The first works, the second doesn’t - unless I scp keypair.pem to the public machine and then do

public:~$ ssh -i keypair.pem private

but I’m hoping to do this without needing to get the private machine keypair(s) on the public one.

Yup, that works perfectly. Which presumably means it’s an ssh forwarding issue, right…?

RESOLVED:

Having woken up to a bunch of responses that indicated that ssh -A should just work, I started digging around in my ssh config files and found this in my local machine ~/.ssh/config file, which I added months/years back. Removing it makes everything work as expected:

Multiplex multiple connections

TCPKeepAlive=yes
ServerAliveInterval=15
ServerAliveCountMax=6
Compression=yes
ControlMaster auto
ControlPath /tmp/%r@%h:%p
ControlPersist yes

Thanks, all, for your replies to this - much appreciated. Now if you’ll excuse me, I have to hit myself with something heavy while chanting “never add stuff to your config files unless you know you need it” ಠ_ಠ

curious - why would that make it not work?

That is an extremely good question - and one which I’m hoping either the ssh docs or /r/ssh is able to help with.

Your current commands arent using the key correctly

yes, you need to set up key forwarding properly