Server Administration and Linux Tipps & Tricks

Did you know how simply you can create “custom commands” in Bash, by using alias?

In Bash, alias creates a custom command that executed your predefined code. This predefined code can be, for example, an ssh command specifying IP adress, port number, login name and private key path. Instead of typing the entire long command (with possibly looking for the IP address in your notes), you can create a simple alias.

Here is an example how an alias can be used in the scenario described above:

$ alias ssh-myserver='ssh -l joey -p 123 -i /path/to/key.dsa 1.2.3.4'
$ ssh-myserver

Instead of typing ssh -l joey -p 123 -i /path/to/key.dsa 1.2.3.4 every single time you want to connect with your server, you can now simply execute ssh-myserver – your alias.

Alias defined like this will be preserved only during time of your current session. If you want to have alias working everytime you enter Bash, add the alias declaration in your ~/.bashrc file. If it does not exist, simply create it.

Your .bashrc may be looking like this:

# My aliases
alias ssh-myserver='ssh -l joey -p 123 -i /path/to/key.dsa 1.2.3.4'

I strongly encourage to use Bash alias for every longer command you have to type on regular basis. Over a time, you will notice how much time aliases can save.