4.17 Remote shell setup hints

4.17.1 Changing the default remote or local shell

By default, TRAMP uses the command /bin/sh for starting a shell on the remote host. This can be changed by setting the connection property "remote-shell"; see Setting own connection related information. If you want, for example, use /usr/bin/zsh on a remote host, you might apply

(add-to-list 'tramp-connection-properties
             (list (regexp-quote "/sshx:user@host:")
                   "remote-shell" "/usr/bin/zsh"))

This works only for connection methods which allow to override the remote login shell, like sshx or plink. See Inline methods and External methods for connection methods which support this.

This approach has also the advantage, that settings in tramp-sh-extra-args will be applied. For zsh, the trouble with the shell prompt due to set zle options will be avoided. For bash, loading ~/.editrc or ~/.inputrc is suppressed.

Similar problems can happen with the local shell TRAMP uses to create a process. By default, it uses the command /bin/sh for this, which could also be a link to another shell. In order to overwrite this, you might apply

(customize-set-variable 'tramp-encoding-shell "/usr/bin/zsh")

This uses also the settings in tramp-sh-extra-args.

Note: If you use an ssh-based method for connection, do not set the RemoteCommand option in your ssh configuration to something like screen. If used, RemoteCommand must open an interactive shell on the remote host. On the other hand, some ssh-based methods, like sshx or scpx, silently overwrite a RemoteCommand option of the configuration file.

4.17.2 Other remote shell setup hints

TRAMP checks for the availability of standard programs in the usual locations. Common tactics include successively trying test -e, /usr/bin/test -e, and /bin/test -e. ls -d is another approach. But these approaches do not help with these new login patterns.

When TRAMP encounters two-factor logins or additional challenge questions, such as entering birth date or security code or passphrase, TRAMP needs a few more configuration steps to accommodate them.

The difference between a password prompt and a passphrase prompt is that the password for completing the login while the passphrase is for authorizing access to local authentication information, such as the ssh key.

There is no one configuration to accommodate all the variations in login security, especially not the exotic ones. However, TRAMP provides a few tweaks to address the most common ones.

tramp-shell-prompt-pattern

tramp-shell-prompt-pattern is for remote login shell prompt, which may not be the same as the local login shell prompt, shell-prompt-pattern. Since most hosts use identical prompts, TRAMP sets a similar default value for both prompts.

tramp-password-prompt-regexp
tramp-otp-password-prompt-regexp
tramp-wrong-passwd-regexp

TRAMP uses tramp-password-prompt-regexp to distinguish between prompts for passwords and prompts for passphrases. By default, tramp-password-prompt-regexp handles the detection in English language environments. See a localization example below:

(customize-set-variable
 'tramp-password-prompt-regexp
  (concat
   "^.*"
   (regexp-opt
    '("passphrase" "Passphrase"
      ;; English
      "password" "Password"
      ;; Deutsch
      "passwort" "Passwort"
      ;; Français
      "mot de passe" "Mot de passe")
    t)
   ".*:\0? *"))

This user option is, by default, initialized from password-word-equivalents when TRAMP is loaded, and it is usually more convenient to add new passphrases to that user option instead of altering this user option.

The user option tramp-otp-password-prompt-regexp has a similar purpose, but for one-time passwords. Those passwords are not cached by TRAMP for reuse.

Similar localization may be necessary for handling wrong password prompts, for which TRAMP uses tramp-wrong-passwd-regexp.

tramp-terminal-type

TRAMP uses the user option tramp-terminal-type to set the remote environment variable TERM for the shells it runs. By default, it is "dumb", but this could be changed. A dumb terminal is best suited to run the background sessions of TRAMP. However, running interactive remote shells might require a different setting. This could be achieved by tweaking the TERM environment variable in process-environment.

(let ((process-environment
       (cons "TERM=xterm-256color" process-environment)))
  (shell))
Determining a TRAMP session

Sometimes, it is needed to identify whether a shell runs under TRAMP control. The setting of environment variable TERM will help:

if test "$TERM" = "dumb"; then
   ...
fi

Another possibility is to check the environment variable INSIDE_EMACS. Like for all subprocesses of Emacs, this is set to the version of the parent Emacs process. TRAMP adds its own package version to this string, which could be used for further tests in an inferior shell. The string of that environment variable looks always like

echo $INSIDE_EMACS
⇒ 27.2,tramp:2.4.5
tset and other questions

To suppress inappropriate prompts for terminal type, TRAMP sets the TERM environment variable before the remote login process begins via the user option tramp-terminal-type (see above). This will silence common tset related prompts.

TRAMP’s strategy for handling such prompts (commonly triggered from login scripts on remote hosts) is to set the environment variables so that no prompts interrupt the shell initialization process.

An alternative approach is to configure TRAMP with strings that can identify such questions using tramp-actions-before-shell. Example:

(defconst my-tramp-prompt-regexp
  "Enter the birth date of your mother:\\s-*"
  "Regular expression matching my login prompt question.")

(defun my-tramp-action (proc vec)
  "Enter \"19000101\" in order to give a correct answer."
  (save-window-excursion
    (with-current-buffer (tramp-get-connection-buffer vec)
      (tramp-message vec 6 "\n%s" (buffer-string))
      (tramp-send-string vec "19000101"))))

(add-to-list 'tramp-actions-before-shell
             '(my-tramp-prompt-regexp my-tramp-action))

The regular expressions used in tramp-actions-before-shell must match the end of the connection buffer. Due to performance reasons, this search starts at the end of the buffer, and it is limited to 256 characters backwards.

Conflicting names for users and variables in .profile

When a user name is the same as a variable name in a local file, such as .profile, then TRAMP may send incorrect values for environment variables. To avoid incorrect values, change the local variable name to something different from the user name. For example, if the user name is FRUMPLE, then change the variable name to FRUMPLE_DIR.

Non-Bourne commands in .profile

When the remote host’s .profile is also used for shells other than Bourne shell, then some incompatible syntaxes for commands in .profile may trigger errors in Bourne shell on the host and may not complete client’s TRAMP connections.

One example of a Bourne shell incompatible syntax in .profile: using export FOO=bar instead of FOO=bar; export FOO. After remote login, TRAMP will trigger an error during its execution of /bin/sh on the remote host because Bourne shell does not recognize the export command as entered in .profile.

Likewise, (~) character in paths will cause errors because Bourne shell does not do (~) character expansions.

One approach to avoiding these incompatibilities is to make all commands in ~/.shrc and ~/.profile Bourne shell compatible so TRAMP can complete connections to that remote. To accommodate using non-Bourne shells on that remote, use other shell-specific config files. For example, bash can use ~/.bash_profile and ignore .profile.

Interactive shell prompt

TRAMP redefines the remote shell prompt internally for robust parsing. This redefinition affects the looks of a prompt in an interactive remote shell through commands, such as M-x shell RET. Such prompts, however, can be reset to something more readable and recognizable using these environment variables.

TRAMP sets the INSIDE_EMACS environment variable in the startup script file ~/.emacs_SHELLNAME.

SHELLNAME is bash or equivalent shell names. Change it by setting the environment variable ESHELL in the .emacs as follows:

(setenv "ESHELL" "bash")

Then re-set the prompt string in ~/.emacs_SHELLNAME as follows:

# Reset the prompt for remote TRAMP shells.
if [ "${INSIDE_EMACS/*tramp*/tramp}" == "tramp" ] ; then
   PS1="[\u@\h \w]$ "
fi
busybox / nc

TRAMP’s nc method uses the nc command to install and execute a listener as follows (see tramp-methods):

$ nc -l -p 42

The above command-line syntax has changed with busybox versions. If nc refuses the ‘-p’ parameter, then overwrite as follows:

(add-to-list 'tramp-connection-properties
             `(,(regexp-quote "192.168.0.1")
               "remote-copy-args" (("-l") ("%r"))))

where ‘192.168.0.1’ is the remote host IP address (see Setting own connection related information).