Unix tip: Using bash on Windows

Sandra Henry-Stocker

The fairly recent appearance of bash on Windows, more properly known as “bash on Ubuntu on Windows” gives Windows some special appeal to those of us who love living on the command line. And it's not just bash, but bash along with all the tools you're likely to be looking for to get your work done.

Jumping in

I have to admit that, during my first few hours, it felt a little strange to be typing Unix commands while running Windows 10. Even so, the command line was obviously so much more powerful and familiar than using PowerShell, that it was also quite exciting. These hours reminded me what it felt like when I first started working from home on the desktop of a system that sat in a data center at work (several hours away) or when I discovered that I could enjoy a glass of my favorite beer while watching a movie at a local movie theater -- all good things, but sufficiently disconcerting to make me do a quick double take. Wow, it actually works. Using bash on a Windows desktop feels more than a little strange.

The bash command line is provided through something called "WSL" -- the Windows Subsystem for Linux -- and is meant to support developers who rely on tools like sed, awk, and grep. The installation requires that you be using the Windows 10 Anniversary Edition on a 64-bit system and you have to switch to developer mode, but the installation is incredibly easy to do. One reboot later and your Windows system will never quite be the same.

Test driving

When you first open your bash terminal window, you'll be prompted to set up a user account by entering a username and password. Then it's time to jump in and see what it feels like to work on the command line. Keep in mind that this is not the same as using a live distribution; you're going to be running native Linux commands directoy on Windows and creating and editing files on your hard drive.

Look for the "bash on Ubuntu on Windows" option in your command menu. Once you open the tool, you should find yourself in your new Linux home directory.

shs@WINDOWS-3B67H60:~$ pwd

Don't like the prompt? No problem, you know how to fix that. Edit your .bashrc file and add a PS1 setting such as PS1="$ ". Then source the file (. .bashrc) or restart your bash window.

$ pwd
$ whoami

Run a few commands and you'll start to sense the familiarity of this Linux environment. Check the date/time setting. Touch a file and look at the permissions and everything looks fairly normal.

$ date
Thu Mar 16 10:25:43 DST 2017
$ touch myfile
$ ls -l
total 0
-rw-rw-rw- 1 shs shs 0 Mar 16 10:18 myfile

And, of course, you can edit the file with vi.

What else can you do?

As you might expect, you can switch to the root account using the sudo command.

$ which sudo
$ sudo su -
sudo: unable to resolve host WINDOWS-3B67H60
[sudo] password for shs:
root@WINDOWS-3B67H60:~# pwd

Clearly not everything is working properly quite yet. We get an "unable to resolve host" error and the who command (shown below) provides no response at all, but most of the commands that you'd use frequently on the command line are looking quite good.

$ who
$ whoami

check this

$ ls /bin
bash                  kmod              plymouth
bunzip2               less              plymouth-upstart-bridge
busybox               lessecho          ps
bzcat                 lessfile          pwd
bzcmp                 lesskey           rbash
bzdiff                lesspipe          readlink
bzegrep               ln                red
bzexe                 loadkeys          rm
bzfgrep               login             rmdir
bzgrep                loginctl          rnano
bzip2                 lowntfs-3g        running-in-container
bzip2recover          ls                run-parts
bzless                lsblk             sed
bzmore                lsmod             setfont
cat                   mkdir             setupcon
chgrp                 mknod             sh
chmod                 mktemp            sh.distrib
chown                 more              sleep
chvt                  mount             ss
cp                    mountpoint        static-sh
cpio                  mt                stty
dash                  mt-gnu            su
date                  mv                sync
dbus-cleanup-sockets  nano              tailf
dbus-daemon           nc                tar
dbus-uuidgen          nc.openbsd        tempfile
dd                    netcat            touch
df                    netstat           true
dir                   nisdomainname     udevadm
dmesg                 ntfs-3g           ulockmgr_server
dnsdomainname         ntfs-3g.probe     umount
domainname            ntfs-3g.secaudit  uname
dumpkeys              ntfs-3g.usermap   uncompress
echo                  ntfscat           unicode_start
ed                    ntfsck            vdir
egrep                 ntfscluster       which
false                 ntfscmp           whiptail
fgconsole             ntfsdump_logfile  ypdomainname
fgrep                 ntfsfix           zcat
findmnt               ntfsinfo          zcmp
fuser                 ntfsls            zdiff
fusermount            ntfsmftalloc      zegrep
grep                  ntfsmove          zfgrep
gunzip                ntfstruncate      zforce
gzexe                 ntfswipe          zgrep
gzip                  open              zless
hostname              openvt            zmore
ip                    pidof             znew
kbd_mode              ping
kill                  ping6

And, yes, you'll have man pages for your commands. You can also perform system updates.

$ sudo apt-get update
sudo: unable to resolve host WINDOWS-3B67H60
[sudo] password for shs:
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
Ign http://archive.ubuntu.com trusty InRelease
Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:3 http://security.ubuntu.com trusty-security/main amd64 Packages [594 kB]
Hit http://archive.ubuntu.com trusty-backports InRelease
Hit http://archive.ubuntu.com trusty Release.gpg

The expected access permissions work as expected.

$ cd /root
bash: cd: /root: Permission denied
$ sudo su -
sudo: unable to resolve host WINDOWS-3B67H60
root@WINDOWS-3B67H60:~# pwd
root@WINDOWS-3B67H60:~# ls -l
total 0
root@WINDOWS-3B67H60:~# ls -l /bin
total 9792
-rwxr-xr-x 1 root root 1021112 Oct  7  2014 bash
-rwxr-xr-x 3 root root   31152 Oct 21  2013 bunzip2
-rwxr-xr-x 1 root root 1918032 Nov 14  2013 busybox

Windows from the Linux command line

Slide over to /mnt/c and you'll be looking at your Windows system from the Linux point of view.

$ cd /mnt/c
$ ls -l
ls: cannot access hiberfil.sys: Permission denied
ls: cannot access pagefile.sys: Permission denied
ls: cannot access swapfile.sys: Permission denied
total 528
drwxrwxrwx 2 root root      0 Feb 13 14:16 360SANDBOX
drwxrwxrwx 2 root root      0 Dec 15 10:12 $360Section
-r-xr-xr-x 1 root root 395268 Jul 10  2015 bootmgr
-r-xr-xr-x 1 root root      1 Oct 30  2015 BOOTNXT
drwxrwxrwx 2 root root      0 Apr  8  2016 dell
-????????? ? ?    ?         ?            ? hiberfil.sys
drwxrwxrwx 2 root root      0 Apr 27  2016 Logs
drwxrwxrwx 2 root root      0 Apr  8  2016 MININT
-????????? ? ?    ?         ?            ? pagefile.sys
d--------- 2 root root      0 Jul 16  2016 PerfLogs
drwxrwxrwx 2 root root      0 Mar 14 09:21 ProgramData
dr-xr-xr-x 2 root root      0 Mar  5 09:41 Program Files
dr-xr-xr-x 2 root root      0 Mar  5 09:42 Program Files (x86)
drwxrwxrwx 2 root root      0 Jan 17 15:24 Python34
d--------- 2 root root      0 Sep 22 02:41 Recovery
drwxrwxrwx 2 root root      0 Aug 11  2016 $Recycle.Bin
-????????? ? ?    ?         ?            ? swapfile.sys
d--------- 2 root root      0 Mar 14 09:17 System Volume Information
dr-xr-xr-x 2 root root      0 Sep 22 02:42 Users
dr-xr-xr-x 2 root root      0 Mar 15 18:05 Windows

Clearly bash doesn't have much to say about Windows' .sys files, but we can dig a bit further.

You can always wind your way over to your Windows home directory and use Linux commands on your Windows files although here you'll see some additional oddities. Your Windows files don't belong to your Linux user. Instead, they appear to belong to root.

$ pwd
$ cat link.txt
This is a file
$ od -bc link.txt
0000000 124 150 151 163 040 151 163 040 141 040 146 151 154 145 015 012
          T   h   i   s       i   s       a       f   i   l   e  \r  \n
0000020 157 156 145 015 012 164 167 157 015 012 164 150 162 145 145 015
          o   n   e  \r  \n   t   w   o  \r  \n   t   h   r   e   e  \r
0000040 012 146 157 165 162 015 012
         \n   f   o   u   r  \r  \n
$ ls -l link.txt
-rwxrwxrwx 1 root root 39 Mar 15 11:15 link.txt


Much more exploration is possible, but the gist is this. Microsoft's adoption of Linux tools onto Windows -- now roughly a year since its original introduction -- brings Linux commands within easy reach of Windows users and gives open source a claim to some interesting new territory.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.
Take IDG’s 2020 IT Salary Survey: You’ll provide important data and have a chance to win $500.