超详细的WSL教程:Windows上的Linux子系统
Table of Contents
Introduction
This tutorial provides a comprehensive guide on using the Windows Subsystem for Linux (WSL) to run Linux commands directly on Windows. WSL allows for seamless integration between Windows and Linux, enabling users to leverage the functionality of both operating systems. By following this guide, you will learn how to install and configure WSL, run Linux commands, and utilize powerful features like Docker and GPU passthrough.
Step 1: Install WSL
To get started with WSL, you need to install it on your Windows system.
-
Open Windows PowerShell as Administrator
- Search for "PowerShell" in the Start menu.
- Right-click and select "Run as administrator."
-
Enable the WSL feature
- In the PowerShell window, enter the following command:
wsl --install
- This command will install WSL along with a default Linux distribution (usually Ubuntu).
- In the PowerShell window, enter the following command:
-
Restart your computer
- After the installation completes, restart your computer to apply the changes.
Step 2: Set Up Your Linux Distribution
Once WSL is installed, you need to set up your Linux distribution.
-
Launch your Linux distribution
- Open the Start menu and find your installed distribution (e.g., Ubuntu).
- Click on it to launch the terminal.
-
Complete the initial setup
- You will be prompted to create a new user account and password. Follow the on-screen instructions to set this up.
-
Update your package list
- It’s a good practice to update your package list. Run the following command:
sudo apt update
- It’s a good practice to update your package list. Run the following command:
Step 3: Run Linux Commands
Now that your Linux environment is ready, you can start running Linux commands.
-
Basic commands
- Test your setup by running simple commands like:
ls pwd
- These commands list files in the current directory and display the current directory path, respectively.
- Test your setup by running simple commands like:
-
Install additional packages
- You can install software using:
sudo apt install <package-name>
- Replace
<package-name>
with the desired software. For example, to install Git:sudo apt install git
- You can install software using:
Step 4: Integrate Windows Applications
WSL allows you to run Windows applications from the Linux terminal.
-
Launch a Windows application
- You can open Notepad from your Linux terminal by running:
notepad.exe
- You can open Notepad from your Linux terminal by running:
-
Access files
- Your Windows files are accessible in the
/mnt/c
directory. For example, to navigate to your Documents folder:cd /mnt/c/Users/YourUsername/Documents
- Your Windows files are accessible in the
Step 5: Use Docker and GPU Passthrough
WSL supports advanced functionalities like Docker and GPU passthrough.
-
Install Docker
- First, ensure WSL 2 is enabled. You can check this with:
wsl -l -v
- For Docker installation, follow the official Docker documentation for WSL setup.
- First, ensure WSL 2 is enabled. You can check this with:
-
GPU Passthrough Setup
- Ensure you have the latest Windows updates and a compatible GPU.
- Follow specific instructions from your GPU vendor to enable GPU support in Docker.
Conclusion
You have successfully set up WSL on your Windows machine, allowing you to run Linux commands and applications seamlessly alongside Windows. Explore the various packages available in Linux, try integrating more Windows applications, and experiment with advanced features like Docker and GPU passthrough for enhanced productivity. As you become familiar with WSL, consider diving deeper into specific Linux tools or programming languages to maximize the benefits of this powerful integration.