当サイトは、アフィリエイト広告を利用しています
サーバーにssh接続するための認証の設定で少し詰まったので
パスワード認証と公開鍵認証のそれぞれの設定の仕方を
忘備録としての残しておく。
sshの認証には
の二つがあり、設定を/etc/ssh/sshd_configで記述する
必要がある。
パスワード認証の設定はPasswordAuthenticationで行う。
#PubkeyAuthentication yes# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2# but this is overridden so installations will only check .ssh/authorized_keys#AuthorizedKeysFile .ssh/authorized_keys# To disable tunneled clear text passwords, change to no here!PasswordAuthentication yes#PasswordAuthentication yes
この設定の場合、パスワード認証のみが許可される PasswordAuthenticationが二つあるが1つだけしか有効化しない。
公開鍵認証を使用するためには下記の二つの設定をする
PubkeyAuthentication yes# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2# but this is overridden so installations will only check .ssh/authorized_keysAuthorizedKeysFile .ssh/authorized_keys
コメントアウトされてるので解除する
ちょっと言い方が難しいがこれで公開鍵認証を使用できるようになったが
まだ許可はされていないので使うことはできない。
次に少し下を探すと下記の二つが見つかる。
二つとも有効化し、パスワード認証許可の方をnoに変更する
# To disable tunneled clear text passwords, change to no here!PasswordAuthentication noPasswordAuthentication yes
二つとも有効化している場合は、上記のように
になるが、1つしか有効化されていない場合は、
となるので注意!!
パターンとしては下記になる
パターンごとの設定をまとめておく
公開鍵認証の使用設定部分はコメントアウトし
PasswordAuthentication yesを一つだけ活性化
#PubkeyAuthentication yes# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2# but this is overridden so installations will only check .ssh/authorized_keys#AuthorizedKeysFile .ssh/authorized_keys# To disable tunneled clear text passwords, change to no here!PasswordAuthentication yes#PasswordAuthentication yes
この設定の場合、パスワード認証のみが許可される
公開鍵認証の使用設定部分は有効化し
PubkeyAuthentication yes# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2# but this is overridden so installations will only check .ssh/authorized_keysAuthorizedKeysFile .ssh/authorized_keys# To disable tunneled clear text passwords, change to no here!PasswordAuthentication noPasswordAuthentication yes
この設定の場合、公開鍵認証のみが許可される
公開鍵認証の使用設定部分は有効化し
上のPasswordAuthenticationをyesにする
下のPasswordAuthenticationをyesにする。
PubkeyAuthentication yes# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2# but this is overridden so installations will only check .ssh/authorized_keysAuthorizedKeysFile .ssh/authorized_keys# To disable tunneled clear text passwords, change to no here!PasswordAuthentication yesPasswordAuthentication yes
この設定の場合、パスワード認証OR公開鍵認証が許可される
rockylinuxのDockerコンテナを作って
を検証してみる
フォルダ構成は下記のような形にする
.|-- docker| `-- Dockerfile`-- docker-compose.yml
rockylinuxでDockerコンテナを作成する。
コンテナを二つ
作ってauthtest1からauthtest2に対してsshする
rockylinuxのイメージを使ってコンテナを作成する
FROM rockylinux/rockylinux# systemctlを使える様にするRUN dnf -y update \&& dnf install -y openssh-server \openssh-clients \&& dnf clean all# ポートを20022にする# パスワード設定RUN sed -ri 's/^# Port 22/ Port 20022/' /etc/ssh/sshd_config \&& echo 'root:password' | chpasswd# ポートEXPOSE 20022
systemctlだけ使えるようにしておく
docker-composeでコンテナ二つを作る
version: "3"# コンテナservices:# サーバー1authtest1:container_name: authtest1# image: rockylinux/rockylinuxbuild:context: ./docker #Dockerfileへのpathdockerfile: Dockerfilerestart: alwaystty: trueprivileged: truecommand: /sbin/initnetworks:auth_network:ipv4_address: 172.25.1.2# サーバー2authtest2:container_name: authtest2# image: rockylinux/rockylinuxbuild:context: ./docker #Dockerfileへのpathdockerfile: Dockerfilerestart: alwaystty: trueprivileged: truecommand: /sbin/initports:- 20022:20022networks:auth_network:ipv4_address: 172.25.1.3# ネットワークnetworks:auth_network:ipam:driver: defaultconfig:- subnet: 172.25.1.0/24
authtest1とauthtest2のコンテナを作る
二つのコンテナを起動させる
対象プロジェクトに移動して下記コマンドを実行する
docker compose up -d
起動できたことを確認する
$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES57b65f1cc616 authtest-authtest2 "/sbin/init" 7 minutes ago Up 7 minutes 0.0.0.0:20022->20022/tcp authtest22a05f8a4e05d authtest-authtest1 "/sbin/init" 7 minutes ago Up 7 minutes 20022/tcp authtest1
二つのコンテナが起動できている
authtest2に対してDockerからログインする
$ docker exec -it authtest2 bash[root@57b65f1cc616 /]#
ログインできたらsshd_configを確認する。
下記の状態になってることを確認する
公開鍵認証の使用設定部分はコメントアウトし
PasswordAuthentication yesが一つだけ活性化されている状態にする
#PubkeyAuthentication yes# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2# but this is overridden so installations will only check .ssh/authorized_keys#AuthorizedKeysFile .ssh/authorized_keys# To disable tunneled clear text passwords, change to no here!PasswordAuthentication yes#PasswordAuthentication yes
パスワード認証が有効になってるので、
authtest1からauthtest2にsshしてみる
$ docker exec -it authtest1 bash[root@2a05f8a4e05d /]# ssh root@172.25.1.3root@172.25.1.3's password:Last login: Sat Sep 2 10:20:40 2023 from 172.25.1.2[root@57b65f1cc616 ~]#
パスワード認証でauthtest1からauthtest2にsshでログインできる。
パスワード認証から公開鍵認証に切り替えてみる
ssh-keygenで鍵ペアを作成する
[root@2a05f8a4e05d ~]# ssh-keygen
秘密鍵と公開鍵ができる
[root@2a05f8a4e05d .ssh]# lsid_rsa id_rsa.pub
~pubの方が公開鍵
authtest1から公開鍵を送るためのフォルダを
authtest2に作成する
[root@2a05f8a4e05d ~]# ssh root@172.25.1.3root@172.25.1.3's password:Last login: Sat Sep 2 10:45:01 2023 from 172.25.1.2[root@57b65f1cc616 ~]# mkdir .ssh
authtest1で実行する
[root@2a05f8a4e05d .ssh]# scp ./id_rsa.pub root@172.25.1.3:~/.ssh/root@172.25.1.3's password:id_rsa.pub 100% 571 1.1MB/s 00:00
[root@57b65f1cc616 .ssh]# chmod 700 ~/.ssh[root@57b65f1cc616 .ssh]# cat ~/.ssh/id_rsa.pub >> authorized_keys[root@57b65f1cc616 .ssh]# lsauthorized_keys id_rsa.pub[root@57b65f1cc616 .ssh]# chmod 600 ~/.ssh/authorized_keys[root@57b65f1cc616 .ssh]# rm ~/.ssh/id_rsa.pubrm: remove regular file '/root/.ssh/id_rsa.pub'? yes[root@57b65f1cc616 .ssh]# lsauthorized_keys[root@57b65f1cc616 .ssh]#
公開鍵認証の使用設定部分は有効化し
PubkeyAuthentication yes# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2# but this is overridden so installations will only check .ssh/authorized_keysAuthorizedKeysFile .ssh/authorized_keys# To disable tunneled clear text passwords, change to no here!PasswordAuthentication noPasswordAuthentication yes
この設定の場合、公開鍵認証のみが許可される
[root@57b65f1cc616 ssh]# systemctl restart sshd
[root@2a05f8a4e05d .ssh]# ssh root@172.25.1.3Last login: Sat Sep 2 11:00:26 2023 from 172.25.1.2