How does ssh run a command?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x