Sunday, 15 January 2017

Install Azure CLI for Linux on Windows

The following lists the steps needed to install Windows Subsystem for Linux on Windows 10 and Azure CLI, so that you can try out using the CLI on a "Linux-like" environment.

Enable WSL:

If not done yet, enable WSL (Windows Subsystem for Linux):
  1. Enable Developer Mode:
    Windows > Settings > Update and Security > For developers > Developer mode;
  2. Enable the Windows Subsystem for Linux and restart by running this command in an elevated PowerShell:Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
  3. Run "bash.exe" and complete installation of WSL;

Install Azure CLI on WSL:

  1. Launch bash
  2. If not done yet, work around sudo not being able to resolve your machine name by running:sudo chmod 666 /etc/hosts
    sudo echo $HOSTNAME >>/etc/hosts
    sudo chmod 644 /etc/hosts
  3. Install (a decent!) NODE package:
    curl -sL | sudo -E bash -
    sudo apt-get install -y nodejs

    NOTE 1: if you try going the "simpler" way of just running "sudo apt-get install node", you'll get a very old node version and when you later run any azure command you'll get an error similar to the one below: (this is after running azure login, which does work even with the broken node!)
    login@COMPUTER:~$ azure location listinfo: Executing command location list+ Getting ARM registered providers
    error: Object
    has no method 'endsWith'error: Error information has been recorded to /home/mad/.azure/azure.err
    error: location list command failed

    NOTE 2: if you get connectivity problems when running the above commands, try disabling your AV's FW - I ran into some weird problems using Kaspersky and temporarily disabling allowed these to run properly. The errors I'd get would complain of no connectivity from an IPv6 IP address, even though I have IPv6 disabled...
  4. Install the Azure CLI NPM module: sudo npm install -g azure-cli
  5. Enable auto-completion: azure --completion >> ~/azure.completion.shecho 'source ~/' >> ~/.bashrc
  6. Exit and re-open bash and you should now have a working install of Azure CLI commands for Linux, running on your Windows Subsystem for Linux.
  7. Try it out: 
    1. azure login (Follow the on-screen instructions) 
    2. azure account list Don't forget that you have auto completion for azure commands too!

Bonus: Private/Public Keys

  1. Generate a private/public SSH key pair for easier authentication (replace "mypassword" with a proper password that you'll type when needed) ssh-keygen -t rsa -b 2048 -C "$USER@$HOSTNAME" -f ~/.ssh/id_rsa -N mypassword
  2. Verify that the ssh-agent is running (you should get a PID value listed):
    eval "$(ssh-agent -s)"
  3. Add the newly created key to ssh-agent:
    ssh-add ~/.ssh/id_rsa
  4. If you have already created a VM you can install the new SSH public key to your Linux VM with (replace login@yourvmip with proper values):ssh-copy-id -i ~/.ssh/ login@yourvmip
  5. Test the login using keys instead of a password (again, replace login@yourvmip with proper values – this is a single line, split only on the blog!): ssh -o PreferredAuthentications=publickey -o PubkeyAuthentication=yes -i ~/.ssh/id_rsa login@yourvmip
  6. SSH is successfully configured if you are not prompted for an SSH private key password, or a login password to the VM.

If you need to re-install WSL:

(As I did a few times trying to compile and test these instructions)
Run from a command prompt:
lxrun /uninstall /full
Note: the /full will WIPE your home folder, so a new install will be a clean install; if you want to keep those, just omit /full
lxrun /install
Please note that installing will re-download the Linux image from the Windows store again, so beware of internet charges if applicable.

Sources used to compile this information:

No comments:

Post a Comment