This article of amazon web service tutorial series , aims to show you step by step how you can connect to amazon EC2 instance remotely right from your PC so that you can manage it in the ways you wish. I hope, you have already know about setting up amazon EC2 Linux instance, and have one ready to be connected. In brief, I will describe about the following three key features here:
- Amazon EC2 Security Group: You will know what is security group, how important it is and how to configure it properly.
- Set Up SSH Keys: To connect to instance, a key must need to be set up. If you haven’t already during launch of the EC2 instance, you can learn to do it now also.
- Connect To Your Amazon EC2 Instance: Two different ways are there and I will show the easiest one to get connected for performing necessary operations on your instance.
Make sure, you have read or know about the earlier tutorials on this series to understand this one flawlessly.
Understanding Amazon EC2 Security Group:
This is a very important aspect for management of your AWS EC2 instances. You need to understand properly what kind of security your server needs and thus configure one or assign a previously configured one. Lets go to the ‘Security Group’ option from the left menu items.
As you can see (under ‘inbound’ tab at bottom), you can add as many security group as you want and each can be configured with several protocol/IP access grant. You can either add your own custom port range, or can use the given options for various protocol like SMTP,HTTP, SSH etc. which will automatically open the default ports used for those services. After adding a rule, you must need to submit the ‘Apply Rule Changes’ button as well.
For this tutorial, as we will need to make an ssh connection to our server, please make sure that you have added and applied the ‘ssh’ protocol rule properly.
Adding A Key Pair:
For able to access via ssh, we will need a ssh key pair as well, instead of password based authentication. If you already did one while launching an ec2 instance, you won’t need to do anymore. Just make sure you have the key downloaded on your local PC. But, if you need a new key pair, please go to the ‘Key Pairs’ menu option on left of EC2 dashboard. Click on ‘create key pair’ button.
Enter a name on the pop up window and submit. A key pair will be created and available to you for download to your local PC. In an alternative way, you can import an already existing key pair from your PC as well as follows:
You need to make sure that the ssh key is in a secure place and have proper private access mode(644 is preferred). Otherwise key will be ignored while connecting and you will be shown an error message mentioning it.
Let’s Connect To Amazon EC2 Instance:
As we are set up with the security configuration and have our ssh key as well, we can now go ahead to connect to our instance. Go to your ‘instances’ menu option. Right click on the instance or check the instance and click on ‘Actions’ button above. From the list of commands, choose ‘connect to instance’ option. after clicking it, you should come up with a window with two different options as follows:
- From Browser Using Java SSH client: This is the easiest and my personally favorite option. You won’t have to have installed anything on PC, just need to have java enabled on browser. There is a small concern though. You won’t be able to use chrome for this purpose if you are a mac OSX users( as java 7 for mac doesn’t go well with 32 bit chrome).
- Using A Standalone SSH Client: You can use x-shell/putty in windows environment. For mac or Linux OS, terminal will work fine as well. Though I usually face an issue on mac terminal. It get hanged after some time and I need to restart the terminal and re-connect, which seems annoying to me.
As the easiest option, we will see how can we get connected from browser. First, check whether java is enabled on your browser. If not, above connection window will show you a message mentioning that. In this case, run latest java run-time installer and restart browser(if required). After you have enabled java successfully, you should be able to see options to enter in this window:
Public DNS: The public DNS address/IP will be given for the instance you selected. You won’t need to enter this.
User Name: It’s given as ‘root’ by default. However, amazon won’t allow you to connect as root for security reason. Rather, you will rather need to access via another user and then you may switch to root if you want. Here, for my case, a default user ‘ubuntu’ was existed by default and I did entered that username here.
Key name: It’s also given by default with the name you created/imported the key pair.
Private Key Path: The path of the private key file on your local system. Give the full path name. Just to mention, this key doesn’t have any extension.
Also, optionally you can select to save these given info on browser, so that next time you don’t need to re-enter them.
Congratulation, you have done it! We are now successfully connected to our amazon ec2 instance via ssh protocol.
Run Basic Linux Commands:
Well, let’s test that we can really run commands on our terminal. first, after logged in, you should be able to see whether there is any update available for OS or not. If so, lets run the following commands to get our OS up to date:
$ sudo apt-get update $ sudo apt-get upgrade
If it’s done, we can hope (and definitely will 😉 ) to be able to run our other necessary commands also in future then.
Change The Key Pair For EC2 Instance:
Usually, the key pair attached at the time of creation is the only key pair you can access your instance with. If in any case, you loose the ‘.pem’ file you downloaded or deleted the key from aws control panel, you might need to update the instance with this new key pair. It’s not very much easy. An stack-overflow answer to thread on this key pair change issue explains the procedure very well. Follow those instruction to get it done. However, on step 5, if you don’t know how to mount a EBS volume on a ec2 instance, follow this stackexchange thread.
Please checkout amazon’s official documentation about connect to amazon ec2 instance as well for further reference. Feel free to ask any question, if you have. And stay tuned to receive the next tutorial 🙂