本地生成ssh私钥、公钥文件
ssh-keygen -t rsa -C "you_email@qq.com"
默认生成id_rsa私钥和id_rsa.pub公钥
复制id_rsa.pub公钥至远程服务器
使用ssh-copy-id命令
ssh-copy-id -i ~/.ssh/id_rsa.pub username@ip
第一次需要输入远程服务器密码
修改本地.ssh/config文件
编辑~/.ssh/config,添加下面内容
Host HostA
HostName HostA.domain.net
User useratA
IdentityFile ~/.ssh/id_rsa
HostA是别名,可以随意定,ssh连接时用到,HostName是主机域名,没有域名,用IP也可以,User指定ssh登录用户,IdentityFile指向ssh-keygen生成的本地私钥
当有多个要登录的远程服务器时,config可以这样写
Host HostA
HostName HostA.domain.net
User useratA
IdentityFile