Gue*_*OCs 4 server ssh boot raspberrypi
我需要在第一次启动之前以我知道其ssh指纹的方式配置树莓派,因此唯一的方法是在我的计算机上的SD卡中生成密钥并获取指纹。我这样做了,但是当树莓启动时,它会在我生成的密钥之上创建新密钥。如何防止 ssh 守护进程这样做?如果它是第一次启动,它肯定会在某个地方读取,这必须是一种改变它的方法。
编辑:
我的脚本将生成的 ssh 文件放在 /etc/ssh.conf 中。在第一次启动时,这是 sh 服务的 /var/log/daemon.log 的日志:
Sep 7 16:12:31 raspberrypi sh[297]: removed '/etc/ssh/ssh_host_dsa_key'
Sep 7 16:12:31 raspberrypi sh[297]: removed '/etc/ssh/ssh_host_dsa_key.pub'
Sep 7 16:12:31 raspberrypi sh[297]: removed '/etc/ssh/ssh_host_ecdsa_key'
Sep 7 16:12:31 raspberrypi sh[297]: removed '/etc/ssh/ssh_host_ecdsa_key.pub'
Sep 7 16:12:31 raspberrypi sh[297]: removed '/etc/ssh/ssh_host_rsa_key'
Sep 7 16:12:31 raspberrypi sh[297]: removed '/etc/ssh/ssh_host_rsa_key.pub'
Run Code Online (Sandbox Code Playgroud)
它以某种方式知道这些密钥不是由 SSH 服务生成的。请注意,我没有生成 ssh_host_ed25519_key。这可能是原因吗?但我注释掉了它的行sshd_config:
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
Run Code Online (Sandbox Code Playgroud)
这可能是它检测到并删除密钥的原因吗?
编辑2:
我从之前启动的 SD 卡中添加了 ed25519 密钥,但它仍然删除了所有密钥:
Sep 7 16:12:32 raspberrypi sh[311]: removed '/etc/ssh/ssh_host_dsa_key'
Sep 7 16:12:33 raspberrypi sh[311]: removed '/etc/ssh/ssh_host_dsa_key.pub'
Sep 7 16:12:33 raspberrypi sh[311]: removed '/etc/ssh/ssh_host_ecdsa_key'
Sep 7 16:12:33 raspberrypi sh[311]: removed '/etc/ssh/ssh_host_ecdsa_key.pub'
Sep 7 16:12:33 raspberrypi sh[311]: removed '/etc/ssh/ssh_host_ed25519_key'
Sep 7 16:12:33 raspberrypi sh[311]: removed '/etc/ssh/ssh_host_ed25519_key.pub'
Sep 7 16:12:33 raspberrypi sh[311]: removed '/etc/ssh/ssh_host_rsa_key'
Sep 7 16:12:33 raspberrypi sh[311]: removed '/etc/ssh/ssh_host_rsa_key.pub'
Run Code Online (Sandbox Code Playgroud)
我还看到我忘记为每个文件添加适当的权限,但我在最后一次尝试中做了但仍然没有。我600为私钥和644公钥做了,就像sshd它生成新的一样
编辑 3:
我试图在github源上搜索“removed”,看看是否能找到触发密钥删除但找不到的代码段:https : //github.com/openssh/openssh-portable/search? utf8=%E2%9C%93&q=已删除&type=
小智 7
Raspbian 在镜像中有一个名为 regenerate_ssh_host_keys 的服务。
[Unit]
Description=Regenerate SSH host keys
Before=ssh.service
[Service]
Type=oneshot
ExecStartPre=-/bin/dd if=/dev/hwrng of=/dev/urandom count=1 bs=4096
ExecStartPre=-/bin/sh -c "/bin/rm -f -v /etc/ssh/ssh_host_*_key*"
ExecStart=/usr/bin/ssh-keygen -A -v
ExecStartPost=/bin/systemctl disable regenerate_ssh_host_keys
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
这将删除任何已经存在的密钥,然后重新生成密钥,然后禁用该服务,使其不再运行。您可以/etc/systemd/system/multi-user.target.wants/regenerate_ssh_host_keys.service在安装密钥的同时禁用该服务(通过删除文件)。