There was a problem loading the comments.

How to Use Public Key Authentication with SSH on Linux

Support Portal  »  Knowledgebase  »  Viewing Article

  Print

How to Use Public Key Authentication with SSH on Linux

Using public key authentication with SSH is a more secure way of logging into a remote system than using a password, as it uses a cryptographic key pair for validation. Although making sure to use a strong password can help to prevent brute-force attacks, public key authentication provides cryptographic strength and automated passwordless logins.

Follow these steps to set up public key authentication with SSH:

  1. Generate a key pair on your local machine:

    9674cf53215ca2c71ba14e6344a64786f1c3c9cea8c38a80d60c2156969eff1a65d37f4aec0bbaa0?t=119a76daf898f4f46cd74b9dd2a82c3e

    This command will create a pair of keys, one private and one public, in the ~/.ssh directory of your home directory.

  2. Copy the public key to the remote server:


    1337cbda32482abbf4c85cf15c50d7cc201ee5296c246dd71ee684c495d50b170488c5d558e58fc0?t=16389b5992406938999438dafe8c2b0f
     

    This command will copy the public key to the remote server and add it to the authorised keys file. You will be prompted to enter the password for the remote user.

    If the ssh-copy-id command is not available on your system, you can copy the contents of the public key file manually and paste it into the .ssh/authorized_keys file on the remote server.

  3. Test the connection:

    56be8e01d5e5148949daa249e1dee68074f8cd58e77c45c59dcf28fd17e41c05ae23b76332212b32?t=c78395a4d5b964aab0062abb3c4202a2

    This command should log you into the remote server without prompting you for a password. 

  4. f you need to disable the password authentication:

    3d64805d840b7d92aace0ffde80419ba1c41db5d4dc2ddae7748464eb3047d29c821fcd48c18475f?t=bddd252d9759ce34fe0b2287d533c683

    Change the following line from:

    903492d730ee0d9cb239d739db95280c0e0a20353ee9682a3c89da81cec9e65e64f4b672d3e0acfd?t=cb323ccef37ab208c220990a0965bfb7


    to:

    4cd1ef016d3a97854c08bc7248b027da1a263b9814022ea173a4a6f72ebcab3c99a8b64c7ef25e10?t=8518f1d448f076ea5fabe30a1dcb0caf

    Save and close the file, then restart the SSH service:

    2fdc0f7a0feebdf6d1ae40b91c386e5dbe1c4403815c206f808c46e78813d343fffad4cf1452340a?t=1c4a8a0890cfe6583551199b2e9a217e

    This will prevent users from logging in with a password and force them to use public key authentication.

    That's it! You have now set up public key authentication with SSH on Linux and can log in to the remote server without using a password.

    How to configure one or multiple SSH/SFTP Users for your key on Linux

    To configure one or multiple SSH/SFTP users for key authentication on Linux, you can follow these steps:

    1. Generate a key pair for each user:


      08da5b36aa642612ebfa5d3f5ef37de20e5ab6dac0072174e7bed2d985945b51065c3880e665f753?t=3c0ae71728f5eca390d0106077a188c9

      This command will create a pair of keys, one private and one public, in the ~/.ssh directory of each user's home directory.

    2. Copy the public key of each user to the remote server:

      98c5737bcae6781b335881e7c9f352a44b5636b8f3733768e7ba833a613e71a95342200f381bef6d?t=48153502a455c71e6ed57bc11ab3035e

      This command will copy the public key of each user to the remote server and add it to the authorized keys file. You will be prompted to enter the password for each remote user.

      If the ssh-copy-id command is not available on your system, you can copy the contents of the public key file manually and paste it into the .ssh/authorized_keys file on the remote server.

    3. Configure SSH/SFTP access for each user:

      dfef9d074f6676b033ed65b9bf40cd7e14ccebcdb9a6e7ba25592c2f4411862267a50bb199ea89c0?t=6b31e16f7ebeb43d2d7d933497b657a6

      Add the following lines at the end of the file for each user:

      ac0d13aa45dc81f2579850b55e4399fc0def139b0d7adb8cda4dc05fc500d37f1c34126bb380cc19?t=b696bff41f1d0f46bc3a94ffe6cf972f

      Replace username with the actual username of each user.

    4. Restart the SSH service:

      6cfa83e6b8c81919f942e549112ec72c48f162e53596fa1a7a14f54bdea3718a20f8964f4658e3ba?t=2f7cc06125ec4ed5913fa068f0c788be

      This will apply the changes to the SSH/SFTP configuration.

    That's it! You have now configured one or multiple SSH/SFTP users for key authentication on Linux. Each user can log in to the remote server with their key and access their home directory via SFTP.


Share via
Did you find this article useful?  

Related Articles

Tags

© Rackzar