wsl linux 桌面_(Windows)桌面上Linux年-WSL技巧和窍门

wsl linux 桌面_(Windows)桌面上Linux年-WSL技巧和窍门_第1张图片

wsl linux 桌面

I've been doing a ton of work in bash/zsh/fish lately - Linuxing. In case you didn't know, Windows 10 can run Linux now. Sure, you can run Linux in a VM, but it's heavy and you need a decent machine. You can run a shell under Docker, but you'll need Hyper-V and Windows 10 Pro. You can even go to https://shell.azure.com and get a terminal anywhere - I do this on my Chromebook.

最近,我在bash / zsh / fish中做了大量工作-Linuxing 。 如果您不知道,Windows 10现在可以运行Linux。 当然,您可以在VM中运行Linux,但是它很重,您需要一台像样的机器。 您可以在Docker下运行Shell,但需要Hyper-V和Windows 10 Pro。 您甚至可以访问https://shell.azure.com并在任何地方找到终端-我是在Chromebook上完成此操作的。

But mostly I run Linux natively on Windows 10. You can go. Just open PowerShell once, as Administrator and run this command and reboot:

但是大多数情况下,我都是在Windows 10上本机运行Linux。可以。 只需以管理员身份打开一次PowerShell,然后运行以下命令并重新启动:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Then head over to the Windows Store and download Ubuntu, or Debian, or Kali, or whatever.

然后转到Windows应用商店并下载Ubuntu,Debian,Kali或其他任何软件。

  • Ubuntu

    的Ubuntu

  • OpenSUSE

    OpenSUSE

  • SLES

    SLES

  • Kali Linux

    卡利Linux

  • Debian GNU/Linux

    Debian GNU / Linux

What's happening is you're running user-mode Linux without the Linux Kernel. The syscalls (system calls) that these un-modified Linuxes use are brokered over to Windows. Fork a Linux process? It a pico-process in Windows and shows up in the task manager.

发生的事情是您正在运行没有Linux内核的用户模式Linux。 这些未修改Linux使用的syscall(系统调用)被代理到Windows。 分叉一个Linux进程? 它在Windows中是一个微微过程,并显示在任务管理器中。

Want to edit Windows files and edit them both in Windows and in Linux? Keep your files/code in /mnt/c/ and you can edit them with other OS. Don't use Windows to "reach into the Linux file system." There be dragons.

是否要编辑Windows文件并在Windows和Linux中进行编辑? 将文件/代码保存在/ mnt / c /中,然后可以使用其他操作系统进行编辑。 不要使用Windows来“进入Linux文件系统”。 有龙。

wsl linux 桌面_(Windows)桌面上Linux年-WSL技巧和窍门_第2张图片

Once you've got a Linux installed (or many, as I do) you can manage then and use them in a number of ways.

一旦您安装了Linux(或者,我安装了许多Linux),就可以进行管理并以多种方式使用它们。

Think this is stupid or foolish? Stop reading and keep running Linux and I wish you all the best. More power to you.

认为这是愚蠢还是愚蠢? 停止阅读并继续运行Linux,祝您一切顺利。 给您更多的力量。

Want to know more? Want to look new and creative ways you can get the BEST of the Windows UI and Linux command line tools? Read on, friends.

想知道更多? 是否想以新颖的方式获得Windows UI和Linux命令行工具的精华? 继续阅读,朋友们。

wslconfig (wslconfig)

WSL means "Windows Subsystem for Linux." Starting with the Windows 10 (version 1709 - that's 2017-09, the Fall Creators Update. Run "Winver" to see what you're running), you've got a command called "wslconfig." Try it out. It lists distros you have and controls which one starts when you type "bash."

WSL的意思是“ Linux的Windows子系统”。 从Windows 10(版本1709-2017-09,秋季创作者更新。从Windows 10开始,运行“ Winver”以查看运行的内容),您将获得一个名为“ wslconfig”的命令。 试试看。 它列出了您拥有的发行版,并控制您在键入“ bash”时启动哪个发行版。

Check out below that my default for "bash"  is Ubuntu 16.04, but I can run 18.04 manually if I like. See how I move from cmd into bash and exit out, then go back in, seamlessly. Again, no VM.

在下面查看我的“ bash”默认值为Ubuntu 16.04,但是如果愿意,我可以手动运行18.04。 看看我如何从cmd移入bash并退出,然后无缝返回。 同样,没有虚拟机。

C:\>wslconfig /l /all
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
Ubuntu-18.04
openSUSE-42
Debian
kali-rolling

C:\>wslconfig /l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
Ubuntu-18.04
openSUSE-42
Debian
kali-rolling

C:\>bash
128 → $ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
128 → $ exit
logout

C:\>ubuntu1804
scott@SONOFHEXPOWER:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04 LTS
Release: 18.04
Codename: bionic
scott@SONOFHEXPOWER:~$

You can also pipe things into Linux commands by piping to wsl or bash like this:

您还可以通过将管道输送到wsl或bash将管道输送到Linux命令中,如下所示:

C:\Users\scott\Desktop>dir | wsl grep "poop"
05/18/2018 04:23 PM
poop

If you're in Windows, running cmd.exe or powershell.exe, it's best to move into Linux by running wsl or bash as it keeps the current directory.

如果您使用的是Windows,请运行cmd.exe或powershell.exe,最好通过运行wsl或bash进入Linux,因为它会保留当前目录。

C:\Users\scott\Desktop>bash
129 → $ pwd
/mnt/c/Users/scott/Desktop
129 → $ exit
logout

Cool! Wondering what that number is before my Prompt? That's my blood sugar. But that's another blog post.

凉! 想知道我的提示前那个数字是多少? 那是我的血糖。 但这是另一篇博客文章。

wsl.conf (wsl.conf)

There's a file in /etc/wsl.conf that lets you control things like if your Linux of choice automounts your Windows drives. You can also control more advanced things like if Windows autogenerates a hosts file or processes /etc/fstab. It's up to you!

/etc/wsl.conf中有一个文件,可让您控制诸如选择Linux是否自动挂载Windows驱动器之类的操作。 您还可以控制更高级的操作,例如Windows是否自动生成主机文件或处理/ etc / fstab。 由你决定!

发行版 (Distros)

There's a half dozen distros available and more coming I'm told, but YOU can also make/package your own Linux distribution for WSL with packager/distro-launcher that's open sourced at GitHub.

有六种发行版可供使用,我听说会有更多发行版,但是您也可以使用在GitHub上开源的packager / distro-launcher为WSL制作/打包自己Linux发行版。

Docker和WSL (Docker and WSL)

Everyone wants to know if you can run Docker "natively" on WSL. No, that's a little too "Inception," and as mentioned, the Linux Kernel is not present. The unmodified elf binaries work fine but Windows does the work. BUT!

每个人都想知道您是否可以在WSL上“本地”运行Docker。 不,这有点“盗梦”,而且如上所述,Linux内核不存在。 未修改的elf二进制文件可以正常工作,但Windows可以。 但!

You can run Docker for Windows and click "Expose daemon on localhost:2375" and since Windows and WSL/Linux share the same port space, you CAN run the Docker client very happily on WSL.

您可以运行Docker for Windows,然后单击“在本地主机上暴露守护程序:2375”,并且由于Windows和WSL / Linux共享相同的端口空间,因此您可以在WSL上非常愉快地运行Docker客户端。

After you've got Docker for Windows running in the background, install it in Ubuntu following the regular instructions. Then update your .bashrc to force your local docker client to talk to Docker for Windows:

在后台运行Windows的Docker之后,请按照常规说明将其安装在Ubuntu中。 然后更新您的.bashrc以强制您的本地Docker客户端与Docker for Windows对话:

echo "export DOCKER_HOST=tcp://0.0.0.0:2375" >> ~/.bashrc && source ~/.bashrc

There's lots of much longer and more details "Docker on WSL" tutorials, so if you'd like more technical detail, I'd encourage you to check them out! If you use a lot of Volume Mounts, I found Nick's write-up very useful.

《 WSL上的Docker》教程有很多更长,更多的细节,因此,如果您想了解更多技术细节,我建议您检查一下! 如果您使用大量的Volume Mounts,我发现Nick的文章非常有用。

Now when I run "docker images" or whatever from WSL I'm talking to Docker for Windows. Works great, exactly as you'd expect and you're sharing images and containers in both worlds.

现在,当我运行“ docker映像”或WSL中的任何内容时,我正在与Docker for Windows交谈。 完全符合您的期望,并且在两个世界中共享图像和容器,效果很好。

128 → $ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
podcast test 1bd29d0223da 9 days ago 2.07GB
podcast latest e9dd366f0375 9 days ago 271MB
microsoft/dotnet-samples aspnetapp 80a65a6b6f95 11 days ago 258MB
microsoft/dotnet-samples dotnetapp b3d7f438bad3 2 weeks ago 180MB
microsoft/dotnet 2.1-sdk 1f63052e44c2 2 weeks ago 1.72GB
microsoft/dotnet 2.1-aspnetcore-runtime 083ca6a642ea 2 weeks ago 255MB
microsoft/dotnet 2.1-runtime 6d25f57ea9d6 2 weeks ago 180MB
microsoft/powershell latest 708fb186511e 2 weeks ago 318MB
microsoft/azure-cli latest 92bbcaff2f87 3 weeks ago 423MB
debian jessie 4eb8376dc2a3 4 weeks ago 127MB
microsoft/dotnet-samples latest 4070d1d1e7bb 5 weeks ago 219MB
docker4w/nsenter-dockerd latest cae870735e91 7 months ago 187kB
glennc/fancypants latest e1c29c74e891 20 months ago 291MB

Fabulous.

极好。

编码和编辑文件 (Coding and Editing Files)

I need to hit this point again. Do not change Linux files using Windows apps and tools. However, you CAN share files and edit them with both Windows and Linux by keeping code on the Windows filesystem.

我需要再说一遍。 不要使用Windows应用程序和工具更改Linux文件。 但是,您可以通过将代码保留在Windows文件系统上来共享文件并在Windows和Linux上进行编辑。

For example, my work is at c:\github so it's also at /mnt/c/github. I use Visual Studio code and edit my code there (or vim, from within WSL) and I run the code from Linux. I can even run bash/wsl from within Visual Studio Code using its integrated terminal. Just hit "Ctrl+P" in Visual Studio Code and type "Select Default Shell."

例如,我的工作在c:\ github上,所以它也在/ mnt / c / github上。 我使用Visual Studio代码并在那里(或在WSL中从vim中)编辑我的代码,然后从Linux运行代码。 我什至可以使用集成终端在Visual Studio Code中运行bash / wsl。 只需在Visual Studio代码中按“ Ctrl + P”,然后键入“选择默认外壳”即可。

wsl linux 桌面_(Windows)桌面上Linux年-WSL技巧和窍门_第3张图片

On Windows 10 Insiders edition, Windows now has a UI called "Sets" that will give you Tabbed Command Prompts. Here I am installing Ruby on Rails in Ubuntu next to two other prompts - Cmd and PowerShell. This is all default Windows - no add-ons or extra programs for this experience.

在Windows 10 Insiders版本上,Windows现在具有一个名为“ Sets”的UI,该UI将为您提供选项卡式命令提示符。 在这里,我将在Ubuntu上的其他两个提示(Cmd和PowerShell)旁边安装Ruby on Rails。 这是所有默认的Windows系统-没有插件或额外的程序可以提供这种体验。

wsl linux 桌面_(Windows)桌面上Linux年-WSL技巧和窍门_第4张图片

I'm using Rails as an example here because Ruby/Rails support on Windows with native extensions has historically been a challenge. There's been a group of people heroically (and thanklessly) trying to get Ruby on Rails working well on Windows, but today there is no need. It runs great on Linux under Windows.

我这里以Rails为例,因为在Windows上具有本机扩展的Ruby / Rails支持一直以来都是一个挑战。 有一群人英勇地(而且值得庆幸的是)试图让Ruby on Rails在Windows上正常运行,但是今天没有必要了。 它在Windows下Linux上运行良好。

I can also run Windows apps or tools from Linux as long as I use their full name with extension (like code.exe) or set an alias.

只要我使用带扩展名的全名(例如code.exe)或设置别名,我就可以从Linux运行Windows应用程序或工具。

Here I've made an alias "code" that runs code in the current directory, then I've got VS Code running editing my new Rails app.

在这里,我制作了一个别名“代码”,该别名在当前目录中运行代码,然后运行VS Code,以编辑新的Rails应用程序。

wsl linux 桌面_(Windows)桌面上Linux年-WSL技巧和窍门_第5张图片

I can even mix and match Windows and Linux when piping. This will likely make Windows people happy and deeply offend Linux people. Or, if you're non-denominational like me, you'll dig it!

进行管道传输时,我什至可以混合使用Windows和Linux。 这可能会使Windows用户感到高兴,并深深地冒犯Linux用户。 或者,如果您像我这样的非教派的人,那就去挖吧!

$ ipconfig.exe | grep IPv4 | cut -d: -f2
172.21.240.1
10.159.21.24

Again a reminder: Modifying files located not under /mnt/ with a Windows application in WSL is not supported. But edit stuff on /mnt/x with whatever and you're cool.

再次提醒您:不支持使用WSL中的Windows应用程序修改/mnt/下的文件。 但是用任何东西在/ mnt / x上编辑内容,您就很酷。

分享分享分享 (Sharing Sharing Sharing)

If you have Windows 10 Build 17064 or newer (run ver from windows or "cmd.exe /c /ver" from Linux) and you can even share an environment variable!

如果您具有Windows 10 Build 17064或更高版本(从Windows运行ver或从Linux运行“ cmd.exe / c / ver”),甚至可以共享一个环境变量!

131 → $ cmd.exe /c ver

Microsoft Windows [Version 10.0.17672.1000]

There's a special environment variable called "WSLENV" that is a colon-delimited list of environment variables that should be included when launching WSL processes from Win32 or Win32 processes from WSL. Basically you give it a list of variables you want to roam/share. This will make it easy for things like cross-platform dual builds. You can even add a /p flag and it'll automatically translate paths between c:\windows style and /mnt/c/windows style.

有一个特殊的环境变量“ WSLENV ”,它是用冒号分隔的环境变量列表,当从Win32启动WSL进程或从WSL启动Win32进程时,应包含这些变量。 基本上,您给它一个要漫游/共享的变量列表。 这将简化跨平台双重构建之类的事情。 您甚至可以添加/ p标志,它将自动在c:\ windows样式和/ mnt / c / windows样式之间转换路径。

Check out the example at the WSL Blog about how to share a GOPATH and use VSCode in Windows and run Go in both places.

在WSL博客上查看有关如何共享GOPATH并在Windows中使用VSCode并在两个地方都运行Go的示例。

You can also use a special built-in command line called "wslpath" to translate path names between Windows and WSL. This is useful if you're sharing bash scripts, doing cross-platform scripts (I have PowerShell Core scripts that run in both places) or just need to programmatically switch path types.

您还可以使用特殊的内置命令行“ wslpath”在Windows和WSL之间转换路径名。 如果您要共享bash脚本,执行跨平台脚本(我在两个地方都有PowerShell Core脚本),或者只需要以编程方式切换路径类型,这将非常有用。

131 → $ wslpath "d:\github\hanselminutes-core"
/mnt/d/github/hanselminutes-core
131 → $ wslpath "c:\Users\scott\Desktop"
/mnt/c/Users/scott/Desktop

There is no man page for wslpath yet, but copied from this GitHub issue, here's the gist:

目前尚无wslpath的手册页,但已从GitHub问题中复制了,这是要点:

wslpath usage:
-a force result to absolute path format
-u translate from a Windows path to a WSL path (default)
-w translate from a WSL path to a Windows path
-m translate from a WSL path to a Windows path, with ‘/’ instead of ‘\\’

One final note, once you've installed a Linux distro from the Windows Store, it's on you to keep it up to date. The Windows Store won't run "apt upgrade" or ever touch your Linuxes once they have been installed. Additionally, you can have Ubuntu 1604 and 1804 installed side-by-side and it won't hurt anything.

最后一点,一旦您从Windows应用商店安装了Linux发行版,就可以随时更新它。 一旦安装了Linux,Windows应用商店将不会运行“ apt升级”或接触您Linux。 此外,您可以并排安装Ubuntu 1604和1804,它不会造成任何伤害。

相关链接 (Related Links)

  • Setting up a Shiny Development Environment within Linux on Windows 10

    在Windows 10上Linux中设置闪亮的开发环境

  • Badass Terminal: WSL, macOS, and Ubuntu dotfiles update!!! by Jessica Deen

    Badass Terminal:WSL,macOS和Ubuntu点文件更新!!! 杰西卡·迪恩(Jessica Deen)

Are you using WSL?

您正在使用WSL吗?

翻译自: https://www.hanselman.com/blog/the-year-of-linux-on-the-windows-desktop-wsl-tips-and-tricks

wsl linux 桌面

你可能感兴趣的:(python,linux,java,docker,ubuntu)