Operating Systems
Posted By Luca

How to connect to a Linux Server without password


A secure password is normally long and complex, so it is hard to remember and to type for authentication. Today I want show how to connect to a Linux server using a certificate without entering the password.

In this short tutorial I use my MacOS Sierra as client and a Ubuntu 10 as server. It will also work on other Linux / Unix versions.

Generate a key pair

The first step is to generate a key pair (private and public key) to use for authentication. The private key will remain on your computer in a safe place. The public key will be imported on the server. Open a terminal and type:

mbpl:~ lucacosta$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/lucacosta/.ssh/id_rsa): luca
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in luca.
Your public key has been saved in luca.pub.
The key fingerprint is:
SHA256:KoTUeuoGwMvHSEsEGy4KzBe09XKLQ+RVZiUbgVucS3s lucacosta@mbpl
The key's randomart image is:
+---[RSA 2048]----+
|+ .o o .+B+. |
|++ .* o.o=+ |
|*+.o.+ o+.o |
|==.o. +..o E |
|* B oo .S . |
|.= * .. |
| .o . . |
| .. . |
| .. |
+----[SHA256]-----+

Copy your public key to the server that you want connect

Replace <user>@<destination_server> with your username and destination server:

mbpl:~ lucacosta$ ssh-copy-id -f -i /Users/lucacosta/luca.pub <user>@<destination_server>
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/lucacosta/luca.pub"
xxxx@xxxxxxxxx's password: 

Number of key(s) added:        1

Move your private key in your .ssh folder

The last thing to do is to move your private key on your computer to a folder named .ssh and rename it with “id_rsa”. In my home directory I have two keys named luca and luca.pub (this is the name that you typed when creating the key pair). With the command “ls” you get the list of the files like this:

mbpl:~ lucacosta$ ls
Applications			Music
Applications (Parallels)	NoSync
Creative Cloud Files		OneDrive
Desktop				Pictures
Documents			Public
Downloads			Synology
Dropbox				luca
Library				luca.pub
Movies

Now copy your private key (in my case “luca”) to the “.ssh” folder with the new name “id_rsa”:

mbpl:~ lucacosta$ cp luca .ssh/id_rsa

Test the connection with the certificate

Now we can test the connection (if you choosed to set a password for your private key you need to enter it when prompt):

mbpl:~ lucacosta$ ssh xxxxx@xxxxxxxx
Enter passphrase for key '/Users/lucacosta/.ssh/id_rsa': 
Linux xxxxxxxxxxxxxxxxxxxxxx 2.6.32-042stab120.5 #1 SMP Tue Oct 25 22:31:12 MSK 2016 x86_64 GNU/Linux
Ubuntu 10.04.4 LTS

Welcome to Ubuntu!
 * Documentation:  https://help.ubuntu.com/
Last login: Thu Mar  2 20:36:54 2017 from xxxxxxxxxxxxx
xxxxxxx@xxxxxxxxxxx:~# 

My name is Luca Costa, an enthusiastic system engineer. In this blog you will find articles about technolgy and more. Feel free to contact me!

View Comments
There are currently no comments.