1. 程式人生 > >Linux服務器 ssh免密登錄

Linux服務器 ssh免密登錄

key main ber pty 添加 ant rop sta usr

本文Linux系統為CentOS Linux 7。
?

?ssh-keygen命令生成公鑰私鑰,名字分別是“id_rsa.pub”和“id_rsa”,公鑰發送到服務器上,私鑰本地使用。

遠程是在“~/.ssh/authrized_key”文件中,本地是在“%usersprofile%/.ssh/”目錄

密鑰生成命令
#ssh-keygen -t rsa

操作記錄

[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

密鑰上傳

使用“ssh-copy-id”命令上傳
[root@learnshell .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]


/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
[email protected]‘s password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh ‘[email protected]‘"
and check to make sure that only the key(s) you wanted were added.

使用其他工具將文件添加至authorized_keys文件末尾

上傳 id_rsa.pub文件後,使用重定向符追加至authorized_keys文件末尾

[root@learnshell .ssh]$ cat id_rsa.pub >> authorized_keys

[root@learnshell .ssh]# cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAxxxxxAwmvid5q8naRwUohZTHYa/BVP0vxhdcav2TI0b4YRFIPdhd/kNprqV1HgQQQdbOdHwqMXepM68B+n8ukt7JFyfEF1IHML8hQy27hoJHwUo2fNJI8N8w5mO6glG+GxQciPP4dAJ+UAdd1qxtj3QKhYvRxxxxxoqDKOvkkvTp0brHz0vYDNV8Fx6MELMz4rSdcQPFZhSeZ9P5dJCcW6hdPBcUnKKMY8RtadPAPw+AGINexxxxxXbwEcqLxNRpW/DHG5E14Voxp4lgndeWOt8CJNWgWrp3mAYhf9sXvtICTdnhZA8Hjs0ckUV6tabfUlKsyj1gKEgSymP [email protected]

免密登錄使用

命令行使用
將id_rsa拷貝至“%usersprofile%/.ssh/”目錄,直接使用“ssh [email protected]”登錄

技術分享圖片

SecureCRT使用
?SecureCRT,新建會話,協議選擇“ssh2”,其他根據實際情況填寫,選中“PublicKey”,點擊左側的“Properties”,瀏覽生成的私鑰保存。
?註意,使用私鑰登錄需要去掉“Password”前的勾。

技術分享圖片
技術分享圖片
技術分享圖片

Linux服務器 ssh免密登錄