Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sshd (script for Windows, docs for mac / linux) #744

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sshd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: OpenSSH (for Windows)
homepage: https://webinstall.dev/sshd
tagline: |
OpenSSH: Window's built-in SSH implementation for remote login
---

To update (replacing the current version) run `webi sudo`.

## Cheat Sheet

> Does the tedious work of installing, registering, and starting Windows' built-in OpenSSH Server (`sshd`)

As this requires Administrator permissions, you must run the command yourself:

```sh
sshd-service-install
```

### Files

These are the files / directories that are created and/or modified with this
install:

```text
~/.local/bin/sudo.bat
~/.local/bin/sshd-service-install.bat
```


52 changes: 52 additions & 0 deletions sshd/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env pwsh

$ErrorActionPreference = 'stop'

function Repair-MissingCommand {
Param(
[string]$Name,
[string]$Package,
[string]$Command
)

Write-Host " Checking for $Name ..."
$HasCommand = Get-Command -Name $Command -ErrorAction Silent
IF ($HasCommand) {
Return
}

& $HOME\.local\bin\webi-pwsh.ps1 $Package
$null = Sync-EnvPath
}

function Install-WebiHostedScript () {
Param(
[string]$Package,
[string]$ScriptName
)
$PwshName = "_${ScriptName}.ps1"
$PwshUrl = "${Env:WEBI_HOST}/packages/${Package}/${ScriptName}.ps1"
$PwshPath = "$HOME\.local\bin\${PwshName}"
$OldPath = "$HOME\.local\bin\${ScriptName}.ps1"

$BatPath = "$HOME\.local\bin\${ScriptName}.bat"
$PwshExec = "powershell -ExecutionPolicy Bypass"
$Bat = "@echo off`r`n$PwshExec %USERPROFILE%\.local\bin\${PwshName} %*"

Invoke-DownloadUrl -Force -URL $PwshUrl -Path $PwshPath
Set-Content -Path $BatPath -Value $Bat
Write-Host " Created alias ${BatPath}"
Write-Host " to run ${PwshPath}"

# fix for old installs
Remove-Item -Path $OldPath -Force -ErrorAction Ignore
}


Repair-MissingCommand -Name "sudo (RunAs alias)" -Package "sudo" -Command "sudo"
Install-WebiHostedScript -Package "sshd" -ScriptName "sshd-service-install"

Write-Output ""
Write-Output "${TTask}Copy, paste, and run${TReset} the following to install sshd as a system service"
Write-Output " ${TCmd}sshd-service-install${TReset}"
Write-Output ""
35 changes: 35 additions & 0 deletions sshd/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
__install_sshd() {
my_os="$(uname -s)"
if test "Darwin" = "${my_os}"; then
echo >&2 ""
echo >&2 "Copy, paste, and run the following to enable the built-in sshd:"
echo >&2 " sudo systemsetup -f -setremotelogin on"
echo >&2 " sudo systemsetup -getremotelogin"
echo >&2 ""
exit 1
fi

echo >&2 "Install and enable sshd using your system package manager:"
my_cmd=""
if test command -v sudo > /dev/null; then
my_cmd="sudo "
fi

if test command -v apt > /dev/null; then
echo " ${my_cmd}apt install -y openssh-server"
echo " ${my_cmd}systemctl enable ssh"
echo " ${my_cmd}systemctl start ssh"
elif test command -v yum > /dev/null; then
echo " ${my_cmd}yum -y install openssh-server"
echo " ${my_cmd}systemctl enable ssh"
echo " ${my_cmd}systemctl start ssh"
elif test command -v apk > /dev/null; then
echo " ${my_cmd}apk add --no-cache openssh"
echo " ${my_cmd}service sshd added to runlevel default"
echo " ${my_cmd}service sshd start"
else
echo " (unknown package manager / init daemon)"
fi

exit 1
}
62 changes: 62 additions & 0 deletions sshd/sshd-service-install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env pwsh

$Esc = [char]27
$Warn = "${Esc}[1m[33m"
$ResetAll = "${Esc}[0m"

# See
# - <https://gist.github.com/HacDan/026fa8d7d4130fbbc2409d84c2d04143#load-public-keys>
# - <https://techcommunity.microsoft.com/t5/itops-talk-blog/installing-and-configuring-openssh-on-windows-server-2019/ba-p/309540>
# - <https://learn.microsoft.com/windows-server/administration/openssh/openssh_install_firstuse>

function InstallOpenSSHServer {
$OpenSSHServer = Get-WindowsCapability -Online | `
Where-Object -Property Name -Like "OpenSSH.Server*"
IF (-Not ($OpenSSHServer.State -eq "Installed")) {
Add-WindowsCapability -Online -Name $sshd.Name
}

$Sshd = Get-Service -Name "sshd"
IF (-Not ($Sshd.Status -eq "Running")) {
Start-Service "sshd"
}
IF (-Not ($Sshd.StartupType -eq "Automatic")) {
Set-Service -Name "sshd" -StartupType "Automatic"
}

$SshAgent = Get-Service -Name "ssh-agent"
IF (-Not ($SshAgent.Status -eq "Running")) {
Start-Service "ssh-agent"
}
IF (-Not ($SshAgent.StartupType -eq "Automatic")) {
Set-Service -Name "ssh-agent" -StartupType "Automatic"
}

Install-Module -Force OpenSSHUtils -Scope AllUsers
}

function SelfElevate {
Write-Host "${Warn}Installing 'sshd' requires Admin privileges${ResetAll}"
Write-Host "Install will continue automatically in 5 seconds..."
Sleep 5.0

# Self-elevate the script if required
$CurUser = New-Object Security.Principal.WindowsPrincipal(
[Security.Principal.WindowsIdentity]::GetCurrent()
)
$IsAdmin = $CurUser.IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator
)
if ($IsAdmin) {
Return 0
}

$CurLoc = Get-Location
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Set-Location $CurLoc
Exit 0
}

SelfElevate
InstallOpenSSHServer
Loading