linux-上传ssh公钥到服务器上

服务器装完新系统需要上传公钥,以后用ssh客户端连才不需要输入密码
同时需要 git clone 其他地方的仓库,需要上传 秘钥


上传公钥

让有 秘钥 的客户端通过ssh连上服务器

  1. 服务器新建一个 .ssh 文件夹
1
2
root@localhost:~# mkdir .ssh
root@localhost:~# cd .ssh/
  1. 执行上传命令
1
2
3
4
5
# 服务器地址 11.22.33.44,ssh 端口 29158,本地文件  id_rsa.pub,上传服务器目录 /root/.ssh/authorized_keys
$ scp -P 29158 id_rsa.pub root@11.22.33.44:/root/.ssh/authorized_keys

root@11.22.33.44's password: # 需要输入 root 用户的密码
id_rsa.pub 100% 742 3.8KB/s 00:00 # ok

然后服务器上就有这个东西了

1
2
root@localhost:~/.ssh# ls -la
-rw-r--r-- 1 root root 742 Jul 3 22:06 authorized_keys
  1. 然后就可以用ssh链接服务器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ ssh -l root 11.22.33.44 -p 55
    Welcome to Ubuntu 16.04 LTS (GNU/Linux 2.6.32-042stab123.3 x86_64)

    * Documentation: https://help.ubuntu.com/
    Last login: Mon Jul 3 21:57:45 2017 from 33.44.55.66
    root@localhost:~#

    root@localhost:~# logout # 登出服务器
    Connection to 11.22.33.44 closed.

上传秘钥

让服务器可以 git pull 下私有仓库

  1. 基于上一步 上传公钥 ,已经不用密码链接,直接上传秘钥

    1
    2
    $ scp -P 29158 id_rsa root@11.22.33.44:/root/.ssh/id_rsa
    id_rsa 100% 3243 8.1KB/s 00:00
  2. 测试一下 clone 自己在开源中国的私人仓库

    1
    2
    3
    4
    5
    6
    7
    8
    9
    root@ubuntu:~# git clone git@git.oschina.net:yangxuan0261/z_mywiki.git
    Cloning into 'z_mywiki'...
    remote: Counting objects: 1788, done.
    remote: Compressing objects: 100% (1525/1525), done.
    remote: Total 1788 (delta 1053), reused 421 (delta 225)
    Receiving objects: 100% (1788/1788), 2.52 MiB | 1.50 MiB/s, done.
    Resolving deltas: 100% (1053/1053), done.
    root@ubuntu:~# ls
    auth docker-compose.yml test_yml z_mywiki
  3. ok, done