docker centos7 specific steps for installing ssh
- 2020-05-24 06:30:36
- OfStack
docker centos7 the specific steps to install ssh, recorded here, can also help friends reading the article.
1. Download the centos official image from docker hub
hr:centos7 hr$ docker pull centos:7
After downloading, view the local repository:
hr:centos7 hr$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos 7 ce20c473cd8a 7 weeks ago 172.3 MB
Run the container
hr:centos7 hr$ docker run -i -t centos:7 /bin/bash
2. Install passwd openssl, openssh - server
[root@b5926410fe60 /]# yum install passwd openssl openssh-server -y
Start the sshd:
# /usr/sbin/sshd -D
The Times has the following error:
[root@ b5926410fe60 /]# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
Execute the following command to resolve:
[root@b5926410fe60 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@b5926410fe60 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@b5926410fe60 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
And then, modify /etc/ssh/sshd_config Configuration information:
UsePAM yes Instead of UsePAM no
UsePrivilegeSeparation sandbox Instead of UsePrivilegeSeparation no
[root@b5926410fe60 /]# sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
[root@b5926410fe60 /]# sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
After the modification, restart sshd
[root@b5926410fe60 /]# /usr/sbin/sshd -D
3. Change the root password
[root@b5926410fe60 /]# passwd root
4. View the container ip address (skip this step if the host is linux)
[root@b5926410fe60 /]# ip addr ls eth0
84: eth0@if85: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever
5. Save the current container as a mirror
hr:centos7 hr$ docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5926410fe60 centos:7 "/bin/bash" 4 minutes ago Exited (0) 4 seconds ago centos7ssh
hr:centos7 hr$ docker commit b5926410fe60 herong/centos7-ssh
6. Start the new container on the host based on the newly created image
-- Delete the previous container first
hr:centos7 hr$ docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4122f818a741 herong/centos7-ssh:latest "/usr/sbin/sshd" 13 seconds ago Exited (0) 13 seconds ago happy_mclean
hr:centos7 hr$ docker rm -f 4122f818a741
-- Run the container based on the new image
hr:centos7 hr$ docker run -d -p 10022:22 herong/centos7-ssh:latest /usr/sbin/sshd -D
-- See if the mapped port was successful
hr:centos7 hr$ docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4966d35fe0a3 herong/centos7-ssh:latest "/usr/sbin/sshd -D" 3 seconds ago Up 3 seconds 0.0.0.0:10022->22/tcp compassionate_kowalevski
hr:centos7 hr$ docker port 4966d35fe0a3
22/tcp -> 0.0.0.0:10022
7. Connect from host to container
w If the host is wrong linux Operating system, need to pass docker-machine ip Connected to the container
-- To view docker-machine Ip address
hr:centos7 hr$ docker-machine ip default
192.168.99.100
-- through docker-machine ip Connect to the container and enter the password you set to log in successfully
hr:centos7 hr$ ssh root@192.168.99.100 -p 10022
The authenticity of host '[192.168.99.100]:10022 ([192.168.99.100]:10022)' can't be established.
ECDSA key fingerprint is SHA256:d3JNckcTVv1ASJlwv+IT/bJwlzMC4U1T/PmsKYIHMhQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.99.100]:10022' (ECDSA) to the list of known hosts.
root@192.168.99.100's password:
[root@4966d35fe0a3 ~]# pwd
/root
w If the host is linux Operating system, through the first 4 Step check ip Address the connection
hr:centos7 hr$ ssh root@172.17.0.2 -p 10022
Thank you for reading, I hope to help you, thank you for your support of this site!