Setting ssh remote host environment variables (Interactive shell)

By neokrates, written on April 29, 2010

howto

View Counter:
  • 1,859 views
Rate it
  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
Ad
Poll
  • Your skill as shell programmer is?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext
Most popular search terms:

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:

[v] Ubuntu 9.10
[v] OpenSsh
[v] Bash

Should also work for:

[v] 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 yes

Default 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!

 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +3 (from 9 votes)
6 votes 'YES'  3 votes 'NO'


TAGS

RELATED
Pages
Posts
    nope :(

SOCIAL
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • BlinkList
  • Blogosphere News
  • E-mail this story to a friend!
  • Furl
  • LinkArena
  • Live
  • MisterWong
  • Print this article!
  • StumbleUpon
  • Technorati
  • Webnews.de
  • YahooMyWeb

INCOMING SEARCH TERMS


Leave a Reply