Cloning a Git Repository over SSH Without a Password on Ubuntu

When I tried to clone a Git repository from my Ubuntu laptop over SSH, the server asked for the git user’s password. I already had an Ed25519 public key on the laptop. Here is how to authorize that key on the server so Git can use it instead of asking for the remote account password.

git clone git@git.developernote.com:~/repositories/tradeclient.git
Cloning into 'tradeclient'...
git@git.developernote.com's password:

Check the existing SSH key

On the laptop, I checked that the public key existed:

ls ~/.ssh/id_ed25519.pub
/home/dmitriano/.ssh/id_ed25519.pub

The matching private key is ~/.ssh/id_ed25519. Keep it on the laptop; only the .pub file belongs on the server. If both files already exist, skip key generation.

If the SSH key does not exist

If the check reports No such file or directory, first check for an existing private key:

ls -l ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub

If neither file exists, generate a new Ed25519 key pair on the laptop:

ssh-keygen -t ed25519

At Enter file in which to save the key, press Enter to accept /home/dmitriano/.ssh/id_ed25519 (your username may differ). If asked to overwrite an existing key, answer n and inspect it before proceeding.

Enter and confirm a passphrase to protect the private key. You can use ssh-agent, described below, to avoid typing it for every connection. Pressing Enter twice leaves the key without a passphrase, so anyone who obtains the private key can use it.

This creates ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public). Continue with ssh-copy-id in the next section. See the OpenSSH ssh-keygen manual.

If the private key exists but only its public file is missing, recreate the public file from that private key instead of generating a replacement pair:

ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub

Install the public key on the Git server

Run this command on the laptop:

ssh-copy-id -i ~/.ssh/id_ed25519.pub git@git.developernote.com

Enter the remote git account’s password when prompted. ssh-copy-id appends the selected public key to ~/.ssh/authorized_keys in that account’s home directory on the server, creating the directory and file if necessary. See the Ubuntu ssh-copy-id manual.

This assumes a self-hosted SSH account that permits key installation through a remote shell. If the account uses a restricted Git shell, ask the server administrator to add the public key to that account’s authorized_keys file. Hosted Git services usually provide a web interface for adding SSH keys.

Clone the repository again

cd ~/dev/repos
git clone git@git.developernote.com:~/repositories/tradeclient.git

Once the server accepts the key, Git should no longer ask for the remote account password. In this URL, git is the remote SSH user, and ~/repositories/tradeclient.git is relative to that user’s home directory on the server.

If SSH asks for the key’s passphrase

A prompt such as Enter passphrase for key refers to the local private key, rather than the remote account. To load the key into an existing SSH agent, run:

ssh-add ~/.ssh/id_ed25519

If no agent is running, start one for the current shell first:

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

Enter the key’s passphrase when prompted. The agent can then use the loaded key for subsequent connections from this shell without repeated passphrase prompts while it remains loaded. See the OpenSSH ssh-add manual.

The first connection’s host fingerprint

The first connection may also ask whether to trust the server’s host key. Verify its fingerprint through a trusted source before accepting it. Saving that host key in known_hosts identifies the server; installing your public key in the server account’s authorized_keys authorizes your laptop to log in. These are separate steps.

Leave a Reply

Your email address will not be published. Required fields are marked *