Ubuntu多个SSH秘钥管理

作者: OriginLeon 2017-01-26 00:09:09

单SSH秘钥管理


 


1、在个人电脑上执行下面的命令,即可生成ssh的key


$ ssh-keygen -t rsa


$ cd ~/.ssh


$ ll


 


2、将生成的id_rsa.pub拷贝到服务器/root/.ssh/下,若没有目录,则创建


 


3、登录远程服务器,这将是最后一次输入密码


ssh root@xxx.xxx.xxx.xxx (若不是22端口 -pxxxx)


 


4、登录服务器后,执行


# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys


 


5、关闭终端,重新打开测试,免密码登录成功。


 


配置github免密码登录(需要再创建SSH的秘钥)


 


1、执行下面命令,可以看到~/.ssh目录下,又生成了两个文件git_rsa和git_rsa.pub


$ ssh-keygen -t rsa -C "youremail@email.com" -f ~/.ssh/git_rsa


$ cd ~/.ssh


$ ll


 


2、在~/.ssh目录下,新建config文件。


$ vim config


Host github.com


Hostname github.com


User git


Identityfile ~/.ssh/git_rsa


$ sudo chmod +600 ./config


 


3、这两个选项会在以后的使用过程中自动添加到代码中。


$ git config --global user.name  "用户名或者用户ID"


$ git config --global user.email  邮箱


 


4、登录github,Setting--->SSH and GPG keys--->New SSH key 将git_rsa.pub中的内容复制到文本框中,保存。


 


5、命令行输入,测试连接是否成功,若出现Permission denied (publickey),说明连接失败,请删除git_rsa和git_rsa.pub后,重复1——4步。


$ ssh -T git@github.com


Hi username! You've successfully authenticated, but GitHub does not provide shell access.


 


6、将git项目push到github。


6.1首先查看git当前的推送方式


```


$ git remote -v


origin https://github.com/xxxxxx/someproject.git (fetch) 


origin https://github.com/xxxxxx/someproject.git (push)


```


6.2将https改为ssh的方式


```


$ git remote set-url origin git@github.com:xxxxxx/someproject.git


```


6.3在git项目的根目录下执行


```


 $ git push


```


 


7、推送成功


 


本文永久更新地址:http://www.linuxdiyf.com/linux/27933.html

相关资讯