LoginSignup
8
11

More than 3 years have passed since last update.

リモートサーバーのコンテナにsshする

Last updated at Posted at 2019-10-16

1. 概要

リモートサーバーの特定のポートにコンテナ内のsshポートをポートフォワードすることで出来る.

2. 環境

2.1. リモートサーバー

  • ubuntu 18.04

3. 手順

3.1. sshする先のコンテナをpullする

$ docker pull kazetof/python_cuda_pipenv

立ち上げるコンテナのimageは適宜変更する.これはpythonコードの開発用私的image.

$ docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
kazetof/python_cuda_pipenv   latest              28c032e01eef        2 months ago        2.25GB

3.2. containerをポートフォワードして立ち上げる.

$ docker run -it -d -p 49153:22 --name ssh_container 28c032e01eef 

49153:22host側port:container側portを接続してる.

49153は空いているポートから適当に選んだ.

3.3. ssh serverをコンテナ内に入れる

本当はコンテナのimage内に最初からssh serverを入れて置くのが良いが,手動では以下のようにして行う.

root@37c638a9462b:/# apt update
root@37c638a9462b:/# apt install openssh-server -y

ssh-serverが入ったか確認する.

root@37c638a9462b:/# dpkg -l | grep ssh
ii  openssh-client              1:7.6p1-4ubuntu0.3                amd64        secure shell (SSH) client, for secure access to remote machines
ii  openssh-server              1:7.6p1-4ubuntu0.3                amd64        secure shell (SSH) server, for secure access from remote machines
ii  openssh-sftp-server         1:7.6p1-4ubuntu0.3                amd64        secure shell (SSH) sftp server module, for SFTP access from remote machines
ii  ssh-import-id               5.7-0ubuntu1.1                    all          securely retrieve an SSH public key and install it locally

openssh-serverが入ってる.

3.4. ssh serverの設定を変更する.

設定ファイルは以下のファイルで,

/etc/ssh/sshd_config
PasswordAuthentication yes

のコメントアウトを外してパスワードログインを許可した.(これも簡易的で,本当はssh public keyを使用した方が良い.)

ssh serverをrestartする.

root@37c638a9462b:/etc/ssh# /etc/init.d/ssh restart

3.5. ssh接続するユーザーを作成する.

rootではなく,sshするuserを切ってそれでsshする.
ユーザーの作成は以下のようにして行う.

ubuntu ユーザを追加して sudo 権限をつける - Qiita

3.6. portを指定してssh接続する.

以下のように-pオプションで接続するポートを指定する.

ssh {user}@{server ip} -p 49153
8
11
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
8
11