Skip to content

Instantly share code, notes, and snippets.

@adripo
Forked from ThePlenkov/boot.sh
Created February 20, 2024 15:33
Show Gist options
  • Save adripo/0b65877d4b2a08baf268d05625ea9c99 to your computer and use it in GitHub Desktop.
Save adripo/0b65877d4b2a08baf268d05625ea9c99 to your computer and use it in GitHub Desktop.

Revisions

  1. @ThePlenkov ThePlenkov revised this gist Sep 6, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    There are tons of gists available for this problem:
    - [Fix DNS resolution in WSL2](https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6)

    However even solution is there - I was not satisfied that this problem alaways persists and I had to do same steps again from time to time
    However even solution is there - I was not satisfied that this problem always persists and I had to do same steps again from time to time. Also using a static DNS like 8.8.8.8 was not the option for me because we use pool of enterprise DNS servers and those are controlled by our IT on our Windows machines. So I wanted to find the way to get those DNS servers dynamically from Windows.

    This guide describes the permanent solution using brand-new WSL boot [command](https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings).

  2. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions boot.sh
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    #!/bin/bash

    # Remove existing "nameserver" lines from /etc/resolv.conf
    sudo sed -i '/nameserver/d' /etc/resolv.conf
    sed -i '/nameserver/d' /etc/resolv.conf

    # Run the PowerShell command to generate "nameserver" lines and append to /etc/resolv.conf
    # we use full path here to support boot command with root user
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | ForEach-Object { "nameserver $_" }' | sudo tee -a /etc/resolv.conf > /dev/null
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | ForEach-Object { "nameserver $_" }' | tr -d '\r'| tee -a /etc/resolv.conf > /dev/null
  3. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    There are tons of gists available for this problem:
    - [Fix DNS resolution in WSL2](https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6)

    However even solution is there - I was not satisfied that this probalem alaways persists and I had to do same steps again from time to time
    However even solution is there - I was not satisfied that this problem alaways persists and I had to do same steps again from time to time

    This guide describes the permanent solution using brand-new WSL boot [command](https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings).

  4. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ However even solution is there - I was not satisfied that this probalem alaways

    This guide describes the permanent solution using brand-new WSL boot [command](https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings).

    > This gist removes nameservers every time you boot your WSL loads and adds newly from powershell results. If it doesn't work for you - please feel free to adjust the script according to your needs.
    > This gist removes nameservers every time your WSL starts and adds newly from powershell results. If it doesn't work for you - please feel free to adjust the script according to your needs.
    ## Prepare boot config

  5. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@ However even solution is there - I was not satisfied that this probalem alaways

    This guide describes the permanent solution using brand-new WSL boot [command](https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings).

    > This gist removes nameservers every time you boot your WSL loads and adds newly from powershell results. If it doesn't work for you - please feel free to adjust the script according to your needs.
    ## Prepare boot config

    - login to your wsl and run `sudo bash`. It's important that you do this with a root user because it's a user which executes this command during boot
  6. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # How to resolve DNS issue in WSL once and foreever
    # How to resolve DNS issue in WSL once and forever

    There are tons of gists available for this problem:
    - [Fix DNS resolution in WSL2](https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6)
  7. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 2 changed files with 30 additions and 11 deletions.
    File renamed without changes.
    41 changes: 30 additions & 11 deletions resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,36 @@
    # powershell script
    # How to resolve DNS issue in WSL once and foreever

    - create file [resolve_wsl_dns.sh](#file-resolve_wsl_dns-sh) with content and save. You can use `nano resolve_wsl_dns.sh` for example
    There are tons of gists available for this problem:
    - [Fix DNS resolution in WSL2](https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6)

    - make it executable
    However even solution is there - I was not satisfied that this probalem alaways persists and I had to do same steps again from time to time

    This guide describes the permanent solution using brand-new WSL boot [command](https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings).

    ## Prepare boot config

    - login to your wsl and run `sudo bash`. It's important that you do this with a root user because it's a user which executes this command during boot
    - change to ~ folder ( or use any folder you like )
    - create a new file for example `boot.sh` and update it with a content from [boot.sh](#file-boot-sh) gist
    - make it executable with `chmod +x boot.sh`
    - create a symbolic link in one of PATH folders ( keep in mind, that for root user number of path folders is very limited by default )
    ```
    chmod +x resolve_wsl_dns.sh
    /usr/local/sbin
    /usr/local/bin
    /usr/sbin
    /usr/bin
    /sbin
    /bin
    ```

    - Move it to any of PATH folders, if you have a plan to run it during
    - i have used `ln -s ~/boot.sh /usr/local/bin/boot.sh`
    - having such a limited list of folders makes root not aware of powershell.exe and rest of window utilities. I have preferred to use an absolute path like `/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe` in my script - but you may think of creating a symlink for powershell too.
    - now let's add boot command to `/etc/wsl.conf` file. In my case it looks like this:
    ```
    mv resolve_wsl_dns.sh ~/bin/
    echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc
    [network]
    generateResolvConf=false
    [boot]
    systemd=true
    command=boot.sh
    ```

    - if you have Windows 11 - you can try to use [boot](https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings) command
    nan
    - now just `exit` from bash, from wsl as well. Terminate your wsl with a command like `wsl -t Ubuntu` and start again
    - if everything went well `cat /etc/resolv.conf` should have nameservers from your Windows
  8. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 1 addition and 11 deletions.
    12 changes: 1 addition & 11 deletions resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,14 @@
    # powershell script

    - create file [resolve_wsl_dns.sh](#file-resolve_wsl_dns-sh) with content and save. You can use `nano resolve_wsl_dns.sh` for example
    ```
    #!/bin/bash
    # Remove existing "nameserver" lines from /etc/resolv.conf
    sudo sed -i '/nameserver/d' /etc/resolv.conf
    # Run the PowerShell command to generate "nameserver" lines and append to /etc/resolv.conf
    powershell.exe -Command '(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | ForEach-Object { "nameserver $_" }' | sudo tee -a /etc/resolv.conf > /dev/null
    ```

    - make it executable
    ```
    chmod +x resolve_wsl_dns.sh
    ```

    - Move it to any of PATH folders or create a new one, for example
    - Move it to any of PATH folders, if you have a plan to run it during
    ```
    mkdir ~/bin
    mv resolve_wsl_dns.sh ~/bin/
    echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc
    ```
  9. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. No changes.
  10. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # powershell script

    - create file [resolve_wsl_dns.sh](raw/resolve_wsl_dns.sh) with content and save. You can use `nano resolve_wsl_dns.sh` for example
    - create file [resolve_wsl_dns.sh](#file-resolve_wsl_dns-sh) with content and save. You can use `nano resolve_wsl_dns.sh` for example
    ```
    #!/bin/bash
  11. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # powershell script

    - create file resolve_wsl_dns.sh with content and save. You can use `nano resolve_wsl_dns.sh` for example
    - create file [resolve_wsl_dns.sh](raw/resolve_wsl_dns.sh) with content and save. You can use `nano resolve_wsl_dns.sh` for example
    ```
    #!/bin/bash
  12. @ThePlenkov ThePlenkov revised this gist Aug 7, 2023. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion resolve_wsl_dns.sh
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,5 @@
    sudo sed -i '/nameserver/d' /etc/resolv.conf

    # Run the PowerShell command to generate "nameserver" lines and append to /etc/resolv.conf
    powershell.exe -Command '(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | ForEach-Object { "nameserver $_" }' | sudo tee -a /etc/resolv.conf > /dev/null
    # we use full path here to support boot command with root user
    /mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | ForEach-Object { "nameserver $_" }' | sudo tee -a /etc/resolv.conf > /dev/null
  13. @ThePlenkov ThePlenkov created this gist Aug 7, 2023.
    27 changes: 27 additions & 0 deletions resolve_wsl_dns.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # powershell script

    - create file resolve_wsl_dns.sh with content and save. You can use `nano resolve_wsl_dns.sh` for example
    ```
    #!/bin/bash
    # Remove existing "nameserver" lines from /etc/resolv.conf
    sudo sed -i '/nameserver/d' /etc/resolv.conf
    # Run the PowerShell command to generate "nameserver" lines and append to /etc/resolv.conf
    powershell.exe -Command '(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | ForEach-Object { "nameserver $_" }' | sudo tee -a /etc/resolv.conf > /dev/null
    ```

    - make it executable
    ```
    chmod +x resolve_wsl_dns.sh
    ```

    - Move it to any of PATH folders or create a new one, for example
    ```
    mkdir ~/bin
    mv resolve_wsl_dns.sh ~/bin/
    echo 'export PATH=$PATH:$HOME/bin' >> ~/.bashrc
    ```

    - if you have Windows 11 - you can try to use [boot](https://learn.microsoft.com/en-us/windows/wsl/wsl-config#boot-settings) command
    nan
    7 changes: 7 additions & 0 deletions resolve_wsl_dns.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/bin/bash

    # Remove existing "nameserver" lines from /etc/resolv.conf
    sudo sed -i '/nameserver/d' /etc/resolv.conf

    # Run the PowerShell command to generate "nameserver" lines and append to /etc/resolv.conf
    powershell.exe -Command '(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | ForEach-Object { "nameserver $_" }' | sudo tee -a /etc/resolv.conf > /dev/null