Windows和Linux的工具和命令可以通过WSL互换使用。
可以在Linux子系统中运行Windows命令,也可以在Windows中运行Linux子系统工具。
但是需要注意:Windows和Linux文件系统以不同的方式处理大小写—Windows是不区分大小写的,Linux是区分大小写的。
\\wsl$
通过Windows查看Ubuntu文件目录
分别在Ubuntu和Windows运行ls -la 指令
Ubuntu:
ls -la
windows:
wsl ls -la
在Ubuntu中运行 ls -la可以看Ubuntu的文件目录,在Windows power shell中运行wsl ls -la则可以看到Windows文件目录。
我们在Ubuntu中安装了dig工具,那么除了可以在Ubuntu系统中使用,也可以通过power shell中执行:
wsl dig baidu.com
直接比照Ubuntu中的指令用法,会有提示报错。
PS C:\Users\Administrator> dig baidu.com
dig : The term 'dig' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the s
pelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ dig baidu.com
+ ~~~
+ CategoryInfo : ObjectNotFound: (dig:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Administrator>
执行wsl dig baidu.com则可以正常执行,并输出结果:
PS C:\Users\Administrator> wsl dig baidu.com
; <<>> DiG 9.18.12-0ubuntu0.22.04.1-Ubuntu <<>> baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47817
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;baidu.com. IN A
;; ANSWER SECTION:
baidu.com. 429 IN A 39.156.66.10
baidu.com. 429 IN A 110.242.68.66
;; Query time: 47 msec
;; SERVER: 114.114.114.114#53(114.114.114.114) (UDP)
;; WHEN: Tue Nov 28 14:59:15 CST 2023
;; MSG SIZE rcvd: 70
PS C:\Users\Administrator>
在Windows使用sudo更新Linux子系统
wsl sudo apt-get update
PS C:\Users\Administrator> wsl sudo apt-get update
[sudo] password for mirror:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Get:5 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [989 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1200 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1003 kB]
Get:8 http://security.ubuntu.com/ubuntu jammy-security/main Translation-en [192 kB]
Get:9 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [1143 kB]
Get:10 http://security.ubuntu.com/ubuntu jammy-security/restricted Translation-en [186 kB]
Get:11 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [799 kB]
Fetched 5741 kB in 28s (202 kB/s)
Reading package lists... Done
PS C:\Users\Administrator>
用Linux的指令ls -la 列出文件目录,再通过power shell的管道符 和findstr 指令找出目录中含有“Do”字符串的目录。
wsl ls -la | findstr "Do"
PS C:\Users\Administrator> wsl ls -la | findstr "Do"
drwxrwxrwx 1 mirror mirror 4096 Nov 9 10:59 Documents
drwxrwxrwx 1 mirror mirror 4096 Nov 21 16:01 Downloads
lrwxrwxrwx 1 mirror mirror 36 Mar 25 2022 My Documents -> /mnt/c/Users/Administrator/Documents
PS C:\Users\Administrator>
先通过dir 查看当前文件目录
PS C:\Users\Administrator> dir
Directory: C:\Users\Administrator
Mode LastWriteTime Length Name
---- ------------- ------ ----
d-r--- 2022/3/25 13:23 3D Objects
d-r--- 2022/3/25 13:23 Contacts
d-r--- 2023/11/27 14:09 Desktop
d-r--- 2023/11/9 10:59 Documents
d-r--- 2023/11/21 16:01 Downloads
d-r--- 2022/3/25 13:23 Favorites
d-r--- 2022/3/25 13:23 Links
d-r--- 2022/3/25 13:23 Music
d-r--- 2022/3/25 13:24 OneDrive
d-r--- 2022/3/25 13:23 Pictures
d----- 2023/2/15 17:20 PycharmProjects
d-r--- 2022/3/25 13:23 Saved Games
d-r--- 2022/3/25 13:23 Searches
d-r--- 2023/6/13 13:24 Videos
d----- 2023/2/28 16:02 WebControlThird
-a---- 2023/11/2 15:10 1014 res.bmp
接着运用Windows和Linux混合指令,找出含有“Do”字符串的目录
dir | wsl grep Do
PS C:\Users\Administrator> dir | wsl grep Do
d-r--- 2023/11/9 10:59 Documents
d-r--- 2023/11/21 16:01 Downloads
PS C:\Users\Administrator>
在Ubuntu中通过Windows ipconfig工具查看当前IP地址:
mirror@Ubuntu22:~$ ipconfig.exe
Windows IP Configuration
Ethernet adapter 以太网:
Connection-specific DNS Suffix . : domain.com
Link-local IPv6 Address . . . . . : fe80::82b:d1c2:85c7:7798%10
IPv4 Address. . . . . . . . . . . : 192.168.8.20
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.8.250
过滤IPv4地址:
ipconfig.exe | grep IPv4
mirror@Ubuntu22:~$ ipconfig.exe | grep IPv4
IPv4 Address. . . . . . . . . . . : 192.168.8.20
mirror@Ubuntu22:~$
再来一个Windows netsh工具的使用范例:
mirror@Ubuntu22:~$ netsh.exe
netsh>lan
netsh lan>show interface
There is 1 interface on the system:
Name : 以太网
Description : Intel(R) Ethernet Connection (11) I219-LM
GUID : 32da7e41-d361-47c1-99b0-77e1a1a069d9
Physical Address : 88-88-88-88-88-87
State : Connected. Network does not support authentication.
netsh lan>