首页·终端命令

ssh-keygen

网络

用法

ssh-keygen -t ed25519 -C "<comment>"

生成一对公钥和私钥;放到服务器上的只有 `.pub` 那一半,而不设口令生成的密钥就是一个普通文件,谁复制走都能直接登录。

常用选项

选项含义
-t ed25519Key type. ed25519 is the modern default; rsa needs -b 4096 to be worth using.
-C "you@laptop"A comment so you can tell your keys apart later.
-f <path>Where to write the key, when you want more than one.
-N ""No passphrase. Then the file alone is enough to log in.
-pChange or add the passphrase on an existing key.
-l -f <key.pub>Print the fingerprint, to compare against what a server shows.
-y -f <key>Recreate the public half from a private key.
-R <host>Remove a host from known_hosts after it was rebuilt.

示例

ssh-keygen -t ed25519 -C "jade@laptop"

Create a modern key pair in ~/.ssh.

ssh-keygen -R old.example.com

Fix the "REMOTE HOST IDENTIFICATION HAS CHANGED" warning properly.

ssh-keygen -lf ~/.ssh/id_ed25519.pub

The fingerprint of your public key.

连不上的时候,这些能把范围缩小——是名字解析不了、没有路由,还是端口被挡。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. ssh-keygen 是做什么的?

生成一对公钥和私钥;放到服务器上的只有 `.pub` 那一半,而不设口令生成的密钥就是一个普通文件,谁复制走都能直接登录。

Q. 怎么写?

ssh-keygen -t ed25519 -C "<comment>" —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 8 个,完整列表在 man ssh-keygen。这条命令属于网络。

相关命令

man 手册: man ssh-keygen