How to Automate SSH login

Published in: HowTo

I have to remotely log in to a lot of servers quite often, which is why I have setup several ssh related aliases on ~/.bash_profile (Mac) or ~/.profile (Ubuntu) to automate my login process so that I could simply type sg on the terminal and get logged in to my remote server without any prompts. But this is the last part. Let’s start from the beginning.

First, you need to create a keygen for your server. Make sure you hit enter for both Key location prompt and passphrase prompt:

ssh-keygen -t rsa -b 2048 (1)
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): (2)
Enter passphrase (empty for no passphrase): (3)
Enter same passphrase again: (4)
Your identification has been saved in /home/username/.ssh/id_rsa. (5)
Your public key has been saved in /home/username/.ssh/id_rsa.pub.    (6)
1 Generating 2048-bit RSA Key
2 Press Enter.
3 Press Enter.
4 Press Enter.
5 Saving private Key.
6 Saving public Key.

Copy your keys to the target server:

$ ssh-copy-id id@server
id@server's password:

Try logging into the server, with ssh id@server, and check in: .ssh/authorized_keys to make sure you haven’t added extra keys that you weren’t expecting.

Finally, create an alias on ~/.bash_profile (Mac) or ~/.profile (Ubuntu), so that you can just type one word to log in to your server. My list of alias looks something like this:

alias sg="ssh -p 1234 root@1.2.3.4"
alias dns="ssh -p 1000 root@2.3.4.5"
alias ftp="ssh -p 1000 root@3.4.5.6"
alias log="ssh -p 1000 root@4.5.6.7"
alias ssa="ssh -p 1001 root@5.6.7.8"
alias ats="ssh -p 1000 root@6.7.8.9"



about | twitter | facebook | archive | rss