Setting ssh remote host environment variables (Interactive shell)By neokrates, written on April 29, 2010 |
howto |
- neokrates
- Email: uwarov@yahoo.com
- Website: http://www.thinkplexx.com
- Join date: 05-31-09
- Posts: 20
Best shells (or command line language) are?
- Bash (47%, 9 Votes)
- Different shell (21%, 4 Votes)
- Zsh (21%, 4 Votes)
- Csh (11%, 2 Votes)
- Power shell (5%, 1 Votes)
- Ksh (5%, 1 Votes)
- Lush (0%, 0 Votes)
- Quicksilver (0%, 0 Votes)
- IPython (0%, 0 Votes)
- Rush (0%, 0 Votes)
- Pash (0%, 0 Votes)
- Dos cl (0%, 0 Votes)
- Fish shell (0%, 0 Votes)
Total Voters: 19
Loading ...
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.
Software:
Ubuntu 9.10
OpenSsh
Bash
Should also work for:
Other Linux distros
| 1 |
Following applies to both interactive and non-interactive modes
| 1.1 |
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.
| 1.2 |
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:
source ~/.bashrc
Alternatively, you can make a symbolic link:
ln -s ~/.bashrc ~/.profile
| 1.3 |
Exporting variables
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.
| 2 |
Non-interctive shell special treatment
| 2.1 |
Ssh must be allowed to set the environment
Edit sshd_config
On the REMOTE_SERVER login as root user and edit /etc/ssh/sshd_config. Add:
PermitUserEnvironment yesDefault is PermitUserEnvironment no, which means, regardless of who and how sets the environment, it just won’t work.
Restart sshd
On REMOTE_SERVER for config to take effect, sshd must be restarted:
/etc/init.d/ssh restart
| 2.2 |
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!
|
LEARN MORE (amazon bookstore)
|
|
TAGS
|
|
RELATED
|
Pages
Posts
|
|
SOCIAL
|
|
INCOMING SEARCH TERMS
|


















