How to create shortcut for ssh?
ssh root@162.159.152.4
If above command looks familiar to you then you know what exactly I will be writing about here. If you are a developer who need to access server through ssh quite often may be to deploy your code or to monitor server, then you can understand how painful it is to type ssh command with ip address and ports by either remembering it or copy pasting from somewhere else. And of course memorizing it is tough if you have multiple servers as well. So you can simply create an alias of them so that you do not have to type whole thing again and again.
- Using SSH Config
You can create shortcut for ssh using config file inside .ssh folder. It is very straightforward process and I also preferred this method.
First of all go to your .ssh folder. And then open config file in edit mode.
cd ~/.ssh
nano config
Now you can add your shortcuts in this file specifying hostname, username, port and key as well as necessary. Look at the example below:
Host sample1
HostName medium.com
User ubuntu
Host sample2
HostName 162.159.152.4
User root
Host sample3
HostName 162.159.152.4
User xyz
Port 3030
Host sample4
HostName ec2.amazon.com
User ec2-user
IdentityFile /path/to/privatekey.pem
Now save the file and now you can just write below commands to ssh into your server.
ssh sample1
ssh sample2
ssh sample3
ssh sample4
2. Using alias in .bashrc file
You can also create shortcut using alias in your .bashrc file. You can use it for shortcuts for any commands besides ssh. Open .bashrc file and add the line as:
nano ~/.ssh/.bashrc
Now add your alias
alias sample1='ssh ubuntu@medium.com'
alias sample2='ssh root@162.159.152.4'
alias sample3='ssh xyz@162.159.152.4 -p 3030'
alias sample4='ssh ec2-user@ec2.amazon.com -i /path/to/privatekey.pem'
Save the file and restart your terminal. Now to ssh just simple type the command.
sample1
sample2
sample3
sample4
This method can be used to create shortcuts for any commands as you like.
Check this article to open regularly used applications in mac using bashscript. How to automate your start of day as a developer with Bash?