Configuring and Deploying Windows Subsystem for Linux on Windows 10
The Windows Subsystem for Linux (WSL) provides a compatibility layer for running native Linux ELF64 binaries directly on Windows 10. This integration allows developers to utilize Linux command-line tools and applications without the overhead of a traditional virtual machine.
Activating Developer Mode
To begin, enable Developer Mode in your Windows settings:
- Open the Settings application.
- Navigate to Update & Security.
- Select For developers from the left-hand menu.
- Under "Use developer features," choose Developer mode.
Enabling the Windows Subsystem for Linux Feature
Next, activate the core WSL feature within Windows:
- Press
Win + Xto open the Quick Link menu, then select Apps and Features. - Scroll down and click Programs and Features (or search for "Turn Windows features on or off" in the Start menu).
- In the "Windows Features" dialog, locate and check the box next to Windows Subsystem for Linux.
- Click OK and restart your Windows system when prompted.
Installing a Linux Distribution
After restarting, install your preferred Linux distribution from the Microsoft Store:
- Open the Microsoft Store application.
- Search for "WSL" or a specific distribution like "Ubuntu".
- Select an Ubuntu version, for example, Ubuntu 22.04 LTS.
- Click Get or Install to download the distribution.
- Once the download completes, click Launch to start your new Linux environment.
Initial Setup of the Linux Environment
Upon the first launch of your Linux distribution, you will be prompted to:
- Create a default Unix username.
- Set a password for this user.
This user will have sudo privileges, allowing you to execute commands with administrative access by prefixing them with sudo.
Updating Software Repositories
Its good practice to update your package lists and upgrade installed software immediately after installation:
-
Backup the default
sources.listfile:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak ```
-
Edit
sources.listto configure package mirrors. For example, to use a common mirror for Ubuntu 22.04 LTS (Jammy Jellyfish):
sudo vim /etc/apt/sources.list ```
Replace existing entries with lines similar to these (or choose a regional mirror as appropriate, e.g., Aliyun mirror for China):
```
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse ```
-
Update your package list:
sudo apt update ```
Quick Access to the WSL Terminal
You can quickly open a WSL terminal from Windows:
- Press
Win + R, then typebashand press Enter. - Alternatively, open Command Prompt or PowerShell and type
wslor the name of your distribution (e.g.,ubuntu).
Configuring SSH Server in WSL
To enable remote connections to your WSL instance, install and configure an SSH server:
-
Check for existing SSH processes:
ps -ef | grep sshd ```
-
Install the OpenSSH server. If already installed, it's good practice to remove and reinstall to ensure a clean setup:
sudo apt remove openssh-server sudo apt install openssh-server ```
-
Modify the SSH daemon configuration file to allow password authentication and set the port:
sudo vim /etc/ssh/sshd_config ```
Ensure these lines are present and uncommented (or adjust if commented):
```
Port 22 PasswordAuthentication yes ```
-
Restart the SSH service to apply changes:
sudo service ssh restart
orbash
sudo service ssh --full-restart
```
Enabling Root Login via SSH (Use with Caution)
For specific scenarios, you might need to enable direct root login via SSH. This is generally discouraged for security reasons but can be configured as follows:
-
Edit the SSH daemon configuration file again:
sudo vim /etc/ssh/sshd_config ```
-
Add or uncomment the line
PermitRootLogin yes.
Authentication:
LoginGraceTime 120 #PermitRootLogin without-password PermitRootLogin yes ```
-
Restart the SSH service:
sudo service ssh restart ```
After these steps, you can connect to your WSL Linux environment using any SSH client (like SecureCRT, PuTTY, or the ssh command in PowerShell/Command Prompt) from your Windows host or another machine, using the IP address of your WSL instance.