- 第一步
ssh-keygen -t rsa -f ~/.ssh/id_rsa
输入服务器登录密码 - 第二步
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 8022 zhangsan@10.81.126.118
此时使用 ssh luban@10.81.126.118 -p 8022 命令登录,会如下提示,要求输入私钥
Enter passphrase for key ‘~/.ssh/id_rsa’: - 第三步
将私钥添加到钥匙串中,按照提示输入私钥密码
ssh-add -K ~/.ssh/id_rsa - 第四步
新建config文件,配置config文件,实现免密快速登录
vim ~/.ssh/config
添加
Host zs
User zhangsna
Hostname 10.81.126.118
Port 8022
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
UseKeychain yes
AddKeysToAgent yes - 第五步
ssh zs 直接登录服务器
scp dir_local zs:dir_remote 跳过端口和密码
如果遇到错误bad configuration option usekeychain
可以添加两行到配置文件里,参考https://www.unixtutorial.org/ssh-bad-configuration-option-usekeychain/
Luckily, there’s a simple enough fix: just add the IgnoreUnknown directive into .ssh/config right above the “UseKeychain yes” and it will be ignored on Linux systems. On macOS it will still keep working as intended.
Here’s how the updated fragment of .ssh/config should look:
IgnoreUnknown UseKeychain
UseKeychain yes