Giter Site home page Giter Site logo

bash-wsl2-host's Introduction

wsl2hosts

中文文档

Bash script to automatically update your Windows hosts file with the WSL2 VM IP address and name.

Dependencies:

gerardog/gsudo: gsudo - a sudo for Windows https://github.com/gerardog/gsudo

gsudo is a sudo equivalent for Windows, with a similar user-experience as the original Unix/Linux sudo. Allows to run commands with elevated permissions, or to elevate the current shell, in the current console window or a new one.

Usage:

  1. Download gsudo in Windows and install. For example, I did portable installation in %USERPROFILE%\gsudo\

  2. Add this script to ~/.bashrc and
    Define your dns name in _wsl_ssh_ip_name variable.
    Add location from gsudo and WindowsPowerShell to PATH environment variable in the script.

#!/bin/bash

# adjust this paths to your environment
export PATH=/mnt/c/Users/your-USERNAME/gsudo/x64/:$PATH
export PATH=/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:$PATH

change_wsl_ssh_ip(){
  # define your dns name
  _wsl_ssh_ip_name="ubuntu2004.wsl"

  # set windows hosts file path
  _wslhosts=/mnt/c/Windows/System32/drivers/etc/hosts
  _winhosts='C:\Windows\System32\drivers\etc\hosts'

  # get wsl real ip and ignore wsl-vpnkit
  _wsl_ssh_ip_addr=$(ip -4 addr show "eth0" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)
  echo "wsl ssh ip addr:[$_wsl_ssh_ip_addr]"

  # get current ip from windows hosts
  _win_ssh_ip_addr=$(grep -oP "\d+(\.\d+){3}(?=\s*$_wsl_ssh_ip_name)" $_wslhosts)
  echo "win ssh ip addr:[$_win_ssh_ip_addr]"

  # check if ip exists or diff, then modify
  if [[ "$_win_ssh_ip_addr" == "" ]]
  then
    echo "ssh ip is missing, adding to windows hosts"
    gsudo powershell.exe -Command "echo '' '$_wsl_ssh_ip_addr  $_wsl_ssh_ip_name' | out-file -encoding ASCII $_winhosts -append"
  else
    if [[ "$_win_ssh_ip_addr" != "$_wsl_ssh_ip_addr" ]]
    then
      echo "ssh ip diff, modifying windows hosts"
      gsudo powershell.exe -Command "(gc $_winhosts) -replace '$_win_ssh_ip_addr', '$_wsl_ssh_ip_addr' | out-file -encoding ASCII $_winhosts"
    else
      echo "ssh ip ok"
    fi
  fi

  # resulting windows hosts
  echo "now $_wsl_ssh_ip_name 's ip is:"
  grep "$_wsl_ssh_ip_name" $_wslhosts
}

# run the function
change_wsl_ssh_ip
  1. Now shutdown and restart your wsl to check whether the bash script works.
C:\>wsl --shutdown
  1. Finally check the stdout that the content of the Windows hosts file has changed.
...
123.45.67.89  ubuntu2004.wsl 
...

bash-wsl2-host's People

Contributors

berndgz avatar iamqiz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

berndgz lsiu

bash-wsl2-host's Issues

Script empties the hole hosts file :-O

Nice idea, but I'm having trouble with the script. I'm located behind a proxy and VPN, therefore I use wsl-vpnkit. There was no entry in the hosts file for the running distribution and the Bash-wsl2-host script stuck during the Windows PowerShell process and empties the hole hosts file, that's not good.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.