概要
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コミット箇所