内容纲要
  • ssh-keygen 创建公私钥秘钥对
  • ssh-copy-id 拷贝本地公钥到远程主机到 authorized_keys 文件
  • ssh-add 将私钥加载到本地主机上的SSH代理

生成秘钥对

local

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/osboxes/.ssh/id_rsa): 
Created directory '/home/osboxes/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/osboxes/.ssh/id_rsa.
Your public key has been saved in /home/osboxes/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:8nV1GYGA0a2Hu3Yweomg8LVgPsF2PD2nWU/vJlLMBkk osboxes@osboxes
The key's randomart image is:
+---[RSA 2048]----+
|         .+.o .o.|
|         . E o  o|
|          . + ...|
|           = o . |
|    . o S . B    |
|   . * O + B *   |
|    * * = O X .  |
|     = . = * + o |
|      .   o o +. |
+----[SHA256]-----+

拷贝公钥

$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
jsmith@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

或者

$ cat ~/.ssh/id_rsa.pub | ssh demo@198.51.100.0 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >>  ~/.ssh/authorized_keys"

登录远程主机

$ ssh-copy-id -i ~/.ssh/id_rsa.pub osboxes@192.168.59.131
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/USERNAME/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
osboxes@192.168.59.131's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'osboxes@192.168.59.131'"
and check to make sure that only the key(s) you wanted were added.

other

发表评论

电子邮件地址不会被公开。 必填项已用*标注