Black Android Smartphone on Top of White Book

Server Access: Set Up SSH Key Authentication

How to access server via ssh and setup ssh key for passwordless authentication

Without setting up SSH Key It will ask Password each time we try to get access

Enhance security and convenience by establishing passwordless SSH logins using SSH key authentication. This guide explains the process of generating a key pair on your local machine, copying the public key to your server, and configuring your server to accept it.

Prerequisites:

  • A local machine (Windows, macOS, or Linux) with an SSH client installed.
  • An SSH server running on your remote server/VPS.
  • Access to both your local machine and server (SSH login credentials for the server).
  • To Get Access via SSH
Syntax:- ssh -p PORT USERNAME@HOSTIP
Example:- ssh -p 21350 root@216.32.44.12

Note:- Run Below Commands on Your Local Machine, Not on Your VPS or Remote Server

  • Generate SSH Key
ssh-keygen -f C:\Users\R/.ssh/my_key -t rsa -b 4096
  • Copy SSH Public Key to Remote Server (Not for Windows)
Syntax:- ssh-copy-id -p PORT -i ~/.ssh/keyname.pub USERNAME@HOSTIP
Example:- ssh-copy-id -p 21350 -i ~/.ssh/id_rsa.pub u27653@216.32.44.12
  • Copy Public Key to Remote Server (for Windows)
1. Make sure you have .ssh folder in remote server
Syntax:- ssh -P PORT USERNAME@HOSTIP "mkdir /home/USERNAME/.ssh"
Example:- ssh -P 21350 u27653@216.32.44.12 "mkdir /home/u27653/.ssh"

2. Copy Public Key
Syntax:- scp -P PORT SSH_PUBLIC_KEY_PATH USERNAME@HOSTIP:/home/USERNAME/.ssh/authorized_keys
Example:- scp -P 21350 C:\Users\R/.ssh\id_rsa.pub u27653@216.32.44.12:/home/u27653/.ssh/authorized_keys