Search code examples
linuxdockersshcontainers

SSH connection asks for password connecting to Docker container


I've already looked for similar questions and tried changing several things in my configuration but couldn't come up with a solution.

I'm trying to connecting via SSH to a Docker container, here's the Dockerfile:

FROM ubuntu

RUN apt-get update && \
 apt-get install -y openssh-server

RUN useradd remote_user && \
    echo "remote_user:test1234" | chpasswd && \
    mkdir /home/remote_user/.ssh -p && \
    chmod 700 /home/remote_user/.ssh && \
    mkdir -p -m0755 /var/run/sshd


COPY id_rsa.pub /home/remote_user/.ssh/authorized_keys

RUN chown remote_user:remote_user   -R /home/remote_user && \
    chmod 600 /home/remote_user/.ssh/authorized_keys

RUN apt-get install -y php php-mbstring php-xml php-bcmath php-fpm && \
    apt-get install -y composer && apt-get install -y vim
    
RUN apt-get install -y nginx

CMD /usr/sbin/sshd -D

Once I try to connect to the container as "remote_user" with ssh -Tv [email protected] (where "staging.local" is the container IP) I get this message:

...
...
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected]>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /home/xxx/.ssh/id_rsa RSA SHA256:5QNPe89pdQp+tgE61N9YPaIJEs8QR9DxaChmStfvzBU agent
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: xxx@xxx RSA SHA256:C+VWlGUd4mVywHnh8JWtjL0gmO8cuqUEs4YYCbQGvaE agent
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/xxx/.ssh/id_dsa
debug1: Trying private key: /home/xxx/.ssh/id_ecdsa
debug1: Trying private key: /home/xxx/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/xxx/.ssh/id_ed25519
debug1: Trying private key: /home/xxx/.ssh/id_ed25519_sk
debug1: Trying private key: /home/xxx/.ssh/id_xmss
debug1: Next authentication method: password
[email protected]'s password:

As you can see it can't connect and is asking for the password.

If I ls -ll .ssh folder files in my host machine I have this:

-rw------- 1 xxx xxx 2610 Jan  3 12:08 id_rsa
-rw-r--r-- 1 xxx xxx  577 Jan  3 12:08 id_rsa.pub
-rw-r--r-- 1 xxx xxx  222 Jan  3 12:25 known_hosts

If I docker exec into the container as root user and see permissions of /home/remote_user/.ssh I have:

  • home folder permissions: drwxr-xr-x 1 root root 4096 Jan 3 11:22 home

  • remote_user folder permissions: drwxr-xr-x 1 remote_user remote_user 4096 Jan 3 11:22 remote_user

  • .ssh folder permissions: drwx------ 1 remote_user remote_user 4096 Jan 3 11:22 .ssh

  • authorized_keys file permissions: -rw------- 1 remote_user remote_user 577 Jan 3 11:08 authorized_keys


Solution

  • It should work if you forward the port :

    docker run -p 222:22 your-image
    

    then :

    ssh -p 222 remote_user@localhost