Accessing Servers via SSH without passwords

Idea

Sometimes we try to make the server passwords complex for safety concerns, but that also makes each ssh access painful. Fortunately, there are solutions to non-password SSH logins, such as sshpass or SSH-Key pairs. sshpass can be a good option combined with alias commands defined in your .zshrc or .profile configurations. But still, it has too many steps. In this article, I will be focus on the ssh-key pair solution.

SSH-Key Pairs

A pair has two items. Therefore ssh-key pairs have two keys as a pair. One is private on your local machine, and the other is public, which should be stored on your target machine. A widely used generation algorithm is called RSA algorithm. You can look into the steps if you are interested. Moreover, there are many options to generate ssh-key pairs. The option -t is often used in defining the key type. The key pair we are generating below is RSA-type. But other platforms, such as GitHub, are using the ed25519 type.

Generate Key Pair

The RSA algorithm is considered highly secure, making it almost impractical to decrypt your private key from the published key. To generate an RSA-typed ssh-key pair, use the following command,

1
ssh-keygen -t rsa

Press <Enter> and accept all fields as default. Ideally, two files, id_rsa and id_rsa.pub, will be generated under the hidden directory ~/.ssh.

Copy .pub Key to Target Machine

You can log in to the remote machines and touch a authorized_keys file under ~/.ssh directory if it does not exist. Then append the public key manually to the authorized_keys file. A more recommended and decent way to do this is using the ssh-copy-id command.

1
ssh-copy-id user@server.address

After typing the password on the remote machine for once, you are good to go.

Reference

Author

Ziang Zhou

Posted on

2022-06-28

Updated on

2022-06-27

Licensed under

Comments