Interaction between WSL and Windows

1. What is WSL?

​ WSL is the abbreviation of Windows Subsystem for Linux . It is mainly a compatibility layer for natively running Linux binary executable files (ELF format) on Windows 10. In layman's terms, a Linux subsystem (default is ubuntu) is embedded in Windows 10, which is convenient for running most Linux commands and software, such as grep MySQL Apache. This is very convenient for students who use Windows for development, and there is no need for dual systems or virtual machines.

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

2. New features of WSL

Starting from Windows 10 version 1709, you can directly enter wslthe interactive environment, and the bash method will be gradually abandoned.

The previous is bash -c [command]directly wsl [command]replaced by .

Another feature is that other Linux distributions can be downloaded and installed in the Windows 10 store. This way you can choose freely without being restricted to Ubuntu.

Then you can open Ubuntu directly in the program list, or enter ubuntu directly in CMD or Powershell:

PS D:\> ubuntu
mush@mushroom ~ % ls
go  mush  test
mush@mushroom ~ % pwd
/home/mush
mush@mushroom ~ %

The following are based on wsl, Ubuntu, powershellto introduce and demonstrate.

3. WSL management configuration

Windows 10 comes with wslconfigit to manage multiple installed distributions, such as uninstalling a distribution and setting the default launch version.

Entering in PowerShell wslconfig /?, you can see:

PS D:\> wslconfig /?
在 Linux Windows 子系统上执行管理操作

用法:
    /l, /list [/all] - 列出已注册的分发内容。
        /all - 有选择地列出所有分发内容,包括目前
               正安装或未安装的分发内容。
    /s, /setdefault <DistributionName> - 将指定的分发内容设置为默认值。
    /u, /unregister <DistributionName> - 注销分发内容。

Switch the default distribution:

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

After Windows 1803, more configurations are supported. Such as network, root directory, etc. Once in the distribution, it can be /etc/wsl.confconfigured in . If you don't have this file, you can create a configuration manually:

[automount]
enabled = true  # 自动挂载 c:/ 等到 /mnt
root = /windir/
options = "metadata,umask=22,fmask=11"
mountFsTab = false

[network]
generateHosts = true
generateResolvConf = true

4. WSL Interaction

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

PS D:\test>  wsl ls -la
total 5836
drwxrwxrwx 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

Also within WSL you can use Windows applications like notepad, docker:

root@mushroom:/mnt/d/go/src/code.teambition.com/soa/webhooks# docker.exe ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES
63698edb01a8        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

This is a very nice feature that greatly facilitates developers. But in the process of using it, I found that there is a place where the experience is very bad. You must have a .exesuffix, otherwise you will be prompted that the command cannot be found:

root@mushroom:/mnt/d/go/src/code.teambition.com/soa/webhooks# docker
The program 'docker' is currently not installed. You can install it by typing:
apt-get install docker

For example, a colleague wrote a docker buildscript on the mac, and after putting it on Windows, he wanted to use WSL to execute it. He found that a suffix must be added, so the script could not be unified.

5. Solutions

Of course, you can also install a docker in it instead of using the docker on the host. But this would be redundant and perform poorly. After some tossing and found several solutions:

5.1 Using aliases

Set the alias in .bashrc in WSL, remove the suffix:

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

In this way, the command can be run correctly, but the alias is only valid in the interactive environment, and the script execution environment will not work.

5.2 Make an extra copy

Find docker.exe on the host machine, then copy and rename it to docker and put it in the same level directory, so that it can be executed in wsl, which is a bit stupid and cute black magic.

5.3 Redirect

The idea is to define a command_not_found_handlefunction (supported by bash 4.0+) that will be called when any command is not found. Then try to call cmd.exe on the host in this function, which will execute the command and return the result.

Add in .bashrc:

command_not_found_handle() {
    if cmd.exe /c "(where $1 || (help $1 |findstr /V Try)) >nul 2>nul && ($* || exit 0)"; then
        return $?
    else
        if [ -x /usr/lib/command-not-found ]; then
           /usr/lib/command-not-found -- "$1"
           return $?
        elif [ -x /usr/share/command-not-found/command-not-found ]; then
           /usr/share/command-not-found/command-not-found -- "$1"
           return $?
        else
           printf "%s: command not found\n" "$1" >&2
           return 127
        fi
    fi
}

or .zshrcadd in:

command_not_found_handler() {
    if cmd.exe /c "(where $1 || (help $1 |findstr /V Try)) >nul 2>nul && ($* || exit 0)"; then
        return $?
    else
        [[ -x /usr/lib/command-not-found ]] || return 1
        /usr/lib/command-not-found --no-failure-msg -- ${1+"$1"} && :
    fi
}

Use symbolic links to map docker.exe on the host to WSL:

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

6. Others

6.1 Small talk

I haven't blogged for about 2 years. The main reason is that we have switched from C#/Net to Golang-related technology stacks, and we need to re-accumulate and learn. I wrote c++ for a while in the early stage, and then wrote Golang, and found that Golang is much more comfortable to write. Of course, it has a lot to do with being lazy after having a girlfriend.

This is the beginning, I hope that I can continue to share, and it is also conducive to my own growth. The new blog will be synchronized to github for easy backup and modification.

6.2 Reference

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023528&siteId=291194637