Saturday, November 29, 2014

Connecting to Amazon EC2 behind a proxy

This is more kind of a note to self.

Port 22 which is used for ssh is blocked on my college network, so I needed to tunnel ssh connections over HTTP or HTTPS. For this I did the following things :
  • ssh into the remote EC2 machine (using a proxy free connection).
  • Add
    Port 443
    to /etc/ssh/sshd_config below the line which says Port 22. I’m using 443 instead of some other port because right now I don’t serve my website over https. I might need to change this port to something else once I start accepting https connections too.
  • Restart sshd by running
sudo service ssh restart
  • Now on my machine, install corkscrew by running 
sudo apt-get install corkscrew
  • Edit your ~/.ssh/config file to like the one given below
Host AWS-Proxy
Hostname <Public DNS>
Port 443
User ubuntu
IdentityFile <path to key file>
ProxyCommand /usr/bin/corkscrew 10.10.78.61 3128 %h %p
Host AWS-Free
Hostname <Public DNS>
Port 22
User ubuntu
IdentityFile <path to key file>
  • Now I can simply connect to the EC2 instance when behind proxy using
ssh AWS-Proxy
  • When on a proxy free connection I simply use
ssh AWS-Free

Troubleshooting :
If the above doesn’t work, first of all verify if you’ve allowed incoming connections to your machine over HTTPS (or the custom port that you’re using). Follow the instructions given here for doing so : http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html#adding-security-group-rule
You might need to change the username in the config file depending on the AMI that’s running. For Amazon Linux, the user name is ec2-user. For RHEL5, the user name is often root but might be ec2-user. For an Ubuntu, AMI the user name is ubuntu. Otherwise, check with your AMI provider.
You might need to change the proxy address and port depending on your scenario.

No comments:

Post a Comment

See all Posts