ssh-keygenコマンドは認証鍵を生成するコマンドです。
やすひら
ssh-keygenコマンドの概要と使用方法を紹介します
この記事でわかること
- ssh-keygenコマンドの概要
- ssh-keygenコマンドの使い方
目次
コマンド概要
ssh-keygenコマンドは、SSHで使用される公開鍵および秘密鍵のペアを生成するためのコマンドです。
SSHの認証鍵は、リモートサーバーへの安全な接続を確立するために使用され、パスワードを使わずに認証を行うことができます。
コマンドオプション
コマンドオプションを紹介します。
コマンドオプション | 意味 |
---|---|
-t | アルゴリズム選択 |
-b | 鍵の長さを指定(bit) |
-N | パスフレーズを設定 |
コマンドの使い方
ssh-keygenコマンドの使い方を紹介します。
認証鍵を生成
認証鍵を生成します。
コマンド形式
ssh-keygen -t [アルゴリズム] -b [鍵の長さ]
コマンド実行例
$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:f5hc+awy3LfQ0Nm9om5r2OUR5r6Il9qw2mSkp4A6QP0 user@machine000
The key's randomart image is:
+---[RSA 4096]----+
| |
| |
| . |
| . . = o.|
|. . S. * + o|
|. .E oo + O .|
|. . . . *B.B.=. |
| .. . *.OB+=o |
| .. o.=*Oooo. |
+----[SHA256]-----+
$ cat /home/user/.ssh/id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAACFwAAAAdzc2gtcn
NhAAAAAwEAAQAAAgEA66zZCZHSa+XlAxLOemxa+vIyjUFRb80mBuw94P9/nZ+6tRLU3t4x
Q7deufhgUF6urpfbfp1AgOSktsLzxyZJGWDrjccdPkUkI2NL23lwuQ2HleRXEbDsDhU2g2
EHuTTjVp3lKNyQphijDOkCGhevobjE/S4gmdBbIrIGIlndEnGpfkuqqzW+5KfVj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NONWneUo3JCmGKMM6QIaF6+huMT9LiCZ0FsisgYiWd0Scal+S6qrNb7kp9WN1yTtU7D1kT7ydPnzD7T/Pm0MK+uLBU81XyVfESTCYg3cj2fnZIAmRPkk/11kz1ZZKFYlP8+0ZTkDaeR1AdUDEhdVcIwstz3UqVVkR53Odg952Y8JRXKiTvR3DZi81nz4hWYnxdodKHbDGhgpioOT25x+bGDy9VbANbh+IGtnrW/N29xk3xvtGGv/d525dnjXcB9Mn9bYBAEWPd5fmYXXzxZ3c+i/ZHZkk43ikaOw2A7CIqWuC17k5lzTk4537bmc2LPpuWQpUBzoBGLPb/7xW80uXYzJfx+cuTw+p2n7zn2jRsUNDhfhuGYJyTPkxOYRVhoP0e9HD3yhXnas+HUbcQTzezuYCnbx8kL6Vf8MWlbFjQOZHOPuyc09sA2E0YhEz6yYIoUZcUuPwsTjG8xrrP6Vh83GJV0c6eJ1Tm3zioUj5jqrp7SApx1FCBOT9C7t+eOjoKsBSKThnGF+woa+vgktLQjqks7nux2/KCL4Kutv00Wttw== user@machine000
パスフレーズありで認証鍵を生成
パスフレーズありで認証鍵を生成します。
コマンド形式
ssh-keygen -t [アルゴリズム] -b [鍵の長さ] -N [パスフレーズ]
コマンド実行例
$ ssh-keygen -t rsa -b 4096 -N "hogehoge"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:xNfJcWalSvEC2Xeoc3GLsBapsfiOsnTGMPEl2YjjHDY user@machine000
The key's randomart image is:
+---[RSA 4096]----+
| .o+ +o.|
| ..+..B.O+.o|
| E ++o= Xo++.|
| + *oo+ +o+.. |
| = .S . .o |
| + . |
| . +o |
| ..o. . |
| .o |
+----[SHA256]-----+
$ cat /home/user/.ssh/id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAA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JpH8aPMbYVuSuCJesGU4l/t4wVGiFzFm6fUPUgHB2/WUHTNW4bFCmgYg5CQ/UOUFh/fc
Ntzg2vjD12IViwU2m6a5EonaA=
-----END OPENSSH PRIVATE KEY-----
$ cat /home/user/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDCkFeuRYzwb1PnfsKq8IWyewKnswkp1Rsn1sZRaCXHn8damD7oBwhidO8pn8h+azdNeQlNCVToh18HekyTzGrNEnGjvWFXmn4G7+LQ3EGvSHtu1iAUv+7Ib7cgpTyN8m7/P368s6IB29Xd903L8wGUi01+9qPiVFxv2LuuSAn70Jl6bVtWO44z1aaAt0IoPIa1i2+d+AsCZnNWgrJ45zXu2Ckl4i4ImKcfU25KyDDeiiHgsEoVXug+0AT5hC7+MYEp5hVnff4qeaz19TOK205Ndg11pjibWCLrWubpNATCyqFyNh0ju6Re5sZP2nYAeKNvAkhMwNm//JYV6mc0i2KTwokO/+KO8FldksS4uzCNyueKrjG5YlH+Vss5UsRS6F16bkgPY5pkS7/MZWFBD0nvo/Sf0ibbZLLX4icx/Pfq5/mx2rvdXA9xaM6HMnr7+FyJAQ8nW+Zv7VtFZqDnDLyJtf6JH9eplY1zsalW1aFQ8S62Cn3mjZ6RxaH6xSgnYNRCsv6SJ1KyuqO+EYOSLTGCAlEmYdCJuRw8fxvxpZl++zGpTzI3AECoUFwosXkeCWkmcIKpAjDbiHdQMrbIMO/C6RK3qpwtVrMve7PgsEKc5L1bP4j0MWnqMkO9JLhopWVbi1TnJxnJdnwye7LKR9fO5lujvonHAIF98TERoD0noQ== user@machine000
まとめ
ssh-keygenコマンドの概要と使い方を紹介しました。
ssh-keygenコマンドは
- SSHの認証鍵を生成できる
SSHを使用する場合、鍵認証によるログインの方が、パスワード認証より安全で楽なので、ssh-keygenコマンドで認証鍵を利用することを推奨します。