Setting ssh remote host environment variables (Interactive shell)By neokrates, written on April 29, 2010
- Website: http://www.thinkplexx.com
- Join date: 05-31-09
- Posts: 23
You have some user YOUR_USER in your HOME_SYSTEM. And you want to set environment on remote host REMOTE_SERVER. For the interactive shell it is as easy as properly configuring .bashrc and .profile. For non-interactive shell it might be a problem. Many things can go wrong. Here is explained what might be the problem and how to fix it.
Should also work for:
Other Linux distros
Following applies to both interactive and non-interactive modes
What is the default shell for YOUR_USER?
On REMOTE_SERVER check the /etc/passwd:
>grep YOUR_USER /etc/passwd YOUR_USER:x:23014:150::/home/YOUR_USER:/bin/bash
The last entry is the default shell. If it is not /bin/bash (maybe /bin/sh?), change it to /bin/bash.
This is important because each shell uses its own files to setup environment. We will tune .bashrc and .profile. That would be useless if the shell is not bash.
Same environment for interactive and non-interactive shell
There are two files ~/.profile and ~/.bashrc.
~/.profile – read by ssh in interactive shell on login
~/.bashrc – read by ssh in non-interctive shell on login
To simplify things you can set the .profile content so it reads .bashrc:
Alternatively, you can make a symbolic link:
ln -s ~/.bashrc ~/.profile
In your .bashrc there are many ways to export variables:
1 2 3 4 5 6
export YOUR_VAR=YOUR_VALUE YOUR_VAR1==YOUR_VALUE1 export YOUR_VAR1 export PATH=$PATH:/my/extra/path
These steps are usually enough to setup the environment of interactive shell. But for the non-interactive shell additional steps might be required.
Non-interctive shell special treatment
Ssh must be allowed to set the environment
On the REMOTE_SERVER login as root user and edit /etc/ssh/sshd_config. Add:
Default is PermitUserEnvironment no, which means, regardless of who and how sets the environment, it just won’t work.
On REMOTE_SERVER for config to take effect, sshd must be restarted:
Validate new environment
Following will give you what you need:
ssh -i ~/.ssh/YOUR_PRIVATE_KEY YOUR_USER@REMOTE_SERVER "env"
1. Is the shell variable properly set. (SHELL=/bin/bash)?
2. Do you see your exported variables?
That’s it. Have fun!
INCOMING SEARCH TERMS