Using SSH key for Login

Generating an SSH Key

Ian Peter
2 min readDec 24, 2022

ssh-keygen -t rsa: This command generates a new SSH key pair, consisting of a private key (saved by default to ~/.ssh/id_rsa) and a public key (saved by default to ~/.ssh/id_rsa.pub).

The -t option specifies the type of key to create, in this case, an RSA key. RSA is a widely-used algorithm for secure data transmission and is commonly used for SSH authentication.

Adding the Public Key to the Authorized Keys

cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys

This command copies the public key that was generated in the previous step to the authorized_keys file, which is used by the SSH server to authenticate users.

When a user attempts to log in to the server using an SSH client, the client presents the server with the user's public key. If the server has the corresponding private key in its authorized_keys file, it allows the user to log in.

Starting the SSH Server

systemctl start ssh.socket

This command starts the SSH server, which listens for incoming connections from SSH clients and authenticates them using the public keys stored in the authorized_keys file.

It's important to start the SSH server before attempting to log in to the machine using an SSH client, as the server is responsible for accepting and authenticating incoming connections.

Connecting to the Machine Using an SSH Client

ssh localhost

This command connects to the machine that the user is currently logged in to (in this case, localhost, which refers to the current machine) using an SSH client. The client prompts the user for their login credentials and, if the server has the user's public key in its authorized_keys file, the user is authenticated and granted access to the machine.

Thank you for reading!

Feel free to reach out if you have any further questions.

--

--

Ian Peter

CTF player. Cybersecurity enthusiast and Computer science student