DEV Community

Walter Nascimento
Walter Nascimento

Posted on

πŸ”’ How to access SSH via terminal using SSH key πŸ”’

Authenticating to the SSH server without the need for a password is a convenient and secure practice. By using the SSH key, you can access your servers remotely quickly and without typing the password each time. In this post, we will show you how to configure SSH access using a key. Let's go!

Step 1: Copy your public SSH key

First, you need to copy the public SSH key to the server. To do this, follow the steps below:

  1. Copy the contents of the public SSH key.

open your ~/.ssh/id_rsa.pub file with any text editor and select everything and copy (CRTL+C)

Step 2: Add your SSH key in the "authorized_keys" file

Now, you need to access the server via SSH and add your SSH key directly to the "authorized_keys" file.

Follow the steps below:

  1. Connect to the server using SSH:
ssh username@server_address
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the "~/.ssh/" directory inside the server:
cd ~/.ssh/
Enter fullscreen mode Exit fullscreen mode
  1. Add the contents of your public SSH key to the "authorized_keys"
vim ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode
  1. Make sure the "authorized_keys" file permissions are correct:
chmod 644 authorized_keys
Enter fullscreen mode Exit fullscreen mode

Step 3: Test SSH access

After adding your SSH key to the server, log out of the SSH server. The next time you try to login via SSH, access will be automatic, without the need to enter the password. Test access using the following command:

ssh username@server_address
Enter fullscreen mode Exit fullscreen mode

Extra

Using the -A option

Additionally, you can enable authentication forwarding when connecting to a remote server using SSH. This allows you to use your locally stored private keys to authenticate to other servers from the remote server, without having to enter passwords or private keys directly on the remote server. To do this, use the -A option in the SSH command when connecting to the remote server:

ssh -A username@server_address
Enter fullscreen mode Exit fullscreen mode

With this option enabled, you will be able to access other servers from the remote server without having to reauthenticate, as long as your private keys are correctly configured locally.

Enjoy secure and convenient SSH access using keys and authentication forwarding. Keep your connection protected and simplify your remote access processes!

πŸ’‘ NOTE: ⚠️ Remember to keep your private keys in a safe and secure place, ensuring the security of your servers.


Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!

😊😊 See you later! 😊😊


Support Me

Youtube - WalterNascimentoBarroso
Github - WalterNascimentoBarroso
Codepen - WalterNascimentoBarroso

Top comments (2)

Collapse
 
darkain profile image
Vincent Milum Jr

I think this article is a little misleading and might need to be flushed out a little more. The workflow basically reads as "connect to SSH in order to be able to connect to SSH" - it assumes you can already do the thing you're trying to do.

Collapse
 
walternascimentobarroso profile image
Walter Nascimento

ok, what title do you suggest?

because really, I just wanted to make it simpler when I connect over ssh :D