Black Friday/Cyber Monday Sale

Ends Monday, Nov. 27th CST

Installing Bash on Windows 10



Introduction

This guide may seem a little contradictory at first glance, but bear with me! This might be one of the greatest things a cloud engineer in a Windows environment can do! I’m sure most of you have heard of Windows’ budding partnership with Canonical, and some of you may be a little nervous about the deal, but it has certainly led to some great improvements. One of those improvements is the ability to use Bash on your Windows machine without third party applications!

PREREQUISTES AND IMPORTANT THINGS TO KNOW

To accomplish this feat, you will want to keep two things in mind:

1.You MUST have the 64-bit Windows 10 Anniversary edition or later. The Windows 10 Anniversary Edition corresponds to OS Build 14393.x. Anything above this will suffice. If you do not have this version, you will want to check for updates. This information can be found in the Settings -> System -> About dialogue:

user_58180_59384c2079f4b.png


2.Bash on Windows is in Beta! It is not complete and may not be fully complete for some time. If you are attempting to install Linux applications, they may not work properly. The best use for this is to allow easy and more native SSH access to your servers.

INSTALLATION

Ok, so once you have verified your Windows 10 is up to date and is 64-bit (most of them are) and you accept the fact that you can’t do everything Ubuntu can on your Windows machine, now is the time to actually get it installed!

The installation is a pretty straightforward process:


1.First, you will need to enable “Developer Mode”. This can be found in the Settings -> Update and Security -> For Developers section:

user_58180_59384d85a113d.png


2.Once you have enabled Developer mode, you will need to enable the Windows Subsystem for Linux feature. This can be found by clicking the start menu, typing “Windows Features”, and selecting “Turn Windows features on or off”, which should be the only result.


3.Now that you have the “Turn Windows features on or off” window open, scroll down until you see “Windows Subsystem for Linux (Beta)”. Add a check in the box next to this, then click “OK”:

user_58180_59384daad0502.png


4.This is Windows, not Linux, so you’re going to have to reboot your computer. Bash will not work until you have restarted your computer.


5.Once you have restarted, you can either search for “Bash” in the Start Menu, or you can open a Command Prompt and type “Bash” to start the Bash prompt, then create your new user as prompted.


Congratulations! You now have Bash on Windows! But we’re not done yet! We still need to go through connecting to an EC2 instance using an SSH key, which will take a few more steps.

CONNECTING TO EC2 INSTANCES USING BASH ON WINDOWS

If you’ve ever connected to EC2 or another key-protected server using Bash, most of these steps will be familiar. There are a few things you will need to know to copy your key to the correct location in order to change the permissions of the key and so on. So, let’s get started!


1.First, locate the key you downloaded when you created your EC2 instance. If you haven’t created an EC2 instance, go ahead and do that. If you don’t know how to do this, there are many resources on Linux Academy to walk you through the process.


2.Once you have your key, or know where your old one is located, we will want to copy it to our home directory. You can, of course, place it in a different location, but it’s easiest to have it easily accessible.


3.Your Windows “C:” drive is located in the /mnt directory. If you downloaded your .pem key straight to your Downloads folder (the default download location), you will need to get the key from there. This is typically:

/mnt/c/Users/<Username>/Downloads

Remember: This IS Linux, so capitalization is important!


4.If my key is called “mykey.pem” and it is located in my Downloads folder, I would run:

cp /mnt/c/Users/Derek/Downloads/mykey.pem /~

… which would copy my pem key to my home directory. If you’re not sure what your directories

are called, you can always use “ls” and tab-completion to find them. If you have questions about

these commands, you will want to check out our “Linux Essentials” course to get acclimated.


5.Once you have copied your key to your directory, run:

chmod 400 <keyname>.pem

to set the proper permissions. In my case, it would be:

chmod 400 mykey.pem


6.Once you have done that, you’re ready to connect! Obtain the public IP address or DNS for your server and run:

ssh –i <keyname>.pem ec2-user@<ip_of_server>

if you have a RHEL or CentOS instance.

If you have an Ubuntu instance, run:

ssh –i <keyname>.pem ubuntu@<ip_of_server>

Of course, if you have neither of these or you aren’t using a default Amazon AMI, then you will

use whatever username is appropriate.

OTHER SSH TRICKS!


Alright! So now we can log in to our instances easily without dealing with PuTTY! Another great feature of using Bash for Windows is that we can use all of the SSH features available. This allows us to use an SSH-agent to store keys and perform SSH forwarding!

If you wish to not have to type your key every time you connect, just type:

ssh-agent bash
ssh-add <keyname>.pem

Then, you can connect by just typing:

ssh <username>@<ip_of_server>

Once you have added the key to your SSH agent, you can also perform key forwarding in order to use a Bastion host. To do this, just use:

ssh –A <username>@<ip_of_bastion>

This will allow you to connect to any private server that uses the same key by forwarding the request through the bastion host!

CONCLUSION

Well awesome! You can now connect to your EC2, or any other instances, without having to deal with PuTTY! No more pesky key conversions or cryptic error messages. You finally get to use your company and gaming approved computer to do some real work! I hope this guide helps you out! Feel free to reach out to any of us at Linux Academy for assistance!

SOURCES / RESOURCES

Where can we go for more information?

https://msdn.microsoft.com/en-us/commandline/wsl/install_guide




  • post-author-pic
    Terrence C
    06-07-2017

    Fantastic guide! Who would have thought even a couple years ago - Bash on Windows!

  • post-author-pic
    Prakyath R
    06-08-2017

    great guide but I do not think chmod works for files under C drive as it is NTFS file system. I tried connecting to aws instances using win 10 bash recently but could not do it. 

  • post-author-pic
    Travis T
    06-08-2017

    Awesome guide Derek!

  • post-author-pic
    Derek M
    06-08-2017

     @prakx_rj  that's why we move the key to our home directory in Bash. Once it's out of /mnt, you can do whatever you want. 

  • post-author-pic
    Prakyath R
    06-09-2017

    Awesome!!! Thanks for clearing my doubt  @derekm1215 

Looking For Team Training?

Learn More