Password-free SSH link between host and Docker container, ssh password-free connection between container and container

Source: Internet
Author: User
Tags commit stdin ssh centos free ssh docker cp docker run

(1) Pull a new CentOS image for Docker pulls centos:7.2

(2) Running a container, ready to install the necessary environment

Docker run--privileged--dns controler--name slurm_control-i-t-v/container_data/:/data C Entos:centos7/bin/bash

(3) Install the SSH environment inside the container

Yum Install wget vim

wget-0/etc/yum.repos.d/centos-base.repo Http://

Yum Cleal All

Yum Install passwd

Yum Install Openssh-server

Modify container Password (advance yum-y reinstall Cracklib-dicts)

echo "123456" |passwd--stdin Root

Generate a Public private key:


CD ~/.ssh/

CP Authorized_keys

Prepare before opening (the following instructions go straight to the carriage)

Ssh-keygen-t rsa-f/etc/ssh/ssh_host_rsa_key

Ssh-keygen-t dsa-f/etc/ssh/ssh_host_dsa_key

Ssh-keygen-t rsa-f/etc/ssh/ssh_host_ecdsa_key

Ssh-keygen-t rsa-f/etc/ssh/ssh_host_ed25519_key


Exit the container and make it into a new container


Docker Stop 2ba4a64c7f80

Docker Commit 2BA4A64C7F80 Docker_ssh

Docker run--privileged-i-T Docker_ssh/sbin/init (stuck in the current interface, re-open a new terminal, enter the container inside the sshd service)

Docker exec-it Af40bd07fa0f/bin/bash

Systemctl Restart Sshd.service

echo "123456" |passwd--stdin Root

Netstat-nplt|grep 22//View status

Yum Install-y openssh-clients

(6) Exit the container and copy the ~/.ssh/ of the host to the ~/.ssh/authorized_keys in the container for password-free login

Execute on Host

Docker CP ~/.ssh/ aea267757cc9:/root/.ssh/

Authorization code to append to the container after entering the container

Docker Attach Aea267757cc9

Cat >>/root/.ssh/authorized_keys

Production complete, backup as new mirror

Docker commit AEA267757CC9 Ssh_docker1

Docker run-d-P 221:22--name ssh_container2-h slaver1-v/container_data/:/data f57ef7db72aa/usr/sbin/sshd-d

Secret password-free connection test on host

ssh-p221 Root@lcoalhost

The host already has a password-free login to the Docker container

(7) To prevent Docker from starting IP changes each time, use a custom network after each skin freeze adds a new container to the host column in

Docker Network Create--SUBNET= Shadownet

Installing two nodes for a Slurm cluster one is a compute node one is controlled, one is used to compute nodes

Open three containers in the same way, Controler,slaver1,slaver2,

Docker run-d-P 220:22--name ssh_container1-h controler--net shadownet--ip "slaver1:172.18.0. "-v/container_data/:/data 0bedf27156e7/usr/sbin/sshd-d

Docker run-d-P 221:22--name ssh_container2-h slaver1--net shadownet--ip "controler: 0 "-v/container_data/:/data 0bedf27156e7/usr/sbin/sshd-d

The test between the container and the container starts here.

Then go into a container, use: SSH (controler,slaver1,slaver2 any one,) and then link the other two Docker containers

Okay, so far, all two links: Host <-----> containers, containers <------> Password-free login between containers completed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.