Skip to content

Instantly share code, notes, and snippets.

@dayne
Last active September 17, 2023 19:01
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dayne/313981bc3ee6dbf8ee57eb3d58aa1dc0 to your computer and use it in GitHub Desktop.
Save dayne/313981bc3ee6dbf8ee57eb3d58aa1dc0 to your computer and use it in GitHub Desktop.
WLS-Helpers

Install linux subsystem Ubuntu 18.04

See Microsoft's WSL install guide for windows 10 for details.

Work-in-progress - command line only install:

  • Open Powershell as Administrator and run:
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
  • Open Windows App Store
    • Search for "Ubuntu" and install Ubuntu 18.04

(work in progress - command line install steps):

  • Download linux system system: Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu1804.zip -UseBasicParsing
    • Install it

Setup open-ssh server

  • Install openssh-server (found I needed to remove the original package first)
    apt remove openssh-server
    apt update
    apt install openssh-server
    
  • Modify default port used by editing /etc/ssh/sshd_config
    Port 2218 # change from 22 AllowUsers dayne # add this line with your usename
  • Start service
    sudo /etc/init.d/ssh start
  • Add 2218 to windows firewall (see below)
  • Then try to ssh in: ssh username@windowsbox.lan -p 2218

Windows Firewall settings:

  • Windows Defender Firewall -> Advanced Settings
  • Windows Defender Firewall with Advanced Security
    • inbound rules -> Actions Tab -> New Rule
    • Port -> TCP, Specificed local ports: 2218 ->
    • Allow the connection,
    • Checked: Domain, Private
    • Name: ubuntu1806ssh

set ssh server to autolaunch on boot

See harleyday's original gist for details.

  • Set Ubuntu-18.04 as default
    • Open Windows PowerShell
    wslconfig /l   # list your linux subsystems
    wslconfig /setdefault Ubuntu-18.04 # set default
    bash.exe       # launch default WLS
    lsb_release -a # verify you've got Ubuntu 18.04.1
    
  • Enable passwordless sudo to start openssh:
    • visudo and add the following at the end of the file:
    %sudo ALL=NOPASSWD: /etc/init.d/ssh
    
  • Put win-start-linux.vbs in Startup folder (available below) - this calls boot-linux.bat on login.
    • Open start menu: type run to Run Command
    • Then type shell:startup to open up your Startup folder
  • Put boot-linux.bat in your Windows Documents directory (available below) - this call the boot.sh from within the WSL
  • Put boot.sh in your WSL root (as /boot.sh) (available below) - this starts the openssh server
    • Note: this needs passwordless sudo to work

Windows Defender/Anti-malware Causing Performance Issues

microsoft/WSL#1932

https://gist.github.com/ian-p-cooke/4e1713729b3676d2a5eaaf96b99978da

https://medium.com/@rspeets/tip-speed-up-your-wsls-i-o-195781b901b9

Here is the example script provided by ian-p-cooke (updated for Ubuntu18.04)

$win_user = "ipc"
$linux_user = "ipc"
$package = "CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc"
$base_path = "C:\Users\" + $win_user + "\AppData\Local\Packages\" + $package + "\LocalState\rootfs"
$dirs = @("\bin", "\sbin", "\usr\bin", "\usr\sbin", ("\home\" + $linux_user + "\.cargo\bin"))
$dirs | ForEach { Add-MpPreference -ExclusionProcess ($base_path + $_ + "\*") }
Add-MpPreference -ExclusionPath $base_path

Ruby script to generate the above script automatically based on your own custom path. Run this inside the Ubuntu WSL and copy-n-paste result into an administrator PowerShell:

#!/usr/bin/env ruby

ubuntu = Dir.glob("/mnt/c/Users/*/AppData/Local/Packages/CanonicalGroupLimited.*")
case ubuntu.size
  when 0
    # puts "# ERROR: Unable to detect any Ubuntu WSL /mnt/c/Users/*/AppData/Local/Packages/CanonicalGroupLimited.*"
    target = "ERROR-FINDING-UBUNTU_WSL_PATH"
  when 1
    # puts "# Found a single Ubuntu WSL target: ${ubuntu.first}"
    target = File.basename( ubuntu.first )
  else
    puts "#\n#\n# Found multiple Ubuntu WSL targets"
    ubuntu.each { |t| puts "# #{t}" }
    target = File.basename( ubuntu.first )
    puts "# Using first result: #{target}"
end
puts target

path = ENV['PATH'].split(':').map {|p| "\"#{ p.tr('/',%Q{\\}) }\"" }
puts "

#
# Windows PowerShell command to exclude all linux paths from defender:
#
$win_user = $env:UserName
$linux_user = \"#{ ENV['USER'] }\"
$package = \"#{target}\""

puts '$base_path = "C:\Users\" + $win_user + "AppData\Local\Packages" + $package + "\LocalState\rootfs"'

puts "$dirs = @( #{path.join(", ")} )"

puts '$dirs | ForEach { Add-MpPreference -ExclusionProcess ($base_path + $_ + "\*") }
Add-MpPreference -ExclusionPath $base_path'

Getting Docker for Windows accessable in WSL.

https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly

Step 1: Install Docker for Windows

  • Make sure to open Docker for Windows Settings
    • Check `Expose daemon on tcp://localhost:2375 without TLS'

Step 2: Install WSL (Ubuntu 18.04)

Step 3: Install Docker in the WSL

From within Ubuntu do normal install for Docker from their repository: https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-using-the-repository

Fast steps:

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
$ sudo usermod -aG docker $USER

Manual steps:

sudo apt-get update
sudo apt-get remove docker docker-engine docker.io  # just to be sure legacy docker stuff isn't around

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
   
sudo apt-get update

sudo apt-get install docker-ce

sudo usermod -aG docker $USER

Step 4: Install Docker-Compose in the WSL

https://github.com/docker/compose/releases/

export DOCKER_COMPOSE_VERSION=1.23.2
sudo curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` \
     -o /usr/local/bin/docker-compose \
     && sudo chmod +x /usr/local/bin/docker-compose

Step 5: Configure WSL to Connect to Docker for Windows

Add the following to .bashrc in WSL

export DOCKER_HOST=tcp://localhost:2375

The quick way of doing that

echo export DOCKER_HOST=tcp://localhost:2375 >> ~/.bashrc

Step 6: Verify

Close the WSL bash window and re-open it.

  • groups should list docker as a group in your user now
  • docker info should list details.
    • Permission denied error means you didn't get in docker group.
    • Unable to connect likely means DOCKER_HOST not set or Docker for Windows service is not actually running yet.
#!/bin/bash
apt_line="deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main"
source_file="/etc/apt/sources.list.d/ansible.list"
apt_key="93C4A3FD7BB9C367"
ansible_location=`which ansible` > /dev/null
if [ $? -eq 0 ]; then
echo "ansible already available at: $ansible_location"
echo "exiting setup"
exit 1
fi
if [ "$USER" != "root" ]; then
echo "this needs to be run as root"
exit 1
fi
if [ ! -f $source_file ]; then
echo "adding ansible apt source to: ${source_file}"
echo ${apt_line} >> $source_file
if [ $? -eq 1 ]; then
echo "append failed .. running as root? try to sudo"
exit 1
fi
else
echo "ansible apt source already setup"
fi
# launch dirmngr for apt-key management and allow it to be smart about connecting
# to network pub key services
ps -Aef | grep dirmngr > /dev/null
if [ $? -ne 0 ]; then
dirmngr --daemon # needed on winboxes to allow apt-key adv to work
fi
apt-key list | grep -w $apt_key 2> /dev/null
if [ $? -eq 0 ]; then
echo "apt key for ansible missing - adding $apt_key"
if [ ! -f /tmp/ansible.key ]; then
curl -o /tmp/ansible.key 'http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x93C4A3FD7BB9C367'
if [ $? -ne 0 ]; then
echo 'fetch of ansible key failed'
exit 1
fi
fi
# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $apt_key # failes in WLS
apt-key add /tmp/ansible.key
if [ $? -ne 0 ]; then
echo "adding key failed..."
exit 1
fi
else
echo "skipping anisble key setup - already installed"
fi
apt-get update && apt-get install -y ansible
REM assumes bash.exe is set to your default WSL environment you want
REM hint: wslconfig /l # list WSL environments
REM hint: wslconfig /setdefault Ubuntu-18.04 # set default
REM Script assumes passwordless sudo for a launch script of /boot.sh in the linux system
REM hint: visudo and then add NOPASSWD
REM
C:\Windows\System32\bash.exe -c "sudo /boot.sh"
REM if you only want
REM C:\Windows\System32\bash.exe -c "sudo /etc/init.d/ssh start"
#!/bin/bash
/etc/init.d/ssh start
if [ -f /etc/hosts.append ]; then
grep APPEND /etc/hosts > /dev/null
if [ $? -eq 0 ]; then
echo "/etc/hosts already has append content"
else
echo "appending /etc/hosts.append to /etc/hosts"
cat /etc/hosts.append >> /etc/hosts
fi
fi
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Users\Dayne\Documents\start-ssh.bat" & Chr(34), 0
Set WinScriptHost = Nothing
@chfanghr
Copy link

In win-start-linux.vbs,I think use %USERPROFILE% instead of "Dayne" is a better choice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment