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.

How to Add SSH Public Key to Server (2024)

References

Top Articles
How to Download Any Video From the Internet: 11 Free Methods
Paris Olympics in pictures: 32 captivating photos from each event of the Summer Games
Consignment Shops Milford Ct
F2Movies.fc
Weather On October 15
Orange County's diverse vegan Mexican food movement gains momentum
Uta Kinesiology Advising
Calling All Competitors Wow
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Wlds Obits
Lsn Nashville Tn
Email Hosting » Affordable Mail Solution with Personal Domain | IONOS
’Vought Rising’: What To Know About The Boys Prequel, A Season 5 Link
Cooktopcove Com
Slither.io | Play the Game for Free on PacoGames
The Menu Showtimes Near Regal Edwards Ontario Mountain Village
Who should be in the Country Music Hall of Fame (but isn't yet)? Our picks
Creigs List Maine
Craigs List Duluth Mn
Carlitos Caribbean Bar & Grill Photos
Poker News Views Gossip
Squeezequeens
How Far To Tulsa
15 Best HDMovie2 Alternatives to Watch Movies in Hindi & Other Indian Languages Online Free Leawo Tutorial Center
Timon Meaning In Swahili
Aeries Brea
Alex Galindo And Leslie Quezada Net Worth 2022
Lkq Pull-A-Part
Oakly Rae Leaks
Twitter Pestel Analysis 2024| Free Pestel Framework
The Legend of Maula Jatt | Rotten Tomatoes
Sunset Time Yesterday
Craigs List New Haven Ct
MyEyeDr. near Lind<b>ergh Center Metro Station
N9K-C9372Px E Eol
Coventry Evening Telegraph Ccfc
Idaho Pets Craigslist
Trap Candy Strain Leafly
Claudy Jongstra on LinkedIn: Tonight at 7 PM opens NAP+, a new, refreshing and enriching addition to…
How To Get Mini Tusks In Blox Fruits
Kristine Leahy Spouse
Lavender Dreams Nails Walnut Creek Photos
Sam's Club Hiring Near Me
Austin Powers Judo Chop Gif
Research Tome Neltharus
Breitling ENDURANCE PRO X82310E51B1S1 für 2.885 € kaufen von einem Trusted Seller auf Chrono24
'It's huge': Will Louisville's Logan Street be the next Findlay or Pike Place market?
Sesame Street 4323
Craigslist Farm And Garden Lexington
Caldo Tlalpeño de Pollo: Sabor Mexicano - Paulina Cocina
Tokyo Spa Memphis Tn Reviews
Schematic Calamity
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6458

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.