当サイトは、アフィリエイト広告を利用しています

【Linux】sshの認証方式の設定について

作成日:2023月09月02日
更新日:2023年09月02日

サーバーにssh接続するための認証の設定で少し詰まったので
パスワード認証と公開鍵認証のそれぞれの設定の仕方を
忘備録としての残しておく。

環境

  • rockylinux

sshの認証の設定

sshの認証には

  • パスワード認証
  • 公開鍵認証がある

の二つがあり、設定を/etc/ssh/sshd_configで記述する
必要がある。

パスワード認証の設定

パスワード認証の設定はPasswordAuthenticationで行う。

sshd_config
#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つだけしか有効化しない。

公開鍵認証の設定

公開鍵認証を使用するためには下記の二つの設定をする

sshd_config
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

コメントアウトされてるので解除する
ちょっと言い方が難しいがこれで公開鍵認証を使用できるようになったが
まだ許可はされていないので使うことはできない。

次に少し下を探すと下記の二つが見つかる。

  • 上の方がパスワード認証の許可設定
  • 下の方が公開鍵認証の許可設定

二つとも有効化し、パスワード認証許可の方をnoに変更する

sshd_config
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
PasswordAuthentication yes

二つとも有効化している場合は、上記のように

  • 上の方がパスワード認証の許可設定
  • 下の方が公開鍵認証の許可設定

になるが、1つしか有効化されていない場合は、

  • パスワード認証の許可設定

となるので注意!!

パスワード認証と公開鍵認証の組み合わせパターン

パターンとしては下記になる

  • パスワード認証のみ
  • 公開鍵認証のみ
  • 両方設定(どちらでも認証しても良い)

パターンごとの設定をまとめておく

パスワード認証のみを設定する場合

公開鍵認証の使用設定部分はコメントアウトし
PasswordAuthentication yesを一つだけ活性化

sshd_config
#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をnoにする
  • 下のPasswordAuthenticationをyesにする
sshd_config
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 no
PasswordAuthentication yes

この設定の場合、公開鍵認証のみが許可される

両方設定(どちらでも認証しても良い)を設定する場合

公開鍵認証の使用設定部分は有効化し
上のPasswordAuthenticationをyesにする 下のPasswordAuthenticationをyesにする。

sshd_config
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

この設定の場合、パスワード認証OR公開鍵認証が許可される

Dockerを使って検証する

rockylinuxのDockerコンテナを作って

  • パスワード認証
  • 公開鍵認証

を検証してみる

フォルダ構成は下記のような形にする

フォルダ構成
.
|-- docker
| `-- Dockerfile
`-- docker-compose.yml

コンテナ作成

rockylinuxでDockerコンテナを作成する。
コンテナを二つ

  • authtest1
  • authtest2

作ってauthtest1からauthtest2に対してsshする

Dockerfile

rockylinuxのイメージを使ってコンテナを作成する

Dockerfile
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.yml

docker-composeでコンテナ二つを作る

docker-compose.yml
version: "3"
# コンテナ
services:
# サーバー1
authtest1:
container_name: authtest1
# image: rockylinux/rockylinux
build:
context: ./docker #Dockerfileへのpath
dockerfile: Dockerfile
restart: always
tty: true
privileged: true
command: /sbin/init
networks:
auth_network:
ipv4_address: 172.25.1.2
# サーバー2
authtest2:
container_name: authtest2
# image: rockylinux/rockylinux
build:
context: ./docker #Dockerfileへのpath
dockerfile: Dockerfile
restart: always
tty: true
privileged: true
command: /sbin/init
ports:
- 20022:20022
networks:
auth_network:
ipv4_address: 172.25.1.3
# ネットワーク
networks:
auth_network:
ipam:
driver: default
config:
- subnet: 172.25.1.0/24

authtest1とauthtest2のコンテナを作る

コンテナの確認

二つのコンテナを起動させる
対象プロジェクトに移動して下記コマンドを実行する

GitBash
docker compose up -d

起動できたことを確認する

GitBash
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57b65f1cc616 authtest-authtest2 "/sbin/init" 7 minutes ago Up 7 minutes 0.0.0.0:20022->20022/tcp authtest2
2a05f8a4e05d authtest-authtest1 "/sbin/init" 7 minutes ago Up 7 minutes 20022/tcp authtest1

二つのコンテナが起動できている

パスワード認証を設定する

authtest2に対してDockerからログインする

GitBash
$ docker exec -it authtest2 bash
[root@57b65f1cc616 /]#

ログインできたらsshd_configを確認する。

下記の状態になってることを確認する
公開鍵認証の使用設定部分はコメントアウトし
PasswordAuthentication yesが一つだけ活性化されている状態にする

sshd_config
#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する

パスワード認証が有効になってるので、
authtest1からauthtest2にsshしてみる

GitBash
$ docker exec -it authtest1 bash
[root@2a05f8a4e05d /]# ssh root@172.25.1.3
root@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でログインできる。

公開鍵認証に切り替える

パスワード認証から公開鍵認証に切り替えてみる

authtest1で鍵ペアを生成する

ssh-keygenで鍵ペアを作成する

GitBash
[root@2a05f8a4e05d ~]# ssh-keygen

秘密鍵と公開鍵ができる

GitBash
[root@2a05f8a4e05d .ssh]# ls
id_rsa id_rsa.pub

~pubの方が公開鍵

authtest2に.sshフォルダを作成する

authtest1から公開鍵を送るためのフォルダを
authtest2に作成する

GitBash
[root@2a05f8a4e05d ~]# ssh root@172.25.1.3
root@172.25.1.3's password:
Last login: Sat Sep 2 10:45:01 2023 from 172.25.1.2
[root@57b65f1cc616 ~]# mkdir .ssh

公開鍵をauthtest2へ送る

authtest1で実行する

GitBash
[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

公開鍵の名称を変更する

GitBash
[root@57b65f1cc616 .ssh]# chmod 700 ~/.ssh
[root@57b65f1cc616 .ssh]# cat ~/.ssh/id_rsa.pub >> authorized_keys
[root@57b65f1cc616 .ssh]# ls
authorized_keys id_rsa.pub
[root@57b65f1cc616 .ssh]# chmod 600 ~/.ssh/authorized_keys
[root@57b65f1cc616 .ssh]# rm ~/.ssh/id_rsa.pub
rm: remove regular file '/root/.ssh/id_rsa.pub'? yes
[root@57b65f1cc616 .ssh]# ls
authorized_keys
[root@57b65f1cc616 .ssh]#

sshd_configを編集する

公開鍵認証の使用設定部分は有効化し

  • 上のPasswordAuthenticationをnoにする
  • 下のPasswordAuthenticationをyesにする
sshd_config
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 no
PasswordAuthentication yes

この設定の場合、公開鍵認証のみが許可される

sshdサービスを再起動する

GitBash
[root@57b65f1cc616 ssh]# systemctl restart sshd

パスワード認証なしでログインできる

GitBash
[root@2a05f8a4e05d .ssh]# ssh root@172.25.1.3
Last login: Sat Sep 2 11:00:26 2023 from 172.25.1.2

参考

新着記事

top