Recently, I was playing around with setting up my own server using Amazon's EC2 platform. My goal was to get a Ruby on Rails application up and running on an EC2 instance. If you are unfamiliar with the EC2 platform, it basically allows anyone to rent virtual computers which run your own applications. Learn more about EC2 here.
Instead of using a traditional web hosting company like Heroku or Media Temple, you can run and operate your own server, which is much more cost-effective. In addition, you can dedicate whatever resources you want to the application, making it much faster than traditional hosting. The downside is that you need to be pretty savvy with the command line and know how to operate your own server.
Since I typically use Windows and more specifically the Windows Subsystem for Linux to develop and launch Rails applications, I needed to be able to SSH into my EC2 instance to be able to interact with the host machine.
Note: This post assumes you know how to set up an EC2 instance and how to generate key pairs. If you are unsure how to do this, check out this post here.
Here's how I did it
First, you will need to be on the Linux side of your machine, so make sure you have the Windows Subsystem for Linux all set up. Now, navigate to:
# Now create a new folder called pems # Or use the Bash terminal: mkdir pems # Now download your pem file into this new directory # Mine was called beta.pem # Next, install openssh server in the Bash terminal with: sudo apt-get install openssh-server
Now, before we can proceed we need to change the file permissions on beta.pem, as it is too open and will result in a permission error.
# In the Bash terminal type: cd pems ls # You should now see beta.pem or whatever your file is called. # Now run: chmod 400 beta.pem # change to your file name # This will solve the permission error. # Now you can SSH into your server with: ssh -i beta.pem firstname.lastname@example.org # Note! the 188.8.131.52 number is fake, you will replace this number with the public IP of your machine.
Now if everything worked, you should be inside of your virtual machine - woohoo! One caveat though, I used my public IP instead of my Public DNS link, because it did not work for some reason. It may work for you though...
Let's now refactor
Instead of typing ssh -i beta.pem email@example.com each time, let's make it a bit easier and just type ssh my-first-server.
# To accomplish this, you need to be in the Bash terminal again # Type cd .. # to return to your home directory # Now create a new file with: touch ~/.ssh/config # Now open this file in your favourite text editor, or just use Nano or VIM nano ~/.ssh/config # Enter the below information: Host my-first-instance Hostname: enter-your-public-dns-link User: ec2-user IdentityFile ~/pems/beta.pem # enter your filename, whatever it is called. # Now save the file: Ctrl + 0 Ctrl + X # to exit nano # Note: You can have multiple configurations in this file, for other servers you are running.
Now in the terminal you can simply run:
I hope this post has been helpful.