1

I would like to ssh to a machine with a pre-defined setting, specifically in zsh.

To do so, I have tried doing the following from the client (inspired by this answer):

$ ssh -t machine@domain "STARTUP_OPTION='A'; exec /path/to/zsh;"

and then I have the following .zshrc in the server:

if [ "$STARTUP_OPTION" = "A" ]; then
    echo "Logging in with A"
fi

This doesn't seem to work. How can I login with a specific shell while passing the value for a shell variable in the remote shell?

0
6

By default the only environment variable that's transmitted over an SSH connection is TERM. You can pack information there but you've got to be sure that it'll be unpacked on the server side. The client can transmit other messages, but the server needs to be set up to accept them with an AcceptEnv directive in /etc/sshd_config. Under Debian and most derivatives (Ubuntu, Mint, …) all variables whose name begins with LC_ are also accepted by the SSH server. These variables are conventionally used for locale settings, but you can use one of your own to pass something else. If your server allows it, you can simply write

LC_STARTUP_OPTION=A ssh

Another possibility is to run a command based on the user authentication key (this requires only control of ~/.ssh/authorized_keys, not the sshd configuration).

If you aren't able to transmit environment variables, you can set it on the command line and then execute an interactive shell, as you've been doing. Since you're executing a new shell (that's necessary because you can't both specify an initial command to run and get an interactive shell), this needs to be an environment variable, not a shell variable.

ssh -t machine@domain 'export STARTUP_OPTION="A"; exec /path/to/zsh'

Yet another approach is to feed some data to the shell via its standard input, and then redirect the standard input to the terminal.

ssh -t localhost 'echo "echo foo; exec </dev/tty" | exec zsh -i'
stdin: is not a tty
stty: standard input: Inappropriate ioctl for device
~% 

Despite the error messages about not being able to initialize the terminal, zsh seems to cope properly with the interactive part of the session.

2
  • Gilles, should not it be /etc/ssh/sshd_config? – heemayl Oct 7 '15 at 3:03
  • @heemayl Either, depending on the distribution. – Gilles 'SO- stop being evil' Oct 7 '15 at 3:11
3

You could export the variable definition, so that the variable definition is inherited by the zsh process:

$ export foo=bar
$ exec zsh
$ echo $foo
bar

Or use env:

$ exec env foo=bar zsh
$ echo $foo           
bar

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.