How to Add SSH Public Key to Server


mhrsntrk / July 19, 2022

In order to access a remote server from your personal computer, you will need to upload your SSH public key to the authorized keys of the server.


  • You should have an access to destination server
  • Your destination server should have ssh enabled
  • You should have generated public and private ssh keys (just use the command ssh-keygen -t rsa -b 4096 -C "

Step 1: Get the SSH Public Key

On your public computer's terminal,

cat ~/.ssh/

This command will return the SSH public key something similar to below example.

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ3GIJzTX7J6zsCrywcjAM/7Kq3O9ZIvDw2OFOSXAFVqilSFNkHlefm1iMtPeqsIBp2t9cbGUf55xNDULz/bD/4BCV43yZ5lh0cUYuXALg9NI29ui7PEGReXjSpNwUD6ceN/78YOK41KAcecq+SS0bJ4b4amKZIJG3JWm49N1nszwUzxWOcsb0VR3UiCvvSYpmC+lA9JZJ25DL8D5nppOUD+DQhw48/U2EecvT//zE8wAilYhnwQp/U0PtHH7+l4QD4YK2LZVPFktK1pfxIsUf5evwrmEoNrWLjiwqXplCoQ2mPM/16pzBu++qb/JcUlKTfy2KsC2yygEGHOGMwipa13wM7DzMBuYwYvjPmzKR2JXRVYKyOetnFRJAZEXU6+QUm3V31f/T1QNSQIuykB5oi4LwYy8Jow8J11/bJmuwQTelAQ== username@hostname

You can copy the public key for the second step.

Step 2: Add the SSH Public Key

  • Login to user account of the destination server using username and password or another SSH key that already has access to the destination server.
  • Create the authorized_keys file by using below command
mkdir -p /home/user_name/.ssh && touch /home/user_name/.ssh/authorized_keys
  • Open the authorized_keys using your favorite editor (you can use nano, vi, vim, nvim etc.) using the below command
nano /home/user_name/.ssh/authorized_keys
  • Paste the previously copied personal computer's SSH public key into the authorized_keys file (if there are any entries, paste it into a new line)
  • Save and close

Step 3: Access the Server

You can use the below command to access the destination server

ssh user_name@server-ip

If everything works well, the server will ask your passphrase for the SSH key.

I personally prefer mosh instead of ssh you can read my previous blog post to learn how to install and use mosh,