How to Add SSH Public Key to Server (2024)

Public key authentication allows you to access a server via SSH without password. Here are two methods to copy the public ssh key to the server.

I believe you understand the basic SSH concept. Your Linux server has ssh enabled. You have generated ssh keys on your personal computer. Now you want to upload your public key to the authorized keys of the server so that you can access it without typing your account password all the time.

This quick tutorial shows you two methods to add a public SSH key to the server.

Requirements

Before you see that, let’s be clear about what you should already have:

  • Your destination server should have ssh enabled
  • You should have generated public and private ssh keys (just use the command ssh-keygen -t rsa)
  • You should have a user account and password on the server. Even root account will do.
  • You should know the IP address of the server

Now that you have made sure of the above four requirements, let’s see how to use public key authentication.

The authentication is per user base so the public key goes in the intended user’s home.

How to Add SSH Public Key to Server (1)

Method 1: Automatically copy the ssh key to server

The first method is where the end user copies its personal computer’s public key to the list of the authorized keys on the remote server.

Here, I assume that you were able to log in to the remote server using ssh user_name@ip_of_server. It asks for your account’s password and you enter the server.

If you add your public key to the server, you should be able to log in without typing the password all the time.

OpenSSH provides a handy tool call called ssh-copy-id for copying ssh public keys to remote systems. It even creates required directories and files.

As I mentioned earlier, you should know the username and password to the server you want to access via public key authentication.

ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR_USER_NAME@IP_ADDRESS_OF_THE_SERVER

When prompted, enter the password for your user account at the remote server. Your public key should be copied at the appropriate folder on the remote server automatically.

I have used ~/.ssh/id_rsa.pub because that is the default location for the public ssh key. If you have it at some other location, you should use that in the above command.

$100 Linode Credit | Linode

Deploy more with Linux virtual machines, global infrastructure, and simple pricing. No surprise bills, no lock-in, and the same price for every data center.

How to Add SSH Public Key to Server (2)Linode

How to Add SSH Public Key to Server (3)

Method 2: Manually copy the public ssh key to the server

The first method had the action on the user side. Let’s say that you are the sysadmin and your server doesn’t allow SSH login via password. The only way to access the server is using SSH public key authentication.

In such a case, you can ask the end user to provide her/his public key. Now what you can do is to create .ssh/authorized_keys directory and then copy the public key here.

Let me show the steps.

Step 1: Get the public key

Ask the end user to provide the public key by typing the following command:

cat ~/.ssh/id_rsa.pub

It will show a long random string starting with ssh-rsa:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ3GIJzTX7J6zsCrywcjAM/7Kq3O9ZIvDw2OFOSXAFVqilSFNkHlefm1iMtPeqsIBp2t9cbGUf55xNDULz/bD/4BCV43yZ5lh0cUYuXALg9NI29ui7PEGReXjSpNwUD6ceN/78YOK41KAcecq+SS0bJ4b4amKZIJG3JWm49NWvoo0hdM71sblF956IXY3cRLcTjPlQ84mChKL1X7+D645c7O4Z1N3KtL7l5nVKSG81ejkeZsGFzJFNqvr5DuHdDL5FAudW23me3BDmrM9ifUmt1a00mWci/1qUlaVFft085yvVq7KZbF2OP2NQACUkwfwh+iSTP username@hostname

You can get this text via email or messaging tools. Normally, it shouldn’t be a problem.

Step 2: Create ssh directory in the user’s home directory (as a sysadmin)

Keep in mind that you have to create these new directories and files in the end user’s home directory, not your own (root/sysadmin).

mkdir -p /home/user_name/.ssh && touch /home/user_name/.ssh/authorized_keys

Now open this /home/user_name/.ssh/authorized_keys file with a text editor like Vim and add the public key of the user here:

vim /home/user_name/.ssh/authorized_keys

Save and close the file. It’s almost ready.

DigitalOcean – The developer cloud

Helping millions of developers easily build, test, manage, and scale applications of any size – faster than ever before.

How to Add SSH Public Key to Server (4)Explore our products

How to Add SSH Public Key to Server (5)

Step 3: Set appropriate permission to the file

Having appropriate file permission on the ssh file is very important otherwise you’ll see errors like Permission denied (publickey).

First, make sure to set the correct file permissions:

chmod 700 /home/user_name/.ssh && chmod 600 /home/user_name/.ssh/authorized_keys

You created those file with either root or your own admin accounts for some other user. You need to change the ownership to the user:

chown -R username:username /home/username/.ssh

Now that it’s done, you can ask the end user to log in to the server.

Do let me know if you face any issues or if you have any suggestion on this topic.

I am an expert in the field of secure shell (SSH) and public key authentication, possessing a deep understanding of the concepts involved. My expertise is substantiated by hands-on experience and a comprehensive knowledge of the intricacies associated with SSH key management and authentication protocols.

In the provided article, the author discusses two methods for adding a public SSH key to a server, facilitating passwordless access via SSH. The concepts covered include:

  1. Public Key Authentication:

    • Public key authentication is a mechanism that allows secure access to a server without the need for a password.
    • It involves the use of asymmetric cryptography, where a pair of public and private keys is generated.
  2. SSH Key Generation:

    • The article assumes that users have already generated public and private SSH keys on their personal computers using the command ssh-keygen -t rsa.
    • This command generates an RSA key pair, typically stored in the ~/.ssh/ directory.
  3. Server Requirements:

    • The destination server must have SSH enabled.
    • Users should have a valid user account and password on the server.
    • The IP address of the server should be known.
  4. Method 1: Automatically Copy SSH Key to Server (Using ssh-copy-id):

    • ssh-copy-id is a tool provided by OpenSSH for copying public SSH keys to remote systems.
    • The command ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR_USER_NAME@IP_ADDRESS_OF_THE_SERVER automates the process of copying the public key to the authorized keys on the server.
    • Authentication is per user base, and the public key is placed in the intended user's home directory.
  5. Method 2: Manually Copy SSH Key to Server:

    • This method is suitable for sysadmins when SSH login via password is not allowed on the server.
    • The sysadmin creates the ~/.ssh/authorized_keys directory in the end user's home directory and manually copies the public key there.
    • Correct file permissions (chmod) and ownership (chown) are crucial for proper functionality.
  6. Key File Locations:

    • The default location for the public SSH key is ~/.ssh/id_rsa.pub.
    • The article highlights that if the key is stored in a different location, the user should specify that location in the respective command.
  7. File Permissions and Ownership:

    • Setting appropriate file permissions (chmod 700 for the .ssh directory and chmod 600 for authorized_keys) is emphasized to avoid permission issues.
    • Ownership of the files should be assigned to the respective user (chown -R username:username).

By presenting these concepts, the article aims to guide users and sysadmins in implementing secure and convenient SSH access using public key authentication.

How to Add SSH Public Key to Server (2024)

FAQs

How do I add a public key to my SSH server? ›

SSH Public Key Subsystem
  1. Use the SSH Client to connect to the SSH Server using the account for which you want to configure the public key.
  2. Once connected, open Client key manager. Right-click on the keypair, and select Upload to server.
  3. You should now be able to authenticate using this keypair.

How do I manually add a SSH key to a server? ›

How to add new SSH key to a cloud server
  1. Connect to the server using your current key. ssh [email protected] -i /current_private_ssh_key.
  2. Add a public key to the "authorized_keys" file. You can add a public key to the "authorized_keys" file using "nano" text editor (or any text editor of your choice): ...
  3. Verify new key.

How to transfer SSH public key to server? ›

Copying Public Keys to Remote Servers

You can run ssh-copy-id or manually configure the file. For systems with password authentication enabled, you can copy the public key from the client system to the remote server using the ssh-copy-id command.

How to connect server with ssh key? ›

The SSH public key authentication has four steps:
  1. Generate a private and public key, known as the key pair. ...
  2. Add the corresponding public key to the server.
  3. The server stores and marks the public key as approved.
  4. The server allows access to anyone who proves the ownership of the corresponding private key.
Aug 10, 2021

How to add SSH key in local? ›

Adding your SSH key to the ssh-agent
  1. First, check to see if your ~/.ssh/config file exists in the default location. $ open ~/.ssh/config > The file /Users/YOU/.ssh/config does not exist.
  2. If the file doesn't exist, create the file. ...
  3. Open your ~/.ssh/config file, then modify the file to contain the following lines.

How do I generate a public SSH key pair? ›

Generate an SSH Key Pair
  1. Run the ssh-keygen command. You can use the -t option to specify the type of key to create. ...
  2. The command prompts you to enter the path to the file in which you want to save the key. ...
  3. The command prompts you to enter a passphrase. ...
  4. When prompted, enter the passphrase again to confirm it.

How do I add an existing key to SSH? ›

Adding SSH Key to SSH Agent
  1. Check if SSH Agent is running. eval "$(ssh-agent -s)"
  2. Add the Keys to SSH Agent. ssh-add ~/.ssh/nameofkey.
  3. Verify Keys Added to SSH Agent. ssh-add -l.
  4. Copy Key to Remote Server. ssh-copy-id [email protected].
  5. Copy Server Key to Host. ssh-copy-id [email protected].
Mar 22, 2022

How to add public key to SSH server in PuTTY? ›

How to setup SSH public key access with puTTY?
  1. Download the latest version of PuTTY and associated apps.
  2. Run PuTTY Key Generator.
  3. Click “Generate” to create a new key.
  4. Open a terminal to ipFire with PuTTY with password authentication.
  5. On the terminal enter >nano /etc/ssh/authorized_keys to create and edit the new file.
May 6, 2022

What is the SSH public key? ›

What is an SSH key? updated on: 01.22.2024. An SSH key is a secure access credential used in the Secure Shell (SSH) protocol. SSH key pairs use public key infrastructure (PKI) technology, the gold standard for digital identity authentication and encryption, to provide a secure and scalable method of authentication.

How to add ssh key to computer? ›

For Windows 10 & 11
  1. Press the Windows key or open up the Start Menu. Type “cmd”.
  2. Under “Best Match”, click “Command Prompt”.
  3. In the command prompt, use the ssh-keygen command: ...
  4. The system will now generate the key pair and display the key fingerprint and a randomart image. ...
  5. Open your file explorer.

How do I give someone SSH access to my server? ›

Granting another device SSH access to your server
  1. Create a new SSH key. Follow the instructions in the initial setup article on generating a new SSH key while on the device you'd like to add access to.
  2. Copy the public key. ...
  3. Add to authorized_keys file.

Can I share my SSH public key? ›

Conceivably, you can share the public key with anyone without compromising the private key; you store it on the remote system in a .ssh/authorized_keys directory. To use SSH public key authentication: The remote system must have a version of SSH installed.

How to SSH into a server? ›

Connecting to your Dedicated Server via Terminal
  1. Open the terminal on your computer.
  2. Type ssh, followed by a space. ...
  3. If you see a message stating “Are you sure you want to continue connecting” type yes, then click the Enter key.
  4. You will then be prompted to enter your password.
Feb 21, 2024

How to open ssh key in terminal? ›

The first method is to use the cat command:
  1. Using the run command.
  2. Execute the following command. cat ~/.ssh/id_rsa.pub. The command will display your SSH key on your Linux machine without requiring your key authentication password.
Sep 12, 2023

How to SSH into a server using IP? ›

Enter the hostname or IP address of your server into the Host Name field and leave the port number set to 22. Select Open to open a connection to your server, you'll see a login prompt to enter a username, then just enter your SSH username and password to connect to your server.

How do I add a SSH public key to Devops? ›

Generate an SSH key pair

Go to Valohai and navigate to the repository settings in your Valohai Project through Settings > Repository. Click on the “Generate SSH Key” button. This will generate you a new SSH key pair and you can download/copy both the Public (. pub) and Private key (.

How to add public key to SSH EC2? ›

1 Answer
  1. Open the Amazon EC2.
  2. Click Key Pairs in the Navigation pane.
  3. The console displays a list of key pairs associated with your account.
  4. Click Create Key Pair.
  5. The Key Pair dialog box appears.
  6. Enter a name for the new key pair in the Key Pair Name field and click Create.
  7. You are prompted to download the key file.
Jul 12, 2012

Top Articles
Cryptocurrency exchanges: what's the difference between CEXes and DEXes?
LotR cryptogram instructions
Omega Pizza-Roast Beef -Seafood Middleton Menu
Tryst Utah
Asist Liberty
Week 2 Defense (DEF) Streamers, Starters & Rankings: 2024 Fantasy Tiers, Rankings
Frank Lloyd Wright, born 150 years ago, still fascinates
How To Be A Reseller: Heather Hooks Is Hooked On Pickin’ - Seeking Connection: Life Is Like A Crossword Puzzle
Meer klaarheid bij toewijzing rechter
Lost Ark Thar Rapport Unlock
Vanadium Conan Exiles
CHESAPEAKE WV :: Topix, Craigslist Replacement
Tabler Oklahoma
Craigslist Free Grand Rapids
The ULTIMATE 2023 Sedona Vortex Guide
Ou Class Nav
Jayah And Kimora Phone Number
How to Watch the Fifty Shades Trilogy and Rom-Coms
Leccion 4 Lesson Test
Morristown Daily Record Obituary
Talkstreamlive
Gazette Obituary Colorado Springs
Understanding Gestalt Principles: Definition and Examples
Getmnapp
Inkwell, pen rests and nib boxes made of pewter, glass and porcelain.
Mineral Wells Skyward
6892697335
Pioneer Library Overdrive
Harrison County Wv Arrests This Week
Movies - EPIC Theatres
Nikki Catsouras: The Tragic Story Behind The Face And Body Images
Elanco Rebates.com 2022
5 Star Rated Nail Salons Near Me
Rund um die SIM-Karte | ALDI TALK
Nail Salon Open On Monday Near Me
Lil Durk's Brother DThang Killed in Harvey, Illinois, ME Confirms
Beth Moore 2023
#scandalous stars | astrognossienne
Final Exam Schedule Liberty University
Ktbs Payroll Login
“To be able to” and “to be allowed to” – Ersatzformen von “can” | sofatutor.com
Nina Flowers
John M. Oakey & Son Funeral Home And Crematory Obituaries
Trending mods at Kenshi Nexus
Lawrence E. Moon Funeral Home | Flint, Michigan
Unit 11 Homework 3 Area Of Composite Figures
Windy Bee Favor
Canonnier Beachcomber Golf Resort & Spa (Pointe aux Canonniers): Alle Infos zum Hotel
Craigslist Pets Charleston Wv
Rubmaps H
The Ultimate Guide To 5 Movierulz. Com: Exploring The World Of Online Movies
Scholar Dollar Nmsu
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 5962

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.