Talk Tech to Me: Using Linux BASH on Windows 10

Our Linux guru, Jason Eckert, is back to “Talk Tech to Me” with a few crash courses on some of the hottest Linux topics. For each of the next three weeks, we’ll have a new post full of commands and step-by-step instructions to help you master Linux.
Talk Tech to Me, brought to you by CompTIA

Our Linux guru, Jason Eckert, is back to “Talk Tech to Me” with a few crash courses on some of the hottest Linux topics. For each of the next three weeks, we’ll have a new post full of commands and step-by-step instructions to help you master Linux.

Read more from Jason in All About Linux and Linux+ and 3 Ways CompTIA Linux+ Helps New Grads Land Jobs.

What is BASH and WSL?

The Bourne Again Shell (BASH) is the command-line interface used on Linux/UNIX/Mac systems. It has a rich scripting environment that allows system administrators the ability to automate most administrative tasks.

Windows Subsystem for Linux (WSL) is the Windows 10 feature that allows a Linux system to run natively within the Windows 10 operating system. It’s important to note that WSL does not work like a virtual machine; instead, it simply provides a software bridge that allows Windows 10 to spawn a full Linux operating system within RAM (including BASH) when the Windows 10 user chooses to open a BASH shell.

How Do I Install WSL and Obtain BASH Within Windows 10?

Before installing W SL, you must first ensure that you have the latest updates on your Windows 10 system. Next, you must enable Developer mode in Settings > Update & security > For developers as shown below:

A screenshot of the Windows developers menu

After enabling Developer mode, you must reboot your system. Following this, you will be able to add the WSL feature using Programs & features > Turn Windows features on or off:

A screenshot of the menu to turn Windows features on or off

Again, you’ll need to reboot your system after enabling WSL. After this reboot, you’ll be able to start BASH in one of two ways:

  1. By searching for BASH on the Windows Start menu and executing it.
  2. By opening a regular Windows command prompt and typing bash.

The first time that you start a BASH shell, you will be prompted to accept a license agreement, and then your system will proceed to download and install Ubuntu 14.04 Linux. Finally, you will be prompted to specify a regular username and password for the Ubuntu system. (I recommend using the same username and password as your Windows 10 login for simplicity – in my case, jasoneckert.) When the process is finished, you will receive a BASH shell prompt ($) for your user account. At this prompt, you can type the lsb_release -a and uname -a commands to verify that you are running an Ubuntu Linux system.

BASH shell prompt

On Ubuntu systems, the root (administrator) user is not enabled by default. To enable the root user, you can run the sudo passwd root command, specify your password, and then specify the new root user password twice when prompted. Once you do this, you can then switch to the root user using the su – command (the – character imports the root user’s environment variables), which will display the root user # prompt:

Root user prompt

Using BASH on Windows 10

You can easily navigate the Ubuntu Linux filesystem using a variety of different commands. All Windows filesystems are mounted under the /mnt directory (e.g., you can access C:\ by navigating to /mnt/c and you can access D:\ by navigating to /mnt/d). Most standard Linux commands that you’ll find on a typical Linux system will work just fine within BASH on Windows 10, including advanced text tools and command piping.

pwd               <--print working directory

ls                <--list files under current directory

ls /              <--list files under the / of the system

cd /mnt/c         <--change current dir to c:\

ll                <--do a long list of files (alias to ls -l)

nano /etc/hosts   <--edit /etc/hosts file (using the easy nano editor)

vi /etc/hosts     <--edit /etc/hosts file (using the powerful vi editor)

grep -i mother letter

This prints all lines that have the word mother (case-insensitive) in the file called letter.

ps -ef | grep -i bash | wc –l

This sends the list of system processes (ps -ef) to the grep command, which then searches for lines with the word bash case-insensitive (grep -i bash), and then sends this list of lines to the wc command, which counts the number of lines (wc -l) and shows you the end number.

You can also access the Ubuntu Linux filesystem from Windows. Simply navigate to the C:\Users\your_username\AppData\Local\lxss directory! However, you must first show hidden and system files within Windows Explorer > View > Options > View tab.

Root user prompt

What Can I Do with BASH on Windows 10 From an IT Perspective?

BASH is primarily for software developers, which is why you enabled it via Developer mode. It comes with several programming languages that developers can use directly on their Win10 computer, including Javascript/node.js, Ruby, Python, C/C++, C# & F#, Rust, Go, Perl, Git and more. However, it boasts many useful features for IT professionals as well! For starters, to manage your remote Linux hosts via Secure Shell (SSH), you can simply open a BASH shell and run the SSH client: ssh user@hostname_or_IPaddress

While Microsoft has not allowed SMB/CIFS/NFS/FTP access within WSL at the time of this writing, you can still use other Linux commands to transfer files. For example, to switch to your Windows desktop, download and extract the contents of my class.tar file, you could use the following commands:

cd /mnt/c/Users/username/Desktop

wget http://triosdevelopers.com/jason.eckert/trios/class.tar

tar -xvf class.tar

ls

If there is a utility that you’d like to download and use that isn’t installed, you can easily install it from an Ubuntu software repository using the Debian package manager. For example, to install the sysstat package, you could use: apt-get install sysstat

Alternatively, to update the software repository information and upgrade all existing packages to the latest version, you could run: apt-get update ; apt-get upgrade

You can also use the file utilities within BASH to process Windows files very quickly. For example, to add the .jpg extension to all of the files within a pictures directory on your Windows Desktop, you could use the following at a BASH prompt:

cd /mnt/c/Users/username/Desktop/pictures

for FILE in *

do

mv $FILE $FILE.jpg

done

Additionally, you could create and use the BASH scripts that you are accustomed to using on your Linux systems within Windows 10. The following command will download a sample backup script that I’ve made:

wget http://triosdevelopers.com/jason.eckert/trios/myscript

You can view the contents of this script (including the comment lines), give it execute permission and use it to back up your pictures directory using the following commands:

less myscript

chmod +x myscript

./myscript         <--note the usage information

./myscript pictures

ls /mnt/c/backup

Finally, you can host certain Linux network services within WSL without having to install Linux within a virtual machine on your Windows 10 host. At the time of this writing, Ubuntu on Win10 only supports running an SSH server and a LAMP (Linux Apache MySQL PHP) server, as these services primarily support developers who want to test Web apps.

To setup LAMP, you must first start BASH in administrator mode – simply search for BASH on your Start menu, right click the BASH shell and Run as administrator:

BASH administrator menu

Next, run the following commands:

su –                            <--switch to root user

apt-get install lamp-server^    <--install all LAMP packages

nano /etc/apache2/apache2.conf  <--edit the Apache configuration file

Add these two lines to apache2.conf, save your changes and quit:

Servername localhost

AcceptFilter http none

/etc/init.d/apache2 start   <--start the Apache daemon

Now, you can use your Web browser on your Win10 machine to navigate to http://127.0.0.1 and view the Ubuntu Apache test page. You can edit this page by editing the /var/www/html/index.html file. Also remember that WSL starts the Ubuntu system only while a BASH shell is open. If you close your BASH shell, you’ll need to start the Apache daemon again the next time you start a BASH shell.

Ubuntu default page

To set up an SSH server, you must first stop/disable the Win10 SSH Server Broker service. Simply open the Services console (type services.msc in the Start menu), locate the SSH Server Broker service and double-click it to Stop and change the Startup type to Disabled:

SSH Server Broker
Server Broker Properties

Next, return to your BASH shell and type the following:

nano /etc/ssh/sshd_config       <--edit the SSH daemon configuration file

Modify the following lines as shown below, save your changes and quit.

           UsePrivilegeSeparation no

           PubkeyAuthenication no

           PasswordAuthentication yes

service ssh start           <--start the SSH daemon

You can now remotely connect to your Windows 10 BASH shell from a remote computer.

Ready to show employers you have the Linux skills needed on the job? Check out CompTIA Linux+.

Email us at [email protected] for inquiries related to contributed articles, link building and other web content needs.

Read More from the CompTIA Blog

Leave a Comment