WSL and Windows interaction practices

Source: Internet
Author: User
Tags symlink etcd

    • 1. What is WSL?
    • 2. WSL new features
    • 3. WSL Management configuration
    • 4. WSL interaction
    • 5. Solution
      ? * 5.1 Using aliases
      ? * More than 5.2 copies one copy
      ? * 5.3 redirect
      ? * 5.4 Symlink
    • 6. Other
      ? * 6.1 Chat
      ? * 6.2 References
1. What is WSL?

? WSL is the short name of Windows Subsystem for Linux , primarily to run Linux binary executables (elf format) natively on Windows 10, while providing a compatibility layer. In layman's terms, embedded in the WINDOWS10 Linux subsystem (default is Ubuntu), easy to run most of the Linux commands and software, such as grep MySQL Apache . This is very convenient to use Windows to develop the classmate, do not need a dual system or virtual machine.

在Windows功能中启用```适用于Linux的Windows子系统```,然后在Windows CMD中直接输入```bash```,即可进入Linux环境,执行命令:

2. WSL new features

Starting with the WINDOWS10 1709 release, you can enter wsl into the interactive environment directly, and bash will gradually be discarded.

Used as a bash -c [command] direct wsl [command] substitute.

Another feature is: in the Windows 10 store, you can download and install other Linux distributions. This gives you the freedom to choose without limiting to Ubuntu.

You can then open Ubuntu entry directly in the Programs list, or enter Ubuntu directly into CMD or PowerShell:

PS D:\> ubuntu[email protected] ~ % lsgo  mush  test[email protected] ~ % pwd/home/mush[email protected] ~ %

The following are all based on, wsl Ubuntu powershell to introduce and demonstrate.

3. WSL Management configuration

Windows10 comes with the wslconfig management of multiple installed distributions, such as uninstalling a release and setting the default startup hairstyle version.

Enter in PowerShell wslconfig /? and you can see:

PS D:\> wslconfig /?在 Linux Windows 子系统上执行管理操作用法:    /l, /list [/all] - 列出已注册的分发内容。        /all - 有选择地列出所有分发内容,包括目前               正安装或未安装的分发内容。    /s, /setdefault <DistributionName> - 将指定的分发内容设置为默认值。    /u, /unregister <DistributionName> - 注销分发内容。

To toggle the default release version:

PS D:\> wslconfig /l# 适用于 Linux 的 Windows 子系统:Legacy (默认)UbuntuPS D:\> wslconfig /s UbuntuPS D:\> wslconfig /l# 适用于 Linux 的 Windows 子系统:Ubuntu (默认)Legacy

After Windows 1803, more configurations are supported. such as the network, root directory and so on. After you enter the release version, you can configure it in /etc/wsl.conf . If you do not have the file, you can manually create a configuration:

[automount]enabled = true  # 自动挂载 c:/ 等到 /mntroot = /windir/options = "metadata,umask=22,fmask=11"mountFsTab = false[network]generateHosts = truegenerateResolvConf = true
4. WSL interaction

Also starting from 1709, WSL supports the direct use of Linux commands on Windows 10:

PS D:\test>  wsl ls -latotal 5836drwxrwxrwx 1 root root    4096 Jan 25 13:20 .drwxrwxrwx 1 root root    4096 Apr 20 16:25 ..-rwxrwxrwx 1 root root     105 Oct 14  2017 03-build.ps1

Windows applications can also be used within WSL, such as Notepad,docker:

[email protected]:/mnt/d/go/src/code.teambition.com/soa/webhooks# docker.exe psCONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES63698edb01a8        quay.io/coreos/etcd:latest   "/usr/local/bin/etcd"    2 days ago          Up 27 hours         0.0.0.0:2379->2379/tcp, 2380/tcp                                                             etcd

It's a great feature and it's great for developers. But in the use of the process found that there is a very bad experience, you must bring a .exe suffix, or you will not be prompted to find the command:

[email protected]:/mnt/d/go/src/code.teambition.com/soa/webhooks# dockerThe'docker' is currently not installed. You can install it by typing:apt-get install docker

For example, a colleague wrote a script on a Mac docker build , put it on windows and wanted to use WSL to do it, and found it had to be suffixed, so the script couldn't be unified.

5. Solution

Of course, you can also load Docker instead of using Docker on the host. But this can be very redundant, and performance is not good. After some tossing and finding solutions:

5.1 Using aliases

In WSL. BASHRC set the alias, remove the suffix:

alias docker=docker.exealias docker-compose=docker-compose.exe

This will allow the command to run correctly, but the alias is only valid in the interactive environment and the script execution is bad.

More than 5.2 copies a copy

Find Docker.exe on the host, and then copy the rename to Docker and put it in the sibling directory so that it can be executed in WSL, a bit of a silly black magic feeling.

5.3 Redirects

The idea is to define the command_not_found_handle function (bash 4.0+ support), which is called when any command is not found. The function then tries to call the host on the Cmd.exe, which comes in to execute the command and returns the result.

Added in. BASHRC:

Command_not_found_handle () {    if Cmd.exe/C"(Where $ || ( Help $|findstr/v Try)) >nul 2>nul && ($*| | exit 0) ";  Then        return $?    Else        if [ - x/usr/lib/command-not-found ]; Then           /usr/lib/command-not-found--" $"           return $?        Elif [ - x/usr/share/command-not-found/command-not-found ]; Then           /usr/share/command-not-found/command-not-found--" $"           return $?        Else           printf "%s:command not found\n" " $" >&2           return127fi    fi}

Or .zshrc Add in:

Command_not_found_handler () {    if Cmd.exe/C"(Where $ || ( Help $|findstr/v Try)) >nul 2>nul && ($*| | exit 0) ";  Then        return $?    Else        [[ - x/usr/lib/command-not-found ]] || return1/usr/lib/command-not-found--no-failure-msg--${1+" $"} && :    fi}
5.4 Symlink

Using symbolic connections, the Docker.exe on the host hosts are mapped to WSL:

ln -sf /mnt/c/Program\ Files/Docker/Docker/resources/bin/docker.exe /usr/bin/docker
6. Other 6.1 Chats

Almost 2 years or so, no blog. Mainly because from c#/net, turn to Golang related technology stack, need to accumulate and learn under. Write a period of time C + +, and then write Golang, found Golang write more comfortable. Of course, after having a girlfriend, getting lazy also has a lot of relationship.

This is the beginning, hope to continue to share, but also conducive to their own growth. The new blog will be synced to GitHub for easy backup changes.

6.2 References

Https://docs.microsoft.com/en-us/windows/wsl/interop

Https://docs.microsoft.com/en-us/windows/wsl/wsl-config

https://github.com/Microsoft/WSL/issues/2003

WSL and Windows interaction practices

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.