Skip to content

Commit 5d2e9c6

Browse files
committed
feat: add sshd / sshd-service-install
1 parent 204525e commit 5d2e9c6

File tree

4 files changed

+168
-51
lines changed

4 files changed

+168
-51
lines changed

sshd/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: OpenSSH (for Windows)
3+
homepage: https://webinstall.dev/sshd
4+
tagline: |
5+
OpenSSH: Window's built-in SSH implementation for remote login
6+
---
7+
8+
To update (replacing the current version) run `webi sudo`.
9+
10+
## Cheat Sheet
11+
12+
> Does the tedious work of installing, registering, and starting Windows' built-in OpenSSH Server (`sshd`)
13+
14+
As this requires Administrator permissions, you must run the command yourself:
15+
16+
```sh
17+
sshd-service-install
18+
```
19+
20+
### Files
21+
22+
These are the files / directories that are created and/or modified with this
23+
install:
24+
25+
```text
26+
~/.local/bin/sudo.bat
27+
~/.local/bin/sshd-service-install.bat
28+
```
29+
30+

sshd/install.ps1

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,52 @@
11
#!/usr/bin/env pwsh
22

3-
$Esc = [char]27
4-
$Warn = "${Esc}[1m[33m"
5-
$ResetAll = "${Esc}[0m"
6-
7-
# See
8-
# - <https://gist.github.com/HacDan/026fa8d7d4130fbbc2409d84c2d04143#load-public-keys>
9-
# - <https://techcommunity.microsoft.com/t5/itops-talk-blog/installing-and-configuring-openssh-on-windows-server-2019/ba-p/309540>
10-
# - <https://learn.microsoft.com/windows-server/administration/openssh/openssh_install_firstuse>
11-
12-
function InstallOpenSSHServer {
13-
$OpenSSHServer = Get-WindowsCapability -Online | `
14-
Where-Object -Property Name -Like "OpenSSH.Server*"
15-
IF (-Not ($OpenSSHServer.State -eq "Installed")) {
16-
Add-WindowsCapability -Online -Name $sshd.Name
17-
}
3+
$ErrorActionPreference = 'stop'
184

19-
$Sshd = Get-Service -Name "sshd"
20-
IF (-Not ($Sshd.Status -eq "Running")) {
21-
Start-Service "sshd"
22-
}
23-
IF (-Not ($Sshd.StartupType -eq "Automatic")) {
24-
Set-Service -Name "sshd" -StartupType "Automatic"
25-
}
5+
function Repair-MissingCommand {
6+
Param(
7+
[string]$Name,
8+
[string]$Package,
9+
[string]$Command
10+
)
2611

27-
$SshAgent = Get-Service -Name "ssh-agent"
28-
IF (-Not ($SshAgent.Status -eq "Running")) {
29-
Start-Service "ssh-agent"
30-
}
31-
IF (-Not ($SshAgent.StartupType -eq "Automatic")) {
32-
Set-Service -Name "ssh-agent" -StartupType "Automatic"
12+
Write-Host " Checking for $Name ..."
13+
$HasCommand = Get-Command -Name $Command -ErrorAction Silent
14+
IF ($HasCommand) {
15+
Return
3316
}
3417

35-
Install-Module -Force OpenSSHUtils -Scope AllUsers
18+
& $HOME\.local\bin\webi-pwsh.ps1 $Package
19+
$null = Sync-EnvPath
3620
}
3721

38-
function SelfElevate {
39-
Write-Host "${Warn}Installing 'sshd' requires Admin privileges${ResetAll}"
40-
Write-Host "Install will continue automatically in 5 seconds..."
41-
Sleep 5.0
42-
43-
# Self-elevate the script if required
44-
$CurUser = New-Object Security.Principal.WindowsPrincipal(
45-
[Security.Principal.WindowsIdentity]::GetCurrent()
22+
function Install-WebiHostedScript () {
23+
Param(
24+
[string]$Package,
25+
[string]$ScriptName
4626
)
47-
$IsAdmin = $CurUser.IsInRole(
48-
[Security.Principal.WindowsBuiltInRole]::Administrator
49-
)
50-
if ($IsAdmin) {
51-
Return 0
52-
}
53-
54-
$CurLoc = Get-Location
55-
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
56-
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
57-
Set-Location $CurLoc
58-
Exit 0
27+
$PwshName = "_${ScriptName}.ps1"
28+
$PwshUrl = "${Env:WEBI_HOST}/packages/${Package}/${ScriptName}.ps1"
29+
$PwshPath = "$HOME\.local\bin\${PwshName}"
30+
$OldPath = "$HOME\.local\bin\${ScriptName}.ps1"
31+
32+
$BatPath = "$HOME\.local\bin\${ScriptName}.bat"
33+
$PwshExec = "powershell -ExecutionPolicy Bypass"
34+
$Bat = "@echo off`r`n$PwshExec %USERPROFILE%\.local\bin\${PwshName} %*"
35+
36+
Invoke-DownloadUrl -Force -URL $PwshUrl -Path $PwshPath
37+
Set-Content -Path $BatPath -Value $Bat
38+
Write-Host " Created alias ${BatPath}"
39+
Write-Host " to run ${PwshPath}"
40+
41+
# fix for old installs
42+
Remove-Item -Path $OldPath -Force -ErrorAction Ignore
5943
}
6044

61-
SelfElevate
62-
InstallOpenSSHServer
45+
46+
Repair-MissingCommand -Name "sudo (RunAs alias)" -Package "sudo" -Command "sudo"
47+
Install-WebiHostedScript -Package "sshd" -ScriptName "sshd-service-install"
48+
49+
Write-Output ""
50+
Write-Output "${TTask}Copy, paste, and run${TReset} the following to install sshd as a system service"
51+
Write-Output " ${TCmd}sshd-service-install${TReset}"
52+
Write-Output ""

sshd/install.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
__install_sshd() {
2+
my_os="$(uname -s)"
3+
if test "Darwin" = "${my_os}"; then
4+
echo >&2 ""
5+
echo >&2 "Copy, paste, and run the following to enable the built-in sshd:"
6+
echo >&2 " sudo systemsetup -f -setremotelogin on"
7+
echo >&2 " sudo systemsetup -getremotelogin"
8+
echo >&2 ""
9+
exit 1
10+
fi
11+
12+
echo >&2 "Install and enable sshd using your system package manager:"
13+
my_cmd=""
14+
if test command -v sudo > /dev/null; then
15+
my_cmd="sudo "
16+
fi
17+
18+
if test command -v apt > /dev/null; then
19+
echo " ${my_cmd}apt install -y openssh-server"
20+
echo " ${my_cmd}systemctl enable ssh"
21+
echo " ${my_cmd}systemctl start ssh"
22+
elif test command -v yum > /dev/null; then
23+
echo " ${my_cmd}yum -y install openssh-server"
24+
echo " ${my_cmd}systemctl enable ssh"
25+
echo " ${my_cmd}systemctl start ssh"
26+
elif test command -v apk > /dev/null; then
27+
echo " ${my_cmd}apk add --no-cache openssh"
28+
echo " ${my_cmd}service sshd added to runlevel default"
29+
echo " ${my_cmd}service sshd start"
30+
else
31+
echo " (unknown package manager / init daemon)"
32+
fi
33+
34+
exit 1
35+
}

sshd/sshd-service-install.ps1

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env pwsh
2+
3+
$Esc = [char]27
4+
$Warn = "${Esc}[1m[33m"
5+
$ResetAll = "${Esc}[0m"
6+
7+
# See
8+
# - <https://gist.github.com/HacDan/026fa8d7d4130fbbc2409d84c2d04143#load-public-keys>
9+
# - <https://techcommunity.microsoft.com/t5/itops-talk-blog/installing-and-configuring-openssh-on-windows-server-2019/ba-p/309540>
10+
# - <https://learn.microsoft.com/windows-server/administration/openssh/openssh_install_firstuse>
11+
12+
function InstallOpenSSHServer {
13+
$OpenSSHServer = Get-WindowsCapability -Online | `
14+
Where-Object -Property Name -Like "OpenSSH.Server*"
15+
IF (-Not ($OpenSSHServer.State -eq "Installed")) {
16+
Add-WindowsCapability -Online -Name $sshd.Name
17+
}
18+
19+
$Sshd = Get-Service -Name "sshd"
20+
IF (-Not ($Sshd.Status -eq "Running")) {
21+
Start-Service "sshd"
22+
}
23+
IF (-Not ($Sshd.StartupType -eq "Automatic")) {
24+
Set-Service -Name "sshd" -StartupType "Automatic"
25+
}
26+
27+
$SshAgent = Get-Service -Name "ssh-agent"
28+
IF (-Not ($SshAgent.Status -eq "Running")) {
29+
Start-Service "ssh-agent"
30+
}
31+
IF (-Not ($SshAgent.StartupType -eq "Automatic")) {
32+
Set-Service -Name "ssh-agent" -StartupType "Automatic"
33+
}
34+
35+
Install-Module -Force OpenSSHUtils -Scope AllUsers
36+
}
37+
38+
function SelfElevate {
39+
Write-Host "${Warn}Installing 'sshd' requires Admin privileges${ResetAll}"
40+
Write-Host "Install will continue automatically in 5 seconds..."
41+
Sleep 5.0
42+
43+
# Self-elevate the script if required
44+
$CurUser = New-Object Security.Principal.WindowsPrincipal(
45+
[Security.Principal.WindowsIdentity]::GetCurrent()
46+
)
47+
$IsAdmin = $CurUser.IsInRole(
48+
[Security.Principal.WindowsBuiltInRole]::Administrator
49+
)
50+
if ($IsAdmin) {
51+
Return 0
52+
}
53+
54+
$CurLoc = Get-Location
55+
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
56+
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
57+
Set-Location $CurLoc
58+
Exit 0
59+
}
60+
61+
SelfElevate
62+
InstallOpenSSHServer

0 commit comments

Comments
 (0)