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 8.8.8.8--dns 8.8.4.4-h 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://mirrors.aliyun.com/repo/Centos-7.repo

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:

SSH-KEYGEN-T RSA

CD ~/.ssh/

CP Id_rsa.pub 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

/usr/sbin/sshd

Exit the container and make it into a new container

Ctrl+d

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/id_rsa.pub of the host to the ~/.ssh/authorized_keys in the container for password-free login

Execute on Host

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


Authorization code to append to the container after entering the container

Docker Attach Aea267757cc9

Cat Id_dsa.pub >>/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=172.18.0.0/16 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 172.18.0.10--add-host= "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 172.18.0.11--add-host= "controler:172.18.0.1 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: info-contact@alibabacloud.com 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.