How to use/run bash aliases over ssh based session

I have set up a bash alias named file_repl. It works entirely when I log in using ssh command. However, my bash aliases are not running over ssh, for example:
$ ssh file_repl
bash: file_repl: command not found

How do I run bash shell aliases when I use ssh command?

SSH client (ssh) is a Linux/Unix/macOS/*BSD command for logging into a remote server and for executing shell commands on a remote system. It is designed to provide secure encrypted communications between two untrusted machines over an insecure network such as the Internet.


How to run or execute commands with Linux ssh client

To run a free command or date command with ssh:
$ ssh date
Sample outputs:

Tue Dec 26 09:02:50 UTC 2017

$ ssh free -h
Sample outputs:

              total        used        free      shared  buff/cache   available
Mem:           2.0G        428M        138M        145M        1.4G        1.1G
Swap:            0B          0B          0B

Understanding bash shall and command types

The bash shell understands the following types of commands:

  1. Aliases such as ll
  2. Keywords such as if
  3. Functions (user defined functions such as genpasswd)
  4. Built in such as pwd
  5. Files such as /bin/date

The type command or command command can be used to find out a command type:
$ type -a date
date is /bin/date
$ type -a free
free is /usr/bin/free
$ command -V pwd
pwd is a shell builtin
$ type -a file_repl
is aliased to `sudo -i /shared/takes/master.replication'

Both date and free are an external commands and file_repl is aliased to sudo -i /shared/takes/master.replication. One can not simply execute aliased command such as file_repl:
$ ssh user@remote file_repl

How to run bash aliases using ssh client

Let us see command line syntax to run bash shell aliases using the ssh command under Linux, macOS, *BSD and Unix-like systems.

Bash aliases not running or working over ssh client on Unix based system

To solve this problem run ssh command as follows:
$ ssh -t user@remote /bin/bash -ic 'your-alias-here'
$ ssh -t user@remote /bin/bash -ic 'file_repl'

Where ssh command options:

Where bash shell options as follows:

  1. -i : Make the shell is interactive so that it can run bash aliases
  2. -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.

In short run the following command to run a bash aliases called ll:
$ ssh -t /bin/bash -ic 'll'
Sample session:

Running bash aliases over ssh based session when using Unix or Linux ssh cli

Running bash aliases over ssh based session when using Unix or Linux ssh cli

Here is my sample shell script:

[ ! -f "$I" ] && { echo "$I file not found."; exit 10; }
cat "$I" | sort | uniq | while read -r u
	echo "~^$uu 1;" >>"${O}"
echo "Config file created at ${O} and now updating remote nginx config file"
scp "${O}" ${box}:/tmp/
ssh ${box} /usr/bin/lxc file push /tmp/ nginx-container/etc/nginx/
ssh -t ${box} /bin/bash -ic 'push_config_job'


You leanred how to run commands and bash shell aliases on a remote machine using the ssh command. For more info see OpenSSH client and bash man page by typing the following commands:
$ man ssh
$ man bash
$ help type
$ help command

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04


Comments on this entry are closed.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.