When I started working as a data developer and a data scientist in a publishing company, I began receiving multiple login credentials to remote servers. It was a hassle to search and keep track of these credentials, especially when I have to switch servers. So, I had to find a way to automate my logins.
There are few steps you have to follow in order to automate your SSH login.
- Create an SSH key without Passphrase
- Copy SSH key to the target server
- Verification of copied SSH key in the remote server
- Create bash aliases to easily remember the server domains or use SSH Config
No Passphrase SSH Key
There are other ways to automate your login to remote server such as via EXPECT. However, this exposes your password in your script and it defeats the purpose of SSH which is using ssh keys. Aside from this, expect is not available on windows’ git bash. Hence, I had chosen the SSH key login without passphrase.
Create an SSH key and leave the passphrase blank by hitting ENTER twice.
$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The default SSH key will be saved at /home/username/.ssh/id_rsa for private key and /home/username/.ssh/id_rsa.pub for public key.
Copying SSH key to the Remote Server
Copy your SSH key to your target server and supply your password by the following command;
$ ssh-copy-id username@server
server password:
Verify the Copied SSH key in the Remote Server
You have to verify that your copied SSH key is in the remote server by logging to it using the conventional SSH command and making sure that there are no extra keys in place. This time, this should not ask for a password.
$ ssh username@server
$ cat .ssh/authorized_keys
Creating Bash Aliases or Create an SSH Config
It is not much of a hassle if your remote server has a domain name that you can easily remember. If you are given with IP addresses, you have to remember them or create shortcut commands like aliases or via SSH config.
- To create bash aliases in your .bash_profile, open/create it with your favorite text editor. I use vim.
$ vi .bash_profile
Inside the vim editor, insert the alias;
alias datawarehouse='ssh username@server'
In the line above, ‘datawarehouse’ is the shortcut command line to replace my SSH login to my remote server.
Save and exit from the vim editor.
This time, you can easily type,
$ datawarehouse
You will automatically be connected to your server.
2. Another option is to create an SSH config.
Create a config file inside the .ssh folder
$ vi ~/.ssh/config
Inside the config file, write the following lines;
Host datawarehouse
HostName targetserver
User username
IdentityFile ~/.ssh/id_rsa
Save and exit from the vim editor. Just like the first method, you can just type in
$ ssh datawarehouse
In my case, I chose the alias method. I like to create shortcuts in one place.
Conclusion
Logging in to remote servers can be a hassle-free. This can be accomplished with few steps and a bit of familiarity with Linux commands.
In my next story, I pushed an extra step by automating my access to a Jupyterlab which is running in the remote server. If you are interested in this topic, come visit this story at (still in progress.. 😄).
References: