It is information for people who meet all requirements.
You can solve the above problem by the following methods.
This technique can be used not only for sshd, but also for starting other daemons that utilize host-specific information. (However, you can use DNS or DHCP for IP and host name.)
All work is done on an NFS server, chrooting to the OS image distributed to the nodes. Below, it is assumed that the host name of the diskless node is host1 to host4.
For security, keep all keys in / root / host_keys.
# mkdir /root/host_keys; cd /root/host_keys
# for N in `seq 1 4`; do HN="host${N}"; mkdir ${HN}; for T in rsa ecdsa ed25519; do ssh-keygen -h -t ${T} -C ${HN} -N '' -f ${HN}/ssh_host_${T}_key; done; done
# mkdir /var/lib/sshd
# vim /etc/fstab
(The changes are as follows)
#
Add the following line to / etc / fstab
tmfps /var/lib/sshd tmpfs defaults,mode=700 0 0
# vi /etc/ssh/sshd_config
(The changes are as follows)
#
Changes (enabled and changed HostKey entry commented out by default)
(abridgement)
HostKey /var/lib/sshd/ssh_host_rsa_key
HostKey /var/lib/sshd/ssh_host_ecdsa_key
HostKey /var/lib/sshd/ssh_host_ed25519_key
(abridgement)
In order to handle the timing properly, create a new systemd unit file and format it to call a dedicated script from here.
The file name can be anything, but it should be / usr / local / sbin / copy_sshkeys.
# vim /usr/local/sbin/copy_sshkeys
# chmod 700 /usr/local/sbin/copy_sshkeys
The contents of the script are as follows. Set the return value so that systemd can determine the error.
#!/bin/bash
HN=`hostname`
RETVAL=0
if [ "${HN}" != "" ]
then
cp -pf /root/host_keys/${HN}/ssh_host* /var/lib/sshd/
RETVAL=$?
else
#Failed to get the host name
RETVAL=2
fi
exit $RETVAL
Create a new Unit file under / etc / systemd / system. The name of this file can be anything, as long as you follow the rules for the extension .service, but for clarity, name it ssh_hostkey_copy.service.
# vi /etc/systemd/system/ssh_hostkey_copy.service
(The contents are as follows)
# systemctl enable ssh_hostkey_copy.service
(Output result omitted)
#
The contents of the Unit file are as follows.
[Unit]
Description=SSH Hostkey copy process for diskless clients
After=network.target local-fs.target
#Sshd depending on the distribution.It may be service
Before=ssh.service
[Service]
ExecStart=/usr/local/sbin/copy_sshkeys
Type=oneshot
[Install]
WantedBy=multi-user.target
RequiredBy=ssh.service
Now, even diskless clients can start sshd using the unique host key.