LoginSignup
0
0

More than 1 year has passed since last update.

【Linux】公開鍵認証の設定

Last updated at Posted at 2022-07-07

CentOSにリモートからログインする際に、パスワードを使用しておりました。
パスワードから鍵認証への変更を行いたいとの要望を頂いて、手順についてを自分なりにまとめたのでこちらに記載します。

まず、鍵の作成はクライアント側で実施する必要があります。
ssh-keygenコマンドで鍵の作成を行う事が出来ます。今回は純粋な検証なので、暗号化方式等のオプションは指定しておりません。また、パスフレーズも設定しておりません。

man ssh-keygenコマンドの一部の結果だけ載せておきますね。下記オプションはちょくちょく使います。

-t dsa | ecdsa | ed25519 | rsa | rsa1
    Specifies the type of key to create.  The possible values are
    “rsa1” for protocol version 1 and “dsa”, “ecdsa”, “ed25519”, or
    “rsa” for protocol version 2.
  
-b bits
    Specifies the number of bits in the key to create.  For RSA keys,
    the minimum size is 1024 bits and the default is 2048 bits.  Gen‐
    erally, 2048 bits is considered sufficient.  DSA keys must be
    exactly 1024 bits as specified by FIPS 186-2.  For ECDSA keys,
    the -b flag determines the key length by selecting from one of
    three elliptic curve sizes: 256, 384 or 521 bits.  Attempting to
    use bit lengths other than these three values for ECDSA keys will
    fail.  Ed25519 keys have a fixed length and the -b flag will be
    ignored.

ssh-keygenコマンド実施後に、ホームディレクトリ内に.sshフォルダが作成されます。
ll -aコマンドで作成されている事を確認します。また、.sshフォルダ内にid_rsa(秘密鍵)とid_rsa.pub(公開鍵)が作成されている事も確認します。

コマンド
[test_test@localhost ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test_test/.ssh/id_rsa):
Created directory '/home/test_test/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/test_test/.ssh/id_rsa.
Your public key has been saved in /home/test_test/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:l2IFyxNaNfOba8YIrxXmpciJCmH4GDLGZc+WspSmPNg test_test@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|        +.+      |
|       + + +     |
|   o  . + . .    |
|..o + .  o . o   |
|=oo= =  S = +    |
|=B+.+  + X * .   |
|o+E.  . + * =    |
|  .. .   o o     |
|    .   .        |
+----[SHA256]-----+
[test_test@localhost ~]$ ll -a
合計 16
drwx------. 3 test_test test_test  95  7月  7 12:05 .
drwxr-xr-x. 4 root      root       38  7月  4 22:48 ..
-rw-------. 1 test_test test_test  51  7月  4 22:59 .bash_history
-rw-r--r--. 1 test_test test_test  18  4月  1  2020 .bash_logout
-rw-r--r--. 1 test_test test_test 193  4月  1  2020 .bash_profile
-rw-r--r--. 1 test_test test_test 231  4月  1  2020 .bashrc
drwx------. 2 test_test test_test  38  7月  7 12:05 .ssh
[test_test@localhost ~]$ ll .ssh
合計 8
-rw-------. 1 test_test test_test 1679  7月  7 12:05 id_rsa
-rw-r--r--. 1 test_test test_test  413  7月  7 12:05 id_rsa.pub

作成された公開鍵をログインの対象となるサーバに配置します。
方法は色々あると思いますが、ssh-copy-idコマンドで配置する方法とscpコマンドで配置する方法の2パターンを紹介します。

ssh-copy-idコマンドはかなり簡単です。
クライアント側で、ssh-copy-id "ユーザ名"@"IPアドレス" を実施します。これで、サーバ側に公開鍵が配置されます。
上記コマンド実施後に、サーバ側に公開鍵が配置されているかの確認を行います。ssh-copy-idコマンドで指定したユーザのホームディレクトリ配下に.sshディレクトリが作成されている事を確認します。さらに、その配下にauthorized_keysファイルが作成されている事も確認します。authorized_keysには公開鍵の情報が登録されています。問題が発生していない場合は、これだけで秘密鍵のログインでの行う事が可能です。

コマンド
#クライアント側
[test_test@localhost ~]$ ssh-copy-id test_01@IPアドレス

#サーバ側
[root@localhost test_01]# ll -a
合計 16
drwx------. 3 test_01 test_01  95  7月  7 22:52 .
drwxr-xr-x. 4 root    root     38  7月  4 21:05 ..
-rw-------. 1 test_01 test_01 230  7月  7 22:44 .bash_history
-rw-r--r--. 1 test_01 test_01  18  4月  1  2020 .bash_logout
-rw-r--r--. 1 test_01 test_01 193  4月  1  2020 .bash_profile
-rw-r--r--. 1 test_01 test_01 231  4月  1  2020 .bashrc
drwx------. 2 test_01 test_01  29  7月  7 22:52 .ssh
[root@localhost .ssh]# ll
合計 4
-rw-------. 1 test_01 test_01 413  7月  7 22:52 authorized_keys

もう一つの方法では、若干手順が増えます。
基本はssh-copy-idコマンドでよいと思いますが、記載致します。

scpコマンドで公開鍵をサーバーに送ります。
余談ですが、公開鍵をauthorized_keysファイルを登録する際に権限回りの設定を誤り、秘密鍵でのログインが行えない事が多々あります。なので、ログイン出来ない場合はとりあえず権限回りを確認する事をお勧めします。

所有者/グループをtest_01としたいので、スイッチします。
mkdirコマンドで、.sshディレクトリを作成。アクセス権限も700にします。
id_rsa.pubファイルの中身を、authorized_keysに追記します。アクセス権限は600にします。本番なりお客様環境であれば、追記する前にバックアップを取っておいてください。
これで、秘密鍵でのログインが可能となります。

コマンド
#クライアント側
[test_test@localhost ~]$ scp .ssh/id_rsa.pub test_01@IPアドレス:/home/test_01
test_01@IPアドレス's password:
id_rsa.pub                                    100%  413   379.1KB/s   00:00

#サーバ側
[root@localhost test_01]# ll -a
合計 20
drwx------. 2 test_01 test_01 101  7月  7 23:13 .
drwxr-xr-x. 4 root    root     38  7月  4 21:05 ..
-rw-------. 1 test_01 test_01 235  7月  7 23:06 .bash_history
-rw-r--r--. 1 test_01 test_01  18  4月  1  2020 .bash_logout
-rw-r--r--. 1 test_01 test_01 193  4月  1  2020 .bash_profile
-rw-r--r--. 1 test_01 test_01 231  4月  1  2020 .bashrc
-rw-r--r--. 1 test_01 test_01 413  7月  7 23:13 id_rsa.pub
[root@localhost test_01]# su - test_01
[test_01@localhost ~]$
[test_01@localhost ~]$ mkdir .ssh | chmod 700 .ssh
[test_01@localhost ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[test_01@localhost ~]$ chmod 600 .ssh/authorized_keys

今後はパスワードでのログインは行えないようにしたいとの事だったので、sshd_configを編集します。

PasswordAuthentication yes → PasswordAuthentication no に変更すると、パスワードでのログインが不可となります。
設定反映のために、sshdサービスの再起動も忘れずに行います。
登録後のid_rsa.pub(公開鍵)については、削除しても問題ありません。

参考になれば、幸いです。

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