Linux 配置 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 指令来添加应用当前公钥的服务器地址,例如 [email protected][email protected]

ssh -T [email protected]
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 同名的分支上。

参考:

猜你喜欢

转载自blog.csdn.net/linshuhe1/article/details/79390396