• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Web Server / How To Connect To Amazon EC2 Instance From Local PC

How To Connect To Amazon EC2 Instance From Local PC

January 10, 2013 by Rana Ahsan Leave a Comment

Set Up Amazon EC2 Linux Instance

This article from the amazon web service tutorial series aims to show you step by step how you can connect to an amazon EC2 instance remotely right from your PC so that you can manage it in the ways you wish. I hope you already know about setting up an amazon EC2 Linux instance and have one ready to be connected. In brief, I will describe the following three key features here:

  • Amazon EC2 Security Group: You will know what is security group is, how important it is and how to configure it properly.
  • Set Up SSH Keys: To connect to an instance, a key must need to be set up. If you haven’t already during the 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 to perform 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:

EC2 Security Group Options

This is a very important aspect of the 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. Let’s go to the ‘Security Group’ option from the left menu items.

As you can see (under the ‘inbound’ tab at the bottom), you can add as many security groups as you want and each can be configured with several protocol/IP access grants. You can either add your own custom port range or can use the given options for various protocols like SMTP, HTTP, SSH etc. which will automatically open the default ports used for those services. After adding a rule, you must also submit the ‘Apply Rule Changes’ button.

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:

create key pair

For access via ssh, we will need an 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 it 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 the left of the EC2 dashboard. Click on the ‘create key pair’ button.

import key pair

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 has proper private access mode(644 is preferred). Otherwise, the 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 personal favourite option. You won’t have to have installed anything on PC, need to have java enabled on the browser. There is a small concern, though. You won’t be able to use chrome for this purpose if you are a mac OSX user ( as java 7 for mac doesn’t go well with 32-bit chrome).
    Java SSH Client For Connect To Amazon EC2 Instance Window
  • Using A Standalone SSH Client: You can use x-shell/putty in a windows environment. For mac or Linux OS, the terminal will work fine as well. Though I usually face an issue on the mac terminal. It gets hanged after some time, and I need to restart the terminal and re-connect, which seems annoying to me.
    connect via your own ssh client

As the easiest option, we will see how we can get connected from the browser. First, check whether java is enabled on your browser. If not, the above connection window will show you a message mentioning that. In this case, run the latest java run-time installer and restart the 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 reasons. Rather, you will rather need to access via another user, and then you may switch to root if you want. Here, in my case, a default user ‘ubuntu’ existed by default, and I 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 the given info on the browser so that next time, you don’t need to re-enter them.

launching java ssh client

Now, click the ‘Launch SSH Client’ button. If you have done all properly, it should get you a java based ssh terminal as follows:(you may be asked for permission first, from OS as well)

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 logging in, you should be able to see whether there is any update available for OS or not. If so, let’s run the following commands to get our OS up to date:


$ sudo apt-get update
$ sudo apt-get upgrade
Code language: JavaScript (javascript)

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 lose the ‘.pem’ file you downloaded or delete the key from AWS control panel, you might need to update the instance with this new key pair. It’s not very much easy. A stack-overflow answer to the thread on this key pair change issue explains the procedure very well. Follow those instructions to get it done. However, on step 5, if you don’t know how to mount an EBS volume on an ec2 instance, follow this stackexchange thread.

References:

Please check out amazon’s official documentation about connecting to the amazon ec2 instance as well for further reference. Feel free to ask any questions you have. And stay tuned to receive the next tutorial 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Web Server Tagged With: aws, cloud server, ssh

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#
  • Beginning Codeigniter Application Development
    Beginning Codeigniter Application Development
  • Control HTML5 Audio With Jquery
    Control HTML5 Audio With Jquery

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023