简体   繁体   中英

docker image - centos 7 > ssh service not found

I installed docker image - centos 7 on my ubuntu machine. But ssh service not found. so I cant run this service.

[root@990e92224a82 /]# yum install openssh-server openssh-clients
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirror.dhakacom.com
 * extras: mirror.dhakacom.com
 * updates: mirror.dhakacom.com
Package openssh-server-6.6.1p1-31.el7.x86_64 already installed and latest version

Package openssh-clients-6.6.1p1-31.el7.x86_64 already installed and latest version
Nothing to do
[root@990e92224a82 /]# ss            
ssh          ssh-agent    ssh-keygen   sshd         ssltap       
ssh-add      ssh-copy-id  ssh-keyscan  sshd-keygen  

How can I remotely login docker image?

You have to do the following instructions on Dockerfile.

RUN yum install -y sudo wget telnet openssh-server vim git ncurses-term
RUN useradd your_account

RUN mkdir -p /home/your_account/.ssh && chown -R your_account   /home/your_account/.ssh/ 

# Create known_hosts
RUN touch /home/your_account/.ssh/known_hosts


COPY files/authorized_keys /home/your_account/.ssh/
COPY files/config          /home/your_account/.ssh/
COPY files/pam.d/sshd      /etc/pam.d/sshd
RUN touch /home/your_account/.ssh/environment
RUN chown -R your_account /home/your_account/.ssh
RUN chmod 400 -R  /home/your_account/.ssh/*
RUN chmod 700 -R  /home/your_account/.ssh/known_hosts
RUN chmod 700  /home/your_account/.ssh/environment


# Enable sshd
COPY files/sshd_config /etc/ssh/
RUN ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa 

# Add a account into sudoers and this account doesn't need to type his password
COPY files/sudoers /etc/
COPY files/start.sh /root/

I have to remove "pam_nologin.so" on the file /etc/pam.d/sshd, because when I upgrade the openssh-server's version to openssh-server-6.6.1p1-31.el7, the pam_nologin.so will disallow remote login for any users even the file /etc/nologin is not exist.

start.sh

#!/bin/bash
/usr/sbin/sshd -E /tmp/sshd.log

Start centos container

  • docker run -d -t -p $(sshPort):22 --name $(containerName) $(imageName) /bin/bash
  • docker exec -d $(containerName) bash -c "sh /root/start.sh"

Login container

  • ssh $(Docker ip) $(sshPort)

In extend to @puritys

You could do this in the Dockerfile instead

Last in the file:

 ENTRYPOINT /usr/sbin/sshd -E /tmp/sshd.log && /bin/bash

Then you will only need to run:

 docker run -d -p -t $(sshPort):22 --name $(containerName) $(imageName) /bin/bash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM