Talk to your Windows machine with SSH!

Accessing VMs with RDP is tedious, slow and hacky DevOps way.

Here's how you can set up your Windows based environment with OpenSSH.

WSL

Using Windows Subsystem for Linux on client machine can be quite helpful as most of SSH related tools don't come out to Windows. I like to use Ubuntu inside my Windows.

SSH Server

# Run as Administrator

$openSshStatus = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Server*'
if($openSshStatus[0].State -eq "NotPresent"){ 
    Write-Output "OpenSSH server not detected. Processing to installation..." 
    $installResult = Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
    if($installResult.Online -eq $true){
        Write-Output "Installation succeeded."
    }
} else {
    Write-Output "OpenSSH server is already installed."
}

$sshd = Get-Service sshd # SSH Server deamon
if($sshd.Status -ne "Running"){
    # Register
    Set-Service -Name sshd -StartupType 'Automatic'

    # Confirm the Firewall rule is configured. It should be created automatically by setup. 
    $fwRule = Get-NetFirewallRule -Name *ssh*
    if($fwRule -eq $null) {
        # There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled
        # If the firewall does not exist, create one
        New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
    }

    Start-Service sshd
} else {
    Write-Output "OpenSSH server is running."
}

If you'd like to log in with Windows user account (instead of using RSA keys), you can it like this:

ssh YOUR_USER_ACCOUNT_NAME@192.168.0.42

Package Manager

Default (and the best), community driven, package manager for Windows is Chocolatey. You can install it with PowerShell. Follow instructions here or simply copy & paste:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Text Editor

Windows default editor - Notepad - cannot be open within terminal.

Nano is popular text editor that can work in GUI-less mode. You can install it with Chocolatey:

cinst nano -y

Node.JS

To install Node.js, use NVM (Node Version Manager) for Windows.

Here's you can install it with just PowerShell as Choco package seems to be broken :(

if(Test-Path "C:\Program Files\nodejs") {
    rm "C:\Program Files\nodejs" -Recurse -Force
}

wget https://github.com/coreybutler/nvm-windows/releases/download/1.1.7/nvm-setup.zip -OutFile "~/Downloads/nvm-setup.zip"
Expand-Archive "~/Downloads/nvm-setup.zip" -DestinationPath "~/Downloads/nvm-setup"
~/Downloads/nvm-setup/nvm-setup.exe /SILENT /SUPPRESSMSGBOXES /WAIT
sleep 10
rm "~/Downloads/nvm-setup.zip"
rm "~/Downloads/nvm-setup" -Recurse

After that install Node.JS in specific version

nvm -v
nvm install 12.18.0
nvm use 12.18.0
comments powered by Disqus