Search code examples
dockerssh

Permission denied (incorrect password) when attempting to SSH to docker container


I am trying to create a docker container to ssh into for unit testing on Ubuntu 20.04. Using the DockerFile below:

# Use a base image with the desired OS (e.g., Ubuntu, Debian, etc.)
FROM ubuntu:latest

# Install SSH server
RUN apt-get update && \
    apt-get upgrade -y
RUN apt-get install openssh-server -y supervisor
RUN apt-get install nano

# Create an SSH user
RUN useradd -rm -d /home/sshuser -s /bin/bash -g root -G sudo -u 1000 sshuser

# Set the SSH user's password (replace "password" with your desired password)
RUN echo 'sshuser:password'

# Allow SSH access
RUN mkdir /var/run/sshd

RUN /usr/bin/ssh-keygen -A

# Expose the SSH port
EXPOSE 22

# Start SSH server on container startup
CMD ["/usr/sbin/sshd", "-D"]

and

setting up with docker build -t ssh_server .

docker run -d -p 22:22 ssh_server

when running ssh sshuser@localhost I am prompted to enter the password. But whenever I enter the correct password, I get Permission denied, please try again. error. Interestingly, this is working on Windows without issue.

Full log:

(base) joe@Ubuntu:~/git-repos/datashuttle/tests/ssh_test_images$ docker build -t ssh_server .
[+] Building 1.4s (12/12) FINISHED                               docker:default
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 651B                                       0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest           1.2s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [1/8] FROM docker.io/library/ubuntu:latest@sha256:1b8d8ff4777f36f19bf  0.0s
 => CACHED [2/8] RUN apt-get update &&     apt-get upgrade -y              0.0s
 => CACHED [3/8] RUN apt-get install openssh-server -y supervisor          0.0s
 => CACHED [4/8] RUN apt-get install nano                                  0.0s
 => CACHED [5/8] RUN useradd -rm -d /home/sshuser -s /bin/bash -g root -G  0.0s
 => CACHED [6/8] RUN echo 'sshuser:password'                               0.0s
 => CACHED [7/8] RUN mkdir /var/run/sshd                                   0.0s
 => CACHED [8/8] RUN /usr/bin/ssh-keygen -A                                0.0s
 => exporting to image                                                     0.0s
 => => exporting layers                                                    0.0s
 => => writing image sha256:cc8b8b9f8a500fb399ae7ef2ea7ef0a667706dde81330  0.0s
 => => naming to docker.io/library/ssh_server                              0.0s
(base) joe@Ubuntu:~/git-repos/datashuttle/tests/ssh_test_images$ docker run -d -p 22:22 ssh_server
cf70b859e7c1889d32f0e866697c6d273fb6bde27bb5d9f80de0ae546f5cc6dd
(base) joe@Ubuntu:~/git-repos/datashuttle/tests/ssh_test_images$ ssh sshuser@localhost
sshuser@localhost's password: 
Permission denied, please try again.

Any help would be much appreciated as have been banging my head against a wall with this for a while.


Solution

  • First things first: I have no idea why it worked on Windows for you, I'd assume that given this Dockerfile it should not work at all :D

    This line:

    RUN echo 'sshuser:password'
    

    doesn't do anything other than displaying text "sshuser:password".

    You need to set the password for it to work correctly. You can do it for example by adding | chpasswd to the mentioned line, so that it looks like this:

    RUN echo 'sshuser:password' | chpasswd