Linux 和 Windows 下配置 SSH(复用已有的公钥)

假如项目中添加了需要使用 ssh 方式连接服务器的 git 子模块,则需要配置 ssh 公钥,这里我是直接复用之前用过的公钥,这样我们可以在多台设备上使用同一份公钥。

复用旧公钥

~/ 目录下新建 .ssh 目录,然后复制已有的 id_rsaid_rsa.pub 到此目录下,然后修改 id_rsa 的权限,不然会报错 "Permissions 0644 for '/root/.ssh/id_rsa' are too open.",修改方式:

$ cd ~/.ssh
$ sudo chmod 0600 id_rsa

然后执行 ssh 指令来添加应用当前公钥的服务器地址,例如 git@github.comgit@gitee.com

ssh -T git@gitee.com
The authenticity of host 'gitee.com (116.211.167.14)' can't be established.
ECDSA key fingerprint is SHA256:xxx
ECDSA key fingerprint is MD5:27:xxx
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,116.211.167.14' (ECDSA) to the list of known hosts.
Welcome to Gitee.com, linshuhe1!

~/.ssh 目录下会生成一个 known_hosts 文件。

免密码 git 操作

假如配置完了 ssh 然后执行 git pull 还需要输入账号密码,说明在使用 git clone 指令拉取仓库源码时使用的地址是 HTTPS 格式而非 SSH 格式,两个格式的差别在于:

推荐使用 SSH 格式拉取 git 工程。

解决 warning: push.default is unset

使用 ssh 管理工程之后,在执行 git push 提示:

git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

导致原因:push 的时候没有指定要 push 修改的分支

解决方案:执行 git config --global push.default matching 即可,它的作用就是 push 的时候在没有指定 branch 分支时,采用同名匹配的规则,默认 push 到与当前项目所在 branch 同名的分支上。

Window 下复用已有公钥

将已有的公钥和私钥文件 id_rsaid_rsa.pub 放在当前用户目录下的 .ssh 文件夹下,然后在 Git bash 下配置仓库服务器:
github 配置:

$ ssh -T git@github.com
Hi linshuhe! You've successfully authenticated, but GitHub does not provide shell access.

码云配置:

$ ssh -T git@gitee.com
The authenticity of host 'gitee.com (116.211.167.14)' can't be established.
ECDSA key fingerprint is SHA256:(...).
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,116.211.167.14' (ECDSA) to the list of known hosts.
Hi linshuhe1! You've successfully authenticated, but GITEE.COM does not provideshell access.

Coding 配置:

$ ssh -T git@git.coding.net
The authenticity of host 'git.coding.net (118.25.166.124)' can't be established.
RSA key fingerprint is SHA256:(...).
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'git.coding.net,118.25.166.124' (RSA) to the list ofknown hosts.
Coding 提示: Hello linshuhe, You've connected to Coding.net via SSH. This is a personal key.
linshuhe,你好,你已经通过 SSH 协议认证 Coding.net 服务,这是一个个人公钥

参考:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值