Setup Git & SSH Key for GitHub on Ubuntu

Setup Git & SSH Key for GitHub on Ubuntu

Bitnesia Development Mar 25, 2026 327 ID

Git is one of the most popular version control systems used by developers to manage source code. In modern workflows, Git is typically integrated with repository hosting services such as GitHub. For better security and convenience, GitHub requires the use of SSH keys for authentication, so you don’t need to enter your username and password every time you perform a push or pull.

1. Install Git on Ubuntu

First, update the system and install Git:

sudo apt update
sudo apt install git -y

Check the Git version:

git --version

Expected output example:

git version 2.43.0

2. Configure Git Identity

After installing Git, set your Git account identity globally on the system:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Check the configuration:

git config --list

3. Generate a New SSH Key

Create an SSH key using the ed25519 algorithm (more modern and secure):

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

When prompted for the storage location, press Enter to save it in the default path ~/.ssh/id_ed25519.

If you want to use a different name (e.g., for GitHub):

ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/github_id_ed25519

Start the ssh-agent:

eval "$(ssh-agent -s)"

Add the key to the agent:

ssh-add ~/.ssh/github_id_ed25519

4. Add the SSH Key to GitHub

Copy the public key content:

cat ~/.ssh/github_id_ed25519.pub

Copy the output, then open GitHub:

  1. Go to Settings → SSH and GPG keys → New SSH key
  2. Paste the key and give it a name (e.g., Ubuntu Laptop)
  3. Click Add SSH key

5. Configure SSH Client

For convenience, you can create a configuration file:

nano ~/.ssh/config

Fill it with the following configuration:

# GitHub Account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_id_ed25519
IdentitiesOnly yes

Save and exit (CTRL+O, CTRL+X).

6. Test SSH Connection

Test the connection to GitHub:

ssh -T [email protected]

If successful, you will see a message like:

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

7. Clone Repository Using SSH

You can now clone GitHub repositories using SSH without needing a password:

git clone [email protected]:username/repository-name.git

8. Multi-Account GitHub Configuration with SSH Config

Sometimes you may have more than one GitHub account:

  • A personal account for open-source projects
  • A work account for professional use

To avoid confusion, you can create separate SSH keys and configure the ~/.ssh/config file so Git knows which key to use.

1. Generate SSH Keys for Each Account

Personal Account:

ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/github_personal

Work Account:

ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/github_work

Add both keys to the ssh-agent:

ssh-add ~/.ssh/github_personal
ssh-add ~/.ssh/github_work

2. Add SSH Keys to GitHub

  • For the personal account → add ~/.ssh/github_personal.pub to your personal GitHub account
  • For the work account → add ~/.ssh/github_work.pub to your work GitHub account

3. Configure SSH Client

Open the configuration file:

nano ~/.ssh/config

Fill it with:

# Personal GitHub Account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/github_personal
IdentitiesOnly yes
# Work GitHub Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/github_work
IdentitiesOnly yes

With this setup, you can use aliases like github-personal or github-work when cloning repositories.

4. Clone Repositories Based on Account

Clone a personal repository:

git clone git@github-personal:username/personal-repo.git

Clone a work repository:

git clone git@github-work:org/work-repo.git

5. Set Git User Configuration per Repository

To ensure commits use the correct identity, you can configure Git user settings per repository:

Enter the work repository directory and run:

git config user.name "Work Name"
git config user.email "[email protected]"

For a personal repository:

git config user.name "Personal Name"
git config user.email "[email protected]"

With this additional configuration, you can:

  • Use multiple GitHub accounts on a single Ubuntu laptop/PC
  • Assign different SSH keys per account
  • Avoid identity conflicts when making commits

This ensures a clean and organized workflow for both personal and professional projects.

Help me create more! Your donations go directly toward better equipment and research for future tutorials.

Support future guides

Related Posts