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)
~ ssh username@youserverip -p portnumber
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
~.ssh/ ssh-keygen -t rsa
Give the key pair a name
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/admin/.ssh/id_rsa):
Enter a passphrase for the key pair, then again to validate the pair. The passphrase encrypts the pair and prevent someone else from using it.
Enter passphrase (empty for no passphrase):
Enter same passphrase again
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)
Your identification has been saved in admin.
Your public key has been saved in admin.pub.
The key fingerprint is:SHA256:email@example.comThe key’s randomart image is:+ — -[RSA 2048] — — +| . || o . . || * + + o || ..B X * || . .o++ S || = Eo== || . B B=o || +.B+= || .B=+.. |+ — — [SHA256] — — -+
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.
Connect manually using private key
~ ssh -i test username@youserverip
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
~ ssh-copy-id -i test.pub username@youserverip
Now with the public key and config settings in place connecting is as easy as typing a simple command.
~ ssh hostnamefromconfig
Want to shorten the command even more? Create an alias.
~ alias connectme=’ssh hostnamefromconfig’~ connectme
bshelling is full stack developer/software engineer from New Orleans, with a background in graphic design and digital marketing.
Follow him on Twitter @bshelling