用SSH免密码登录Linux服务器

参考文章。

  1. 首先在本机器生成密钥对 key pair:
    输入ssh-keygen,然后一路回车。这样就成功的在~/.ssh/下创建了id_rsa私钥和id_rsa.pub公钥,且没有passphrase密码。
  2. 连接到服务器。一般也是通过SSH连接,因为没配置好ssh呢,所以先用户密码登录。
  3. 将本地的id_rsa.pub公钥内容复制到服务器的~/.ssh/authorized_keys文件中,这个文件支持多个公钥设置,每一行写一个:
echo "刚刚复制的本机公钥内容" >> ~/.ssh/authorized_keys
  1. 一般来说,到了这里,就可以直接通过ssh登录服务器了。
  2. 有的服务器的ssh默认设置,没有允许别人通过密钥登录等等,所以需要在设置文件里修改下:
vim /etc/ssh/sshd_config

#然后找到以下几样内容,改成一样的:
# 开启密钥登录功能
RSAAuthentication yes
PubkeyAuthentication yes

#  root 用户也可以通过 SSH 登录
PermitRootLogin yes

# 禁用密码登录
PasswordAuthentication no

# 编辑完后,保存退出,然后重启ssh
service sshd restart

更新

更方便的方法:

# 将私钥加入 ssh-agent
$ ssh-add ~/.ssh/id_rsa

# 将公钥复制到树莓派上
$ ssh-copy-id [email protected]

猜你喜欢

转载自blog.csdn.net/weixin_33712987/article/details/87474652