Skip to content

Instantly share code, notes, and snippets.

@realasib
Last active July 31, 2024 15:47
Show Gist options
  • Select an option

  • Save realasib/cae3d4229589f81cc03dc80f55f80958 to your computer and use it in GitHub Desktop.

Select an option

Save realasib/cae3d4229589f81cc03dc80f55f80958 to your computer and use it in GitHub Desktop.
How to set up SSH from your local Linux Mint machine to GitHub

To set up SSH from your local Linux Mint machine to GitHub, follow these steps:

  1. Check for existing SSH keys:

    Open a terminal and run:

    ls -al ~/.ssh

    This will list the files in the .ssh directory to see if you already have an SSH key. Look for files like id_rsa and id_rsa.pub.

  2. Generate a new SSH key:

    If you don't have an existing SSH key, generate a new one by running:

    ssh-keygen -t ed25519 -C "[email protected]"

    If you're using an older system that doesn't support the ed25519 algorithm, use:

    ssh-keygen -t rsa -b 4096 -C "[email protected]"

    When prompted to "Enter a file in which to save the key," press Enter to accept the default file location. And when prompted to set a paraphrase, you can set a paraphrase for security reason. You have to remember this

  3. Add your SSH key to the ssh-agent:

    Start the ssh-agent in the background:

    eval "$(ssh-agent -s)"

    Add your SSH private key to the ssh-agent:

    ssh-add ~/.ssh/id_ed25519

    or for RSA:

    ssh-add ~/.ssh/id_rsa
  4. Add the SSH key to your GitHub account:

    • Copy the SSH key to your clipboard:

      xclip -selection clipboard < ~/.ssh/id_ed25519.pub

      or for RSA:

      xclip -selection clipboard < ~/.ssh/id_rsa.pub
    • Install xclip if it's not installed

      sudo apt install xclip
    • Go to GitHub and log in.

    • In the upper-right corner of any page, click your profile photo, then click Settings.

    • In the user settings sidebar, click SSH and GPG keys.

    • Click New SSH key or Add SSH key.

    • In the "Title" field, add a descriptive label for the new key.

    • Paste your key into the "Key" field.

    • Click Add SSH key.

    • Confirm your GitHub password, if prompted.

  5. Test your SSH connection:

    Run:

    You may see a message like this:

    The authenticity of host 'github.com (IP ADDRESS)' can't be established.
    RSA key fingerprint is SHA256:.......
    Are you sure you want to continue connecting (yes/no)?
    

    Type yes to continue.

    If your setup was successful, you should see a message like:

    Hi username! You've successfully authenticated, but GitHub does not provide shell access.
    

Now, your local machine is set up to use SSH with GitHub!

Sometimes or should I say most of the time if you have cloned a repository using https such as git clone https://github.com/username/repo then git would still prompt you to Username for 'https://github.com': like this.

If Git is still prompting for a username and password, it's likely that your remote URL is using HTTPS instead of SSH. You need to change the remote URL to use the SSH protocol.

Here's how you can do that:

  1. Check the current remote URL:

    Open a terminal and navigate to your Git repository, then run:

    git remote -v

    You'll see something like this:

    origin  https://github.com/username/repo.git (fetch)
    origin  https://github.com/username/repo.git (push)
    
  2. Change the remote URL to SSH:

    Run the following command to change the remote URL to use SSH:

    git remote set-url origin [email protected]:username/repo.git

    Replace username with your GitHub username and repo with your repository name.

  3. Verify the change:

    Run the following command again to verify that the remote URL has been updated:

    git remote -v

    You should now see:

    origin  [email protected]:username/repo.git (fetch)
    origin  [email protected]:username/repo.git (push)
    
  4. Push your changes:

    Now, try pushing your changes again:

    git push -u origin main

    You should no longer be prompted for a username and password.

This should resolve the issue and allow you to push changes using SSH without being prompted for your GitHub credentials.

@realasib
Copy link
Author

See the repo for details and bash script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment