Sysadmin Diaries - Day 32
Don’t you hate it when you’ve done something before and then when you come to do it again, some time later, you can’t remember what you did and you’re back to square one. That’s why I make a habit of recording what I did. However, for some reason, I didn’t. Annoying,
What had i forgotten about? SSH agent forwarding, If you haven’t come across it before, it’s a way of forwarding ssh keys. This sounds confusing I agree. Here’s why I was using it.
I wanted to connect to an AWS EC2 instance that was in a private subnet, To do that I use a jump host in a DMZ AWS subnet, In case you’re not familiar with the way EC2 connectivity works, when you launch an AWS EC2 instance, you specify an SSH key pair to use. The public key part of this key pair is injected into the EC2 instance when it launches, You download the private key, However, it’s not a good idea from a security perspective storing the private key in a DMZ instance. SSH agent forwarding presents a way around this, Here’s what you do.
You need to have the ssh-agent process running. If it’s not, start it. Find out the PID of the ssh-agent process :
:~$ Â eval `ssh-agent -s`
Agent pid 26208
:~$ export SSH_AGENT_PID=26208
Clear anything already stored already
All identities removed. Add the key for the private instance/server (BastionKeyPair.pem in my case)
:~/.ssh$ ssh-add BastionKeyPair.pem
Identity added: BastionKeyPair.pem (BastionKeyPair.pem)
:~/.ssh$ ssh-add -L
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCe93dBIgsmZO9BYweIkF64cKM/7zbXNxe1Z/…….etc
Now ssh to the jump host using it’s key (tony-aws.pem)
ssh -i tony-aws.pem -A [email protected]
:~/.ssh$ ssh -i tony-aws.pem -A [email protected]
Last login: Wed Feb 15 11:11:05 2017 from cpc90752-finc18-2-0-cust890.4-2.someplace.net
   __|  __|_  )
   _|  (   /  Amazon Linux AMI
   ___|\___|___|
https://aws.amazon.com/amazon-linux-ami/2016.03-release-notes/
38 package(s) needed for security, out of 96 available
Run "sudo yum update" to apply all updates.
Amazon Linux version 2016.09 is available.
Now you can ssh to the private instance/server and the BastionKeyPair.pem will be used for access
[ec2-user@ip-10-50-0-31 ~]$ ssh 10.50.1.94