SSH Commands – How Does SSH Run a Command

shellssh

I'm using Bash on both client and server. When running a command over SSH:

  • ssh <host> 'declare' gives a list of shell variables.

  • ssh <host> 'mount' gives a list of mountpoints.

However, declare is a Bash builtin, while mount is an external command. How does SSH know which to run if there is a shell builtin and an external command with the same name on the server?

Best Answer

The ssh runs the commands you provide in the remote user's shell (obtained from the /etc/passwd), as visible from the source code:

argv[0] = (char *) shell0;
argv[1] = "-c";
argv[2] = (char *) command;
argv[3] = NULL;
execve(shell, argv, env);

Therefore the respective commands that is executed for your example on the remote server are:

  • bash -c declare
  • bash -c mount

Both of them are passed to the bash and evaluated. Built-ins are evaluated inside, and the external commands are called as if you would do that from your local command line prompt.

Related Question