<\/span><\/h2>\n\n\n\nOn your local machine, you need to generate a new pair of keys in order to set up SSH key authentication. To do that, we’ll run:<\/p>\n\n\n\n
ssh-keygen<\/pre>\n\n\n\nYou can then hit enter until the key is created. Or, if you want, you can setup a password on the step:<\/p>\n\n\n\n
Enter passphrase (empty for no passphrase):
Enter same passphrase again:<\/pre>\n\n\n\nPlease keep a note of this password, as it will be required for every access to the server.<\/p>\n\n\n\n
<\/span>2. Copy your key to your Linux Server with ssh-copy-id<\/span><\/h2>\n\n\n\nAfter your key is copied, there’s another command where you can copy your key to your server without needing to edit\/add to the authorized_keys<\/code> file manually. You can do that by running the following:<\/p>\n\n\n\nssh-copy-id user@host -p port_number<\/pre>\n\n\n\nJust remember to change the username user<\/code> to your actual username, and host<\/code> to your server’s hostname or IP address. This will automatically copy your public key to your server, and after that, you can try to ssh to your server. You should not be prompted to provide a password, and you’ll be automatically logged in. To test it out, run:<\/p>\n\n\n\nssh user@host -p port_number<\/pre>\n\n\n\n<\/span>2.1. Copy your key when ssh-copy-id is not available<\/span><\/h2>\n\n\n\nIn some systems, you might not have the command ssh-copy-id<\/code> available, in that case, you’ll need to do it over a traditional SSH connection. To do that, we’ll run the following pre-made command. This will work on Linux systems:<\/p>\n\n\n\ncat ~\/.ssh\/id_rsa.pub | ssh user@host -p port_number \"mkdir -p ~\/.ssh && cat >> ~\/.ssh\/authorized_keys && chmod 700 -R ~\/.ssh\"<\/pre>\n\n\n\nJust remember to change the username user<\/code> to your actual username, and host<\/code> to your server’s hostname or IP address. This should do all the work for you – that one line will copy your local key to your server and insert it into your authorized_keys<\/code> file.<\/p>\n\n\n\nYou can test it after you run this command by trying to SSH into your server:<\/p>\n\n\n\n
ssh user@host -p port_number<\/pre>\n\n\n\n<\/span>Set up SSH key authentication on Windows<\/span><\/h2>\n\n\n\nWindows doesn’t come with the commands we show in step 2.1, so here is what you need to do to set up SSH key authentication if you’re running Windows. This guide’s steps are meant for readers that are running Windows 11. <\/p>\n\n\n\n
First, open the Terminal application (not as an administrator). You’ll then need to run the ssh-keygen.exe<\/code> command, like so:<\/p>\n\n\n\nPS C:\\Users\\rosehosting> ssh-keygen.exe<\/pre>\n\n\n\nYou’ll then get a few questions. You can use the default directory, and then set no passphrase (you can also set one if you prefer, but you’ll need to enter your passphrase every time you want to authenticate using your key. Here’s how our output looked:<\/p>\n\n\n\n
Generating public\/private rsa key pair.\nEnter file in which to save the key (C:\\Users\\rosehosting\/.ssh\/id_rsa):\nCreated directory 'C:\\\\Users\\\\rosehosting\/.ssh'.\nEnter passphrase (empty for no passphrase):\nEnter same passphrase again:\nYour identification has been saved in C:\\Users\\rosehosting\/.ssh\/id_rsa\nYour public key has been saved in C:\\Users\\rosehosting\/.ssh\/id_rsa.pub<\/pre>\n\n\n\nYou now have a saved keypair. You will now need to copy the public key (the one named id_rsa.pub<\/code>) to your server. First, print the public key in your terminal and copy it:<\/p>\n\n\n\nPS C:\\Users\\rosehosting> cat .\\.ssh\\id_rsa.pub<\/pre>\n\n\n\nThen SSH into your server:<\/p>\n\n\n\n
PS C:\\Users\\rosehosting> ssh.exe user@host -p port_number<\/pre>\n\n\n\nRun this command to create the folder where your SSH public key will be stored:<\/p>\n\n\n\n
mkdir -p ~\/.ssh<\/pre>\n\n\n\nYou can then open a new file using your preferred text editor. We’ll use nano:<\/p>\n\n\n\n
nano ~\/.ssh\/authorized_keys<\/pre>\n\n\n\nPaste your public key into the file, save, and exit. You then need to update the file permissions on the new folder and file:<\/p>\n\n\n\n
chmod 700 -R ~\/.ssh<\/pre>\n\n\n\nWith that, your key access should be all set up. You can now log out of your server and try to log back in. You should not be prompted to enter a password anymore.<\/p>\n\n\n\n