Generating an SSH Key and Using it on Azure

SSH KEYS allow us to connect to VMs without using passwords but by passing a private key that can be managed by you or your organization.

For more about SSH

There are three parts to this tutorial:
A. Generate an SSH Key
B. Create a VM in Azure that uses the public key
C. Connect to VM using SSH keys

Prerequisites:
Bash
ssh-keygen ($ info ssh-keygen to learn more)
An Azure Subscription

A. Generate an SSH Key

Open bash and enter:
$ ssh-keygen -t rsa -b 2048 -C "Ubuntu@azure-server"
Keyname: server-key
Passphrase: somethingMemorable

Copy the contents of server-key.pub
$ cat server-key.pub

Should look like this:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMlUr7PCEdBmCVZHG5RqI8i7GgYAzd2G/FZ987XXa63vnqxZmZogVmmXrTnBHeM6oDv7v7g495CiiiINhJbGR4o7t4agiHOM43egDv7BbiViTlfVr3y5AxLUvRwHnC3egl8ABVX1anfXXR73x7IS3YRNWkh6gXtlhImw8UKG04UoZEmWB9BLt53lk/9c3Hxz22YZarzImrpQYy1XEUZ096B9mK/Fe+/McH78ZHUpXEgOZBIDP5KdqPk5XKznpwUDJ4/SPXPEWWCCjQ8gOoTFcFMaiMnXp5o5Udsi/DFO1TS/t8BeCRymkr5tdPvzexjkjkjkjkjkjkjkjkjkjkjkjt Ubuntu@azure-server

Here’s what it looks like for me:
keygenandcat

Cool and you’ll also notice that there’s another file in that same directory – server-key
$ ls | grep server
Here’s what that looks like for me:
lskeys

Now that we have our public and private keys let’s get our VM setup.

B. Create a VM in Azure that uses the public key

1. Go to the Azure Portal

2. Select New -> Search: Ubuntu Server
(I’m using 14.04 this time)
selectubuntu1404

3. Make sure you’ve selected Resource Manger and click Create:
resourcemanagecreate

4. Now configure the basics per our ssh-keygen parameters
Name: azure-server
VM Disk Type: Up To You
User name: Ubuntu
Authentication type: SSH public key
SSH public key: Paste the results of $ cat server-key.pub
Subscription: Depends how you want to pay for the server
Resource Group: Up to you – I’m going to create a new one so I can quickly delete it.
Location: Up to you

Should look like this:
basicsconfigurationforssh

Then select OK to go to the next section.

5. Choose Virtual Machine Size
I’m going with the smallest VM for testing.
You can also view all different VM sizes to find the right one for your use case.
pickvmsize

6. Configure optional Features
Setting the Storage account name to something you’ll remember easily is good.
And if you want to configure ports now you can select Network Security group to allow ports specific traffic.
Here’s what that looks like:
optional-azure-settings
Click okay to continue to the Summary of your VM.

Here’s our summary:
summary-click-okay-to-create-vm

Select okay to start your VM.

7. Wait for it to be ready.
Dashboard will have an icon and you’ll get a notification when its ready:
waiting-for-vm-to-spin-up-from-dashboard

8. Once ready select on it to see the overview and the IP address.
Should look like this:
vm-overview-ip

Great! We have a VM and its IP address. Lets use our Private SSH key to connect.

C. Connect to VM using SSH Keys

1. Open bash to file location you created the keys in.
Make sure they’re there:
$ ls | grep server

2. Enter this command to use SSH to connect:
$ ssh -i server-key Ubuntu@52.183.31.11 -v
or more generally
$ ssh -i keyname username@ip.address -v
Make sure you’re using server-key and not server-key.pub
Tip: -v is the verbose option. Not necessary, but it helps to see if the key is being accepted

3. Great, now accept the certificate, and enter your memeroablePassphrase
Whole thing should look like this:
ssh-using-key-to-inbash

And you’ll be in the terminal of your VM:
in-the-terminal-of-the-vm

Yay!
You’ve got the key, you’ve got the VM, now put it to work!
Flask on Ubuntu
Node on Ubuntu
Mongo on Ubuntu
Connecting to VMs from Azure Web Apps

Let me know if you have any questions by posting in the comments below!

These people just want High Fives!
These people just want High Fives!

Running Node and Express on Ubuntu VM

So you just spun up your first Ubuntu Virtual Machine?
No…
Let’s fix that: “Intro to Ubuntu on Azure”

YEAH!
Let’s put it to work!

The Basic Steps to using node on an Azure VM:
1. Open the Port
2. Install Git and Install Updates
3. Install Node and NVM
4. Code and Install Express
5. Run it and check it out!
6. Use forever to keep it alive

1. Open the Port
We’re not talking battleships or submarines we’re talking Infrastructure as a Service.
Visit the landing page for your Ubuntu Virtual Machine:
vmnumberonelandingpage

And select the resource group in the top left corner:
resourcegroup

Resource groups are the way we break down how our VM interacts with the internet, other vms, storage, and public/private networks.

To open the port we need to change our network security group which is represented by the shield. (Underlined in the above screenshot)
Then we’ll select settings -> Inbound security rules
networksecuritygroupsettings

This will allow us to open up our VM to the Public Internet so we can visit it like any other website.

Under ‘Inbound security rules’ SSH is already included:
defaultssh

We’re going to add a new Inbound security rule named ExpressServerPort where we’ll set the Destination port range to 3000 which we’ll see later when starting our server. Here’s the configuration pane for our ExpressServerPort:
destinationport

2. Install Git and Check Updates
Git is fun!

SSH into our Virtual Machine like we did in the VM intro then enter:
$ sudo apt-get install git
sudoaptgetinstallgit

Yay! We have Git

Let’s run our update command just to double check we’re up to date:

$ sudo apt-get update
sudoaptgetupdate

Great let’s move on with Node!

3. Install NVM and Node

First NVM: https://github.com/creationix/nvm

NVM is a version manager for Node you can install using curl:
Enter this command to install it:
$curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash

Then source your profile to add it to your commands:
$ source ~/.profile

Then check out the version to make sure it installed:
$ nvm --version

It should look like this:
nvmisntallversion

NVM is installed!
Let’s install a version of Node!
$ nvm install 5.0

Then check our version of Node:
$ node -v
nvminstallfive

Sweeeeeeet

Check out NVMs readme on the github for more commands:
https://github.com/creationix/nvm

With node comes ‘npm’ which allows us to install a whole bunch of node awesomeness. One of the more popular packages is express a minimalist web framework, we’ll install this to start coding away.

4. Code and Install Express

We’ll be following along “Express’s” introduction if you get lost/have more questions about express.
http://expressjs.com/en/starter/installing.html

First let’s create our a directory, cd into it and initialize our app using nom.

$ mkdir myapp
$ cd myapp
$ npm init

After entering npm init we’ll be walked through a configuration step for our app. The only one that matters for now is (index.js) which will be the entry point for our app everything else can be the default for now.

If you were actually going to submit/share this code you’d want to accurately fill out this info.

After the initialization step we’ll add express and list it as a dependency:
$ npm install express --save

Here’s what those steps look like:
mkdirmyapp

Weeee now have express and an app ready for your code!

Lets open nano and put the helloworld sample into index.js

$ sudo nano index.js

sudonanohelloworld

Now lets run our app!
$ node index.js
nodeindexjs

Now that its running lets visit it by entering the IP address and port number into our browser.
In my case the URL is: http://13.88.180.170:3000/
ipaddressandport

Neat!

6. Use forever to keep it alive
Unfortunately, if we close our Terminal/SSH Connection our project will stop running.
To solve this, we use another NPM package called forever.
Here’s the link to the repository with clear instructions.

In short we install it globally:
$ sudo npm install forever -g

Then start it:
$ forever start app.js

And stop it:
$ forever stop app.js

That’s it for now!

Now clone one of your node projects and run them in the cloud!
Happy Hacking!

When the Male Roommates are home alone...
When the Male Roommates are home alone…