How to

How to Add SSH Public Keys to a Server

How to Add SSH Public Keys to a Server

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

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

I am sure you understand the basic SSH concept. Your Linux server has enabled ssh. You have created an ssh key on your personal computer. Now you want to upload your public key to the server’s official key so you can access it without typing in your account password all the time.

This short tutorial shows you two methods for adding public SSH keys to the server.


Before you see that, let’s make it clear what you should have:

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

Now that you have confirmed the three requirements above, let’s look at how to use public key authentication.

Authentication is per user base so the public key goes to the user’s intended home.


Method 1: Automatically copy the ssh key to the server

The first method is when the end user copies the public key of his personal computer to the list of keys that are authorized on the remote server.

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

If you add a public key to the server, you must be able to log in without typing the password at any time.

OpenSSH provides a handy tool call called ssh-copy-id to copy ssh public keys to remote systems. Even creating the necessary directories and files.

As I mentioned before, you must know the username and password to the server that you want to access via public key authentication.


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

I have used ~ / .ssh / because that is the default location for the public ssh key. If you have it in several other locations, you must use it in the above command.

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

The first method has an action on the user’s side. Say you are a sysadmin and your server does not allow SSH to enter through a password. The only way to access the server is using SSH public key authentication.

In such cases, you can ask the end user to provide their public key. Now all you can do is create the .ssh / otor_keys directory and then copy the public key here.

Let me show the steps.

Step 1: Get a public key

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

cat ~/.ssh/

This will display 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. Usually, that shouldn’t be a problem.

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

Remember that you must 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 user’s public key here:

vim /home/user_name/.ssh/authorized_keys

Save and close the file. Almost ready.

Step 3: Set the appropriate permissions for the file

Having the appropriate file permissions on the ssh file is very important if you don’t see all the 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 create the files with root or your own admin account for several other users. You need to change ownership to the user:

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

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

Let me know if you encounter any issues or if you have suggestions on this topic.

Related posts

How to Share Games on the Nintendo Switch


How to see Wishlist on Audible on any device


How to Limit Screen Time on Xbox One


How to Download NVIDIA Drivers Without GeForce Experience


How to Organize Your Life with Kanban Boards


How to Download the Amazon Games Apps for Twitch Prime Games


How to overclock the CPU


How to Host Your Own VPN with Algo and Cloud Hosting


How to Join a Meeting in the Messenger Room