Home > Net >  Why Docker doesn't see the hostkeys? sshd: no hostkeys available -- exiting
Why Docker doesn't see the hostkeys? sshd: no hostkeys available -- exiting

Time:10-13

In my Jenkins folder I have

-rw-rw-r--  1 miki miki  411 Oct 12 12:45 docker-compose.yml
-rw-rw-r--  1 miki miki   36 Sep 23 11:08 Dockerfile
drwxrwxr-x  8 miki miki 4096 Sep 23 13:24 .git/
drwxr-xr-x 18 root root 4096 Oct 12 12:46 jenkins_home/
drwxrwxr-x  8 miki miki 4096 Sep 23 13:23 react-app/

docker-compose output is

services:

  jenkins:
    build: .
    container_name: jenkins
    privileged: true
    restart: always
    ports:
      - 8080:8080
    volumes:
      - ./jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker
  
  remote_host:
    container_name: remote_host
    image: remote_host
    build: 
      context: ../udemyjenkins/  

When I go for standard

docker-compose up -d

I got

Creating network "evhenybristov_default" with the default driver
Creating jenkins     ... done
Creating remote_host ... done

But remote_host container is exited.

docker inspect remote_host
[
    {
        "Id": "2ab29c5ecf2b67e085e714b21bbfd092024e0d20d5c854c737f459002f7847ca",
        "Created": "2022-10-12T10:45:54.594448184Z",
        "Path": "/usr/sbin/sshd",
        "Args": [
            "-D"
        ],
        "State": {
            "Status": "exited",
            "Running": false,

My goal is to ssh from jenkins to remote_host. Other Dockerfile(udemyjenkins)

FROM ubuntu:latest
RUN apt update
RUN apt install -y openssh-server sudo vim-tiny

RUN useradd -ms /bin/bash -g root -G sudo -u 1000 remote_user
RUN  echo 'remote_user:ae******' | chpasswd -c SHA256

RUN service ssh start
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
RUN bash -c 'install -m755 <(printf "#!/bin/sh\nexit 0") /usr/sbin/policy-rc.d'
RUN ex  '%s/^#\zeListenAddress/\1/g' -scwq /etc/ssh/sshd_config
RUN ex  '%s/^#\zeHostKey .*ssh_host_.*_key/\1/g' -scwq /etc/ssh/sshd_config
RUN RUNLEVEL=1 dpkg-reconfigure openssh-server
RUN ssh-keygen -A -v
RUN update-rc.d ssh defaults

RUN ex  "%s/^%sudo.*$/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/g" -scwq! /etc/sudoers

USER remote_user

RUN ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519

EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

I deleted image and run again. Now I have a new image, but the problem persists.

docker logs remote_host
Unable to load host key: /etc/ssh/ssh_host_rsa_key
Unable to load host key: /etc/ssh/ssh_host_ecdsa_key
Unable to load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.

What is wrong?

CodePudding user response:

You are running the sshd command as the user remote_user so it is unable to access the hostkeys, which are only available to root:

$ ls -l /etc/ssh/ssh_host_*key
-rw------- 1 root root 1381 Oct 12 12:06 /etc/ssh/ssh_host_dsa_key
-rw------- 1 root root  513 Oct 12 12:03 /etc/ssh/ssh_host_ecdsa_key
-rw------- 1 root root  411 Oct 12 12:03 /etc/ssh/ssh_host_ed25519_key
-rw------- 1 root root 2602 Oct 12 12:03 /etc/ssh/ssh_host_rsa_key

This looks like a simple error in your Dockerfile; you need to reset the USER value after creating the ssh key for remote_user:

USER remote_user
RUN ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519

# Reset user to root
USER root

EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
  • Related