ozbillwang
12/8/2015 - 11:20 PM

Easy way to connect to ec2 instance in office behind proxy and at home

Easy way to connect to ec2 instance in office behind proxy and at home

AWS ec2 instances normally have both public DNS name and public IP address. We can wisely use them for office environment which may behind proxy and directly connect from home.

Set below code in .ssh/config and enjoy convenience.

Suppose the instance has name as

ec2-54-252-146-85.ap-southeast-2.compute.amazonaws.com
54.252.146.85

So you can set as below

Host ec2-54-252-146-85.ap-southeast-2.compute.amazonaws.com
  User ec2-user
  IdentityFile ~/.ssh/yourkey.pem

Host 54.252.146.85
  ProxyCommand nc -X connect -x proxyserver:8080 %h %p
  User ec2-user
  IdentityFile ~/.ssh/yourkey.pem

With that, you can connect with its IP address in office

ssh 54.252.146.85

And connect with its dns name at home

ssh ec2-54-252-146-85.ap-southeast-2.compute.amazonaws.com