How to Set Up SSH Keys for GitHub, GitLab, and VPS Servers Without Password Login
Passwords are like socks in a dryer. They vanish, get stolen, or end up where they should not be. SSH keys are different. They are safer, faster, and much cooler. Once you set them up, you can push code to GitHub, work with GitLab, and log in to your VPS without typing a password every time.
TLDR: Create an SSH key on your computer with ssh-keygen. Add the public key to GitHub, GitLab, or your VPS server. Test the connection. Then, for VPS servers, turn off password login only after you confirm the key works.
What Is an SSH Key?
An SSH key is a pair of digital keys. One key is private. One key is public.
The private key stays on your computer. Guard it like a dragon guards gold. Do not email it. Do not paste it into chat. Do not upload it to random websites.
The public key can be shared. You add it to GitHub, GitLab, or a VPS server. When you connect, the server checks if your private key matches the public key. If it does, the door opens.
No password dance. No copy and paste from a password manager. Just secure magic.
Why Use SSH Keys?
SSH keys make life easier. They also make your accounts safer.
- No more password typing. Your terminal signs in for you.
- Better security. SSH keys are much harder to guess than passwords.
- Great for Git. Push and pull code without login prompts.
- Great for servers. Log in to your VPS fast and safely.
- Works with automation. Deploy scripts love SSH keys.
Think of SSH keys as a VIP pass. Your computer shows the pass. The server says, “Nice. Come in.”
Step 1: Check If You Already Have an SSH Key
Open your terminal. On macOS and Linux, use the Terminal app. On Windows, use PowerShell, Windows Terminal, or Git Bash.
Run this command:
ls ~/.ssh
You may see files like these:
id_ed25519id_ed25519.pubid_rsaid_rsa.pub
The .pub file is your public key. The file without .pub is your private key.
If you already have a key, you can use it. But if you are not sure, create a fresh one. Fresh keys are clean and easy to name.
Step 2: Create a New SSH Key
Use ed25519 if possible. It is modern, secure, and nice. Like a tiny security ninja.
Run this command. Replace the email with your email:
ssh-keygen -t ed25519 -C "you@example.com"
If your system does not support ed25519, use RSA:
ssh-keygen -t rsa -b 4096 -C "you@example.com"
The terminal will ask where to save the key. Press Enter to use the default location.
Then it asks for a passphrase. You can press Enter for no passphrase. But a passphrase is safer. It protects your private key if someone steals your laptop.
A good passphrase is long and easy to remember. For example, something like:
purple cats drink tea at midnight
Do not use that exact one. The cats are already famous now.
Step 3: Start the SSH Agent
The SSH agent remembers your private key while you work. This means you do not need to type the passphrase again and again.
On macOS or Linux, run:
eval "$(ssh-agent -s)"
Then add your key:
ssh-add ~/.ssh/id_ed25519
If you used a different name, change the file path.
On Windows with Git Bash, you can usually run the same commands. If you use Windows OpenSSH, run PowerShell as your normal user and try:
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519
If Windows complains, do not panic. Windows likes drama. Make sure the OpenSSH Authentication Agent service is installed and enabled.
Step 4: Copy Your Public Key
Now you need the public key. Remember, this is the shareable one.
To print it in your terminal, run:
cat ~/.ssh/id_ed25519.pub
Copy the full line. It starts with ssh-ed25519 or ssh-rsa. It ends with your email or comment.
Do not copy your private key. The private key does not end in .pub. If you share the private key, the raccoons win.
Set Up SSH Keys for GitHub
GitHub uses SSH keys to let you clone, pull, and push code without typing your username and password.
- Go to GitHub.
- Click your profile picture.
- Open Settings.
- Click SSH and GPG keys.
- Click New SSH key.
- Add a clear title, like Work Laptop or Home PC.
- Paste your public key into the key box.
- Click Add SSH key.
Now test it:
ssh -T git@github.com
You may see a warning about authenticity. Type:
yes
If it works, GitHub says something like:
Hi username! You've successfully authenticated.
Nice. You are now speaking SSH with GitHub.
Use SSH URLs with GitHub
SSH keys only help if your Git remote uses an SSH URL.
An HTTPS URL looks like this:
https://github.com/username/project.git
An SSH URL looks like this:
git@github.com:username/project.git
Check your current remote:
git remote -v
Change it to SSH:
git remote set-url origin git@github.com:username/project.git
Now git pull and git push should work without password login.
Set Up SSH Keys for GitLab
GitLab works in a very similar way. Same idea. Different buttons. Classic internet.
- Go to GitLab.
- Click your avatar.
- Open Preferences or Edit profile.
- Click SSH Keys.
- Paste your public key.
- Add a title, like Main Laptop.
- Set an expiration date if you want.
- Click Add key.
Test the GitLab SSH connection:
ssh -T git@gitlab.com
If GitLab greets you, you are in. The message may say that shell access is not provided. That is normal. GitLab is just saying, “You can use Git, not my whole house.”
For GitLab remotes, use this format:
git@gitlab.com:username/project.git
Change an existing remote like this:
git remote set-url origin git@gitlab.com:username/project.git
Set Up SSH Keys for a VPS Server
A VPS is a virtual private server. It is your tiny computer in the cloud. You can host websites, apps, bots, databases, and other fun chaos.
You often log in like this:
ssh root@your_server_ip
Or like this:
ssh username@your_server_ip
At first, the server may ask for a password. We want to replace that with SSH key login.
Step 1: Copy Your Public Key to the VPS
The easiest way is ssh-copy-id. It copies your public key to the server and puts it in the right place.
Run:
ssh-copy-id username@your_server_ip
Use your real username and server IP.
If you log in as root, use:
ssh-copy-id root@your_server_ip
Enter your password one last time. After that, your key should be installed.
Now test it:
ssh username@your_server_ip
If you get in without the account password, great. If you used a key passphrase, you may still need to unlock the key locally. That is normal.
If ssh-copy-id Is Not Available
Some systems do not have ssh-copy-id. No problem. We can do it by hand.
First, print your public key:
cat ~/.ssh/id_ed25519.pub
Copy the full line.
Log in to the server with your password:
ssh username@your_server_ip
Then run these commands on the server:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
Paste your public key into the file. Save and exit.
Then set the right permissions:
chmod 600 ~/.ssh/authorized_keys
Permissions matter. SSH is picky. If the permissions are too open, SSH may refuse the key. It is like a bouncer who hates messy folders.
Step 2: Test Key Login Before Changing Anything
This part is important. Very important. Put a tiny alarm bell in your brain.
Open a new terminal window. Do not close your current server session yet.
Try to log in:
ssh username@your_server_ip
If it works, celebrate gently. Maybe nod like a wise wizard.
If it fails, fix it before going further. Do not disable password login yet. That is how people lock themselves out and start yelling at clouds.
Step 3: Disable Password Login on the VPS
Once key login works, you can disable password logins. This makes your VPS much safer.
Open the SSH server config file:
sudo nano /etc/ssh/sshd_config
Find these lines. They may be commented with a #.
PasswordAuthentication no
PubkeyAuthentication yes
Also consider this line for root login:
PermitRootLogin prohibit-password
This means root cannot log in with a password. Root may still log in with a key. If you want to block root login completely, use:
PermitRootLogin no
That is safer, but only do it if you have another sudo user ready.
Save the file. Then test the SSH config before restarting:
sudo sshd -t
If there is no output, that is good. Silence is success. Weird, but true.
Restart SSH:
sudo systemctl restart ssh
On some systems, the service is called sshd:
sudo systemctl restart sshd
Now open another new terminal and test again:
ssh username@your_server_ip
If you can log in, you did it. Password login is gone. Your server door now has a much better lock.
Using Different Keys for Different Places
You can use one SSH key everywhere. But for better organization, you may want separate keys.
For example:
~/.ssh/id_ed25519_github~/.ssh/id_ed25519_gitlab~/.ssh/id_ed25519_vps
Create a named key like this:
ssh-keygen -t ed25519 -C "github key" -f ~/.ssh/id_ed25519_github
Then add it to the agent:
ssh-add ~/.ssh/id_ed25519_github
To make SSH use the right key for the right service, edit your SSH config:
nano ~/.ssh/config
Add something like this:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab
Host myvps
HostName your_server_ip
User username
IdentityFile ~/.ssh/id_ed25519_vps
Then you can log in to your VPS with a cute shortcut:
ssh myvps
That feels good. Like naming your spaceship.
Common Problems and Easy Fixes
- Permission denied: Make sure the public key is added to the correct account.
- Wrong remote URL: Use SSH URLs, not HTTPS URLs.
- Bad file permissions: Use
chmod 700 ~/.sshandchmod 600 ~/.ssh/authorized_keys. - SSH agent not running: Start it and add your key with
ssh-add. - Using the wrong key: Set the right
IdentityFilein~/.ssh/config.
Final Safety Tips
Never share your private key. Back it up only in a secure place. Remove old keys from GitHub, GitLab, and servers when you stop using a computer. If a laptop is lost, delete its public key from every service right away.
SSH keys may look scary at first. But they are simple once you meet them. You create a key pair. You share the public key. You protect the private key. Then your Git pushes and server logins become smooth, safe, and wonderfully password-free.
Where Should We Send
Your WordPress Deals & Discounts?
Subscribe to Our Newsletter and Get Your First Deal Delivered Instant to Your Email Inbox.


