Introduction
In a previous article we explained along with practical examples what are aliases in bash and how they work. In that article we also demonstrated how we could create create more complex aliases in the form of functions and how we could unset or remove and alias. I’ve you’ve tried to run an alias over an ssh session you would’ve noticed that they do not work by default. In this article we will explain how to run aliases over a remote ssh session.
What is a bash shell alias?
Although we’ve defined aliases in our previous article but we’ll explain them again briefly in this article for the sake of completeness. I’ve you’ve worked on the command line for a while you would be aware that on many occasions we have to chain commands i.e. execute multiple commands together in a single line separated by semicolons as delimiters and pipe the output of one command to the input of another command. Although the creators of shell commands have made an effort to ensure that command names are short and precise. But chaining or piping commands which we use frequently can result in a lot of typing on the users’ part. Bash allows us to create our own shortcuts of such long combination of commands by using aliases.
Syntax for declaring an alias
The syntax for declaring an alias is fairly straightforward.
alias alias_name="long command combination"
To demonstrate we will now create an alias named ds for the command “df -hTP”. The sample output of the df -hTP command is shown below
[root@linuxnix ~]# df -hTP Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_pbox6-lv_root ext4 18G 4.4G 12G 27% / tmpfs tmpfs 491M 239M 252M 49% /dev/shm /dev/sda1 ext4 477M 34M 418M 8% /boot
Now let’s create an alias for it using the alias command
[root@linuxnix ~]# alias ds="df -hTP"
We will now test the validity of the alias we just created.
[root@linuxnix ~]# ds Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_pbox6-lv_root ext4 18G 4.4G 12G 27% / tmpfs tmpfs 491M 239M 252M 49% /dev/shm /dev/sda1 ext4 477M 34M 418M 8% /boot [root@linuxnix ~]#
As you may have observed from the above output the result is the same when we type “df -hPT” or ds thereby confirming that our alias has been setup correctly.
Running aliases over ssh:
Before we attempt to execute our alias ds over an ssh session on a remote server, let’s first run the df command to check that we are in fact able to execute generic command line utilities on the remote system.
[root@linuxnix ~]# ssh 192.168.19.128 df root@192.168.19.128's password: Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_pbox6-lv_root 18003272 4511132 12570952 27% / tmpfs 502068 244696 257372 49% /dev/shm /dev/sda1 487652 34542 427510 8% /boot [root@linuxnix ~]#
The above output proves that commands are responding correctly over ssh on the remote system. If we try to use the alias we’ve just created instead of df we will encounter an error as shown below.
[root@linuxnix ~]# ssh 192.168.19.128 ds root@192.168.19.128's password: bash: ds: command not found [root@linuxnix ~]#
To resolve this issue we need to add a few additional options while invoking our ssh connection to the remote system. The syntax for the complete ssh command with options is as follows:
ssh -t user@remote /bin/bash -ic 'your-alias-here'
Now lets execute the alias ds again on the remote system using the additional ssh and bash shell options.
[root@linuxnix ~]# ssh -t 192.168.19.128 /bin/bash -ic 'ds' root@192.168.19.128's password: Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_pbox6-lv_root ext4 18G 4.4G 12G 27% / tmpfs tmpfs 491M 251M 240M 52% /dev/shm /dev/sda1 ext4 477M 34M 418M 8% /boot Connection to 192.168.19.128 closed. [root@linuxnix ~]#
Ssh option description:
-t : Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful.
Bash shell option description:
-i : Make the shell is interactive so that it can run bash aliases
-c: Commands are read from the first non-option argument command_string. If there are arguments after the command_string, they are assigned to the positional parameters, starting with $0.
Conclusion
In this article we briefly explained the concept of aliases in bash and then demonstrated how we could execute shell aliases over an ssh session by using additional ssh and bash options. Please note that in order for the alias to work over ssh it must be added to the .bashrc file in the home directory of the user running the ssh command.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021