In this tutorial, we'll have a look at how to use the more secure SSH keys for logging in to your server instead of the weaker password method.
For this tutorial, we assume that you have a Linux OS running (Windows users can use WSL).
First, we need to generate our SSH key pair, which we can use later to log in to our remote machine. To do this, we start by running the following command:
This command will by default generate a key with 2048-Bit-RSA encryption. If you need more security, you can also add the
-b 4096 flag to the command to generate keys with 4096-Bit-RSA encryption.
Now you have to define where to store the key. The default setting is
~/.ssh/id_rsa (where ~ points to the home folder of your user on Linux).
Then the script prompts you to enter a password that will prevent the key from being used without a password. If you don't want to protect this key with a password, just hit
Enter (not recommended).
If the key was successfully generated, the output should look like this:
Your identification has been saved in ~/.ssh/id_rsa Your public key has been saved in ~/.ssh/id_rsa.pub The key fingerprint is: SHA256:G+UCCe2r45jt7az1cTVEZj4R1/B8U7eAMNmw0DsAOA username@host The key's randomart image is: +---[RSA 3072]----+ | .oo. oo+oB=oo | | . ..o . @=o+oo| | E .o o + =o o+| | .. + . . .| | .S . o | | . + . . | | .. o . | | +o+ . o | | oo=++ . | +----[SHA256]-----+
We now have the private and the public keys on our local system, but we need to transfer the public key to our server.
For that we can use the built in
ssh-copy-id of our Linux OS.
Use this command:
188.8.131.52 is the IP of the server.
When connecting to the server for the first time, the system will ask whether it should trust the key fingerprint of the server:
The authenticity of host '184.108.40.206 (220.127.116.11)' can't be established. ECDSA key fingerprint is fd:fd:d5:f9:88:fe:73:84:e1:55:00:ab:d6:6f:22:fe. Are you sure you want to continue connecting (yes/no)?
When you type
yes, the script connects to the server:
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys email@example.com's password:
Next enter the password of your remote server. If the connection was successful, the script will copy the public key into the
~/.ssh folder of this user account:
Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'firstname.lastname@example.org'" and check to make sure that only the key(s) you wanted were added.
ssh-copy-id is not available on your system, you can also connect to your server via SSH and then copy the key manually.
Run the following command on your local machine to get the content of the key:
The output should look like this:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDAuEmfbUGlpPtjpxZU7jcLmmgQ5OAsfPumzvadL4qjLhv3FFbpanQpsSlCfE8kDvWqCkHRhXm29TpjtGLU0mJisg3dz9zHkoG1LoRTnPACmmzjW3Ca+MX56cgWKRFB/d2QN9riPCkRfvFCBgeHlZIa5hGlmbcu+uJNfTu/1vDjlhnyWMB9gxuesOPEB08YT64Ro84ACPAIdfFC4RJICilO+L9FgeYjKwi9jUvYOL3Ywhgs/DkC+EvTkZGyUL8cX8ITkFjLpVh7c/8KkFZCB6DQe1CJQKo8Y5TUM2wit0UlrsrO2AhnlfXXTtIOELQ3ygug6YI1CPaqGpLPbzlM1hfnros5S2UJB6OmiDM17YuyMdbQj2O5HyDUTEtGzgfKt/IG3Na6wFSGEOZhY4TIV9Z6FwXvJ+F4GK4srMgq/HJAIthbRP3x30K29Sfb8edgzDtaJbR38vrIjGUAVbr+mSqVZ1yeithXjuQ9hVOc1GaCxaMuVAw/CHtwdnq100= username@host
Now copy this key (all of it including the
ssh-rsa part and also the
username@host in the end) and connect to your server via SSH.
If you are connected to your remote server, create a new file in the
~/.ssh folder named
Paste the previously copied public key content into the file, save it with
Ctrl+O and close the file with
Now the key should be ready for use.
The last step is to test the key. Therefore, log out of your server if you are still logged in and log in again using the usual SSH command:
Instead of username/password, the SSH key is now used for login. If you have previously set a password for the key file, you must use it for the key to work.
To make the whole login process more secure, you can disable password login in general and use only SSH key.
For this we need to edit our
sudo nano /etc/ssh/sshd_config
Next we have to change this option:
- #PasswordAuthentication yes + PasswordAuthentication no
and restart the SSH service:
sudo systemctl restart ssh
From now on, only SSH key login will work.
There should be an SSH key on your server that you can use to log in to it. This method is considerably more secure than the usual password method and helps to increase server security.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
By making a contribution to this project, I certify that:
The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or
The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or
The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.