公開鍵認証による SSH接続の方法
目次
SSH(Secure Shell)は、ネットワーク上の通信が暗号化されて行われるプロトコルで、安全にリモートコンピュータと通信することができます。SSHのインストール作業はサーバーで行いますが、導入後はリモートアクセスで作業する事が出来るようになります。
今回は、アカウントと公開鍵認証でログインする方法を紹介します。
アカウントとパスワード認証よりセキュリティーが向上します。
SSHをインストール
// インストール
# apt install openssh-server
この記事は2013年の手順で、鍵の作り方と運び方が今のおすすめと違います。
| 項目 | この記事(2013年) | 2026年11月時点のおすすめ |
|---|---|---|
| 鍵の種類 | RSA 2048bit(ssh-keygen -t rsa) | Ed25519(ssh-keygen -t ed25519) |
| 鍵を作る場所 | サーバーで作って、秘密鍵を手元にダウンロード | 手元のパソコンで作って、公開鍵だけをサーバーに送る |
| 公開鍵の登録 | サーバーで authorized_keys に追記 | ssh-copy-id で送る(Windows は下の方法) |
| Windows の接続ソフト | PuTTY(鍵は puttygen で変換) | Windows 10・11 標準の OpenSSH(ターミナルから ssh)。PuTTY も使える |
| 設定の書き方 | sshd_config 本体を編集 | /etc/ssh/sshd_config.d/ に自分の設定だけを置く |
秘密鍵はサーバーに置いたり転送したりしないのが原則です。手元で作れば、秘密鍵は一度もネットワークを通りません。
# 手元のパソコン(Linux・Mac・Windows のターミナル)で作る
$ ssh-keygen -t ed25519 -C "hoge@example.com"
# Linux・Mac:公開鍵をサーバーに登録する
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub hoge@サーバーのIP
# Windows(PowerShell):ssh-copy-id が無いので、公開鍵を送って追記する
PS> type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh hoge@サーバーのIP "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
鍵で入れることを確かめてから、サーバー側でパスワード認証を止めます。
# vi /etc/ssh/sshd_config.d/10-local.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
# sshd -t
# systemctl restart ssh
sshd_config の古い項目が今の OpenSSH でどう扱われるか(実際に検査した結果)は、パスワード認証でSSH接続する方法 の補足にまとめています。
秘密鍵・公開鍵の作成
SSHでログインしたい一般ユーザーで、ssh-keygen コマンドを実行して秘密鍵・公開鍵を作成します。
※ 秘密鍵:id_rsa、公開鍵:id_rsa.pub
// 鍵作成
$ ssh-keygen -t rsa
// 出力内容
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hoge/.ssh/id_rsa):
Created directory '/home/hoge/.ssh'.
Enter passphrase (empty for no passphrase):******* ← パスワードを入力
Enter same passphrase again:******* ← もう一度パスワードを入力
Your identification has been saved in /home/hoge/.ssh/id_rsa.
Your public key has been saved in /home/hoge/.ssh/id_rsa.pub.
The key fingerprint is:
a8:a0:47:4a:fc:05:9b:03:4f:0d:6c:25:1e:43:bb:c9 hoge@hostname
The key's randomart image is:
+--[ RSA 2048]----+
| o*.. |
| .oB |
| ..= . |
|. = * . |
| ooE .. S |
|.+..o. |
|o ... |
| . |
| |
+-----------------+
■ 秘密鍵(クライアント側で使用)
/home/hoge/.ssh/id_rsa
■ 公開鍵(サーバー側で使用)
/home/hoge/.ssh/id_rsa.pub
公開鍵の登録(サーバー側)
作成された公開鍵から authorized_keys を作成します。
// 公開鍵を登録
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
// アクセス権変更
$ chmod 600 ~/.ssh/authorized_keys
// 公開鍵削除(不安であれば残しておいても良い)
$ rm ~/.ssh/id_rsa.pub
秘密鍵の登録(クライアント・Windowsの場合)
id_rsa(秘密鍵)を FTP などを使ってダウンロードして下さい。
秘密鍵の登録するには、フリーソフトの puttygen.exeを使います。
登録手順
(1) puttygen.exe の起動
(2) ファイル → 秘密鍵の読込 → 「ファイルの種類」 → 「全てのファイル」
(3) 手元に持ってきたid_rsa(秘密鍵)を選択
(4) パスフレーズ入力(先ほど入力した、SSHログイン用のパスワードを入力)
(5) 鍵インポートの確認 → OK
(6) 秘密鍵の保存
続いて、putty.exe に秘密鍵を登録します。PuTTYを使います。
(1) putty.exe の起動
(2) カテゴリ → 接続 → SSH → 認証 →「認証のためのプライベートキーファイル」
(3) 先ほど生成した秘密鍵を登録します。
(4) セッションに戻り、セッション情報の登録をして保存。
※この段階では、まだ鍵によるSSH接続はできません。SSH側で鍵認証を有効にする必要があります。
秘密鍵の登録(クライアント・Linuxの場合)
// id_rsa(秘密鍵)をSCPコマンドで取得して ~/.ssh に保存します。
$ scp hoge@コピー元サーバーIP:~/.ssh/id_rsa ~/.ssh
// どのサーバーの鍵か分かりやすいようにリネームします。
$ mv ~/.ssh/id_rsa ~/.ssh/hoge_rsa
// アクセス権変更
$ chmod 600 ~/.ssh/hoge_rsa
既に他の秘密鍵がある場合は、下記のように config を作成して登録します。
$ vi ~/.ssh/config を作成します。
IdentityFile ~/.ssh/user_rsa ← 追加:既にある秘密鍵
IdentityFile ~/.ssh/hoge_rsa ← 追加:今回登録する秘密鍵
SSHサーバー設定(鍵認証有効にする)
# vi /etc/ssh/sshd_config を修正します。
下記の sshd_config は既に修正済みです。
# Package generated configuration file
# See the sshd_config(5) manpage for details
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
// rootのログインを禁止。
PermitRootLogin yes → PermitRootLogin no
// 公開鍵の保存場所を指定。
AuthorizedKeysFile %h/.ssh/authorized_keys
// パスワード認証を禁止(鍵認証で接続が成功してから禁止した方がいいと思います。)
#PasswordAuthentication yes → PasswordAuthentication no
設定ファイルのチェック
// チェック
# /usr/sbin/sshd -t
----- OK例 -----
何も表示されなければOK。
----- NG例 -----
/etc/ssh/sshd_config line 81: Bad yes/no argument: yesi
SSHサーバーを再起動(設定反映)
// 再起動
# systemctl restart ssh
Windowsクライアントからのリモート接続
Windowsクライアントからリモート接続するには、フリーソフトのPuTTYを使います。PuTTY を起動して、ホスト名を入力して開くを押すとサーバーに接続する事が出来ます。
login as: hoge ← 秘密鍵を作成した一般ユーザー。
Authenticating with public key "imported-openssh-key"
Passphrase for key "imported-openssh-key": ******* ← 鍵を作成した時のパスワード
hoge@hostname:~$ su -
パスワード: ******* ← rootのパスワード
hostname:~# ← rootでログインされた。
最後に
生成された秘密鍵を紛失すると、SSHログインが出来なくなりますのでくれぐれもご注意ください。秘密鍵を無くしてしまったら、サーバーの端末を直接操作して、SSHサーバーのパスワード認証を有効にすれば、パスワードでリモート接続出来ます。VPSの場合は、事業者が用意している管理画面にコンソールがあれば、そこからサーバーに接続して同様の手段で対応できます。