Skip to contentSkip to conversation

openssh: Start key generation in multi-user mode

RFC In review Joshua Watt 2 participants 3 messages Sep 22, 2020

lore
Share & tools
lore
#0 lore raw

RFC 1/1 openssh: Start key generation in multi-user mode

The sshkeygen uses a lot of CPU time which delays booting (especially in the case where no SSH connection is going to happen), but we also want to be proactive about generating it early so that the first SSH login doesn't have to wait a long time.

Starting the key generation when the sockets are created (which is pretty early in the boot process) slows down the boot due to high CPU usage, so as a compromise start it no later than multi-user mode when things have typically settled down. Note that if an SSH connection comes in before multi-user.target is reached, key generation will be started immediately.

---
 meta/recipes-connectivity/openssh/openssh/sshd.socket        | 1 -
 meta/recipes-connectivity/openssh/openssh/[email protected]      | 1 +
 .../recipes-connectivity/openssh/openssh/sshdgenkeys.service | 5 +++++
 3 files changed, 6 insertions(+), 1 deletion(-)
meta/recipes-connectivity/openssh/openssh/sshd.socket +0 -1
index 8d76d62309..12c39b26b5 100644
@@ -1,6 +1,5 @@
 [Unit]
 Conflicts=sshd.service
-Wants=sshdgenkeys.service
 
 [Socket]
 ExecStartPre=@BASE_BINDIR@/mkdir -p /var/run/sshd
meta/recipes-connectivity/openssh/openssh/[email protected] +1 -0
index 9d9965e624..40fd38730a 100644
@@ -1,5 +1,6 @@
 [Unit]
 Description=OpenSSH Per-Connection Daemon
+Wants=sshdgenkeys.service
 After=sshdgenkeys.service
 
 [Service]
meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service +5 -0
index fd81793d51..52617ec275 100644
@@ -7,3 +7,8 @@ ExecStart=@LIBEXECDIR@/sshd_check_keys
 Type=oneshot
 RemainAfterExit=yes
 Nice=10
+
+[Install]
+# Start key generation no later than multi-user mode so that we can hopefully
+# have it generated before the first SSH connection
+WantedBy=multi-user.target
Signature · 1 line
2.28.0
#1 lore raw
Quoted 47 lines · Joshua Watt

The sshkeygen uses a lot of CPU time which delays booting (especially in the case where no SSH connection is going to happen), but we also want to be proactive about generating it early so that the first SSH login doesn't have to wait a long time.

Starting the key generation when the sockets are created (which is pretty early in the boot process) slows down the boot due to high CPU usage, so as a compromise start it no later than multi-user mode when things have typically settled down. Note that if an SSH connection comes in before multi-user.target is reached, key generation will be started immediately.

---
 meta/recipes-connectivity/openssh/openssh/sshd.socket        | 1 -
 meta/recipes-connectivity/openssh/openssh/[email protected]      | 1 +
 .../recipes-connectivity/openssh/openssh/sshdgenkeys.service | 5 +++++
 3 files changed, 6 insertions(+), 1 deletion(-)
meta/recipes-connectivity/openssh/openssh/sshd.socket +0 -1
index 8d76d62309..12c39b26b5 100644
@@ -1,6 +1,5 @@
 [Unit]
 Conflicts=sshd.service
-Wants=sshdgenkeys.service
 
 [Socket]
 ExecStartPre=@BASE_BINDIR@/mkdir -p /var/run/sshd
meta/recipes-connectivity/openssh/openssh/[email protected] +1 -0
index 9d9965e624..40fd38730a 100644
@@ -1,5 +1,6 @@
 [Unit]
 Description=OpenSSH Per-Connection Daemon
+Wants=sshdgenkeys.service
 After=sshdgenkeys.service
 
 [Service]
meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service +0 -0
index fd81793d51..52617ec275 100644

meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service ExecStart=@LIBEXECDIR@/sshd_check_keys · line 14

Quoted 8 lines
meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service +5 -0
@@ -7,3 +7,8 @@ ExecStart=@LIBEXECDIR@/sshd_check_keys
 Type=oneshot
 RemainAfterExit=yes
 Nice=10
+
+[Install]
+# Start key generation no later than multi-user mode so that we can hopefully
+# have it generated before the first SSH connection
+WantedBy=multi-user.target

This looks better than what we have. Please do something similar for dropbearkey.service as well.
I also wonder if we need After anchor but I would not worry too much about it.

Quoted 3 lines

--
2.28.0

#2 lore raw
Quoted 59 lines · Khem Raj
Quoted 13 lines · Joshua Watt

The sshkeygen uses a lot of CPU time which delays booting (especially in the case where no SSH connection is going to happen), but we also want to be proactive about generating it early so that the first SSH login doesn't have to wait a long time.

Starting the key generation when the sockets are created (which is pretty early in the boot process) slows down the boot due to high CPU usage, so as a compromise start it no later than multi-user mode when things have typically settled down. Note that if an SSH connection comes in before multi-user.target is reached, key generation will be started immediately.

Review

Quoted 8 lines
---
 meta/recipes-connectivity/openssh/openssh/sshd.socket        | 1 -
 meta/recipes-connectivity/openssh/openssh/[email protected]      | 1 +
 .../recipes-connectivity/openssh/openssh/sshdgenkeys.service | 5 +++++
 3 files changed, 6 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/meta/recipes-connectivity/openssh/openssh/sshd.socket

Review

Quoted 11 lines

index 8d76d62309..12c39b26b5 100644

meta/recipes-connectivity/openssh/openssh/sshd.socket +0 -1
@@ -1,6 +1,5 @@
 [Unit]
 Conflicts=sshd.service
-Wants=sshdgenkeys.service
 
 [Socket]
 ExecStartPre=@BASE_BINDIR@/mkdir -p /var/run/sshd
(quoted diff) +0 -0

b/meta/recipes-connectivity/openssh/openssh/[email protected]

Quoted 11 lines

index 9d9965e624..40fd38730a 100644

meta/recipes-connectivity/openssh/openssh/[email protected] +1 -0
@@ -1,5 +1,6 @@
 [Unit]
 Description=OpenSSH Per-Connection Daemon
+Wants=sshdgenkeys.service
 After=sshdgenkeys.service
 
 [Service]

diff --git

a/meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service b/meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service

meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service ExecStart=@LIBEXECDIR@/sshd_check_keys · line 12

Quoted 10 lines

index fd81793d51..52617ec275 100644

meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service +3 -0
@@ -7,3 +7,8 @@ ExecStart=@LIBEXECDIR@/sshd_check_keys
 Type=oneshot
 RemainAfterExit=yes
 Nice=10
+
+[Install]
+# Start key generation no later than multi-user mode so that we can

hopefully

Quoted 8 lines

meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service ExecStart=@LIBEXECDIR@/sshd_check_keys · line 14

Quoted 2 lines
meta/recipes-connectivity/openssh/openssh/sshdgenkeys.service +2 -0
+# have it generated before the first SSH connection
+WantedBy=multi-user.target

This looks better than what we have. Please do something similar for dropbearkey.service as well.
I also wonder if we need After anchor but I would not worry too much about it.

Yes, it may need After=multi-user.target, otherwise it might start earlier than you want

Quoted 4 lines
Quoted 3 lines

--
2.28.0