Skip to content

Instantly share code, notes, and snippets.

@imrankabir02
Last active April 15, 2025 08:23
Show Gist options
  • Select an option

  • Save imrankabir02/d26589de93c91f00fa0c22db4ddda026 to your computer and use it in GitHub Desktop.

Select an option

Save imrankabir02/d26589de93c91f00fa0c22db4ddda026 to your computer and use it in GitHub Desktop.
SSH Key Setup

SSH Key Setup

🧠 What You're Doing:

You're telling your Nginx server:

β€œHey, only let me in if I prove I have the private key that matches this public key.”

This is called SSH key-based authentication β€” it's more secure than using passwords.


πŸ”‘ The Key Pair:

When you generate an SSH key, you get two files:

  • id_rsa β†’ Private key (keep this secret, never share)
  • id_rsa.pub β†’ Public key (can be shared, it’s safe)

They work like a lock and key:

  • The public key goes on the server (like locking the door)
  • The private key stays with you (like the key that unlocks the door)

πŸš€ What Each Step Does:

πŸ›  1. Generate the Key

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • Creates a strong RSA key pair (public + private)
  • Saves them in ~/.ssh/ (unless you specify otherwise)

πŸ” 2. Send Public Key to Server

ssh-copy-id -i ~/.ssh/id_rsa.pub -p 2222 root@192.168.0.100

OR do it manually by copying the content of id_rsa.pub and pasting it in:

~/.ssh/authorized_keys

on the server.

This tells the server:

"Here’s my public key. If someone tries to SSH in, check if their private key matches this one."


πŸ” 3. Set File Permissions on the Server

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

These commands make sure:

  • Your .ssh folder is private
  • The authorized_keys file is readable only by the owner

Linux is picky about SSH security β€” wrong permissions = login won’t work.


πŸšͺ 4. Log in Without a Password

ssh -p 2222 root@192.168.0.100

Now the server says:

"Okay, I’ve got your public key. Can you prove you own the private key?"

Your SSH client proves it silently using cryptography, and if it checks out β€” you're in βœ…

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