Skip to content
Logo SR
  • Home
  • Português
  • English
  • About
Logo SR
  • Home
  • Português
  • English
  • About

Tutorial for SSH Public Key Authentication

Network, Servers

In this tutorial, we will teach you how to create and configure access to an SSH server using a public key.

Português

For this tutorial, we will use two virtual machines that are within the same network.

However, the scenario used can be replicated for machines that are on other networks and even on the Internet.

Table Of Contents
  1. Scenario Used
  2. Creating Public Key for the SSH Client
    • Sending the Client's Public Key to the SSH Server
    • Testing the Connection to the SSH Server
  3. Verifying the Client's Key on the SSH Server

Scenario Used

The figure below shows our lab scenario. In this case, we have:

Client Machine: Will use the SSH client to access the server.

Server Machine: Will listen on port 22 using the OpenSSH server.

Creating Public Key for the SSH Client

Now let’s log into the client machine and open a terminal. Remember, we’re using a Linux machine with Ubuntu.

In the terminal, we will use the command below to generate the public and private keys for the SSH client.

ssh-keygen

On the first question about naming the file, press “ENTER“.

On the second and third questions about inserting a “passphrase“, press “ENTER” as well.

Note. We are assuming that we will keep the private key secure, so we will not use a “passphrase“. In your case, you should check if there’s a need to use a “passphrase“.

Next, we can verify if the SSH public and private keys were generated. To do this, type the command below to list your key directory.

ls ~/.ssh/

Now let’s enter the directory where the SSH public and private keys are located. To do this, we’ll use the command below.

cd ~/.ssh/

Now type the command below to list the directory files.

ls

Sending the Client’s Public Key to the SSH Server

In this step, we will send the client’s public key to the SSH server. The client needs to send the public key to the server, which stores it in the user’s “~/.ssh/authorized_keys“ file for login purposes.

ssh-copy-id -i id_YourPubFile YourUser@YourIP
  • 1 = the ssh-copy-id command, which will send the client’s public key.
  • 2 = the “i” option to specify the client’s public key file.
  • 3 = The client’s public key file.
  • 4 = the client’s user @ the SSH server’s IP address. (In our case, the user is “redes”).

After typing the command, a prompt will ask if you want to continue. To proceed, type “yes” and press “ENTER“.

Next, you will be asked for the “password” to connect to the SSH server. Enter the connection password for your client to the SSH server.

Testing the Connection to the SSH Server

Finally, we can log into the SSH server without a password. To do this, type the command below. Remember to replace “redes” with your username and the IP with your SSH server’s IP address.

ssh [email protected]

Verifying the Client’s Key on the SSH Server

If desired, you can log into the server and verify the key that was created for the SSH client. To do this, type the command below.

cat ~/.ssh/authorized_keys

Congratulations, you have successfully created a public key to access the SSH server without needing to enter a password.

See more:

How to use Ngrok

Install ubuntu 24 on virtualbox

How to X11 Forwarding using SSH

Install SSH server Windows 11

https://www.openssh.com

Juliana Mascarenhas

Data Scientist and Master in Computer Modeling by LNCC.
Computer Engineer

Linkedin
← Previous Post
Next Post →

Related Posts

OpenWRT in VirtualBox

Network, OpenWRT_en, Raspberry PI en, VirtualBox_en, virtualization

pfBlockerNG Customize Blocklist

Network, pfBlockerNG_en, PfSense_en

Android with VirtualBox

Network, VirtualBox_en, virtualization

Virtualizing the Raspberry Pi – Raspbian

Network, Raspberry PI en, VirtualBox_en, virtualization

Squid Proxy for Linux Installation and Configuration

Network, Proxy_en, Servers

PfBlockerNG: Exception List for Clients

Network, pfBlockerNG_en, PfSense_en
  • Português
  • English
  • Blockchain (3)
  • Data (9)
    • Data Science_en (3)
    • Database (6)
  • Network (65)
    • Cloud_en (2)
    • OpenWRT_en (3)
    • PacketTracer_en (3)
    • Protocol (13)
    • Proxy_en (6)
    • Servers (9)
  • PfSense_en (9)
    • pfBlockerNG_en (3)
  • programming (18)
    • Java_en (3)
    • Python_eng (13)
  • Raspberry PI en (7)
  • security (19)
    • Suricata_en (3)
  • Sem categoria (1)
  • virtualization (19)
    • Docker_en (6)
    • VirtualBox_en (13)
  • VPN_en (8)
  • Zabbix_en (5)
  • Português

Latest Articles

  • How to Use Snap Behind a Proxy on Linux (Step-by-Step Guide) 
  • How to Create a Network with a Switch in Packet Tracer – Step-by-Step Guide for Beginners
  • Why use Kali Linux inside VirtualBox?
  • How to install pfBlocker on pfSense: step by step guide
  • Packet Tracer for Dummies: Setting Up Your First Network with 2 PCs (Quick Start Guide)
  • Learn how to use the curl command: tutorial with practical examples
  • How to Install Kali Linux on VirtualBox: Step-by-Step Guide for Beginners
  • Python Package Managers: Pip and Conda – A Complete Beginner’s Guide
  • What is CGNAT ?
  • Tutorial: How to use WHOIS and RDAP
  • How to Set Up a Postfix and Dovecot Email Server on Linux: A Step-by-Step Guide
  • Tutorial how to Install and configure VNC on Ubuntu
  • Build Your Own DNS Server: A Step-by-Step Guide using BIND9
  • Tutorial for SSH Public Key Authentication
  • Socket UDP Python Chat
  • Socket TCP Python make a chat
  • apt get behind proxy
  • Best IDE for Python?
  • Python get metadata from images and pdfs
  • How to use Ngrok
  • Português
  • English
  • Cookie Policy / Política de Cookies
  • Privacy Policy
  • About
We use cookies on our website remembering your preferences and visits. By clicking “Accept All”, you consent to the use of ALL the cookies. Visit " Settings" to provide a controlled consent./ Usamos cookies no site lembrando suas preferências e visitas. Clicando em “Aceitar todos”, você concorda com o uso de TODOS os cookies. visite "Configurações cookies" para um consentimento controlado.
Settings/ConfiguraçõesAccept All / Aceitar tudo
Manage consent / Gerenciar consentimento

Privacy Overview / Visão geral da privacidade

This website, uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience. **/** Este site usa cookies para melhorar a sua experiência enquanto navega pelo site. Destes, os cookies que são categorizados como necessários são armazenados no seu navegador, pois são essenciais para o funcionamento das funcionalidades básicas do site. Também usamos cookies de terceiros que nos ajudam a analisar e entender como você usa este site. Esses cookies serão armazenados em seu navegador apenas com o seu consentimento. Você também tem a opção de cancelar esses cookies. Porém, a desativação de alguns desses cookies pode afetar sua experiência de navegação.
Necessary_en
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-advertisement1 yearSet by the GDPR Cookie Consent plugin, this cookie is used to record the user consent for the cookies in the "Advertisement" category
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". O cookie é definido pelo consentimento do cookie GDPR para registrar o consentimento do usuário para os cookies na categoria "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". Este cookie é definido pelo plug-in GDPR Cookie Consent. Os cookies são usados para armazenar o consentimento do usuário para os cookies na categoria "Necessary",
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other".
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. O cookie é definido pelo plug-in GDPR Cookie Consent e é usado para armazenar se o usuário consentiu ou não com o uso de cookies. Ele não armazena nenhum dado pessoal.
Functional_en
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
CookieDurationDescription
pll_language1 yearThe pll _language cookie is used by Polylang to remember the language selected by the user when returning to the website, and also to get the language information when not available in another way.
Performance_en
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
CookieDurationDescription
_tccl_visit30 minutesThis cookie is set by the web hosting provider GoDaddy. This is a persistent cookie used for monitoring the website usage performance.
_tccl_visitor1 yearThis cookie is set by the web hosting provider GoDaddy. This is a persistent cookie used for monitoring the website usage performance.
Analytics_en
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
CookieDurationDescription
__gads1 year 24 daysThe __gads cookie, set by Google, is stored under DoubleClick domain and tracks the number of times users see an advert, measures the success of the campaign and calculates its revenue. This cookie can only be read from the domain they are set on and will not track any data while browsing through other sites.
_ga2 yearsThe _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors.
_gat_gtag_UA_199766752_11 minuteSet by Google to distinguish users.
_gid1 dayInstalled by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously.
Advertisement_en
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
CookieDurationDescription
test_cookie15 minutesThe test_cookie is set by doubleclick.net and is used to determine if the user's browser supports cookies.
Others_en
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
CookieDurationDescription
FCCDCF12 hoursNo description available.
GoogleAdServingTestsessionNo description
SAVE & ACCEPT
Powered by CookieYes Logo