With one command , how to quickly connect to a server via SSH (Secure Shell)

In a world where time waits for no man or woman, we want to do things faster and it delivered to us faster. Then comes the repetitive tasks that become a nuisance with too many steps between the start and end. It’s at this moment each time one would think “%#$#?!!! There has to be an easier way, I hate typing this much and I can’t remember all of these step” As a developer I always reach this moment thinking “How in the hell can I automate this process? ” While a part of me hates the word automate(automation) because it’s the new buzz word related to “Machine Learning”, in the case of this article I’m going to show how I automate the process of connecting to a server via ssh (Secure Shell).

I’m currently using macOS but this task can be performed on any flavor Linux. So let’s get started.

SSH (Secure Shell) protocol uses cryptographic key for securely authenticating users without the use of a username and password. Keygen is the program used to create the key pair (public/private). The private key will live on the user’s machine while the public key will live on the server.

Before getting started be sure to login to the server via without the key. This initial access is essential to producing the shortcut later in these instructions. (Example below)

By convention all keys are stored in the .ssh folder from there enter command:
The -t flag indicates the type of algorithm, for this example it’s rsa

Once complete the key pair will be created. For example if the file name is “test”, the public key is test.pub and the private key is test (the private key will not have an extension)

The next step can be done two different ways. The first way would be placing the public key into the authorized_keys file on the server and connecting with the private key using the -i option to access it. It’s a manual way of connecting.

Remote Server

Connect manually using private key

The second way is a faster way that instantly speeds up the workflow by removing all of those extra keystrokes in between.

From with ssh folder on the user machine there’s a file labeled config that needs to be edited to make things happen. Inside the file these settings need to be added:

Host — name of the ssh shortcut

HostName — server ip address

User — ssh username

IdentifyFile — path/to/private/key

Now add the public key to the server, but wait not the manual way. There’s the ssh-copy-id command that copies the key to the server’s authorized_keys file. Adding the key this way references back to the step where ensuring a connection to the server is required.

Add the pubic key

Now with the public key and config settings in place connecting is as easy as typing a simple command.

Viola!!!

Want to shorten the command even more? Create an alias.

bshelling is full stack developer/software engineer from New Orleans, with a background in graphic design and digital marketing.

Follow him on Twitter @bshelling

Shelling is what they call me. A forward thinker debugging life's code line by line. Creator, crossfitter, developer, engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store