Blog - Bash on Ubuntu on Windows

Added on Saturday, 2017-04-22 11:06 CEST in category Programming
With Windows 10's Creators Update came installed the Windows Subsystem for Linux, which allows Linux binaries to run natively on Windows 10. I'd been considering for a while to set up dual booting with Ubuntu, but since there I mainly use the console anyway, I figured I'd instead give Bash on Ubuntu on Windows a go.

My goal was to make Bash on Windows run as much as possible like Bash on Ubuntu's xterm, and although I'm lacking a few small features, after a bit of tweaking the overall experience has been very good!

Fixing the colors

I find the colors Windows shells use too dark, which tends to make text hard to read (especially blue). You can try out my registry fixes to make the colors a bit lighter, or you can pick from a large selection of alternative color schemes.

You may notice your new color scheme isn't being applied if you open Bash through a shortcut. That's because shortcuts contain their own color schemes, so you'll have to regenerate it. Also, do note that the color codes you input aren't strictly what you'll end up seeing in the shell; they're somehow always a bit skewed…

Alternatively you can change the colors applied by the current shortcut by right-clicking the bash window and selecting Properties, then Color. Do make sure to reselect black as the Screen Background color.

Setting up screen

The folder /var/run/screen that screen requires to run cannot be created by the default user, and therefore screen won't start… To make matters worse /var/run/ is deleted every time all Bash instances are closed. One solution would be to always first run screen as root, but instead my more permanent solution looks like this:
  • Create a /home/nieko/env/scripts/ containing:
    #!/usr/bin/env bash
    mkdir -p /var/run/screen
    chmod 777 /var/run/screen
    chgrp utmp /var/run/screen
    touch /var/run/utmp
    chmod 777 /var/run/utmp
    chgrp utmp /var/run/utmp
  • Add
    %sudo ALL=NOPASSWD: /home/nieko/env/scripts/
    to your sudo file (e.g. using `visudo`).
  • Create an
    alias screen="sudo ~/env/scripts/; /usr/bin/screen \"$@\""
    in ~/.bashrc.

Launching screen when opening Bash

Even though that works pretty well, in a shell I pretty much always use screen anyway, so why not have it start when I open Bash?

By default Bash on Windows doesn't read ~/.profile when opening a login shell, but you can supply it an init file as such:
C:\Windows\System32\bash.exe ~ --init-file /home/nieko/env/scripts/
I recommend placing this in your shortcut to Bash. The script contains pretty much the same as the alias above:
#!/usr/bin/env bash
sudo ~/env/scripts/
exec /usr/bin/screen "$@"

Tweaking the shortcut

Lastly I made a few tweaks to the shortcut "Bash on Ubuntu on Windows.lnk":
  • Add a Shortcut key: CTRL + ALT + T, so that Bash starts exactly like on Ubuntu.
  • Restore the lost icon by pressing Change Icon and selecting %USERPROFILE%\AppData\Local\lxss\bash.ico. If that cannot be found, look for it in C:\Program Files\WindowsApps\CanonicalGroupLimited.UbuntuonWindows_smth_smth_here\images\icon.ico.
  • Under Options enable all the Edit Options and Text Selection checkboxes. This makes for a somewhat better copy/paste mechanism.
  • Under Font increase the Size to 16.

Not fully there yet…

Overall Bash works surprisingly similar to the way it does on Ubuntu, including key bindings and all (!), allowing me e.g. to use the exact same .rc files for mutt, screen, vim and bash. A few things could still use some attention, though:
  • URLs aren't clickable, so I have to copy/paste them instead.
  • Copying and pasting is a bit awkward: screen has mouse support, so in order to select text I need to Shift-select it, and then Shift-right-click to copy. Another Shift-right-click then pastes what was copied. Ctrl-C and Ctrl-V don't really work well.
  • I can't seem to easily increase/decrease the font size, like Ctrl++ and Ctrl+- do in xterm.
Besides these (relatively minor) drawbacks, Bash on Ubuntu on Windows works very well for my needs, and saves me the bother of keeping a dual-boot system :)