Creating an SSH connection to a server

Creating an SSH connection to a server

Introduction

First of all let's talk about SSH, SSH(Secure Shell) is an encrypted protocol that provides a secure avenue for accessing and managing remote devices over an unsecured network. It serves as a shield protecting the communication between two systems, typically a local machine (like your PC) and a remote server (like a web server).

Whether you're a system administrator, developer, or simply someone navigating the digital landscape, SSH acts as a trusted guardian, allowing you to connect to remote systems and execute commands securely.

SSH is your secure key to accessing computers worldwide. It encrypts your data for safety, uses passwords or keys for verification, and lets you manage files and run programs remotely. It ensures a secure and productive digital experience.

Today, I'll guide you through the process of establishing an SSH connection to your server.

Generating the SSH key

To get started, we first need to create an SSH key. It's the digital passcode we need for secure access.

Now lets open our terminal and run the following command to create a new key

ssh-keygen

After running that cmd you'll see a prompt asking you what directory and file you want to save the key, by default (that's if you just click the enter key) it will save in your home directory, the user logged in and a .ssh directory which is a hidden folder by default and the file name will be id_rsa so that will contain the private key and public key only that the public key will be ending with an extension .pub So it should look something like this /root/.ssh/id_rsa

But you can also decide to choose the dir and file you'll like to keep it. So I'll save it like this ~/.ssh/learninig with the ~ standing for the root dir. Then you'll be prompted a passphrase you can decide to skip it or just input something you'll always remember.

After running that your out put should look something like this

So we now have a public and private which means we have successfully created our SSH keys.

Establishing Authorized Keys for a Sever

Now, let's authorize this key by registering it as an authorized host on your server. This step involves interacting with your server's terminal interface, where we'll establish the key's validity for secure access.

Your server should definitely have the ~/.ssh/ directory, if not create it, inside it, there should be a file named authorized_keys if not you can create it but watch out for the names make sure it is rightly spelt.

Then run this command

cat ~/.ssh/id_rsa.pub | ssh username@server_ip 'cat >> ~/.ssh/authorized_keys'

Replace username with your username and server_ip with the IP address to your server. And replace the ~/.ssh/id_rsa.pub with the actual place you saved your public key.

So, the entire command is essentially taking your local public key and appending it to the ~/.ssh/authorized_keys file on the remote server, allowing you to authenticate with your private key when connecting to that server. This is a manual way of adding your public key to the server's authorized keys.

Now we can connect to our remote server

Connecting to the server

Connecting to the server, if you're like me you'll surely get tired and irritated of repeatedly typing in passwords. So we can configure our SSH to connect to the server without having to always type in passwords

Run this command to configure it to do so

ssh-add ~/.ssh/id_rsa

You can replace the file name (id_rsa) if you didn't use the default file. So doing this you're adding your password to the remote server, which won't ask you for a password again.

Now the final lap is actually connecting to the server

ssh username@server_ip

Replace username with your username and server_ip with the IP address to your server.

With these steps completed, you are now set to effortlessly access your server, execute the process, and you'll find yourself seamlessly logged in.

Conclusion

In summary, these SSH essentials provide a key foundation for secure server connections. By following these steps, you've ensured a smooth, password-free pathway to your server. Your journey to secure and efficient remote access is now set in motion.

I hope you find this post useful and learn something new in the process. If you have any questions or suggestions let me know in the comment section.

Feedback will help me a lot and motivate me to share more content like this in the future.

If you have read this far, Thank you very much!

Connect with me on Twitter | LinkedIn | Github

Happy Coding!