Watch & Learn

Debugwar Blog

Step in or Step over, this is a problem ...

Build a command line working environment based on OpenSSH + WSL on Windows

2023-07-19 @ UTC+0

The author’s main working environment has always been Linux, but in some scenarios, it is still impossible to bypass the Windows system. Therefore, the author has a Windows notebook that is always on standby at home. When the author has a need, he will connect back to the VPN network at home to use this terminal. At this time, the problem arises. Because the author is extremely familiar with the Linux command line environment and the command line tools under Windows are relatively scarce, is there a way to comfortably use Windows to work in the command line scenario? The answer is yes, let me explain.

Install WSL

Install WSL using PowerShell, execute Reference 1 in PowerShell with administrator privileges Reference 1

  1. wsl --install

After the installation is complete, WSL will generate the bash.exe we need under System32:


Install OpenSSH server

Currently, the Windows platform natively supports the OpenSSH server and no longer requires additional third-party software packages. It can be installed through PowerShell Reference 2, and the following command can be executed with administrator privileges. Remember to set the service to start automatically.

  1. Windows PowerShell
  2. Copyright (C) Microsoft Corporation。All rights reserved。
  3. Try the new cross-platform PowerShell https://aka.ms/pscore6
  4. PS C:\Windows\system32> Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
  5. Name : OpenSSH.Client~~~~0.0.1.0
  6. State : Installed
  7. Name : OpenSSH.Server~~~~0.0.1.0
  8. State : NotPresent
  9. PS C:\Windows\system32> Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
  10. Path :
  11. Online : True
  12. RestartNeeded : False
  13. PS C:\Windows\system32> Set-Service -Name sshd -StartupType 'Automatic'
  14. PS C:\Windows\system32> Start-Service sshd

Finally, set the default command line of OpenSSH Server to WSL’s bash.exe and restart the SSH service.


  1. PS C:\Windows\system32> New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\bash.exe" -PropertyType String -Force
  2. DefaultShell : C:\Windows\System32\bash.exe
  3. PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH
  4. PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
  5. PSChildName : OpenSSH
  6. PSDrive : HKLM
  7. PSProvider : Microsoft.PowerShell.Core\Registry
  8. PS C:\Windows\system32> ReStart-Service sshd

Finally, it’s OK to use the main notebook to connect to this machine.


Last thing

The Ubuntu that comes with WSL seems to have some problems. There is a certain probability that it will get stuck when starting bash.exe. I am using a third-party created Arch distribution, which can be obtained at the following address:https://github.com/yuk7/ArchWSL

If you want a colored Prompt, you need to set PS1 through .bashrc. The configuration of WSL is a bit more troublesome and requires the configuration of the following two files:

  1. [root@VPNBook ~]# cat ~/.bash_profile
  2. source ~/.bashrc
  3. [root@VPNBook ~]# cat ~/.bashrc
  4. export PS1="\[\e[01;37m\]┏╸\[\e[00;37m\]\u\[\e[01;37m\]@\[\e[01;36m\]\h \[\e[01;35m\]\t \[\e[38;5;214m\]\w\n\[\e[01;37m\]┗╸\[\e[01;32m\]\\$ \[\e[00m\]"
  5. alias ll="ls -l --time-style=long-iso --color"

References

  1. Manual installation steps for older versions of WSL
  2. Get started with OpenSSH for Windows
  3. OpenSSH Server configuration for Windows Server and Windows
Catalog
Install WSL
Install OpenSSH server
Last thing
References

CopyRight (c) 2020 - 2025 Debugwar.com

Designed by Hacksign