免密登录 SSH

通过配置别名与公私钥一键登录远程服务器。

步骤

  • 将本地shell配置文件追加一行,如oh my zsh的配置文件路径为~/.zshrc
alias ssh_dev='ssh -p 端口 用户名@ip地址'
  • 本地生成公私钥
cd ~/.ssh
ssh-keygen -t rsa
# 接下来可选择输入文件名然后回车回车回车,或者不输入回车回车回车,这里指定了文件名
# Enter file in which to save the key (/Users/friday/.ssh/id_rsa): ssh1
# Enter passphrase (empty for no passphrase): 
# Enter same passphrase again: 
# Your identification has been saved in ssh1.
# Your public key has been saved in ssh1.pub.
  • 将公钥上传到远程服务器
scp ./ssh1.pub username@ip:~/.ssh
  • 登录远程服务器并追加公钥
cd ~/.ssh
cat ssh1.pub >> authorized_keys
如果你是 mac 用户,还需在本机~/.ssh目录添加此私钥,执行此命令:

ssh-add -K ssh1

使用

执行你设置的 alias别名即可登录服务器

添加新评论

还没有评论哟 ~