Docker Centos安装Openssh

环境介绍:

Docker版本:1.5.0

镜像:docker.io:centos latest

操作步骤:

1、启动镜像

docker run -ti centos /bin/bash

2、安装passwd

yum install passwd -y

3、修改Centos root密码

# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than characters
Retype new password:
passwd: all authentication tokens updated successfully.

4、安装openssh

yum install openssh-server -y

5、生成公钥、私钥

[root@378ab88a06c8 /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): (直接回车)
Enter same passphrase again: (直接回车)
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
:3c:::e4::7d::cc::ac:::ab::9e root@378ab88a06c8
The key's randomart image is:
+--[ RSA ]----+
| .o +=+|
| o . . o =o|
| * . o = |
| + o = |
| S o |
| + o . |
| . + |
| E |
| |
+-----------------+
[root@378ab88a06c8 /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): (直接回车)
Enter same passphrase again: (直接回车)
Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key.
Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub.
The key fingerprint is:
:ac:b0:::de:e8:4f:5e::d9:fc:1e:b6:d7: root@378ab88a06c8
The key's randomart image is:
+--[ RSA ]----+
| ... |
| . o * |
| + B = |
| . + o o + |
| . . . S = |
| + + o . . |
| o o . o E|
| . . |
| |
+-----------------+
[root@378ab88a06c8 /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): (直接回车)
Enter same passphrase again: (直接回车)
Your identification has been saved in /etc/ssh/ssh_host_ed25519_key.
Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub.
The key fingerprint is:
:0d:b5:fb::a4::::6d::c0::2e:: root@378ab88a06c8
The key's randomart image is:
+--[ RSA ]----+
| E.ooooo=*|
| o.o..++=|
| . . .o+..|
| o . o . |
| S o . |
| . . . . |
| . |
| |
| |
+-----------------+

6、编写启动脚本

# vi /run.sh

#!/bin/bash
/usr/sbin/sshd -D # chmod +x /run.sh

7、修改SSH默认端口

# vi /etc/ssh/sshd_config 

Port 

8、退出容器,保存镜像

[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
378ab88a06c8 centos:latest "/bin/bash" minutes ago Exited () seconds ago ecstatic_hawking
[root@localhost ~]# docker commit 378ab88a06c8 sshd:centos
9f400c5422109a3403779f4b055c673fae57d1a8809a84bad7a20fcc3574b3dc

8、启动容器

[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
sshd centos 9f400c542210 minutes ago 271.3 MB
192.168.31.202:/test latest f6c0d93ac266 days ago 315.6 MB
docker.io/centos latest fd44297e2ddb weeks ago 215.7 MB
docker.io/busybox latest 8c2e06607696 weeks ago 2.43 MB
[root@localhost ~]# docker run --net=host -d sshd:centos /run.sh
bbd39bbd7e03ffbd22b88725041793c74c50dbd4316d25bd20c8053265c3f8f1

9、远程连接测试

[root@localhost ~]# ssh 192.168.31.203 -p
The authenticity of host '[192.168.31.203]:222 ([192.168.31.203]:222)' can't be established.
RSA key fingerprint is :2f:::bb::8c::f2:2c:0d::cb:b2::.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.31.203]:222' (RSA) to the list of known hosts.
root@192.168.31.203's password:
Last login: Tue Jun :: from 192.168.7.46
[root@localhost ~]# ll
total
[root@localhost ~]# cd /
[root@localhost /]# ll
total
lrwxrwxrwx root root Apr : bin -> usr/bin
drwxr-xr-x root root Jun : dev
drwxr-xr-x root root Jun : etc
drwxr-xr-x root root Jun home
lrwxrwxrwx root root Apr : lib -> usr/lib
lrwxrwxrwx root root Apr : lib64 -> usr/lib64
drwx------ root root Apr : lost+found
drwxr-xr-x root root Jun media
drwxr-xr-x root root Jun mnt
drwxr-xr-x root root Jun opt
dr-xr-xr-x root root Jun : proc
dr-xr-x--- root root Jun : root
drwxr-xr-x root root Jun : run
-rwxr-xr-x root root Jun : run.sh
lrwxrwxrwx root root Apr : sbin -> usr/sbin
drwxr-xr-x root root Jun srv
dr-xr-xr-x root root May : sys
drwxrwxrwt root root Jun : tmp
drwxr-xr-x root root Apr : usr
drwxr-xr-x root root Apr : var
[root@localhost /]#
上一篇:Docker - CentOS安装Docker


下一篇:T-SQL备忘(1):表联接