AWS Builder Center

SSH Magic

If you're just getting into managing VMs and learning SSH, this post is the right for you :)

Today I finally feel like I managed to TAME ssh, so I'm going to share it and maybe you can do it too :) make ssh be scared of you, not backwards!
To connect to a server it was to be accesible by your computer, which means that either it has a public IP or it is in your VLAN. Of course you need the credentials too. Then you can connect simply running:
ssh <user>@<ip_address>

Then it will ask you for the password, and you're connected :)
⚡️ OpenSSH Take into account that your server needs to have  openssh  installed in order to allow ssh connections, otherwise you can not connect through ssh!
To allow your pc to enter without asking for the password every time, you can copy your public key under  .ssh/<key_file>.pub  inside server's  .ssh/authorized_keys  file, which contains one public key per line. OR you can do it automatically by running (in your pc, NOT in the server):
ssh-copy-id <user>@<ip_address>

And inserting the server's password when it is requested.
If you still don't have an ssh key, you can generate it with:
ssh-keygen -t rsa -b 4096

Which will use the RSA encryption. The  -b  flag specifies the key size to 4069 bits, default is 3072, and again, 4096 is more secure.
Additionally, you can run it with the  -C <your_email>@example.com  flag to add a comment which, in this case, will be your email. By default this is your  <user>@<hostname>  of your laptop. It can be whatever you want, but it's better if it's something meaningful.
This will generate two files: the private key and the public key. You share the public key, which has the extension  .pub .
To activate your key (tell the ssh agent to use it), you run:
# Add your new key (replace with your actual key path)
ssh-add ~/.ssh/id_rsa

To verify it was added:
ssh-add -l

Now that you were successfully added into a server as a trusted connection, you can save the connection info under  .ssh/config  and give it an alias, so you no longer need to remember the IP of the server (or maybe a very long host address). The format would be the following:
Host <alias>
HostName <ip_address_or_host_address>
User <user>
Port <port:optional>
IdentityFile ~/.ssh/<private_key_file>

Your private key file is the one WITHOUT the extension  .pub 
Now, you can connect to your server with:
ssh <alias>

And everything is easier + secure! :D You could also store your  .ssh/config  file in a private repository, so you don't lost your connections config. However, be careful to not commit your keys D:

Bonus

Now that you are not scared of SSH anymore, here there is a list of a couple of things you can do with it:
  • Your own CD pipeline to a self-managed server: instead of using third-party services that magically auto-deploys your projects when connecting your GitHub account, you can do it yourself! Just add an ssh key on your repo secrets, add it to authorized_keys on your server too, and make your CD job to git pull your repo :)
  • Work from any computer with a remote environment: set up your development environment in a server, connect to it through VSCode with the Remote - SSH extension, and that's it! Enjoy editing a project without cloning it and setting it up locally every time!
  • Your own cloud storage: with sshfs you can use your ssh key to connect to your server through your file system, just like OneDrive in the File Explorer of Windows! It is not friends with network changes so you have to unmount it before connecting or disconnecting to a network, but it is cool to just have your server files as any other file in your laptop :D
Hope this post unlocks a world of self-hosting addiction as it did for me, see you! haha :)
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article