config-central/docs/advanced-remote-access.md

319 lines
12 KiB
Markdown

# Advanced remote access from CachyOS
Use Windows OpenSSH Server for routine remote administration. Windows 10 1809,
Windows 11, and current Windows Server releases provide OpenSSH as an optional
component, while CachyOS already uses the standard OpenSSH client tooling. This
gives each desktop a normal `ssh` shell plus `scp` and `sftp` file transfer.
The Ruby program you may remember is
[Evil-WinRM](https://rubygems.org/gems/evil-winrm). It is primarily presented as
a hacking and penetration-testing WinRM shell. It can connect to an existing
WinRM deployment, but it is unnecessary here: cross-platform WinRM requires
additional listener, authentication, and certificate decisions that OpenSSH
avoids.
## Quick temporary access
For a debugging session, paste this single command into Windows PowerShell:
```powershell
irm 'https://proxy.dandaedre.com/gitea/Beffroi/config-central/raw/branch/main/tools/TemporarySsh.ps1' | iex
```
Approve the UAC prompt. When no SSH server exists, the helper first uses WinGet's
`Microsoft.OpenSSH.Preview` MSI because it avoids the slow Windows servicing
stack. If WinGet is unavailable or that install fails, it falls back to the
supported inbox capability through DISM, which displays servicing progress but
can still take several minutes. The helper then starts SSH and creates a
temporary firewall rule limited to Private networks and the local subnet. It
prints the exact second command to run from CachyOS, for example:
```bash
ssh -t Alice@192.168.1.42 powershell.exe -NoLogo -NoProfile
```
Sign in with the Windows account password. A Windows Hello PIN cannot
authenticate an SSH connection. If the helper reports that no Private network
is active, change the trusted LAN's Windows network profile to Private before
connecting; it deliberately leaves SSH unreachable on Public networks.
A plain `ssh Alice@192.168.1.42` login normally opens `cmd.exe`, where
PowerShell aliases such as `irm` are not available. Type `powershell.exe` in an
existing session to switch shells, or use the command above to enter PowerShell
directly.
When debugging is finished, run this from Windows PowerShell, locally or through
the SSH shell:
```powershell
$env:CONFIG_CENTRAL_TEMP_SSH='disable'; irm 'https://proxy.dandaedre.com/gitea/Beffroi/config-central/raw/branch/main/tools/TemporarySsh.ps1' | iex
```
The disable command removes the temporary rule and restores the service and
standard OpenSSH firewall rule to the state recorded on the first enable. It
leaves the installed OpenSSH package or capability in place, so the next
temporary session starts quickly. Use the permanent key setup below once remote
access becomes a regular workflow.
The WinGet build comes from Microsoft's OpenSSH GitHub releases and is supported
through that project rather than Windows enterprise support. To require the
Windows-supported inbox capability, select it explicitly; expect the slower
servicing step:
```powershell
$env:CONFIG_CENTRAL_TEMP_SSH_INSTALL='Inbox'; irm 'https://proxy.dandaedre.com/gitea/Beffroi/config-central/raw/branch/main/tools/TemporarySsh.ps1' | iex
```
Microsoft documents the [inbox capability installation](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse),
and the OpenSSH project documents its [WinGet/MSI distribution](https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH).
## Network boundary
Use these instructions on a trusted private LAN. Do not forward TCP port 22 from
the internet to a desktop. When access is needed away from home, connect the
machines through a VPN first and allow SSH only over that private network.
Give each Windows machine a DHCP reservation or a stable local DNS name. The
examples below use `win-office` and the Windows account `Alice`; replace both.
## Install the Windows SSH server
On each Windows desktop, open PowerShell as Administrator and run. If the quick
helper already installed an SSH server, the existing service is reused:
```powershell
if (-not (Get-Service -Name sshd -ErrorAction SilentlyContinue)) {
$capability = Get-WindowsCapability -Online -Name 'OpenSSH.Server~~~~0.0.1.0'
if ($capability.State -ne 'Installed') {
Add-WindowsCapability -Online -Name 'OpenSSH.Server~~~~0.0.1.0'
}
}
Set-Service -Name sshd -StartupType Automatic
Start-Service -Name sshd
if (-not (Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -ErrorAction SilentlyContinue)) {
New-NetFirewallRule `
-Name 'OpenSSH-Server-In-TCP' `
-DisplayName 'OpenSSH Server (sshd)' `
-Enabled True `
-Profile Private `
-Direction Inbound `
-Protocol TCP `
-Action Allow `
-LocalPort 22 `
-RemoteAddress LocalSubnet
}
else {
Set-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -Enabled True -Profile Private
Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' |
Get-NetFirewallAddressFilter |
Set-NetFirewallAddressFilter -RemoteAddress LocalSubnet
}
```
Microsoft documents the optional-component and service setup in
[Get started with OpenSSH Server for Windows](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse).
Confirm that the Windows network is marked **Private**, then test password login
from CachyOS before changing authentication settings:
```bash
ssh Alice@win-office
```
`LocalSubnet` limits the firewall rule to the directly connected network. When
using a VPN subnet instead, replace it with that subnet's CIDR range.
## Add a dedicated SSH key
Install the client tools on CachyOS if they are not already present:
```bash
sudo pacman -S --needed openssh
```
Create a separate key on CachyOS and protect it with a passphrase:
```bash
ssh-keygen -t ed25519 -a 100 -f ~/.ssh/config-central
cat ~/.ssh/config-central.pub
```
Copy the single printed `ssh-ed25519 ...` line. For a standard Windows user,
open PowerShell as that user and save the line as follows:
```powershell
$publicKey = 'ssh-ed25519 AAAA... replace-with-the-complete-key'
$sshDirectory = Join-Path $HOME '.ssh'
New-Item -ItemType Directory -Path $sshDirectory -Force | Out-Null
$authorizedKeys = Join-Path $sshDirectory 'authorized_keys'
$existingKeys = if (Test-Path $authorizedKeys) { [IO.File]::ReadAllLines($authorizedKeys) } else { @() }
if ($publicKey -notin $existingKeys) {
[IO.File]::WriteAllLines($authorizedKeys, @($existingKeys) + $publicKey, [Text.UTF8Encoding]::new($false))
}
```
Windows OpenSSH uses a shared file for accounts that belong to the local
Administrators group. For such an account, run this instead from an elevated
PowerShell window:
```powershell
$publicKey = 'ssh-ed25519 AAAA... replace-with-the-complete-key'
$authorizedKeys = Join-Path $env:ProgramData 'ssh\administrators_authorized_keys'
$existingKeys = if (Test-Path $authorizedKeys) { [IO.File]::ReadAllLines($authorizedKeys) } else { @() }
if ($publicKey -notin $existingKeys) {
[IO.File]::WriteAllLines($authorizedKeys, @($existingKeys) + $publicKey, [Text.UTF8Encoding]::new($false))
}
icacls.exe $authorizedKeys /inheritance:r /grant '*S-1-5-32-544:F' /grant '*S-1-5-18:F'
```
The different key locations and required administrator-file permissions come
from Microsoft's
[Windows OpenSSH key-management guide](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement).
Test the key from a second CachyOS terminal while the password-authenticated
session remains open:
```bash
ssh -i ~/.ssh/config-central Alice@win-office
```
Only after key login works, edit `C:\ProgramData\ssh\sshd_config` as
Administrator and set:
```text
PubkeyAuthentication yes
PasswordAuthentication no
AllowUsers alice
```
Replace `alice` with the exact login you successfully tested. Windows OpenSSH
expects names in `AllowUsers` to be lowercase; omit that line until the correct
local or domain account spelling is known. Validate the configuration and
restart the service without closing the working session:
```powershell
& "$env:WINDIR\System32\OpenSSH\sshd.exe" -t
if ($LASTEXITCODE -ne 0) { throw 'Invalid sshd_config; do not restart sshd.' }
Restart-Service sshd
```
Open another new connection before ending the original session. This prevents a
configuration mistake from locking you out.
## Use short machine names from CachyOS
Create or edit `~/.ssh/config`:
```sshconfig
Host cc-office
HostName win-office
User Alice
IdentityFile ~/.ssh/config-central
IdentitiesOnly yes
Host cc-living-room
HostName 192.168.1.52
User Alice
IdentityFile ~/.ssh/config-central
IdentitiesOnly yes
```
Protect the files and connect by alias:
```bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config ~/.ssh/config-central
ssh cc-office
```
The first connection displays the server host-key fingerprint. Compare it with
the fingerprint shown locally on Windows before accepting it:
```powershell
Get-ChildItem $env:ProgramData\ssh\ssh_host_*_key.pub |
ForEach-Object { ssh-keygen.exe -lf $_.FullName }
```
## Use Config Central remotely
Once the alias works, most inspection and config-only operations need no trip to
the Windows desktop:
```bash
ssh cc-office 'cc doctor'
ssh cc-office 'cc configs'
ssh cc-office 'cc configs -Config xnviewmp-settings -Json'
ssh cc-office 'cc status -ConfigsOnly'
ssh cc-office 'cc sync -ConfigsOnly'
```
Commit and push repository edits from CachyOS, then run `cc sync -ConfigsOnly`
on each desktop. Keep package installation and other UAC-dependent operations
for an interactive Windows session unless the SSH account already has the
required elevated token; a remote shell cannot approve a secure-desktop UAC
prompt.
Use SFTP for deliberate file retrieval. `cc configs -Config NAME` shows the live
path before copying anything:
```bash
sftp cc-office
sftp> get C:/Users/Alice/AppData/Roaming/XnViewMP/xnview.ini ./xnview-office.ini
sftp> exit
```
Review retrieved files locally and copy only the portable values into the
repository declaration. In particular, never retrieve and commit the complete
Nextcloud configuration.
## Optional PowerShell as the login shell
Windows initially opens `cmd.exe` for an SSH login. To make PowerShell 7 the
default, run this in an elevated Windows PowerShell session:
```powershell
$pwsh = (Get-Command pwsh.exe -ErrorAction Stop).Source
New-Item -Path 'HKLM:\SOFTWARE\OpenSSH' -Force | Out-Null
New-ItemProperty `
-Path 'HKLM:\SOFTWARE\OpenSSH' `
-Name DefaultShell `
-Value $pwsh `
-PropertyType String `
-Force | Out-Null
Restart-Service sshd
```
Microsoft documents this registry setting in
[OpenSSH Server configuration for Windows](https://learn.microsoft.com/en-us/windows-server/administration/OpenSSH/openssh-server-configuration).
For structured PowerShell sessions instead of a normal terminal, configure the
PowerShell SSH subsystem as described in Microsoft's
[PowerShell remoting over SSH guide](https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/ssh-remoting-in-powershell),
then use `Enter-PSSession -HostName ...` from PowerShell 7 on CachyOS. The normal
SSH shell is sufficient for Config Central commands and has fewer moving parts.
## Troubleshooting
On Windows, check the service and listening port:
```powershell
Get-Service sshd
Get-NetTCPConnection -LocalPort 22 -State Listen
Get-NetFirewallRule -Name OpenSSH-Server-In-TCP
```
Run `ssh -vvv cc-office` on CachyOS for client diagnostics. Windows records SSH
events under **Applications and Services Logs > OpenSSH > Operational** in Event
Viewer. Keep one known-working session open while changing keys or
`sshd_config`.
If disabling fails, the helper now shows the underlying administrator error
instead of only an exit code. Retry from a local administrator PowerShell window.
The saved restoration state at `%ProgramData%\ConfigCentral\temporary-ssh.json`
is retained if firewall restoration or launching the service-stop helper fails.
Do not delete it before retrying. Disabling from SSH may disconnect the session
when the delayed service stop runs. An SSH service that was already running
before temporary access is intentionally left running.