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?

share|improve this question
2  
ssh always run your shell, which is stored in /etc/passwd. If it's /usr/sbin/nologin you can't log in. – Ipor Sircer 20 hours ago
up vote 20 down vote accepted

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.

share|improve this answer
    
If a shell is always started by ssh daemon, then the interesting thing is, when user calls ssh with a command, then that shell is not a login shell, even if user performed some kind of login. Any idea? – Cyker 8 hours ago
    
Yes. The shell running the command is 1) No a login shell 2) Not interactive one. You can force the interactivity by the -t switch, but it will still not load the rc files. – Jakuje 8 hours ago
1  
Note that sshd (the server) is doing this, not ssh (the client). – ysdx 4 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.