Introduction
I constantly use the terminal to work with git and automate the build of projects (layout/frontend: pug and scss compilation, image processing, js transpilation...). I have Windows 10 on my work computer and OSX on my laptop, and I wanted to use the same console commands on both systems, so I used GitBash (the terminal that comes with Git) inside the emulator (Cmder). This combination worked noticeably slower than the terminal on OSX, so I was encouraged by the news “The Linux subsystem was built into Windows 10” (March 2020), but then, in 2020, its use was disappointing: it was impossible to run Windows programs from the bash terminal, the subsystem did not detect a file change if the change was made from Windows. Now these problems have been resolved.
Spoiler
The main gain for me is speed. In my starting repository there is a time-consuming task of completely assembling the project (compiling, transpiling, sprites, fonts, images...), with Gitbash it takes 17 seconds, with the WSL bash terminal - 10 seconds.
Files mentioned in the article
- .bashrc
- .dircolors
- .bash_aliases
- .inputrc
- .vimrc
What is WSL?
At the beginning of the Windows 10 development cycle, Microsoft opened a discussion and voting page for new features. There was a conversation about the Windows command line. The developers asked the community what they didn't like about the Windows Command Prompt and what features they would like to see in a new version.
Many users said they wanted some minor improvements to the command line, while others said it would be nice to be able to use Linux/Unix and Bash tools on Windows 10. Many users agreed that it should be easier to use these tools on Windows.
Listening to the voice of the community, Microsoft first improved CMD, PowerShell and other command line tools. And secondly, they did something that would have seemed impossible a few years ago, they added real, native Bash along with support for all the necessary command line tools running directly on Windows, in an environment that behaves like Linux. This is not some virtual machine, this is real Linux on Windows.
To achieve this, Microsoft has built a new infrastructure in Windows, the Windows Subsystem for Linux, or WSL, on which the Ubuntu environment image supplied by partner Canonical runs. This feature will allow developers to use Linux tools more efficiently. The infrastructure is based on a now-abandoned project, Project Astoria, which was intended to be used to run Android applications on Windows. It can be regarded as the opposite of Wine, only Wine runs Windows applications on Linux, the Linux subsystem allows you to run Linux applications on Windows, or more precisely, only Bash console applications on Windows 10.
From a technical point of view, this is not Linux at all. Every GNU Linux system must be based on the Linux kernel, but here it is simply possible to run binaries that run on Ubuntu.
With each new version, WSL has fewer and fewer restrictions, you can already use services, and graphical applications have become available with WSL 2. The solution is intended for developers who want to run Linux utilities from the Windows command line. Yes, these commands have access to the Windows file system, but you cannot use them to automate your tasks or in the standard Windows command line. Now let's figure out how to install WSL on Windows 10.
Basics of using Ubuntu Terminal on Windows 10
This is a subsystem inside Windows, to use nodeJS and git you need to put them in the subsystem (even if they are already installed on Windows). I chose the Ubuntu option, so when installing nodejs I used these instructions, and installed git with the command sudo apt-get install git (don’t forget to tell git your username and email).
You can run Windows applications. Try notepad.exe - Windows notepad will start.
Windows drives are located in /mnt/ . Try cd /mnt/c/ && ls to go to the C:/ drive and output its root directories (by default the output looks dull, but this is configurable).
The user directory is located in ~ (cd ~ to go to it). Just a reminder.
On Linux systems, uppercase and lowercase characters are different characters. When using autocompletion (Tab), it is important to enter characters in the correct case.
My Linux subsystem user directory is physically located along a rather long path (see below). The nicothin segment is the system username (both at the beginning of the path (Windows) and at the end (Ubuntu)).
c:\Users\nicothin\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs\home\nicothin\
If you edit Linux subsystem files from Windows, then Linux in some cases will not be able to access them (insufficient rights). After such editing, you need to run something like chmod 777 ~/.bashrc (where ~/.bashrc is the file edited from Windows) in the subsystem’s bash terminal and restart the terminal. Or you can use the command source ~/.bashrc (specify the edited file) to apply the changes.
How to run Sublime Text from the Ubuntu console
If you are using Sublime Text, add the path to subl.exe to PATH on Windows so that the subl.exe command in the terminal will launch Sublime Text:
- In Explorer, right-click on This PC , select Properties , then Advanced system settings .
- On the Advanced , click the Environment Variables .
- Double click on Path to edit it.
- Click the New and add the path to subl.exe. In my case it is c:\Program Files\Sublime Text 3\.
- Restart your terminal.
VKontakte VKontakte VKontakte VKontakte
How to Install Linux with Desktop Using WSL 2 on Windows 10
Step 1 - Install WSL 2
If you haven't done this before, install Windows Subsystem for Linux (WSL 2) on Windows 10 .
- Open PowerShell as Administrator and run the following command:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
- Once done, press Y to restart your computer, or press N to manually restart it later.
- Download and install the Linux2 kernel update package from https://aka.ms/wsl2kernel .
- Open PowerShell again, type the following command and press Enter:
wsl --set-default-version 2
After completing these steps, your device will start using the new version of Windows Subsystem for Linux 2 by default for existing and new distributions.
Step 2 – Download and install the Linux distribution from the Microsoft Store.
- Go to the Windows Store and install the Kali Linux
Read more in our guide: Download Linux distribution from Microsoft Store
Step 3 - Install Win kex
- Open the Start and launch Kali Linux , configure the distribution by entering a new administrator username and password.
- Next, run the command that will install the Win kex packages:
sudo apt update && sudo apt install -y kali-win-kex
- During the process, you will see a request to select a keyboard layout, make your choice (see the picture below).
- Wait until the packages are installed.
Step 4 - Launch Kali Linux with a graphical desktop shell using WSL 2
- To launch Kali Linux with GUI desktop interface, type the command and press Enter:
kex
- In the console, enter a new administrator password and confirm the actions for the request: “Would you like to enter a view-only password (y/n)?” by pressing the Y or N keys.
- That's it, you will instantly boot to the Kali Linux desktop; Having tested the system a little and I can say that it works perfectly, faster than on a regular virtual machine.
The desktop looks good and is quite functional. Interesting: Kali Undercover - you can find it in the menu or using the command:
kali-undercover
As a result, Linux will become absolutely similar to Windows. To return to the classic look, run this command again.
Setting up the Ubuntu console
Colors
Alas, immediately after installation the console (especially the output of the ls command) looks “bearded” and “wearing a sweater”.
You can add a color settings file (touch ~/.dircolors to create a file) with this content.
It's convenient to add content from Windows, but then after saving the file in a bash terminal, run chmod 777 ~/.dircolors and restart the terminal.
Go to the desired directory at startup
If, when starting the terminal, you want to get into the projects directory, you can enter the command cd /mnt/d/projects/ at the very top of ~/.bashrc (specify the path to the desired directory).
Aliases
Let's add a file (touch ~/.bash_aliases) with this content and restart the bash terminal.
Now using the subl command. Sublime Text will open (the current folder will be shown as a project), and a command like subl filename.txt can quickly open files.
Strange problem with aliases
I set up a bash terminal on two home computers with Windows 10 and on one of them, after adding a file, the terminal displayed 4 messages upon restart: command not found. Because of what, I still didn’t understand (I had access rights and the owner of the file), but I found lines in ~/.bashrc that led to this. This was connecting a file with aliases:
if \[ -f ~/.bash_aliases \]; then. ~/.bash_aliases fi
I solved the problem by commenting out these lines and entering the aliases directly into ~/.bashrc (at the very bottom).
Showing a git branch on the command line
Step 1: add a function at the beginning of ~/.bashrc to show the branch:
git_branch() { git branch 2> /dev/NULL | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' }
Step 2: define a variable responsible for the command line prompt, add to it a display of the result of this function (see .bashrc). You'll get something like:
if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\ [email protected] \h\[\033[00m\]: \[\033[01;34m\] \w\[\033[00m\]\e[32m$(git_branch)\e[0m \$ ' else PS1='${debian_chroot:+($debian_chroot)}\ [ email protected] \h:\w\ $'fi
Minor improvements
There are quite a few actions in the bash terminal that Windows 10 will respond to with a beep (pressing Backspace when there is nothing left to erase, for example). To disable this, create ~/.inputrc with the line set bell-style none .
Vim displays fairly dark text by default, which looks bad against a dark background. You can create ~/.vimrc with the line set background=dark to make the text more readable.
Unification of Win32 and UWP
As part of Project Reunion, announced by Microsoft, the company plans to provide simple integration between the Win32 and Universal Windows Platform (UWP) programming interfaces. providing a single development platform with backwards compatibility.
In other words, now developers will be able to create applications that will work on any device with Windows 10, regardless of the hardware platform and operating system version. It will also be possible to adapt existing applications to the new model.
The first components of the new platform were the native interface design framework WinUI 3 and WebView2, which allows you to integrate web content into any application.
The source code of both libraries is open and available to anyone in the corresponding Github repositories.
- Conference on new communication technologies INTERCOM / Remote edition will be held on October 15
- Short link
Noticed bugs
Working with the clipboard by default is not comfortable (Ctrl + V, Shift + Insert do not work, you have to right-click (!!!) on the window title and select Edit → Paste ). It can be solved by using the terminal not independently, but inside Cmder.
Several times I saw a strange glitch in the display of a multi-line command: as if the characters of the second line were not transferred, but were printed in the first line. Rarely, not critical.
On one of the home computers, saving the history of commands between sessions works, on the other it does not work (although the access rights and the owner of the ~/.bash_history file are the active user and history saving is enabled). Not critical.
Copy to clipboard tools do not work. For example, when setting up SSH for github.com, there are instructions for Linux:
sudo apt-get install xclip #Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`) xclip -sel clip < ~/.ssh/id_rsa.pub # Copies the contents of the id_rsa.pub file to your clipboard
But it is impossible to fulfill it, because... xclip will not work. Solved by using the Windows utility: clip.exe < ~/.ssh/id_rsa.pub
After loading Windows sometimes (only twice) refuses to work. Rebooting helps (this is Windows).
By default, the Windows firewall does not allow access to the server running inside the WSL when a request is made from another device (we launch the development server on a work computer, open its pages from a phone located on the same local network). I haven’t figured out how to treat this, I have to temporarily disable the firewall.
How to access a server running in WSL from third-party devices on the same local network
I write a lot of html pages. During my work, I have a local server running that collects markup, styles, javascript, sprites, etc. It can be convenient to run a server on your main computer and access it using a laptop, phone, tablet (the server is accessible via IP, like https://192.168.1.207:8080). However, the firewall built into Windows 10 blocks such connections by default.
To get to such a server, you need to add a firewall rule:
- Search for “Windows Defender Firewall”, open it, click “Advanced options” on the left.
- In the Firewall Monitor window that opens, select (on the left) “Rules for incoming…”, click “Create a rule” (on the right).
- Select “For port”, click “Next”.
- Select “Specified local ports”, enter the port number (my local server runs on port 8080, that’s what I entered), click “Next”.
- Select “Allow connection”, click “Next”, select profiles (I selected all), “Next”.
- Enter a name and click “Done”.
After this, you will be able to log in from any device (on the same local network) to a server running in WSL on your work computer.
Comfortable use with Cmder
To fine-tune the appearance and comfortable work with the clipboard (the usual combination of Ctrl + V for pasting), it is convenient to use the bash terminal of the Linux subsystem inside cmder (a terminal emulator, inside which you can run one of the terminals installed in Windows). To do this, you need to add the ability to start a WSL console in the Cmder settings:
- Right click on the burger icon in the lower right corner to go to settings.
- In the Startup , select the Auto save/restore opened tabs so that you don’t have to create tabs again during a restart.
- In the Startup , Tasks , check for the presence of the WSL::bash . If it is not there, click the Add/refresh default tasks... and agree to add tasks.
- WSL::bash item will appear in the task list ; when creating a new terminal tab, you need to select it.
WSL::bash item in the list of tasks , but WSL does not start when you select it (at the time of creating a new Cmder tab), then in the settings you can try clicking Add/refresh default tasks... and agree to update the list of tasks (see p. 3). If after this you cannot launch the subsystem terminal (Cmder reports a problem and writes that pressing Enter will close the tab), then you can add your own task, which will launch cmd.exe and immediately call the bash command in it. The terminal launch command will look like this:
*cmd /k ""%ConEmuDir%..init.bat" " && bash
Linux Emulator on Windows and Windows on Linux
There is a class of programs that allow you to run programs from other operating systems (not necessarily Windows) on Linux and other UNIX systems.
These are emulators - programs that allow, roughly speaking, to emulate Linux on Windows or vice versa. Among them, the most frequently used ones should be highlighted.
1. WINE open source, GPL. Wine Is Not a (CPU) Emulator is a layer that translates Win32API into the POSIX API, that is, a free implementation of the Windows API that allows you to run Windows programs, translating program calls under Windows into Linux calls. This allows programs to run at almost full speed. However, due to the fact that Microsoft products are closed, it is impossible to accurately reproduce the structure of their API, which leads to all sorts of glitches and oddities when running Windows programs in WINE. The positive thing is that you don't need to install Windows - just WINE and the program. Pros: fairly fast work on real machine resources, work with real directories and files, no need to install Windows. Cons: not all applications are supported and do not always work stably. Links: https://www.winehq.org https://gazette.linux.ru.net/team/a_kuprin.html https://linuxshop.ru/linuxbegin/print633.html
1.1 CodeWavers CrossOver Office closed source. A commercial product based on WINE, characterized by the best performance of especially important Windows applications, such as Adobe Photoshop, Microsoft Office, Autodesk AutoCAD and some others. In general, a more stable, although not the latest, version of WINE, with a graphical installer that will give you a convenient and simple interface to configure. Links: https://www.codeweavers.com
1.2 TransGaming Technologies WineX / Cedega
proprietary, but it is possible to download the sources.
Also based on WINE, but with one very significant improvement, especially for gamers, it contains improved, and in addition, full (in terms of compatibility) support for the Direct3D
. Links: https://www.transgaming.com
2. DOSEMUopensource, GPL. dosemu is a DOS emulator for running legacy applications, often used for accounting and other very specific needs. Many DOS programs used its “features” and bugs to work, which may not be present in dosemu. Pros: quick launch and operation of outdated but necessary DOS programs (accounting, for example), not demanding on resources. Cons: non-trivial installation, not all programs will work. Links: www.dosemu.org
3. Win4Lin closed source. A full copy of Windows is placed in the file, which for Windows will be a disk, and then the necessary applications are installed there. The old version of Win4Lin allowed you to run Windows 95 at a very decent speed and work with applications for these systems in Windows, but this requires kernel patches. Not all versions of kernels have patches, and certain versions of Win4Lin had their own patches - you had to guess. Win4lin PRO does not require a patch, it allows you to run Windows2000/XP, but it works slowly and at the same time loads utilities to “speed it up”. There are suspicions that this emulator uses the developments of the author of QEMU and especially its accelerating module. A special feature of this emulator is that it is possible to work with real Linux directories. Pros: ability to run Windows. Cons: problems with applying patches to the kernel (closed and possibly unsafe), or very low startup speed of Windows, the need to install Windows, the inability to emulate other operating systems. Links: https://www.win4lin.com/ https://www.netraverse.com/ https://sys.net.ru/docs/win4lin/Win4Lin6/ https://sys.net.ru/docs/ win4lin/win4lin2debian
4. Virtual machines Simulate the entire computer along with the hard drive (in the form of a file), CD-ROM and USB (all this is connected to the hardware of a real computer). The OS installed there will work with real hardware only through emulated hardware. Virtual machines are what are usually called a “Linux emulator” or “Windows emulator” - with the help of virtual machines you can actually emulate Linux or Windows.
4.1 QEMU open source, GPL. QEMU is a virtual machine. You can install almost any OS in QEMU - any Windows (from Windows95 to WindowsXP inclusive), any Linux distribution or FreeBSD, BeOS/Haiku, ReactOS, Syllable, etc. To work quickly, you need kqemu, an accelerating module (also under GPL), which is available on the author’s website. This will require kernel headers - mainly to assemble the kqemu accelerating module, with which the guest OS will slow down by up to 1.5 times (instead of 5-10 times without the module). This emulator uses the work of another project, BOCHS
. It also allows you to create virtual machines using your own qemu-img utility. Working with the emulator (calling, setting parameters) occurs in console mode, but there are also graphical shells. Pros: good operating speed (with kqemu) of the guest OS, stability, ample capabilities (emulation of several platforms, simultaneous launch of multiple systems, emulation accuracy), protected environment. Cons: somewhat slower emulation speed than VMware, console interface (although there are graphical shells like Qemulator). Links: forum about QEMU www.qemu.org
4.2 BOCHS open source, GPL. BOCHS is a virtual machine, an exact emulator of the x86 platform. It has its own BIOS, VGABIOS, etc., which is what QEMU uses. Very accurate, but very slow, used mainly by developers who need confidence that this is a glitch in the program and not the emulator. Pros: full accurate x86 emulation, ability to install almost any OS on it, easy and simple installation in Debian. Cons: very low speed of the guest OS. Links: www.bochs.org
4.3 VMWare closedsource. VMWare is, like QEMU, a virtual machine. It has a higher emulation speed and supports more operating systems. Additionally, installing VMWare on Debian is not trivial, although there are deb packages in unofficial repositories; requires compilation of proprietary accelerating modules with kernel headers. There are reports that VMWare has problems with full screen mode. In terms of resource requirements, it differs little from QEMU, consuming a lot of RAM and CPU time. The capabilities are the same: a safe environment for emulating guest operating systems. Plus a graphical interface for creating virtual machines. Pros: faster guest OS emulation, there is a graphical interface for creating virtual machines. Cons: Closed proprietary modules can potentially throw the host OS out of a stable state. Links: https://www.vmware.com/ https://www.nixp.ru/news/6914 https://software.newsforge.com/print.pl?sid=05/04/27/186203
4.4 VirtualBox open source, GPL. VirtualBox is also an x86 virtual machine, based on the developments of QEMU
.
It features a simple graphical interface and very high emulation speed (often faster than QEMU
) and peripheral support (USB, network, FireWire).
Emulation accuracy is lower than QEMU
(cannot run many OSes, such as BeOS/Haiku, MenuetOS, Syllable).
There are guest additions that allow very necessary things, namely copy-paste from the host system to the guest system (clipboard sharing) and invisible mouse capture. Pros: easy installation and configuration, functionality can be expanded with plugins, wide support for peripherals, in some cases faster than QEMU
. Cons: the graphical interface gets in the way when used intensively; the accuracy of emulation of less common operating systems is not high. Links: https://www.virtualbox.org/
4.4 KVM - Kernel-based Virtual Machine open source, GPL. KVM is a full virtualization solution, although it is itself a Linux kernel module. Requires a modified version of QEMU
. Requires processor support for virtualization. Pros: accelerator module in the core, easy installation. Cons: Requires processor virtualization support. Links: https://kvm.qumranet.com/kvmwiki https://inf.by/linux/211/
5. Opensource hypervisors, GPL. XEN is a hypervisor that allows you to run a guest OS as a kernel process on the host system. It features very low overhead costs (from 2 to 8% loss in guest OS performance), protection of the host OS, and the use of virtualization technologies of modern processors. However, in order to use its capabilities, a kernel patch is required not only for the host OS, but also for the guest OS, which makes running proprietary systems inside a very difficult task. Pros: potentially the highest achievable operating speed of the guest OS, lower resource requirements of the host OS. Cons: very non-trivial installation - a patch to the guest OS kernel is required. Links: https://www.cl.cam.ac.uk/Research/SRG/netos/xen/ https://wiki.xensource.com/xenwiki/XenFaq https://www.howtoforge.com/book/print /1169 https://xgu.ru/xen/manual/
6. Virtualization at the operating system level opensource, GPL. Open VZ is a “lightweight” virtualization technology that allows you to run several different versions of Linux on one hardware system with one kernel (supporting ovz and contained, as a rule, on a small root with limited access via ssh and without other services). Unlike “heavy” options like Xen, Qemu or VMware, the overhead costs are usually a few percent.