ssh免密码登录


ssh连接通常需要输入用户的密码,但是可以通过生成RSA加密算法完成面密码登录。

方法

首先在本地通过RSA加密算法生成公私钥对

ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa

此时在本地用户.ssh文件夹下生成了一个密钥id_rsa和一个公钥id_rsa.pub
将我们公钥放入我们要远程登录的服务器的对应用户的.ssh文件夹下。下次进行SSH连接时,会将本地的密钥和服务器端的公钥进行比对,从而免去密码登录。
当本地有多个服务器的密钥时,需要连接时需要在配置文件中进行指明。比如

Host my-server(3090)
  HostName 服务器ip
  Port 22
  User username
  IdentityFile 本地密匙位置
.ssh文件目录
  • config: 配置文件
  • id_rsa: 私钥
  • id_rsa.pub: 公钥
  • known_hosts: 记录已确认的SSH主机的公钥

后续

由于ssh-key文件名太乱了,我打算删除.ssh文件夹下所有得文件,重新进行生成。一通操作成功得把我的github免密连接搞没了。后续进行了一系列操作,在这里记录一下。

ssh免密登录github

1.生成ssh密钥

ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa

会生成id_rsa密钥和id_rsa.pub公钥

2.启动SSH代理并添加密钥

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

3.在github上添加公钥

将id_rsa.pub文件中的内容复制到github的ssh密钥中。

4.测试

ssh -T git@github.com

如果出现Hi username! You've successfully authenticated, but GitHub does not provide shell access.则表示成功。

出现的一些问题

我选择生成密钥的名字为id_rsagithub,然后在配置文件config中添加

Host github
  HostName github.com
  User git
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_rsagithub

然后执行ssh -T git@github时出现Permission denied (publickey).
使用命令ssh -vT git@github.com生成调试信息得到如下结果(我加入了一些个人注释)。

$ ssh -vT git@github.com
OpenSSH_9.4p1, OpenSSL 3.1.2 1 Aug 2023  #开源远程连接工具openssh和开源密码工具openssl
debug1: Reading configuration data /c/Users/63155/.ssh/config #读取配置文件
debug1: Reading configuration data /etc/ssh/ssh_config #这里应该是我的wsl上的文件,没有内容
debug1: Connecting to github.com [20.205.243.166] port 22.
debug1: Connection established.
#这里读取id_rsa等一系列文件全都失败
debug1: identity file /c/Users/63155/.ssh/id_rsa type -1 
debug1: identity file /c/Users/63155/.ssh/id_rsa-cert type -1
debug1: identity file /c/Users/63155/.ssh/id_ecdsa type -1
debug1: identity file /c/Users/63155/.ssh/id_ecdsa-cert type -1
debug1: identity file /c/Users/63155/.ssh/id_ecdsa_sk type -1
debug1: identity file /c/Users/63155/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /c/Users/63155/.ssh/id_ed25519 type -1
debug1: identity file /c/Users/63155/.ssh/id_ed25519-cert type -1
debug1: identity file /c/Users/63155/.ssh/id_ed25519_sk type -1
debug1: identity file /c/Users/63155/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /c/Users/63155/.ssh/id_xmss type -1
debug1: identity file /c/Users/63155/.ssh/id_xmss-cert type -1
debug1: identity file /c/Users/63155/.ssh/id_dsa type -1
debug1: identity file /c/Users/63155/.ssh/id_dsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.4
debug1: Remote protocol version 2.0, remote software version babeld-a406152d #SSH远程协议
debug1: compat_banner: no match: babeld-a406152d
debug1: Authenticating to github.com:22 as 'git'
#这里开始读取known_hosts文件,是以前连接过的信息,也失败
debug1: load_hostkeys: fopen /c/Users/63155/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
debug1: load_hostkeys: fopen /c/Users/63155/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'github.com' is known and matches the ED25519 host key.
debug1: Found key in /c/Users/63155/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: get_agent_identities: bound agent to hostkey
debug1: get_agent_identities: agent returned 1 keys
debug1: Will attempt key: sdj RSA SHA256:2sOOA/s3qLdESVFlsKSyYJQJEZZtaknttbvRHEsyMb4 agent
debug1: Will attempt key: /c/Users/63155/.ssh/id_rsa
debug1: Will attempt key: /c/Users/63155/.ssh/id_ecdsa
debug1: Will attempt key: /c/Users/63155/.ssh/id_ecdsa_sk
debug1: Will attempt key: /c/Users/63155/.ssh/id_ed25519
debug1: Will attempt key: /c/Users/63155/.ssh/id_ed25519_sk
debug1: Will attempt key: /c/Users/63155/.ssh/id_xmss
debug1: Will attempt key: /c/Users/63155/.ssh/id_dsa
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: sdj RSA SHA256:2sOOA/s3qLdESVFlsKSyYJQJEZZtaknttbvRHEsyMb4 agent
debug1: Authentications that can continue: publickey
debug1: Trying private key: /c/Users/63155/.ssh/id_rsa
debug1: Trying private key: /c/Users/63155/.ssh/id_ecdsa
debug1: Trying private key: /c/Users/63155/.ssh/id_ecdsa_sk
debug1: Trying private key: /c/Users/63155/.ssh/id_ed25519
debug1: Trying private key: /c/Users/63155/.ssh/id_ed25519_sk
debug1: Trying private key: /c/Users/63155/.ssh/id_xmss
debug1: Trying private key: /c/Users/63155/.ssh/id_dsa
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

这些调试信息并没有看懂,但是意思就是没有根据我的配置文件来,自顾自的读取一些默认的文件,并没有读取我改名后的文件,所以最后我改回rsa后就正确了。

目前考虑是由于/etc/ssh/ssh_config覆盖了我的/c/Users/63155/.ssh/config,导致其寻找的都是默认位置。


文章作者: sdj
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 sdj !
  目录