Skip to content

Commit 83f6dca

Browse files
committed
Add optional SSH host key persistence
Adds a new boolean option, `persist_ssh_host_keys`, to allow users to persist SSH host keys in the /data directory and that secure permissions are set.
1 parent 0d56868 commit 83f6dca

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

vscode-remote-ssh/DOCS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ Enables you to connect to Home Assistant via Visual Studio Code Remote SSH.
66

77
### Option: `ssh_keys`
88

9-
List of ssh keys that are allowed to connect
9+
List of ssh keys that are allowed to connect
10+
11+
### Option: `persist_ssh_host_keys` (Optional)
12+
13+
If set to `true`, the add-on will store the SSH host keys in a persistent location (`/data`). This prevents the host key from changing after an add-on update or restart, avoiding "REMOTE HOST IDENTIFICATION HAS CHANGED" errors on the client. Defaults to `false`.

vscode-remote-ssh/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ _Example configuration_:
2222
Addon:
2323
```yaml
2424
ssh_keys: ["ssh-rsa yourverylongsshkey", "ssh-ed25519 andanotherone"]
25+
26+
# Optional:
27+
persist_ssh_host_keys: true
2528
```
29+
_The `persist_ssh_host_keys` option, when `true`, also stores the SSH host keys in the persistent `/data` directory. This maintains a stable host identity, which is standard practice for SSH servers and prevents `known_hosts` errors on the client._
2630

2731
VS-Code ssh config ($HOME/.ssh/config):
2832
```

vscode-remote-ssh/config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: 1.0.43
33
slug: vscode-remote-ssh
44
description: Enables you to connect to Home Assistant via Visual Studio Code Remote
55
SSH.
6-
url: https://github.com/mietzen/hassio-addons/tree/master/vscode-remote-ssh
6+
url: https://github.com/mietzen/hassio-addons/tree/main/vscode-remote-ssh
77
arch:
88
- armhf
99
- armv7
@@ -25,7 +25,9 @@ ports:
2525
22/tcp: 22000
2626
options:
2727
ssh_keys: []
28+
persist_ssh_host_keys: false
2829
schema:
2930
ssh_keys:
3031
- str
32+
persist_ssh_host_keys: bool?
3133
log_level: list(debug|info|warning|error)?

vscode-remote-ssh/rootfs/run.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,37 @@ bashio::log.notice 'Symlinking home directory to persistent storage.'
1212
rm -rf /root
1313
ln -s /data/root /root
1414

15+
# Conditionally persist SSH host keys to survive container updates
16+
if bashio::config.true 'persist_ssh_host_keys'; then
17+
bashio::log.notice "Host key persistence is enabled."
18+
19+
# On first run, move the original /etc/ssh directory to the persistent /data location
20+
if ! [ -d /data/ssh ]; then
21+
bashio::log.notice 'Initializing persistent SSH directory from /etc/ssh...'
22+
mv /etc/ssh /data/ssh
23+
fi
24+
25+
# Ensure the standard /etc/ssh path is always a symlink to our persistent storage
26+
bashio::log.notice 'Linking /etc/ssh to persistent storage at /data/ssh.'
27+
rm -rf /etc/ssh
28+
ln -s /data/ssh /etc/ssh
29+
30+
# If no host keys exist in the persistent directory, generate the full default set.
31+
if ! find /data/ssh -name "ssh_host_*_key" -print -quit | grep -q .; then
32+
bashio::log.warning 'No SSH host keys found in persistent storage. Generating new set for first-time use...'
33+
ssh-keygen -A
34+
bashio::log.notice 'Default set of host keys generated in /data/ssh.'
35+
fi
36+
37+
# Enforce secure permissions on the host keys and configuration
38+
bashio::log.notice 'Verifying permissions for persistent SSH files...'
39+
chmod 600 /data/ssh/ssh_host_*_key 2>/dev/null || true
40+
chmod 644 /data/ssh/ssh_host_*_key.pub 2>/dev/null || true
41+
chmod 644 /data/ssh/sshd_config 2>/dev/null || true
42+
else
43+
bashio::log.notice "Host key persistence is disabled. Keys will be ephemeral."
44+
fi
45+
1546
# Check ssh_keys
1647
if bashio::config.is_empty 'ssh_keys'; then
1748
bashio::log.fatal 'Invalid configuration.'

vscode-remote-ssh/translations/de.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ configuration:
22
ssh_keys:
33
name: Autorisierte SSH Schlüssel
44
description: SSH Schlüssel denen es erlaubt ist sich mit dem Visual Studio Code Remote SSH Server zu verbinden.
5+
persist_ssh_host_keys:
6+
name: SSH-Host-Schlüssel beibehalten
7+
description: Wenn auf 'true' gesetzt, werden die SSH-Host-Schlüssel im /data-Ordner gespeichert, um Container-Updates zu überstehen.

vscode-remote-ssh/translations/en.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ configuration:
22
ssh_keys:
33
name: Authorized SSH Keys
44
description: SSH keys that are authorized to connect to the Visual Studio Code Remote SSH Server.
5+
persist_ssh_host_keys:
6+
name: Persist SSH Host Keys
7+
description: If true, the SSH host keys will be persisted in the /data folder to survive container updates.

0 commit comments

Comments
 (0)