CentOS のロゴは「Red Hat Inc.」の登録商標です。ロゴは公式ページの利用規約を沿って使用しています。
VPS やオンプレミス環境などのサーバーを構築したとき、CentOS では初期のログイン方法としてパスワード形式が設定されています。
セキュリティ上、パスワードによるログインを許容すると不正ログインをされる可能性が高くなるため、あまりおすすめできません。そこで今回は SSH ログイン方法について、パスワード認証から公開鍵認証に変更する手順をまとめていきます。
Linux サーバーにて SSH の公開鍵認証を設定する手順
公開鍵認証を設定する手順は、大きく分けると下記のようになります。
- 公開鍵と秘密鍵を作成する
- サーバーに公開鍵の設定する
- sshd_config ファイルにて公開鍵認証を許容する
- sshd_config ファイルにてパスワードによるログインを禁止する
では順番に作業手順を確認していきましょう。
ログイン用の公開鍵と秘密鍵を作成する方法
公開鍵と秘密鍵を作成するコマンド
ログイン用の公開鍵と秘密鍵は、「ssh-keygen」コマンドで作成します。
公開鍵と証明書が作成される場所は、root ユーザーと一般ユーザーで異なります。rootユーザーは「/root」フォルダ直下にファイルが生成され、一般ユーザーは「/home/(ユーザー名)」フォルダに作成されます。
root ユーザーの公開鍵と証明書が作成される場所(パス)
証明書 | 作成されるパス |
---|---|
秘密鍵 | /root/.ssh/id_rsa |
公開鍵 | /root/.ssh/id_rsa.pub |
一般ユーザーの公開鍵と証明書が作成される場所(パス)
証明書 | 作成されるパス |
---|---|
秘密鍵 | /home/(ユーザー名)/.ssh/id_rsa |
公開鍵 | /home/(ユーザー名)/.ssh/id_rsa.pub |
公開鍵と秘密鍵を作成した実行サンプル
今回は root ユーザーの公開鍵と秘密鍵した際の実行履歴を載せています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[root@localhost]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:qH2b4yFU5y+FDD7LaM++WYWVr1BnfLZ1MNa6ZxszS3k hoge@example The key's randomart image is: +---[RSA 2048]----+ | . . .| |o o . | | .+. . o =.| | B* o o o . . | | S o o ++.O.| | + o o++o *| | *E . = o oo o | |o . + o. . o.*| | .o . .o oo| +----[SHA256]-----+ |
作成した公開鍵をサーバーに設定する方法
ログインしたいユーザーのカレントディレクトリ以下、「.ssh/authorized_keys」に公開鍵を設定します。なお authorized_keys ファイルには、改行区切りで複数の公開鍵を設定することができます。
公開鍵の設置先
ユーザー | 公開鍵の設置先パス |
---|---|
root ユーザー | /root/.ssh/authorized_keys |
一般ユーザー | /home/(ユーザー名)/.ssh/authorized_keys |
今回は作成した公開鍵である id_rsa.pub ファイルを authorized_keys ファイルとしてコピーして使用します。
1 2 3 4 5 6 7 8 9 10 11 |
[root@localhost]# chmod 700 .ssh [root@localhost]# cd .ssh [root@localhost .ssh]# cp id_rsa.pub authorized_keys [root@localhost .ssh]# chmod 600 authorized_keys [root@localhost .ssh]# ls -la total 20 drwx------ 2 root root 4096 Apr 6 10:21 . dr-xr-x---. 5 root root 4096 Apr 6 10:36 .. -rw------- 1 root root 396 Apr 6 10:21 authorized_keys -rw-r--r-- 1 root root 396 Apr 6 10:15 id_rsa -rw-r--r-- 1 root root 396 Apr 6 10:15 id_rsa.pub |
sshd_config ファイルにて公開鍵認証を許容する
次にサーバーへのログイン方法をパスワード形式から公開鍵認証に変更します。CentOS では SSH に関係する設定は「/etc/ssh/sshd_config」に全て記述されています。
パスワード形式から公開鍵認証に変更する方法
ログイン方法をパスワード形式から公開鍵認証に変更したい場合は、「PubkeyAuthentication yes」に変更します。なおコメントアウトされているときはコメントアウトを外して有効化してください。
公開鍵認証を許容する実行サンプル
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
vi /etc/ssh/sshd_config # This is the sshd server system-wide configuration file. See # sshd_config(5) for more information. # This sshd was compiled with PATH=/usr/local/bin:/usr/bin # The strategy used for options in the default sshd_config shipped with # OpenSSH is to specify options with their default value where # possible, but leave them commented. Uncommented options override the # default value. (省略) # Authentication: (省略) #RSAAuthentication yes #PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys #AuthorizedKeysCommand none #AuthorizedKeysCommandRunAs nobody (省略) |
パスワードによるログインを禁止(無効化)する
パスワードの無効化も「/etc/ssh/sshd_config」にある PasswordAuthentication 項目を「no」に設定することが実現することができます。
sshd_config の文法チェック
SSH の設定ファイルである shd_config の文法チェックは「/usr/sbin/sshd -t」で行うことができます。sshd の設定変更を行った際は reload や restart を実行する前に必ず文法チェックを行うことをおすすめします。文法が間違った状態で再実行すると最悪、サーバーに全くログインできなくなる可能性があります。
もし sshd のパスが無いというエラーが出た場合は、「which sshd」でご自身の sshd の設置場所を確認した上でコマンドを実行してください。
sshd_config の設定反映
最後に sshd をリロードして設定を反映して、設定終了です。
1 2 |
[root@localhost]# systemctl reload sshd Reloading sshd: [ OK ] |
ページ数: 440ページ
出版社:SBクリエイティブ
発売日:2015/06/06
出典:amazon

Author:しき 投稿一覧
関連する記事
インフラカテゴリの最新記事
-
- 2023.02.09
- docker,
[docker]対処法:コンテナにログインした時にvi(vim)コマンドが使えない
-
- 2023.01.27
- docker,
[docker]対処法:OCI runtime create failed exec: “bash”: executable file not found
-
- 2023.01.06
- ubuntu,
Debian/Ubuntu系統のサーバOSではcrontabのMAILFROMは使えない件について
-
- 2022.07.19
- ubuntu,
[AWS]Ubuntu22.04 LTS にSSH接続(ログイン)できない問題の解決方法
-
- 2022.06.07
[python]torch がインストールできない場合の対応策
-
- 2022.06.02
- cloudwatch,
[AWS]CloudWatchで監視を行う項目