-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sshd / sshd-service-install
- Loading branch information
Showing
4 changed files
with
168 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,52 @@ | ||
#!/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 | ||
} | ||
$ErrorActionPreference = 'stop' | ||
|
||
$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" | ||
} | ||
function Repair-MissingCommand { | ||
Param( | ||
[string]$Name, | ||
[string]$Package, | ||
[string]$Command | ||
) | ||
|
||
$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" | ||
Write-Host " Checking for $Name ..." | ||
$HasCommand = Get-Command -Name $Command -ErrorAction Silent | ||
IF ($HasCommand) { | ||
Return | ||
} | ||
|
||
Install-Module -Force OpenSSHUtils -Scope AllUsers | ||
& $HOME\.local\bin\webi-pwsh.ps1 $Package | ||
$null = Sync-EnvPath | ||
} | ||
|
||
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() | ||
function Install-WebiHostedScript () { | ||
Param( | ||
[string]$Package, | ||
[string]$ScriptName | ||
) | ||
$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 | ||
$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 | ||
} | ||
|
||
SelfElevate | ||
InstallOpenSSHServer | ||
|
||
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 "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |