My website/blog hosted on Codeberg pages.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
12 KiB

<!DOCTYPE html>
<html xmlns="" lang="en-us" xml:lang="en-us">
<title>No Linux? Windows Subsystem for Linux!</title>
<meta name="owner" content="Michael Zeevi">
<meta name="designer" content="Michael Zeevi">
<meta name="author" content="Michael Zeevi">
<meta name="copyright" content="Michael Zeevi">
<meta name="date" content="2021-08-05">
<meta name="revised" content="2021-08-05">
<meta name="keywords" content="wsl, linux, bash, bashrc, virtualization">
<meta name="generator" content="pandoc"> <!-- template by Michael Zeevi -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="language" content=”en-us”>
<meta http-equiv="content-language" content=”en-us”>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta charset="utf-8">
<link rel="stylesheet" href="res/styles.css">
<link rel="stylesheet" href="" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<header class="wrapper">
<h1 id="site-title"><a href="index.html"></a></h1>
<ul id="site-menu">
<li><a href="index.html">Home</a></li>
<li><a href="blog.html">Tech Blog</a></li>
<li><a href="links.html">Links</a></li>
<li><a href="photography.html">Photography</a></li>
<div id="colorscheme">
<a id="colorscheme-toggle">
<i class="fas fa-fw fa-moon"></i><i class="fas fa-fw fa-toggle-on" id="colorscheme-toggle-switch"></i><i class="fas fa-fw fa-sun"></i>
<script src="res/colorscheme.js"></script>
<hr class="hidden-on-normal-displays">
<h1 id="content-title">No Linux? Windows Subsystem for Linux!</h1>
<p class="content-header author">Michael Zeevi</p>
<p class="content-header date"><time datetime="2021-08-05">2021-08-05</time></p>
<h2 id="intro">Intro</h2>
<p>This post will focus on <em>Windows Subsystem for Linux</em> (WSL), its advantages (over a conventional VM running Linux), setting it up and some tips for getting started with it.</p>
<h2 id="background">Background</h2>
<p>Sometimes you want your Linux environment, but for security reasons your workplace/company/customer enforces usage of standard issue “uniform” Windows computers (not that Linux is not secure, just that IT teams often choose to <em>not</em> manage multiple security tools &amp; policies for different operating systems). In these cases we have <strong>a few options</strong>...</p>
<p>One option is creating a “classic” Virtual Machine (using <strong><em>Virtual Box</em></strong>, for example), reserving a portion of your host system’s resources (memory, storage, etc.), and installing a full Linux image (such as Ubuntu) on it.</p>
<p>Another option, is to use <strong>WSL</strong> with a Linux distribution (such as Ubuntu) from the Microsoft Store, which we’ll discuss below.</p>
<p>Note: I won’t touch options like CygWin or MinGW, because they are riddled with differences such as not actually supporting Linux packages (instead they need them to go through modifications before even being compiled).</p>
<h2 id="advantages">Advantages</h2>
<p>Some of the advantages that make WSL standout:</p>
<li>Has very low memory demands - it doesn’t “block” a portion of the host system’s memory. Instead, it shares it - <em>similarly</em> to how a Docker container behaves (this can be demonstrated by running <code>free -h</code> in WSL and seeing the output list your host’s <em>full</em> memory).</li>
<li>Uses a special VM tailored for its purpose and allowing it to be very smooth, lightweight and responsive.</li>
<li>Natively integrates with Windows host - for example its filesystem is automatically mounted and available from within the Linux distribution off that bat (without any special configuration).</li>
<li>Natively <a href="">integrates with Docker</a> for Windows.</li>
<h2 id="caveats">Caveats</h2>
<p>Nothing is perfect (not <em>even</em> Linux), definitely not WSL (it runs on Windows after all..), so here are some slight disadvantages:</p>
<li>Doesn’t support GUI based applications (hopefully you love the CLI like I do!) out of the box (but it <a href=""><em>is</em> possible with specific drivers</a>).</li>
<li>Cannot run 32-bit applications (i.e. relics of the past).</li>
<h2 id="setup">Setup</h2>
<p>WSL is a Windows feature that must be enabled. Since it uses a VM behind the scenes, one must also <a href="">enable hardware <strong>virtualization</strong></a> in your computer’s BIOS.</p>
<p>Setup steps (assuming virtualization is already enabled in BIOS):</p>
<ol type="1">
<li><p>Run <strong>Powershell</strong> as Administrator and input the following commands:</p>
<pre><code>dism.exe /online /enable-feature /all /norestart /featurename:VirtualMachinePlatform
dism.exe /online /enable-feature /all /norestart /featurename:Microsoft-Windows-Subsystem-Linux</code></pre></li>
<li><p>Open the <em>Microsoft Store</em> (you can search for it from <em>Start Menu</em>), search for and install your desired <strong>Linux distribution</strong> (Debian or Ubuntu are solid choices) and <strong>Windows Terminal</strong> (which is a more feature-rich client than the default client - for example, it allows opening multiple tabs).</p></li>
<li><p>Run Windows Terminal and go into <em>Settings</em>. Under <em>Startup</em>, set <strong><em>Default profile</em></strong> to the Linux distribution you got last step.</p></li>
<li><p>Still in <em>Settings</em>, under <em>Profiles</em> select your Linux distribution and set the <strong><em>Starting directory</em></strong> to <code>//wsl$/&lt;YOUR_DISTRO_NAME&gt;/home/&lt;YOUR_USER_NAME&gt;</code> (this will make WSL boot into your Linux home directory, insted of the [default] Windows one).</p></li>
<li><p>The first time you launch your new WSL distribution it will run its setup and prompt you for setting a <strong>root user</strong> password, and creating your <strong>own user</strong>. Do this.</p></li>
<h2 id="follow-up-actions-and-tips">Follow-up actions and tips</h2>
<p>After running your new WSL Linux distribution for the first time, here are a few useful things to do:</p>
<li><p>Install common packages normally. Here are some useful ones if you are on Debian based distributions:</p>
<pre><code>sudo apt install -y \
bash-completion \
dnsutils \
git \
iputils-ping \
iproute2 \
jq \
man-db \
python3 \
python3-pip \
<li><p>Symbolically link the main folders from your Windows home directory (which is case-sensitive) to your Linux home directory. I suggest linking <em>Documents</em> and <em>Downloads</em>:</p>
<pre><code>ln -s /mnt/c/Users/${YOUR_WINDOWS_USERNAME}/Documents/ ~/Documents
ln -s /mnt/c/Users/${YOUR_WINDOWS_USERNAME}/Downloads/ ~/Downloads</code></pre>
<p>Note the automatically mounted <code>C:</code> drive from Windows under your Linux filesystem’s <code>/mnt/c/</code>.</p>
<li><p>Configure your organization’s proxy (and exclude list), by appending to the <code>/etc/environment</code> file (replace with your organization’s proxy details):</p>
<pre><code>export HTTP_PROXY=http://your.organization.proxy:8080/
export HTTPS_PROXY=https://your.organization.proxy:8443/
export NO_PROXY=$(hostname),localhost,,,</code></pre></li>
<li><p><strong><em>Bonus tip</em> (relevant to <em>any</em> system migration, not just WSL):</strong> I <em>personally</em> like (and recommend) to manage one configuration file for my Linux devices. A common practice is to keep it <a href="">backed up on a Git server</a>.<br> I call this file my <code>~/.userrc</code> (replace “user” with <em>your name</em> or <em>initials</em>), and I source it from the main <code>~/.bashrc</code> file by adding the line <code>[ -f ~/.userrc ] &amp;&amp; . ~/.userrc</code> (which sources it, only <strong>if</strong> it exists). The actual contents of your personal configuration file is up to you, here is an example of mine:</p>
<pre><code># this file should be sourced by adding the following expression to ~/.bashrc:
# [ -f ~/.mzrc ] &amp;&amp; . ~/.mzrc
# env
export EDITOR=vi
export VISUAL=vim
export AWS_PROFILE=${AWS_PROFILE} # keep same aws profile as parent shell
# aliases
alias ll=&#39;ls -hlF --group-directories-first --color=auto&#39;
alias diff=&#39;diff --color=auto&#39;
alias whatismyip=&#39;curl;
alias cat=&#39;bat -pp&#39; # get bat from
alias k=kubectl
# completion
source &lt;(helm completion bash)
source &lt;(kubectl completion bash)
complete -F __start_kubectl k
complete -C aws_completer aws
complete -C terraform terraform</code></pre></li>
<h2 id="additional-info">Additional info</h2>
<li>Some useful WSL commands (for Powershell) - especially if you’ve got more than one Linux distribution:
<li><code>wsl --list --verbose</code> lists distros, their states, and VM version (make sure your default version is 2).</li>
<li><code>wsl --distribution &lt;distro&gt;</code> runs a specific distro (else will run the default one).</li>
<li><code>wsl --terminate &lt;distro&gt;</code> shuts down/restarts the distro.</li>
<li><code>wsl --unregister &lt;distro&gt;</code> removes a distro from WSL.</li>
<li>Full and <a href="">official WSL documentation</a>.</li>
<h2 id="conclusion">Conclusion</h2>
<p>As much as we (or at least, I) strive to be at home and in our Linux systems, sometimes we are forced to work in slightly less “ideal” conditions...</p>
<p>If you need to work with a Linux system from within a Window host, I recommend using WSL. It is as close to a native Linux experience as one can get, from within Windows, without coming on expense of other criteria such as performance.</p>
<p>WSL is one of the best ways I’ve found for achieving this.</p>
<hr class="hidden-on-normal-displays">
<ul id="social">
<li><a rel="author" target="_blank" href="" title="Mastodon:" ><i class="fab fa-fw fa-mastodon" ></i><div class="hidden-on-tiny-displays"> Mastodon</div></a></li>
<li><a rel="author" target="_blank" href="" title="Pixelfed:"><i class="fas fa-fw fa-camera" ></i><div class="hidden-on-tiny-displays"> Pixelfed</div></a></li>
<li><a rel="author" target="_blank" href="" title="Codeberg: maze" ><i class="fas fa-fw fa-code-branch"></i><div class="hidden-on-tiny-displays"> Codeberg</div></a></li>
<li><a rel="author" target="_blank" href="" title="E-mail: Michael Zeevi" ><i class="fas fa-fw fa-envelope" ></i><div class="hidden-on-tiny-displays"> E-mail</div></a></li>
<li><a rel="author" target="_blank" href="res/pgp.asc" title="PGP: Public key block" ><i class="fas fa-fw fa-fingerprint"></i><div class="hidden-on-tiny-displays"> PGP</div></a></li>
<li><a rel="author" target="_blank" href="" title="Keyoxide"><i class="fas fa-fw fa-key" ></i><div class="hidden-on-tiny-displays"> Keyoxide</div></a></li>
<p id="license" xmlns:cc="" xmlns:dct="">
<a href="index.html" property="dct:title" rel="cc:attributionURL"></a> by
<a target="_blank" href="" property="cc:attributionName" rel="cc:attributionURL dct:creator">Michael Zeevi</a> is licensed under
<a target="_blank" href="" rel="license noopener noreferrer">CC BY-SA 4.0</a>