Pilou
Pilou

Reputation: 1478

Why my ~/.bashrc is sourced while I'm connected and not when I only execute a command?

I have a target AIX 6.1 server where I need to execute a script.

The ~/.bashrc on my target server is configured to add a variable MACHINE=1

When I ssh the server and connect I effectively view the ~/.bashrc variables in my env:

[user@source ~]$ ssh [email protected]
[user@target]
$env |grep "MACHINE"
MACHINE=1
[user@target]
$

But when I execute directly the env command in the ssh the variable is not set:

[user@source ~]$ ssh [email protected] "env" |grep MACHINE
[user@source ~]$

Is there something to configure more on the server?

Upvotes: 2

Views: 276

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295696

One option you have is to tell sshd to set the environment variables for you. In /etc/sshd_config:

PermitUserEnvironment yes

In ~/.ssh/environment:

MACHINE=1

Upvotes: 2

devnull
devnull

Reputation: 123608

ssh remote command execution happens in a non-interactive shell. As such, your ${HOME}/.bashrc, /etc/bashrc aren't read.

If you want ssh commands to read environment variables, add those to /etc/profile.

Alternatively, try hacks:

ssh [email protected] 'source ${HOME}/.bashrc; echo ${MACHINE}'

Upvotes: 2

Related Questions