使用场景
想象这样一种场景,工作时间开发公司项目;下班的后同一台机器也可能需要维护个人的Github上的开源项目。配置多账户就是一个很现实的问题。
配置步骤
前提是已经安装Git客户端了;
- 如果配置了全局用户,请取消该配置。Git命令如下:
--取消全局配置
git config --global --unset user.name
git config --global --unset user.email
--在项目所在目录下 设置项目所用的账户信息
git config user.email "somebody@someweb.com"
git config user.name "somename"
- 生成与邮箱相关的sshkey并配置,如下:
---Windows为例,路径可以自定义,注意大小写
$ ssh-keygen -t rsa -f C:/Users/用户名/.ssh/id_rsa_alicode -C "somebody@someweb.com"
- 为不同的项目网址配置指定公钥,配置文件位于C:/Users/登录用户/.ssh/config,注意是一个文件不是文件夹,如果不存在就新建一个同名文件,找到后按下面格式编辑该文件。
# First user 个人项目git配置
Host code.aliyun.com
HostName code.aliyun.com
User aliyun
IdentityFile C:\Users\用户名\.ssh\id_rsa_alicode
# second user 公司项目配置
Host 192.168.1.1
HostName 192.168.1.1
User work
IdentityFile C:\Users\用户名\.ssh\id_rsa
- 测试公钥配置是否生效,如下:
---查看是否采用了正确的id_rsa_alicode文件
$ ssh -vT code.aliyun.com
---生效的结果是下面这样的
OpenSSH_7.5p1, OpenSSL 1.0.2k 26 Jan 2017
debug1: Reading configuration data /c/Users/ziyuo/.ssh/config
debug1: /c/Users/ziyuo/.ssh/config line 2: Applying options for code.aliyun.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to code.aliyun.com [120.55.150.20] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\ziyuo\\.ssh\\id_rsa_alicode type 1
debug1: key_load_public: No such file or directory
debug1: identity file C:\\Users\\ziyuo\\.ssh\\id_rsa_alicode-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5
debug1: Remote protocol version 2.0, remote software version Go
debug1: no match: Go
debug1: Authenticating to code.aliyun.com:22 as 'aliyun'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:ZrA2ZqYTVyPbw4zytCSAv74ZMaS2LDH74I7sMPtQIG0
debug1: Host 'code.aliyun.com' is known and matches the RSA host key.
debug1: Found key in /c/Users/ziyuo/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: C:\\Users\\ziyuo\\.ssh\\id_rsa_alicode
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).
Authenticated to code.aliyun.com ([120.55.150.20]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: channel 0: free: client-session, nchannels 1
Connection to code.aliyun.com closed by remote host.
Transferred: sent 2648, received 1472 bytes, in 0.0 seconds
Bytes per second: sent 8978590.9, received 4991120.0
debug1: Exit status -1
- 配置生效后,就可以clone项目了;这时候可以按照步骤1的操作为单个项目设置独立账户。
以上,祝顺利!
网友评论