SSHノンパス設定メモ。(公開鍵認証)
| 登録日 | :2025/07/20 11:58 |
|---|---|
| カテゴリ | :Linux |
公開鍵認証によるSSHノンパス設定手順
まず、クライアントノードでSSH鍵ペアを作成する。
コマンド)
ssh-keygen -t rsa
以下、対話型シェルが立ち上がるが、特に何も設定せずにEnterで鍵ペア作成可能。
パスフレーズは任意。セキュリティとしては登録してもよいが、ノンパス設定にパスフレーズの入力が必要になり、ssh-agentを利用する必要があるなど、さらに手間がかかる。
今回は、パスフレーズなし、としての手順とする。
実行例)
[root@rx94rpmbuild ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:dfO8zTeT2aJsHh2Nbx+3xcuByZoVC0LRWZ4nyy1ri0E root@rx94rpmbuild
The key's randomart image is:
+---[RSA 3072]----+
| .. o. |
| .o. . |
| .. o+ . |
| .. ..+=o |
| S. E ==..|
| o o.BB+|
| ..O+B@|
| .Oooo%|
| *+. +.|
+----[SHA256]-----+
[root@rx94rpmbuild ssh]#
作成した鍵ペアのうち、公開鍵(id_rsa.pub)をSSHサーバ側のauthorized_keysに登録する
コマンド)
cat ~/.ssh/id_rsa.pub | ssh root@hostname 'cat >> .ssh/authorized_keys'
ホスト名は、サーバのIPアドレスでもよい。/etc/hostsやDNSに登録している場合は、hostnameで指定してもOK。
実行例)
[root@rx94rpmbuild ssh]# cat ~/.ssh/id_rsa.pub | ssh root@gpu01 'cat >> .ssh/authorized_keys'
root@gpu01's password:
[root@rx94rpmbuild ssh]#
テスト)
ノンパスでログインできることを確認する
[root@rx94rpmbuild ssh]# ssh gpu01
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sun Jul 20 11:55:11 2025 from 192.168.56.120
[root@gpu01 ~]#
2026.2.22
最近は、暗号方式として、ed25519を使った方が良さそう。
MacでもLinuxに対して、SSHノンパス設定可能。
~$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/nobuyuki/.ssh/id_ed25519):
Enter passphrase for "/Users/nobuyuki/.ssh/id_ed25519" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/nobuyuki/.ssh/id_ed25519
Your public key has been saved in /Users/nobuyuki/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:++NsMtgMP88Wa1xrEv/FcMRZp3Ojil8t64ktlBscCuI nobuyuki@nobuyukinoMacBook-Air.local
The key's randomart image is:
+--[ED25519 256]--+
| o|
| o+|
| oo+|
| . . . .+.|
| . . S o o.. .|
| E. o+=o .+ |
| *.o.Bo+ .o|
| . B+XoB +. |
| X**o*. |
+----[SHA256]-----+
~$ ls
Desktop/ Downloads/ Movies/ Pictures/ PyCharmMiscProject/
Documents/ Library/ Music/ Public/ RockyLinux8_FileServer.utm/
~$ ls -a .ssh
./ ../ id_ed25519 id_ed25519.pub known_hosts known_hosts.old
~$ cat ~/.ssh/id_ed25519.pub | ssh root@192.168.64.2 'cat >> .ssh/authorized_keys'
root@192.168.64.2's password:
~$ ssh root@192.168.64.2
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sat Feb 21 12:19:58 2026 from 192.168.64.1
[root@rx8manage ~]#