Master SSH Key Authentication: Generate, Deploy, and Use ssh-agent
This guide walks you through configuring SSH key‑based authentication, generating a public‑private key pair, copying the public key to a remote server, and using ssh‑agent to cache your passphrase for seamless logins.
You can configure SSH to use key‑based authentication instead of username/password by generating a public‑private key pair.
Environment: Two virtual machines – workstation (client) and servera (server). The goal is to SSH from workstation to servera using keys.
On workstation run ssh-keygen to create the key pair, optionally setting a passphrase.
[it@workstation ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/it/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/it/.ssh/id_rsa.
Your public key has been saved in /home/it/.ssh/id_rsa.pub.
...id_rsa is the private key and id_rsa.pub is the public key.
Upload the public key to the server with ssh-copy-id :
[it@workstation ~]$ ssh-copy-id -i .ssh/id_rsa it@servera
...
Number of key(s) added: 1Now connect with ssh ; you will be prompted for the private‑key passphrase.
[it@workstation ~]$ ssh it@servera
Last login: Thu Oct 29 14:41:13 2020 from 10.10.10.20To avoid entering the passphrase each time, start ssh-agent and add the key:
[it@workstation ~]$ eval $(ssh-agent)
Agent pid 3191
[it@workstation ~]$ ssh-add .ssh/id_rsa
Enter passphrase for .ssh/id_rsa:
Identity added: .ssh/id_rsa ([email protected])After adding, subsequent ssh commands reuse the cached credentials in the same shell. In a new shell you must add the key again. The same steps can be repeated for other servers such as serverb .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
