- Add LocalStack and Adjust local Emailing Signed-off-by: Dave Richer <dave@imexsystems.ca>
7.7 KiB
Setting up External Networking and Static IP for WSL2 using Hyper-V
This guide will walk you through the steps to configure your WSL2 (Windows Subsystem for Linux) instance to use an external Hyper-V virtual switch, enabling it to connect directly to your local network. Additionally, you'll learn how to assign a static IP address to your WSL2 instance.
Prerequisites
- Windows 10/11 with WSL2 installed.
- Hyper-V enabled on your system. If not, follow these steps to enable it:
- Open PowerShell as Administrator and run:
dism.exe /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V - Restart your computer.
- A basic understanding of networking and WSL2 configuration.
Step 1: Create an External Hyper-V Switch
- Open Hyper-V Manager:
- Press
Windows Key + X, selectHyper-V Manager.
- Create a Virtual Switch:
- In the right-hand pane, click
Virtual Switch Manager. - Choose
Externaland clickCreate Virtual Switch. - Select your external network adapter (this is usually your Ethernet or Wi-Fi adapter).
- Give the switch a name (e.g.,
WSL External Switch), then clickApplyandOK.
Step 2: Configure WSL2 to Use the External Hyper-V Switch
Now that you've created the external virtual switch, follow these steps to configure your WSL2 instance to use this switch.
- Set WSL2 to Use the External Switch:
- By default, WSL2 uses NAT to connect to your local network. You need to configure WSL2 to use the external Hyper-V switch instead.
- Check WSL2 Networking:
- Inside WSL, run:
ip a - You should see an IP address in the range of your local network (e.g.,
192.168.x.x).
Step 3: Configure a Static IP Address for WSL2
Once WSL2 is connected to the external network, you can assign a static IP address to your WSL2 instance.
- Open WSL2 and Edit the Network Configuration:
- Depending on your Linux distribution, the file paths may vary, but typically for Ubuntu-based systems:
sudo nano /etc/netplan/01-netcfg.yaml - If this file doesn’t exist, create a new file or use the correct configuration file path.
- Configure Static IP:
- Add or update the following configuration:
network: version: 2 renderer: networkd ethernets: eth0: dhcp4: no addresses: - 192.168.1.100/24 # Choose an IP address in your network range gateway4: 192.168.1.1 # Your router's IP address nameservers: addresses: - 8.8.8.8 - 8.8.4.4 - Adjust the values according to your local network settings:
addresses: This is the static IP you want to assign.gateway4: This should be the IP address of your router.nameservers: These are DNS servers, you can use Google's public DNS or any other DNS provider.
- Apply the Changes:
- Run the following command to apply the network configuration:
sudo netplan apply
- Verify the Static IP:
- Check if the static IP is correctly set by running:
ip a - You should see the static IP you configured (e.g.,
192.168.1.100) on the appropriate network interface (usuallyeth0).
Step 4: Restart WSL2 to Apply Changes
To ensure the changes are fully applied, restart WSL2:
- Open PowerShell or Command Prompt and run:
wsl --shutdown - Then, start your WSL2 instance again.
Step 5: Verify Connectivity
- Check Internet and Local Network Connectivity:
- Run a ping command from within WSL to verify that it can reach the internet:
ping 8.8.8.8
- Run a ping command from within WSL to verify that it can reach the internet:
- Test Access from other Devices:
- If you're running services inside WSL (e.g., a web server), ensure they are accessible from other devices on your local network using the static IP address you configured (e.g.,
http://192.168.1.100:4000).
- If you're running services inside WSL (e.g., a web server), ensure they are accessible from other devices on your local network using the static IP address you configured (e.g.,
Configuring vm.overcommit_memory in sysctl for WSL2
To prevent memory overcommitment issues and optimize performance, you can configure the vm.overcommit_memory setting in WSL2. This is particularly useful when running Redis or other memory-intensive services inside WSL2, as it helps control how the Linux kernel handles memory allocation.
1. Open the sysctl Configuration File:
To set the vm.overcommit_memory value, you'll need to edit the sysctl configuration file. Inside your WSL2 instance, run the following command to open the sysctl.conf file for editing:
sudo nano /etc/sysctl.conf
2. Add the Overcommit Memory Setting:
Add the following line at the end of the file to allow memory overcommitment:
vm.overcommit_memory = 1
This setting tells the Linux kernel to always allow memory allocation, regardless of how much memory is available, which can prevent out-of-memory errors when running certain applications.
3. Apply the Changes:
After editing the file, save it and then apply the new sysctl configuration by running:
sudo sysctl -p
Install Docker and Docker Compose in WSL2
Local Stack
- LocalStack Front end (Optional) - https://apps.microsoft.com/detail/9ntrnft9zws2?hl=en-us&gl=US
- http://localhost:4566/_aws/ses will allow you to see emails sent
Docker Commands
General docker-compose Commands:
- Bring up the services, force a rebuild of all services, and do not use the cache:
docker-compose up --build --no-cache - Start Containers in Detached Mode: This will run the containers in the background (detached mode):
docker-compose up -d - Stop and Remove Containers: Stops and removes the containers gracefully:
docker-compose down - Stop containers without removing them:
docker-compose stop - Remove Containers, Volumes, and Networks:
docker-compose down --volumes - Force rebuild of containers:
docker-compose build --no-cache - View running Containers:
docker-compose ps - View a specific containers logs:
docker-compose logs <container-name> - Scale services (multiple instances of a service):
docker-compose up --scale <container-name>=<instances number> -d - Watch a specific containers logs in realtime with timestamps:
docker-compose logs -f --timestamps <container-name>
Volume Management Commands
- List Docker volumes:
docker volume ls - Remove Unused volumes
docker volume prune - Remove specific volumes
docker volume rm <volume-name> - Inspect a volume:
docker volume inspect <volume-name>
Container Image Management Commands:
- List running containers:
docker ps - List all containers:
docker os -a - Remove Stopped containers:
docker container prune - Remove a specific container:
docker container rm <container-name> - Remove a specific image:
docker rmi <image-name>:<version> - Remove all unused images:
docker image prune -a
Network Management Commands:
- List networks:
docker network ls - Inspect a specific network:
docker network inspect <network-name> - Remove a specific network:
docker network rm <network-name> - Remove unused networks:
docker network prune
Debugging and maintenance:
- Enter a Running container:
docker exec -it <container name> /bin/bash(could also be/bin/shor for exampleredis-clion a redis node) - View container resource usage:
docker stats - Check Disk space used by Docker:
docker system df - Remove all unused Data (Nuclear option):
docker system prune
Specific examples
- To simulate a Clean state, one should run
docker system prunefollowed bydocker volume prune -a - You can run
docker-compose upwithout the-doption, and you will get what is identical to the experience you were used to, this includes being able to control-c and bring the entire stack down