ssh连接gitlab,免密码登录

    xiaoxiao2022-07-02  113

    第一步:检查是否存在SSH秘钥

    1.Enter ls -al ~/.ssh to see if existing SSH keys are present1.0

    2.如果显示ls: 无法访问/root/work/.ssh: 没有那个文件或目录 表示目录不存在,ssh不存在

    第二步:生成 SSH key.

    1.输入指令:

    $ ssh-keygen -t rsa -C "your_email@example.com"

    注意:字母C要大写,如下图所示: 

    2、生成密钥 

    Enter file in which to save the key (/root/work/.ssh/id_rsa): 【按回车】Enter passphrase (empty for no passphrase): 【输入密码】Enter same passphrase again: 【再次输入密码】

    如上图所示,到此为止,已经生成私钥和公钥,存储路径如下所示:

    Your identification has been saved in /root/work/.ssh/id_rsa.Your public key has been saved in /root/work/.ssh/id_rsa.pub.

     

    第三步:添加 SSH key到 ssh-agent

    1、首先确保ssh-agent 工作

    $ eval $(ssh-agent -s)Agent pid 8569

     

    2、将 ssh 私钥添加到 ssh 代理中。如果使用不同的名称创建了密钥, 或者添加了具有不同名称的现有密钥, 请用私钥文件的名称替换该命令中的 id_rsa。

    $ ssh-add ~/.ssh/id_rsaEnter passphrase for /home/qy/.ssh/id_rsa: 【输入密码】Identity added: /home/qy/.ssh/id_rsa (/home/qy/.ssh/id_rsa)

    执行后报错:

    只需要执行:

    $ssh-agent bash

    第四步:添加一个新的 SSH key到你的Github账户

    1、复制 SSH key 到你的剪贴板

    $ clip < ~/.ssh/id_rsa.pub# Copies the contents of the id_rsa.pub file to your clipboard.

     

    或者直接打开~/.ssh/id_rsa.pub 文件,复制里面的内容 

    2、添加一个新的 SSH key到你的Github账户 

    登录你的github,头像处下拉框选择settings。 

    进入设置页后点击侧边栏的SSH and GPG keys按钮。 

    点击New SSH key, title可以任意填,并且将上一步骤复制的内容粘贴到这里的key输入框中 

    如果出现提示, 请确认您的 GitHub 密码。

     

    如果进行上述操作后还无法免密登录:

    打开ssh配置文件:

    vim /etc/ssh/ssh_config

    要修改的内容如下:

    Host * RSAAuthentication yes PubkeyAuthentication yes GSSAPIAuthentication yes

     

    再次执行:

    $ ssh-add ~/.ssh/id_rsa

    这一步非常重要。配置后,就可以免密登录了。

    最新回复(0)