Set Up SSH Key-Based (Passwordless) Login on Linux Server
Typically, SSH keys is more secure and convenient than traditional password authentication. In this article, I’m going to show how to setup SSH key-based login on any linux server such as CentOS, Ubuntu. Let’s get started:
Table of Contents
Generate SSH Key
We want to login to our server from our PC without entering password. Let’s do it. From your PC, run this commad:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
It will ask you to provide a file name.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
We want to store in default file. Just hit ENTER button.
Then it’ll ask you to provide a passphrase. You can skip it. Just press ENTER button.
Enter passphrase (empty for no passphrase):
To verify your new SSH key pair is generated, run this command:
Output will look like:
We’ve successfully generated SSH key in our PC.
Copy Public Key to Server
We need to add our newly generated key to our server. We can copy with
ssh-copy-id utility. Run this command to copy:
ssh-copy-id [email protected]_ip_address
ssh-copy-id is not available on your computer, you can copy SSH key to server using this command:
cat ~/.ssh/id_rsa.pub | ssh [email protected]_ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Then we need to enter the remote user password to login to server.
[email protected]_ip_address's password:
After authenticating, the local file (
~/.ssh/id_rsa.pub) will be copied to the remote user’s
Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]_ip_address'" and check to make sure that only the key(s) you wanted were added.
Login Using Key
Now just run this command to login to server:
That’s all. Thanks for reading. ?
ssh [email protected]_ip_address
Preview may take a few seconds to load.
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Bold **double asterisks**
Three back ticks and then enter your code blocks here.
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
> type a greater than sign and start typing your quote.
You can add links by adding text inside of  and the link inside of (), like so:
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of , and the image URL inside of (), like so:
To add a divider you can add three dashes or three asterisks:
--- or ***