Feb. 10, 2024

SSH keys


More on:   
Go back

Post Image
In order to access a server from your machine, you have to generate an SSH key. This is an access credential for the secure shell network protocol. It is used to start a secure handshake. It allows you to transfer files, manage the network and access the operating system of the server.

The "ssh-keygen" is a tool to generate the SSH keys. They are generated with a cryptographic algorithm.

The SSH protocol uses a pair of keys: a private one and a public one. The public key is meant to be given to the remote entity. The private key should be kept in a secure place. It is used by your computer to decrypt the data that was encrypted by the remote entity, using the public key.

Before generating SSH keys, it is recommended to check if the machine has already generated them. If they exist, you can use those. But you can still generate new ones.

To generate them on a Mac, enter the following command:
ssh-keygen -t ed25519 -C “email_address@something.xyz”
The “ssh-keygen” is the tool to generate the keys, and “ed25519” is the algorithm. You will be asked the name of the file in which to save the key. Click enter to take the default.

Then you are asked to enter an optional passphrase.

Now you need to add the SSH key to the ssh-agent. This is another program holds private keys. It needs to run in the background:
$ eval "$(ssh-agent -s)"
Add your SSH private key to the ssh-agent:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
You can list what was created in the .ssh directory.

To connect to your server, use the following:
ssh ubuntu@55.333.34.90
Replace “ubuntu” with your user name, and the numbers with the IP address of your server.

Now move your pubic key to the server (do this from your local machine):
scp ~/.ssh/id_ed25519.pub ubuntu@55.333.34.90:~/.ssh/authorized_keys
Authorize now yourself (or the owner of the key) in the server:
sudo chmod 700 ~/.ssh/*
Edit the file /etc/ssh/sshd_config (still in the server), so the use of password is no longer authorized. Uncomment PasswordAuthentication to "no".
PasswordAuthentication no
Finally, restart the ssh:
sudo systemctl restart ssh