0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Github】GithubにSSHでアクセスできなかったときの対処

Last updated at Posted at 2025-01-25

GithubにSSHでアクセスできなかったときの対処

はじめに

状況として、Github アカウントに公開鍵を登録して使っていたのに、アクセスできなかった。これまで使っていたLinux環境から別のLinux環境にうつり、秘密鍵を~/.ssh/ に同じように配置すれば動くのかな、、、と思っていたらダメだった。
SSH Agentというのに登録したら動くようになった、という認識。でメモします。

内容

最初にSSH-Agentを起動、鍵を登録して動かす

登録している鍵を調べてみようとすると、、、

$ ssh-add -l -E sha256
Could not open a connection to your authentication agent.

Authentication agentた無いと言われるので、起動します。

$ exec ssh-agent bash

そして、確認してみると、、、

$ ssh-add -l -E sha256
The agent has no identities.

登録がないそうです。なので登録します。
自分はkey20240101, 20240101.pub というペアを昔作り、20240101.pub をGithub の SSH keysに登録していました。

$ ls ~/.ssh/
config  key20240101  key20240101.pub  known_hosts 
$ ssh-add key20240101
Identity added: key20240101 (username@gmail.com)

これで確認ができました。

$ ssh-add -l -E sha256
256 SHA256:nZ8jOuDNsn5I/ABCDEFGHHxxxxxxxxx123040293444 username@gmail.com (ED25519)

Github の認証は通ったが、shell アクセスはできないと。これで通常のgit push とかできるようになりました。

$ ssh -vT git@github.com
OpenSSH_9.6p1 Ubuntu-3ubuntu13.5, OpenSSL 3.0.13 30 Jan 2024
debug1: Reading configuration data /home/xt/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to github.com [20.27.177.113] port 22.
debug1: Connection established.
debug1: identity file /home/xt/.ssh/id_rsa type -1
......
Hi xt! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2304, received 2636 bytes, in 0.3 seconds
Bytes per second: sent 6825.9, received 7809.5
debug1: Exit status 1

自動化

実は、前節で書いた方法は1回限りでした。毎回起動するたびに実行するのは面倒です。

SSH Agentが起動しているかの確認

環境変数を調べます。ssh-agent が起動していない場合、この二つは空です。

echo $SSH_AUTH_SOCK
echo $SSH_AGENT_PID

下記を実行するとSSH_AUTH_SOCK 環境変数が設定され、ssh-agent がバックグラウンドで実行されます。

eval "$(ssh-agent -s)"

これを .bashrc などに書いておくと、新しいターミナルごとにSSH-agent が起動されます。

キーリングへの登録

リスクはあるそうですが、下記で登録できるそうです。

$ ssh-add -k key20240101
Identity added: key20240101 (username@gmail.com)

参考情報

おわりに

とりあえず問題解決したのでよかった。
(2025/1/25)

追記:
確認、キーリングへの追加を書きました。(2025/2/11)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?