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!

Python with Ubuntu on Windows

Now that bash is on Windows, I wanted to try and make all the other guides I’d writen for Python on Windows irrelevant.

So here’s how to setup an effective environment for Python on Ubuntu on Windows.

1. Install Bash on Windows
2. Check for updates
3. Check out the REPL
4. Install Pip
5. Install VirtualEnv
6. Install VirtualEnvWrapper
7. Create your first virtualenv
8. Configure bashrc to keep it working
9. Install some packages
10. Test Flask

1. Install Bash on Windows:
Here’s the announcement blog for context.
How to Geek has a good breakdown of making it happen.

Make sure you remember your password.

Now that its installed try opening a command prompt and typing bash.
The prompt should change like this:
bashtomnt

Notice that path?
/mnt/c/Users/TimReilly

That’s your user directory for windows where your OneDrive, Documents, Desktop, etc. exist.

You can go in there now and run python scripts that might already exist, but your probably won’t have all the necessary packages installed.

Before we move forward we want to make sure Ubuntu is up to date.

2. Check for updates
From another command prompt:
lxrun /update

And inside bash
sudo apt-get update

Thanks reddit for the tips!

3. Check out the REPL

Now run python!
$ python

Should look like this:
pythonrepl

4. Install Pip

Now we’ll install Pip:
sudo apt-get install python-pip

If you have permission issues try starting an elevated prompt:
$ sudo -i
$ apt-get install python-pip
$ exit

Use exit to return to the regular prompt.
Should look like this:
sudoi
Yay we’ve got pip!
Try pip list to see what comes standard.

5. Install VirtualEnv
Now we’re basically following along with the guide presented at hitchhikersguidetopython.com

Again, you might need to start an elevated prompt to install virtualenv.

$ sudo -i
$ pip install virtualenv
$ exit
$ cd my_project_folder
$ virtualenv venv

Then to use the VirtualEnvironment

$ source venv/bin/activate

You should now see a little (venv) before your prompt.
Like this:
virtualenvfolder

Now you’ve created a virtualenv inside of your my_project_folder directory. Which is cool, but can be confusing with git, sharing code, and testing package versions.
So we use VirtualEnvWrapper to keep our virtualenvs in the same place.

Before we move on make sure you deactivate your env
deactivate

6. Install VirtualEnvWrapper

http://virtualenvwrapper.readthedocs.io/en/latest/index.html


$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh

$ export WORKON_HOME=~/
Can be customized to whichever directory you’d like to place your virtualenvs

7. Create virtualenv using virtualenvwrapper

$ mkvirtualenv venv
$ workon venv
$ deactivate

Here’s an example of what it looks like to remove our venv directory and instead use venvv which will be stored in the directory underlined in red.

venvv

8. Configure bashrc to keep it working

This might not happen to you, but when I opened a new bash terminal I had to re-source my virtualenvwrapper.sh and WORKON_HOME.

So instead I added those lines to my bashrc script.

$ sudo nano ~/.bashrc
-- type in password --

This is what it looks like in nano for me.
Ctrl+X to exit and y-enter to save.
bashrc

Then either:

Source ~/.bashrc

Or start a new command prompt->bash and try “workon” or “lsvirtualenv”

See the next image for a simple workflow.

9. Install some packages

Now lets install ‘requests’ into our newly created virtualenv:
pipinstallrequests

Isn’t that nice!

10. Test Flask
Finally we’re going to test this with flask.
First we install the required files using pip into our activated ‘venv’
Then runserver -> navigate to the designated address -> and see our site.

Here’s what it looks like:
flaskrunning

Have fun building with Python, on Ubuntu, on Windows!

Bike with pizza tied to the back
Sometimes you gotta tie a pizza to your bike.