Network: 解决多个ssh key问题 - 通过~/.ssh/config文件进行管理

如果在本地需要有多个ssh key, 可以通过如下方法进行管理:

1. 通过ssh-add命令通知ssh-agent管理这些key

hzak@B85RPI:~/.ssh$ ssh-add ~/.ssh/github_brobwind
Could not open a connection to your authentication agent.

ssh-agent运行着呢,why?

hzak@B85RPI:~/.ssh$ ssh-agent 
SSH_AUTH_SOCK=/tmp/ssh-B2BQngT8ulRb/agent.12305; export SSH_AUTH_SOCK;
SSH_AGENT_PID=12306; export SSH_AGENT_PID;
echo Agent pid 12306;

2. 通过~/.ssh/config文件进行管理

Host *.github.com
    IdentiftyFile ~/.ssh/github_brobwind
    User brobwind

这里可以看出如果通过ssh登录到github.com时会使用~/.ssh/github_brobwind文件中的private key,测试配置是否生效可通过如下命令:

hzak@B85RPI:~/.ssh$ ssh -T git@github.com
Hi brobwind! You've successfully authenticated, but GitHub does not provide shell access.

显示更加详细的信息可通过如下命令:

hzak@B85RPI:~/.ssh$ ssh -T -v git@github.com
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /home/hzak/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to github.com [192.30.253.112] port 22.
debug1: Connection established.
debug1: identity file /home/hzak/.ssh/id_rsa type -1
debug1: identity file /home/hzak/.ssh/id_rsa-cert type -1
debug1: identity file /home/hzak/.ssh/id_dsa type -1
debug1: identity file /home/hzak/.ssh/id_dsa-cert type -1
debug1: identity file /home/hzak/.ssh/id_ecdsa type -1
debug1: identity file /home/hzak/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/hzak/.ssh/id_ed25519 type -1
debug1: identity file /home/hzak/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7
debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /home/hzak/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/hzak/.ssh/id_rsa
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.253.112]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LC_PAPER = en_US.UTF-8
debug1: Sending env LC_ADDRESS = en_US.UTF-8
debug1: Sending env LC_MONETARY = en_US.UTF-8
debug1: Sending env LC_NUMERIC = en_US.UTF-8
debug1: Sending env LC_TELEPHONE = en_US.UTF-8
debug1: Sending env LC_IDENTIFICATION = en_US.UTF-8
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_MEASUREMENT = en_US.UTF-8
debug1: Sending env LC_TIME = en_US.UTF-8
debug1: Sending env LC_NAME = en_US.UTF-8
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi brobwind! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3504, received 1480 bytes, in 0.5 seconds
Bytes per second: sent 7295.9, received 3081.6
debug1: Exit status 1

发表评论

电子邮件地址不会被公开。 必填项已用*标注