SSH Passwordless Login Using SSH Keygen(Mac and Linux )

Chukwudike
GitStacks

--

SSH (Secure SHELL) is an open-source and most trusted network protocol that is used to login to remote servers for the execution of commands and programs. It is also used to transfer files from one computer to another computer over the network using a secure copy (SCP) Protocol.

Once you set up a shell user and try to log in via SSH, you’ll find you must enter your password each time. If you’d like to avoid entering your password every time, you can set up Passwordless Login. This way, you’ll be able to automatically login each time immediately without needing to enter your password.

The following are instructions on how to set up Passwordless Login for any Unix, Linux Machines

Step 1 — Generate the key pair

  1. On your Local machine ( Client ) generate the key pair using the ssh-keygen.
  • If you’re using Linux or Mac OS X, open your terminal and run the following command under your username:
[local]$ ssh-keygen -t rsa

If you click ‘Enter’, the key will be created with the default name of ‘id_rsa’. You can name this anything you like, but if you choose a custom name, you’ll need to let your SSH client know about the new key name. View Step #3 below for details.

Also, if you choose to use a custom name, make sure to specify the full path to your user’s .ssh directory. If you do not, the new key pair is created in the directory you’re running the command.

2. Click Enter on your keyboard to continue

You do not need to enter a passphrase, but it’s highly recommended as it protects your private key if compromised. If so, someone would still need your passphrase in order to unlock it.

The exception to this is if you’re running an automated process such as a cron job. You should then leave the password out. Generally, all keys used for interactive access should have a passphrase. Keys without a passphrase are useful for fully automated processes.

3. Click Enter again

Step 2 — Copying the public key you just created on your home computer (client) to your remote computer (server)

  1. Copy the public key on your local computer to your remote server by running the following command below.

2. This command responds with the following:

Step 3 — Adding your custom key to your ssh client

This step is only necessary if you gave your key a custom name in Step #1 above.

When creating your key pair, you choose what to name it. For example, the default name is ‘id_rsa’, but you can name it anything you like while creating it. However, if you choose a custom name, you’ll need to let your SSH client know about the new key.

You do this by starting ssh-agent. Start ssh-agent by running the following command. Make sure you use the backquote ` character and not a single quote — this backquote character is usually on the top left of your keyboard on the tilde ~ key:

When creating your key pair, you choose what to name it. For example, the default name is ‘id_rsa’, but you can name it anything you like while creating it. However, if you choose a custom name, you’ll need to let your SSH client know about the new key.

You do this by starting ssh-agent. Start ssh-agent by running the following command. Make sure you use the backquote ` character and not a single quote — this backquote character is usually on the top left of your keyboard on the tilde ~ key:

This initialises the ssh agent
This makes ssh-agent aware of your custom key

Step 4 — Confirming the SSH connection

If everything is configured properly, you should now be able to access your remote server through SSH without a password. Run this command on your home computer where you just created the original key pair.

You should now be able to log in without using a password.

What if you have more than one key pair?

If you have more than one key, you’ll need to add them all to ssh-agent. For example, if you have an id_rsa key in addition to a custom key, make sure you add both using ssh-add (as shown above).

Specifying a key pair for SSH to use

By default, your client will use the identity (private key) named ~/.ssh/id_rsa. However, if you’ve created more than one key, you can specify which one to use when connecting using the -i flag. For example:

I hope you learnt a thing or two about ssh from this article. Please leave a comment if you have any questions, I will reply as quickly as I can.

Thank you for reading.

--

--