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?

  • 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 Dec 23 '16 at 21:26

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.

  • 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 Dec 24 '16 at 9:15
  • 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 Dec 24 '16 at 9:33
  • 1
    Note that sshd (the server) is doing this, not ssh (the client). – ysdx Dec 24 '16 at 13:02
  • @ysdx They're working together. The client tells the server to execute the command through a shell, and the server does it. – Barmar Dec 28 '16 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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