skydum

個人的な作業記録とか備忘録代わりのメモ

dockerのCentOS7でsshdを使えるようにする

dockerでCentOS7 + sshdを起動してホストから接続する

  • 検証用にdockerで起動したCentOS7にホストからsshdで接続したいので作った
  • 検証用なのでコンテナイメージをなるべく小さくするなどの対処は行わない

事前準備

  • CentOS7のdockerイメージそのままではsshdは使えないのでDockerfileを作ってイメージを作成する
  • 任意の場所に以下のDockerfileを作る
  • 作成するコンテナイメージでsshで接続するユーザー
    • ID: root, password: rootとする
    • ID: user, password: userとする
FROM centos:7

RUN yum update -y
RUN yum install -y openssh-server

RUN ssh-keygen -A
RUN echo "root:root" | chpasswd

RUN adduser user
RUN echo "user:user" | chpasswd

CMD ["/usr/sbin/sshd", "-D"]

はてなブログはDockerfileはシンタックスハイライトが効かない

sshdが使える様にしたイメージの作成

  • 以下のコマンドでssh-serverの名称でイメージを作成する
  • 作成するコンテナイメージの名前はcentos7-sshd:latest
$ docker build --tag centos7-sshd .

$ docker images
REPOSITORY     TAG       IMAGE ID       CREATED              SIZE
centos7-sshd   latest    4b6b1ef383b0   33 seconds ago       673MB
<none>         <none>    9d2878d51761   About a minute ago   885MB
centos         7         eeb6ee3f44bd   7 months ago         204MB

sshd-serverのイメージを起動する

  • 起動するサーバのホスト名: server
  • 起動するサーバのコンテナ名: sshd-server
  • 起動するサーバのポート番号: 10022
$ docker run -it --hostname server --name sshd-server -p 10022:22/tcp -d centos7-sshd

$ docker ps
CONTAINER ID   IMAGE          COMMAND               CREATED         STATUS         PORTS                                     NAMES
d94cb10cd66f   centos7-sshd   "/usr/sbin/sshd -D"   8 seconds ago   Up 7 seconds   0.0.0.0:10022->22/tcp, :::10022->22/tcp   sshd-server

起動したsshd-serverにsshでホストから接続する

$ ssh -p 10022 root@localhost
The authenticity of host '[localhost]:10022 ([127.0.0.1]:10022)' can't be established.
ECDSA key fingerprint is SHA256:Z3I2gQ3w7wo1MNJ/8fuTBE1Wnd169RR0qgZ5IyhBHpk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:10022' (ECDSA) to the list of known hosts.
root@localhost's password: 
[root@server ~]#

sshで接続した時にエラーが出た場合

$ ssh -p 10022 root@localhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:/W9DGeoHg56m1FvGj8UWp9SBJ+ZNcRVhDdVZDI52XaA.
Please contact your system administrator.
Add correct host key in /home/username/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/username/.ssh/known_hosts:1
  remove with:
  ssh-keygen -f "/home/username/.ssh/known_hosts" -R "[localhost]:10022"
ECDSA host key for [localhost]:10022 has changed and you have requested strict checking.
Host key verification failed.
  • エラーが出た場合~/.ssh/known_hostsに書かれている接続先のホストの公開鍵と現在の接続先のホストの公開鍵が一致しない為エラーが出ている(中間者攻撃対策のため)
    以下のコマンドを使って古いホストの情報を削除して、もう一度接続する
$ ssh-keygen -R [localhost]:10022
# Host [localhost]:10022 found: line 1
/home/username/.ssh/known_hosts updated.

$ ssh -p 10022 root@localhost
The authenticity of host '[localhost]:10022 ([127.0.0.1]:10022)' can't be established.
ECDSA key fingerprint is SHA256:/W9DGeoHg56m1FvGj8UWp9SBJ+ZNcRVhDdVZDI52XaA.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:10022' (ECDSA) to the list of known hosts.
root@localhost's password: 
[root@server ~]#

sshd-serverを停止してイメージを削除

$ docker ps -a    # 停止しているコンテナも含めて表示する
CONTAINER ID   IMAGE          COMMAND               CREATED          STATUS                      PORTS     NAMES
3ca739888943   centos7-sshd   "/usr/sbin/sshd -D"   21 seconds ago   Exited (0) 12 seconds ago             sshd-server

$ docker stop sshd-server
$ docker rm 3ca739888943    # docker rm sshd-serverでも可

$ docker images
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
centos7-sshd   latest    5c352901945b   4 seconds ago   673MB
centos         7         eeb6ee3f44bd   7 months ago    204MB

$ docker rmi 5c352901945b    # docker rmi centos7-sshd:latestでも可
$ docker rmi eeb6ee3f44bd    # docker rmi centos:7でも可

$ docker ps -a    # 停止しているコンテナも含めて表示する
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE