在Win 10的Windows Subsystem for Linux中安装Kali

本文介绍如何在Windows子系统Linux(WSL)环境下,通过WSL Distribution Switcher快速安装并配置Kali Linux环境。文章详细说明了安装步骤,包括更新Windows 10、安装Git和Python、使用WSL Distribution Switcher下载Kali Linux镜像,并解决了国内用户遇到的docker源连接速度慢的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置环境

While the setup is described well over at the WSL Distribution Switcher README file, we’ve made a quick 4-minute video to walk you through the setup and installation process. For an easier copy / paste operation, these are the basic steps taken:

  1. Update your Windows 10 machine. Open an administrative PowerShell window and install the Windows Subsystem with this one-liner. A reboot will be required once finished.
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
  1. Once rebooted, open a command line shell and run the following commands to install the default Ubuntu environment. This will lay down the foundations for our Kali install.
lxrun /install
  1. Download “Git for Windows Setup” and install it on your Windows 10. And ensure git.exe in the cmd PATH.

  2. Download and install Python on your Windows 10.And ensure python.exe in the cmd PATH.

  3. Setup and install the WSL Switcher with Git for Windows in cmd on your Windows 10, download a Kali base, and extract it to disk:

git clone https://github.com/RoliSoft/WSL-Distribution-Switcher.git
cd WSL-Distribution-Switcher
python get-prebuilt.py kalilinux/kali-linux-docker
python install.py rootfs_kalilinux_kali-linux-docker_latest.tar.gz
lxrun /setdefaultuser root
  1. Now that Kali is set up on your Windows 10 machine, you can interact with it by running the “bash” command:
bash
  1. At this point, you’re inside Kali and you can use it as you normally do–install packages, use tools, etc. We strongly recommend first running an update and upgrade:
export LANG=C
apt-get update
apt-get dist-upgrade

解决国内docker源速率慢或连接频繁断开问题

在安装配置步骤的第5步中,我们要使用WSL-Distribution-Switcher中的python脚本get-prebuilt.py下载后续所需的kalilinux/kali-linux-docker,但由于某些众所周知的原因,脚本中使用的国外docker库连接较慢,且容易断线,如下所示:

PS C:\Users\Neo\WSL-Distribution-Switcher> python get-prebuilt.py kalilinux/kali-linux-docker
[*] Requesting authorization token...
[*] Fetching manifest info for kalilinux/kali-linux-docker:latest...
[*] Downloading layer sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4...
[*] Downloading layer sha256:270f87c17cf3b314255839cd93e1044691c9c11c76cae6ea973cda78b8a996cb...
[!] Failed to open file rootfs_kalilinux_kali-linux-docker_latest.tar.gz for writing: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。>

PS C:\Users\Neo\WSL-Distribution-Switcher> python get-prebuilt.py kalilinux/kali-linux-docker
[*] Requesting authorization token...
[*] Fetching manifest info for kalilinux/kali-linux-docker:latest...
Traceback (most recent call last):
  File "D:\Applications\Python\Python3.6.4\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "D:\Applications\Python\Python3.6.4\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\Applications\Python\Python3.6.4\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\Applications\Python\Python3.6.4\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\Applications\Python\Python3.6.4\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "D:\Applications\Python\Python3.6.4\lib\http\client.py", line 964, in send
    self.connect()
  File "D:\Applications\Python\Python3.6.4\lib\http\client.py", line 1392, in connect
    super().connect()
  File "D:\Applications\Python\Python3.6.4\lib\http\client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "D:\Applications\Python\Python3.6.4\lib\socket.py", line 724, in create_connection
    raise err
  File "D:\Applications\Python\Python3.6.4\lib\socket.py", line 713, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "get-prebuilt.py", line 59, in <module>
    with urllib.request.urlopen(r) as f:
  File "D:\Applications\Python\Python3.6.4\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "D:\Applications\Python\Python3.6.4\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "D:\Applications\Python\Python3.6.4\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "D:\Applications\Python\Python3.6.4\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "D:\Applications\Python\Python3.6.4\lib\urllib\request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "D:\Applications\Python\Python3.6.4\lib\urllib\request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试 失败。>

PS C:\Users\Neo\WSL-Distribution-Switcher> python get-prebuilt.py kalilinux/kali-linux-docker
[*] Requesting authorization token...
[*] Fetching manifest info for kalilinux/kali-linux-docker:latest...
[*] Downloading layer sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4...
[!] Failed to open file rootfs_kalilinux_kali-linux-docker_latest.tar.gz for writing: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。>

所以我们将脚本中的连接替换为国内docker镜像源。

将脚本中第56行和第92行中的urlhttps://registry.hub.docker.com替换为国内源https://registry.docker-cn.com

# r = urllib.request.Request('https://registry.hub.docker.com/v2/%s/blobs/%s' % (fimage, layer['blobSum']))
# 替换为国内源https://registry.docker-cn.com
r = urllib.request.Request('https://registry.docker-cn.com/v2/%s/blobs/%s' % (fimage, layer['blobSum']))
# r = urllib.request.Request('https://registry.hub.docker.com/v2/%s/blobs/%s' % (fimage, layer['blobSum']))
# 替换为国内源https://registry.docker-cn.com
r = urllib.request.Request('https://registry.docker-cn.com/v2/%s/blobs/%s' % (fimage, layer['blobSum']))

修改之后再执行第5步中的python get-prebuilt.py kalilinux/kali-linux-docker命令,就可以顺利下载了,如下所示:

PS C:\Users\Neo\WSL-Distribution-Switcher> python get-prebuilt_modified.py kalilinux/kali-linux-docker
[*] Requesting authorization token...
[*] Fetching manifest info for kalilinux/kali-linux-docker:latest...
[*] Downloading layer sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4...
[*] Downloading layer sha256:270f87c17cf3b314255839cd93e1044691c9c11c76cae6ea973cda78b8a996cb...
[*] Downloading layer sha256:fb6edc730b9f1f1487e310e44fe10535a599381f878e45a134ee98fc0d00ecf6...
[*] Downloading layer sha256:40a1af1b968019e535ecdd797f42f04c8b41f048d18e78d4e980bcf9dbbc0b91...
[*] Downloading layer sha256:524584107b55123080dc1e9ee28fdb381df6f1f1c2443c67f5d7109f160a18f8...
[*] Downloading layer sha256:789ba1ebcb41d691b60e8de9f5f12a7faf1fb373acd7cac6999be3dbd635538e...
[*] Downloading layer sha256:6ab25bddb7998b8bba3532c2688254ee9691beca3b5ee10de95d2c97a536c858...
[*] Downloading layer sha256:ccd85f0810ad9d5b7e67ca3893cf22379424dacc8d5c30512fc402c905be9f37...
[*] Downloading layer sha256:4e80058918bf66da29110ce517ae13bbe1c5f6388d89ffc28de9fa84730120cc...
[*] Downloading layer sha256:9d39d049d5d04857957674f3e8cc594df514cab102a80c35c00230ff64ca9e5d...
[*] Downloading layer sha256:c38f04972c6b6bcacca087810b1363af7d498e30750aaa57cb94bbbcd26bd1c3...
[*] Downloading layer sha256:9febb14563a0fc828485ebf3aadab364df03a16e22b30bc10a7b28ff9a117e3a...
[*] Downloading layer sha256:014a6d74f96c12add473e5b5f6c46fad92dc786fe91c46e57bcf831fdc9cca30...
[*] Rootfs archive for kalilinux/kali-linux-docker:latest saved to rootfs_kalilinux_kali-linux-docker_latest.tar.gz.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值