1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

踏み台サーバー経由でのSSHポートフォワーディング

Posted at

踏み台サーバー経由、かつ、送信元PC上で設定なしに目標のコンテナにSSH接続させたい時の検証メモ。

Dockerfile
# CentOS7を使用
FROM centos:centos7

# OpenSSH サーバ/クライアントをインストールする
RUN yum -y install openssh-server openssh-client

# iprouteをインストールする
RUN yum -y install iproute

# sshでログインできるようにする 
RUN sed -ri 's/^#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config

# root のパスワードを 設定
RUN echo 'root:rootpass' | chpasswd

# 使わないにしてもここに公開鍵を登録しておかないとログインできない 
RUN ssh-keygen -t rsa -N "" -f /etc/ssh/ssh_host_rsa_key

# sshd の使うポートを公開する
EXPOSE 22

# sshd を起動する
CMD ["/usr/sbin/sshd", "-D"]
# イメージ作成
$ docker build -t ssh-test .

# 接続したいコンテナ作成(ip addrコマンドで 172.17.0.2 を確認)
$ docker run -d --hostname target-container --name target-container ssh-test

# 踏み台コンテナ作成(PCのローカルポート6666から踏み台コンテナの7777にアクセスする形にする)
$ docker run -d -p 6666:7777 --hostname bastion --name bastion ssh-test

# 踏み台コンテナに入ってポートフォワードコマンドを実行(ip addrコマンドで 172.17.0.4 を確認)
$ docker exec -it bastion2 /bin/bash

# 踏み台コンテナで実行
$ ssh -L 7777:172.17.0.2:22 root@localhost -fNg

# PCから疎通確認
$ ssh -p 6666 root@localhost
<< OUTPUT
root@localhost's password: 
Last login: Fri Sep 17 04:59:26 2021 from 172.17.0.4
[root@target-container ~]#
OUTPUT
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?