【转】WSL 配置指北:打造 Windows 最强命令行

原文:WSL 配置指北:打造 Windows 最强命令行
在这里插入图片描述

6.5 与 Windows 的互操作性

WSL 与 Windows 之间的互操作性 (Interoperability) 很牛逼。怎么个牛逼法呢?

Windows 下的所有盘符都挂载在 WSL 中的 /mnt 目录下,可以直接操作。
 
WSL 中的所有数据则存放于 C:\Users{你的用户}\AppData\Local\Packages{Linux发行版包名}\LocalState\rootfs目录中(不要在 Windows 中修改这些文件,这会造成文件权限错误):

$ ls /mnt
c  d  e
$ mount -l
rootfs on / type lxfs (rw,noatime)
C: on /mnt/c type drvfs (rw,noatime,uid=1000,gid=1000)
D: on /mnt/d type drvfs (rw,noatime,uid=1000,gid=1000)
E: on /mnt/e type drvfs (rw,noatime,uid=1000,gid=1000)

你可以在 Windows 命令行环境中直接调用 WSL 中的命令:

PS C:\temp> wsl ls -al
total 0
drwxrwxrwx 1 printempw printempw 4096 Sep  7 19:04 .
drwxrwxrwx 1 printempw printempw 4096 Sep  7 18:38 ..
-rwxrwxrwx 1 printempw printempw    4 Sep  7 19:04 foo.txt

你也可以在 WSL 中调用 Windows 中的命令行程序:

$ which ipconfig.exe
/mnt/c/Windows/System32/ipconfig.exe
$ ipconfig.exe
Windows IP Configuration
...

你可以在 WSL 中直接启动 Windows 应用:

$ notepad.exe "C:\temp\foo.txt"

你还可以通过 pipes 与 Windows 程序通信:

# 复制内容至 Windows 剪贴板
$ cat foo.txt | clip.exe

你甚至可以把 Windows 命令和 WSL 命令混着用:

PS> ipconfig | wsl grep IPv4
IPv4 Address. . . . . . . . . . . : 192.168.1.114

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

$ ls -al | findstr.exe foo.txt
-rwxrwxrwx 1 printempw printempw    4 Sep  7 19:04 foo.txt

$ cmd.exe /c dir
 Volume in drive C is Windows
 Volume Serial Number is B263-****

 Directory of C:\temp

2018/09/07  19:04    <DIR>          .
2018/09/07  19:04    <DIR>          ..
2018/09/07  19:04                 4 foo.txt
               1 File(s)              4 bytes
               2 Dir(s)  194,422,341,632 bytes free

同时,WSL 与 Windows 共享网络栈,也就是说你可以:

在 WSL 中启动 web server,在 Windows 上使用浏览器访问; 在 Windows 下启动 MySQL/Redis 服务器,在 WSL 中连接; 诸如此类。

6.6 DrvFs 文件权限问题

虽然 WSL 中可以直接访问 Windows 磁盘的内容,但如果你曾经这么做过,你应该对这样绿油油一片的 ls 不会感到陌生。为什么 NTFS 文件系统中的文件到 WSL 下权限就全部成 0777 了呢?
在这里插入图片描述

5. 使用更专业的终端模拟器

我猜你现在正在对上面那个窗口发呆。

—— 这个新宋体他娘的是个什么情况?
在这里插入图片描述


如果你正在使用中文 Windows 系统,而且之前并没有修改过 Win32 Console 的默认配置,那么你的 WSL 终端默认就会是这样的。新宋体,就是这么 Hardcore。惊不惊喜,意不意外?

好吧不开玩笑,Windows 这个控制台窗口就是很多人讨厌它的原因之一,难用又难看。

个人比较推荐的终端模拟器有:

  • wsl-terminal
    专门为 WSL 开发的终端模拟器,基于 mintty 与 wslbridge,稳定易用。

  • ConEmu
    Windows 上的老牌终端模拟器,功能极为强大,要啥有啥。

  • Hyper
    基于 Electron 的跨平台终端模拟器,好看和可扩展性是卖点,BUG 不少。

还有其他各种各样的终端模拟器,选个自己喜欢的就好。反正不管选哪个,都比默认的那玩意儿要好用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值