0
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?

お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

Dockerコンテナ間でSSH接続パスワード認証の手順 図有り

Last updated at Posted at 2024-07-06

概要

ssh接続のパスワード認証の接続手順を投稿します。
動きを再現しやすいように、Dockerを使ってます。

開発環境

名称 説明
Windows10 Dockerを動かしているOS
powershell コンソール
Docker 仮想化マシン
vscode IDE
rockylinux8 コンテナのOS

コンテナ情報

コンテナ名 IPアドレス 説明
centos_main 172.30.0.2 hostのコンテナ
centos_sub 172.30.0.4 クライアントのコンテナ

(大まかな流れ)図

スクリプト

Host:前準備

host_prepare.bash
# host 側 centos_hostの準備
# client側がssh接続をする前に実行する。以下コマンドで実行できる。
# docker exec -it centos_main  bash script/11_security/ssh_password/host_prepare.bash >> ./host_prepare.log
# echo "↓↓↓↓↓↓↓↓↓↓"
# echo "↑↑↑↑↑↑↑↑↑↑"

# パッケージのインストール
echo "↓↓↓↓↓パッケージのインストール↓↓↓↓↓"
yum -y install openssh-server
echo "--------1.openssh-serverがインストール済みであることを確認----------"
yum list installed | grep openssh-server
echo "↑↑↑↑↑パッケージのインストール↑↑↑↑↑"

# sshdを起動する
echo "↓↓↓↓↓sshdの起動↓↓↓↓↓"
systemctl start sshd.service
systemctl status sshd.service | grep sshd.service
systemctl status sshd.service | grep Active
echo "↑↑↑↑↑sshdの起動↑↑↑↑↑"

# configファイルの設定
# PasswordAuthentication

sed  's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config |grep PasswordAuthentication yes
# PasswordAuthentication yes であることを確認
cat /etc/ssh/sshd_config | grep PasswordAuthentication
echo "↓↓↓↓↓ユーザの追加↓↓↓↓↓"
# パスワードの暗号化
encrypted_password=$(openssl passwd -crypt test)

# ssh専用ユーザーを作成
useradd  -p "$encrypted_password" testuser

# ユーザーが作成されることを確認
less /etc/passwd | grep testuser
echo "↑↑↑↑↑ユーザの追加↑↑↑↑↑"

Client:前準備

client_prepare.bash
# client 側 centos_subの準備
# docker exec -it centos_sub  bash script/11_security/ssh_password/client_prepare.bash >> ./client_prepare.log

# Packegeのインストール
yum -y install openssh-clients
# openssh-clientsがインストールされていることを確認
yum list installed | grep ssh

Client:接続

manul.bash
# ssh接続
# client_prepare.bash とhost_prepare.bash実行後に以下コマンドを実行
# 手動で実行する

# クライアントのサーバーに入る
docker exec -it centos_sub bash

# ip: 172.30.0.2 ユーザー名testuserでssh接続する。
ssh testuser@172.30.0.2

#finger printやらでYesを選択
yes

# パスワードが要求されるので入力
test

結果

sshコマンド実施からssh接続完了後のコンソール画面
ユーザー名とホスト名が変わりますね

結果.bash
docker exec -it centos_sub bash
-[root@7461e1e28137 /]# ssh testuser@172.30.0.2
The authenticity of host '172.30.0.2 (172.30.0.2)' can't be established.
ECDSA key fingerprint is SHA256:5MvadjCm0W3yDY0eA/dweGnT1lyhB0nQjBMT2UlQhFA.
Are you sure you want to continue connecting (yes/no/[fingerprint])? test # 初回は入力ミスった...
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '172.30.0.2' (ECDSA) to the list of known hosts.
testuser@172.30.0.2's password:  #testと入力
+[testuser@973c5443a04c ~]$ ip

参考にした資料

本気で学ぶlinux実践入門 p476

githubコミット箇所

0
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
0
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?