- ssh-keygen 创建公私钥秘钥对
- ssh-copy-id 拷贝本地公钥到远程主机到
- ssh-add 将私钥加载到本地主机上的SSH代理
$ 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 email@example.com "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"
$ ssh-copy-id -i ~/.ssh/id_rsa.pub firstname.lastname@example.org /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 email@example.com's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'firstname.lastname@example.org'" and check to make sure that only the key(s) you wanted were added.