本文Linux系統爲CentOS Linux 7。
shell
ssh-keygen命令生成公鑰和私鑰,名字分別是「id_rsa.pub」和「id_rsa」,公鑰發送到服務器上,私鑰本地使用。服務器
遠程是在「~/.ssh/authrized_key」文件中,本地是在「%usersprofile%/.ssh/」目錄ssh
密鑰生成命令
#ssh-keygen -t rsaide
操做記錄工具
[root@learnshell ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 密鑰文件存儲路徑,直接回車使用默認
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 密鑰文件密碼,直接回車使用不設置
Enter same passphrase again:
[root@learnshell ~]# cd /root/.ssh/
[root@learnshell .ssh]# ls
id_rsa id_rsa.pub命令行
密鑰上傳code
使用「ssh-copy-id」命令上傳
[root@learnshell .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.2.106
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.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
root@192.168.2.106's password: blogNumber of key(s) added: 1ci
Now try logging into the machine, with: "ssh 'root@127.0.0.1'"
and check to make sure that only the key(s) you wanted were added.rem
使用其餘工具將文件添加至authorized_keys文件末尾
上傳 id_rsa.pub文件後,使用重定向符追加至authorized_keys文件末尾
[root@learnshell .ssh]$ cat id_rsa.pub >> authorized_keys
[root@learnshell .ssh]# cat ~/.ssh/authorized_keys
ssh-rsaAAAAB3NzaC1yc2EAAAADAQABAAABAxxxxxAwmvid5q8naRwUohZTHYa/BVP0vxhdcav2TI0b4YRFIPdhd/kNprqV1HgQQQdbOdHwqMXepM68B+n8ukt7JFyfEF1IHML8hQy27hoJHwUo2fNJI8N8w5mO6glG+GxQciPP4dAJ+UAdd1qxtj3QKhYvRxxxxxoqDKOvkkvTp0brHz0vYDNV8Fx6MELMz4rSdcQPFZhSeZ9P5dJCcW6hdPBcUnKKMY8RtadPAPw+AGINexxxxxXbwEcqLxNRpW/DHG5E14Voxp4lgndeWOt8CJNWgWrp3mAYhf9sXvtICTdnhZA8Hjs0ckUV6tabfUlKsyj1gKEgSymP root@learnshell.local
命令行使用
將id_rsa拷貝至「%usersprofile%/.ssh/」目錄,直接使用「ssh root@192.168.2.106」登陸
SecureCRT使用
SecureCRT,新建會話,協議選擇「ssh2」,其餘根據實際狀況填寫,選中「PublicKey」,點擊左側的「Properties」,瀏覽生成的私鑰保存。
注意,使用私鑰登陸須要去掉「Password」前的勾。