'SSH key not working for git push unless recently logged in
Background
I use SiteGround to host my websites. I have git repos set up for each of my sites on SiteGround. I use SSH keys for authentication. I just set siteground to be one of my remotes that I can push too.
The problem
The SSH keys appear to expire after a while, unless I login via SSH. I haven't been able to determine how long it takes before expiration. It seems to be a few weeks.
Procedure
- Wait a few weeks.
- Make some modifications to my website, commit and merge to
main
. $ git push sitegroundRemote HEAD:master
Then I get this error:
$ git push sitegroundRemote HEAD:master
<username>@subdomain.siteground.biz: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
$ ssh sitename
<-- this works. I'm now logged into a remote session$ exit
$ git push sitegroundRemote HEAD:master
And now it works. And I can continue to push for the next few days or weeks without having to ssh
in.
It seems like the key expires for git purposes, but not logging into the shell. And logging into the shell re-enables the key to be used for git purposes.
Additional Info
SSH config
The entry in my ~/.ssh/config file looks like:
Host sitename
HostName sitename.com
User <user-name>
Port <port-number>
IdentityFile ~/.ssh/sitename.com
git config
The entry in my git config file looks like:
[remote "sitegroundRemote"]
url = ssh://<user-name>@<subdomain>.siteground.biz:<port>/home/<user-name>/www/sitename.com/repos/repo.git
fetch = +refs/heads/*:refs/remotes/master/*
Generating the SSH key
To generate my SSH key, I did the following:
$ ssh-keygen
- Entered location and name and password (
/Users/username/.ssh/sitename.com
) $ ssh-add -K /Users/username/.ssh/sitename.com
$ pbcopy < ~/.ssh/sitename.com.pub
- Paste that into SiteGround's SSH key section.
All of this allows me to just $ ssh sitename
to gain access to my site via bash. So the key works. I didn't use any -t
options to put a time limit on it or anything.
The question
Any idea how I can continue to push to SiteGround without having to login every so often?
I should add that I did contact SiteGround's support, and the answer was basically "Sessions expire. There's nothing we can do about it." So I'm hoping someone here has some ideas.
Solution 1:[1]
So I'm hoping someone here has some ideas.
Beside a workaround, there might not be any obvious solution.
As a workaround, you could set a cron job which, every day, would do a ssh sitename ls
(single command and exit), in order to "refresh" the session.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | VonC |